// This supports the DisplayLinking subsystem.
function setElementDisplay(id, visible) {
	var e = document.getElementById(id);
	e.style.display = (visible ? '' : 'none');
}
function toggleElementDisplay(id) {
	var e = document.getElementById(id);
	e.style.display = (e.style.display != 'none' ? 'none' : '');
}

function changeCheckBoxColor(control) {
	if(control.checked) {
		control.parentNode.parentNode.parentNode.parentNode.parentNode.className = 'checkedChecklistCheckboxDiv';
	}
	else {
		control.parentNode.parentNode.parentNode.parentNode.parentNode.className = '';
	}
}

function getClientUtcOffset(id) {
	var utcOffset = $get(id);
	var timeString = new Date().toUTCString();
	utcOffset.value = timeString;
}

// Supports DurationPicker
// Formats numbers entered in the textbox to HH:MM and prevents input out of the range of TimeSpan.
function ApplyTimeSpanFormat(field) {
	var digits = field.value.replace(":", "").split("");

	if(digits.length > 1 && digits[digits.length - 2] > 5) {
		digits[digits.length - 2] = 5;
		digits[digits.length - 1] = 9;
	}
	if(digits.length == 4) {
		if(digits[0] > 2) {
			digits[0] = 2;
			digits[1] = 3;
		} else if(digits[0] == 2 && digits[1] > 3)
			digits[1] = 3;
	}

	switch(digits.length) {
		case 4:
			field.value = "" + digits[0] + digits[1] + ":" + digits[2] + digits[3];
			break;
		case 3:
			field.value = "0" + digits[0] + ":" + digits[1] + digits[2];
			break;
		case 2:
			field.value = "00:" + digits[0] + digits[1];
			break;
		case 1:
			field.value = "00:0" + digits[0];
			break;
		default:
			field.value = "00:00";
	}
}

// Returns true if...
// Key pressed is a command or function key
// There is a selection or Length is <= 4 and
// Key pressed is numerical
function NumericalOnly(evt, field) {

	if(evt.ctrlKey || evt.altKey)
		return true;

	var charCode = (evt.which) ? evt.which : evt.keyCode;

	switch(charCode) {
		//Enter
		case 13: ApplyTimeSpanFormat(field);
			//Backspace
		case 8:
			//Ctrl
		case 9:
			//Alt
		case 18:
			//Home, End
		case 35:
		case 36:
			//Arrow Keys
		case 37:
		case 38:
		case 39:
		case 40:
			//Delete
		case 46:
			//Function Keys
		case 112:
		case 113:
		case 114:
		case 115:
		case 116:
		case 117:
		case 118:
		case 119:
		case 120:
		case 121:
		case 122:
		case 123:
			return true;
		default:
			//Max of 4 digits, numbers only.
			return ($(field).getSelection().text != "" || field.value.length < 4) && (charCode >= 48 && charCode <= 57);
	}
}

//This function gets called by jQuery's on-document-ready event. This will run the following code after the page has loaded.
function OnDocumentReady() {
	ApplyEwfTextBoxEventClassChangeActions();
	RemoveClickScriptBinding();
}

//Finds all EwfTextBoxes and appends onfocus and onblur events to apply focus CSS styles.
function ApplyEwfTextBoxEventClassChangeActions() {
	$(".textBoxWrapper").find("input").focus(
			function () {
				$(this.parentNode).addClass('textBoxWrapperFocused');
			}
		).blur(
			function () {
				$(this.parentNode).removeClass('textBoxWrapperFocused');
			}
		);
}

//Used for dynamic tables
//Finds ewfClickable rows that are also selectable, altering the JavaScript
//to allow them to be clickable without firing when selected.
function RemoveClickScriptBinding() {
	//Clickable Rows
	$("tr.ewfClickable").each(
			function () {
				//If this row doesn't contain notClickables, don't bother it
				if($(this).find(".ewfNotClickable").length == 0)
					return;
				//Grab the clickscript we want to apply
				var clickScript = eval($(this).attr("onclick"));
				//Unbind it from the row
				$(this).removeAttr("onclick");
				//For each td
				$(this).children(":not(.ewfNotClickable)").click(clickScript);
			}
    );
}

function postBackRequestStarted() {
	if(Sys.WebForms.PageRequestManager.getInstance()._postBackSettings.async == false) {
		$("div.ewfProcessing.ewfDialog > div.ewfTimeOut")[0].style.display = 'none';
		$("div.ewfProcessing").fadeIn(0);
		setTimeout('$("div.ewfProcessing.ewfDialog > div.ewfTimeOut").fadeIn(0);', 10000);
	} else {
		var uniqueId = "a" + new Date().getTime();
		$(".ajaxLoading[postbackowner=" + $("[name^='" + $("#__EVENTTARGET").val() + "']").attr('id') + "]").html(
		'<span class="ajaxLoadingMessage" id="' + uniqueId + '" style="display: none;"><img src="' + $('.ajaxloaderImage').attr('src') + '" style="display: inline;" /> <span>Loading...</span></span>');
		$('#' + uniqueId + '').fadeIn(2000);
	}
}

function stopPostBackRequest() {
	$("div.ewfProcessing").each(function (i) { this.style.display = 'none'; });
	if(window.stop)
		window.stop(); // Firefox
	else
		document.execCommand('Stop'); // IE
}