/*
Description:    Used to trap keystrokes that are not allowed in the application (e.g. Ctrl-N)
*/
var aDisallowedCtrlKeys = new Array();
aDisallowedCtrlKeys[0] = '78';  /* Ctrl-N */

document.onkeydown = function(e) { // needs updated.
    var bReturnVal = true;
   
    try{
        if ( ( window.event.ctrlKey ) && ( window.event.keyCode == 78 ) )
        {
            for ( var i = 0; ( i < aDisallowedCtrlKeys.length ) && ( bReturnVal ); i++ )
            {
                if ( window.event.keyCode.toString() == aDisallowedCtrlKeys[i] ) bReturnVal = false;
            }
        }
    }catch(ex){;}
    
    return bReturnVal;
}

// returns key code for pressed key
//
function getPressedKey(e){
    if(e){
        if(typeof(e.charCode) == 'number')
            return e.charCode;
        else if(typeof(e.which) == 'number') 
            return e.which;
        else if(typeof(e.keyCode) == 'number')
            return e.keyCode;
        else 
            return;
    }
    else{
        if(window.event) 
            return window.event.keyCode;
        else 
            return;
    }
}

/*
'Javascript that makes the a button be automatically clicked when user hits the "enter" key.
document.onkeypress = function() {
   if ( window.event.keyCode == 13 ) {
       event.returnValue=false;
       event.cancel=true;
       document.getElementById('<some button>').click();
   }
}
*/

/*****************************************************************************************************************************
*****************************************************************************************************************************
*****************************************************************************************************************************
*****************************************************************************************************************************
*****************************************************************************************************************************/

//--------------------------
////////////////////////////////////////////////////////////////////////////////
//		   Name:	EnablePageValidators
//	 Parameters:	sAttribute - specific attribute to look for on a validator
//						control, or blank to handle all validators
//					bEnabled - enable/disable validator control(s)
//	    Returns:	void
//	Description:	Used to enable/disable validation controls on a page.
////////////////////////////////////////////////////////////////////////////////
function EnablePageValidators( sAttribute, bEnabled )
{
	if ( Page_Validators )
	{
		for ( var i = 0 ; i < Page_Validators.length ; i++ )
		{
			if ( Page_Validators[ i ] != null )
			{
				// If not looking for validator controls with a specific attribute OR
				// current control in loop has the attribute being searched for
				if ( ( sAttribute == '' ) || ( Page_Validators[ i ].getAttribute( sAttribute ) ) )
				{
					EnableValidator( Page_Validators[ i ], bEnabled );
					//if ( typeof( ValidatorEnable ) == 'function' ) ValidatorEnable( Page_Validators[ i ], bEnabled );
				}
			}
		}
	}
}

function EnableValidator( oValidator, bEnabled )
{
	if ( typeof( ValidatorEnable ) == 'function' && oValidator != null )  ValidatorEnable( oValidator, bEnabled );
}

function isWhitespace(s)
{
	if (isEmpty(s)) return true;

	var whitespace = " \t\n\r";

	for (var i = 0; i < s.length; i++)
	{   
		if (whitespace.indexOf(s.charAt(i)) == -1) return false;
	}

	return true;
}

function isEmpty(s)
{
	return ((s == null) || (s.length == 0));
}

function jLTrim(strIn) 
{
	// Performs a left trim on the passed in value 
	var str = new String(strIn);
		
	while (1) 
	{
		if (str.substring(0, 1) != " ")
		{
			break;
		}
		str = str.substring(1, str.length);
	}

	return str;
}

function jRTrim(strIn) 
{
	// Performs a right trim on the passed string 
	var str = new String(strIn);
	 
	while (1) 
	{
		if (str.substring(str.length - 1, str.length) != " ")
		{
			break;
		}
		str = str.substring(0, str.length - 1);
	}

	return str;
}

function jTrim(strIn) 
{
	// Returns a trim of the passed string 
	return jLTrim(jRTrim(strIn));
}

function compareStr(sStr1,sStr2) 
{
	var sStr1 = sStr1.toUpperCase();
	var sStr2 = sStr2.toUpperCase();
	if (String(sStr1) == String(sStr2)) return true;
	else return false;
}

function Left(str, n) 
{
	if (n <= 0) {
		return "";
	}
	else if (n > String(str).length) {
		return str;
	}
	else {
		return String(str).substring(0,n);
	}
}

function Right(str, n)
{
	if (n <= 0) {
		return "";
	}
	else if (n > String(str).length) {
		return str;
	}
	else {
		var oStrObj = String(str);
		return oStrObj.substring((oStrObj.length - n))
	}
}

function coalesce(vVal1, vVal2)
{
	if (jTrim(vVal1) == '')
		if (jTrim(vVal2) == '')
			return '';
		else
			return vVal2;
	else
		return vVal1;
}

////////////////////////////////////////////////////////////////////////////////
//		   Name:	addDays(myDate,days)
//	 Parameters:	myDate - date
//					days - number of days to add
//	    Returns:	new date that added the days
//	Description:	Used to add a specified number of days to a passed-in date
//					value and return that new date.
////////////////////////////////////////////////////////////////////////////////
function addDays(myDate,days) 
{
    return new Date(myDate.getTime() + days*24*60*60*1000);
}


