// 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 fill in 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="";
	}
	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));
//	item.value = dtItem.toSimpleForm();
	return true;
}


function vs_item_selected(item, itemName) {
	var strErrorMsg = "hey, choose your " + 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 = "uh, type in a real email address";
	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 the 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;
}

// 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;
	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();

//Validate Date
function checkValidDate(dateStr) {
    // dateStr must be of format month day year with either slashes
    // or dashes separating the parts. Some minor changes would have
    // to be made to use day month year or another format.
    // This function returns True if the date is valid.
    var slash1 = dateStr.indexOf("/");
    if (slash1 == -1) { slash1 = dateStr.indexOf("-"); }
    // if no slashes or dashes, invalid date
    if (slash1 == -1) { return false; }
    var dateMonth = dateStr.substring(0, slash1)
    var dateMonthAndYear = dateStr.substring(slash1+1, dateStr.length);
    var slash2 = dateMonthAndYear.indexOf("/");
    if (slash2 == -1) { slash2 = dateMonthAndYear.indexOf("-"); }
    // if not a second slash or dash, invalid date
    if (slash2 == -1) { return false; }
    var dateDay = dateMonthAndYear.substring(0, slash2);
    var dateYear = dateMonthAndYear.substring(slash2+1, dateMonthAndYear.length);
    if ( (dateMonth == "") || (dateDay == "") || (dateYear == "") ) { return false; }
    // if any non-digits in the month, invalid date
    for (var x=0; x < dateMonth.length; x++) {
        var digit = dateMonth.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text month to a number
    var numMonth = 0;
    for (var x=0; x < dateMonth.length; x++) {
        digit = dateMonth.substring(x, x+1);
        numMonth *= 10;
        numMonth += parseInt(digit);
    }
    if ((numMonth <= 0) || (numMonth > 12)) { return false; }
    // if any non-digits in the day, invalid date
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text day to a number
    var numDay = 0;
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        numDay *= 10;
        numDay += parseInt(digit);
    }
    if ((numDay <= 0) || (numDay > 31)) { return false; }
    // February can't be greater than 29 (leap year calculation comes later)
    if ((numMonth == 2) && (numDay > 29)) { return false; }
    // check for months with only 30 days
    if ((numMonth == 4) || (numMonth == 6) || (numMonth == 9) || (numMonth == 11)) { 
        if (numDay > 30) { return false; } 
    }
    // if any non-digits in the year, invalid date
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text year to a number
    var numYear = 0;
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        numYear *= 10;
        numYear += parseInt(digit);
    }
    // Year must be a 2-digit year or a 4-digit year
    if ( (dateYear.length != 2) && (dateYear.length != 4) ) { return false; }
    // if 2-digit year, use 50 as a pivot date
    if ( (numYear < 50) && (dateYear.length == 2) ) { numYear += 2000; }
    if ( (numYear < 100) && (dateYear.length == 2) ) { numYear += 1900; }
    if ((numYear <= 0) || (numYear > 9999)) { return false; }
    // check for leap year if the month and day is Feb 29
    if ((numMonth == 2) && (numDay == 29)) {
        var div4 = numYear % 4;
        var div100 = numYear % 100;
        var div400 = numYear % 400;
        // if not divisible by 4, then not a leap year so Feb 29 is invalid
        if (div4 != 0) { return false; }
        // at this point, year is divisible by 4. So if year is divisible by
        // 100 and not 400, then it's not a leap year so Feb 29 is invalid
        if ((div100 == 0) && (div400 != 0)) { return false; }
    }
    // date is valid
    return true;
}

