/**** common functions shortened (most functions are named the same as the asp versions) ****/
function dw(str) { document.write(str); }
function rr(url) { window.location.href=url; }
function rq(varName) {
	var qs = window.location.search;
	var rexp = new RegExp("("+varName+"=)","i");
	if ( varName == 0 ) { return qs; } // returns entire query string starting with ?
	else if ( qs.search(rexp) == -1 ) { return false; } // can't find this variable name in query string
	else {
		rexp.compile("([\?|&]"+varName+"=)([^&]*)","gi");
		var arVal = qs.match(rexp); // returns an array of matching name=value pairs, i.e. ["test=1","test=2","test=3"]
		var thisStr = "";
		for (i in arVal) { // loop through previous compiled array
			thisStr = arVal[i].split("="); // split each name=value pair at =
			arVal[i] = thisStr[1]; // reassign only the value to this spot in array
		}
		return arVal;
	}
}



/**** variables to use with DHTML objects ****/
	var isDOM = (document.getElementById ? true : false); 
	var isIE4 = ((document.all && !isDOM) ? true : false);
	var isNS4 = (document.layers ? true : false);

	function getRef(id) {
		if (isDOM) return document.getElementById(id);
		if (isIE4) return document.all[id];
		if (isNS4) return document.layers[id];
	}
	function getSty(id) {
		return (isNS4 ? getRef(id) : getRef(id).style);
	} 
//------------------------------------------------------------------------------------------------------------------------------------------------------------

 

/**** this function brings up a confirmation alert when deleting line item ****/
function confirmDelete(formName) {
	var thisForm = eval("document.forms." + formName);
	if( !checkAny(formName) ) {
		window.alert("You must check at least one box in the Delete column.");
		return false;
	}
	else if( window.confirm("Are You Sure You Want To Delete These Items?\nThis Action Cannot Be Undone.") ) {
		return true;
	}
	else { return false; }
}

/**** called from confirmDelete, returns false if no boxes are checked ****/
function checkAny(formName) {
	var thisForm = eval("document.forms." + formName);
	var checkedFlag = false;
	if( thisForm.deleteItem.length == "undefined" || isNaN(thisForm.deleteItem.length) && thisForm.deleteItem.checked ) {
		checkedFlag = true;
	}
	else {
		for(var i = 0; i <= thisForm.deleteItem.length-1; i++) {
			if( thisForm.deleteItem[i].checked == true ) {
				checkedFlag = true;
				break;
			}
		}
	}
	return checkedFlag;
}

/**** checks all delete check boxes ****/
function checkAll(formName) {
	var thisForm = eval("document.forms." + formName);

	if( thisForm.deleteItem.length == "undefined" || isNaN(thisForm.deleteItem.length) && !thisForm.deleteItem.checked ) {
		thisForm.deleteItem.checked = true;
	}
	else {
		for(var i = 0; i <= thisForm.deleteItem.length-1; i++) {
			thisForm.deleteItem[i].checked = true;
		}
	}
}

