function validateUsername(theField){

	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	var checkStr = theField.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
	}
	if (!allValid)
	{
		//alert("Please enter only letter and numeric characters in the '"+theField.fieldname+"' field.");
		theField.value=0;
		return (false);
	}
	
	// require at least 5 characters in the password field
	if (theField.value.length < 6)
	{
		//alert("Please enter at least 6 characters in the '"+theField.fieldname+"' field.");
		return (false);
	}
	return true;

}

function validateUsername2(theField){

	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	var checkStr = theField.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
	}
	if (!allValid)
	{
		//alert("Please enter only letter and numeric characters in the '"+theField.fieldname+"' field.");
		//theField.value="";
		return (false);
	}
	
	// require at least 5 characters in the password field
	if (theField.value.length < 4)
	{
		//alert("Please enter at least 6 characters in the '"+theField.fieldname+"' field.");
		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 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);
}

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);
		}
	}
}

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 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,erow){
// carry out form validation
// displays errors
// returns true if everything is ok
	var msg;
	var empty_fields= "";
	var errors = "";
	var value = 0;
	var blah;
	var origPassword="";
	var origEmail="";

	var undef;
	if(undef==erow){
		erow="";
	}

	if (errs!=null){
	  errors = errors + errs;
	 }

	for (var i=0; i < f.length ; i++ ){
		var e = f.elements[i];
		if(e.fieldname!=undef){
			if ((e.type=="text") || (e.type=="password") || (e.type=="username2") || (e.type=="username") || (e.type=="textarea") || (e.type=="select-one") || (e.type=="radio") || (e.type=="password")){
				if(e.fieldname=="Password"){
					origPassword=e.value;
				}
				e.style.borderBottom="";

				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;
					e.style.borderBottom="1px solid #FF0000";
					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";
						e.style.borderBottom="1px solid #FF0000";
					}
				}

				if ((e.datatype == "username") && (!((value==null)||(value=="")||isBlank(value)))){
					if (!(validateUsername(e))){
						e.style.borderBottom="1px solid #FF0000";
						errors +=""+e.fieldname+" may only contain letters and numbers, and must be at least 6 characters long.\n";
					}
				}

				if ((e.datatype == "username2") && (!((value==null)||(value=="")||isBlank(value)))){
				if (!(validateUsername2(e)))
					errors +=" - "+e.fieldname+" may only contain letters and numbers, and must be 4 characters long.\n";
				}

				if(e.datatype == "password"){
				// check if both password fields are the same
					if (e.value != origPassword)
					{
						//alert(e.value + " " + origPassword);
						errors +="The confirmation password did not match the original password.\n";
						e.style.borderBottom="1px solid #FF0000";
					}
				}
				//alert("Fieldname "+e.fieldname+" Type: "+e.datatype+" Name:"+e.name+" Value: "+e.value);
				//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";
							e.style.borderBottom="1px solid #FF0000";
						}
						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";
							e.style.borderBottom="1px solid #FF0000";
						}
						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))){
							e.style.borderBottom="1px solid #FF0000";
							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
			if((e.type=="select-one") ){
				if(eval(e.required)){
					if (""==e.options[e.options.selectedIndex].value || "0"==e.options[e.options.selectedIndex].value || "Please Select"==e.options[e.options.selectedIndex].text || ""==e.options[e.options.selectedIndex].text || "0"==e.options[e.options.selectedIndex].text)
					{
						e.style.borderBottom="1px solid #FF0000";
						empty_fields += "\n.........." + e.fieldname;
					}
				}
			}
		}
	} //end for

	// display errors
	if (!empty_fields && !errors){
		if (f.customValidation){
			return(f.customValidation(f))
		}
		else
		{
			return true;
		}
	}

	msg  = "_______________________________________________\n\n";
	msg += "   This form is not finished. Please correct\n";
	msg += "   the errors and then re-submit the form.\n";
	msg += "_______________________________________________\n\n";
	msg = "";

	if (empty_fields){
		msg += "The following required fields are empty:" + empty_fields + "\n";
		if (errors) msg += "\n";
	}

	msg += errors;
	if(erow==""){
		alert(msg);
	}else{
		msg=msg.replace("\n","<BR>");
		msg=msg.replace(" ","&nbsp;");
		var strReplaceAll = msg;
		var intIndexOfMatch = strReplaceAll.indexOf( "\n" );
		while (intIndexOfMatch != -1){
			strReplaceAll = strReplaceAll.replace( "\n", "<BR>" )
			intIndexOfMatch = strReplaceAll.indexOf( "\n" );
		}
		intIndexOfMatch = strReplaceAll.indexOf( " " );
		while (intIndexOfMatch != -1){
			strReplaceAll = strReplaceAll.replace( " ", "&nbsp;" )
			intIndexOfMatch = strReplaceAll.indexOf( " " );
		}
		msg=strReplaceAll;
		document.getElementById(erow + "_row").style.display="";
		document.getElementById(erow + "_cell").innerHTML=msg;

	}
	return false;
}