/*

editscripts.js

(c) Copyright 2001
box2 technologies llc
all rights reserved
http://www.box2.com/

version 0109.19
- Now unselect inputs that have been corrected
*/

var pageChanged = false;
var arrFormChecks = new Array();
var checkStatus, checkMessage, checkFirstObject;
var isNN = (document.layers);
var isIE = (document.all);
var restoreColorValues = new Array(); //an array that holds original color values prior to checkERror changing them
var elementCounter; //used to allow us to index through an array

function addCheck(formCheck)
{
	arrFormChecks[arrFormChecks.length] = formCheck;
}


function checkDate(fieldObj,fieldName)
{
	/*	 
	Date: 9-24-01 3:23pm 
	Author: Sam Chehab
	Check to ensure the value passed is in a date format.  
		1 - check to see if the formatting of the date is proper, using regex to do that.
		2 - checking to make sure the month and day values aren't too high or 0
		3 - comparing the month value to the day value to see if its legit.
	*/
		
		
		//Variable Declarations
	var reDate = /^[0-3]?\d\/[0-3]?[\d]\/\d\d\d\d/
 	var arrDate = fieldObj.value.match(reDate);
	
	var temp = fieldObj.value;
	var dateArray = temp.split("/");
    var month = dateArray[0];
    var day = dateArray[1];
    var year = dateArray[2];
    //Declare and array and compare the month values to the arguments			
	var daysInMonth = new Array(12);
				daysInMonth[1]  = 31;
				daysInMonth[2]  = 29;   
				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;
        
    //if nothing was passed in return true and exit function
     if (fieldObj.value.length == 0){
    		return true;
  	 }
    //if value matches regex, then we dig deeper to see if there are that many days in the month
    if (arrDate){
    
 	  
		//Converting to int so mac IE doesn't get upset with us
    	var dayInt = parseInt(day,10);
		var monthInt = parseInt(month,10);
		var yearInt = parseInt(year);
	
		if (monthInt > 12 || monthInt == 0){
			checkError(fieldObj, fieldName + ' has an invalid entry for month.\n');
			return false;
		}
		//Make sure they don't have  
		else if (dayInt > 31 || dayInt == 0){
			checkError(fieldObj, fieldName + ' has an invalid entry for day.\n');
			return false;
		}
	
		else{
				//Does the actual comparison of the day vs the month's actual # of days
	            //If correct, then return true, otherwise return false.
	            	
	           if(dayInt > daysInMonth[monthInt] ){
	           		checkError(fieldObj, fieldName + ' has an incorrect number of days for this month\n');
		           	return false;
	           }
	           else{
	               	return true; 
	           }
    		   		  			
		}
		
	}	
	//if value entered doesn't match the regex	
	else{
			checkError(fieldObj, fieldName + ' is not a properly formatted date\n');
			return false;
		
		}
}// end of functionCheckDate


function checkStateOrProvince(stateObj, countryID)
{
	if (parseInt(countryID) == 840 || parseInt(countryID) == 124 ) 
	{
		return checkRequired(stateObj,"State/Province required for U.S. and Canada.");
	}
	
	else 
	{
		
		return true;
	}
	


} // End of checkStateOrProvince