////////////////////////////////////////////////////////////////////////////////
// BEGIN - VALIDATOR CONTROLS FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
function validateIsInteger(oSource, oArgs)
{
	oArgs.IsValid = ( isInteger(oArgs.Value.toString()) );	
	return;
}

function validateIsPosNegInteger(oSource, oArgs)
{
	oArgs.IsValid = ( isValidPosNegInteger(oArgs.Value.toString()) );
}

function validateIsDouble(oSource, oArgs)
{
	oArgs.IsValid = ( isValidDouble(oArgs.Value.toString()) );	
	return;
}

function validateIsPosNegDouble(oSource, oArgs)
{
	oArgs.IsValid = ( isValidPosNegDouble(oArgs.Value.toString()) );	
	return;
}

function validateIsEmail(oSource, oArgs)
{
	oArgs.IsValid = ( isEmail(oArgs.Value.toString()) );
	return;
}

function validateDropdownRequired(oSource, oArgs)
{
	oArgs.IsValid = ( parseInt(oArgs.Value) != -1 );
	return;
}

////////////////////////////////////////////////////////////////////////////////
// END - VALIDATOR CONTROLS FUNCTIONS
////////////////////////////////////////////////////////////////////////////////

function isInteger (s)
{
	var i;

	if (isEmpty(s)) 
	{
		if (isInteger.arguments.length == 1) 
		{
			return true;
		}
		else 
		{
			return (isInteger.arguments[1] == true);
		}
	}

	// Search through string's characters one by one
	// until we find a non-numeric character.
	// When we do, return false; if we don't, return true.

	for (i = 0; i < s.length; i++)
	{   
		// Check that current character is number.
		var c = s.charAt(i);

		if (!isDigit(c)) return false;
	}

	// All characters are numbers.
	return true;
}

////////////////////////////////////////////////////////////////////////////////
//		   Name:	isPosNegInteger
//	 Parameters:	sVal - value excluding the character just entered
//					sChar - character just entered
//	    Returns:	BOOL - FALSE if data is invalid, otherwise TRUE
//	Description:	Used to validate a double value.  Should be used in "onKeyPress"
//					event of text inputs where value must be a double.
////////////////////////////////////////////////////////////////////////////////
function isPosNegInteger(sVal, sChar)
{
	var bRetVal = true;
	var i;

	if (isEmpty(sChar)) 
	{
		if (isPosNegInteger.arguments.length != 1) 
		{
			bRetVal = (isPosNegInteger.arguments[1] == true);
		}
	}

	if ( bRetVal )
	{
		// If character entered is a minus sign
		if ( sChar == '-' )
		{
			var bMinusFound = false;

			// Loop through value that character was entered into to determine if a minus sign already exists in value
			for (i = 0; ( i < sVal.length ) && ( !bMinusFound ); i++)
			{
				var c = sVal.charAt(i);

				// If current character is a minus sign
				if ( c == '-' )
				{
					// Indicate that a second minus sign was found
					bMinusFound = true;
				}
			}

			// Set return value to FALSE if a second minus sign is found in value
			bRetVal = (!bMinusFound);
		}
		// If character entered is NOT a number (and is not a minus sign)
		else if ( !isDigit(sChar) )
		{
			bRetVal = false;
		}
	}

	return( bRetVal );
}

////////////////////////////////////////////////////////////////////////////////
//		   Name:	isDouble
//	 Parameters:	sVal - value excluding the character just entered
//					sChar - character just entered
//	    Returns:	BOOL - FALSE if data is invalid, otherwise TRUE
//	Description:	Used to validate a double value.  Should be used in "onKeyPress"
//					event of text inputs where value must be a double.
////////////////////////////////////////////////////////////////////////////////
function isDouble(sVal, sChar)
{
	var bRetVal = true;
	var i;

	if (isEmpty(sChar)) 
	{
		if (isDouble.arguments.length != 1) 
		{
			bRetVal = (isDouble.arguments[1] == true);
		}
	}

	if ( bRetVal )
	{
		// If character entered is a decimal point
		if ( sChar == '.' )
		{
			var bDecimalFound = false;

			// Loop through value that character was entered into to determine if a decimal point already exists in value
			for (i = 0; ( i < sVal.length ) && ( !bDecimalFound ); i++)
			{
				var c = sVal.charAt(i);

				// If current character is a decimal point
				if ( c == '.' )
				{
					// Indicate that a second decimal point was found
					bDecimalFound = true;
				}
			}

			// Set return value to FALSE if a second decimal point is found in value
			bRetVal = (!bDecimalFound);
		}
		// If character entered is NOT a number (and is not a decimal point)
		else if ( !isDigit(sChar) )
		{
			bRetVal = false;
		}
	}

	return( bRetVal );
}

