//The following functions are included in this javascript validation file

//	checkmandatory		used to determine if required fields are populated
//	TrimTrailing		used with checkmandatory() to remove leading and trailing spaces
//	valDate			used to validate a date field
//	validate		called from valDate() to validate date
//	GetMM			called from validate() - used to retrieve month from mmddyy value
//	GetDD			called from validate() - used to retrieve day from mmddyy value
//	GetYY			called from validate() - used to retrieve year from mmddyy value
//	Trim			another function to remove particular characters (including spaces) from field
//	LTrim			called from Trim() - removes specified characters from the left of the field
//	RTrim			called from Trim() - removes specified characters from the right of the field
//	reformat		reformats a phone/fax number in the following format: (xxx) xxx-xxxx
//	chkZip			used to determine if a zip code is in the proper format: xxxxx or xxxxx-xxxx
//	chkEmail		used to determine if an email address is in the proper format
//	chkPhone		used to determine if a phone number is in the proper format
//	chkFax			used to determine if a fax number is in the proper format
//	checkInt		used to determine if incoming parameter is an integer
//	checkFloat		used to determine if incoming parameter is a decimal
//	checkChar		used to determine if incoming parameter is a string
//	confirmPassword		used to compare the password field and confirm password field
//  pageSubmit		when the page submits, this function is called to check all fields with an answer type
//  CheckLen       used to determine if a textarea is over the answerMaxLength
//  re				used for removing commas for parseFloat
//	chkSSN			used to check for social security string - added 12/10/03 by REM

var re = /[$\,]/g;

//----------------------------------------------------------------------------------
function checkRequired(objName, strField) 
{
	var field = objName;
	if (field.value == null || TrimTrailing(field.value) == "")
	{
		//alert(strField + " is required.  Please enter a valid value.");
		alert(strField + " is required.  Please enter a value.");
		if (field.type !='hidden' && field.disabled !=true){
			field.focus();
		}
		return false;
	}
	else 
		return true;
}

