function checkAll(ctl, selClass)
{		 
	if (selClass) selClass = "."+selClass;
	
	$(":checkbox"+selClass).each(function(i) 
	{		
		this.checked = ctl.checked;			
	});	
}
function appendTBPrint(jq_obj)
{	
	if (!jq_obj) jq_obj = window;
	
	var tb = $('.uitb_window');
	
	var buttons = tb.dialog('option', 'buttons');
	
	$.extend(buttons, 
	{
		'Print': function() 
				 { 
					//Find the table to be printed inside the object provided
					var content = $("body", jq_obj.document).parent().html();
					
					var win = window.open('','printWindow', 'width=1,height=1,screenX=0,left=0,screenY=0,top=0');
					
					$(document).focus();
					
					win.document.open();
					win.document.write("<html><head><link rel='stylesheet' type='text/css' href='themes/mri/style.css' />" +
									   "</head><body onload='window.print(); window.close();'>" + content + "</body></html>");
					win.document.close();					
					
					//win.close();
					//jq_obj.print(); 
				 }
	});
	
	tb.dialog('option', 'buttons', buttons);	
}
$(document).ready(function()
{
	var elem = document.createElement("a");	
	elem.href = '#';
	elem.innerHTML = "&nbsp;";
	
	$(elem).addClass('minMax').click(function(event) 
	 { 		
		 var col = $(this).data('collapsed');			 
		 if (col == null) col = false;
		 
		 if (!col) 
		 {			 
			 $(this).addClass("max").parent().children("span.toggle").show();			 
		 }
		 else
		 {			 
			 $(this).removeClass("max").parent().children("span.toggle").hide();
		 } 
		 
		 $(this).data('collapsed', !col);
		 
		 $(this).parent().siblings().slideToggle('normal');		 
		 
		 return false;		 
	 }).prependTo("h1.minimize");
	
	//Put a non-breaking space in every empty TD (there you go, Internet Exporer - happy now?)
	$("td:empty").html("&nbsp;");	
	
	$.post("instances.ajax",{action: "getContextHelp", url: window.location.href.split("/").pop()}, function(output)
	{		
		if (output.content)
		{			
			$("#headerBlock").append(output.content);
			
			$("#btnOpenHelp").addClass('ui-state-error').append(" (" + output.count + ")").button(
			{
				icons: 
				{
                	primary: "ui-icon-help",
                	secondary: "ui-icon-triangle-1-s"
				}				
            }).click(function() 
			{ 
            	//Next element is the topic container
            	var obj = $(this).next();
            	
				if (obj.is(":visible"))
				{
					obj.fadeOut('slow');
				}
				else
				{
					obj.fadeIn('normal');
					
					obj.position(
					{
						my: "right top",
						at: "right bottom",				
						of: "#btnOpenHelp",
						offset: "0 1"
					});
				}
			});
			
			$("#contextHelp button").button();
		}
	}, "json");
});