function isValidDouble(sVal)
{
	var bRetVal = true;
	var bDecimalFound = false;

	if ( !isWhitespace(sVal) )
	{
		for (var i = 0; ( i < sVal.length ) && ( bRetVal ); i++)
		{
			var c = sVal.charAt(i);

			// If current character is a decimal
			if ( c == '.' )
			{
				if ( !bDecimalFound )
				{
					// Indicate that a second decimal point was found
					bDecimalFound = true;
				}
				else
				{
					bRetVal = false;
				}
			}
		}
		
		if ( bRetVal )
		{
			for (var i = 0; ( i < sVal.length ) && ( bRetVal ); i++)
			{
				var c = sVal.charAt(i);

				if ( c != '.' )
				{
					bRetVal = isDigit(c);
				}
			}
		}
	}
	
	return( bRetVal );
}

function isValidPosNegInteger(sVal)
{
	var bRetVal = true;
	var bMinusFound = false;
	var bDecimalFound = false;

	if ( !isWhitespace(sVal) )
	{
		for (var i = 0; ( i < sVal.length ) && ( bRetVal ); i++)
		{
			var c = sVal.charAt(i);

			// If current character is a minus sign
			if ( c == '-' )
			{
				if ( !bMinusFound )
				{
					// Indicate that a second minus sign was found
					bMinusFound = true;
				}
				else
				{
					bRetVal = false;
				}
			}
		}
		
		if ( bRetVal )
		{
			for (var i = 0; ( i < sVal.length ) && ( bRetVal ); i++)
			{
				var c = sVal.charAt(i);

				if ( c != '-' )
				{
					bRetVal = isDigit(c);
				}
			}
		}
	}
	
	return( bRetVal );
}

function isValidPosNegDouble(sVal)
{
	var bRetVal = true;
	var bMinusFound = false;
	var bDecimalFound = false;

	if ( !isWhitespace(sVal) )
	{
		for (var i = 0; ( i < sVal.length ) && ( bRetVal ); i++)
		{
			var c = sVal.charAt(i);

			// If current character is a minus sign
			if ( c == '-' )
			{
				if ( !bMinusFound )
				{
					// Indicate that a second minus sign was found
					bMinusFound = true;
				}
				else
				{
					bRetVal = false;
				}
			}
			// If current character is a decimal
			else if ( c == '.' )
			{
				if ( !bDecimalFound )
				{
					// Indicate that a second decimal point was found
					bDecimalFound = true;
				}
				else
				{
					bRetVal = false;
				}
			}
		}
		
		if ( bRetVal )
		{
			for (var i = 0; ( i < sVal.length ) && ( bRetVal ); i++)
			{
				var c = sVal.charAt(i);

				if ( ( c != '-' ) && ( c != '.' ) )
				{
					bRetVal = isDigit(c);
				}
			}
		}
	}
	
	return( bRetVal );
}

////////////////////////////////////////////////////////////////////////////////
//		   Name:	isPosNegDouble
//	 Parameters:	sVal - value excluding the character just entered
//					sChar - character just entered
//	    Returns:	BOOL - FALSE if data is invalid, otherwise TRUE
//	Description:	Used to validate a double value.  Should be used in "onKeyPress"
//					event of text inputs where value must be a double.
////////////////////////////////////////////////////////////////////////////////
function isPosNegDouble(sVal, sChar)
{
	var bRetVal = true;
	var i;

	if (isEmpty(sChar)) 
	{
		if (isPosNegDouble.arguments.length != 1) 
		{
			bRetVal = (isPosNegDouble.arguments[1] == true);
		}
	}

	if ( bRetVal )
	{
		// If character entered is a minus sign
		if ( sChar == '-' )
		{
			var bMinusFound = false;

			// Loop through value that character was entered into to determine if a minus sign already exists in value
			for (i = 0; ( i < sVal.length ) && ( !bMinusFound ); i++)
			{
				var c = sVal.charAt(i);

				// If current character is a minus sign
				if ( c == '-' )
				{
					// Indicate that a second minus sign was found
					bMinusFound = true;
				}
			}

			// Set return value to FALSE if a second minus sign is found in value
			bRetVal = (!bMinusFound);
		}
		// If character entered is a decimal point
		else if ( sChar == '.' )
		{
			var bDecimalFound = false;

			// Loop through value that character was entered into to determine if a decimal point already exists in value
			for (i = 0; ( i < sVal.length ) && ( !bDecimalFound ); i++)
			{
				var c = sVal.charAt(i);

				// If current character is a decimal point
				if ( c == '.' )
				{
					// Indicate that a second decimal point was found
					bDecimalFound = true;
				}
			}

			// Set return value to FALSE if a second decimal point is found in value
			bRetVal = (!bDecimalFound);
		}
		// If character entered is NOT a number (and is not a decimal point or minus sign)
		else if ( !isDigit(sChar) )
		{
			bRetVal = false;
		}
	}

	return( bRetVal );
}

function isValidPosNegNumber(oFld)
{
	var bMinusFound = false;

	if ( oFld )
	{
		// Loop through value that character was entered into to determine if a minus sign already exists in value
		for (i = 0; ( i < oFld.value.length ) && ( !bMinusFound ); i++)
		{
			var c = oFld.value.charAt(i);

			// If current character is a minus sign
			if ( ( c == '-' ) && ( i > 0 ) )
			{
				// Indicate that a second minus sign was found
				bMinusFound = true;
			}
		}
	}
	
	return( !bMinusFound );
}

function isDigit (c)
{
   return ((c >= "0") && (c <= "9"))
}

