/*
		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
*/
var incarnation = 0;
var error_functions_array = [];																// create new array to hold functions that can be called upon validatioin failure (as set in the fields validaiton registration)
function get_value(this_element_obj,this_form_obj)
{	this_field_type = this_element_obj.type;												// establish the type of this field
	//alert(this_field_type);
	if(this_field_type == 'select-one')														// if the emelemnt is of type select box (single)
	{	if(this_element_obj.options.length > 0)												// if there are some options in the select list
		{	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 there are no options in the select list
		{	this_field_value = '';															// set the value to nothing which will trigger validation where it is mandatory
		}
		//alert('select one'+this_element_obj.name);
	}
	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)
		//alert('select one'+this_element_obj.name);
		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(this_field_type == 'checkbox')														// 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 checkboxes
		if(this_field_count == 1|typeof this_field_count == 'undefined')					// if there is only one checkboxes option
		{	if(this_element_obj.checked)													// 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 clear_error_state(obj,label_class_name_used,control_class_name_used)
{	// If the class name we were passed contains the 'error' class that we added to the objects
	// class remove it from the class names used for both the label and control fields
	
	
	//GOtemp||obj.id=='careerone_job_inventory_location_1_label'
//	if(obj.id=='careerone_job_inventory_location_1')
//	{	alert('BEFORE\n'+obj.id+'\ncontrol: ]'+control_class_name_used+'[');
//	}
	//ENDtemp
	
	
	// CHECK version of this file prior to 22.04.2011 for original RegEx filter
	var re = new RegExp('(^|\\s)error','g'); 											// Matches ' error ', ' error$', '^error ' (^ = Start of String | $ = End of String)
	label_class_name_used = label_class_name_used.replace(re,' ');
	control_class_name_used = control_class_name_used.replace(re,' ');
	
	
	//GOtemp
//	if(obj.id=='careerone_job_inventory_location_1')
//	{	alert('AFTER\n'+obj.id+'\ncontrol: ]'+control_class_name_used+'[');
//	}
	//ENDtemp
	
	
	if(obj.type != 'radio')																	// if not looking at a radio button
	{	obj.className = control_class_name_used;											// reset the class (incase previously in error)
		//alert('Set '+obj.id+'\nlabel: '+label_class_name_used+'\ncontrol: '+control_class_name_used);
	}
	else
	{	if(control_class_name_used.length > 0 && control_class_name_used != 'normal')
			obj.className = control_class_name_used;										// turn off error class and change it back to specified class class
		else
			obj.className = 'radio';														// turn off error class and change it back to radio class
		
	}
	if(obj.validate_labelID.length > 0)
	{	//alert(obj.validate_labelID);
		document.getElementById(obj.validate_labelID).className = label_class_name_used;
	}
	if(obj.validate_match_labelID.length > 0)
		document.getElementById(obj.validate_match_labelID).className = label_class_name_used;
	if(obj.validate_match_field.length > 0)
		document.getElementById(obj.validate_match_field).className = control_class_name_used;
}





function validate_form(this_form_obj)
{	var validation_passed = true;
	var validation_message = '';

	// Regular Expression Validation Patterns
	validation_match_array = new Array("email_check","file_name","no_html","numeric_strict","mobile_phone","currency_check","decimal_check","numeric_time24","numeric_time60","date","custom_regex");	// 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 = '';
	file_name = /^[ :\\a-z0-9_\-\&]+\.[a-z]{2,4}$/i;
	file_name_format = '\tA valid file format is any file name made up of alpha-numeric characters, spaces (though its best to avoid them) and underscores \'_\'.\n\tSome other characters like ampersands \'\&\' are also allowed but again it is best to avoid their use.\n\tThe file name must also only contain one period followed by a two, three or four charater extension which can only be made up of letters.\n\n\tDO NOT change the file name in the form field.\n\tIf you need to change the name of your file go to the file on your computer and physically change its name then reselect it for upload.';
	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 thousands separator.\n\n';
	mobile_phone = /^\+?[0-9 ]+$/;
	mobile_phone_format = '\tValid Mobile Phone format allows for an optional plus symbol followed by numbers with optional spaces.\n\n';
	numeric_time24 = /^([0-2][0-9]|[0-9])$/;
	numeric_time24_format = '\tValid numeric format allows for only whole numbers between 0 and 23. This also means no decimal places.\n\n';
	numeric_time60 = /^([0-5][0-9]|[0-9])$/;
	numeric_time60_format = '\tValid numeric format allows for only whole numbers between 0 and 59. This also means no decimal places.\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';
	date = /^(?:((31-(Jan|Mar|May|Jul|Aug|Oct|Dec))|((([0-2]\d)|30)-(Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))|(([01]\d|2[0-8])-Feb))|(29-Feb(?=-((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))-((1[6-9]|[2-9]\d)\d{2})$/;
	date_format = '\tPlease enter a valid Date in the format \'dd-mmm-yyyy\'\n\tE.g. \'29-Feb-2008\' or \'19-Oct-1986\'\n';
	
	/*	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_]{1,}[a-z0-9_\-\.]*)*	: Optional sub domains
		[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
		{	var this_field_obj = this_form_obj[i];																// localise the field we are interrogating
			var this_field_type = this_field_obj.type;															// establish the type of this field
			var this_field_value = get_value(this_field_obj,this_form_obj);										// call functin to get the value of the field
			var this_field_length = this_field_value.length;													// resolve the length of the fields value
			var this_field_label = document.getElementById(this_field_obj.validate_labelID);					// localise this fields label (if it has on)
			if (typeof this_field_label == "undefined" || this_field_label == null)								// if this fields label was not found (i.e. there is no label to validate for this form field)
				this_field_label = "";																			// set this fields label to an empty string
				
			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;
			
			// Declare then reset (if required) classes used for this element - This accommodates custom styles used in forms
			var label_class_name_used = this_field_label == "" ? "" : this_field_label.className;				// declare the class used in the non error state for the form label. If this form object does not have a label set the classname to nothing, otherwise use the current class this label currently has 
			var label_error_class_name_used = label_class_name_used + ' error';									// declare the class used in the error state for the form label
			var control_class_name_used = this_field_obj.className;												// declare the class used in the non error state for the form control
			var control_error_class_name_used = control_class_name_used + ' error';								// declare the class used in the error state for the form control
			
			// Custom label class type
			if(this_field_obj.validate_label_class_type != 'normal')											// if this form element does NOT use the normal class for its label
			{	label_class_name_used = this_field_obj.validate_label_class_type;								// reset the var to use the one passed in
				label_error_class_name_used = this_field_obj.validate_label_class_type + ' error';				// reset the label class for the error state by appending the '_error' suffix
			}
			
			// Custom control class type
			if(this_field_obj.validate_control_class_type != 'normal')
			{	control_class_name_used = this_field_obj.validate_control_class_type;	
				if(control_class_name_used.length > 0)
					control_error_class_name_used = control_class_name_used + ' error';
				else
					control_error_class_name_used = 'error';						
			}
			
			clear_error_state(this_field_obj,label_class_name_used,control_class_name_used);											// clear the error state for this field (and its label if any)
					
			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' && this_field_type != 'checkbox')															// do not change backgrounds of radio or checkboxes												// if the field is NOT a radio button
					this_field_obj.className = control_error_class_name_used;															// turn on error class
				if(this_field_label != "")
					this_field_label.className = label_error_class_name_used;
				if(this_field_obj.validate_show_hide_panel.length > 0)
					show_hide_switch(this_field_obj.validate_show_hide_panel,'show',false);
				if(this_field_obj.validate_failed_function.length > 0)																	// if registration on field includes a javascript function
					error_functions_array[error_functions_array.length] = [this_field_obj.validate_failed_function];					// add the function to the array of functions called when alert message run(below)
			}
			else if(this_field_obj.validate_mandatory == 'custom_pair_match' || this_field_obj.validate_mandatory == 'custom_pair_match_mandatory')						// if custom validation on matching field (if one entered the other must be and both the same data)
			{	
				match_field_obj = eval('this_form_obj.'+this_field_obj.validate_match_field);																			// localize 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_value != this_field_value || (this_field_obj.validate_mandatory == 'custom_pair_match_mandatory' && this_field_value.length == 0)) 		// if the fields do not have the same data OR actually request manadatory and either one has no data
				{	validation_passed = false;																															// fail validation
					validation_message = validation_message + '\n' + this_field_obj.validate_mandatory_message;															// display message
					this_field_obj.className = control_error_class_name_used;																							// turn on error class					
					match_field_obj.className = control_error_class_name_used;
					if(this_field_label != "")
						this_field_label.className = label_error_class_name_used;
					if(this_field_obj.validate_match_labelID.length > 0)
						document.getElementById(this_field_obj.validate_match_labelID).className = label_error_class_name_used;
					if(this_field_obj.validate_show_hide_panel.length > 0)
						show_hide_switch(this_field_obj.validate_show_hide_panel,'show',false);
					if(this_field_obj.validate_failed_function.length > 0)																								// if registration on field includes a javascript function
						error_functions_array[error_functions_array.length] = [this_field_obj.validate_failed_function];												// add the function to the array of functions alled when alert message run(below)
				}
			}
			
			// RUN OTHER VALIDATION - if no other errors yet
			//if(validation_passed == true)																												// 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
				{	if (validation_match_array[v] == 'custom_regex')																					// If we are performing a customer Regular Expression match
					{	this_re = new RegExp(this_field_obj.validate_data_regex);																		// Set the Regular Expression to the RegEx function passed in
					}
					else 
					{	this_re = eval(validation_match_array[v]);																						// define the re string to match for this validation option 																						
						this_eg = eval(validation_match_array[v] + '_format');																			// define the format message (if any)
					}
					
					if(this_field_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 (validation_match_array[v] != 'custom_regex') 																				// If this Validation match is not a custom regex show the format message if required. Custom Regex will pass in it's own Message if required.
						{	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
							else 
								validation_message = validation_message + '\n' + eval(validation_match_array[v] + '_format');
						}
						
						this_field_obj.className = control_error_class_name_used;																		// turn on error class
						if(this_field_label != "")
							this_field_label.className = label_error_class_name_used;
						if(this_field_obj.validate_show_hide_panel.length > 0)
							show_hide_switch(this_field_obj.validate_show_hide_panel,'show',false);
						if(this_field_obj.validate_failed_function.length > 0)																			// if registration on field includes a javascript function
							error_functions_array[error_functions_array.length] = [this_field_obj.validate_failed_function];							// add the function to the array of functions alled when alert message run(below)
					}
				//}		
				// ENTER OTHER VALIDATION TYPES HERE			
				}
		
		}
	}
	
	if(validation_passed == true)																			// if NO validation has failed
	{	return true;
		//this_form_obj.submit();																			// submit the form
		//incarnation = incarnation + 1;
		//return incarnation;
	}
	else																									// if any validation failed
	{	
		// FAILED VALIDATION FUNCTION CALLS - does checks to ensure same function only run once
		if(error_functions_array.length > 0)																// if any field registrations included a function call (array build during validation)
		{	var run_functions_array = [];																	// declare var for array of functions that have been run
			for(i=0;i<error_functions_array.length;i++)														// loop over all function calls
			{	this_fn_str = error_functions_array[i] + '';												// ensure the value is a string data type by adding nothing to it
				this_fn_has_been_run = false;																// declare flag indicating if this function has been run or not
				for(a=0;a<run_functions_array.length;a++)													// loop over all function calls that have already been run
				{	this_run_fn_str = run_functions_array[a] + '';											// ensure the value is a string data type by adding nothing to it
					//alert('Testing '+this_run_fn_str+' for '+this_fn_str);
					if(this_run_fn_str == this_fn_str)														// if this function requested is hte item on the already run array we are looking at
					{	this_fn_has_been_run = true;														// set flag to say it has already run
						break;																				// no need to continue the loop
					}
				}
				//alert(this_fn_str  +' - '+this_fn_has_been_run);
				if(this_fn_has_been_run == false)															// after looking at all functions in the 'already run' array and the one requested was not found
				{	run_functions_array[run_functions_array.length] = this_fn_str;							// ad the requested function to the already run array
					eval(this_fn_str)																		// Call the actual function
				}
			}
		}
		// END FAILD VALIDATION FUNCTIOIN CALLS
		
		alert('Alert:\n' + validation_message + '\n\n');													// alert and display individual message(s)
		if(typeof save_button_id != "undefined" && document.getElementById(save_button_id) != null && typeof reset_save_button_label != "undefined")	// if save button exists in the form and reset function available
			reset_save_button_label();
		return false;
	}
}
