//Included functions:
//validZipCodeUS
//blnValidZipCodeUS
//validInteger
//blnValidInteger
//validEmail
//blnValidEmail
//checkLength
//validUSPhone
//blnValidUSPhoneExt
//blnValidUSPhone
//validFloat
//trimString
//closeWindow
//isValidDateUS
//validMonth(month)
//validDay(month,day)
//blnValidMonth
//blnValidDay
//blnValidYear
//blnValidDate
//PrintDocument

function validZipCodeUS(passfld){

	var rezip = /^\d{5}$|^\d{5}[\-\s]?\d{4}$/;

	if (passfld.value.length > 0){
		if (!(rezip.test(passfld.value))){				
			alert("Enter a valid US Zip Code (ex: 99999 or 99999-9999)");
			passfld.value = "";
			passfld.focus();
		}
	}
}

function blnValidZipCodeUS(passval){

	var rezip = /^\d{5}$|^\d{5}[\-\s]?\d{4}$/;

	if (!(rezip.test(passval))){				
		return false;	
	}
	return true;
}


//validInteger - This function checks that the input contains a valid Integer

function validInteger(passfld){

	var reInt = /^\d+$/;

	if (passfld.value.length > 0) {
		if (!(reInt.test(passfld.value))){
			alert("You must enter a valid Integer (ex: 10 or 0)");
			passfld.value = "";
			passfld.focus();
		}
	}			
}

//Returns a boolean determing whether the passed value is an integer

function blnValidInteger(passval){

	var reInt = /^\d+$/;

	if (!(reInt.test(passval))){
		return false;
	}

	return true;
}

//validEmail - Checks that a vaild Email is entered

function validEmail(passfld){

	var reEmail1 = /^.+\@.+\..+$/;
	var reEmail2 = /[\s\,\']/;

	if(passfld.value.length > 0){
		if ((!reEmail1.test(passfld.value)) || (reEmail2.test(passfld.value))){
			alert("Enter a valid Email address (ex: user@domain.com)");
			passfld.value = "";
			passfld.focus();
		}
	}
}

function blnValidEmail(passval) {
	
	var reEmail1 = /^.+\@.+\..+$/;
	var reEmail2 = /[\s\,\']/;

	if ((!reEmail1.test(passval)) || (reEmail2.test(passval))){
		return false;
	}
	
	return true;
}

//checkLength - This function is used for checking that the length of an input control is valid.
//compoper (comparison operator) can be one of six possibilities:
//e - equal to
//ne - not equal to
//gte - greater than or equal
//gt - greater than
//lte - less than or equal	
//lt - less than
//strlen specifies the value that input is being compared against.

function checkLength(passfld, compoper, complen){

var errexists = false;
var val = passfld.value.length;
var stroper;


if (val > 0){
//	alert("val: " + val + ", compoper: " + compoper + ", complen: " + complen);
	switch(compoper){
		case "e":
			if(val != complen){ 
				errexists = true;
				stroper = "be equal to";
			}
			break;
		case "ne":
				if(val == complen){
					errexists = true;
					stroper = "not be equal to";
				}
			break;
		case "gt":
				if(val <= complen){
					errexists = true;
					stroper = "be greater than";
				}
			break;	
		case "gte":
				if(val < complen){
					errexists = true;
					stroper = "be greater than or equal to";
				}
			break;
		case "lt":
				if(val >= complen){
					errexists = true;
					stroper = "be less than";
				}
			break;		
		case "lte":
				if(val > complen){
					errexists = true;
					stroper = "be less than or equal to";
				}
			break;
		default:
			alert("An error exists, you did not correctly specify compoper");
		
	}

	if (errexists == true){
		alert("Your input must " + stroper + " " + complen + " units long.");
		passfld.value = "";
		passfld.focus();
	}
}
}

//validUSPhone - checks for a valid US phone number

function validUSPhone(passfld){

var rePhone1 = /^\(\d{3}\)\s?\d{3}\-\d{4}$/
var rePhone2 = /^\d{3}\-\d{3}\-\d{4}$/

	if (passfld.value.length > 0){
		if ((!(rePhone1.test(passfld.value))) && (!(rePhone2.test(passfld.value)))){
			alert("Enter a valid US Phone number (ex: 999-999-9999 or (999) 999-9999)");
			passfld.value = "";
			passfld.focus();
		}
	}
}

function blnValidUSPhoneExt(passval){

	var rePhone = /^\(\d{3}\)\s\d{3}\-\d{4}/;

	if (!(rePhone.test(passval))){
		return false;
	}
	return true;

}

function blnValidUSPhone(passval){

	var rePhone = /^\(\d{3}\)\s\d{3}\-\d{4}$/;

	if (!(rePhone.test(passval))){
		return false;
	}
	return true;

}
function validFloat(passfld){

var reFloat = /^\d*\.?\d+$/

	if (passfld.value.length >0){
		if(!(reFloat.test(passfld.value))){
			alert("Enter a valid decimal number (ex: 9.5, 37)");
			passfld.value = "";
			passfld.focus();
		}
	}
}

function trimString (str) {
  
	while (str.charAt(0) == ' '){ 
		str = str.substring(1);
	}
 	while (str.charAt(str.length - 1) == ' '){
		str = str.substring(0, str.length - 1);
	}
	return str;

}

function closeWindow(){


	if (top != self) {

		top.close();
	
	}
	else{

		self.close();

	}
}

function isValidDateUS(dateStr) {

// Date validation function courtesty of 
// Sandeep V. Tamhankar (stamhankar@hotmail.com) -->

// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2})$/; // requires 4 digit year

	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) {
		alert(dateStr + " Date is not in a valid format.")
		return false;
	}

	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];
	if (month < 1 || month > 12) { // check month range
		alert("Month must be between 1 and 12.");
		return false;
	}

	if (day < 1 || day > 31) {
		alert("Day must be between 1 and 31.");
		return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn't have 31 days!")
		return false;
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			alert("February " + year + " doesn't have " + day + " days!");
			return false;
   		}
   	}

	return true;

}