/**** deselects all delete check boxes ****/
function checkNone(formName) {
	var thisForm = eval("document.forms." + formName);
	if( thisForm.deleteItem.length == "undefined" || isNaN(thisForm.deleteItem.length) && thisForm.deleteItem.checked ) {
		thisForm.deleteItem.checked = false;
	}
	else {
		for(var i = 0; i <= thisForm.deleteItem.length-1; i++) {
			thisForm.deleteItem[i].checked = false;
		}
	}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------


/**** focuses input onload ****/
function defaultFocus(formRef,inputRef) {
	eval('document.forms['+formRef+'].'+inputRef+'.focus()');
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------


/**** window dimensions ****/
function winWidth() {
	if (window.innerWidth) { return window.innerWidth; }
	else if (window.outerWidth) { return window.outerWidth; }
	else if (window.width) { return window.width; }
	else { return 790 } // older browser, assume they are in 800 x 600 resolution
}
function winHeight() {
	if (window.innerHeight) { return window.innerHeight; }
	else if (window.outerHeight) { return window.outerHeight; }
	else if (window.height) { return window.height; }
	else { return 580 } // older browser, assume they are in 800 x 600 resolution
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------


/**** timeout function ****/

// redirect after session timed out
timedouturl = "components/logout.asp?timeout=1";
confirmBoxID = 'timerid';

function Minutes(data) {
	for (var i = 0; i < data.length; i++) {
		if (data.substring(i, i + 1) == ":") { break; }
	}
	return (data.substring(0, i));
}
function Seconds(data) {
	for (var i = 0; i < data.length; i++) {
		if (data.substring(i, i + 1) == ":") { break; }
	}
	return (data.substring(i + 1, data.length));
}
function Display(min, sec) {
	var disp;
	if (min <= 9) { disp = " 0"; }
	else { disp = " "; }

	disp += min + ":";
	
	if (sec <= 9) { disp += "0" + sec; }
	else { disp += sec; }
	return (disp);
}
function Down() { 
	sec--;      
	if (sec == -1) { sec = 59; min--; }
//	document.forms.timerform.clock.value = Display(min, sec);
	window.status = "Session will time out in: " + Display(min, sec);
//	if (min <= 4 && getSty(confirmBoxID).visibility != 'visible') {
//		getSty(confirmBoxID).visibility = 'visible';
//	}
	
	if (min == 0 && sec <= 0) { window.location.href = timedouturl; }
	else { down = setTimeout("Down()", 1000); }
}

function timeIt() {
	min = 240;
	sec = 0;
	Down();
}

function writeConfirmBox() {
	var boxWidth = 260;
	var winX,winY,thisStyle;
		winX = Math.round( (winWidth() - boxWidth)/2 );
		if (screen.availHeight > 500) {
			winY = 150;
		}
		else { winY = Math.round( (winHeight/2) - 50 ); }
		
	var str = '<div class="timeout" style="width:'+boxWidth+'px;left:'+winX+'px;top:'+winY+'px" align="center" id="'+confirmBoxID+'">'+
		'<div class="timeoutTitle">'+
		'<form name="timerform">Your have been idle for 15 min.<br>Your session will end in<input type="text" name="clock" size="5" class="clock">min.<br>'+
		'Do you wish to stay logged in?</form>'+
		'</div>'+
		'<div style="padding:10px"><input type="button" name="ok" value="OK" onclick="window.location.href=window.location.href;" class="but_bg">&nbsp;'+
		'<input type="button" name="no" value="LOG OFF" onclick="window.location.href=\''+timedouturl+'\';" class="but_bg"></div>'+
		'</div>';
		document.write(str);

		getSty(confirmBoxID).visibility = 'hidden';
		
}

/**** place this in onload event ****/
/**** OnLoad="timeIt()" ****/
//------------------------------------------------------------------------------------------------------------------------------------------------------------






/**** this function brings up a confirmation alert when deleting line item ****/
function confirmDelete(formName) {
	var thisForm = eval("document.forms." + formName);
	if( !checkAny(formName) ) {
		window.alert("You must check at least one box in the Delete column.");
		return false;
	}
	else if( window.confirm("Are You Sure You Want To Delete These Items?\nThis Action Cannot Be Undone.") ) {
		return true;
	}
	else { return false; }
}

/**** called from confirmDelete, returns false if no boxes are checked ****/
function checkAny(formName) {
	var thisForm = eval("document.forms." + formName);
	var checkedFlag = false;
	if( thisForm.deleteItem.length == "undefined" || isNaN(thisForm.deleteItem.length) && thisForm.deleteItem.checked ) {
		checkedFlag = true;
	}
	else {
		for(var i = 0; i <= thisForm.deleteItem.length-1; i++) {
			if( thisForm.deleteItem[i].checked == true ) {
				checkedFlag = true;
				break;
			}
		}
	}
	return checkedFlag;
}

/**** checks all delete check boxes ****/
function checkAll(formName) {
	var thisForm = eval("document.forms." + formName);

	if( thisForm.deleteItem.length == "undefined" || isNaN(thisForm.deleteItem.length) && !thisForm.deleteItem.checked ) {
		thisForm.deleteItem.checked = true;
	}
	else {
		for(var i = 0; i <= thisForm.deleteItem.length-1; i++) {
			thisForm.deleteItem[i].checked = true;
		}
	}
}

/**** deselects all delete check boxes ****/
function checkNone(formName) {
	var thisForm = eval("document.forms." + formName);
	if( thisForm.deleteItem.length == "undefined" || isNaN(thisForm.deleteItem.length) && thisForm.deleteItem.checked ) {
		thisForm.deleteItem.checked = false;
	}
	else {
		for(var i = 0; i <= thisForm.deleteItem.length-1; i++) {
			thisForm.deleteItem[i].checked = false;
		}
	}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------


/**** focuses input onload ****/
function defaultFocus(formRef,inputRef) {
	eval('document.forms['+formRef+'].'+inputRef+'.focus()');
}


//------------------------------------------------------------------------------------------------------------------------------------------------------------


/**** window dimensions ****/
function winWidth() {
	if (window.innerWidth) { return window.innerWidth; }
	else if (window.outerWidth) { return window.outerWidth; }
	else if (window.width) { return window.width; }
	else { return 790 } // older browser, assume they are in 800 x 600 resolution
}
function winHeight() {
	if (window.innerHeight) { return window.innerHeight; }
	else if (window.outerHeight) { return window.outerHeight; }
	else if (window.height) { return window.height; }
	else { return 580 } // older browser, assume they are in 800 x 600 resolution
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------


/**** timeout function ****/

// redirect after session timed out
timedouturl = "logout.asp?timeout=1";
confirmBoxID = 'timerid';

function Minutes(data) {
	for (var i = 0; i < data.length; i++) {
		if (data.substring(i, i + 1) == ":") { break; }
	}
	return (data.substring(0, i));
}
function Seconds(data) {
	for (var i = 0; i < data.length; i++) {
		if (data.substring(i, i + 1) == ":") { break; }
	}
	return (data.substring(i + 1, data.length));
}
function Display(min, sec) {
	var disp;
	if (min <= 9) { disp = " 0"; }
	else { disp = " "; }

	disp += min + ":";
	
	if (sec <= 9) { disp += "0" + sec; }
	else { disp += sec; }
	return (disp);
}
function Down() { 
	sec--;      
	if (sec == -1) { sec = 59; min--; }
//	document.forms.timerform.clock.value = Display(min, sec);
	window.status = "Session will time out in: " + Display(min, sec);
//	if (min <= 4 && getSty(confirmBoxID).visibility != 'visible') {
//		getSty(confirmBoxID).visibility = 'visible';
//	}
	
	if (min == 0 && sec <= 0) { window.location.href = timedouturl; }
	else { down = setTimeout("Down()", 1000); }
}

function timeIt() {
	min = 240;
	sec = 0;
	Down();
}

function writeConfirmBox() {
	var boxWidth = 300;
	var winX,winY,thisStyle;
		winX = Math.round( (winWidth() - boxWidth)/2 );
		if (screen.availHeight > 500) {
			winY = 300;
		}
		else { winY = Math.round( (winHeight/2) - 50 ); }
		
	var str = '<div class="timeout" style="width:'+boxWidth+'px; position:absolute; height:300px; font-family: Arial, Helvetica, sans-serif; font-size:12px; color:#000099; background: #fff; border: 5px solid navy; left:'+winX+'px;top:'+winY+'px" align="center" id="'+confirmBoxID+'">'+
		'<div style="margin-top:75px;" class="timeoutTitle">'+
		'<br><form name="timerform">Your have been idle for 30 minutes.<br>Your session will end in<input type="text" name="clock" size="5" class="clock">min.<br>'+
		'Do you wish to stay logged in?</form>'+
		'</div>'+
		'<div style="padding:10px"><input type="button" name="ok" value="OK" onclick="window.location.href=window.location.href;" class="but_bg">&nbsp;'+
		'<input type="button" name="no" value="LOG OFF" onclick="window.location.href=\''+timedouturl+'\';" class="but_bg"></div>'+
		'</div>';
		document.write(str);

		getSty(confirmBoxID).visibility = 'hidden';
		
}

/**** place this in onload event ****/
/**** OnLoad="timeIt()" ****/
//------------------------------------------------------------------------------------------------------------------------------------------------------------



