// formValidation.js
/* @author Kim Cardwell
*  @version 1.0.0
*  Description: Miscellaneous form validation functions
*               checkDate - validates a correct date was entered mmddyyyy
*               checkHours - validates the date is greater than parm hours passed than todays date
*				checkEmail - validates a correct email format was entered
*				validateNumber - validates a number was entered
*				validateNumber2 - validates a number was entered and checks min & max
*				checkSSN - validates a correct social security number was entered
*				checkTextAreaLength - Check the length of data entered in a field against the 
*                                     max & min values you specified in the parameter list
*				checkWhiteSpace - validates a value was entered into a field
*
*/

// Validate date function
function checkDate(vdate){
	var checkstr = "0123456789";
	var DateField = vdate;
	var Datevalue = "";
	var DateTemp = "";
	var seperator = "/";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;
   	err = 0;
   	DateValue = DateField.value;
   	/* 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) {
      	DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   	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 00 ist entered, no error, deleting the entry */
   	if ((day == 0) && (month == 0) && (year == 00)) {
   	   err = 0; day = ""; month = ""; year = ""; seperator = "";
   	}
  	 /* if no error, write the completed date to Input-Field (e.g. mm/dd/yyyy) */
  	 if (err == 0) {
  	    DateField.value = month + seperator + day + seperator + year;
  	 }
  	 /* Error-message if err != 0 */
  	 else {
    	alert("Date is incorrect!");
      	DateField.select();
	  	DateField.focus();
	  	return(false);
   		}
} //  End checkDate -->

// Check the hourly difference between dates function
var btnClicked = false;
function checkHours(vdate,hours){

	var checkstr = "0123456789";
	var DateField = vdate;
	var wrkHour = hours;
	var Datevalue = "";
	var DateTemp = "";
	var seperator = "/";
	var day;
	var month;
	var year;
	var i;
	var leap = 0;
	var err = 0;
	var today = new Date();
	var wrkDate = new Date();
	var difference = 0;

// Strip out all non valid characters	
   	DateValue = DateField.value;
	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) {
      	DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   	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 ((day != 0) && (month != 0) && (year != 00)) {
   		// Set wrkDate with date parameter for calculation   	
   		wrkDate.setFullYear(year);
   		wrkDate.setMonth(month - 1);
   		wrkDate.setDate(day);
	
		/* Calculate the difference between the dates */
		difference = wrkDate.getTime() - today.getTime();
		difference = Math.floor(difference / (1000 * 60 * 60));
	
		/* Display error if the difference is less than hour parameter*/
		if(difference < wrkHour){
			err=27;
		}
	}
	
	/* if 00 ist entered, no error, deleting the entry */
   	if ((day == 0) && (month == 0) && (year == 00)) {
   	   err = 0; day = ""; month = ""; year = ""; seperator = "";
   	}
	
	if (err != 0){
	var msg= " ";
	switch(err){
		case 27:
		btnClicked = true;
		msg += " Please allow 24 hours notice for a pre-registration.";
		break;
		case 19:
		btnClicked = true;
		msg += " Invalid Date entered.";
		break;
		case 20:
		btnClicked = true;
		msg += " Invalid Year entered.";
		break;
		case 21:
		btnClicked = true;
		msg += " Invalid Month entered.";
		break;
		case 22:
		btnClicked = true;
		msg += " Invalid Date entered.";
		break;
		case 23:
		btnClicked = true;
		msg += " Invalid Date entered.";
		break;
		case 24:
		btnClicked = true;
		msg += " Invalid Date, this is not a leap year.";
		break;
		case 25:
		btnClicked = true;
		msg += " Invalid Date entered.";
		break;
		case 26:
		btnClicked = true;
		msg += " Invalid Date entered.";
		break;
		default:
		btnClicked = true;
		msg += " Invalid Date";
	}//end switch
	}//end if
	
  	 /* if no error, write the completed date to Input-Field (e.g. mm/dd/yyyy) */
  	 if (err == 0) {
	 	btnClicked = false;
  	    vdate.value = month + seperator + day + seperator + year;
  	 }
  	 /* Error-message if err != 0 */
  	 else { 
	 	if(btnClicked==true){
    	alert(msg);
      	vdate.select();
	  	vdate.focus();
		vdate.value = "";
		btnClicked = false;
	  	return false;
		}
   	}
} //  End checkHours -->