function validMonth(passfld){

	month_val = passfld.value;

	if (month_val.length > 0){

	   if (blnValidInteger(month_val)){
	
		if (!((month_val >= 1) && (month_val <= 12))){
			alert("The month must be between 1 and 12.");
			passfld.value = "";
			passfld.focus();
		}
	   }
	   else{
	   	alert("Enter a valid Integer for the month between 1 and 12.");
		passfld.value = "";
		passfld.focus();
	   }
	
	}
}

function validDay(passmonthfld, passdayfld){

	errexists = 0;
	day_val = passdayfld.value;
	month_val = passmonthfld.value;

	switch(month_val){

		case "": 
			  if (day_val.length > 0){ 
			  	errexists = 1;
			  }
			  break;
	
		case "1": 
			  if (!((day_val >= 1) && (day_val <= 31))){ 
			  	errexists = 1;
			  }
			  break;
		
		case "2": 
			  if (!((day_val >= 1) && (day_val <= 29))){ 
			  	errexists = 1;
			  }
			  break;
		
		case "3": 
			  if (!((day_val >= 1) && (day_val <= 31))){ 
			  	errexists = 1;
			  }
			  break;
		
		case "4": 
			  if (!((day_val >= 1) && (day_val <= 30))){ 
			  	errexists = 1;
			  }
			  break;
		
		case "5": 
			  if (!((day_val >= 1) && (day_val <= 31))){ 
			  	errexists = 1;
			  }
			  break;
		
		case "6": 
			  if (!((day_val >= 1) && (day_val <= 30))){ 
			  	errexists = 1;
			  }
			  break;
		
		case "7": 
			  if (!((day_val >= 1) && (day_val <= 31))){ 
			  	errexists = 1;
			  }
			  break;
		
		case "8": 
			  if (!((day_val >= 1) && (day_val <= 31))){ 
			  	errexists = 1;
			  }
			  break;
		
		case "9": 
			  if (!((day_val >= 1) && (day_val <= 30))){ 
			  	errexists = 1;
			  }
			  break;
		
		case "10": 
			  if (!((day_val >= 1) && (day_val <= 31))){ 
			  	errexists = 1;
			  }
			  break;
		
		case "11": 
			  if (!((day_val >= 1) && (day_val <= 30))){ 
			  	errexists = 1;
			  }
			  break;
		
		case "12": 
			  if (!((day_val >= 1) && (day_val <= 31))){ 
			  	errexists = 1;
			  }
			  break;
		default: 
			alert("You must enter a valid month between 1 and 12.");
	
	}

	if (errexists == 1) {
		alert("The day you entered is not valid for the specified month.");
		passdayfld.value = "";
		passdayfld.focus();
	}
}