//----------------------------------------------------------------------------------
function checkmandatory(objName, strField) 
{
	var field = objName;
	if (field.value == null || TrimTrailing(field.value) == "")
	{
		//alert(strField + " is required.  Please enter a valid value.");
		bOK=confirm(strField + " is required.  Please enter a valid value.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
		if (bOK) field.focus();
		return false;
	}
	else 
	{
		return true;
   	}
}


//this function is used in conjunction with checkmandatory
function TrimTrailing(strToBeTrimmed) 
{
	while(strToBeTrimmed.charAt(strToBeTrimmed.length-1)==' ' && strToBeTrimmed.length > 0)
		strToBeTrimmed=strToBeTrimmed.substring(0,strToBeTrimmed.length-1);
	return strToBeTrimmed;
}



//----------------------------------------------------------------------------------
function valDate(objName,strField) 
	{
	//validate incoming date
	var field = objName;
	var mmddyy = field.value;

	//retrieve month, day, year values from date 
	var mm = GetMM(mmddyy); 
	var dd = GetDD(mmddyy); 
	var yy = GetYY(mmddyy); 

	rtnval = validate(mm,dd,yy); 
	if (rtnval==1) 
		{ 
		//alert("The " + strField + " entered is not a valid date for the following reasons:\nIncorrect month, day, and/or year\nIncorrect format - Please re-enter the date in the following format: mm/dd/yyyy");
		bOK=confirm("The " + strField + " entered is not a valid date for the following reasons:\nIncorrect month, day, and/or year\n format - Please re-enter the date in the following format: mm/dd/yyyy\n\nPress OK to return to the field, or Cancel to remain on the next field.");
		if (bOK) field.focus(); 
		return false; 
		} 
	} 

function validate(mm,dd,yy) 
	{ 
	//initialize error flag 
	var err = 0; 

	//perform validation checks 
	if (mm.length>2 || mm.length<1) err = 1
	if (dd.length>2 || dd.length<1) err = 1
	if (yy.length!=4) err = 1
	if (mm<1 || mm>12) err = 1
	if (dd<1 || dd>31) err = 1
	if (yy<1970) err = 1
	if (mm==4 || mm==6 || mm==9 || mm==11)
		{
		if (dd==31) err = 1
		}
	if (mm==2)
		{
		var g=parseInt(yy/4)
		if (isNaN(g))
			{
			err = 1
			}
		if (dd>29) err=1
		if (dd==29 && ((yy/4)!=parseInt(yy/4))) err=1 
		} 

	//return to calling function 
	if (err==1) 
		{ 
		return 1; 
		} 
	
	//no errors 
	return 0; 
	} 


//this function is used to extract the month from an incoming date parm
function GetMM(mmddyy) 
	{ 
	mm=mmddyy.substring(0, mmddyy.indexOf("/")); 

	//modify month field if month is one digit (used for the compare) 
	if (mm.length == 1) 
		{ 
		mm = String(0 + mm) 
		}
	return mm; 
	} 


//this function is used to extract the day from an incoming date parm
function GetDD(mmddyy) 
	{ 
	dd=mmddyy.substring(mmddyy.indexOf("/")+1, mmddyy.lastIndexOf("/")); 

	//modify day field if day is one digit (used for the compare) 
	if (dd.length == 1) 
		{ 
		dd = String(0 + dd) 
		} 
	return dd; 
	} 


//this function is used to extract the year from an incoming date parm
function GetYY(mmddyy) 
	{
	
	//this function should only return the four digit year - two digit years are not allowed 
	yy=mmddyy.substring(mmddyy.lastIndexOf("/")+1, mmddyy.length); 

	//modify year field if year is 4 digits (used for the compare) 
	//if (yy.length == 2) 
	//	{ 
	//	if (yy > 70) 
	//		{ 
	//		yy = (19 + yy); 
	//		} 
	//	else 
	//		{ 
	//		yy = (20 + yy); 
	//		} 
	//	}
	return yy; 
	}


//----------------------------------------------------------------------------------
//this function validates an incoming zip code
function chkZip(objName)  
	{
	//validate zip code	
	ValidChars = "0123456789-";
	NumChars = "0123456789";
	var c;
	var field = objName;
	var ZipCode = field.value;
 	
  	//first check to see if a zip code has been entered
  	if (Trim(ZipCode) != '')
  		{
  
		//Check for invalid Characters
		for (var x = 0; x < ZipCode.length; x++)  
			{
   			c = ZipCode.charAt(x);
   			if (ValidChars.indexOf(c) == -1) 
				{
				//alert("The zip code you have entered is invalid.\nValid formats are: 99999 or 99999-9999.");
				bOK=confirm("The zip code you have entered is invalid.\nValid formats are: 99999 or 99999-9999.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
				if (bOK) field.focus();
				return false;
				}
			}
	
		if (ZipCode.length == 5)
			{
			if (ZipCode.indexOf('-') == -1)
				{
				return true;
				} 
			else
				{
				//alert("The zip code you have entered is invalid.\nValid formats are: 99999 or 99999-9999.");
				bOK=confirm("The zip code you have entered is invalid.\nValid formats are: 99999 or 99999-9999.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
				if (bOK) field.focus();
				return false;
				}
			}
		
		if (ZipCode.length == 10)
			{
			if ((ZipCode.indexOf('-') != ZipCode.lastIndexOf('-')) || (ZipCode.indexOf('-') != 5))
				{
				//alert("The zip code you have entered is invalid.\nValid formats are: 99999 or 99999-9999.");
				bOK=confirm("The zip code you have entered is invalid.\nValid formats are: 99999 or 99999-9999.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
				if (bOK) field.focus();
				return false;
				} 
			else
				{
				return true;
				}
			}
		//alert("The zip code you have entered is invalid.\nValid formats are: 99999 or 99999-9999.");
		bOK=confirm("The zip code you have entered is invalid.\nValid formats are: 99999 or 99999-9999.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
		if (bOK) field.focus();
		return false;	
		}	
}


//----------------------------------------------------------------------------------
//the function is used to format the phone number and fax number fields (if filled)   
function reformat (s)
	{
	var arg;
	var sPos = 0;
	var resultString = "";

	for (var i = 1; i < reformat.arguments.length; i++) 
		{
	        arg = reformat.arguments[i];
        	if (i % 2 == 1) resultString += arg;
	        else 
			{
			resultString += s.substring(sPos, sPos + arg);
			sPos += arg;
			}
		}
	return resultString;
	}


//use this function to return a string with no leading or trailing spaces
function Trim(myString)
	{
	return LTrim(RTrim(myString));
	}


//removes leading spaces from a string [used with function TRIM()]		
function LTrim(myString)
	{
	return myString.replace(/^\s+/,'');
	}


//removes trailing spaces from a string	[used with function TRIM()]
function RTrim(myString)
	{
	return myString.replace(/\s+$/,'');
	}


//----------------------------------------------------------------------------------
//use this function to validate an email address
function chkEmail(objName)
	{   
	    
	// there must be >= 1 character before @
	var i = 1;
    	var field = objName;
	var sEmail = field.value;
    	var sLength = sEmail.length;
	
	//first check to see if empty, if not, validate.
	if (Trim(sEmail) != '')
		{
	    // look for @
		while ((i < sLength) && (sEmail.charAt(i) != "@"))
			{
			i++
			}

		if ((i >= sLength) || (sEmail.charAt(i) != "@")) 
			{
			//alert("The email address provided is invalid.\nPlease re-enter a valid email address.");
			bOK=confirm("The email address provided is invalid.\nPlease re-enter a valid email address.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
			if (bOK) field.focus();
			return false;
			}
		else
			{
			i += 2;
			}

		// look for .
		while ((i < sLength) && (sEmail.charAt(i) != "."))
		{ i++
		}

		// there must be at least one character after the .
		if ((i >= sLength - 1) || (sEmail.charAt(i) != "."))
			{
			//alert("The email address provided is invalid.\nPlease re-enter a valid email address.");
			bOK=confirm("The email address provided is invalid.\nPlease re-enter a valid email address.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
			if (bOK) field.focus();
			return false;
			}
		}	
	}

//----------------------------------------------------------------------------------
//use this function to validate a phone number
function chkPhone(objName) {
	// check phone number field
	var field = objName;
	var Phone = field.value;
	var newPhone = '';
			
	if (Trim(Phone) != '') {
		for(i=0; i < Phone.length;i++) {
			charPhone = Phone.charAt(i);
			if (charPhone >= "0" && charPhone <= "9") {
				newPhone = newPhone + charPhone;
			}  
		}
			
		if (newPhone.length != 10) {
			//alert("The phone number entered appears to be invalid.\nPlease re-enter the phone number in the following format: (###)###-####.");
			bOK=confirm("The phone number entered appears to be invalid.\nPlease re-enter the phone number in the following format: (###)###-####.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
			if (bOK) field.focus();
			return false;
		} else {
			field.value = reformat (newPhone, "(", 3, ") ", 3, "-", 4);
		}	
	}	
	//no errors found
	return true;
}


//----------------------------------------------------------------------------------
//use this function to validate a fax number
function chkFax(objName) {
	// check fax number field
	var field = objName;
	var Fax = field.value;
	var newFax = '';
			
	if (Trim(Fax) != '') 
		{
		for(i=0; i < Fax.length;i++) {
			charFax = Fax.charAt(i);
			if (charFax >= "0" && charFax <= "9") {
				newFax = newFax + charFax;
			}  
		}
				
		if (newFax.length != 10) {
			//alert("The fax number entered appears to be invalid.\nPlease re-enter the fax number in the following format: (###)###-####.");
			bOK=confirm("The fax number entered appears to be invalid.\nPlease re-enter the fax number in the following format: (###)###-####.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
			if (bOK) field.focus();
			return false;
		} else {
			field.value = reformat (newFax, "(", 3, ") ", 3, "-", 4);
		}	
	}	
	//no errors
	return true;
}


//----------------------------------------------------------------------------------
//use this function to test a string field
function checkChar(objName, strField)  
{
	ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
//	var strTest = new String(objName.value.toLowerCase());
	var strTemp = "";
  	var c;
	//Check for invalid Characters
	for (var x = 0; x < objName.value.length; x++)  
	{
   		c = objName.value.charAt(x);
   		if (ValidChars.indexOf(c) == -1) 
		{
			//alert("The " + strField + " you have entered is invalid.\nPlease enter a valid string.");
			bOK=confirm("The " + strField + " you have entered is invalid.\nPlease enter a valid string.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
			if (bOK) objName.focus();	
			return false;
		}
	}
	return true;
}


//----------------------------------------------------------------------------------
//use this function to test a decimal field
function checkFloat(objName, strField)  
{
	NumChars = "0123456789.-";
	ValidChars = "0123456789.$,-+";
  	var strTemp = "";
  	var c;
  	for (var x = 0; x < objName.value.length; x++)  
	{
   		c = objName.value.charAt(x);
   		if (ValidChars.indexOf(c) == -1) 
		{
			//alert("The " + strField + " you have entered is invalid.\nPlease enter a valid decimal number.");
			bOK=confirm("The " + strField + " you have entered is invalid.\nPlease enter a valid decimal number.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
			if (bOK) objName.focus();	
			return false;
		}
	}
	//Clean out non-numeric characters
  	for (var x = 0; x < objName.value.length; x++)  
	{
   		c = objName.value.charAt(x);
   		if (NumChars.indexOf(c) != -1) 
		{
			strTemp = strTemp + c
		}
	}
	strTemp = parseFloat(strTemp);
	if (isNaN(strTemp)) 
	{
		//alert("The " + strField + " you have entered is invalid.\nPlease enter a valid decimal number.");
		bOK=confirm("The " + strField + " you have entered is invalid.\nPlease enter a valid decimal number.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
		if (bOK) objName.focus();	
		return false;
	}
	else  
	{
		if (document.frmSection.hidDecimalPlaces.value == 0) {
			var iDecIdx = objName.value.indexOf(".");
			if (iDecIdx > -1) {
				if (parseInt(objName.value.substr(iDecIdx+1)) > 0){
					bOK=confirm("You must enter only whole number values.\nPlease enter a valid whole number.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
					if (bOK) objName.focus();	
					return false;					
				}
			}
		}
//		objName.value = strTemp;
		return true;
	}
}


//----------------------------------------------------------------------------------
//use this function to test a decimal field that should ALWAYS allow decimal values
function checkAlwaysFloat(objName, strField)  
{
	NumChars = "0123456789.-";
	ValidChars = "0123456789.$,-+";
  	var strTemp = "";
  	var c;
  	for (var x = 0; x < objName.value.length; x++)  
	{
   		c = objName.value.charAt(x);
   		if (ValidChars.indexOf(c) == -1) 
		{
			//alert("The " + strField + " you have entered is invalid.\nPlease enter a valid decimal number.");
			bOK=confirm("The " + strField + " you have entered is invalid.\nPlease enter a valid decimal number.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
			if (bOK) objName.focus();	
			return false;
		}
	}
	//Clean out non-numeric characters
  	for (var x = 0; x < objName.value.length; x++)  
	{
   		c = objName.value.charAt(x);
   		if (NumChars.indexOf(c) != -1) 
		{
			strTemp = strTemp + c
		}
	}
	strTemp = parseFloat(strTemp);
	if (isNaN(strTemp)) 
	{
		//alert("The " + strField + " you have entered is invalid.\nPlease enter a valid decimal number.");
		bOK=confirm("The " + strField + " you have entered is invalid.\nPlease enter a valid decimal number.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
		if (bOK) objName.focus();	
		return false;
	}
	else  
	{
//		objName.value = strTemp;
		return true;
	}
}



//----------------------------------------------------------------------------------
//use this function to test a integer field
function checkInt(objName, strField)  
{
	ValidChars = "0123456789,$+-";
	NumChars = "0123456789-";
  	var strTemp = "";
  	var c;
	//Check for invalid Characters
	for (var x = 0; x < objName.value.length; x++)  
	{
   		c = objName.value.charAt(x);
   		if (ValidChars.indexOf(c) == -1) 
		{
			//alert("The " + strField + " you have entered is invalid.\nPlease enter a valid integer.");
			bOK=confirm("The " + strField + " you have entered is invalid.\nPlease enter a valid integer.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
			if (bOK) objName.focus();	
			return false;
		}
	}

	//Clean out non-numeric characters
  	for (var x = 0; x < objName.value.length; x++)  
	{
   		c = objName.value.charAt(x);
   		if (NumChars.indexOf(c) != -1) 
		{
			strTemp = strTemp + c;
		}
	}
	strTemp = parseFloat(strTemp);
	if (isNaN(strTemp)) 
	{
		//alert("The " + strField + " you have entered is invalid.\nPlease enter a valid integer.");
		bOK=confirm("The " + strField + " you have entered is invalid.\nPlease enter a valid integer.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
		if (bOK) objName.focus();	
		return false;
	}
	else  
	{
		objName.value = strTemp;
		return true;
	}
}

//----------------------------------------------------------------------------------
//use this function to test a non negative decimal field
function checkNonNegativeFloat(objName, strField)  
{
	NumChars = "0123456789.-";
	ValidChars = "0123456789.$,-+";
  	var strTemp = "";
  	var c;
  	for (var x = 0; x < objName.value.length; x++)  
	{
   		c = objName.value.charAt(x);
   		if (ValidChars.indexOf(c) == -1) 
		{
			//alert("The " + strField + " you have entered is invalid.\nPlease enter a valid  number.");
			bOK=confirm("The " + strField + " you have entered is invalid.\nPlease enter a valid decimal number.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
			if (bOK) objName.focus();	
			return false;
		}
	}
	//Clean out non-numeric characters
  	for (var x = 0; x < objName.value.length; x++)  
	{
   		c = objName.value.charAt(x);
   		if (NumChars.indexOf(c) != -1) 
		{
			strTemp = strTemp + c
		}
	}
	strTemp = parseFloat(strTemp);
	if (isNaN(strTemp)) 
	{
		//alert("The " + strField + " you have entered is invalid.\nPlease enter a valid decimal number.");
		bOK=confirm("The " + strField + " you have entered is invalid.\nPlease enter a valid decimal number.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
		if (bOK) objName.focus();	
		return false;
	}
	else  
	{
		if (document.frmSection.hidDecimalPlaces.value == 0) {
			var iDecIdx = objName.value.indexOf(".");
			if (iDecIdx > -1) {
				if (parseInt(objName.value.substr(iDecIdx+1)) > 0){
					bOK=confirm("You must enter only whole number values.\nPlease enter a valid whole number decimal number.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
					if (bOK) objName.focus();	
					return false;					
				}
			}
		}
//		objName.value = strTemp;
		if (objName.value <= 0)
		{
			bOK=confirm("The " + strField + " you have entered is invalid.\nPlease enter a decimal number greater than zero.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
			if (bOK) objName.focus();	
			return false;
		}
		return true;
	}
}

//---------------------------------------------------
function checkEl(oEl){
	var bReturnVal;
	var iIdx;
	var oEl;
	var iPos;
	var iAnswerType;
  var iLastPos;
  var sFieldName;

	bReturnVal=true;  //init this
	for (iIdx=1;iIdx<2;iIdx++){         //put this here so the break will work
      // 7/12/02 rjd Change method for getting answer type
      //iPos = oEl.name.lastIndexOf("_");
      //iAnswerType = oEl.name.substr(iPos+1);
      sFieldName = oEl.name
      iPos = sFieldName.indexOf("_")
      iPos = sFieldName.indexOf("_", iPos + 1)
      iLastPos = sFieldName.lastIndexOf("_")
      if (iPos == -1)
        iPos = iLastPos;

      if (iPos == iLastPos)
        {
          // get the last part
          iAnswerType = sFieldName.substr(iPos + 1);
        }
      else
        {
          // extract third piece
          iLastPos = sFieldName.indexOf("_", iPos + 1)
          iAnswerType = sFieldName.substring(iPos + 1, iLastPos);
        }

			switch (parseInt(iAnswerType)){
				case 71:			//ANSWER_INTEGER
					if (oEl.value != ''){
						bReturnVal = checkInt(oEl,'field');
						if (bReturnVal==false){
							iIdx=endCount;
							break;
						}
					}
					break;
				case 290:			//ANSWER_DECIMAL
				case 446:			//ANSWER_BUDGET_VALUE
				case 1163:			//custom col decimal
					if (oEl.value != ''){ 
						bReturnVal = checkFloat(oEl,'field');
						if (bReturnVal==false){
							iIdx=endCount;
							break;
						}
					}
					break;
				case 1330:			//greater than zero decimal.
					if (oEl.value != ''){ 
						bReturnVal = checkNonNegativeFloat(oEl,'field');
						if (bReturnVal==false){
							iIdx=endCount;
							break;
						}
					}
					break;
				case 1279:			//ANSWER_ALWAYS_DECIMAL
					if (oEl.value != ''){ 
						bReturnVal = checkAlwaysFloat(oEl,'field');
						if (bReturnVal==false){
							iIdx=endCount;
							break;
						}
					}
					break;
				case 635:			//ANSWER_PERCENT
					if (oEl.value != ''){
						//bReturnVal = checkFloat(oEl,'field');
						bReturnVal = checkAlwaysFloat(oEl,'field');    //---- 9/27/02 jrh
						if (bReturnVal==false){
							iIdx=endCount;
							break;
						}
						if (oEl.value >100){
							//alert('Percentage field cannot exceed 100%.');
							bOK=confirm('Percentage field cannot exceed 100%.\n\nPress OK to return to the field, or Cancel to remain on the next field.');
							bReturnVal=false;
							if (bOK) oEl.focus();
							iIdx=endCount;
							break;
						}
					}
					break;
				case 134: 			//ANSWER_DATE
					if (oEl.value != ''){
						bReturnVal = valDate(oEl,'field');
						if (bReturnVal==false){
							iIdx=endCount;
							break;
						}
					}
					break;
				case 566:			//ANSWER_EMAIL
					if (oEl.value != ''){
						bReturnVal = chkEmail(oEl);
						if (bReturnVal==false){
							iIdx=endCount;
							break;
						}
					}
					break;
				case 567:			//ANSWER_PHONE
					if (oEl.value != ''){
						bReturnVal = chkPhone(oEl);
						if (bReturnVal==false){
							iIdx=endCount;
							break;
						}
					}
					break;
				case 634:			//equipment unit cost
					if (oEl.value != ''){
						bReturnVal = checkFloat(oEl,'field');
						if (bReturnVal==false){
							iIdx=endCount;
							break;
						}
						if (oEl.value < 1500) {
							//alert("Unit cost of equipment must be greater than $1500");
							bOK=confirm("Unit cost of equipment must be greater than $1500.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
							bReturnVal=false;
							iIdx=endCount;
							oEl.value=0;
							if (bOK) oEl.focus();
							break;
						}
					} else {
						alert('Unit cost is required to add equipment detail.');
						bReturnVal=false;
						break;
					}
					break;
				case 794:			//equipment unit cost 3000 (ANSWER_EQUIP_UNITCOST_3000)
					if (oEl.value != ''){
						bReturnVal = checkFloat(oEl,'field');
						if (bReturnVal==false){
							iIdx=endCount;
							break;
						}
						if (oEl.value < 3000) {
							//alert("Unit cost of equipment must be greater than $3000");
							bOK=confirm("Unit cost of equipment must be greater than $3000.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
							bReturnVal=false;
							iIdx=endCount;
							oEl.value=0;
							if (bOK) oEl.focus();
							break;
						}
					} else {
						alert('Unit cost is required to add equipment detail.');
						bReturnVal=false;
						break;
					}
					break;
				case 482:		//budget detail value
					if (oEl.value != ''){
						bReturnVal = checkInt(oEl,'field');
						if (bReturnVal==false){
							iIdx=endCount;
							break;
						}
					}			
					break;
				case 1409:			//ANSWER_DECIMAL_FOR_FER
			}  //end of switch statement
	} //end of phony for loop
	return bReturnVal
}


//==========================================================================================
//the big submit script that fires off the whole ball of wax.
function pageSubmit(theForm){
	var bReturnVal = true;
	var bCheckingRadio = false;
	var bFoundRadioCheck=false;
	var endCount = theForm.elements.length;
	var iIdx;
	var oEl;
	var iPos;
	var iAnswerType;
  var iLastPos;
  var sFieldName
	
	
// commented out due to netscape error - jds 02.15.02 	document.body.style.cursor='wait';
	for (iIdx=0; iIdx<endCount; iIdx++){
		oEl = theForm.elements[iIdx];
		// 7/12/02 rjd Change method for getting answer type
		//iPos = oEl.name.lastIndexOf("_");
		//iAnswerType = oEl.name.substr(iPos+1);
		sFieldName = oEl.name
		iPos = sFieldName.indexOf("_")
		iPos = sFieldName.indexOf("_", iPos + 1)
		iLastPos = sFieldName.lastIndexOf("_")
		if (iPos == -1) {
			iPos = iLastPos;
		}

		if (iPos == iLastPos) {
			// get the last part
			iAnswerType = sFieldName.substr(iPos + 1);
		} else {
		    // extract third piece
		    iLastPos = sFieldName.indexOf("_", iPos + 1)
		    iAnswerType = sFieldName.substring(iPos + 1, iLastPos);
		}

		if (oEl.name.substr(0,3)=='tbl' || (oEl.name.substr(0,1)=='b' && !isNaN(parseInt(oEl.name.substr(1,1))))){
			//first, if it is a narrative, and we're marking complete, make sure the answer has a value
			//jds - added single quotes around 1

			// 8/19/02 jrh - required per question flag
			if (!oEl.required) {
				bQuestionReq = false;
			} else {
				if (oEl.required==true){
					bQuestionReq = true;
				}else{
					bQuestionReq = false;
				}
			}

			if (theForm.hidAreaType.value=='1') {
				bAllAnswersReq=true;
			}else{
				bAllAnswersReq=false;
			}
			
			//if (theForm.hidAreaType.value=='1' || bQuestionReq==true){
			if (bAllAnswersReq==true || bQuestionReq==true){
				//we are in a narrative section
				//jds - added single quotes around 1 & 3
				if (theForm.hidSaveType.value=='1' || theForm.hidSaveType.value=='3'){
					//user did hit mark complete
					if (oEl.type=='radio'){
						if (bCheckingRadio){
							if (oEl.name != sElement) {		//there were two seperate groups of radio buttons, one right after the other
								if (!bFoundRadioCheck){			//we just finished a group of radio buttons, were any checked?
									alert ("Please make sure you have picked an option for all radio-button type questions before marking the section complete.")
									// commented out due to netscape error - jds 02.15.02 document.body.style.cursor='default';		
									return false;
								}
								bFoundRadioCheck=false;
							}
						} 
						bCheckingRadio=true;
						sElement = oEl.name;
						if (oEl.checked){
							bFoundRadioCheck=true;
						}
					}else{
						if (bCheckingRadio){			
							if (!bFoundRadioCheck){			//we just finished a group of radio buttons, were any checked?
								alert ("Please make sure you have picked an option for all radio-button type questions before marking the section complete.")
								// commented out due to netscape error - jds 02.15.02	document.body.style.cursor='default';
								return false;
							}
						}
						bCheckingRadio=false;
						bFoundRadioCheck=false;				//reset this in case there is another batch of radio buttons later.
						if (parseInt(iAnswerType)==140){  //checkbox
							// for now we are ignoring checkboxes - because as is, IE always ignores them and NS 6.2 always catches them.
						}else{
						if (parseInt(iAnswerType)==141){  //checkbox other
							if (oEl.checked){
								if (theForm.elements[iIdx+1].value.length < 1) { 
									//alert ("Please complete all questions before marking the section complete.");
									sAlertMsg = 'Please complete all ';
									if (bAllAnswersReq!=true) sAlertMsg = sAlertMsg+'required ';
									sAlertMsg = sAlertMsg+'questions before marking the section complete.\n\nPress OK to return to the field, or Cancel to remain on the next field.';
									bOK=confirm(sAlertMsg);
									if (theForm.elements[iIdx+1].type != 'hidden' && theForm.elements[iIdx+1].disabled !=true){
										if (bOK) theForm.elements[iIdx+1].focus();
									}
									// commented out due to netscape error - jds 02.15.02	document.body.style.cursor='default';
									return false;
								}
							}else{
								iIdx++;  //skip the textbox for the other
							}
						} else {
							if (parseInt(iAnswerType)==139){  //radio other
								if (theForm.elements[iIdx-1].checked){
									if (oEl.value=='' || !oEl.value){
										//alert ("Please complete all questions before marking the section complete.")
										sAlertMsg = 'Please complete all ';
										if (bAllAnswersReq!=true) sAlertMsg = sAlertMsg+'required ';
										sAlertMsg = sAlertMsg+'questions before marking the section complete.\n\nPress OK to return to the field, or Cancel to remain on the next field.';
										bOK=confirm(sAlertMsg);
										if (oEl.type != 'hidden'  && oEl.disabled !=true){
											if (bOK) oEl.focus();
										}
										// commented out due to netscape error - jds 02.15.02	document.body.style.cursor='default';
										return false;
									}
								}
							} else {
								//listboxes (booleans, etc) need special due to netscape
								if (oEl.type.substr(0,6)=='select'){
									if (oEl.selectedIndex<0){
										if (theForm.hidSaveType.value=='3'){
											//alert ("Please complete all row fields before adding a row for the question.")
											sAlertMsg = 'Please complete all ';
											if (bAllAnswersReq!=true) sAlertMsg = sAlertMsg+'required ';
											sAlertMsg = sAlertMsg+'row fields before adding a row for the question.\n\nPress OK to return to the field, or Cancel to remain on the next field.';
											bOK=confirm(sAlertMsg);
										}else{
											//alert ("Please complete all questions before marking the section complete.")
											sAlertMsg = 'Please complete all ';
											if (bAllAnswersReq!=true) sAlertMsg = sAlertMsg+'required ';
											sAlertMsg = sAlertMsg+'questions before marking the section complete.\n\nPress OK to return to the field, or Cancel to remain on the next field.';
											bOK=confirm(sAlertMsg);
										}
										if (oEl.type != 'hidden'  && oEl.disabled !=true){
											if (bOK) oEl.focus();
										}
										return false;
									}
								} else {
									if (oEl.value=='' || !oEl.value){
									//jds - add single quotes around 3
										if (theForm.hidSaveType.value=='3'){
											//alert ("Please complete all row fields before adding a row for the question.")
											sAlertMsg = 'Please complete all ';
											if (bAllAnswersReq!=true) sAlertMsg = sAlertMsg+'required ';
											sAlertMsg = sAlertMsg+'row fields before adding a row for the question.\n\nPress OK to return to the field, or Cancel to remain on the next field.';
											bOK=confirm(sAlertMsg);
										}else{
											//alert ("Please complete all questions before marking the section complete.")
											sAlertMsg = 'Please complete all ';
											if (bAllAnswersReq!=true) sAlertMsg = sAlertMsg+'required ';
											sAlertMsg = sAlertMsg+'questions before marking the section complete.\n\nPress OK to return to the field, or Cancel to remain on the next field.';
											bOK=confirm(sAlertMsg);
										}
										if (oEl.type != 'hidden'  && oEl.disabled !=true){
											if (bOK) oEl.focus();
										}
										// commented out due to netscape error - jds 02.15.02	document.body.style.cursor='default';
										return false;
									}
								}
							}
						}
						}  //end of special types check
					}
				}
			}

			bReturnVal = checkEl(oEl);
			if (bReturnVal == false)
				{
				break;
				}
		
		}		//end of if a real question conditional		
	}			//end of for each element loop

	//in case radio buttons were last questions on there:
	if (bCheckingRadio){			
		if (!bFoundRadioCheck){			//we just finished a group of radio buttons, were any checked?
			alert ("Please make sure you have picked an option for all radio-button type questions before marking the section complete.")
// commented out due to netscape error - jds 02.15.02		document.body.style.cursor='default';
			return false;
		}
	}
// commented out due to netscape error - jds 02.15.02	document.body.style.cursor='default';
	if (bReturnVal != false)
		{
		bReturnVal = true;
		}
	return bReturnVal;
}
//
function CheckLen(sObject, iMaxLen){
		if (sObject.value.length > iMaxLen)
			{
			//alert("You have exceeded the maximum length!\n      Please modify your response.")
			bOK=confirm("You have exceeded the maximum length!\n      Please modify your response.\n\nPress OK to return to the field, or Cancel to remain on the next field.")
			if (sObject.type !='hidden' && sObject.disabled !=true){
				if (bOK) sObject.focus();
			}
			return false;
			}
		else
			{
			return true;
			}
	}

//----------------------------------------------------------------------------------
//use this function to validate a phone number
function chkSSN(objName) {
	// check phone number field
	var field = objName;
	var SSN = field.value;
	var newSSN = '';
			
	if (Trim(SSN) != '') {
		for(i=0; i < SSN.length;i++) {
			charSSN = SSN.charAt(i);
			if (charSSN >= "0" && charSSN <= "9") {
				newSSN = newSSN + charSSN;
			}  
		}
			
		if (newSSN.length != 9) {
			//alert("The phone number entered appears to be invalid.\nPlease re-enter the phone number in the following format: (###)###-####.");
			bOK=confirm("The SSN number entered appears to be invalid.\nPlease re-enter your SSN number in the following format: ###-###-####.\n\nPress OK to return to the field, or Cancel to remain on the next field.");
			if (bOK) field.focus();
			return false;
		} else {
			field.value = reformat (newSSN, "", 3, "-", 2, "-", 4);
		}	
	}	
	//no errors found
	return true;
}
