//*********************************************
// function GenerateErrorList
//
// Checks for valid data on current form based
//  on array of fields (Big Deal!)
// 1 aRequiredFields - array of fields and various attributes assigned to it
//                     0) Actual object name on form
//                     1) Type of object (this can be determined from object itself
//                        but this process can also use a predtermined type to check
//                        for certain data in say a text box...)  
//                     2) Description of field
//                     3) Is this field mandatory? if not we just need to see if the
//                        data was valid. If it is blank, we ignore it.
// 2 psCategory - If the data on the form falls under a particular category.
//                (we may not need this - I don't see us needing it, but what the hell...)  
// 
// Note: Before this is run all text/ textarea fields should already be trimmed
// (see function TrimText - this is usually called on the page .js level)
//*********************************************
function GenerateErrorList (aRequiredFields, psCategory)
{	
	//*********************************************
	// Is the data in the Required Fields valid?
	//*********************************************
	var iRequiredFieldsValid = true;
	//*********************************************
	// Is the data in the current field we're looking at valid?
	//*********************************************
	var iFieldValueValid
	//*********************************************	
	// Is the date data in the current field we're looking at valid?
	//*********************************************
	var iDateValueValid	= true
	//*********************************************
	// Is the memo data in the current field we're looking at valid?
	//*********************************************
	var iMemoValueValid	= true
	//*********************************************
	// Array Element Number of first REQUIRED FIELD with an errror
	//*********************************************
	var iFirstErrorElementNo = -1
	//*********************************************
	// Error Message
	//*********************************************
	var sErrorMsg
	//*********************************************
	// Reference of Form Object being proccesed
	//*********************************************
	var oFormObject
	//*********************************************
	// Data Type of Form Object being proccesed
	//*********************************************
	var sDataType	
	//*********************************************
	// Description of Form Object being proccesed
	//*********************************************
	var sFieldDesc
	//*********************************************
	// Background colors for form objects
	//*********************************************
	var sGoodColor = "white"	
	var sBadColor = "#E8C140"	
	//*********************************************
	// Define Error heading based on whether we have a category or not
	//*********************************************
	if (psCategory == "")
		sErrorMsg = "The following field(s) were not filled out or were filled incorrectly:\n";
	else
		sErrorMsg = "The following field(s) for " + psCategory +  " were not filled out or were filled incorrectly:\n";
	//*********************************************
	//loop through array of passed in form objects	
	//*********************************************	
	for (var iCounter = 0; iCounter < aRequiredFields.length; iCounter++) 
	{
		iFieldValueValid = true;       
		iDateValueValid = true;
		//*********************************************
		//* break apart array elements
		//*********************************************		
		oFormObject = aRequiredFields[iCounter][0]
		sDataType = aRequiredFields[iCounter][1]
	    sFieldDesc = aRequiredFields[iCounter][2]
	    bMandatoryField = aRequiredFields[iCounter][3]
		
		//*********************************************
		//* reset form object background color to white (IE only)
		//*********************************************			    
		if (navigator.appName != "Netscape" && sDataType != "radio")
			oFormObject.style.backgroundColor = sGoodColor	
		//'**********************************
		//'* Check for data type
        //'**********************************
		switch (sDataType)
		{
			//'**********************************
			//'* AlphaNumeric
			//'**********************************			
			case "alphanum":
			{
				//'**********************************
				//'* if this is a mandatory field
				//'* data must exist
				//'**********************************
				if (oFormObject.value.length == 0)
				{
					if (bMandatoryField)
						iFieldValueValid = false;               
				}
				//'**********************************
				//'* needs to be alphanumeric				
				//'**********************************               
				else if (!(isAlphaNumeric(oFormObject.value)))					
					iFieldValueValid = false;	 	
			}	
			break;
			//'**********************************
			//'* Numeric type
			//'**********************************			
			case "numeric":
			{
				//'**********************************
				//'* if this is a mandatory field
				//'* data must exist
				//'**********************************
				if (oFormObject.value == '')
				{
					if (bMandatoryField)
						iFieldValueValid = false;               
				}
				//'**********************************
				//'* but at any rate, if it is filled in
				//'* the data does need to be numeric
				//'**********************************               
				else if (isNaN(oFormObject.value))					
					iFieldValueValid = false;	 	
			}	
			break;
			//'**********************************
			//'* Integer type
			//'**********************************			
			case "integer":
			{
				//'**********************************
				//'* if this is a mandatory field
				//'* data must exist
				//'**********************************
				if (oFormObject.value == '')
				{
					if (bMandatoryField)
						iFieldValueValid = false;               
				}
				//'**********************************
				//'* if the data's not numeric
				//'* or shows other behavior that's very
				//'* uninteger-like than, we may have a problem...
				//'**********************************               
				else if (isNaN(oFormObject.value))					
					iFieldValueValid = false;	 	
		   		
		   		else if (oFormObject.value.indexOf(".") > -1 )					
		  			iFieldValueValid = false;	 	
		  			 
				else if ((oFormObject.value) <=0 )					
		  	  		iFieldValueValid = false;	 	
			}	
			break;			
			//'**********************************
			// year type
			//'**********************************
			case "year":
			{
				if (oFormObject.value == '')
				{
					if (bMandatoryField)
						iFieldValueValid = false;
				}
				else 
				{
					var ValidYearFormat = /^\d{4}$/
						if (!(ValidYearFormat.test(oFormObject.value)))
							iFieldValueValid = false;
				}		
			}
			break;
			//'**********************************
			//  text (string) type
			//'**********************************
            case "text":     
			{
	            if (oFormObject.value == '')
					if (bMandatoryField)
						iFieldValueValid = false;
						
			}
			break;
			//'**********************************
			//  name type
			//'**********************************
            case "name":     
			{
				if (oFormObject.value == '')
				{
					if (bMandatoryField)
						iFieldValueValid = false;
				}
				else if (oFormObject.value.indexOf(" ") == -1 )					  
					iFieldValueValid = false;
			}
			break;
			//'**********************************
			//  email type
			//'**********************************
            case "email":     
			{
				if (oFormObject.value == '')
				{
					if (bMandatoryField)
						iFieldValueValid = false;
				}
				else 
				{
					var dotIndex = oFormObject.value.lastIndexOf(".")
					var atIndex = oFormObject.value.lastIndexOf("@") 
					if (!((atIndex > 0) && (dotIndex > atIndex)))
						iFieldValueValid = false;
                 }
			}
			break;
			//'**********************************
			// Social Security Number Type
			//'**********************************
			case "SSN":
			{
				if (oFormObject.value == '')
				{
					if (bMandatoryField)
						iFieldValueValid = false;
				}
				else 
				{
					var ValidSSNFormat = /^\d{3}-\d{2}-\d{4}$/
						if (!(ValidSSNFormat.test(oFormObject.value)))
							iFieldValueValid = false;
				}		
			}
			break;
			//'**********************************
			// Phone
			//'**********************************
			case "phone":
			{
				if (oFormObject.value == '')
				{
					if (bMandatoryField)
						iFieldValueValid = false;
				}
				else 
				{
					var ValidPhoneFormat = /^\d{3}-\d{3}-\d{4}$/
						if (!(ValidPhoneFormat.test(oFormObject.value)))
							iFieldValueValid = false;
				}		
			}
			break;
			//'**********************************
			//'* Zip
			//'**********************************
			case "zip":
			{
				if (oFormObject.value == '')
				{
					if (bMandatoryField)
						iFieldValueValid = false;
				}
				else 
				{
					var ValidZipFormat1 = /^\d{5}$/
					var ValidZipFormat2 = /^\d{5}-\d{4}$/
						if (!(ValidZipFormat1.test(oFormObject.value)) && !(ValidZipFormat2.test(oFormObject.value)))
							iFieldValueValid = false;
				}		
			}
			break;
			//****************
			// multilist
			//****************	           
			case "multilist":      
			{
			    if(oFormObject.selectedIndex == -1)
					if (bMandatoryField)
						iFieldValueValid = false;
			}
			break;
			//****************
			// heading list - make sure at least ONE element is in place
			//****************	           
			case "headinglist":      
			{
			    if(oFormObject.length == 0)
					if (bMandatoryField)
						iFieldValueValid = false;
			}
			break;

			//****************
			// for drop down list (assuming first line has content of "Select From Me")
			// if first line of drop-down data has real data,
			// change from 0 to -1
			//****************	           
			case "dropdown":      
			{
			    if (oFormObject.selectedIndex == 0)
				{
					if (bMandatoryField)
						iFieldValueValid = false;
				}		
			}
			break;
			//****************
			// for radio and check boxes
			//****************
			default:         
			{
				var iCollectionCount = oFormObject.length;
				iCollectionSelected = false;

				if (iCollectionCount != null)
				{
					for(var iCollectionCounter=0; iCollectionCounter < iCollectionCount; iCollectionCounter++)
					{
						if(oFormObject[iCollectionCounter].checked == true)
						{
							iCollectionSelected = true;
							iCollectionCounter = iCollectionCount;
						}
					}
				}
				else
				{
					if(oFormObject.checked == true)
						iCollectionSelected = true;
				}

				if(!iCollectionSelected)
				{
					if (bMandatoryField)
						iFieldValueValid = false;
				}		
			}
		}// end switch
				
		if (iMemoValueValid == false)
			break;
		
		if (iDateValueValid == false || iFieldValueValid == false)
		{
			sErrorMsg += sFieldDesc 
			switch (sDataType)
			{
				case "numeric" || "year":
				{
					sErrorMsg += " (must be " + sDataType + ")"
				}
				break;
				case "integer":
				{
					sErrorMsg += " (must be positive whole number)"
				}
				break;	
				case "name":
				{
					sErrorMsg += " (must include at least the first and last name)"
				}
				break;
				
				case "SSN":
				{
					sErrorMsg += " (must be in a ###-##-#### format)"
				}
				break;	
				case "phone":
				{
					sErrorMsg += " (must be in a ###-###-#### format)"
				}
				break;	
				case "date":
				{
					sErrorMsg += " (must be in a mm/dd/yyyy format)"
				}
				break;	
			}	
			sErrorMsg += "\r";
			iRequiredFieldsValid = false;
            if (iFirstErrorElementNo == -1)
				iFirstErrorElementNo = iCounter
		}              
	} // end for	
	//*********************************************
	//* Do we have invalid data?
	//*********************************************			        
    if((iRequiredFieldsValid == false) || (iDateValueValid == false) && (iMemoValueValid !=false))
    {
		//*********************************************
		//*set 1st invalid form object background color to light blue (IE only)
		//*********************************************			    
		if (navigator.appName != "Netscape" && sDataType != "radio")
			aRequiredFields[iFirstErrorElementNo][0].style.backgroundColor = sBadColor
		//*********************************************
		//* remove last carriage return from error message
		//*********************************************			    
		sErrorMsg = sErrorMsg.substr(0, sErrorMsg.length-1);
		//*********************************************
		//* Show error message
		//*********************************************			    
        alert(sErrorMsg);          
		//*********************************************
		//* Focus on 1st invalid form object 
		//*********************************************			            
        if (!(aRequiredFields[iFirstErrorElementNo][1] == "radio" || aRequiredFields[iFirstErrorElementNo][1] == "checkboxcollection"))
			aRequiredFields[iFirstErrorElementNo][0].focus()    

		return false;
	}
	else
		return true;
}
