// See if a value is there
// Left trim a string with blanks in front
function LTrim (sStr)
  {
  while (sStr.charAt(0) == ' ')
    sStr = sStr.substr(1, sStr.length - 1)
  return sStr;
  }  
  
// Right trim a string with blanks in back
function RTrim (sStr)
  {
  while (sStr.charAt(sStr.length - 1) == ' ')
    sStr = sStr.substr(0, sStr.length - 1)
  return sStr;
  }
  
// Left trim and right trim a string with blanks in front and in back  
function Trim (sStr)
  {
  return LTrim(RTrim(sStr));
  }

function ValueIsThere (blnRequired, strFieldValue)
	{
	if (strFieldValue.length <= 0)
		{
		if (blnRequired)
			alert("This field is required");
		return (false);
		}
	else
		return (true);
	}

// Globals
var daysInMonth = new Array(12);

daysInMonth[1] = 31;
daysInMonth[2] = 28;
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;


// IsDate accepts dates in the following format:
//		mm/dd/yyyy
//		mm-dd-yyyy
//		mmddyyyy
function IsDate(strDt) {
	var re1 = new RegExp("^[0-9]{1,2}[\/\-][0-9]{1,2}[\/\-]([0-9][0-9]){1,2}$");
	var re2 = new RegExp("^[0-9]{8}$");
	var intDay, intMonth, intYear;
	var intInd1, intInd2;
	var boolRetVal = false;
		
	if (re1.test(strDt)) {
		intInd1 = strDt.indexOf("/");

		if (intInd1 == -1)
			intInd1 = strDt.indexOf("-");
		intInd2 = strDt.indexOf("/",intInd1+1);

		if (intInd2 == -1)
			intInd2 = strDt.indexOf("-",intInd1+1);
			
		intMonth = parseInt(strDt.substring(0,intInd1),10);
		intDay = parseInt(strDt.substring(intInd1+1,intInd2),10);
		intYear = parseInt(strDt.substring(intInd2+1,strDt.length),10);
	} else if (re2.test(strDt)) {
		intMonth = parseInt(strDt.substring(0,2),10);
		intDay = parseInt(strDt.substring(2,4),10);
		intYear = parseInt(strDt.substring(4,8),10);
	} else
		return(false);

	if  ((intYear % 100) == 0)
	   if ((intYear % 400) == 0)
	    	daysInMonth[2] = 29;
	else
	   if ((intYear % 4) == 0)
	        daysInMonth[2] = 29;
		
	if (intMonth > 0 && intMonth < 13) 
		if (intDay > 0 && intDay <= daysInMonth[intMonth])
			if (intYear > 0 && intYear < 9999)
				boolRetVal = true;
				
	return(boolRetVal);
}


// ReturnDate accepts a date string and converts it into a date object
function ReturnDate(strDt) {
	var re = new RegExp("^[0-9]{8}$");
	var intMonth,intDay,intYear;
	var dtRetVal;
	
	if (IsDate(strDt)) {
		if (re.test(strDt)) {
			intMonth = parseInt(strDt.substring(0,2),10);
			intDay = parseInt(strDt.substring(2,4),10);
			intYear = parseInt(strDt.substring(4,8),10);
			dtRetVal = new Date(Date.parse(intMonth + "/" + intDay + "/" + intYear));
		}
		else
			dtRetVal = new Date(strDt);
		return(dtRetVal);
	} else 
		return(null);
}


// CompareDates accepts two strings in date format and returns:
//		1 if strDt1 < strDt2
//		0 if strDt1 = strDt2
//		-1 if strDt1 > strDt2
function CompareDates(strDt1,strDt2) {
	var dblDt1,dblDt2;
	
	if (IsDate(strDt1) && IsDate(strDt2)) {
		dblDt1 = Date.parse(ReturnDate(strDt1));
		dblDt2 = Date.parse(ReturnDate(strDt2));
		if (dblDt1<dblDt2)
			return(1);
		else if (dblDt1==dblDt2)
			return(0);
		else
			return(-1);
	}
	return(-2);
}


