/*********************************************************
returns true if a string, s, is blank (null or whitespace)
**********************************************************/
function isblank(s) {
	if (s == null) return true;
	if (s == "") return true;
	for (var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

/**************************************************************************
Returns true if date, d, is in a valid format.
Does not check for valid dates (ie 2/30/1999 returns true)
Required date format ?#/?#/####
***************************************************************************/
function isdate(d) {
	if (d == null) return false;
	if (d == "") return false;
	if (d.charAt(1) != '/' && d.charAt(2) != '/') return false;
	if (d.charAt(1) == '/' && !(d.charAt(3) == '/' || d.charAt(4) == '/')) return false;
	if (d.charAt(2) == '/' && !(d.charAt(4) == '/' || d.charAt(5) == '/')) return false;
	var ary = d.split('/');
	if (isNaN(parseInt(ary[0])) || isNaN(parseInt(ary[1])) || isNaN(parseInt(ary[2]))) return false;
	if (ary[0].charAt(0) == '0') if (ary[0].length < 2) return false; else ary[0] = ary[0].charAt(1);
	if (ary[1].charAt(0) == '0') if (ary[1].length < 2) return false; else ary[1] = ary[1].charAt(1);
	if (d.length > 10 || d.length < 8) return false;
	if (ary[0].length < 1 || ary[0].length > 2) return false;
	if (ary[1].length < 1 || ary[1].length > 2) return false;
	if (ary[2].length != 4) return false;
	if (parseInt(ary[0]) < 1 || parseFloat(ary[0]) > 12) return false;
	if (parseInt(ary[1]) < 1 || parseFloat(ary[1]) > 31) return false;
	return true;
}

/**************************************************************************
Returns true is webaddress, w, is in a valid format
Example :  	w = "http://aaa.bbb.com/~c" returns true
			w = "http://aaa.bbb.org"	returns true
			w = "ftp://aaa.bbb.org" 	return false
	URL can end in com, org, net, mil, gov, cc, ws, tv
***************************************************************************/
function isweb(w) {
	if (w == null) return false;
	if (w == "") return false;
	var arry = w.split('//');
	if (arry[0] != "http:") return false;
	var arry2 = arry[1].split('/');
	var arry3 = arry2[0].split('.');;
	if (arry3[arry3.length - 1] == "com" || arry3[arry3.length - 1] == "org" ||
		arry3[arry3.length - 1] == "mil" || arry3[arry3.length - 1] == "net" ||
		arry3[arry3.length - 1] == "gov" || arry3[arry3.length - 1] == "cc"  ||
		arry3[arry3.length - 1] == "tv"  || arry3[arry3.length - 1] == "ws") 
		return true;
	else return false;
}

/***********************************************************************************
Loops through all elements of form f.
Possible conditions on the elements can be:
	required - text fields must be non-blank, selects must have selectedIndex != 0
	email - value must contain a '@'
	isdate - value must return true from isdate() above
	loosedate - like isdate, but blank is ok
	numeric - text value must be numeric
	loosenumeric - like numeric, but blank is ok
	minvalue - text value must be numeric and >= specified value
	maxvalue - text value must be numeric and <= specified value
	semireq - at least one of the elements marked as semireq must be non-blank (text) or selectedIndex != 0 (select)

To set these conditions, create an onSubmit() form handler like this:

<form name="foo" action="result.asp" onSubmit="return formsetup();">

the formsetup function will look like this:

function formsetup() {
	f = document.foo;

// Define the validation rules for the form.
	f.emailfield.email = true;
	f.requiredfield.required = true;
	f.numericfield.numeric = true;
	f.numericfield.minvalue = 0;
	f.numericfield.maxvalue = 100;
	f.datefield.loosedate = true;
	f.semireqelt1.semireq = true;
	f.semireqelt2.semireq = true;

// Also, you must specify a pname (pretty name) for each field that will be validated.
// The pretty name will be displayed in the error box, instead of using the (perhaps arcane) field name:
	f.emailfield.pname = "Email";
	f.datefield.pname = "Date";
// etc
// Also, if there is a semi-required rule, give it a name in the function call to formverify:
	return formverify(f, "semi req field name - ie entry method");
}
*********************************************************************************/

function formverify (f, seminame) {

	var msg;
	var errors = "";
	var issemireq = false;
	var semireq = false;
	var loosedatematch = "";

	for (var i = 0; i < f.length; i++) {
		var e = f.elements[i];
		
		if (e.type != "hidden") {
//		window.alert ("name: " + e.name + "\nvalue: " + e.value + "\ntype: " + e.type + "\nldate: " + e.loosedate + "\nisdate: " + e.isdate + "\nrequired: " + e.required + "\nsreq: " + e.semirequired + "\npname: " + e.pname);
// check for invalid characters
			if ((((e.type == "text" || e.type == "textarea") && e.validate) || e.type == "password") && e.value.indexOf("%") != -1)
				errors += "\nInvalid character in " + e.pname + ".";
// check for valid URL
			if (e.url && (e.value.substring (0, 7) != "http://" || e.value.length < 8 || isblank(e.value)))
				if (!isweb(e.value))
					errors += "\nPlease enter a valid url for " + e.pname + ".";
// check for valid email address
			if (e.email && (isblank(e.value) || e.value.indexOf("@") == -1))
				errors += "\nPlease enter a valid email address for " + e.pname + ".";
// check for valid dates
			if ((e.loosedate && e.value != loosedatematch) || e.isdate)
				if (!isdate(e.value))
					errors += "\nThe date you entered for " + e.pname + " is invalid.";
// check for required values
			if (e.required) {
				if (e.type == "text" || e.type == "password" || e.type == "textarea")
					if ((e.def && e.value == e.def) || isblank(e.value))
						errors += "\nPlease enter a value for " + e.pname + ".";
				if (e.type == "select-one" || e.type == "select-multiple")
					if ((e.type == "select-one" && !e.selectedIndex) || (e.type == "select-multiple" && e.selectedIndex < 0))
						errors += "\nPlease enter a value for " + e.pname + ".";
			}
// check for numeric values
			if (((e.loosenumeric && !isblank(e.value)) || e.numeric) && isNaN(parseFloat(e.value)))
				errors += "\n" + e.pname + " must be numeric.";
// check for min/max values
			if (e.minvalue != null && !isNaN(parseFloat(e.value)) && (parseFloat(e.value) < e.minvalue))
				errors += "\n" + e.pname + " must be at least " + e.minvalue + ".";
			if (e.maxvalue != null && !isNaN(parseFloat(e.value)) && (parseFloat(e.value) > parseFloat(e.maxvalue)))
				errors += "\n" + e.pname + " must be less than " + e.maxvalue + ".";
// check for semirequired values
			if (e.semireq && !semireq) {
				issemireq = true;
				if (e.type == "text" || e.type == "password" || e.type == "textarea")
					if (!((e.def && e.value == e.def) || isblank(e.value)))
						semireq = true;
				if (e.type == "select-one" || e.type == "select-many")
					if (e.selectedIndex > 0)
						semireq = true;
			}
		}
	}

	if (issemireq && !semireq) errors += "\nYou must enter a value for at least one " + seminame + " field.";
//	alert ("ERRORS: " + errors);

	if (!errors) return true;
	
//	msg  = "_____________________________________________________________\n\n";
	msg = "The form was not submitted because of the following error(s).\n";
	msg += "Please correct these errors and re-submit.";
//	msg += "_____________________________________________________________\n\n"
	alert (msg + "\n" + errors);
//	alert (errors);
	return false;
}

function RTrim(str)
        /***
                PURPOSE: Remove trailing blanks from our string.
                IN: str - the string we want to RTrim

                RETVAL: An RTrimmed string!
        ***/
        {
                // We don't want to trip JUST spaces, but also tabs,
                // line feeds, etc.  Add anything else you want to
                // "trim" here in Whitespace
                var whitespace = new String(" \t\n\r");

                var s = new String(str);

                if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
                    // We have a string with trailing blank(s)...

                    var i = s.length - 1;       // Get length of string

                    // Iterate from the far right of string until we
                    // don't have any more whitespace...
                    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
                        i--;


                    // Get the substring from the front of the string to
                    // where the last non-whitespace character is...
                    s = s.substring(0, i+1);
                }

                return s;
        }
function LTrim(str)
        /***
                PURPOSE: Remove leading blanks from our string.
                IN: str - the string we want to LTrim

                RETVAL: An LTrimmed string!
        ***/
        {
                var whitespace = new String(" \t\n\r");

                var s = new String(str);

                if (whitespace.indexOf(s.charAt(0)) != -1) {
                    // We have a string with leading blank(s)...

                    var j=0, i = s.length;

                    // Iterate from the far left of string until we
                    // don't have any more whitespace...
                    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
                        j++;


                    // Get the substring from the first non-whitespace
                    // character to the end of the string...
                    s = s.substring(j, i);
                }

                return s;
        }
function trim(str)
        /***
                PURPOSE: Remove trailing and leading blanks from our string.
                IN: str - the string we want to Trim

                RETVAL: A Trimmed string!
        ***/
        {
                return RTrim(LTrim(str));
        }