function isLetter(sChr)
{
	sChr = sChr.toLowerCase();
	return( ( sChr >= 'a' ) && ( sChr <= 'z' ) );
}

function isEmail(sEmail)
{
	var bEmailValid = true;
	
	// If there is a value to check
	if ( !isWhitespace(sEmail) )
	{
		// Check for a character before the @ 
		var i = 1;
		var iLength = sEmail.length;
				
		// Check for @
		while ((i < iLength) && (sEmail.charAt(i) != "@"))
		{
			i++;
		}
				
		if ((i >= iLength) || (sEmail.charAt(i) != "@"))
		{
			bEmailValid = false;
		}
		else 
		{
			i += 2;

			// Check for a period (.)
			while ((i < iLength) && (sEmail.charAt(i) != "."))
			{
				i++;
			}

			// Check for at least one character after the period (.)
			if ((i >= (iLength - 1)) || (sEmail.charAt(i) != ".")) 
			{
				bEmailValid = false;
			}
		}
	}
	
	return( bEmailValid );
}

////////////////////////////////////////////////////////////////////////////////
//		   Name:	isMoney(sValue)
//	 Parameters:	Money
//	    Returns:	BOOL - FALSE if data is invalid, otherwise TRUE
//	Description:	Used to validate money 8 information (15 integers, decimal, 4 integers).
//					To be used in combination with the isDouble validation.
//					NOTE: Decimal point is NOT required.  Value can be whole number.
////////////////////////////////////////////////////////////////////////////////
function isMoney(sValue)
{
	var bValid = true;
	var sFee = sValue;
	var i = 1;
	var sLength = sFee.length;
						
	// Check for .
	while ((i < sLength) && (sFee.charAt(i) != "."))
	{ 
		i++
	}
	if (i < sLength)
	{
		if (sFee.charAt(i) == ".")
		{
			var j = 1;
			var k = (sLength - i);
			i++
				
			if (k > 5)
			{
				bValid = false;
			}
			//Check for another decimal
			while ((j <= (k)) && (sFee.charAt(i) != "."))
			{
				i++
				j++
			}
			if (j <= k)
			{
				if (sFee.charAt(i) == ".")
				{
					bValid = false;
				}
			}
		}
		if ( bValid )
		{
			for ( i = 0; (i < sLength) && (bValid); i++ )
			{
				if ( ( sFee.charAt(i) != '.' ) && ( !isDigit(sFee.charAt(i)) ) )
				{
					bValid = false;
				}
			}
		}
	}
	else 
	{
		bValid = isInteger(sFee);
	}	

	return( bValid );
}

////////////////////////////////////////////////////////////////////////////////
//		   Name:	fnRoundToPrecision
//	 Parameters:	dNum - number to round
//					iNumDecimalPlaces - number of decimal places to round to
//	    Returns:	void
//	Description:	Used to round a number to a specified number of decimal places.
////////////////////////////////////////////////////////////////////////////////
function fnRoundToPrecision(dNum, iNumDecimalPlaces)
{
	var sNum = dNum.toString();
	
	if ( !isWhitespace(sNum) )
	{
		var iChrPos = sNum.indexOf('.');

		if ( iChrPos >= 0 )
		{
			// Keep whole number portion of number to round
			var sWholeNum = Left(sNum, iChrPos);
	
			// Get decimal portion of number up to and including the place of the number used to round to precision
			var sWholeDecimalNum = Left(Right(sNum, (sNum.length - (iChrPos + 1))), (iNumDecimalPlaces + 1));
			
			// If decimal portion of number more than the number of decimal places to round to
			if ( (iChrPos + (iNumDecimalPlaces + 1)) < sNum.length )
			{				
				// Get decimal portion of number to keep (and to round)
				var sDecimalNum = Left(sWholeDecimalNum, iNumDecimalPlaces);
				
				// Get number at position to round
				var sNumToRound = Right(sDecimalNum, 1);
				
				// Get number at postion used to round
				var sNumUsedToRound = Right(sWholeDecimalNum, 1);

				// If number used to round is 5 or greater
				if ( parseInt(sNumUsedToRound) >= 5 )
				{
					// Round number up one
					
					// If decimal portion of number to keep is 99
					if ( parseInt(sDecimalNum) == 99 )
					{
						// Round whole number portion of number up one and include trailing zeroes
						sWholeNum = (parseInt(sWholeNum) + 1).toString();
						sDecimalNum = '';
						for ( var i = 0; i < iNumDecimalPlaces; i++ )
						{
							sDecimalNum += '0';
						}
					}
					else
					{
						// If decimal number to round is 9
						if ( parseInt(sNumToRound) == 9 )
						{
							// Round decimal portion of number to keep up one
							sDecimalNum = (parseInt(sDecimalNum) + 1).toString();
						}
						else
						{
							// Round decimal number up one and update decimal portion of number to keep
							sNumToRound = (parseInt(sNumToRound) + 1).toString();
							sDecimalNum = Left(sDecimalNum, (iNumDecimalPlaces - 1)) + sNumToRound;
						}
					}
				}

				sWholeNum += '.' + sDecimalNum;
				sNum = sWholeNum;
			}
			else
			{
				// Put trailing zeroes at the end of the return value number.
				while( sWholeDecimalNum.length < iNumDecimalPlaces )
				{
					sWholeDecimalNum += '0';
				}
				
				sWholeNum += '.' + sWholeDecimalNum;
				sNum = sWholeNum;
			}
		}
		else
		{
			if ( parseFloat(sNum) != 0 )
			{
				sNum += '.00';
			}
		}
	}
	
	return( sNum );
}

