// This set of function are general includes for validation
// They are designed in pairs the validation and the event function
// the event function will call the validation with the event src

function display_name(item) {
	var strDisplay = item.getAttribute("DisplayName");
	if (strDisplay==null || strDisplay=="")
		strDisplay="Field";
	return strDisplay;
}

function default_value(item) {
	var strDefault = item.defaultValue;
	if (strDefault==null || strDefault=="")
		strDefault="";
	return strDefault;
}

function vs_valid_number(item, itemName) {
	var strErrorMsg = "Please enter a valid number in the " + itemName + " field.";
	var strDefault = default_value(item);
	if (strDefault.length==0) {
		strDefault="0";
	}
	item.value=item.value.Trim();
	if (item.value.length==0)
		item.value=strDefault;
	var num = ".0123456789";
	for (var intLoop = 0; intLoop < item.value.length; intLoop++) {
		if (num.indexOf(item.value.charAt(intLoop)) == -1) {
			item.focus();
			alert(strErrorMsg);
			return false;
		}
	}
	if (item.value.indexOf(".")!=item.value.lastIndexOf(".")) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

function trim_string() {
	var ichar, icount;
	var strValue = this;
	ichar = strValue.length - 1;
	icount = -1;
	while (strValue.charAt(ichar)==' ' && ichar > icount)
		--ichar;
	if (ichar!=(strValue.length-1))
		strValue = strValue.slice(0,ichar+1);
	ichar = 0;
	icount = strValue.length - 1;
	while (strValue.charAt(ichar)==' ' && ichar < icount)
		++ichar;
	if (ichar!=0)
		strValue = strValue.slice(ichar,strValue.length);
	return strValue;
}

function date_toSimpleForm() {
	var toSimpleForm = new String;
	toSimpleForm = this.toLocaleString();
	toSimpleForm = toSimpleForm.substring(0,toSimpleForm.indexOf(' '));
	return toSimpleForm;
}


function vs_non_blank(item, itemName) {
	var strErrorMsg = "Please complete the " + itemName + " field.";
	item.value=item.value.Trim();
	if (item.value.length==0) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}


	


function vs_valid_hours(item, itemName) {
	var strErrorMsg = itemName;
	if (!vs_valid_number(item, itemName))
		return false;
	var itemValue = new Number(item.value);
	if ((itemValue < 0 || itemValue > 80)) {
		item.focus();
		alert("Please enter a value from 0 to 80 hours for the " + strErrorMsg + " field.");
		return false;
	}
	itemValue *= 4;
	if ((itemValue)!=Math.ceil(itemValue)) {
		item.focus();
		alert("Please enter a valid quartely increment for the " + strErrorMsg + " field.");
		return false;
	}
	return true;
}


function vs_valid_date(item, itemName) {
	var strErrorMsg = itemName;
	if (isNaN(Date.parse(item.value))) {
		item.focus();
		alert("Please enter a valid date for the " + strErrorMsg + " field.");
		return false;
	}
	var dtItem = new Date(Date.parse(item.value));
	item.value = dtItem.toSimpleForm();
	return true;
}


function vs_item_selected(item, itemName) {
	var strErrorMsg = "Please select the " + itemName + ".";
	if (item.selectedIndex==0) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

function vs_valid_zip(item, itemName) {
	var strErrorMsg = "Please enter a valid ZIP Code of the form 99999 for the " + itemName + " field.";
	item.value=item.value.Trim();
	if (!(/^\d{5}$/.test(item.value) || /^\d{5}-\d{4}$/.test(item.value))) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

function vs_valid_ssnbr(item, itemName) {
	var strErrorMsg = "Please enter a valid SSN of the form 999-99-9999 for the " + itemName + " field.";
	item.value=item.value.Trim();
	if (!(/^\d{3}-\d{2}-\d{4}$/.test(item.value))) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}


function vs_valid_email(item, itemName) {
	var strErrorMsg = "Please enter a valid address in the " + itemName + " field.";
	item.value=item.value.Trim();
	if (!(/^[\w\.\_\-]+@[\w-\.\_\-]+$/.test(item.value))) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

//two fields must have identical values
//**THIS FUNCTION IS NOT LIKE THE OTHERS**
//only pass the two element names(in any order)
function vs_password_confirm(itemA, itemB) {
	var strErrorMsg = "Please confirm your password.";
	if (itemA.value!=itemB.value) {
		itemB.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

//at least one radio button must be checked
function vs_radio_selected(item, itemName){
	var strErrorMsg="Please select the "+itemName;
	for (var i=0; i<item.length; i++)
	if (item[i].checked == true){
	return true;
	}
	alert(strErrorMsg);
	return false;
}

//	valid phone number
//	requires phone number including area code
//	accepted formats:
//	(XXX)XXX-XXXX
//	XXX-XXX-XXXX
//	XXXXXXXXXX
function vs_valid_phone(item, itemName) {
	var strErrorMsg = "Please enter a valid phone number in the " + itemName + " field.";
	item.value=item.value.Trim();
	if (!(/^\d{10}$/.test(item.value) || /^\d{3}-\d{3}-\d{4}$/.test(item.value) || /^\(\d{3}\)\d{3}-\d{4}$/.test(item.value)  )) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

//make sure string begins with http://
//always returns true
function vs_http_prefix(item) {
	var itemValue=item.value.Trim();
		if (itemValue.substring(0,7).toLowerCase() !="http:\/\/")
			item.value = "http:\/\/" + itemValue;
	return true;
}

//prevents troublesome characters from being entered
function vs_bad_characters(item, itemName) {
var strErrorMsg = "The " + itemName + " field may not contain any of the following characters: & \+ \( \) ' \; % \> \< ' or \x22.";
	var badChar = new Array("&","\+","\(","\)","'","\;","%","\>","\<","[,]",'"');
	var strItem = item.value.Trim();
	for (var i = 0; i < 11; i++) {
		if (strItem.indexOf(badChar[i]) != -1){
		item.focus();
		alert(strErrorMsg);
		return false;
		}
	}
	return true;
}



//build the validation object
function validation_setup() {
	this.nonBlank = vs_non_blank;
	this.validNumber = vs_valid_number;
	this.validHours = vs_valid_hours;
	this.validDate = vs_valid_date;
	this.itemSelected = vs_item_selected;
	this.validZip = vs_valid_zip;
	this.validSSNbr = vs_valid_ssnbr;
	this.validEmail = vs_valid_email;
	this.confirmPassword = vs_password_confirm;
	this.radioSelected = vs_radio_selected;
	this.validPhone = vs_valid_phone;
	this.httpPrefix = vs_http_prefix;
	this.badChars = vs_bad_characters;
	return this;
}

// Extend the string object to include a trim function
String.prototype.Trim = trim_string;
// Extend the date object to include a simple form string conversion
Date.prototype.toSimpleForm = date_toSimpleForm;

// Construct the validation object
var validation = new Object;
validation = validation_setup();
