//<script language=javascript>
function trimZeroes(theField) {
	var num;
	if (theField.value.length == 2 && theField.value.charAt(0) == "0"){
		num = theField.value.charAt(1);
		theField.value = num;
	}
}

function isValidEmail(theField) {
// checks for valid email address formatting
	var bad = " :;'"
	if(theField.value.indexOf("@", 0) == -1 || theField.value.indexOf(".", 0) == -1 ){
		 return false;
	}
	for(i=1; i<theField.value.length; i++){
		if(bad.indexOf(theField.value.charAt(i)) != -1 ){
			return false;
		}
	}
	return true;
}

function validateEmail(theField) {
//generates error message
	if (!isValidEmail(theField)){
		alert('The field "'+theField.fieldname+'" requires an email address \nin the form userid@company.com');
		return false;
	}
	return true;
}

function isValidURL(theField) {
// checks for valid URL formatting, requiring http://...
	var bad = " ;'@"
	if(theField.value.indexOf("http://", 0) == -1 || theField.value.indexOf(".", 0) == -1 ){
		 return false;
	}
	for(i=1; i<theField.value.length; i++){
		if(bad.indexOf(theField.value.charAt(i)) != -1 ){
			return false;
		}
	}
	return true;
}

function validateURL(theField) {
//generates error message
	if (!isValidURL(theField)){
		alert('The field "'+theField.fieldname+'" requires a URL \nin the form http://www.mycompany.com');
		theField.value = '';
		return false;
	}
	return true;
}

function isValidURL2(theField) {
// checks for valid URL formatting for relative links
	var bad = " ;'@"
	if(theField.value.indexOf("/", 0) == -1){
		 return false;
	}
	for(i=1; i<theField.value.length; i++){
		if(bad.indexOf(theField.value.charAt(i)) != -1 ){
			return false;
		}
	}
	return true;
}

function validateURL2(theField) {
//generates error message
	if (!isValidURL(theField)){
		alert('The field "'+theField.fieldname+'" requires a relative URL \nsuch as \'/folder/file.htm\'');
		theField.value = '';
		return false;
	}
	return true;
}

function validateNumeric(theField) {
// checks for valid number
	var tmp = fnCleanDecimal(theField.value);
	if (isNaN(parseFloat(tmp))){
		alert('The field "'+theField.fieldname+'" requires a number.');
		theField.value=0;
		return false
	}
	theField.value=tmp;
	return true;
}

function validateInteger(theField) {
// checks for valid number

	var tmp = fnCleanNumber(theField.value);
	if (isNaN(parseFloat(tmp))){
		alert('The field "'+theField.fieldname+'" requires an integer.');
		theField.value=0;
		return false
	}
	theField.value=tmp;
	return true;
}

function validateWholeNumber(theField){
	var tmp = (theField.value);
	var numbers = "0123456789";
	for(q=0; q<tmp.length; q++){
		if (numbers.indexOf(tmp.charAt(q)) == -1){
			alert('The field "'+theField.fieldname+'" requires a whole number.');
			theField.value=0;
			return false
		}
	}
	theField.value=tmp;
	return true;
}

function validatePhone(dataField){
// format US phone number
	var format = "(###) ###-####";
	var tmp = dataField.value;
	var tmp3 = fnCleanNumber(tmp)
	if(tmp3.length == 10){
		fnFormat(dataField, format);
	}
	else{
		alert("Please Enter Phone Number in This Format (###) ###-####");
		dataField.value = '';
	}
}

function isValidPhone(dataField){
// format US phone number
	var format = "(###) ###-####";
	var tmp = dataField.value;
	var tmp3 = fnCleanNumber(tmp)
	if(tmp3.length == 10){
		fnFormat(dataField, format);
		return true;
	}
	else{
		return false;
	}
}

function validateZipCode(dataField){
// format zip code
	var format = "#####-####";
	var tmp = new String( dataField.value );
	if(true){

		if ((tmp.length==9) && (tmp.indexOf("-")<0)){
		// check the zip if length=9
			for(i=0; i<tmp.length; i++){

				if( ( tmp.charAt(i) < "0" )||(  tmp.charAt(i) > "9" ) ){
					alert( "Please Enter the Zip Code in the format ##### or #####-####" );
					dataField.value = '';
					return false;
				}
			}
			// put in the dash
			fnFormat(dataField, format);
			return true;
		}
		else if((tmp.length == 10)&&( tmp.charAt(5) != "-" )){
		// check the zip if 10 long
			alert( "Please Enter the Zip Code in the format ##### or #####-####" );
			dataField.value = '';
			return false;
		}
		else if(tmp.length < 5 || tmp.length > 5 && tmp.length < 10 || tmp.length > 10){
		// check the zip if not 5 long, or 10 long
			alert( "Please Enter the Zip Code in the format ##### or #####-####" );
			dataField.value = '';
			return false;
		}

		for(i=0; i<tmp.length; i++){
			if( ((  tmp.charAt(i) < "0" )||(  tmp.charAt(i) > "9" )) && !(( tmp.charAt(i) == "-" )&&( i == 5 )) ){
				alert( "Please Enter the Zip Code in the format ##### or #####-####" );
				dataField.value = '';
				return false;
			}
		}
	}
}