////////////////////////////////////////////////////////////////////////////////
//		   Name:	fnClearSelectList
//	 Parameters:	oListObj - select list object to clear
//					oFldObj - accompanying text field object to clear
//	    Returns:	void
//	Description:	Used to clear all options from a passed-in select list object.
//					Also used to clear field value that may accompany the list.
////////////////////////////////////////////////////////////////////////////////
function fnClearSelectList(oListObj, oFldObj)
{
	if ( oListObj )
	{
		for (var i = oListObj.length; i >= 0; i--) 
		{
			oListObj.options[i] = null;
		}
	}
	if ( oFldObj ) oFldObj.value = '';

	return;
}

////////////////////////////////////////////////////////////////////////////////
//		   Name:	fnBuildCommaDelimString
//	 Parameters:	oFrmField
//	    Returns:	String
//	Description:	Used to build a comma delimited string from a list control.
////////////////////////////////////////////////////////////////////////////////
function fnBuildCommaDelimString(oFrmField)
{
	var sRetVal = '';
	
	if ( ( oFrmField ) && ( oFrmField.length ) )
	{
		for ( var i = 0; i < oFrmField.length; i++ )
		{
			if ( sRetVal != '' )
			{
				sRetVal += ',';
			}
			sRetVal += oFrmField[i].value;
		}
	}
	
	return( sRetVal );
}

function fnResizePageContentPanel(sPanelId, sHeightOffsetObjects)
{
    if ( sPanelId != '' )
    {
        var oPanel = document.getElementById(sPanelId);
        if ( oPanel )
        {
            var iOffsetHeight = 0;
            
            if ( sHeightOffsetObjects != '' )
            {
                var arrOffsets = sHeightOffsetObjects.split(',');
                for ( i = 0; i < arrOffsets.length; i++ )
                {
                    if ( arrOffsets[i] != '' )
                    {
                        if ( parseInt(arrOffsets[i]) )
                        {
                            iOffsetHeight += parseInt(arrOffsets[i]);
                        }
                        else
                        {
                            var oObject = document.getElementById(arrOffsets[i]);
                            if ( oObject ) iOffsetHeight += oObject.offsetHeight;
                        }
                    }
                }                
            }
            
            if ( parseInt(document.body.clientHeight) > parseInt(iOffsetHeight) )
            {
                oPanel.style.height = document.body.clientHeight - iOffsetHeight;
            }
            if ( parseInt(document.body.clientWidth) > 5 )
            {
                //oPanel.style.width = document.body.clientWidth - 5;
            }            
        }
    }
    
    return;
}
//--------------------------------------------------------------------------------------------------
//global variables to date
var mstrM, mstrD, mstrY, mstrDelimChar;
var mstrDateOut;


/*
Dependencies:	None

Description: String Manipulations

Functions:
	lTrim(str)
	rTrim(str)
	trim(str)
	getNumeric(Item, AllowPeriod, AllowNegative)
*/

//or for all versions (trims characters ASCII<32 not true "whitespace"): 
function ucase(obj)
{
obj.value=obj.value.toUpperCase();
}
//------------------------------------------------------------------------------------------
function lTrim(str) { 
	return str.replace(/^\s+/g, "");
}
//------------------------------------------------------------------------------------------
function rTrim(str) { 
	return str.replace(/\s+$/g, "");
}
//------------------------------------------------------------------------------------------
function trim(str) {
	return lTrim(rTrim(str));
}
//------------------------------------------------------------------------------------------
function getNumeric(Item, AllowPeriod, AllowNegative)
{
Item=trim(Item);
var intLen=Item.length;
var strOut="";
var strChar="";
var intChar;
for(var i=0; i<intLen; i++)
	{
	strChar=Item.substr(i,1);
	if(AllowPeriod && (strChar=="."))
		{
		strOut+=strChar;
		}
	else if (AllowNegative && (strChar=="-"))
		{
		strOut+=strChar;
		}
	else
		{
		intChar=parseInt(strChar,10)
		if(!isNaN(intChar))
			{
			strOut+=intChar;
			}
		}
	}
return strOut;
}
//-->

<!--
/*
Dependencies:	String.js for trim functions

Functions:
	cvValidateDate(src, args) - called by a custom date validator, formats and validates date
	dateChange(DateTB)
	dateFormatValid(DateString)
	isDate(DateVal)
	y2k(number) 
	isDate(day,month,year)
	compareDate(strDteOne, strDteTwo, strCompOp)
*/

