function rerun_validation(obj){
	for(i=0; i < obj.elements.length; i++){
		fld = obj.elements[i];
		if(fld.onchange != null){
			validationtype = fld.onchange.toString();
			if(validationtype.indexOf("validatethis") >= 0){
				if(document.all){
					first_quote = validationtype.indexOf("'");
					validationtype = validationtype.substring(first_quote+1,validationtype.indexOf("'",first_quote+1));
				}
				else {
					first_quote = validationtype.indexOf('"');
					validationtype = validationtype.substring(first_quote+1,validationtype.indexOf('"',first_quote+1));
				}
				if(!validatethis(validationtype,fld)){
					return false;
				}
			}
		}
		else if(fld.onblur != null){
			validationtype = fld.onblur.toString();
			if(validationtype.indexOf("validatethis") >= 0){
				if(document.all){
					validationtype = validationtype.substring(validationtype.indexOf("'")+1,validationtype.lastIndexOf("'"));
				}
				else {
					validationtype = validationtype.substring(validationtype.indexOf('"')+1,validationtype.lastIndexOf('"'));
				}
				if(!validatethis(validationtype,fld)){
					return false;
				}
			}
		}
	}
	return true;
}
function validatethis(type, field) {
	oktosubmit = true;
	msg1 = ""
	msg2 = ""
	if(type == "phone") {
		mask = "'999-999-9999 ??????'"
		msg1 = validatemask("999-999-9999 ??????", field.value)
		msg2 = validatemask("999-9999 ??????", field.value)
	}
	else if(type == "ssn") {
		mask = "'999-99-9999'"
		msg1 = validatemask("999-99-9999", field.value)
		msg2 = "x"
	}
	else if(type == "N"){
		if(checknumber(field.value) == false){
			mask = "of a number, without words or punctuation."
			msg1 = "Invalid format"
			msg2 = "x"
		}
	}
	else if(type == "D"){
		if (checkdate(field,'past') == false) {
		alert("Please enter a valid date");
		field.select();
		field.focus();
		oktosubmit = false;
		}
	}
	else if(type == "D-Future"){
		if (checkdate(field,'future') == false) {
		alert("Please enter a valid date");
		field.select();
		field.focus();
		oktosubmit = false;
		}
	}
	else if(type == "mm/dd/yyyy"){
		if(right(field.value,4).indexOf("/") > 0){
			alert("Please enter a valid date, 4 digit year");
			field.select();
			field.focus();
			oktosubmit = false;
		}
		else if (!checkdate(field,'past')) {
			alert("Please enter a valid date");
			field.select();
			field.focus();
			oktosubmit = false;
		}
	}
	else if(type == "mm/yyyy"){
		if(date_mmyyyy(field) == false){
			mask = "MM/YYYY";
			msg1 = "Please enter a valid date";
			msg2 = "x";
		}
	}
	else if(type == "I"){
		if (checkinteger(field.value) == false) {
		alert("Please enter a valid integer without commas")
		field.select();
		field.focus();
		oktosubmit = false;
		}
	}
	else if(type == "email"){
		var r = /^[\w\-%_\.]+@([\w\-]+\.)+\w{2,4}$/;
		if(field.value && !r.test( field.value )){
			alert("Please enter a valid e-mail address.")
			field.select();
			field.focus();
			return false;
		}
	}
	else if(type == "zip"){
		var r = /^(\d{5}|\d{5}-\d{4})$/;
		var r_cdn = /[A-Z]\d[A-Z][ ]\d[A-Z]\d/;

		if(field.value && !r.test( field.value ) && !r_cdn.test( field.value)){
			alert("Please enter a valid zip/postal code.");
			field.select();
			field.focus();
			return false;
		}
	}
	if(msg1 != "" && msg2 != "") {
		alert(msg1 +": Expecting entry in the form " + mask)
		field.select();
		field.focus();
		oktosubmit = false;
	}
	return oktosubmit;
}
function validatemask(mask, value) {
		if(value.length == 0) {
			return ""
		}
		for (var i=0; i <= Math.max(value.length,mask.length); i++) {
			m = mask.substring(i,i+1);
			v = value.substring(i,i+1);
			if(m == "9") {
				if(outside(v, "0", "9")) return "Invalid format"
			}
			else if(m == "A") {
				if(outside(v.toUpperCase()), "A", "Z") return "Invalid format"
			}
			else if(m == "?") {
			}
			else if(m == " " && v == "") {
			}
			else {
				if(m != v) return "Invalid format"
			}
		}
		return "";
}

