// 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 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_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 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));
	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 xxxxx 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 email address in the " + itemName + " field.";
	item.value=item.value.Trim();
	if (!(/.+\@.+\..+/.test(item.value))) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}



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;
}

//	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;
}

//check that at least one item is selected on a multiple select element
function vs_multi_select(item, itemName) {
	var strErrorMsg = "Please select at least one " + itemName + ".";
	if (item.selectedIndex==-1) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

function vs_check_username(item, itemName) {
	var strErrorMsg;
	if ((item.value.length < 3) || (item.value.length > 20)) {
		item.focus();
		strErrorMsg = "Please confine your user name to be between 3 and 20 characters."
		alert(strErrorMsg);
		return false;
	}
	else {
		if (item.value.indexOf("@") > 0) {
		item.focus();
		strErrorMsg = "Please do not use the character @."
		alert(strErrorMsg)
		return false;
		}
		if (item.value.indexOf("*") > 0) {
		item.focus();
		strErrorMsg = "Please do not use the character *."
		alert(strErrorMsg)
		return false;
		}
		if (item.value.indexOf("$") > 0) {
		item.focus();
		strErrorMsg = "Please do not use the character $."
		alert(strErrorMsg)
		return false;
		}
		if (item.value.indexOf("%") > 0) {
		item.focus();
		strErrorMsg = "Please do not use the character %."
		alert(strErrorMsg)
		return false;
		}
		if (item.value.indexOf("!") > 0) {
		item.focus();
		strErrorMsg = "Please do not use the character !."
		alert(strErrorMsg)
		return false;
		}
		if (item.value.indexOf("^") > 0) {
		item.focus();
		strErrorMsg = "Please do not use the character ^."
		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;
}

function vs_valid_usstateshort(item, itemName){
	var arrStates = new Array('AK','AZ','AR','CA','CO','CT','DE','FL','GA','HI','ID','IL','IN','IA','KS','KY','LA','ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','OH','OK','OR','PA','PR','RI','SC','SD','TN','TX','UT','VT','VA','WA','WV','WI','WY','ZZ');
	var bIsValid = false;
	var strErrorMsg="Please enter a two-character US State code for the "+itemName;
	for(i=0;i<arrStates.length;i++)
	if(item.value.toUpperCase() == arrStates[i])
	bIsValid=true;
	if (bIsValid == true)
	return true;
	alert(strErrorMsg);
	return false;

}

// 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.validPhone = vs_valid_phone;
	this.multiSelect = vs_multi_select;
	this.validUsername = vs_check_username;
	this.radioSelected = vs_radio_selected;
	this.validUSStateShort = vs_valid_usstateshort;
	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();