function cvValidateDate(src, args)
{
//get the datestring we are trying to validate
var strDate=args.Value;

//get an object reference to the validation control
var ctlValidator=document.getElementById(src.id);
//get the name of the control that this is validating
var strCtlValidated=ctlValidator.getAttribute('controltovalidate');
//get the date textbox being validated so that we can set its value later
var ctlValidated=document.getElementById(strCtlValidated);

//default valid value to false
var blnValid=false;
if(intDateChange(strDate))
	{
	
	//fire the regexp validator on the newly formatted date
	blnValid=/^(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/.test(mstrDateOut);
	}	
//if valid, display formatted date
if(blnValid)
	{
	
	ctlValidated.value=mstrDateOut;
	}
else
	{
	//clear control
	ctlValidated.value='';
	}
	
//set valid property of the args	
args.IsValid=blnValid;
}
//------------------------------------------------------------------------------------------
function intDateChange(DateVal)
{
var blnOk=false;
if(dateFormatValid(DateVal))
	{
	if(isDate(mstrD, mstrM, mstrY))
		{
		//rebuild date
		blnOk=true;
		mstrDateOut=mstrM+mstrDelimChar+mstrD+mstrDelimChar+mstrY;
		}
	}
return blnOk;
}
//------------------------------------------------------------------------------------------
function dateChange(DateTB, AlertMsg, ClearField)
{
//this function will validate + format a date for the field which it is passed
var strDate=trim(DateTB.value);
var blnOk=false;
if(dateFormatValid(strDate))
	{
	if(isDate(mstrD, mstrM, mstrY))
		{
		//rebuild date
		blnOk=true;
		DateTB.value=mstrM+mstrDelimChar+mstrD+mstrDelimChar+mstrY;
		}
	}

if(!blnOk)
	{
	//alert if appropriate
	if(AlertMsg)
		{
		alert(AlertMsg);
		}
	//clear field if appropriate
	if(ClearField)
		{
		DateTB.value="";
		}
	}
}
//------------------------------------------------------------------------------------------
function dateFormatValid(DateString)
{
var blnRet=true;
var blnMMDDYY, blnMMDDYYYY, blnMM_DD_YY, blnMM_DD_YYYY;
var intDelim1, intDelim2;

blnMMDDYY = /^\d{6}$/.test(DateString);
blnMMDDYYYY = /^\d{8}$/.test(DateString);
blnMM_DD_YY = /^\d\d?(-|\/)\d\d?(-|\/)\d{2}$/.test(DateString);
blnMM_DD_YYYY = /^\d\d?(-|\/)\d\d?(-|\/)\d{4}$/.test(DateString);

if(blnMMDDYY)
	{
	//ok
	mstrM=DateString.substr(0,2);
	mstrD=DateString.substr(2,2);
	mstrY=DateString.substr(4,2);
	mstrDelimChar="/";
	}
else if(blnMMDDYYYY)
	{
	//ok
	mstrM=DateString.substr(0,2);
	mstrD=DateString.substr(2,2);
	mstrY=DateString.substr(4,4);
	mstrDelimChar="/";
	}
else if(blnMM_DD_YY || blnMM_DD_YYYY)
	{
	if(DateString.indexOf("/")>0)
		{
		intDelim1=DateString.indexOf("/");
		intDelim2=DateString.indexOf("/",(intDelim1+1));
		mstrDelimChar="/";
		}
	else
		{
		intDelim1=DateString.indexOf("-");
		intDelim2=DateString.indexOf("-",(intDelim1+1));
		mstrDelimChar="-";
		}
	mstrM=DateString.substring(0,intDelim1);
	mstrD=DateString.substr((intDelim1+1),intDelim2);
	mstrY=DateString.substr(intDelim2+1);
	}
else
	{
	//invalid
	blnRet=false;
	}

if(blnRet)
	{
	//parse year to 4 digit
	if(mstrY.length==2)
		{
		var intY=parseInt(mstrY,10);
		if(intY>50)
			{
			mstrY="19" + mstrY;
			}
		else
			{
			mstrY="20" + mstrY;
			}
		}
	//clean out leading/trailing zeros
	mstrD=parseInt(mstrD,10).toString();
	mstrM=parseInt(mstrM,10).toString();
	}
	
return blnRet;
}
//------------------------------------------------------------------------------------------
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
//------------------------------------------------------------------------------------------
function isDate(day,month,year) {
// checks if date passed is valid
// will accept dates in following format:
// isDate(dd,mm,ccyy), or
// isDate(dd,mm) - which defaults to the current year, or
// isDate(dd) - which defaults to the current month and year.
// Note, if passed the month must be between 1 and 12, and the
// year in ccyy format.

//parse out days to assure they dont have leading zeros
day=parseInt(day, 10).toString();
month=parseInt(month, 10).toString();

var today = new Date();
year = ((!year) ? y2k(today.getYear()):year);
month = ((!month) ? today.getMonth():month-1);
if (!day) 
	{
	return false;
	}
var test = new Date(year,month,day);
if ( (y2k(test.getYear()) == year) &&
        (month == test.getMonth()) &&
        (day == test.getDate()) )
    {
	return true;
	}
else
	{
    return false;
	}
}
//------------------------------------------------------------------------------------------
function compareDate(strDteOne, strDteTwo, strCompOp)
{
/*this function will make a call to convert two equal string dates
the function expects the dates in the mm/dd/yyyy format.  this 
format can be achieved by using the validateDate function.
this function returns a boolean value to indicate success of comparison
*/

var blnRet=false;
var dteOne = new Date(strDteOne);
var dteTwo = new Date(strDteTwo);

if(strCompOp=="==")
	{
	strCompOp="=";
	}

switch(strCompOp)
	{
	case "!=":
		if(dteOne!=dteTwo)
			{
			blnRet=true;
			}
		break;
	
	case ">":
		{
		if(dteOne>dteTwo)
			{
			blnRet=true;
			}
		break;
		}
			
	case ">=":
		{
		if(dteOne>=dteTwo)
			{
			blnRet=true;
			}
		break;
		}
			
	case "=":
		{
		if(dteOne>dteTwo || dteOne<dteTwo)
			{
			//nothing
			blnRet=false;
			}
		else
			{
			blnRet=true;
			}	
			
		break;
		}
			
	case "<=":
		{
		if(dteOne<=dteTwo)
			{
			blnRet=true;
			}	
		break;
		}
			
	case "<":
		{
		if(dteOne<dteTwo)
			{
			blnRet=true;
			}	
		break;
		}
	}
return blnRet;
}

function cvValidateGreaterThanToday(src,args)
{
    var strDate=args.Value;

    //get an object reference to the validation control
    var ctlValidator=document.getElementById(src.id);
    //get the name of the control that this is validating
    var strCtlValidated=ctlValidator.getAttribute('controltovalidate');
    //get the date textbox being validated so that we can set its value later
    var ctlValidated=document.getElementById(strCtlValidated);

    //default valid value to false
    var blnValid=false;
    var blnVal = false;
    if(intDateChange(strDate))
	{	
	    var ystr = (mstrY.length == 2) ? (mstrY = "20" + mstrY ): mstrY
	    var mstr= (mstrM.length == 1) ? (mstrM = "0" + mstrM ): mstrM
	    var dstr = (mstrD.length == 1) ? (mstrD = "0" + mstrD ): mstrD
	    var mstrDateOut=mstr+"/"+dstr+"/"+ystr
	    var today = new Date()
	    var now = (today.getMonth() + 1) + "/" +  today.getDate() +  "/" + today.getFullYear ()
	    var comp = ">"
	    blnVal = compareDate(mstrDateOut, now,comp) 
	    if(!blnVal) 
	    {
	        ctlValidated.value='';
	    }
	}
    else
	{
	    ctlValidated.value='';
	} 

    args.IsValid  = blnVal
}


//-->

<!--
/*
Dependencies:	
	String.js for trim functions
	Format.js for formatting functions
	
Functions:
	cvValidateAmtGreaterThanZero(src, args) - called by a custom amount validator, formats and validates amt > 0
	calcGenericAmtRem(AmtCtl, AmtOverallCtl, AmtSoFarCtl, AmtRefCtl, AmtSoFarLblName, AmtRemLblName)
	amountDiff(TBSubtractFrom, TBToSubtract)
	amountChange(AmtTB, AllowNegative, AlertMsg, ClearField)
	isCurrency(Amt, AllowNegative)
*/


/*
//------------------------------------------------------------------------------------------
function amountDiff(TBSubtractFrom, TBToSubtract)
{
var strAmtSubFrom=trim(TBSubtractFrom.value);
var strAmtSub=trim(TBToSubtract.value);
var fltRet=0;
if(isCurrency(strAmtSubFrom) && isCurrency(strAmtSub))
	{
	var fltSubFrom=parseFloat(getNumeric(strAmtSubFrom, true, true));
	var fltSub=parseFloat(getNumeric(strAmtSub, true, true));
	fltRet=fltSubFrom-fltSub;
	}
return fltRet;
)
*/
//------------------------------------------------------------------------------
function cvValidateAmtGreaterThanZero(src, args)
{
//get the datestring we are trying to validate
var strAmt=args.Value;
//get an object reference to the validation control
var ctlValidator=document.getElementById(src.id);
//get the name of the control that this is validating
var strCtlValidated=ctlValidator.getAttribute('controltovalidate');
//get the amount textbox being validated so that we can set its value later
var ctlValidated=document.getElementById(strCtlValidated);

//default valid value to false
var blnValid=true;
try
	{
	var fltAmt=parseFloat(getNumeric(strAmt, true, false));
	if(fltAmt<=0)
		{
		blnValid=false;
		}
	}
catch(e)
	{
	//not valid
	blnValid=false;
	}
	
//if valid, display formatted amount
if(blnValid)
	{
	ctlValidated.value=formatCurrency(strAmt, 2, true);
	}
else
	{
	//clear control
	ctlValidated.value='';
	}
	
//set valid property of the args	
args.IsValid=blnValid;
}
//------------------------------------------------------------------------------------------
function calcGenericAmtRem(AmtCtl, AmtOverallCtl, AmtSoFarCtl, AmtRefCtl, AmtSoFarLblName, AmtRemLblName)
{
//read the amount overall and the amount covered and adjust
if(amountChange(AmtCtl, false, '', true))
	{
	var strAmtOverall=AmtOverallCtl.value;
	var strAmtSoFar=AmtSoFarCtl.value;
	var strPaymentAmtRef=AmtRefCtl.value;
	var strPaymentAmt=AmtCtl.value;
	
	//get the numbers out of the strings
	var fltAmtSoFar=parseFloat(getNumeric(strAmtSoFar, true, false));
	var fltPaymentAmt=parseFloat(getNumeric(strPaymentAmt, true, false));
	var fltPaymentAmtRef=parseFloat(getNumeric(strPaymentAmtRef, true, false));
	
	//recalc real payment amount
	fltPaymentAmt=fltPaymentAmt - fltPaymentAmtRef;
	var fltSoFar=fltAmtSoFar + fltPaymentAmt;
	var fltOverall=parseFloat(getNumeric(strAmtOverall, true, false));
	var fltAmtRem=fltOverall - fltSoFar;
	
	//setup some html
	var strAmtSoFarLbl=formatCurrency(fltSoFar.toString(), 2, true);
	var strAmtRemLbl=formatCurrency(fltAmtRem.toString(), 2, true);
	
	document.getElementById(AmtSoFarLblName).innerHTML=strAmtSoFarLbl;
	document.getElementById(AmtRemLblName).innerHTML=strAmtRemLbl;
	}
}
//------------------------------------------------------------------------------------------
function amountChange(AmtTB, AllowNegative, AlertMsg, ClearField)
{
var strAmt=trim(AmtTB.value);
var blnRet=true;
if(isCurrency(strAmt, AllowNegative)) //chg
	{
	strAmt=formatCurrency(strAmt, 2, true);
	AmtTB.value=strAmt;
	}
else
	{
	blnRet=false;
	//alert if appropriate
	if(AlertMsg)
		{
		alert(AlertMsg);
		}
	//clear field if appropriate
	if(ClearField)
		{
		AmtTB.value="";
		}
	}
return blnRet;
}
//------------------------------------------------------------------------------------------
function amountChangeRefField(AmtTB, AllowNegative, AlertMsg, ClearField, RefTBID)
{
var strAmt=trim(AmtTB.value);
var blnRet=true;
if(isCurrency(strAmt, AllowNegative)) //chg
	{
	//set unformatted reference value first
	document.getElementById(RefTBID).value=strAmt;
	//now format out
	strAmt=formatCurrency(strAmt, 2, true);
	AmtTB.value=strAmt;
	}
else
	{
	blnRet=false;
	//alert if appropriate
	if(AlertMsg)
		{
		alert(AlertMsg);
		}
	//clear field if appropriate
	if(ClearField)
		{
		AmtTB.value="";
		document.getElementById(RefTBID).value="";
		}
	}
return blnRet;
}
//------------------------------------------------------------------------------------------
function isCurrency(Amt, AllowNegative)
{
//positive only
if(AllowNegative) 
	{
	return /^\-?\$?(([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+))?(.[0-9][0-9])?$/.test(Amt);
	}
else
	{
	return /^\$?(([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+))?(.[0-9][0-9])?$/.test(Amt);
	}
}
//-->


// JScript File

<!--
/*
Dependencies:	
	String.js for trim functions
	Math.js for number rounding functions
	
Functions:
	formatCurrency(AmountString, DecPlaces, DollarSymbol)
	format2DecimalPlaces(DecimalString)
*/
//------------------------------------------------------------------------------------------
function formatCurrency(AmountString, DecPlaces, DollarSymbol) 
{
	//make sure that we strip out any garbage
	var blnNeg=false;
	if(AmountString.indexOf('-') != -1 || (AmountString.indexOf('(') != -1 && AmountString.indexOf(')') != -1))
		{
		blnNeg=true;
		}
		
	var Amt=getNumeric(AmountString, true, false)	
		
	Amt = round (Amt, DecPlaces);
	for (var i = Amt.indexOf('.') - 3; i > 0; i -= 3)
		Amt = Amt.substring(0, i) + ',' + Amt.substring(i);
		
	if(DollarSymbol)
		{
		Amt="$" + Amt;
		}
		
	if(blnNeg)
		{
		Amt="-" + Amt;
		}
		
	return Amt;
}
//----------------------------------------------------------------------------------------------------------------
//-->

// Function for focusing a Control on page load
function fnFocus(target)
{
    if ( target != '' )
    {
        var oCtrl = document.getElementById(target);
        if ( ( oCtrl ) && ( oCtrl.style.display == '' ) && ( !oCtrl.disabled ) )
        {
            oCtrl.focus();
        }
        else
        {
            alert('Error attempting to set focus to a control that does not exist on the form: ' + target);
        }
    }
}    

/*
Description:    Used to limit the number of characters that can be entered into a TextBox server control
                with a TextMode=MultiLine.
                
Parameters:     field - TextBox object
                maxlimit - max number of allowed characters
                currentCharsDisplayId - ID of span tag that displays the number of characters left to enter
                sAlertMsg - string to display if too many characters entered in textbox
*/
function MultiLineTextBox_LimitCharacters(field, maxlimit, currentCharsDisplayId, sAlertMsg)
{
    if ( field.value.length > maxlimit )
    {
        if ( sAlertMsg != '' ) alert(sAlertMsg);
        field.value = field.value.substring(0, maxlimit);
    }
    
    var oSpan = document.getElementById(currentCharsDisplayId);
    if ( oSpan ) oSpan.innerHTML = (maxlimit - field.value.length).toString() + ' characters left';
}