function checkError(fieldObj, errText)
{
	if (checkStatus) checkFirstObject = fieldObj;
	
		var re1 = /fieldObj.name/
 		var original = 1;
 		var stringMatch; //used to hold array's contents per element
 		/*
 			 - Walk through entire array 
 			 - Look for fieldObj.name inside each array element
 			 - if not found, add to array, otherwise it will naturally fall out	
 		*/
 		for (elementCounter = 0; elementCounter <= restoreColorValues.length; elementCounter++){	
 			
 			//needed to catch if we are on the last element of the array, since it is undefined
 			
 			if (!(restoreColorValues[elementCounter])) //if null or undefined, we've made it through the array, fall into the loop
 			{
 				//Get current colors from fields
 				 var borderColorTemp = fieldObj.style.borderColor;
			 	 var backgroundColorTemp = fieldObj.style.backgroundColor;
 				 
 				 if (borderColorTemp != "#ff0000") //if already an error color, leave for loop
 				 {
 				 	var arrayLength = restoreColorValues.length + 1;
 				 	restoreColorValues[arrayLength] = fieldObj.name + ',' + borderColorTemp + ',' + backgroundColorTemp;
				 	
				 	break;
				 }
				 
				 else
				 {
				 	//catching error colors and we do not want that	
				 	break;
 				 }
 			}
 			
 			else
 			{
 			
 				stringMatch = restoreColorValues[elementCounter];
				
				if(stringMatch.search(re1))
 				{
 					//If its already in the array, leave the loop
 					break;
 				}
 			
 				else
 				{
 					continue;
 				}
 			
 			}
 	 }//end of for loop
 	
	if (isIE)
	{
		fieldObj.style.borderColor = '#ff0000';
		fieldObj.style.backgroundColor = '#ffff99';
	}
		checkMessage += errText;
}//End of function CheckError


function checkFile(fieldObj, fieldName, extension)
{
	var reFile = new RegExp () // empty constructor
	reFile.compile(extension + '$', 'i');
	var arrFile = fieldObj.value.match(reFile);
	if (arrFile || fieldObj.value.length == 0)
	{
		return true;
	}
	else
	{
		checkError(fieldObj, fieldName + ' is not a ' + extension.toUpperCase() + ' file.\n');
		return false;
	}
}

function checkForm()
{
	checkStatus = true;
	checkFirstObject = "";
	checkMessage = 'The form had the following errors:\n\n';
	var myString;
	//Clean out prior color values in array and let checkError maintain list
	for ( var x = 0; x <= restoreColorValues.length; x++ ){
				
				if (restoreColorValues[x] == "" || !(restoreColorValues[x]))
				{
					continue;	
				}
				/*
				 Break up element restoreColorValues, into
				   - name 
				   - borderColor
				   - backgroundColor
				 Then pass them to restoreColorToCell which actually changes the value back
				*/
						
				var newArray = restoreColorValues[x].split(",");
				
				var fieldObjTEMP    = newArray[0];
				var borderColor     = newArray[1];
				var backgroundColor = newArray[2];
				
				restoreColorToCell(fieldObjTEMP, borderColor, backgroundColor);				
							
				restoreColorValues[x] = "";
				
	}	//end of for loop
	
		
	for (var i = 0; i < arrFormChecks.length; i++)
	{
		if(!eval(arrFormChecks[i])) checkStatus = false;
	}
	if(!checkStatus) checkFirstObject.focus();
	return checkStatus;
}


function checkDecimal(fieldObj,fieldName)
{
	
	
	for (var i = 0; i < fieldObj.value.length; i++)
    {   
        // Check that current character is number.
        var c = fieldObj.value.charAt(i);
    	
		if ((c >= "0") && (c <= "9") || (c == ",") || (c == ".")){
			continue;
		}
		else
		{
			checkError(fieldObj, fieldName + ' is not a properly formatted decimal. Ex: 122.53 \n');
			return false;
		}
	} //end of for

	return true;
	
}//End of Check Decimal


function checkEmail(fieldObj, fieldName)
{
	var reEmail = /^[a-zA-Z0-9_\-\.]+@[a-zA-Zz0-9\-\.]+[\.][a-zA-Z0-9\-\.]+$/
	var arrEmail = fieldObj.value.match(reEmail);
	
	if (arrEmail || fieldObj.value.length == 0)
	{
		return true;
	}
	else
	{
		checkError(fieldObj, fieldName + ' is not a properly formatted email address.\n');
		return false;
	}
}


