/*
'**************************************************************************************************
'File Name:		_Common.js
'Object Type:	Javascript file
'
'Author Name:	Mark E. Watkins - Daugherty Systems, Inc.
'Creation Date:	10/23/2003
'
'Purpose:		Used as a library of functions and is included in any pages that need access
'				to it functions.
'
'
'Include Dependencies:
'None.
'
'
'Date Changed		Changed By		Reason
'--------------------------------------------------------------------------------------------------
'
'
'**************************************************************************************************
*/

//
//Variable declarations.
var msDisabledControlsSaved = "";	//Used in conjunction with the disableFormControls() function
									//to memorize which controls were legitimately disabled.
var mbDisabledMode = false;			//Used in conjunction with the disableFormControls() function
									//to track the mode of the disabling.  This will eliminate 
									//possible corruption of the 'msDisabledControlsSaved' variable.
//
//Constant declarations - function navigateMockDisabled().
var KEY_DELETE = 46;			//Delete key-code.
var KEY_TAB = 9;				//Tab key-code.
var KEY_F5 = 116;				//F5 key-code.
var KEY_HOME = 36;				//Home key-code.
var KEY_END = 35;				//End key-code.
var KEY_RIGHTARROW = 39;		//Right arrow key-code.
var KEY_LEFTARROW = 37;			//Left arrow key-code.

//Keycode constants.
var KEY_ENTER = 13;
var KEY_ZERO = 48;
var KEY_NINE = 57;
var KEY_DEC_PT = 46;
var KEY_UPPER_A = 65;
var KEY_UPPER_Z = 90;
var KEY_LOWER_A = 97;
var KEY_LOWER_Z = 122;
var KEY_FORWARD_SLASH = 47;
var KEY_DASH = 45;
var KEY_COLON = 58;
var KEY_UPPER_P = 80;
var KEY_LOWER_P = 112;
var KEY_UPPER_M = 77;
var KEY_LOWER_M = 109;


///////////////////////////////////////////////////////
//
// FUNCTIONS
//
///////////////////////////////////////////////////////
function countOf( psValue, psChar ) { 
	var iStart = psValue.indexOf(psChar);
	var iCount = (iStart > -1)? 1: 0;
	
	if( iCount > 0 ) { 
		while(psValue.indexOf(psChar, iStart) > -1) { 
			iCount++;
			iStart = psValue.indexOf(psChar, iStart) + 1;
		}
	}
	return iCount;
}
function navigateMockDisabled() { 
	//
	//Purpose:
	//This function is used to suppress certain keystrokes on
	//controls that simulate a disabled state.
	//
	//Keystrokes to allow are:
	//	Tab, F5, Home, End, Right Arrow, and, Left Arrow.
	//
	var iKey = event.keyCode;

	return ( iKey == KEY_TAB || iKey == KEY_F5 || iKey == KEY_HOME || 
		iKey == KEY_END || iKey == KEY_RIGHTARROW || iKey == KEY_LEFTARROW )
}
function rTrim( obj ) { 
	//
	//Purpose:
	//Trims the trailing spaces of an objects value.
	//
	
	while(''+obj.charAt(obj.length-1)==' ')
		obj=obj.substring(0,obj.length-1);
	//
	return obj;
}
function isEmpty( sValue ) {
	//
	//Purpose:
	//Returns whether or not 'sValue' is null, undefined or '' (an empty string).
	//
	return ( (sValue == null) || (sValue == undefined) || (sValue.length == 0) );
}
function setCookie( sCookieName, sCookieValue, sCookieDuration ) { 
	var sCookie = sCookieName + "=" + escape(sCookieValue) + ";EXPIRES=" + getExpirationDate(sCookieDuration);

	document.cookie = sCookie;
	//
	if ( !getCookie(sCookieName) ) 
		return false;
	else
		return true;
}
function getCookie( sCookieName ) { 
	var sCookie = "" + document.cookie; 
	var iStartIndex = sCookie.indexOf(sCookieName); 
	
	if ( iStartIndex == -1 || sCookieName == "" ) return ""; 
	
	var iEndIndex = sCookie.indexOf(';', iStartIndex); 
	
	if ( iEndIndex == -1 ) iEndIndex = sCookie.length; 
	
	return unescape(sCookie.substring(iStartIndex + sCookieName.length+1, iEndIndex));
}
function getExpirationDate( iNumDays ) { 
	if ( typeof iNumDays != "number" || iNumDays == undefined ) return "Thu, 01-Jan-70 00:00:01 GMT";
	
	var sUTCstring;
	var sToday = new Date();
	var iMillisecs = Date.parse(sToday);

	sToday.setTime(iMillisecs + iNumDays * 24 * 60 * 60 * 1000);
	//
	sUTCstring = sToday.toUTCString();
	//
	return sUTCstring;
}
//
//
//
function datePopup_onclick( control, callIsDirty, functionToCall, functionToCallWithParams ) {
	if( callIsDirty == undefined || callIsDirty == null ) callIsDirty = true;
	//alert(functionToCallWithParams);
	
	var prevVal = control.value;
	
	displayCalendar(control, true);
	if( callIsDirty ) {
		if( control.value != prevVal )
			isDirty();
	}
	//
	if( functionToCall != undefined && functionToCall != null ) eval(functionToCall + "();");
	//
	if( functionToCallWithParams != undefined && functionToCallWithParams != null ) eval(functionToCallWithParams + ";");
}
function currencyEntry_onkeypress( control ) {
	//
	if( event.keyCode == KEY_DEC_PT && countOf(control.value, ".") > 1 ) event.returnValue = false;
	if( (event.keyCode < KEY_ZERO || event.keyCode > KEY_NINE) && event.keyCode != KEY_DEC_PT ) event.returnValue = false;
}
function numericEntry_onkeypress() {
	//
	if( event.keyCode < KEY_ZERO || event.keyCode > KEY_NINE ) event.returnValue = false;
}
function currencyEntry_onblur( control ) {
	//
	if( control.value.length == 0 )
		control.value = "0.00";
}
function numericEntry_onblur( control ) {
	//
	if( control.value.length == 0 )
		control.value = "0";
}