// Validate email address format
function checkEmail(email) {
	var str = email.value
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str)){
		return (true)
	}
	else {
		alert("Invalid E-mail Address! Please re-enter.")
		email.select();
		email.focus();
		return (false)
		}
}
//  End  checkEmail

//Validate a field for a valid number
function validateNumber(vnumber){
	if(isNaN(vnumber.value)){
		alert("Please enter a valid number.");
		vnumber.select();
		vnumber.focus();
		return false;
	}//end if
}//end validateNumber

//Validate a field for a valid number & number of digits.
function validateNumber2(vnumber,min, max){
	var fieldOK = true;
	var textAreaMin = min;
	var textAreaMax = max;
	var fieldValue = vnumber.value
	if(isNaN(fieldValue)){
		alert("Please enter a valid number.");
		fieldOK = false;
	}//end if
 	else if (fieldValue.length < textAreaMin){
 		alert("Must be at least " + textAreaMin + " digits.");
		fieldOK = false;
	}//end if
	 	else if (fieldValue.length > textAreaMax){
	 		alert("Must not be more than " + textAreaMax + " digits.");
			fieldOK = false;
		}//end if
	if (fieldOK == false){
		vnumber.select();
		vnumber.focus();	
	  	return fieldOK;
	}// end if

}//end validateNumber2

//Validate a valid social security number was entered
function checkSSN(ssn) {
	
	var matchArr = ssn.value.match(/^(\d{3})-?\d{2}-?\d{4}$/);
	var numDashes = ssn.value.split('-').length - 1;
	
	if (matchArr == null || numDashes == 1) {
		alert('Invalid SSN. Must be 9 digits or in the form NNN-NN-NNNN.');
		ssn.select();
		ssn.focus();
		return false;
	}
	else if (parseInt(matchArr[1],10)==0) {
		alert("Invalid SSN: SSN's can't start with 000.");
		ssn.select();
		ssn.focus();
		return false;
	}
	else {
		//if there are no dashes but it is valid
		if(ssn.value.length == 9){
			var ssntemp = ssn.value.substring(0,3)+'-'+ssn.value.substring(3,5)+'-'+ssn.value.substring(5,9)
			//alert(ssntemp);
			ssn.value = ssntemp;
		}
	}
}//End  checkSSN function

// Check the length of data entered in a field against the max & min values you specified in the parameter list 
function checkTextAreaLength(textarea, min, max){
	var fieldOK = true;
	var textAreaField = textarea;
	var textAreaValue = "";
	var textAreaMin = min;
	var textAreaMax = max
	textAreaValue = textAreaField.value;
    if (textAreaValue.length < textAreaMin){
   	    msg = textAreaValue.length;
        alert("Text too short. Must be at least " + textAreaMin + " characters.\n Count is: " + msg);
        fieldOK = false;
    } //end if
        else if (textAreaValue.length > textAreaMax){
   	    	msg = textAreaValue.length;
        	alert("Text too long. Must be " + textAreaMax + " characters or less.\n Count is: " + msg);
			fieldOK = false;
    	} //end else if
    	/*
    	if (fieldOK == false){
    		textArea.select();
	  		textArea.focus();	
	  		return fieldOK;
	  	}// end if
	  	*/
} //end checkTextAreaLength function

//Check for whitespace in a field
function checkWhiteSpace(field){
	var myWS = field.value;
	
	if(myWS.match(/^\W/)){
		alert("You must enter a value.");
		field.select();
		field.focus();
		return false;
	}
	else{	
		return true;
	}
}//end checkWhiteSpace function