function checkInteger(fieldObj,fieldName)
{
	  	  
	/* 
		Date: 9-13-01 3:23pm 
		Author: Sam Chehab
	   
	   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 (var i = 0; i < fieldObj.value.length; i++)
    {   
        // Check that current character is number.
        var c = fieldObj.value.charAt(i);

    /*
	   	Either character or number. If a number return true, 
	    otherwise return false which gets converted into a true and we enter the 
	    if condition
    */
    	
	    if ((c >= "0") && (c <= "9")){
			continue;
		}
		else{
			checkError(fieldObj, 'The "' + fieldName + '\" field requires a numeric integer value\n');
        	return false;
		}
    } //End of For Loop

    // All characters are numbers.
    return true;
}

function checkLength(fieldObj, fieldName, fieldLength)
{
	if (fieldObj.value.length <= fieldLength)
	{
		return true;
	}
	else
	{
		checkError(fieldObj, fieldName + ' is too long (' + fieldObj.value.length + ' char.)' + ' The max field length is ' + fieldLength + '\n');
		return false;
	}
}


function checkMoney(fieldObj,fieldName)
{
	/*	 
	Date: 9-17-01 3:23pm 
	Author: Sam Chehab
	Check to ensure the value passed is in a dollar format 
		1 - check if there are no characters inside the string, otherwise considered a valid dollar amount
	*/	
 	    
 	    
 	   	for (var i = 0; i < fieldObj.value.length; i++){ 
				  
        		// Check that current character is number.
        		var c = fieldObj.value.charAt(i);
				
				if (((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) ) {

	 		  		checkError(fieldObj, 'The "' + fieldName + '\" field requires a decimal value\n');
        			return false;
    			} //end of if
    			
    	
    	} // end of for		
    			
    	
    			
    	var reMoney = /[0-9,.$]/
		var arrMoney = fieldObj.value.match(reMoney);
	
		if (arrMoney || fieldObj.value.length == 0)
		{
				return true;
		}
			
		else
		{
				checkError(fieldObj, fieldName + ' is not a properly formatted US currency.\n');
				return false;
		}
    
} //end of checkMoney


function checkNotLower(fieldObj, fieldName, fieldOriginalValue)
{
	
	//If there is a number, then return true and enter into if statement, if fieldObj.length is == to 0 it'll return true
	//which will be converted to false and force it into the else statement 
	
	var fieldObjValue = parseFloat(fieldObj.value);
	var fieldOriginalValue = parseFloat(fieldOriginalValue);
	
	if ((fieldObjValue >= fieldOriginalValue) || fieldObj.value.length == 0 )
	{
		return true;	
	}
	else
	{
		checkError(fieldObj, fieldName + ' must have a value greater than ' + fieldOriginalValue + '.\n');
		return false;
	}
} //end of checkNotLower


function checkNotHigher(fieldObj, fieldName, fieldOriginalValue)
{
		
	if ((parseFloat(fieldObj.value) <= parseFloat(fieldOriginalValue)) || (fieldOriginalValue.length == 0))
	{
		return true;
	}
	else
	{
		checkError(fieldObj, fieldName + ' cannot be higher than the previously entered value of ' + fieldOriginalValue + '.\n');
		return false;
	}
}


function checkPhone(fieldObj, fieldName, countryID)
{
	if (countryID == 840)
	{
		var rePhone = /^[(]?[2-9]\d\d[)-\/]?\s?[2-9]\d\d-?\d\d\d\d$/
		var arrPhone = fieldObj.value.match(rePhone);
		
		if (arrPhone || fieldObj.value.length == 0)
		{
			return true;
		}
		else
		{
			checkError(fieldObj, fieldName + ' is not a properly formatted 10-digit phone number.\n');
			return false;
		}
	}
	else
	{
		
		return true;
	}
}


function checkPostalCode(fieldObj, fieldName, countryID)
{
	if (countryID == 840)
	{
		var reZip = /^\d\d\d\d\d$/
		var reZipplus4 = /^\d\d\d\d\d-\d\d\d\d$/
		var arrZip = fieldObj.value.match(reZip);
		var arrZipplus4 = fieldObj.value.match(reZipplus4);
		
		if (arrZip || arrZipplus4 || fieldObj.value.length == 0)
		{
			return true;
		}
		else
		{
			checkError(fieldObj, fieldName + ' is not a properly formatted U. S. zip code.\n');
			return false;
		}
	}
	else
	{
		return true;
	}
}


function checkRequired(fieldObj, fieldName)
{
	switch (fieldObj.type)
	{
	case "text":
		if (fieldObj.value.length > 0)
		{
			return true;
		}
		else
		{
			checkError(fieldObj, fieldName + ' is a required value.\n');
			return false;
		}
		break;
	case "password":
		if (fieldObj.value.length > 0)
		{
			return true;
		}
		else
		{
			checkError(fieldObj, fieldName + ' is a required value.\n');
			return false;
		}
		break;
	case "file":
		if (fieldObj.value.length > 0)
		{
			return true;
		}
		else
		{
			checkError(fieldObj, fieldName + ' is a required value.\n');
			return false;
		}
		break;
	case "select-one":
		if (fieldObj.selectedIndex > -1 && fieldObj[fieldObj.selectedIndex].value != "")
		{
			return true;
		}
		else
		{
			checkError(fieldObj, fieldName + ' is a required value.\n');
			return false;
		}
		break;
	// is checked attribute 
	 case "checkbox":
	 
	 	if (fieldObj.checked)
		{
			
			return true;
		}
		else
		{
			checkError(fieldObj, fieldName + ' must be checked.\n');
			return false;
		}
		break;
	 
	 
	 		
	default:
		checkError(fieldObj, fieldObj.type);
		
		//checkError(fieldObj, fieldName + ' is of an unknown type.\n');
	}
}


function checkSSN(fieldObj, fieldName)
{
	var reSSN = /^\d\d\d-\d\d-\d\d\d\d$/
	var arrSSN = fieldObj.value.match(reSSN);
	
	if (arrSSN || fieldObj.value.length == 0)
	{
		return true;
	}
	else
	{
		checkError(fieldObj, fieldName + ' is not a properly formatted social security number.\n');
		return false;
	}
}



function checkURL(fieldObj,fieldName)
{
	  	  
     /*  
	   Date: 9-14-01 4:33pm 
	   Author: Sam Chehab
	   
	   Search through string and ensure a proper url has been given. 
	   If it doesn't already have "http://" or "https://" return and error 
     */
   
		var reURL = /^https?:\/\//
		var arrURL = fieldObj.value.match(reURL);
	if (arrURL || fieldObj.value.length == 0)
	{
		return true;
	}
	else{
 	    checkError(fieldObj, fieldName + ' is not a properly formatted URL.\n');
		return false;
	}
	
	

} // end of checkUrlFunction



function editCancel()
{
	pageChanged = false;
	self.close();
}

function editClose()
{
	if (pageChanged)
	{
		var seqwarning = newWindow('warning.asp','seqwarning',400,100);
	}
}


function editSave()
{
	pageChanged = false;
	checkForm() ? document.editform.submit() : alert(checkMessage);
}

function restoreColorToCell(fieldObj, borderColor, backgroundColor )
{
	/* 
	   If borderColorRestore and backgroundColorRestore aren't passed in, they'll be null by default
	   and we'll simply set the borderColor and backgroundColor to white, otherwise we set it to the 
	   values passed in.
	*/ 
	var tempObject;
	tempObject = 'document.editform.';
	tempObject += fieldObj;
	
		if (isIE)
		{ 
 			//Assigning old values back to object
 			eval(tempObject).style.borderColor = borderColor;
 			eval(tempObject).style.backgroundColor = backgroundColor;
 			
 			return true;
 		}
					
		
		else
		{
			return true;	
		}
		
}//End of restoreColorToCell
		