function alphaEntry_onkeypress( control ) {
	//
	if( !((event.keyCode >= KEY_UPPER_A && event.keyCode <= KEY_UPPER_Z) || (event.keyCode >= KEY_LOWER_A && event.keyCode <= KEY_LOWER_Z)) ) event.returnValue = false;
}

function alphaEntryPlus_onkeypress( control ) {
	//
	if( event.keyCode >= KEY_UPPER_A && event.keyCode <= KEY_UPPER_Z ) event.returnValue = true;
	else if( event.keyCode >= KEY_LOWER_A && event.keyCode <= KEY_LOWER_Z ) event.returnValue = true;
	else if( event.keyCode == KEY_DEC_PT ) event.returnValue = true;
	else if( event.keyCode >= KEY_ZERO || event.keyCode <= KEY_NINE ) event.returnValue = true;
	else if( event.keyCode == KEY_DASH ) event.returnValue = true;
	else event.returnValue = false;
	
}

function dateEntry_onkeypress( control ) {
	//
	if( countOf(control.value, "-") > 1 && event.keyCode == KEY_FORWARD_SLASH ) event.returnValue = false;
	if( countOf(control.value, "/") > 1 && event.keyCode == KEY_DASH ) event.returnValue = false;
	if( (event.keyCode < KEY_ZERO || event.keyCode > KEY_NINE) && event.keyCode != KEY_FORWARD_SLASH && event.keyCode != KEY_DASH ) event.returnValue = false;
}
function dateEntryRange_onkeypress( control ) {
	//
	if( countOf(control.value, "-") > 1 && event.keyCode == KEY_FORWARD_SLASH ) event.returnValue = false;
	if( countOf(control.value, "/") > 1 && event.keyCode == KEY_DASH ) event.returnValue = false;
	if( (event.keyCode < KEY_ZERO || event.keyCode > KEY_NINE) && event.keyCode != KEY_FORWARD_SLASH && event.keyCode != KEY_DASH && event.keyCode != KEY_DEC_PT ) event.returnValue = false;
}
function dateEntry_onchange( control, callIsDirty ) {
	var datesSplit = [];
	var firstGood = true;
	var secondGood = true;
	
	if( control.value.indexOf("...") > -1 ) {
		datesSplit = control.value.split("...");
		
		if( !isDate(datesSplit[0]) )
			firstGood = false;
		else
			datesSplit[0] = formatDate(datesSplit[0], true, true, "/", true);
		//
		if( !isDate(datesSplit[1]) )
			secondGood = false;
		else
			datesSplit[1] = formatDate(datesSplit[1], true, true, "/", true);
		//
		if( !firstGood && !secondGood ) {
			alert("Both dates entered (in the date range) are invalid.");
			control.value = "...";
		}
		else if( firstGood && !secondGood ) {
			alert("The second date entered (in the date range) is invalid.");
			control.value = datesSplit[0] + "...";
		}
		else if( !firstGood && secondGood ) {
			alert("The first date entered (in the date range) is invalid.");
			control.value = "..." + datesSplit[1];
		}
		if( !firstGood || !secondGood )
			event.returnValue = false;
		else
			control.value = datesSplit[0] + "..." + datesSplit[1];
	}
	else {
		if( !isDate(control.value) ) {
			alert("The date you entered is invalid.");
			control.value = "";
			event.returnValue = false;
		}
		else {
			control.value = formatDate(control.value, true, true, "/", true);
		}
	}
}

