function disableButton(dialogWindowId, buttonText) {
	$('#' + dialogWindowId).parent().find("button").each(function() {
        if( $(this).text() == buttonText ) {
            $(this).attr('disabled', true);
        }
    });
}

function addRemoveClass(element, classToAdd,classToRemove) {
	element.removeClass(classToRemove);
	element.addClass(classToAdd);
}

function toggleInput(element) {
	if ($('#' + element).attr('checked')) {
		$('#' + element).removeAttr('checked');
	} else {
		$('#' + element).attr('checked','true');
	}
}

/** JQuery plugin which allows table header values to remain in a fixed position as the user scrolls down the page. */
jQuery.fn.fixedTableHeader = function(options) { 
	var settings = jQuery.extend({ 
		headerrowsize: 1, 
		highlightrow: false, 
		highlightclass: "highlight" 
	}, options); 
	
	this.each(function(i) { 
		var $tbl = $(this); 
		var $tblhfixed = $tbl.find("tr:lt(" + settings.headerrowsize + ")"); 
		var headerelement = "th"; 
		if ($tblhfixed.find(headerelement).length == 0) {
			headerelement = "td"; 
		}
		
		if ($tblhfixed.find(headerelement).length > 0) { 
			$tblhfixed.find(headerelement).each(
				function() { 
					$(this).css("width", $(this).width()); 
				}); 
				
			var $clonedTable = $tbl.clone().empty(); 
			var tblwidth = GetTblWidth($tbl); 
			
			$clonedTable.attr("id", "fixedtableheader" + i).css(
				{ "position": "fixed", "top": "0", "left": $tbl.offset().left })
				.append($tblhfixed.clone()).width(tblwidth).hide()
				.appendTo($("body")); 
				
			if (settings.highlightrow) {
				$("tr:gt(" + (settings.headerrowsize - 1) + ")", $tbl).hover(
					function() { 
						$(this).addClass(settings.highlightclass); }, 
					function() { 
						$(this).removeClass(settings.highlightclass); }); 
			}
			
			$(window).scroll(
				function() { 
					if (jQuery.browser.msie && jQuery.browser.version == "6.0") {
						$clonedTable.css(
							{ "position": "absolute", "top": $(window).scrollTop(), "left": $tbl.offset().left }); 
					} else {
						$clonedTable.css(
							{ "position": "fixed", "top": "0", "left": $tbl.offset().left - $(window).scrollLeft() }); 
					}
					var sctop = $(window).scrollTop(); 
					var elmtop = $tblhfixed.offset().top; 
					if (sctop > elmtop && sctop <= (elmtop + $tbl.height() - $tblhfixed.height())) {
						$clonedTable.show(); 
					} else {
						$clonedTable.hide(); 
					}
				}); 
			$(window).resize(function() { 
				if ($clonedTable.outerWidth() != $tbl.outerWidth()) { 
					$tblhfixed.find(headerelement).each(
						function(index) { 
							var w = $(this).width(); 
							$(this).css("width", w); 
							$clonedTable.find(headerelement).eq(index).css("width", w); 
						}); 
					$clonedTable.width($tbl.outerWidth()); 
				} 
				$clonedTable.css("left", $tbl.offset().left); 
			}); 
		} 
	}); 
	
	function GetTblWidth($tbl) { 
		var tblwidth = $tbl.outerWidth(); 
		return tblwidth; 
	} 
};
      
/** 
 * JQuery Escape function which replaces JQuery reserved meta-characters within the supplied string with a backslash + the character. 
 * The full list of meta-characters reserved within JQuery are:  #;&,.+*~':"!^$[]()=>|/ 
 */
function jEscape(str) {
	str = str.replace(/\+/g,"\\+");
	str = str.replace(/\\/g,"\\");
	str = str.replace(/\//g,"\\/");
	str = str.replace(/!/g,"\\!");
	str = str.replace(/"/g,'\\"');
	str = str.replace(/#/g,"\\#");
	str = str.replace(/\$/g,"\\$");
	str = str.replace(/%/g,"\\%");
	str = str.replace(/&/g,"\\&");
	str = str.replace(/'/g,"\\'");
	str = str.replace(/\(/g,"\\(");
	str = str.replace(/\)/g,"\\)");
	str = str.replace(/\*/g,"\\*");
	str = str.replace(/,/g,"\\,");
	str = str.replace(/\./g,"\\.");
	str = str.replace(/:/g,"\\:");
	str = str.replace(/;/g,"\\;");
	str = str.replace(/\?/g,"\\?");
	str = str.replace(/@/g,"\\@");
	str = str.replace(/\[/g,"\\[");
	str = str.replace(/\]/g,"\\]");
	str = str.replace(/\^/g,"\\^");
	str = str.replace(/`/g,"\\`");
	str = str.replace(/\{/g,"\\{");
	str = str.replace(/\}/g,"\\}");
	str = str.replace(/\|/g,"\\|");
	str = str.replace(/\~/g,"\\~");
	
	return str;
}

function createJQueryDownArrowButton(input) {
	var btn = $( "<button type=\"button\">&nbsp;</button>" )
		.attr( "tabIndex", -1 )
		.attr( "title", "Show All Items" )
		.insertAfter( input )
		.button({
			icons: {
				primary: "ui-icon-triangle-1-s"
			},
			text: false
		})
		.removeClass( "ui-corner-all" )
		.addClass( "ui-corner-right ui-button-icon" )
		.click(function() {
			// close if already visible
			if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
				input.autocomplete( "close" );
				return;
			}
	
			// pass empty string as value to search for, displaying all results
			input.autocomplete( "search", "" );
			input.focus();
		});
	return btn;
}

