// returns true if a string contains only whitespace characters
function isblank(s){
	for(var i = 0; i < s.length; i++){
		var c = s.charAt(i);
		if((c != ' ') && (c != '\n') && (c != '')){
			return false;
		}
	}
	return true;
}


function checkForm(f,havePreviousEmptyFieldsError, previousEmptyFieldsErrorStr,havePreviousError, previousErrorStr){
	if((window.navigator.platform).indexOf("Mac") >= 0){
		if(window.navigator.appName.indexOf("Microsoft Internet Explorer") >= 0){
			var myvarparamarray = f.variableparameters.value.split("\r");
		}else{
			var myvarparamarray = f.variableparameters.value.split("\n");
		}
	}else{
		var myvarparamarray = f.variableparameters.value.split("\n");
	}
	for(var i = 0; i < myvarparamarray.length; i++){
		var e = myvarparamarray[i];
		//set all the variables needed from the variableparameters parameter
		eval("f."+e);
	}
	
	var empty_fields_msg = "";
	var errors_msg = "";
	var empty_fields_bool = havePreviousEmptyFieldsError;
	var empty_fields = previousEmptyFieldsErrorStr;
	var errors_bool = havePreviousError;
	var errors = previousErrorStr;
	for(var i = 0; i < f.length; i++){
		var e = f.elements[i];
		if((e.type == "text") || (e.type == "textarea") || e.type=="radio" || e.type=="select-one"){
			if(e.required){
				if((e.value == null) || isblank(e.value)){
					empty_fields_bool = true;
					empty_fields += "\n"+e.realName;
					//cancels this loop
					continue;
				}
			}
			if(e.numeric){
				if(!e.required && isblank(e.value)){
					//don't need to check anymore it's blank
				}else{ 
					var v = parseFloat(e.value);
					if(isNaN(v) || (e.min != null && e.value < e.min) || (e.max != null && e.value > e.max)){
						errors_bool = true;
						errors += "\n"+e.realName + " must be a number";
						if(e.min != null){
							errors += " that is greater than "+e.min;
						}
						if(e.max != null){
							if(e.min != null){
								errors += " and less than "+e.max;
							}else{
								errors += " that is less than "+e.max;
							}
						}
					}
				}
				continue;
			}
			if(e.date){
				if(!e.required && isblank(e.value)){
					//don't need to check anymore it's blank
				}else{
					//if(!Date.parse(e.value)){
					if(!checkDate(e.value)){
						errors_bool = true;
						errors += "\n"+e.realName + " must be a valid date format";
					}				
				}
			}
			if(e.email){
				if(!e.required && isblank(e.value)){
					//don't need to check anymore it's blank
				}else{
					if(!checkEmail(e.value)){
						errors_bool = true;
						errors += "\n"+e.realName + " must be a valid email format";
					}				
				}
			}
		}
	}
	
	/*
	REMOVED FOR BETTY BRINN NO DATES ENTERED ON FORM
	if(!isblank(f.dtmEndDate.value) && !isblank(f.dtmStartDate.value) && checkDate(f.dtmEndDate.value) && checkDate(f.dtmStartDate.value) && checkDate(f.dtmEndDate.value) < checkDate(f.dtmStartDate.value)){
		errors_bool = true;
		errors += "\nYour Start Date cannot be greater than your End Date";
	}*/
	if(empty_fields_bool || errors_bool){
		var empty_fields_msg = "";
		var errors_msg = "";
		if(empty_fields_bool){
			empty_fields_msg = "\nThe following fields cannot be blank: \n";
		}
		if(errors_bool){
			var droplines;
			if(empty_fields_bool){
				droplines = "\n\n";
			}else{
				droplines = "\n";
			}
			errors_msg = droplines+"The following fields contain an error: \n";
		}
		alert(empty_fields_msg+empty_fields+errors_msg+errors);
		return false;
	}else{
		f.submit();
		//return true;
	}
}

//returns a date object with date represented by the date string or false if can't make a date object with it
//had to do it this way because Date.parse needs a 4 digit year, which we were not requiring
function checkDate(dateString){
	//10 is added to parseInt to make number base 10
	var results = dateString.match(/(\d{1,2})\/(\d{1,2})\/(\d{1,4})/);
	if(results != null){
		if(parseInt(results[1],10) < 13 && parseInt(results[1],10) > 0 && parseInt(results[2],10) < 32 && parseInt(results[2],10) > 0 && parseInt(results[3],10) < 3000 && parseInt(results[3],10) > 0){
			var centuryToAdd;
			if(parseInt(results[3],10) < 1000){
				if(parseInt(results[3],10) > 29){
					centuryToAdd = 1900;
				}else{
					centuryToAdd = 2000;
				}
			}else{
				centuryToAdd = 0;
			}
			var parsedDate = new Date(Date.parse(parseInt(results[1],10)+"/"+parseInt(results[2],10)+"/"+(parseInt(results[3],10)+centuryToAdd)));
			//check to see if a date like 2/31/03 was entered, javascript changes it to 4/3/03 instead of giving an error
			if(parsedDate.getMonth()+1 != parseInt(results[1],10)){
				return false;
			}else{
				return Date.parse(parseInt(results[1],10)+"/"+parseInt(results[2],10)+"/"+(parseInt(results[3],10)+centuryToAdd));
			}
		}else{
			return false; 
		}
	}else{
		return false;
	}
}
function checkEmail(emailString){
	
	var pattern = /^[\w\d\-\.]+@[\w\d\-\.]+[\.][\w]{1,3}$/;
	return pattern.test(emailString);
}
				
		