function zipcodeEntry_onkeypress( control ) {
	//
	if( (event.keyCode < KEY_ZERO || event.keyCode > KEY_NINE) && (event.keyCode != KEY_DASH || countOf(control.value, "-") > 1) ) event.returnValue = false;
}

function timeEntry_onkeypress( control ) {
	//
	if( (event.keyCode < KEY_ZERO || event.keyCode > KEY_NINE) 
		&& (event.keyCode != KEY_UPPER_A && event.keyCode != KEY_LOWER_A 
		&&  event.keyCode != KEY_UPPER_P && event.keyCode != KEY_LOWER_P 
		&&  event.keyCode != KEY_UPPER_M && event.keyCode != KEY_LOWER_M 
		&&  countOf(control.value, ":") > 1) ) event.returnValue = false;
	if( event.keyCode == KEY_UPPER_A && countOf(control.value, "A") > 1 ) event.returnValue = false;
	if( event.keyCode == KEY_LOWER_A && countOf(control.value, "a") > 1 ) event.returnValue = false;
	if( event.keyCode == KEY_UPPER_P && countOf(control.value, "P") > 1 ) event.returnValue = false;
	if( event.keyCode == KEY_LOWER_P && countOf(control.value, "p") > 1 ) event.returnValue = false;
	if( event.keyCode == KEY_UPPER_M && countOf(control.value, "M") > 1 ) event.returnValue = false;
	if( event.keyCode == KEY_LOWER_M && countOf(control.value, "m") > 1 ) event.returnValue = false;
}

function phoneEntry_onkeypress( control ) {
	//
	if( (event.keyCode < KEY_ZERO || event.keyCode > KEY_NINE) && event.keyCode != KEY_DASH ) event.returnValue = false;
}
function phoneEntry_onchange( control ) {
	var dataLength =control.value.length;
	var dataValue = control.value;
	var dataValueClean = "";
	var dataValueFormatted = "";
	
	if( dataLength > 0 )
	{
		for ( var i = 0; i < dataLength; i++ )
		{
			if( "0123456789".indexOf(dataValue.substr( i, 1 ) ) >= 0 )
			{
				dataValueClean += dataValue.substr( i, 1 )
			}
		}
		dataLength = dataValueClean.length;
		if( dataLength > 0 )
		{
			for ( var i = 0; i < dataLength; i++ )
			{
				dataValueFormatted += dataValueClean.substr( i, 1 )
				if( i == 2 || ( i == 5 && dataLength == 10 ) ) { dataValueFormatted += "-"; }
				if( i == 10 ) { dataValueFormatted += " "; }
			}
			control.value = dataValueFormatted;
		}
	}
}
//eof: Mark E. Watkins