///////////////////////////////////////////////////////////////
///	This code was last modified on 6/15/2006 at 11:45am ///
/// by David Coleman of the ITS Department                  ///
/// at Elizabethtown College, Elizabethtown, PA 17022       ///
/// Email: colemand@etown.edu                               ///
///////////////////////////////////////////////////////////////
function checkform(formobj)
{	
//*************Set up dialog messages in case of errors**********************
	//Message if a required field is left blank
	var alertRequiredMsg = "Please complete the following fields:\n";
	//Message if an incorrect value is found in a required field
	var alertFilterMsg = "There was an incorrect value in the following fields:\n";
	//Message if an incorred value is placed in the required or filter tags
	var alertProgramMsg = "Please review your filters in the following fields in the HTML:\n";
	//   Establishes the length of the error messages
	//   this is later used to see if any errors were found and
	//   added to the error message
	var R_Msg = alertRequiredMsg.length;
	var F_Msg = alertFilterMsg.length;
	var P_Msg = alertProgramMsg.length;
//***************************************************************************

	for (var i = 0; i < formobj.elements.length; i++){
		
		//Parsing the attributes in the filter tag
		if(formobj.elements[i].getAttribute('filter')==undefined){
			//  Initializes the fields to not be required or filtered in any manner if the
			//  HTML does not specify these directly.
			fil_bolRequired = false;
			fil_Type = "none";
		}else{
			fullAttributeString = formobj.elements[i].getAttribute('filter');
			//Split the filters - only two exist - into separate sub-strings by the semicolon
			var temp = new Array();
			temp = fullAttributeString.split(";");
			//Get the value for each attribute by spliting again on the colon.
			//  This should be the required filter....true or false (strings)
			
			//Check to see which field is being given first
			// If the ; is located 9 characters in then the first word is REQUIRED
			// If it is not then the first word is TYPE
			if(temp[0].indexOf(':')==8){bol_index=0; typ_index=1;
			}else if(temp[0].indexOf(':')==4){bol_index=1; typ_index=0;
			}else { alertProgramMsg += "Illegal ";}
			
			var len = temp[bol_index].length;
			fil_bolRequiredValue = temp[bol_index].substring(temp[bol_index].indexOf(':')+1,len);
			//Convert to a Boolean literal
			if (fil_bolRequiredValue=="true") fil_bolRequired = true;
				else fil_bolRequired = false;
			//  This should be the type filter....filter by alpha,numeric,none
			//  Ignore fields that a filter of this type is not applicable...(checkboxes, radio buttons, etc)
			
			if (formobj.elements[i].type=="text" || formobj.elements[i].type=="textarea"){
				len = temp[typ_index].length;
				fil_Type = temp[typ_index].substring(temp[typ_index].indexOf(':')+1,len);
				if (fil_Type!="numeric" && fil_Type!="alpha" && fil_Type!="none") {
					if(formobj.elements[i].id=="") formobj.elements[i].id=formobj.elements[i].name;
					alertProgramMsg += " - " + formobj.elements[i].id + "\n";
					alertProgramMsg += "   ** " + fil_Type + " is not valid.\n" ;
					fil_Type="none";
				}
			}else {fil_Type="none";}		
		}
		var obj = formobj.elements[i];
		
		if (obj){
			//Once the type is determined check the following:
			//   Is the field required?
			//		  If the field is required - Check the value
			//		  Else - Check the filter
			//			  If filter exists - use the checkfilter(obj.filter) 
			//				function to check the filter
			//			  Else
			//				Goto next field
			if(obj.id==""){ obj.id=obj.name;}
			//alert("ID= " + obj.id + "\nName= " +obj.name + "\nType= " +obj.type + "\nValue= " +obj.value+ "\nRequired? " + fil_bolRequired);
			
			switch(obj.type){ 
				case "select-one":
					
					if (fil_bolRequired && obj.selectedIndex == 0){
						alertRequiredMsg += " - " + obj.id + "\n";
					}
					break;
				case "select-multiple":
					if (fil_bolRequired && obj.selectedIndex == -1){
						alertRequiredMsg += " - " + obj.id + "\n";
					}
					break;
				case "checkbox":
				case "radio":	
					var blnchecked = false;
					var first_item = obj.name;
					var group_id = obj.id;
					var num_in_group=0;
					if(fil_bolRequired){
						var ckbox_Array = new Array(formobj.elements.length-i);
						while (obj.name==first_item){
							ckbox_Array[num_in_group]=obj;
							i++;
							num_in_group++;
							obj = formobj.elements[i];
						}
						for(var k=0; k<num_in_group;k++){
							if (ckbox_Array[k].checked)	blnchecked=true;}
						if(!blnchecked)	alertRequiredMsg += " - " + group_id + "\n";
						i--;
						obj = formobj.elements[i];
					}
				break;
				case "textarea":
				case "text":
					if (fil_bolRequired && obj.value == ""){alertRequiredMsg += " - " + obj.id + "\n"; break;}
					if(fil_Type!="none"){
						var a = obj.value.toUpperCase();
						var txtLimits = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ,.!:'";
						var numLimits = "0123456789.,";
						var blnErrFound=false;
						switch(fil_Type){
							case "alpha":
								if(a.length==0 && !fil_bolRequired) break;
								var k=0;
								while(k<a.length){
									var blnMatch = false;
									var j=0;
									while (j<txtLimits.length && !blnMatch){
										if(a.charAt(k)==txtLimits.charAt(j)) blnMatch=true;
										else j++;
									}
									if(blnMatch)	k++;
									else break;
								}
								
								if(!blnMatch) alertFilterMsg += " - " + obj.id + "\n";
								break;
							case "numeric":
								if(a.length==0 && !fil_bolRequired) break;
								var k=0;
								while(k<a.length){
									var blnMatch = false;
									var j=0;
									while (j<numLimits.length && !blnMatch){
										if(a.charAt(k)==numLimits.charAt(j)) blnMatch=true;
										else j++;
									}
									if(blnMatch)	k++;
									else break;
								}
								if(!blnMatch) alertFilterMsg += " - " + obj.id + "\n";
								break;
							default:
							}
					}
					break;
				default:
					if (fil_Type!="none"){
						alertProgramMsg += " - " + formobj.elements[i].id + "\n";
						alertProgramMsg += "   ** " + fil_Type + " is not valid.\n" ;
					}
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++) {
					if (obj[j].checked){
						blnchecked = true;
					}
					if (fil_bolRequired && !blnchecked){
						alertRequiredMsg += " - " + obj.id + "\n";
					}
				}
			}
		}
	}
//Display any error messages
	var err=0;
	if (alertRequiredMsg.length != R_Msg){alert(alertRequiredMsg);err=1;}
	if(alertFilterMsg.length != F_Msg){alert(alertFilterMsg);err=1;}
	if(alertProgramMsg.length != P_Msg){alert(alertProgramMsg);err=1;}
	if(err==0){ 
	  if(confirm('Thank you for filling out the form.  Click  Yes or OK to continue.'))
		return true;
	  else return false;}
        else return false;
}