(function()
{
	jQuery.query = function() {
	    var r = {};
	    var params = location.search.replace(/^\?/,'').split('&');
	    for( var i = params.length-1;  i >= 0;  i-- ) {
	       var p = params[i].split('='), key = p[0];
	       if( key ) r[key] = p[1];
	    }
	    return r;
	 };
	 
	 $.fn.value = function(val){		
		 var obj = $(this).get(0);
		 
		 if (!obj) return $(this);
		 
		 if (obj.tagName.toLowerCase() == "select")
		 {
			 return obj.options[obj.selectedIndex].value;
		 }
		 else
		 {		 
			if (val !== undefined)
			{
				$(this).attr("value", val);
				return $(this);
			}
		
			return $(this).attr("value");
		 }
	 };
	 
	 $.fn.fadeInContent = function (content, cb){
	 	$(this).fadeOut('fast', function()
	 	{ 
	 		$(this).html(content); 
	 		if (cb) cb();
	 		$(this).fadeIn(350);
	 	});
	 	
	 	return this;
	 };
	 
	 /*$.fn.setWorking = function(strDesc)
	 {
		 if (!strDesc) strDesc = "Loading";
		 
		 $('div.workingDiv', this).remove();
		 
		 var content = "<center><img src=themes/controls/jquery/ajax_loader.gif /> :: " + strDesc + " :: <img src=themes/controls/jquery/ajax_loader.gif /></center>";
		 var wDiv = $("<div class='workingDiv'>"+content+"</div>");
		 
		 //wDiv.css("height", $(this).height());		 
		 $(this).children().hide();
		 $(this).prepend(wDiv)
		 
		 return this;
	 };*/
	 $.fn.setError = function(errorStr)
	 {
		 alert(errorStr);
		 $(this).html("<strong>Error loading data:</strong> " + errorStr);
	 }
	 $.fn.enable = function() { $(this).removeAttr("disabled").css("cursor","pointer"); return this; };	 
	 $.fn.disable = function() { $(this).attr("disabled", "disabled").css("cursor","not-allowed"); return this; };
	 $.fn.verifyForm = function(event, formField, submitParent)
	 {	 
		 var iArr = $(formField).value().split(";;");
		 
		 for (var e in iArr)
		 {			 
			 //Separate the name from the validation type
			 var tmp = iArr[e].split("::");
			 var regexp = new RegExp(tmp[1],"m");			 
			 
			 if (!regexp.test($("[name=\'" + tmp[0] + "\']").value().toLowerCase())) 
			 {
				 alert("Input field " + tmp[0] + " is not in the proper format, or is missing data. Please correct and try again.");
				 e.returnValue = false;
				 return false;
			 }
			 
		 }		 
		 return true;
	 }
	 $.fn.setWorking = function(label, delay){
		if (!label) label = "Loading ...";		
		$(this).each(function() {
			if(delay !== undefined && delay > 0) {
		        var element = $(this);
		        element.data("_mask_timeout", setTimeout(function() { $.maskElement(element, label)}, delay));
			} else {
				$.maskElement($(this), label);
			}
		});
	};
	
	/**
	 * Removes mask from the element(s). Accepts both single and multiple selectors.
	 */
	$.fn.clearWorking = function(){
		$(this).each(function() {
			$.unmaskElement($(this));
		});
	};
	
	/**
	 * Checks if a single element is masked. Returns false if mask is delayed or not displayed. 
	 */
	$.fn.isMasked = function(){
		return this.hasClass("masked");
	};

	$.maskElement = function(element, label){
	
		//if this element has delayed mask scheduled then remove it and display the new one
		if (element.data("_mask_timeout") !== undefined) {
			clearTimeout(element.data("_mask_timeout"));
			element.removeData("_mask_timeout");
		}

		if(element.isMasked()) {
			$.unmaskElement(element);
		}
		
		if(element.css("position") == "static") {
			element.addClass("masked-relative");
		}
		
		element.addClass("masked");
		
		var maskDiv = $('<div class="loadmask"></div>');
		
		//auto height fix for IE
		if(navigator.userAgent.toLowerCase().indexOf("msie") > -1){
			maskDiv.height(element.height() + parseInt(element.css("padding-top")) + parseInt(element.css("padding-bottom")));
			maskDiv.width(element.width() + parseInt(element.css("padding-left")) + parseInt(element.css("padding-right")));
		}
		
		//fix for z-index bug with selects in IE6
		if(navigator.userAgent.toLowerCase().indexOf("msie 6") > -1){
			element.find("select").addClass("masked-hidden");
		}
		
		element.append(maskDiv);
		
		if(label !== undefined) {
			var maskMsgDiv = $('<div class="loadmask-msg" style="display:none;"></div>');
			maskMsgDiv.append('<div>' + label + '</div>');
			element.append(maskMsgDiv);
			
			//calculate center position
			//maskMsgDiv.css("top", Math.round(element.height() / 2 - (maskMsgDiv.height() - parseInt(maskMsgDiv.css("padding-top")) - parseInt(maskMsgDiv.css("padding-bottom"))) / 2)+"px");
			maskMsgDiv.css("left", Math.round(element.width() / 2 - (maskMsgDiv.width() - parseInt(maskMsgDiv.css("padding-left")) - parseInt(maskMsgDiv.css("padding-right"))) / 2)+"px");
			
			maskMsgDiv.show();
		}
		
	};
	
	$.unmaskElement = function(element){
		//if this element has delayed mask scheduled then remove it
		if (element.data("_mask_timeout") !== undefined) {
			clearTimeout(element.data("_mask_timeout"));
			element.removeData("_mask_timeout");
		}
		
		element.find(".loadmask-msg,.loadmask").remove();
		element.removeClass("masked");
		element.removeClass("masked-relative");
		element.find("select").removeClass("masked-hidden");
	};
}) ();

function serialize(_obj)
{
   // Let Gecko browsers do this the easy way
   /*if (typeof _obj.toSource !== 'undefined' && typeof _obj.callee === 'undefined')
   {
      return _obj.toSource();
   }*/

   // Other browsers must do it the hard way
   switch (typeof _obj)
   {      
      case 'boolean':
    	  return _obj ? 1 : 0;
    	  break;
      case 'number':
      case 'function':
         return _obj;
         break;

      // for JSON format, strings need to be wrapped in quotes
      case 'string':
         return '"' + _obj + '"';
         break;

      case 'object':
         var str;
         if (_obj.constructor === Array || typeof _obj.callee !== 'undefined')
         {
            str = '[';
            var i, len = _obj.length;
            for (i = 0; i < len-1; i++) { str += serialize(_obj[i]) + ','; }
            str += serialize(_obj[i]) + ']';
         }
         else
         {
            str = '{';
            var key;
            for (key in _obj) { str += '"' + key + '":' + serialize(_obj[key]) + ','; }
            str = str.replace(/\,$/, '') + '}';
         }
         return str;
         break;

      default:
         return 'UNKNOWN';
         break;
   }
}
