// 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.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;
	}

	if(digits.length == 4) {
		field.value = "" + digits[0] + digits[1] + ":" + digits[2] + digits[3];
	} else if(digits.length == 3) {
		field.value = "0" + digits[0] + ":" + digits[1] + digits[2];
	} else if(digits.length == 2) {
		field.value = "00:" + digits[0] + digits[1];
	} else if(digits.length == 1) {
		field.value = "00:0" + digits[0];
	} else
		field.value = "00:00";
}

// Prevents a length greater than 4.
// Prevents entering anything except 0-9 into the textbox.
function NumericalOnly(evt, field) {
	var charCode = (evt.which) ? evt.which : evt.keyCode;

	if(charCode == 13)
		ApplyTimeSpanFormat(field);
	else if(evt.currentTarget.value.length == 4)
		return false;
	else if(charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	return true;
}

//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() {
	$("div.ewfProcessing.ewfDialog > div.ewfTimeOut")[0].style.display = 'none';
	$("div.ewfProcessing").fadeIn(0);
	setTimeout('$("div.ewfProcessing.ewfDialog > div.ewfTimeOut").fadeIn(0);', 10000);
}

function stopPostBackRequest() {
	$("div.ewfProcessing").each(function(i) { this.style.display = 'none'; });
	if(window.stop)
		window.stop(); // Firefox
	else
		document.execCommand('Stop'); // IE
}