function isValidZip(dataField){
// format zip code
	var format = "#####-####";
	var tmp = new String( dataField.value );
	if ((tmp.length==9) && (tmp.indexOf("-")<0)){
	// check the zip if length=9
		for(i=0; i<tmp.length; i++){
			if( ( tmp.charAt(i) < "0" )||(  tmp.charAt(i) > "9" ) ){
				return false;
			}
		}
		// put in the dash
		fnFormat(dataField, format);
		return true;
	}
	else if((tmp.length == 10)&&( tmp.charAt(5) != "-" )){
	// check the zip if 10 long
		return false;
	}
	else if(tmp.length < 5 || tmp.length > 5 && tmp.length < 10 || tmp.length > 10){
	// check the zip if not 5 long, or 10 long
		return false;
	}

	for(i=0; i<tmp.length; i++){
		if(((tmp.charAt(i) < "0") ||(tmp.charAt(i) > "9")) && !((tmp.charAt(i) == "-") && (i == 5))){
			return false;
		}
	}
	return true;
}

function fnFormat(string, format){
	var string2 = string.value;
	var tmp = fnCleanNumber(string2);
	var newstring = "";
	for (i=0, j=0; i<format.length; i++) {
		if (format.charAt(i) == "#") {
			newstring += tmp.charAt(j);
			j++;
		}
		else {
			newstring += format.charAt(i);
		}
		string.value = newstring;
	}
}

//clean integer
function fnCleanNumber(string) {
	var numbers = "0123456789";
	var newstring = "";
	for (i=0; i<string.length; i++)	{
		if (numbers.indexOf(string.charAt(i)) == -1) {
		}
		else {
			newstring += string.charAt(i); 
		} 
	}
	return (newstring);
}

//clean decimal
function fnCleanDecimal(string) {
	var numbers = "0123456789.";
	var newstring = "";
	for (i=0; i<string.length; i++)	{
		if (numbers.indexOf(string.charAt(i)) == -1) {
		}
		else {
			newstring += string.charAt(i); 
		} 
	}
	return (newstring);
}

function isBlank(s){
// utility function
// returns true if contains only whitespace characters
	if( s=="undefined" ) 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;
}