// DateAdd adds an increment (+/-) to the supplied date
//		strPart: "d" = days, "m" = months, "y" = years
//		dblNum: number of strPart to move
//		strDt: a valid date string
function DateAdd(strPart,dblNum,strDt) {
   var dblAmount, intYears, intMonths, intDays
	var objDate
	
	if (IsDate(strDt)) {
		objDate = ReturnDate(strDt);
		intMonths = objDate.getMonth() + 1;
		intYears = objDate.getYear();
		intDays = objDate.getDate();
		if  ((intYears % 100) == 0)
		   if ((intYears % 400) == 0)
	   	 	daysInMonth[2] = 29;
			else
			   if ((intYears % 4) == 0)
	   	   	daysInMonth[2] = 29;
		if (strPart=="d") {
			for (var i=0;i<dblNum;i++) {
				intDays++;
				if (intDays>daysInMonth[intMonths]) {
					intMonths++;
					intDays = 1;
					if (intMonths>12) {
						intYears++;
						intMonths = 1;
					}
				}
			}
		} else if (strPart=="m") {
			for (var i=0;i<dblNum;i++) {
				intMonths++;
				if (intMonths>12) {
					intYears++;
					intMonths = 1;
				}
			}
		} else if (strPart=="y")
			intYears += dblNum;
		return(intMonths + "/" + intDays + "/" + intYears);
	}
	
	return("");
}


//	IsEmail accepts any string and will return true if the string is
//	a valid email address and false if not
function IsEmail(strVal) {
	//var re = new RegExp("^[A-Za-z0-9\_\-]+\@([A-Za-z0-9\_\-]+[\.])+[A-Za-z]{2,3}$");
	//return(re.test(strVal));
	return(true);
}


// IsNumeric accepts any string and will return true if the string is 
//	numeric and false if not
function IsNumeric(strVal) {
	var re = new RegExp("^([\+\-]?[0-9]*[\.]?[0-9]+)$");
	var re1 = new RegExp("^([\+\-]?[0-9]+[\.]?[0-9]*)$");
	return(re.test(strVal) || re1.test(strVal));
}


// IsNumeric accepts any string and will return true if the string is 
//	numeric and false if not (requires up to 2 decimal places)
function IsNumeric2(strVal) {
	var re = new RegExp("^\-{0,1}[0-9]*([\.]{0,1}[0-9]{1,2}){0,1}$");
	return(re.test(strVal) && strVal.length > 0);
}


// IsNumeric accepts any string and will return true if the string is 
//	numeric and false if not (requires up to 5 decimal places)
function IsNumeric5(strVal) {
	var re = new RegExp("^\-{0,1}[0-9]{0,3}([\.]{0,1}[0-9]{1,5}){0,1}$");
	return(re.test(strVal) && strVal.length > 0);
}


// IsPositiveInt accepts any string and will return true if the string is 
//	a positive number and false if not
function IsPositiveInt(strVal) {
	var re = new RegExp("^[0-9]+$");
	return(re.test(strVal));
}


// IsPositiveInt accepts any string and will return true if the string is 
//	a positive number and false if not
function IsPositiveNonZeroInt(strVal) {
	var re = new RegExp("^[0-9]+$");
	if (re.test(strVal)) {
		if (parseInt(strVal) > 0)
			return true;
	}

	return false;
}


// IsNegativeInt accepts any string and will return true if the string is 
//	a negative number and false if not
function IsNegativeInt(strVal) {
	var re = new RegExp("^\-[0-9]+$");
	return(re.test(strVal));
}


//	Trim accepts a string and truncates all spaces from beginning
//	to end
function Trim(strVal) {
	var re1 = /^\s+/;
	var re2 = /\s+$/;
	var strTemp = new String(strVal);

	strTemp = strTemp.replace(re1,"");
	strTemp = strTemp.replace(re2,"");
	return(strTemp);
}


//*********************************************************************
// Summary: Pads a string to the left (strDirectionCd = "L") or right
//  (strDirectionCd = "R") with a specified char (strCharPad) for a
//  specified length (lngLen).
//*********************************************************************
function PadStr(strIn, lngLen, strDirectionCd, strCharPad)
	{
	var strTmp;
	var intPadLen;
	var i;
	
    if (lngLen == 0)
        return "";
    else if (strIn.length >= lngLen)
        return strIn.substr(0, lngLen);
    else
		{
        strIn = Trim(strIn);
        intPadLen = lngLen - strIn.length;
        
        strTmp = "";
        for (i = 0; i < intPadLen; i++)
            strTmp = strTmp + strCharPad;
            
        if (strDirectionCd.toUpperCase() == "L")
            strTmp = strTmp + strIn;  // Pad left
        else
            strTmp = strIn + strTmp;  // Pad right
        
        return strTmp;
        }
    }


