// version 1.1.0

// bugs:
//	no known bugs
//	

// enhancements:
//	support for select boxes, radio boxes, multiselects, etc.
//	add datatypes: float, currency, credit card, month/day, and month/year.

function areValidTextBoxes(strFormName,fieldRestrictions) {


// valid datatypes: "integer", "date", "text","time"





	var f;
	f = document.forms[strFormName];




	for(var i=0;i<fieldRestrictions.length;i++){
		var elementNum, element;
		//elementNum = getElementNumber(f,fieldRestrictions[i][0]);
		element = f[fieldRestrictions[i][0]];
			// check required fields
		if (element == null) {
			alert("field, " + fieldRestrictions[i][0] + ", not found");
			return false;
		}
		else {
		
			
			// validate required fields
			if (fieldRestrictions[i][2]) {
				if ((fieldRestrictions[i][3]!='select' && isBlanks(element.value)) ||
					(fieldRestrictions[i][3]=='select' && (element.selectedIndex == -1)))
				{
					alert("The field, " + fieldRestrictions[i][1] + ", is required.");
					
					element.focus();
					return false;
				}
				
			}
			
			// validate data types and min,max
			if (!isValidData(element,fieldRestrictions[i][3],fieldRestrictions[i][1],fieldRestrictions[i][4],fieldRestrictions[i][5])) {
				return false;
			}
			
				
		}
		
	}
	
	// at this point, all the fields are valid
	// replace empty strings with their null strings
	for(var i=0;i<fieldRestrictions.length;i++){
		//element = getElement(f,fieldRestrictions[i][0])
		element = f[fieldRestrictions[i][0]];
		if (element != null) {
			if (isBlanks(element.value)) {
				element.value = fieldRestrictions[i][6];
			}
		}
	}
	
	return true;

}


function isBlanks(varValue){
	var aString="" + varValue;
	var kBlank=" ";
	var bBlanks=true;
	if(aString!="")	{
		for(var i=0;i<aString.length;i++)
			if (aString.charAt(i)!=kBlank){
				bBlanks=false;
				break;
				}
	}
	return bBlanks;
}

//function getElementNumber(form,strField) {
//	var i;
//	for(i=0;i<form.elements.length;i++){
//		if (form.elements[i].name=strField) {
//			alert("getele:" + i + "," + form.elements[i].name);
//			return i;
//		}
//	}
//	//alert("not found");
//	return null;
//}


function isValidData(field,strDataType,strFieldName,min,max) {
	
	var strDataTypeChar
	strDataTypeChar = 't'; //default is text
	if (strDataType == "integer") strDataTypeChar = 'i';
	if (strDataType == "date") strDataTypeChar = 'd';
	if (strDataType == "time") strDataTypeChar = 'm';
	if (strDataType == "text") strDataTypeChar = 't';
	
	switch (strDataTypeChar) {
		
		case 'i': 
		// ignore empty fields, they've been validated already
		if (isBlanks(field.value)) return true;
		
		if (!isInteger(field.value)) {
			alert("The field, " + strFieldName + ", must be an integer.");
			field.focus();
			return false;
		}
		
		
		// min
		if ((min != null) && (parseInt(lZeroTrim(field.value))<parseInt(min))) {
			field.focus();
			alert("The field, " + strFieldName + ", must be equal or greater than " + min);
			return false;
		}
		
		// max
		if ((max != null) && (parseInt(lZeroTrim(field.value))>parseInt(max))) {
			field.focus();
			alert("The field, " + strFieldName + ", must be less than or equal to " + max);
			return false;
		}
		
		return true;
		
		break;
	case 'd':
		// ignore empty fields, they've been validated already
		if (isBlanks(field.value)) return true;
		
		if (!isDate(field.value)) {
			alert("The field, " + strFieldName + ", must be a date.");
			field.focus();
			return false;
		}
		
		
		// min
		if ((min != null) && (new Date(field.value) <  new Date(min))) {
			field.focus();
			alert("The field, " + strFieldName + ", must not be before " + min);
			return false;
		}
		
	
		// max
		if ((max != null) && (new Date(field.value) > new Date(max))) {
			field.focus();
			alert("The field, " + strFieldName + ", must not be later than " + max);
			return false;
		}
		return true;
		
		break;
		
	case 'm':
		// ignore empty fields, they've been validated already
		if (isBlanks(field.value)) return true;
		
		if (!isValidTime(field.value)) {
			alert("The field, " + strFieldName + ", must be a valid time.");
			field.focus();
			return false;
		}
		
		
		// min
		if ((min != null) && (new Date(field.value) <  new Date(min))) {
			field.focus();
			alert("The field, " + strFieldName + ", must not be before " + min);
			return false;
		}
		
	
		// max
		if ((max != null) && (new Date(field.value) > new Date(max))) {
			field.focus();
			alert("The field, " + strFieldName + ", must not be later than " + max);
			return false;
		}
		return true;
		
		break;
	case 't':
		// ignore empty fields, they've been validated already
		if (isBlanks(field.value)) return true;
		
		
		// min
		if ((min != null) && (("" + field.value).length <  min)) {
			field.focus();
			alert("The field, " + strFieldName + ", must not be less than " + min + " characters.");
			return false;
		}
		
	
		// max
		if ((max != null) && (("" + field.value).length > max)) {
			field.focus();
			alert("The field, " + strFieldName + ", must not be longer than " + max + " characters.");
			return false;
		}
	
		return true;
	
	}

}

