/*
		template-name.cfm
		JobOffice 		-Section-or-area-this-template-is-for-
			
		Author:			Rawdyn Nutting
		Date:			-DD-MM-.2004
			
		USE:			-How-is-this-template-accessed-by-the-application-eg-included-on-job_card_cfm-etc-
		TASK:			-Basic-task-this-template-performs-
		OUTPUT:			-Any-output-this-template-produces-
		PROCESS:		-Any-specialnotes_relating-to-this-template
		DOCUMENTS		-Any-documentation-on-this-template-or-its-functionality
*/

function get_value(this_element_obj,this_form_obj)
{	this_field_type = this_element_obj.type;												// establish the type of this field
	if(this_field_type == 'select-one')														// if the emelemnt is of type select box (single)
		this_field_value = eval('this_element_obj.options['+this_element_obj.selectedIndex+'].value');	// define the lenght of the field that triggers the validation if anything selected
	else if(this_field_type == 'select-multiple')														// if the emelemnt is of type select box (single)
	{	this_field_value = '';																			// default the field value as it may be a list
		the_comma = '';																					// default the comma to nothing (rest after first item added to list)
		for(m=0;m<this_element_obj.options.length;m++)													// loop over the options in the selectbox
		{	if(this_element_obj.options[m].selected == true)											// if this istem is selected
			{	this_field_value = this_field_value + the_comma + this_element_obj.options[m].value;	// add the value of the item to the list
				the_comma = ',';																		// make sure the comma exists for further list items
			}
		}	
	}
	else if(this_field_type == 'radio')														// if the emelemnt is of type radio
	{	// NOTE: ONLY CALL THE VALIDATION ON ANY ONE RADIO OF THE RADIO GROUP
		this_field_count = this_form_obj[this_element_obj.name].length;						// establish the number of radio buttons
		
		if(this_field_count == 1)															// if there is only one radio option
		{	if(this_element_obj.selected)													// if the element is selected
				this_field_value = this_element_obj.value;									// pass the value back for walidation
			else																			// if the only item is not selected
				this_field_value = '';														// pass back nothing to trigger validation failed
		}
		else if(this_field_count > 1)														// if more than one radio option on form
		{	this_field_value = '';															// default the result to nothing (which will trigger validation)
			for (count = 0; count < this_field_count; count++)								// loop for the number of radio buttons existing
			{	//alert('43:'+this_form_obj[this_element_obj.name][count].checked);
				if (this_form_obj[this_element_obj.name][count].checked)					// if this radio is the checked one
				{	this_field_value = this_form_obj[this_element_obj.name][count].value;	// record the value as the result fo rthis radio button
					break; 																	// break the loop
				}
			}
		}
		else																				// if field count is 0 (not possible really)
			this_field_value = '';															// pass back nothing selected
	}	
	else																					// if the element is of type text (any text type)
	{	this_field_value = this_element_obj.value;											// define the lenght of the field that triggers the validation if anything entered
	}
	return 	this_field_value;
}