function validateForm( f, errs ){
// carry out form validation
// displays errors
// returns true if everything is ok

	var msg;
	var empty_fields= "";
	var errors = "";
	var value = 0;
	var blah;

	if (errs!=null){
	  errors = errors + errs;
	 }

	for (var i=0; i < f.length ; i++ ){
		var e = f.elements[i];
		if ((e.type=="text") || (e.type=="textarea") || (e.type=="select-one") || (e.type=="radio") || (e.type=="password")){
			value = e.value;

			//first check if the field is empty
			if (eval(e.required) && ((value==null)||(value=="")||isBlank(value)) && (!( e.type=="select-one"))){
				empty_fields += "\n         " + e.fieldname;
				continue;
			}

			//Check for email addresses
			if ((e.datatype == "email") && (!((value==null)||(value=="")||isBlank(value)))){
				if (!(isValidEmail(e)))
					errors +=" - Please enter a valid email address in "+e.fieldname+" (i.e., yourname@yourcompany.com).\n";
			}

			//Check for URL requiring http://
			if ((e.datatype == "URL") && (!((value==null)||(value=="")||isBlank(value)))){
				if (!(isValidURL(e)))
					errors +=" - Please enter a valid URL in "+e.fieldname+" (i.e., http://www.yourcompany.com).\n";
			}

			//Check for URL2: relative URLs
			if ((e.datatype == "URL2") && (!((value==null)||(value=="")||isBlank(value)))){
				if (!(isValidURL2(e)))
					errors +=" - Please enter a valid URL in "+e.fieldname+" (i.e., /folder/file.htm).\n";
			}

			//Check for phone number
			if ((e.datatype == "phone") && (!((value==null)||(value=="")||isBlank(value)))){
				if (!(isValidPhone(e)))
					errors +=" - Please enter a valid phone number in "+e.fieldname+".\n";
			}

			//Check for zipcode
			if ((e.datatype == "zipcode") && (!((value==null)||(value=="")||isBlank(value)))){
				if (!(isValidZip(e)))
					errors +=" - Please enter a valid zipcode in "+e.fieldname+" (i.e., 30332 or 30332-1234).\n";
			}

			//check for IP address
			if ((e.datatype=="ip")&&((e.min!=null)||(e.max!=null))){

				if ( ( e.type=="text")|| ( e.type=="textarea" ) ){
					value=parseFloat(e.value)
				}else if ( e.type=="select-one"){
					value=parseFloat(e.selectedIndex)
				}

				var v =  value;

				if (isNaN(v)||
					((e.min != null) && ( v < e.min)) ||
					((e.max != null) && ( v > e.max))){

					errors += " - The field " + e.fieldname + " must be an IP address in which each octet is between 0 and 255.\n";
				}
			}

			//Now check for fields that are numeric
			if (eval(e.required) || (!((value==null) || (value=="") || isBlank(value)))){

				//check for integer
				if ((e.datatype=="integer") && (!((e.type=="select-one") || (e.type=="radio")))){
					//check integer if min/max not set
					var q,tmp,prob;
					prob = '0';
					tmp=(e.value);
					var numbers = "0123456789";
					for(q=0; q<tmp.length; q++){
						if (numbers.indexOf(tmp.charAt(q)) == -1){
							prob='1';
						}
					}
					if (prob=='1'){
						errors += " - " + e.fieldname + " must be an integer.\n";
					}
					prob = '0';
				}
				//check for decimal value
				if ((e.datatype=="numeric") && (!((e.type=="select-one") || (e.type=="radio")))){
					//check numeric if min/max not set
					var q,tmp,prob;
					prob = '0';
					tmp=(e.value);
					var numbers = "0123456789.";
					for(q=0; q<tmp.length; q++){
						if (numbers.indexOf(tmp.charAt(q)) == -1){
							prob='1';
						}
					}
					if (prob=='1'){
						errors += " - " + e.fieldname + " must be numeric.\n";
					}
					prob = '0';
				}
				//check min/max
				if (((e.datatype=="numeric")||(e.datatype="integer"))&&((e.min!=null)||(e.max!=null))){

					if ( ( e.type=="text")|| ( e.type=="textarea" ) ){
						value=parseFloat(e.value)
					}
					else if ( e.type=="select-one"){
						value=parseFloat(e.selectedIndex)
					}

					var v =  value;

					if (isNaN(v)|| ((e.min != null) && ( v < e.min)) || ((e.max != null) && ( v > e.max))){
						errors += " - The field " + e.fieldname;

						if (!(e.type=="select-one")) {
							errors += " must be a number";
							if (e.min != null){
								errors += " that is greater than " + e.min;
							}
						}
						else if (e.type=="select-one"){
							if (e.min != null){
								errors += " must have a value selected";
							}
						}
						else if (e.max != null && e.min != null){
							errors += " and is less than " + e.max;
						}
						else if (e.max != null ){
							errors += " that is less than " + e.max;
						}
						errors += ".\n";
					}
				}
			}
		} //end if e.type
	} //end for

	// display errors
	if (!empty_fields && !errors){
		if (f.customValidation){
			return(f.customValidation(f))
		}
		else
		{
			return true;
		}
	}

	msg  = "_______________________________________________\n\n";
	msg += "   P L E A S E    N O T E:\n";
	msg += "   This form contains errors. Please correct\n";
	msg += "   the errors and then re-submit the form.\n";
	msg += "_______________________________________________\n\n";

	if (empty_fields){
		msg += " - The following required fields are empty:" + empty_fields + "\n";
		if (errors) msg += "\n";
	}

	msg += errors;
	alert(msg);
	return false;
//	return confirm(msg+"\nSave anyway?");
}

function onFormatField( e ){
	// format the data in the field
	var dataType = new String (e.datatype);
	var controlType = new String (e.type);

	if (e.maxsize!=null) {
		if (e.value.length>e.maxsize){
			alert("The value in "+e.fieldname+" is too long and will be truncated.");
			e.value=e.value.slice(0,e.maxsize-1)
		}
	}

	// if it is a text input....
	if ((controlType.toUpperCase()=="TEXT")&&(!isBlank(e.value))){

		if (dataType.toUpperCase()=="PHONE"){
			fnPhoneNo(e);
		}else if (dataType.toUpperCase()=="ZIPCODE"){
			fnZipCode(e);
		}else if (dataType.toUpperCase()=="STRING"){
			e.value = e.value;
		}else if (dataType.toUpperCase()=="DOMAINNAME"){
			e.value = e.value;
		}else if (dataType.toUpperCase()=="NUMERIC"){
			validateNumeric(e);
		}else if (dataType.toUpperCase()=="IP"){
			validateNumeric(e);
		}else if (dataType.toUpperCase()=="EMAIL"){
			validateEmail(e);
		}
	}
}
//</script>