// Copyright 2003 AlphaPlex, Inc.
// http://www.alphaplex.net
//used by permission//
//Temporary Validation...

/*here
if (theForm.charityId.value == "0") {
		if(!confirm ("You have not selected a charity.  While it is not required, you may click 'Cancel' to choose one now or click 'Ok' to continue."))
			{ theForm.charityId.focus();
			return false;
			}
	}
//here*/

function validateForm(theForm) {
	for (var i=0; i < theForm.length; i++) {  // loop through form elements
		var e = theForm.elements[i];  // current element
		//if ((e.type == "Submit") || (e.type == "Reset")) continue;

		if (e.required) {  // check to see if a required field is empty
			if (e.type == "radio" || e.type == "checkbox") {
				var radioSelected = false;
				for (var j = i; j < theForm.length; j++) {
					if (theForm.elements[j].name == e.name && theForm.elements[j].checked) {
						radioSelected = true;
						break;
					}
				}// end for
				if (!radioSelected) {
					if (e.alertMessage == null)
						alert("Please select one of the \"" + e.name + "\" options.");
					else
						alert(e.alertMessage);
					e.focus();
					return (false);
				}// end if					
			}// end if
			else if (e.type == "select-one" || e.type == "select-multiple") {
				if (e.selectedIndex == 0) {
					if (e.alertMessage == null)
						alert("The first \"" + e.name + "\" option is not a valid selection.  Please choose one of the other options.");
					else
						alert(e.alertMessage);
					e.focus();
					return (false);
				}				
			}
			else {
				if ((e.value == null) || (e.value == "") || isBlank(e.value)) {
					if (e.alertMessage == null)
						alert("Please enter a value in the \"" + e.name + "\" field.");
					else
						alert(e.alertMessage);
					e.focus();
					return false;
				}
			}
		}
		if (e.validator) {		
			var v = e.validator;
		} else {
			var v = "";
		}
		//alert (e.required);
		v = v.toLowerCase();
		var valid = true;
		switch (v) {
			case 'date':
				valid = isValidDate(e);
				break;
			case 'time':
				valid = isValidTime(e);
				break;
			case 'currency':
			case 'money':
				valid = isValidCurrency(e);
				break;
			case 'zip':
			case 'zipcode':
			case 'zip_code':
			case 'zip-code':
				valid = isValidZip(e);
				break;
			case 'email':
			case 'e-mail':
			case 'e_mail':
				valid = isValidEmail(e);
				break;
			case 'phone':
			case 'phone_no':
			case 'phone_number':
			case 'phonenumber':
				valid = isValidPhone(e);
				break;
			case 'image':
			case 'imagefile':
			case 'image_file':
			case 'img':
				valid = isValidImage(e);
				break;
			case 'integer':
			case 'int':
			case 'number':
			case 'num':
				valid = isValidInteger(e);
				break;
			case 'float':
			case 'real':
				valid = isValidFloat(e);
				break;
		}
		if (!valid) return false;
		
		if ((e.maxLength != null) && (e.maxLength != "")) {
			if (! checkLength(e))
				return false;
		}
		
		if ((e.minLength != null) && (e.minLength != "")) {
			if (! checkMinLength(e))
				return false;
		}
	}			

	return true;  // all form fields are valid
}


