// Form validation Script
//	To use:
//			1) In your html have a script tag with src="validform.js">
//			2) In your form tags, include the following specifications
//				required="true" 	-- set if you want it to be required
//				dn="First Name"		-- set to the name of the field (this is what displays in the warning box)
//				number="true"		-- set if you want it to be a number
//				min="0"				-- set to the minimum value or the min. # of chars you want it to be
//				max="999"			-- set to the maximum value or the max # of chars you want it to be				
//			3) In your form tag, include an onsubmit action that returns verify(obj).  
//				Ex.
//					<form onsubmit="return verify(this);">


//Function used to check if a submitted item is empty
function isblank(s){
	for(var x=0; x < s.length; x++){
		var c = s.charAt(x);
		if((c != ' ') && (c != '\n') && (c != '')) 
			return false;
	}
	return true;
}

function IsNumeric(sText){
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}
		
//Main form validation function		
function verify(f) {
	var msg;
	var empty_fields = "";
	var errors = "";
	
	//Loop through all the form elements and check if they are verifiable
	for(var i=0; i < f.length; i++){
		var e = f.elements[i];
		
		var required = e.getAttribute("required");
		var displayname = e.getAttribute("dn");
		var number = e.getAttribute("number");
		var emin = e.getAttribute("min");
		var emax = e.getAttribute("max");
						
		//check to make sure its a verifiable type
		if(((e.type =="text") || (e.type =="textarea") || (e.type == "password")) && required){	

			//Check if the field was left empty
			if((e.value == null) || (e.value == "") || isblank(e.value)){
				empty_fields +="\n           " + displayname;
				continue;
			}

			//Check if its only requisite is that it is a number			
			if(number && !emin && !emax){
				if (!(IsNumeric(e.value)))
					errors += "- The field " + displayname + " must be a number.\n";					
			}					

			//Check to make sure numeric fields are in the correct range
			else if(number && (emin || emax)){
				var v = parseFloat(e.value);
				if (!(IsNumeric(e.value)) || 
					(emin && (v < emin)) ||
 					(emax && (v > emax))){
					errors += "- The field " + displayname + " must be a number";
					if (emin)
						errors += " that is at least " + emin;
					if (emax && emin)
						errors += " and at most " + emax;
					else if (emax)
						errors += " that is at most" + emax;
					errors += ".\n";
				}
			}

			//Check to make sure string fields are long enough
			else if (emin || emax){				
				if (emin && (e.value.length < emin)){
					errors += "- The field " + displayname + " must be at least " + emin + " characters long.\n";
				}else if ((emax !=null) && (e.value.length > emax)){
					errors += "- The field " + displayname + " must be at most " + emax + " characters long.\n";
				}
			}	
		}else if ((e.type =="select-one") && required){
			//Check if the field was left empty
			if((e.value == null) || (e.value == "") || isblank(e.value)){
				empty_fields +="\n           " + displayname;
				continue;
			}
		}
	}
	
	//Display any errors
	if(!empty_fields && !errors) return true;
	
	msg  = "_________________________________________________\n\n";
	msg += "The form was not submitted because of the following error(s).\n";
	msg += "_________________________________________________\n\n";
	
	if(empty_fields){
		msg += "- The following required field(s) are empty:" + empty_fields + "\n";
		if(errors) msg += "\n";
	}

	msg += errors;
	alert(msg);
	return false;
}
					
					