function outside(string, x, y) {
	if (string < x || string > y) return true
	return false
}
function date_mmyyyy(field){
   	mmyyyy = field.value;
 	pos = mmyyyy.indexOf('/');
 	if(pos == 0 || pos != mmyyyy.lastIndexOf("/")){
		return false;
 	}
 	else {
		if(mmyyyy.substr(0,1) == "0"){
			m = mmyyyy.substr(1,1)
		}
		else if(pos == 2) {
			m = mmyyyy.substr(0,2)
		}
		else {
			m = mmyyyy.substr(0,1)
		}
		if(parseInt(mmyyyy.substr(pos+1)) == 0){
  			y = parseInt(mmyyyy.substr(pos+2));
		}
		else {
  			y = parseInt(mmyyyy.substr(pos+1));
		}
		if (mmyyyy == ""){
			//ok
		}
	 	else if (m > 0 && m < 13 && y >= 1870 && y <= 2050){
			//ok
	 	}
	 	else if (m > 0 && m < 13 && y >= 1 && y <= 9){
	 		field.value = m.toString() + "/200" + y.toString();
	 	}
	 	else if (m > 0 && m < 13 && y >= 10 && y <= 20){
	 		field.value = m.toString() + "/20" + y.toString();
	 	}
	 	else {
			return false;
	 	}
	}
	return true;
}
function checkdate(obj,when){
//Returns true if value is a date format or is NULL
//otherwise returns false	

    if (obj.value.length == 0)
        return true;

	if (obj.value.length <= 5){
		today = new Date();
		yr = today.getYear();
		if(ns4) yr = yr + 1900;
		obj.value = obj.value +"/"+ yr;
	}
	object_value = obj.value

    //Returns true if value is a date in the mm/dd/yyyy format
	isplit = object_value.indexOf('/');

	if (isplit == -1 || isplit == object_value.length)
		return false;

    sMonth = object_value.substring(0, isplit);
	isplit = object_value.indexOf('/', isplit + 1);

	if (isplit == -1 || (isplit + 1 ) == object_value.length)
		return false;
    sDay = object_value.substring((sMonth.length + 1), isplit);
	
	sYear = object_value.substring(isplit + 1);
	if (!checkinteger(sMonth)) //check month
		return false;
	else
	if (!checkrange(sMonth, 1, 12)) //check month
		return false;
	else
	if (!checkinteger(sYear)) //check year
		return false;
	else
	if (!checkrange(sYear, 0, 99) && !checkrange(sYear, 1800, 2040)) //check year
		return false;
	else
	if (!checkinteger(sDay) || sDay.length == 0) //check day
		return false;
	else
	if (!checkday(sYear, sMonth, sDay)) // check day
		return false;
	else
	if (when == 'past' && new Date(object_value) > new Date())
		return false;
	else
		return true;
}

function checkday(checkYear, checkMonth, checkDay){
	maxDay = 31;

	if (checkMonth == 4 || checkMonth == 6 ||
			checkMonth == 9 || checkMonth == 11)
		maxDay = 30;
	else
	if (checkMonth == 2)
	{
		if (checkYear % 4 > 0)
			maxDay =28;
		else
		if (checkYear % 100 == 0 && checkYear % 400 > 0)
			maxDay = 28;
		else
			maxDay = 29;
	}

	return checkrange(checkDay, 1, maxDay); //check day
}

function checkinteger(object_value){
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;

    //The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)
	return checknumber(object_value);
    else
	return false;
}

function checkrange(object_value, min_value, max_value){
    //if value is in range then return true else return false

    if (object_value.length == 0)
        return true;

    if (!checknumber(object_value))
	{
	return false;
	}
    else
	{
	return (numberrange((eval(object_value)), min_value, max_value));
	}
	
    //All tests passed, so...
    return true;
}

function numberrange(object_value, min_value, max_value){
    // check minimum
    if (min_value != null)
	{
        if (object_value < min_value)
		return false;
	}

    // check maximum
    if (max_value != null)
	{
	if (object_value > max_value)
		return false;
	}
	
    //All tests passed, so...
    return true;
}

function checknumber(object_value) {
	    //Returns true if value is a number or is NULL
    	//otherwise returns false	
	    if (object_value.length == 0)
    	    return true;

	    //Returns true if value is a number defined as
    	//   having an optional leading + or -.
	    //   having at most 1 decimal point.
	    //   otherwise containing only the characters 0-9.
		var start_format = " .+-0123456789";
		var number_format = " .0123456789";
		var check_char;
		var decimal = false;
		var trailing_blank = false;
		var digits = false;

	    //The first character can be + - .  blank or a digit.
		check_char = start_format.indexOf(object_value.charAt(0))
	    //Was it a decimal?
		if (check_char == 1)
		    decimal = true;
		else if (check_char < 1)
			return false;
        
		//Remaining characters can be only . or a digit, but only one decimal.
		for (var i = 1; i < object_value.length; i++){
			check_char = number_format.indexOf(object_value.charAt(i))
			if (check_char < 0)
				return false;
			else if (check_char == 1){
				if (decimal)		// Second decimal.
					return false;
				else
					decimal = true;
			}
			else if (check_char == 0){
				if (decimal || digits)	
					trailing_blank = true;
	        // ignore leading blanks
	
			}
	    	    else if (trailing_blank)
				return false;
			else
				digits = true;
		}	
    	//All tests passed, so...
	    return true
}
function right(txt,num){
	len = txt.length;
	return txt.substr(len-num,num);
}
function left(txt,num){
	return txt.substr(0,num);
}
function right(txt,num){
	len = txt.length;
	return txt.substr(len-num,num);
}