function blnValidMonth(passval){

	if (!blnValidInteger(passval)){
	   	return false;
	}

	if (!((passval >= 1) && (passval <= 12))){
		return false;	
	}

	return true;
}

function blnValidDay(passmonth, day_val){

	switch(passmonth){

		case "1": 
			  if (!((day_val >= 1) && (day_val <= 31))){ 
			  	return false;
			  }
			  break;
		
		case "2": 
			  if (!((day_val >= 1) && (day_val <= 29))){ 	
			  	return false;
			  }
			  break;
		
		case "3": 
			  if (!((day_val >= 1) && (day_val <= 31))){ 
			 	return false; 
			  }
			  break;
		
		case "4": 
			  if (!((day_val >= 1) && (day_val <= 30))){ 
			 	return false; 
			  }
			  break;
		
		case "5": 
			  if (!((day_val >= 1) && (day_val <= 31))){ 
			 	return false; 
			  }
			  break;
		
		case "6": 
			  if (!((day_val >= 1) && (day_val <= 30))){ 
			 	return false; 
			  }
			  break;
		
		case "7": 
			  if (!((day_val >= 1) && (day_val <= 31))){ 
				return false; 
			  }
			  break;
		
		case "8": 
			  if (!((day_val >= 1) && (day_val <= 31))){ 
			 	return false; 
			  }
			  break;
		
		case "9": 
			  if (!((day_val >= 1) && (day_val <= 30))){ 
			 	return false; 
			  }
			  break;
		
		case "10": 
			  if (!((day_val >= 1) && (day_val <= 31))){ 
				return false;
			  }
			  break;
		
		case "11": 
			  if (!((day_val >= 1) && (day_val <= 30))){ 
			 	return false; 
			  }
			  break;
		
		case "12": 
			  if (!((day_val >= 1) && (day_val <= 31))){ 
				return false;	
			  }
			  break;
		default: 
			return false;	
	
	}

	return true;

}

function blnValidYear(passval) {

	if (!blnValidInteger(passval)) { 
		return false; 
	}
	if (passval.length != 4) { 
		return false; 
	}
	
	return true;

}

function blnValidDate(passmonth,passday,passyear,reqday,reqyear){

	//The month is always required
	//Day and Year are optional
	
	re1 = /^0?/;
	passmonth = passmonth.replace(re1,'');
	passday = passday.replace(re1,'');
	

	if (!blnValidMonth(passmonth)) { 
		return false; 
	}
	
	if ((reqday == "yes") || (passday.length > 0)){
		if (!blnValidDay(passmonth,passday)) { 
			return false; 
		}
	}

	if ((reqyear == "yes") || (passyear.length > 0)){
		if (!blnValidYear(passyear)){ return false; }
	}

	return true;
}

function PrintDocument(){
		
	if (window.print){
		window.print()
	}
	else{
		alert('To print this page, go to the "File Menu" and select "Print...".')
	}
}