function validate_form(this_form_obj)
{	validation_passed = true;
	validation_message = '';
	
	// Regular Expression Validation Patterns
	validation_match_array = new Array("email_check","no_html","numeric_strict","currency_check","decimal_check","decimal_check_neg");	// register of all data match options
	email_check = /^[a-z0-9_\-\.]+@([a-z0-9_]{1,}[a-z0-9_\-\.]*)*[a-z0-9_\-]{2,}\.[a-z]{2,3}((\.[a-z][a-z])?)$/i;
	email_check_format = '';
	no_html = /^[^\<\>]+$/;
	no_html_format = '';
	numeric_strict = /^[0-9]+$/;
	numeric_strict_format = '\tValid numeric format allows for only whole numbers. This means no decimal places or thousans separator.\n\n';
	currency_check = /^\$?([0-9]{1,3}\,[0-9]{3}]*\.[0-9]{2}|[0-9]+\.[0-9]{2})$/;
	currency_check_format = '\tValid currency format is;\n\tDollar sign ($)\t\tOptional\n\tThousands separator (,)\tOptional\n\tDemial Places (.00)\t\tRequired (two places)\n\n';
	decimal_check = /^[0-9]+\.[0-9]{2}$/;
	decimal_check_format = '\tValid decimal format is;\n\tDemial Places (.00)\t\tRequired (two places)\n\n';
	decimal_check_neg = /^[-]{0,1}[0-9]+\.[0-9]{2}$/;
	decimal_check_neg_format = '\tValid decimal format is;\n\tDemial Places (.00)\t\tRequired (two places)\n\n Optional (-) in front for negative.';
	
	/*		EXAMPLES
			Currency-----------------------------------------------------------------
			/^						start of string
			\$?						Option dollar sign
			(						start the 'or' group
				[0-9]{1,3}			number characters one, two or three times
				\,[0-9]{3}]*		comma then three numbers - none or more times
				\.					decimal point
				[0-9]{2}			two digits to the right of the deciaml point
			|						OR
				[0-9]+				number characters one or more times
				\.					decimal point
				[0-9]{2}			two digits to the right of the deciaml point
			)						end the 'or' group
			$/;						end of string
	
			Email--------------------------------------------------------------------
			^							:begining of string
			[a-z0-9_\-\.]+				:Characters from a-z etc one or more times
			@							:'@' Symbol
			[a-z0-9_\-]{2,}				:Two or more allowed domain characters
			\.							:'.' Symbol
			[a-z]{2,3}					:2 or 3 characters for 1st level domain
			((\.[a-z][a-z])?)			:optional (?) second level domain eg- '.au'
			$							:end of string
			i							:makes expression case insensitive	
	*/
	
	
	for(i=0;i<this_form_obj.length;i++)																// loop over all of the form elements
	{	if(this_form_obj[i].validate == '1')														// if this form element requires validation
		{	//alert(this_form_obj[i].name);
			this_field_obj = this_form_obj[i];														// localise the field we are interrogating
			this_field_type = this_field_obj.type;													// establish the type of this field
			this_field_value = get_value(this_field_obj,this_form_obj);								// call functin to get the value of the field
			this_field_length = this_field_value.length;											// resolve the length of the fields value
			
			if(typeof this_field_obj.validate_minimum_length != "undefined")
			{	var this_field_minimum_length = this_field_obj.validate_minimum_length;
			}
			else
			{	var this_field_minimum_length = 0;
			}
			
			if(this_field_type != 'radio')															// if not looking at a radio button
				this_field_obj.className = '';														// reset the class (incase previously in error)
			else
				this_field_obj.className = 'radio';														// turn on error class
			if(this_field_obj.validate_labelID.length > 0)
				document.getElementById(this_field_obj.validate_labelID).className = '';
			//alert(this_field_type);
			//alert(this_field_value);
			
			if(this_field_obj.validate_mandatory == '1' && (this_field_length == 0 || this_field_length < this_field_minimum_length))							// if validation requested and no data exists yet
			{	validation_passed = false;																	// fail validation
				validation_message = validation_message + '\n' + this_field_obj.validate_mandatory_message;	// display message
				if(this_field_type != 'radio')																// if the field is NOT a radio button
					this_field_obj.className = 'error';														// turn on error class
				if(this_field_obj.validate_labelID.length > 0)
					document.getElementById(this_field_obj.validate_labelID).className = 'form_label_error';
			}
			else if(this_field_obj.validate_mandatory == 'custom_match')							// if custom validation on matching field (if one entered the other must be)
			{	match_field_obj = eval('this_form_obj.'+this_field_obj.validate_match_field);		// locaise the field we are matching to
				match_field_type = match_field_obj.type;											// resolve the match field type to establish if it has data
				match_field_value = get_value(match_field_obj);	
				match_field_length = this_field_value.length;				
				if(match_field_length > 0 && this_field_length == 0)											// if the match field has data and this field does not
				{	validation_passed = false;																	// fail validation
					validation_message = validation_message + '\n' + this_field_obj.validate_mandatory_message;	// display message
					this_field_obj.className = 'error';															// turn on error class					
					if(this_field_obj.validate_labelID.length > 0)
						document.getElementById(this_field_obj.validate_labelID).className = 'form_label_error';
				}
			}
			else																								// if all mandatory tests passed, do all other tests
			{	//alert(this_field_obj.name);
				for(v=0;v<validation_match_array.length;v++)													// loop over data validation options
				{	this_re = eval(validation_match_array[v]);													// define the re string to match fo rhtis validatio option
					this_eg = eval(validation_match_array[v]+'_format');										// define the format message (if any)
					if(this_field_value.length > 0 && this_field_obj.validate_data == validation_match_array[v] && !this_re.test(this_field_value))// if validate data matches this data validation option and does not pass the regular expression for said option
					{	validation_passed = false;																// fail validation
						validation_message = validation_message + '\n' + this_field_obj.validate_data_message;	// display message												
						if(this_field_obj.validate_data_format_example && this_eg.length>0)						// if data message calls for format to be displayed (and it exists)
							validation_message = validation_message + '\n' + this_eg;							// add the format to the alert
						this_field_obj.className = 'error';														// turn on error class
						if(this_field_obj.validate_labelID.length > 0)
							document.getElementById(this_field_obj.validate_labelID).className = 'form_label_error';
					}
				}		
				// ENTER OTHER VALIDATION TYPES HERE			
			}
		}
		
	}
	if(validation_passed == true)																	// if NO validation has failed
		this_form_obj.submit();																		// submit the form
		//alert(this_form_obj.action);
	else																							// if any validation failed
		alert('Alert:\n' + validation_message + '\n\n');											// alert and display individual message(s)
}

