// ##LOG: 
//	27/02/02	bkel		Added 'focusField' which makes cursor focus on the first invalid field after it has displayed the list of invalid fields to the user.
//	27/02/02	bkel		Added aditional validation to the email checking (checked for existance of characters that cannot exist in email addresses).
//	12/02/02	bsmith	Changed select box _required code to handle -multi/-one
//	16/01/02	bsmith	Added password to _Required checks

//	31/12/01	shane		Added pre/post formchecks.
//	20/12/01 	Lee 		Altered to return true or false so it can be used in an onsubmit event
//										This allows the form to still have validation when using a submit button
//										which means that the user can submit the form using the enter key.
//	08/10/01	bkel		Added email validation.
//	05/10/01	ben			Created.

// Suffixes used in ins_javascript_formcheck.cfm

// '_validEmail' 	used to define fields used for email addresses.
// '_required'  	used to check if  required fields are filled.

// check / submit the form
	function checkForm(theForm)
	{
		var fieldValid = false;
		var fieldElement
		var focusField = 'unfocused'
		
		// strAlert is updated if an invalid field is found, so it has to be in this page.
		var strAlert = 'You have not entered some required data, please check the following:\n\n'
		var strControl = strAlert;
		
		//control used to ensure external checks passed (pre and post form checks)
		var externalCheck = true;
		
		// use function preFormCheck() to do any extra checks before general form checks
		// function should return a boolean which when false voids extra checking and form submitting. 
		// Function must produce its own error messages		
		
		if(window.preFormCheck != null)
		{
			externalCheck = preFormCheck(theForm);
		}	
	
		
		
		if (externalCheck)
		{
		
			// loop over all elements of the form
			for(var i=0;i<theForm.elements.length;i++)
			{
			
				//  '_validEmail'
				// check if any fields need to contain a valid email address
				if(theForm.elements[i].name.indexOf('_validEmail') > 0)
				{
					fieldValid = false;
					
					// field name minus '_validEmail'
					var fieldName = theForm.elements[i].name.split('_validEmail')[0];
					var fieldElement = eval('document.' + theForm.name + '.' + fieldName);
					// check to see if a value has been entered
					if((fieldElement.value.indexOf("@") > 0 && fieldElement.value.indexOf(".") > 0 && fieldElement.value.indexOf(",") < 0 && fieldElement.value.indexOf("?") < 0 && fieldElement.value.indexOf(" ") < 0 && fieldElement.value.indexOf("/") < 0 && fieldElement.value.indexOf("\\") < 0 && fieldElement.value.indexOf("'") < 0 && fieldElement.value.indexOf('"') < 0) || fieldElement.value == '')	
					{
						// if is has field is valid
						fieldValid = true;
					}
					
					// if field is not valid
					if(fieldValid == false)
					{
						// add the value of the required field to the alert
						strAlert = strAlert + theForm.elements[i].value + '\n';
					}								
				}
				
				
				//  '_number'
				// check if any fields need to contain a numerical value
				else if(theForm.elements[i].name.indexOf('_number') > 0)
				{
				
					fieldValid = false;
					
					// field name minus '_required'
					var fieldName = theForm.elements[i].name.split('_number')[0];
					var fieldElement = eval('document.' + theForm.name + '.' + fieldName);
					if((fieldElement.value * 0) == 0)
					{
						fieldValid = true;
					}
					
					// if field is not valid
					if(fieldValid == false)
					{
						// add the value of the required field to the alert
						strAlert = strAlert + theForm.elements[i].value + '\n';
					}								
				}
				
				
				//  '_required'
				// check if any fields need to contain some value
				else if(theForm.elements[i].name.indexOf('_required') > 0)
				{
	
					fieldValid = false;
					
					// field name minus '_required'
					var fieldName = theForm.elements[i].name.split('_required')[0];
					var fieldElement = eval('document.' + theForm.name + '.' + fieldName);
					
					
					/* field type so we can figure out what to do
						valid field types:
							text
							textarea
							checkbox
							radio
							select-one
							select-multi
					*/
	
	
					// if the field contains multiple instances, get the type of the first one
	
					var fieldType
	
					fieldType = fieldElement.type;
	
					if(fieldElement.length > 0 && !(fieldElement.selectedIndex))
					{
						var fieldType = fieldElement[0].type;
					}
					
					// if the field is a straight text input
					if(fieldType == 'text' || fieldType == 'textarea' || fieldType == 'password' || fieldType == 'file')
					{
						// check to see if a value has been entered
						if(fieldElement.value != '')
						{
							// if is has field is valid
							fieldValid = true;
						}
					}
					// if the field is a checkbox
					else if(fieldType == 'checkbox' || fieldType == 'radio')
					{
						for(var j=0;j<fieldElement.length;j++)
						{
							// make sure at least one is checked
							if(fieldElement[j].checked == true)
							{
								fieldValid = true;
							}
						}
					}
					// if the field is a select box of some type
					else if((fieldType == 'select-multi' && fieldElement.selectedIndex != -1) || (fieldType == 'select-one' && fieldElement.selectedIndex != 0))
					{
						fieldValid = true;
					}
	
	
					// if field is not valid
					if(fieldValid == false)
					{
						// add the value of the required field to the alert
						strAlert = strAlert + theForm.elements[i].value + '\n';	
					}
				}
				
				// if the strAlert is not equal to strControl and focusField has not yet been set to anything.				
				if(strAlert != strControl && focusField == 'unfocused')
				{
					focusField = fieldElement;
				}				
			}		
			
			// if any required fields arent checked by the above include, include code to do it here.
		
			// use function postFormCheck() to do any extra checks after general form checks
			// function should return a boolean which when false voids the form submitting. 
			// Function must produce its own error messages		
			
			if(window.postFormCheck != null)
			{
				externalCheck = postFormCheck(theForm);
			}	
			
			
			if (externalCheck)
			{						
				// if the strings are equal, all good, submit the form		
				if(strAlert == strControl)
				{
					return true;
				}
				else
				{					
					// tell the user whats wrong
					alert(strAlert);
					focusField.focus();
					return false;					
				}
			}
			else
			{
				// from postform check
				return false;
			}

		}
		else
		{
			// from preform check
			return false;
		}
	}	
	
