// 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){
	return checkFormWithPreviousErrors(f,false,'',false,'');
}
function checkFormWithPreviousErrors(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" || e.type=="hidden" || e.type=="password"){
			if(e.required){
				if((e.value == null) || isblank(e.value)){
					empty_fields_bool = true;
					empty_fields += "\n\r"+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(e.exclude != null && v == e.exclude){
						errors_bool = true;
						if(e.type=="select-one"){
							errors += "\n\rYou must chose a "+e.realName;
						}
					}
					if(isNaN(v) || (e.min != null && e.value < e.min) || (e.max != null && e.value > e.max)){
						errors_bool = true;
						if(e.type=="select-one"){
							errors += "\n\rYou must chose a "+e.realName;
						}else{
							if(e.image){
								errors+="\n\rYou must select a "+e.realName;
							}else{
								errors += "\n\r"+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\r"+e.realName + " must be a valid date format.  Must be in form: mm/dd/yy";
					}				
				}
			}
			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\r"+e.realName + " must be a valid email format";
					}				
				}
			}
		}
	}
	if(f.dateStart && f.dateEnd){
		if(!isblank(f.dateStart.value) && !isblank(f.dateEnd.value) && checkDate(f.dateEnd.value) && checkDate(f.dateStart.value) && checkDate(f.dateEnd.value) < checkDate(f.dateStart.value)){
			errors_bool = true;
			errors += "\n\rYour Start Date cannot be greater than your End Date";
		}else if(!isblank(f.dateEnd.value) && checkDate(f.dateEnd.value) && isblank(f.dateStart.value)){
			errors_bool = true;
			errors += "\n\rYour End Date cannot be set unless Start Date is also";
		}
	}
	if(empty_fields_bool || errors_bool){
		var empty_fields_msg = "";
		var errors_msg = "";
		if(empty_fields_bool){
			empty_fields_msg = "\n\rThe following fields cannot be blank: \n\r";
		}
		if(errors_bool){
			var droplines;
			if(empty_fields_bool){
				droplines = "\n\n\r\r";
			}else{
				droplines = "\n\r";
			}
			errors_msg = droplines+"The following fields contain an error: \n\r";
		}
		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);
}

				
		