function isInteger(varValue) {

	var str = trim(varValue);
	str = lZeroTrim(str) ;

	if (str.substring(0,1) == "-") 
		str = str.substring(1,str.length);

	var valid = "0123456789";
	//var data=field.value
	for (var i=0; i < str.length; i++) {
		var temp = "" + str.substring(i, i+1);
		if (valid.indexOf(temp) == "-1"){
			return false;
		}
	}
	return true;
}


function isDate(varValue){

	

	var valid = "0123456789/";
	//var data=field.value
	var data= trim(varValue);
	for (var i=0; i < data.length; i++) {
		var temp = "" + data.substring(i, i+1);
		if (valid.indexOf(temp) == "-1"){
			return false;
		}
	}
	var aArray=data.split("/")
	var itemCount=aArray.length;
	if (itemCount!= 3) {
		return false;
	}
	var month = aArray[0];
	var day=aArray[1];
	var year=aArray[2];

	if (month<1||month>12||day<1||day>31||year<1753||year>9999){
		return false;
	} 
	var newDate=new Date(year, month-1, day)
	var newYear=newDate.getFullYear();
	var newMonth=newDate.getMonth()+1;
	var newDay=newDate.getDate();
	if (newYear!=year||newMonth!=month||newDay!=day) {
		return false;
	}
	return true;
}

function isValidTime(theTime){
	
        var utime,isThereAM,isTherePM;
        var isThereColan,theHour,theMinute;
        
        utime = theTime.toUpperCase();
        
        isThereAM =utime.indexOf("AM");
        isTherePM =utime.indexOf("PM");
		isThereColan = utime.indexOf(":");
		theHour =trim(theTime.substring(0,isThereColan));
		
			if (parseInt(isThereAM) == -1){
			 theMinute = trim(theTime.substring(isThereColan + 1,isTherePM));
				}
				else {
					theMinute = trim(theTime.substring(isThereColan + 1,isThereAM));
						}
		
		//alert(theMinute);
		
			
        if ((parseInt(isThereAM) < 1) && ( parseInt(isTherePM) < 1)) {
			//alert("Please AM or PM in the Pick Up/Delivery time field.");
			//document.OrderBoxLunch.OtxtPUTime.focus();
					
			 return false;
			} 
			else if (parseInt(isThereColan) < 1) { 
				//alert("Please enter 11:22am format");
				//document.OrderBoxLunch.OtxtPUTime.focus();
						
			 return false;
			}	
			 else if ((parseInt(theHour) >12) || (isNaN(theHour)== true)) {
			 //alert("hour must be 12 or less");
			 //document.OrderBoxLunch.OtxtPUTime.focus();
			
			 return false;
			 }
			 else if ((parseInt(theMinute) >59) || (parseInt(theMinute) < 0)|| (parseInt(theMinute.length) != 2) || (isNaN(theMinute)== true)) {
			 //alert("minute must be btween 0 and 59 or less");
			 //document.OrderBoxLunch.OtxtPUTime.focus();
			 return false;
			 }
			 
      return true;
	
}  



function ltrim(varArg) {

	var str = "" + varArg;
	while ((str.length > 0) && (str.substring(0,1) == " ")) {
		str = str.substring(1,str.length);
	}
	return str;

}

function rtrim(varArg) {
	var str = "" + varArg;
	while ((str.length > 0) && (str.substring(str.length-1,str.length) == " ")) {
		str = str.substring(0,str.length-1);
	}
	return str;
}

function trim(varArg) {
	return rtrim(ltrim(varArg));
}


function lZeroTrim(varArg) {
	var str = "" + varArg;
	while ((str.length > 0) && (str.substring(0,1) == "0")) {
		str = str.substring(1,str.length);
	}
	return str;
}

