// ||||||||||||||||||||||||||||||||||||||||||||||||||
// Form Validation | version date: 11/21/02
// returns true if the string is a valid email

function isEmail(str){
	if(isEmpty(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}

// returns true if the string only contains characters A-Z or a-z
function isAlpha(str){
	var re = /[^a-zA-Z]/g
	if (re.test(str)) return false;
	return true;
}

// returns true if the string only contains characters 0-9
function isNumeric(str){
	var re = /[\D]/g
	if (re.test(str)) return false;
	return true;
}

// returns true if the string only contains characters A-Z, a-z or 0-9
function isAlphaNumeric(str){
	var re = /[^a-zA-Z0-9]/g
	if (re.test(str)) return false;
	return true;
}

// returns true if the string is empty
function isEmpty(str){
	return (str == null) || (str.length == 0);
}

// returns true if the string's length equals "len"
function isLength(str, len){
	return str.length == len;
}

// returns true if the string's length is between "min" and "max"
function isLengthBetween(str, min, max){
	return (str.length >= min)&&(str.length <= max);
}

// returns true if the string is a US phone number formatted as...
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhoneNumber(str){
	var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
	return re.test(str);
}

// returns true if the string is a valid date formatted as...
// mm dd yyyy, mm/dd/yyyy, mm.dd.yyyy, mm-dd-yyyy
function isDate(str){
	var re = /^(\d{1,2})[\s\.\/-](\d{1,2})[\s\.\/-](\d{4})$/
	if (!re.test(str)) return false;
	var result = str.match(re);
	var m = parseInt(result[1]);
	var d = parseInt(result[2]);
	var y = parseInt(result[3]);
	
	if(m < 1 || m > 12 || y < 1900 || y > 2100) return false;
	if(m == 2){
		var days = ((y % 4) == 0) ? 29 : 28;
	}else if(m == 4 || m == 6 || m == 9 || m == 11){
		var days = 30;
	}else{
		var days = 31;
	}

	return (d >= 1 && d <= days);
}

// returns true if "str1" is the same as the "str2"
function isMatch(str1, str2){
	return str1 == str2;
}

// returns true if the string contains only whitespace
// cannot check a password type input for whitespace
function isWhitespace(str){ // NOT USED IN FORM VALIDATION
	var re = /[\S]/g
	if (re.test(str)) return false;
	return true;
}

// removes any whitespace from the string and returns the result
// the value of "replacement" will be used to replace the whitespace (optional)
function stripWhitespace(str, replacement){// NOT USED IN FORM VALIDATION
	if (replacement == null) replacement = '';
	var result = str;
	var re = /\s/g

	if(str.search(re) != -1){
		result = str.replace(re, replacement);
	}

	return result;
}

// validate the form
function validateForm(f, preCheck){
	var errors = '';
	if(preCheck != null) errors += preCheck;
	
	var i,e,t,n,v;
	for(i=0; i < f.elements.length; i++){

		e = f.elements[i];

		if(e.optional) continue;
		t = e.type;
		header_msg ='NOKWARE MESSAGE\nPlease correct the following error(s):\n\n'
		n = e.name;
		v = e.value;

// A BEGINS /////////////////////
if(t == 'text' || t == 'password' || t == 'textarea'){

// NOT EMPTY
if(isEmpty(v)){
	if (n == 'firstname') {errors +='-  First Name is empty.\n'; continue;}
	if (n == 'lastname') {errors +='-  Last Name is empty.\n'; continue;}
	if (n == 'address') {errors +='-  Address is empty.\n'; continue;}
	if (n == 'city') {errors +='-  City is empty.\n'; continue;}
	if (n == 'postal') {errors +='-  Postal Code is empty.\n'; continue;}
	if (n == 'phone_day') {errors +='-  Phone Number is empty.\n'; continue;}
	if (n == 'email_address' || n == 'email') {errors +='-  Email Address is empty.\n'; continue;}
	if (n == 'email_address_0' || n == 'email2') {errors +='-  Confirmation Email Address is empty.\n'; continue;}
//	if (n.indexOf("card_num")=='0') {errors +='-  All credit card fields are required.\n'; continue;}
	if (n=='card_num_1') {errors +='-  The first credit card field is empty.\n'; continue;}
	if (n=='card_num_2') {errors +='-  The second credit card field is empty.\n'; continue;}
	if (n=='card_num_3') {errors +='-  The third credit card field is empty.\n'; continue;}
	if (n=='card_num_4') {errors +='-  The fourth credit card field is empty.\n'; continue;}
	//if (n.indexOf("email_address")=='0') {errors +='-  Email Addreses must match.\n'; continue;}
	if (n=='subject') {errors +='-  Subject is empty.\n'; continue;}
	if (n=='message') {errors +='-  Message is empty.\n'; continue;}
	else { errors += n+' cannot be empty.\n'; continue; }
}

// DEFAULT VALUE
//			if(v == e.defaultValue){
//				errors += n+' cannot use the default value.\n'; continue;
//			}


// ALPHABETICAL
if(e.isAlpha){
	if(!isAlpha(v)){
		if (n == 'firstname') {errors +='-  First Name can only contain alphabetical characters.\n   Please make sure threre are no spaces in your First Name.\n'; continue; }
		if (n == 'lastname') {errors +='-  Last Name can only contain alphabetical characters.\n   Please make sure threre are no spaces in your Last Name.\n'; continue; }
		if (n == 'city') {errors +='-  City Name can only contain alphabetical characters.\n'; continue; }
		else { errors += n+' can only contain characters A-Z a-z.\n'; continue; }
	}
}

// NUMERIC
if(e.isNumeric){
	if(!isNumeric(v)){
		if (n=='postal') { errors +='-  Postal Code can only contain numbers.\n'; continue; }
		if (n=='card_num_1') {errors +='-  The first field can only contain numbers.\n'; continue;}
		if (n=='card_num_2') {errors +='-  The second field can only contain numbers.\n'; continue;}
		if (n=='card_num_3') {errors +='-  The third field can only contain numbers.\n'; continue;}
		if (n=='card_num_4') {errors +='-  The fourth field can only contain numbers.\n'; continue;}
		else { errors += n+' can only contain characters 0-9.\n'; continue; }
	}
}

// ALPHA NUMERIC
if(e.isAlphaNumeric){
	if(!isAlphaNumeric(v)){ errors += n+' can only contain characters A-Z a-z 0-9.\n'; continue;}
}

// EMAIL ADDRESS
if(e.isEmail){
	if(!isEmail(v)){
		if (n=='email_address' || n=='email') { errors +='-  Email Address is not valid.\n'; continue; }
		if (n=='email_address_0' || n=='email2') { errors +='-  Confirmation Email Address is not valid.\n'; continue; }
		else { errors += v+' is not a valid email.\n'; continue; }
	}
}

// SPECIFIC LENGTH
if(e.isLength != null){
	var len = e.isLength;
		if(!isLength(v,len)){
			if (n=='card_num_1') {errors +='-  The first field is too shory.\n'; continue;}
			if (n=='card_num_2') {errors +='-  The second field is too shory.\n'; continue;}
			if (n=='card_num_3') {errors +='-  The third field is too shory.\n'; continue;}
			if (n=='card_num_4') {errors +='-  The fourth field is too shory.\n'; continue;}
			else { errors += n+' must contain only '+len+' characters.\n'; continue; }
		}
}

// LENGTH BETWEEN
if(e.isLengthBetween != null){
	var min = e.isLengthBetween[0];
	var max = e.isLengthBetween[1];
		if(!isLengthBetween(v,min,max)){
			if (n == 'firstname') {errors +='-  First Name is too short.\n'; continue; }
			if (n == 'lastname') {errors +='-  Last Name is too short.\n'; continue; }
			if (n == 'address') {errors +='-  Address is too short.\n'; continue; }
			if (n == 'city') {errors +='-  City Name is too short.\n'; continue; }
			if (n == 'postal') {errors +='-  Postal Code is too short.\n'; continue; }
			else { errors += n+' cannot contain less than '+min+' or more than '+max+' characters.\n'; continue; }
		}
}

// PHONE NUMBER
if(e.isPhoneNumber){
	if(!isPhoneNumber(v)){
		if (n=='phone_day') { errors += ' Day-time Phone Number is not valid.\n'; continue; }
		else { errors += v+' is not a valid US phone number.\n'; continue; }
	}
}

	// DATE
	if(e.isDate){
		if(!isDate(v)){ errors += v+' is not a valid date.\n'; continue;}
	}

	// MUST MATCH
	if(e.isMatch != null){
		if (n.indexOf("email_address")=='0') {errors +='-  Please verify your email address.\n'; continue; }
		if(!isMatch(v, e.isMatch)){ errors += n+' does not match.\n'; continue;}
	}
}
// A ENDS /////////////////////


// SELECT BEGINS /////////////
if(t.indexOf('select') != -1){
	// EMPTY
	if(isEmpty(e.options[e.selectedIndex].value)){
		if (n=='state') { errors +='-  You did not select a State.\n'; continue; }
		if (n=='card_exp_month') { errors +='-  You did not select an expiration month.\n'; continue; }
		if (n=='card_exp_year') { errors +='-  You did not select an expiration year.\n'; continue; }
		else { errors += n+' needs an option selected.\n'; continue; }
	}
}
// SELECT ENDS ///////////////

// FILE BEGINS //////////////
if(t == 'file'){
	if(isEmpty(v)){ errors += n+' needs a file to upload.\n'; continue;}
}
// FILE END ////////////////
}


if(errors != '')
	alert(header_msg+errors);
	return errors == '';
}



/*
The following elements are not validated...
button   type="button"
checkbox type="checkbox"
hidden   type="hidden"
radio    type="radio"
reset    type="reset"
submit   type="submit"

All elements are assumed required and will only be validated for an
empty value or defaultValue unless specified by the following properties.

isEmail = true;          // valid email address
isAlpha = true;          // A-Z a-z characters only
isNumeric = true;        // 0-9 characters only
isAlphaNumeric = true;   // A-Z a-z 0-9 characters only
isLength = number;       // must be exact length
isLengthBetween = array; // [lowNumber, highNumber] must be between lowNumber and highNumber
isPhoneNumber = true;    // valid US phone number. See "isPhoneNumber()" comments for the formatting rules
isDate = true;           // valid date. See "isDate()" comments for the formatting rules
isMatch = string;        // must match string
optional = true;         // element will not be validated
*/

// --------------------------------------------------
// All of the previous JavaScript is coded to process
// any form and should be kept in an external file if
// multiple forms are being processed.
// This function configures the previous
// form validation code for this form.
// --------------------------------------------------

//function configureValidation(f){
//	f.firstname.isAlpha = true;
//	f.firstname.isLengthBetween = [2,25];
//	f.middle_initial.optional = true;
//	f.lastname.isAlpha = true;
//	f.lastname.isLengthBetween = [2,25]
//	f.address.isAlphaNumeric = true;
//	f.address.isLengthBetween = [8,75]
//	f.address2.optional = true;
//	f.city.isAlpha = true;
//	f.city.isLengthBetween = [8,50]
//	f.state.isEmpty = true;
//	f.postal.isNumeric = true;
//	f.postal.isLengthBetween = [5,5]
//	f.phone_day.isPhoneNumber = true;
//	f.phone_evening.optional = true;
//
//	var preCheck = (!f.infohtml.checked && !f.infocss.checked && !f.infojs.checked) ? 'select at least one checkbox.\n' : null;
//	var preCheck = (!f.firstname) ? 'All fields marked with a * are required.\n' : null;
//	return validateForm(f, preCheck);
//}
// --------------------------------------------------