/*	ValidPhone accepts the following format:
		9999999999
		999-999-9999
		999.999.9999
		999 999 9999
		(999)999-9999
*/
function ValidPhone(frmName, strMsg) {
	var re1 = new RegExp("^[0-9]{10}$");
	var re2 = new RegExp("^[0-9]{3}[\-\. ][0-9]{3}[\-\. ][0-9]{4}$");
	var re3 = new RegExp("^[\(]{1}[0-9]{3}[\)]{1}[0-9]{3}[\-]{1}[0-9]{4}$");
		
	obj = document.forms[0].all[frmName];
	if (!re1.test(obj.value) && !re2.test(obj.value) && !re3.test(obj.value)){
		alert(strMsg + '  Please enter in format: (999)999-9999.');
		obj.focus();
		return false;
	}	
	return true;
}


function ValidateFormData() {
	var i,sName,sValue,sTemp;
	var oElm;
	var j;
	var k;
	var arTemp;
	var blnReturn;
	var iUncheckedRadioButtonsCount = 0;
	var aoUncheckedRadioButtons = new Array(10);

	document.forms[0].Continue.disabled = true;

	var blnCheckRadio = false;	
	var strRadioPassed = "NONE";
	var sExtraAlert = " Please press OK to go to that incorrect field.";

	blnReturn = true;

	j = document.forms[0].elements.length;		
	for (i=0; i<j; i++) {
		oElm = document.forms[0].elements[i];
		
		if (blnCheckRadio) {
			if (aoUncheckedRadioButtons[iUncheckedRadioButtonsCount].name != oElm.name) {		
				document.forms[0].Continue.disabled = false;
				for (k=0; k <= iUncheckedRadioButtonsCount; k++)
					highLight(aoUncheckedRadioButtons[k]);									
				alert("A required radio button was not selected." + sExtraAlert);
				i = j;				
				blnReturn = false;		
				return blnReturn;		
			}
			else if (oElm.checked) {
				strRadioPassed = oElm.name;			
				blnCheckRadio = false;
			}
			else {
				aoUncheckedRadioButtons[++iUncheckedRadioButtonsCount] = oElm;			
			}
		}
						
		if (! blnCheckRadio && oElm.type == "radio") {
			arTemp = oElm.name.split("__");
			if (arTemp.length > 1) {
				sFlags = arTemp[1].toLowerCase();
				if (sFlags.length >= 1) {
					unhighLight(oElm);
					if (sFlags.indexOf("r") > -1 && (! oElm.checked) && strRadioPassed != oElm.name) {
						strRadioPassed = "NONE";
						iUncheckedRadioButtonsCount = 0;
						aoUncheckedRadioButtons[iUncheckedRadioButtonsCount] = oElm;
						blnCheckRadio = true;
					}
					else if (sFlags.indexOf("r") > -1 && oElm.checked) {
						strRadioPassed = oElm.name;
						blnCheckRadio = false;
					}
				}
			}		
		}						

		if ((oElm.type == "select-one") || (oElm.type == "text")) {
			sValue = Trim(oElm.value);
			arTemp = oElm.name.split("__");
			if (arTemp.length > 1) {
				sFlags = arTemp[1].toLowerCase();
				if (sFlags.length >= 1) {
					unhighLight(oElm);
					if (sFlags.indexOf("r") > -1 && sValue == "") {
						document.forms[0].Continue.disabled = false;
						highLight(oElm);
						alert("A required field was not entered." + sExtraAlert);
						i = j;
						blnReturn = false;
					} else if (sFlags.indexOf("$") > -1 && 
						!IsNumeric2(sValue) && 
						(sFlags.indexOf("r") > -1 || 
						(sFlags.indexOf("o") > -1 && 
						sValue != ""))) {	
						document.forms[0].Continue.disabled = false;
						highLight(oElm);					
						alert("A field was found with an invalid dollar amount." + sExtraAlert);
						i = j;
						blnReturn = false;
					} else if (sFlags.indexOf("d") > -1 && 
						!IsDate(sValue) && 
						(sFlags.indexOf("r") > -1 || 
						(sFlags.indexOf("o") > -1 && 
						sValue != ""))) {	
						document.forms[0].Continue.disabled = false;					
						highLight(oElm);
						alert("A field was found with an incorrect date." + sExtraAlert);
						i = j;
						blnReturn = false;
					} else if (sFlags.indexOf("n") > -1 && 
						!IsPositiveInt(sValue) && 
						!IsNegativeInt(sValue) &&
						(sFlags.indexOf("r") > -1 || 
						(sFlags.indexOf("o") > -1 && 
						sValue != ""))) {
						document.forms[0].Continue.disabled = false;						
						highLight(oElm);
						alert("A field was found with an invalid number." + sExtraAlert);
						i = j;
						blnReturn = false;
					} else if (sFlags.indexOf("p") > -1 && 
						!IsPositiveInt(sValue) && 
						(sFlags.indexOf("r") > -1 || 
						(sFlags.indexOf("o") > -1 && 
						sValue != ""))) {			
						document.forms[0].Continue.disabled = false;		
						highLight(oElm);
						alert("A field was found that must be a valid positive number." + sExtraAlert);
						i = j;
						blnReturn = false;
					} else if (sFlags.indexOf("e") > -1 && 
						!IsEmail(sValue) &&
						(sFlags.indexOf("r") > -1 || 
						(sFlags.indexOf("o") > -1 && 
						sValue != ""))) {	
						document.forms[0].Continue.disabled = false;					
						highLight(oElm);
						alert("Please enter a valid email address." + sExtraAlert);
						i = j;
						blnReturn = false;
					}
				}
			}
		}
	}

	return blnReturn;
}

