//function validateForm(theForm) {
//    var why = "";
//    why += checkEmail(theForm.email.value);
//    why += checkPhone(theForm.phone.value);
//    why += isRequired(theForm.notempty.value);
//    why += checkDropdown(theForm.choose.selectedIndex);
//    if (why != "") {
//       alert(why);
//       return false;
//    }
//return true;
//}

function checkEmail(strng, field){
	var error= "";
	var emailFilter=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!(emailFilter.test(strng))) { 
	       error = "<li>Please enter a valid "+field+".</li>";
	}
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (strng.match(illegalChars)) {
		error = "<li>"+field+" contains illegal characters.</li>";

	}
	return error;
}

function checkPhone(strng, field){
	var error = "";
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) {
   		error = "<li>The "+field+" number contains illegal characters.</li>";
	}
	if (!(stripped.length == 10)) {
		error = "<li>Please enter a valid "+field+" number. Make sure you included an area code.</li>";
	}
	return error;
}

function checkRequired(strng, field) {
	var error = "";
  	if (strng.length == 0) {
     		error = "<li>"+field+" is a required field.</li>"
  	}
	return error;	  
}

function checkWaiver(strng, field) {
	var error = "";
  	if (strng != true) {
     		error = "<li>You must check the "+field+" to indicate you accept the terms.</li>"
  	}
	return error;	  
}

function checkCheckbox(strng, field) {
	var error = "";
  	if (strng != true) {
     		error = "<li>You must check "+field+" field.</li>"
  	}
	return error;	  
}


function checkZip(strng, field) {
	var valid = "0123456789";
	var error = "";

	if (strng.length!=5) {
		error = "<li>Please enter a valid 5 digit "+field+".</li>";
		return error;
	}
	for (var i=0; i < strng.length; i++) {
		temp = "" + strng.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			error = "<li>Please enter a valid 5 digit "+field+".</li>";
			return error;
		}
	}
	return error;
}

function checkInt(strng, field) {
	var valid = "0123456789";
	var error = "";

	for (var i=0; i < strng.length; i++) {
		temp = "" + strng.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			error = "<li>Please enter only numbers 0-9 for "+field+" field.</li>";
			return error;
		}
	}
	return error;
}

function checkURL(strng, field) {
	var error = "";
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	
	if (regexp.test(strng) || strng ==""){
		return error;
	}else{
		error = "<li>Please enter a valid "+field+" URL.</li>"
		return error;
	}
}

function checkTime(timeStr, field) {
	// Checks if time is in HH:MM AM/PM format.
	// The seconds and AM/PM are optional.
	var error = "";
	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	var matchArray = timeStr.match(timePat);
	if (matchArray == null) {
		error = "<li>"+field+" is not in a valid format (h:mmAM).</li>";
		return error;
	}
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];;
	
	if (second=="") { second = null; }
	if (ampm=="") { ampm = null }

	if (hour < 0  || hour > 12) {
		error = "<li>"+field+" hour must be between 1 and 12.</li>";
		return error;
	}
	if (ampm == null) {
		error = "<li>You must specify AM or PM for "+field+".</li>";
		return error;
	}
	if (minute<0 || minute > 59) {
		error = "<li>"+field+" minute must be between 0 and 59.</li>";
		return error;
	}
	return error;
}

function checkDate(strng, field){
var checkstr = "0123456789";
var DateValue = strng;
var DateTemp = "";
var seperator = "/";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var error = "";
var i;
   err = 0;
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	  	DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if (DateValue.length == 6) {
   	err = 18; 
   }
   if (DateValue.length != 8) {
   	err = 19;
   }
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
   	err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(0,2);
   if ((month < 1) || (month > 12)) {
   	err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(2,2);
   if (day < 1) {
   	err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
   	leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
   	err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
   	err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
   	err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
   	err = 26;
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
   	return error;
   }
   /* Error-message if err != 0 */
   else {
   	error = "<li>"+field+" is not in a valid format (mm/dd/yyyy)."+err+"</li>";
        return error;
   }
}

