/*

editscripts.js

(c) Copyright 2000-2002
box2 technologies llc
all rights reserved
http://www.box2.com/

version 20021011


*/

// *** VARIABLE DEFINITIONS ***

var pageChanged = false;
var arrFormChecks = new Array(); //array used to hold all the checks passed in the from the html
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
var isMac = navigator.appVersion.indexOf("Mac") != -1;
var isMacPPC = isMac && (navigator.appVersion.indexOf("PPC") != -1 || navigator.appVersion.indexOf("PowerPC") != -1);


// *** BASIC FUNCTIONS ***

function addCheck(formCheck)
{
	arrFormChecks[arrFormChecks.length] = formCheck;
}


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
 				if (isIE){

					//used to identify radio buttons differently because otherwise object doesn't exist.
					//Example: fieldObj.name will cause undefined to return if the object is a radio button, but not a text field
					var radioButton = 0

 					//must case out radio buttons and select lists because we need to specify array index or we can't get object properties
 					//If its undefined, it isn't a radio button and therefore we need to treat it differently
 					if (fieldObj[0]){

 	
 						if (fieldObj.type == "select-one") {

 							var borderColorTemp	 = fieldObj[0].style.borderColor;
				 			var backgroundColorTemp = fieldObj[0].style.backgroundColor;
 							//Remove radioButton as a flag, because we can't call fieldObj[0].name. It will
 							//return undefined and we'll get problems when we go to set the color back

 						}
 						else {

	 						var borderColorTemp	 = fieldObj[0].style.borderColor;
				 			var backgroundColorTemp = fieldObj[0].style.backgroundColor;
	 						radioButton			 = 1

	 					}

 					}
	 				else
	 				{
	 					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;
	 				 	if (radioButton == 0){
	
		 				 			//All objects are included in here except radio buttons.
			 				 		restoreColorValues[arrayLength] = fieldObj.name + ',' + borderColorTemp + ',' + backgroundColorTemp;
									break;
	
							}
							else{
	
									//This captures radio buttons and retrieves their name
									restoreColorValues[arrayLength] = fieldObj[0].name + ',' + borderColorTemp + ',' + backgroundColorTemp;
									break;
	
							}
						}

					else
				 	{
				 		//catching error colors and we do not want that
 				 		break;
 				 	}
 				}//is IE?


 			}//fill array if possible

 			  else
 			  {

 					stringMatch = restoreColorValues[elementCounter];

					if(stringMatch.search(re1))
 					{
 						//Leave the loop because the object is in the array
 						break;
 					}

 					else
 					{
 						continue;
 					}

 			 }
 	 }//end of for loop

	if (isIE)
	{
		//If the object is a radio button then, set the buttons to error colors, otherwise set the object itself to error colors
		if (fieldObj[0] && fieldObj[0].type == 'radio'){
		// Set all selections for the radio button to error colors
	 		 for (var x=0; x < fieldObj.length; x++) {
	 		 	fieldObj[x].style.borderColor = '#ff0000';
				fieldObj[x].style.backgroundColor = '#ffff99';
	 		 }
	 	}
		else
		{
			fieldObj.style.borderColor = '#ff0000';
			fieldObj.style.backgroundColor = '#ffff99';
		}
	}
		checkMessage += errText;
}//End of function CheckError