function isBlank(s) {
	for (var i=0; i<s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

function checkLength(theField) {
	var len = theField.value.length;
	if (theField.maxLength < 0)
		return true;
	if (len <= theField.maxLength) {
		return true;
	}
	else {
		alert("Please enter no more than " + theField.maxLength + " characters in the \"" + theField.name + "\" field.");
		theField.focus();
		return false;
	}
}

function checkMinLength(theField) {
	var len = theField.value.length;
	if (len >= theField.minLength || (!theField.required && len == 0)) {
		return true;
	}
	else {
		alert("Please enter at least " + theField.minLength + " characters in the \"" + theField.name + "\" field.");
		theField.value = "";
		theField.focus();
		return false;
	}
}

function isValidInteger (theField) {
	var str = null;
	str = theField.value;
	var pattern = /^\d+$/;
	var matchArray = str.match(pattern);
	if (matchArray == null && str != "") {
		alert("Please enter an integer number value (digits only).");
		theField.focus();
		return false;
	}
	return true;
}

function isValidFloat (theField) {
	var str = null;
	str = theField.value;
	var pattern = /^\d+(\.\d+)?$/;
	var matchArray = str.match(pattern);
	if (matchArray == null && str != "") {
		alert("Please enter a floating point number value.");
		theField.focus();
		return false;
	}
	return true;
}

function isValidImage (theField) {
	var str = null;
	str = theField.value;
	var pattern = /^[\w\.\-_]+\.(gif|jpg)$/i;
	var matchArray = str.match(pattern);
	if (matchArray == null && str != "") {
		alert("Invalid image file name: please enter a .gif or .jpg filename.");
		theField.focus();
		return false;
	}
	return true;
}

function isValidPhone (theField) {
	var str = null;
	str = theField.value;
	var pattern = /^\d{3}\-\d{3}\-\d{4}/;;
	var matchArray = str.match(pattern);
	if (matchArray == null && str != "") {
		alert("Phone number is not in a valid format.");
		theField.focus();
		return false;
	}
	return true;
}

function isValidTime(timeField) {
	var timeStr = null;
	timeStr = timeField.value;
	
	//var timePat = /^([0-1]?[0-9]|2[0-3]):[0-5]\d$/;
	var timePat = /^(((0?[1-9]|1[0-2]):[0-5]\d(:[0-5]\d)?\s*(AM|PM))|(([0-1]?[0-9]|2[0-3]):[0-5]\d(:[0-5]\d)?))\s*$/i;
	
	var matchArray = timeStr.match(timePat);
	if (matchArray == null && timeStr != "") {
		alert("Time is not in a valid format.");
		timeField.focus();
		return false;
	}
	return true;
}

function isValidCurrency(currencyField) {
	var currencyStr = null;
	currencyStr = currencyField.value;
	
	var currencyPat = /^\$?((\d{1,3}(,\d{3})*)|(\d+))(\.\d{2})?$/;
	
	var matchArray = currencyStr.match(currencyPat);
	if (matchArray == null && currencyStr != "") {
		alert("Bid amount is not in a valid format.");
		currencyField.focus();
		return false;
	}
	return true;
}

function isValidZip(zipField) {
	var zipStr = null;
	zipStr = zipField.value;
	
	var zipPat = /^\d{5}(-\d{4})?$/;
	
	var matchArray = zipStr.match(zipPat);
	if (matchArray == null && zipStr != "") {
		alert("Zip code is not in a valid format.");
		zipField.focus();
		return false;
	}
	return true;
}

function isValidDate(dateField) {
	var dateStr = null;
	dateStr = dateField.value;

	var monthArray = [null, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4}|\d{2})\s*/;
	var timePat = /^(((0?[1-9]|1[0-2]):[0-5]\d(:[0-5]\d)?\s*(AM|PM))|(([0-1]?[0-9]|2[0-3]):[0-5]\d(:[0-5]\d)?))\s*$/i;

	// To require a 4 digit year entry, use this line instead:
	// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})\s*/;
	
	//First check to see if we have an empty string to avoid a Javascript bug after
	//the matching is performed.
	if (dateStr == "") {
		return true;
	}

	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null && dateStr != "") {
		alert("Date is not in a valid format.");
		dateField.focus();
		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.");
		dateField.focus();
		return false;
	}
	if (day < 1 || day > 31) { // check overall days range
		alert("Error: Day must be between 1 and 31.");
		dateField.focus();
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) { // check for months with only 30 days
		alert("Error: "+monthArray[month]+" doesn't have 31 days.");
		dateField.focus();
		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("Error: February " + year + " doesn't have " + day + " days.");
			dateField.focus();
			return false;
		}
	}
	
	var timeStr = dateStr.replace(datePat, "");
	var timeMatchArray = timeStr.match(timePat);
	if (timeMatchArray == null && timeStr != "") {
		alert("Time within date field is not in a valid format.");
		dateField.focus();
		return false;
	}
	
	return true;  // date is valid
}

function isValidEmail(emailField) {
	var emailStr = null;
	emailStr = emailField.value;
	//alert("Its running.");
	var emailPat = /^[^\/\\<>\(\)\{\}":,;\[\]@\. ]+(\.[^\/\\<>\(\)\{\}":,;\[\]@ ]+)?\@[^\/\\<>\(\)\{\}":,;\[\]@\. ]+\.[^\/\\<>\(\)\{\}":,;\[\]@\. ]+(\.[^\/\\<>\(\)\{\}":,;\[\]@ ]+)?$/;
	
	var matchArray = emailStr.match(emailPat);
	if (matchArray == null && emailStr != "") {
		alert("Email address is not in a valid format.");
		emailField.focus();
		return false;
	}
	return true;
}



