// 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

//var browser = navigator.appName;
//if (browser.indexOf("Microsoft")==-1)
//	var netscape = true

function display_name(item) {
	var strDisplay = item.title;
	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;
	var month = this.getMonth()+1
	if(month<10)
		month = "0"+month	
	var day = this.getDate()
	if(day<10)
		day = "0"+day
	var year = 1900 + this.getYear()
	toSimpleForm = month+"/"+day+"/"+year;
	return toSimpleForm;
}


function es_non_blank() {
	var item = event.srcElement;
	event.returnValue = vs_non_blank(item);
}
function vs_non_blank(item) {
	var strErrorMsg = display_name(item)
	strErrorMsg += " must have a non-blank value";
	item.value=item.value.Trim();
	if (item.value.length==0) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}


function es_valid_number() {
	var item = event.srcElement;
	event.returnValue = vs_valid_number(item);
}
function vs_valid_number(item) {
	var strErrorMsg = display_name(item) + " must be a valid numeric";
	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 es_valid_hours() {
	var item = event.srcElement;
	event.returnValue = vs_valid_hours(item);
}
function vs_valid_hours(item) {
	var strErrorMsg = display_name(item);
	if (!vs_valid_number(item))
		return false;
	var itemValue = new Number(item.value);
	if ((itemValue < 0 || itemValue > 80)) {
		item.focus();
		alert(strErrorMsg + " must have a value from 0 to 80 hours");
		return false;
	}
	itemValue *= 4;
	if ((itemValue)!=Math.ceil(itemValue)) {
		item.focus();
		alert(strErrorMsg + " must be a valid quartely increment");
		return false;
	}
	return true;
}


function es_valid_date() {
	var item = event.srcElement;
	event.returnValue = vs_valid_date(item);
}
function vs_valid_date(item) {
	var strErrorMsg = display_name(item);
	if (isNaN(Date.parse(item.value))) {
		item.focus();
		alert(strErrorMsg + " must be a valid Date");
		return false;
	}
	var dtItem = new Date(Date.parse(item.value));
	dtItem.value = dtItem.toSimpleForm();
			if(dtItem.getYear() < 30)
		  dtItem.setYear(dtItem.getYear() + 2100)
	return true;
}


function es_item_selected() {
	var item = event.srcElement;
	event.returnValue = vs_item_selected(item);
}
function vs_item_selected(item) {
	var strErrorMsg = display_name(item) + " must be a valid selection";
	if (item.selectedIndex==0) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

function es_valid_zip() {
	var item = event.srcElement;
	event.returnValue = vs_valid_zip(item);
}

function vs_valid_zip(item) {
	var strErrorMsg = "Zip codes must be of the form 99999";
	item.value=item.value.Trim();
	if (item.value.length == 0)
		return true;
	if (!(/^\d{5}$/.test(item.value))) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

function vs_valid_phone(item){
	
	var strErrorMsg = display_name(item) + " must be of the form 999-999-9999";
	item.value=item.value.Trim();
	if (!(/^\d{3}-\d{3}-\d{4}$/.test(item.value))) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

function es_valid_ssnbr() {
	var item = event.srcElement;
	event.returnValue = vs_valid_ssnbr(item);
}
function vs_valid_ssnbr(item) {
	var strErrorMsg = display_name(item) + " must be of the form 999-99-9999";
	item.value=item.value.Trim();
	if (!(/^\d{3}-\d{2}-\d{4}$/.test(item.value))) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

function es_valid_email() {
	var item = event.srcElement;
	event.returnValue = vs_valid_email(item);
}
function vs_valid_email(item) {
	var strErrorMsg = display_name(item) + " is not a valid Email";
	item.value=item.value.Trim();
	if (!(item.value.indexOf("@")>0)) {
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

	
// build the validation object
function validation_setup() {
	this.eventNonBlank = es_non_blank;
	this.nonBlank = vs_non_blank;
	this.eventValidNumber = es_valid_number;
	this.validNumber = vs_valid_number;
	this.eventValidHours = es_valid_hours;
	this.validHours = vs_valid_hours;
	this.eventValidDate = es_valid_date;
	this.validDate = vs_valid_date;
	this.eventItemSelected = es_item_selected;
	this.itemSelected = vs_item_selected;
	this.eventValidZip = es_valid_zip;
	this.validZip = vs_valid_zip;
	this.eventValidSSNbr = es_valid_ssnbr;
	this.validSSNbr = vs_valid_ssnbr;
	this.eventValidEmail = es_valid_email;
	this.validEmail = vs_valid_email;
	this.validPhone = vs_valid_phone;
	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();


// This set of function are for processing the key press event
// Used to restrict input on numerics and pure textual fields

function kp_integer() {
	if ((event.keyCode < 48 || event.keyCode > 57))
		event.returnValue = false;
}
function kp_numeric(e) {
	if (e.which == 8)
	{}
	else
	if ((e.which != 46) && (e.which < 48 || e.which > 57))
		return false;
	if (e.which == 46)
	 {}
		
	
}


function kp_character(e) {
	if (e.which == 8)
	{}
	else
	if ((e.which < 65 || e.which > 90) && (e.which < 97 || e.which > 122))
	return false;
 }


function kp_convert_upper() {
	if ((event.keyCode >= 97 && event.keyCode <= 122))
		event.keyCode -= 32;
}

function kp_convert_lower() {
	if ((event.keyCode >= 65 && event.keyCode <= 90))
		event.keyCode += 32;
}

function kp_Zip(e){
	if (e.which == 8)
	{}
	else
	{if (this.value.length > 4)
	 return false;
	if ((e.which != 46) && (e.which < 48 || e.which > 57))
		return false;}
	//if (e.which == 46) {
	//	if (event.srcElement.value.indexOf(".") > -1)
	//		event.returnValue = false;}
		
	} 

function kp_setup() {
	this.Integer = kp_integer;
	this.Numeric = kp_numeric;
	this.Character = kp_character;
	this.ConvertUpper = kp_convert_upper;
	this.ConvertLower = kp_convert_lower;
	this.Zip = kp_Zip;
	return this;
}

var keyPressInput = new Object;
keyPressInput = kp_setup();