function checkForm(formName)
{
	checkStatus = true;
	checkFirstObject = "";
	checkMessage = 'The form had the following errors:\n\n';

	var formsName = formName.name;

	//Clean out prior color values in array and let checkError maintain list
	for ( var x = 0; x <= restoreColorValues.length; x++ ){



				///***Note radio buttons don't make it past here **/
				if (restoreColorValues[x] == "" || !(restoreColorValues[x]))
				{
					continue;
				}
				/*
				 Break up the array restoreColorValues, into element:
				   - object name	 0
				   - borderColor	 1
				   - backgroundColor 2
				 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];


				//Means the object isn't a radio button
				restoreColorToCell(formsName, fieldObjTEMP, borderColor, backgroundColor);



				//Clean array element after finishing color change
				restoreColorValues[x] = "";

	}	//end of for loop


	for (var i = 0; i < arrFormChecks.length; i++)
	{

		if (arrFormChecks[i] != "" ){

			//search for whether or not we're on the same form as the one we submitted.
			//If so lets eval it, otherwise skip

			var temp = arrFormChecks[i]
			var test = (temp =  arrFormChecks[i].indexOf(formName.name));

			//Skip because we can't find the string in the form name
			if(test == "-1"){
				continue;
			}

			else {

				if(!eval(arrFormChecks[i]))
				{
					checkStatus = false;

				};
			}
		}

	}

	if(!checkStatus)
	{
		//If the object is not a button, which will return an undefined status due to index issues, set the focus to it


		if(checkFirstObject.type == 'radio') {
		//if(eval(checkFirstObject.focus())){
			checkFirstObject.focus();
		}
	}
	return checkStatus;
} //end of function checkForm()


function editCancel()
{
	pageChanged = false;
	self.close();
}

function editClose()
{
	if (pageChanged)
	{
		var closewarning = newWindow('warning.asp','closewarning',400,100);
	}
}


function editSave(formName)
{
	pageChanged = false;

	if (!formName) var formName = document.editform;
	var submitForm = "document." ;
	submitForm += formName.name;
	submitForm += ".submit()";

	checkForm(formName) ? eval(submitForm) : alert(checkMessage);
}

function restoreColorToCell(formName, fieldObjName, 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.' + formName + '.' + fieldObjName;

	if (isIE)
	{
		//only change the style back if its a windows machine, don't do it on Macs because it doesn't work
		if ( !(isMac) && !(isMacPPC) ) {
			if (eval(tempObject).name && eval(tempObject).style) {
				//Assigning old values back to object
			 	eval(tempObject).style.borderColor = borderColor;
		 		eval(tempObject).style.backgroundColor = backgroundColor;
			} else {
				var objField = eval(tempObject);
				if (objField[0].style) {
					for(var x=0; x < objField.length; x++) {
						objField[x].style.borderColor = borderColor;
						objField[x].style.backgroundColor = backgroundColor;	
					}
				}
			}
			return true;
		}
	}


	else
	{
		return true;
	}

}//End of restoreColorToCell



// *** CHECK FUNCTIONS ***

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-1]?[0-9][\/-][0-3]?[0-9][\/-][0-9]{4}$/
 	var arrDate = fieldObj.value.match(reDate);

	//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) {

		var temp = fieldObj.value;
		var dateArray = temp.split(temp.charAt(temp.length - 5));

		var month	= dateArray[0];
		var day		= dateArray[1];
		var year	= dateArray[2];

		//Declare an array and compare the month values to the arguments
		var daysInMonth = new Array(12);
		daysInMonth[1]  = 31;
		var isLeapYear = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		isLeapYear ? daysInMonth[2] = 29 : 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;

		//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 bad day
		else if (dayInt > daysInMonth[monthInt] || dayInt == 0) {
			checkError(fieldObj, fieldName + ' has an invalid entry for day.\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 checkDecimal(fieldObj,fieldName)
{
	var reDecimal = /^[-]?[0-9]*([,][0-9]{3})*[.]*[0-9]{0,2}$/
	var arrDecimal = fieldObj.value.match(reDecimal);

	if (arrDecimal || fieldObj.value.length == 0)
	{
		return true;
	}
	else
	{
		checkError(fieldObj, fieldName + ' is not a properly formatted decimal value (ex. 123.45).\n');
		return false;
	}

}//End of Check Decimal


function checkEmail(fieldObj, fieldName)
{
	// re now includes "_" explicitly as some browsers were not inferring it from /w
	var reEmail = /^[_\w\-\.]+@[a-zA-Z0-9\-\.]+[\.][a-zA-Z\-]{2,5}$/
	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 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 checkInteger(fieldObj,fieldName)
{
	var reInteger = /^[-]?[0-9]*([,][0-9]{3})*$/
	var arrInteger = fieldObj.value.match(reInteger);

	if (arrInteger || fieldObj.value.length == 0)
	{
		return true;
	}
	else
	{
		checkError(fieldObj, fieldName + ' is not a properly formatted integer value.\n');
		return false;
	}

}//End of Check Decimal



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)
{
	var reMoney = /^[$]?[-]?[0-9]*([,][0-9]{3})*[.]*[0-9]{0,2}$/
	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 value.\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{5}$/
		var reZipplus4 = /^\d{5}-\d{4}$/
		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)
{
	var fieldType

	fieldType = fieldObj.type ? fieldObj.type : fieldObj[0].type;

	switch (fieldType)
	{
	case "text":
		if (fieldObj.value.length > 0)
		{
			return true;
		}
		else
		{
			checkError(fieldObj, fieldName + ' is a required value.\n');
			return false;
		}
		break;
	case "textarea":
		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 "hidden":
		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;

	case "select-multiple":

		var isSelected = false;

		for (var i=0 ; i < fieldObj.length; i++) {
			if (fieldObj.options[i].selected)
			{
				isSelected = true;
				break;
			}
		}

		if (isSelected)
		{
			return true;
		}
		else
		{
			checkError(fieldObj, fieldName + ' must have a selected option.\n');
			return false;
		}
		break;

	 case "checkbox":

	 	if (fieldObj.checked)
		{

			return true;
		}
		else
		{
			checkError(fieldObj, fieldName + ' must be checked.\n');
			return false;
		}
		break;

	 case "radio":

		var isChecked = false;

		for (var i=0 ; i < fieldObj.length; i++) {
			if (fieldObj[i].checked)
			{
				isChecked = true;
				break;
			}
		}

		if (isChecked)
		{
			return true;
		}
		else
		{
			checkError(fieldObj, fieldName + ' must have a selected option.\n');
			return false;
		}
		break;

	}//end of switch


}//end of function checkRequired


function checkSSN(fieldObj, fieldName)
{
	var reSSN = /^\d{3}-\d{2}-\d{4}$/
	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 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 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?:\/\/[a-zA-Z0-9\-]+[.][a-zA-Z]/
	var arrURL = fieldObj.value.match(reURL);

	if (arrURL || fieldObj.value.length == 0)
	{
		return true;
	}
	else{
 		checkError(fieldObj, fieldName + ' is not a properly formatted URL. (Ex: http://www.yourhotel.com)\n');
		return false;
	}

} // end of checkUrlFunction