function highLight(oElm) {
	oElm.focus();
	if (navigator.appName != 'Netscape')
		oElm.style.backgroundColor = "#EA0000";
}

function unhighLight(oElm) { 
	if (navigator.appName != 'Netscape')
		oElm.style.backgroundColor = "window";
}


// ------------------------------------------------------------------------ 
// Replaces Math.round() which does Banker's rounding.  Do not use Math.round().
// Placed in the public domain by Affordable Production Tools
// March 21, 1998
// Web site: http://www.aptools.com/
// ------------------------------------------------------------------------- 
function NonBankersRound(blnSuppressFrontZero, Number, Decimals)
	{
	var Separator = ".";
	Number += ""          // Force argument to string.
	Decimals += ""        // Force argument to string.
	if(Number.length == 0)
		Number = "0"
	var OriginalNumber = Number  // Save for number too large.
	var Sign = 1
	var Pad = ""
	var Count = 0
	
	// If no number passed, force number to 0.
	if(parseFloat(Number)){
		Number = parseFloat(Number)} else {
		Number = 0}
	// If no decimals passed, default decimals to 2.
	if((parseInt(Decimals,10)) || (parseInt(Decimals,10) == 0)){
		Decimals = parseInt(Decimals,10)} else {
		Decimals = 2}
	if(Number < 0)
		{
		Sign = -1         // Remember sign of Number.
		Number *= Sign    // Force absolute value of Number.
		}
	if(Decimals < 0)
		Decimals *= -1    // Force absolute value of Decimals.
		
	// Next, convert number to rounded integer and force to string value.
	// (Number contains 1 extra digit used to force rounding)
	Number = "" + Math.floor(Number * Math.pow(10,Decimals + 1) + 5)
	if((Number.substring(1,2) == '.')||((Number + '')=='NaN'))
		return(OriginalNumber) // Number too large to format as specified.
		
	// If length of Number is less than number of decimals requested +1,
	// pad with zeros to requested length.
	if(Number.length < Decimals +1) // Construct pad string.
		{
		for(Count = Number.length; Count <= Decimals; Count++)
		Pad += "0"
		}
	Number = Pad + Number // Pad number as needed.
	if(Decimals == 0){
		// Drop extra digit -- Decimal portion is formatted.
		Number = Number.substring(0, Number.length - 1)
		} 
	else
		{
		// Or, format number with decimal point and drop extra decimal digit.
		Number = Number.substring(0,Number.length - Decimals -1) +
        Separator + Number.substring(Number.length - Decimals -1, Number.length -1)
		}
		
	if (! blnSuppressFrontZero)
		if((Number == "") || (parseFloat(Number) < 1))
			Number="0"+Number // Force leading 0 for |Number| less than 1.
		
	if(Sign == -1)
		Number = "-" + Number  // Set sign of number.
	//return(parseFloat(Number))
	return(Number)
	}



// 
// -------------------------------------------------------
// function FormatAsCurrency()
//
// Formats a value to X.XX for Currency values (returns a string)
// Note: this function rounds to 2 decimals places.
//
//  Parameters:
//		blnSuppressFrontZero - Suppresses the 0 in
//			front of the decimal place if the value is 0.XX.
//		dblNumber - The number to format.
// --------------------------------------------------------
function FormatAsCurrency(blnSuppressFrontZero, dblNumber)
	{
	return NonBankersRound(blnSuppressFrontZero, dblNumber, 2);
	}
