

// PUBLIC FORM HANDLER




/*


CONFIG
the config is an array parsed into the html (head or body), with the following structure:
public_form_config_%bid%[n] = ['data_key','data_value','field_label','field_id','field_type','field_required','validation_type','display_field','display_required','display_label','field_style','review_value'];



KEY
0   'data_key' - the data key, used as key for sending information to server (like: "first_name")
1   'data_value' - the data value, used for sending back to server, if a value is provided, the field will be displayed with it (like: "Harry")


    'value_type' - controls how the received value is treated, accepts 'real' or 'hint' (a hint value will be displayed in the field, then disappear when the field is clicked, while a real value will be editable)

6   'value_validate' - the type of validation used to verify the entered data (like: "email")
8   'value_validate_assist' - triggers associated typing assistant if there is one, accepts 1

5   'value_required' - makes correct input obligatory to complete the form, accepts 1 (like: a required checkbox must be checked to let the field validate)

11  'value_review' - this will show the input value again upon succesfully completing the form, accepts 1
13  'value_review_mask' - needs the tag #value# and is wrapped around the value for display in the completed part, (like: '#value#<br>' this is the default when nothing is specified)

4   'input_type' - the html field type (like: "checkbox")
3   'input_locked' - sets input field to read only, accepts 1


9   'input_class' - passes a string to the class attribute of the input, note that this can be a multiclass (like: "plain_text blue_background")
10  'input_style' - passes a string to the style attribute of the input tag (like: "width: 100px; font-size-: 14px;")


2   'label_content' - the form field label, a description of the field as provided by the server (like: "First name:")
12  'label_required_mask' - needs the tag #label# and is wrapped around the label, to alter the appearance of the label of required items
    'label_rejected_mask' - needs the tag #label# and is wrapped around the label, to alter the appearance of the label of rejected items (like: make the text red)



7   'field_mask' - needs the tags #input# and #label#, omitting these tags in the mask code results in the label and/or input being hidden, not defining a field_mask results in a default mask '#label# #input#<br />' being used.



NOTES


FIELD TYPES
text - a text box, can receive and return text
checkbox - a checkbox, can be checked or unchecked, can receive and return either 1 or 0


VALIDATION TYPES
note: some validation types have an associated typing-assistant that is active in the input field, the typing assistant helps prevent faulty input while the user is typing.
email - checks for entry of a valid email address
date - checks for entry of a valid date in the form of dd-mm-yyyy


DATA KEYS
the following keys in the form config have special meaning for the script:
- public_form_bid -> a unique string parsed into the config and html, to let the handler find the right parts
- public_form_btf -> the name of the file to send results to


HTML COMPONENTS
part id's (the html parts that get visually swapped while using the form, like 'starting info', 'user activity', 'ending info')
note: if public_form_part_passive or public_form_part_completed is missing (not in the document) they will automatically be skipped
note: if public_form_part_completed is missing, public_form_message_success in the active part will replace it

public_form_part_passive
public_form_part_active
public_form_part_completed

message id's (html blocks in the active part that get displayed to provide feedback while the user is working on the form)
public_form_message_default
public_form_message_rejected
public_form_message_failure
public_form_message_excess
public_form_message_success

data id's (the user entered data)
public_form_data_readout_area

field id's (span where the fields appear)
public_form_fields_area

debug id's (span where the debug info appears)
public_form_debug_console

*/


function tvgs_typing_assistant(field_element,correction_type)
{	
	
	// validation types that are not known to this function will simply be ignored
	
	
	// date
	// allows typing of numbers and hyphen, max 10 chars
	if (correction_type == 'date')
	{
		
		// truncate
		if (field_element.value.length > 10)
		{
																																			
		  field_element.value = field_element.value.substring(0,10);
			
		}
		
		// replace
		if (field_element.value.match(/[^-0-9]+/) != null || field_element.value.match(/-{2,}/) != null)
		{
																																			
		  field_element.value = field_element.value.replace(/[^-0-9]+/g, '-').replace(/-{2,}/g, '-');
			
		}

	}	
	
	
	// email
	// allows typing of alphanumeric, hyphens, underscores, dots and at
	if (correction_type == 'email')
	{
		
	
		
		// replace
		while (field_element.value.match(/[^-._@0-9a-zA-Z]+/) != null)
		{
																																			
		  field_element.value = field_element.value.replace(/[^-._@0-9a-zA-Z]+/g, '');
			
		}
		
		/*
		// validate
		if(/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z]{2,6})+$/.test(input))
		{         

 		//alert('good');
		 return true;
		
		}
		*/

	}
	
	
	// text-tags
	// allows typing of alphanumeric, hyphens, underscores, dots and at
	if (correction_type == 'text_tags')
	{
		
	
			
		// replace <> tags
		while (field_element.value.match(/<.+?>/) != null)
		{
																																			
		  field_element.value = field_element.value.replace(/<.+?>/g, '');
			
		}
		
		
		// replace [] tags
		while (field_element.value.match(/\[.+?\]/) != null)
		{
																																			
		  field_element.value = field_element.value.replace(/\[.+?\]/g, '');
			
		}
		
			
		
	
		
		/*
		// validate
		if(/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z]{2,6})+$/.test(input))
		{         

 		//alert('good');
		 return true;
		
		}
		*/

	}	


	/*
	if(/^[0-3]?[0-9] *[-\/] *[0-1]?[0-9] *[-\/] *[1-2][0-9]{3}$/.test(input))
	{         

		//alert('good');
		return true;
	
	}
	else
	{
		
		//alert('bad');
		return false;   
	
	}
	*/
}



function checkEnter(e)
{ //e is event object passed from function invocation
	
	
	var characterCode; //literal character code will be stored in this variable
	
	if(e && e.which)
	{ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else
	{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}
	
	
	
	if(characterCode == 13)
	{ //if generated character code is equal to ascii 13 (if enter key)
		//document.forms[0].submit() //submit the form
		return true;
	}
	else
	{
		return false;
	}

}




function tvgs_public_form_handler(public_form_config)
{
	
	// get config values
	var config = eval(public_form_config);
	
	
	if (document.getElementById('sm_js_console')) document.getElementById('sm_js_console').innerHTML += 'Form handler: running form "public_form_config".<br>';
		
	// text field
	//var input_field_id = 'newsletter_signup_text_field';
	
	// status text in signup part
	//var form_text_info_id = 'public_form_message_default';
	//var form_text_fail_id = 'public_form_message_rejected';
	//var form_text_error_id = 'public_form_message_failure';
	
	// user email in success part
	//var form_text_user_email_id = 'top_menu_right_button_block_newsletter_signup_user_email';
	
	// different form parts
	//var form_part_signup_id = 'top_menu_right_button_block_newsletter_signup';
	//var form_part_success_id = 'top_menu_right_button_block_newsletter_signup_success';
	
	// handler at backend
//	var backend_target_file = 'validate_form.php';
	//var public_form_id = 1;
	
	
	
	
	
	// check essential values
	for (var i in config)
	{
		
		// check for public_form_bid (block identity)
		if (is_numeric(i) && config[i][0] == 'public_form_bid')
		{
			 
				var public_form_bid = config[i][1];
			
		}
		
		// check for public_form_btf (backend target file)
		if (is_numeric(i) && config[i][0] == 'public_form_btf')
		{
			 
				var public_form_btf = config[i][1];
			
		}
		
		// check for debug mode
		if (is_numeric(i) && config[i][0] == 'public_form_debug_mode')
		{

				var debug_mode = 1;
			
		}
		
		// activate debug mode
		// last in every iteration so it will eventually catch the debug_mode and public_form_bid
		if (!dm && debug_mode && public_form_bid && document.getElementById('public_form_debug_console_' + public_form_bid))
		{
			 
				var dm = 1;
				
				document.getElementById('public_form_debug_console_' + public_form_bid).innerHTML = 'DEBUG CONSOLE FOR PUBLIC FORM "' + public_form_bid + '"<br />';
			
		}
		
		
	}
	
	
	
	function dm_output(text)
	{
	
	   document.getElementById('public_form_debug_console_' + public_form_bid).innerHTML += text + '<br />';
	
	}
	
	function dm_warning(text)
	{
	
	   document.getElementById('public_form_debug_console_' + public_form_bid).innerHTML += '<span style="color: #aa6666;">Warning: ' + text + '</span><br />';	
	
	}
	
	
	
	// abort when bid or btf is missing
	if (!public_form_bid || !public_form_btf)
	{
		
		if (dm) dm_warning('This public form is missing its BID and/or BTF.');
		return;
		
	}
	else
	{
		
	 if (dm) dm_output('Public form essential values checked. BID = ' + public_form_bid + ', BTF = ' + public_form_btf + '.');
		
	}

	

	// check parts
	
	// check for public_form_part_passive
	if(document.getElementById('public_form_part_passive_' + public_form_bid))
	{
	
		var public_form_part_passive = document.getElementById('public_form_part_passive_' + public_form_bid);
		
	}
	else
	{
		
		if (dm) dm_output('This public form is missing its "passive" part.');
		return;
		
	}
	
	// check for public_form_part_active
	if(document.getElementById('public_form_part_active_' + public_form_bid))
	{
		
		var public_form_part_active = document.getElementById('public_form_part_active_' + public_form_bid);
		
	}
	else
	{
		
		if (dm) dm_warning('This public form is missing its "active" part.');
		return;
		
	}
	
	// check for public_form_part_completed
	if(document.getElementById('public_form_part_completed_' + public_form_bid))
	{
		
		var public_form_part_completed = document.getElementById('public_form_part_completed_' + public_form_bid);
		
	}
	else
	{
		
		if (dm) dm_output('This public form is missing its "completed" part.');
		return;
		
	}
	
	if (dm) dm_output('Public form parts checked.');
	
	
	
	
	// check form message area
	
	// check for public_form_message_default
	if(document.getElementById('public_form_message_default_' + public_form_bid))
	{
	
		var public_form_message_default = document.getElementById('public_form_message_default_' + public_form_bid);
		
	}
	else
	{
		
		if (dm) dm_output('This public form is missing a "form message area".');
		return;
		
	}
	
	// check for public_form_message_rejected
	if(document.getElementById('public_form_message_rejected_' + public_form_bid))
	{
	
		var public_form_message_rejected = document.getElementById('public_form_message_rejected_' + public_form_bid);
		
	}
	else
	{
		
		if (dm) dm_output('This public form is missing a "form message" area.');
		return;
		
	}
	
	// check for public_form_message_failure
	if(document.getElementById('public_form_message_failure_' + public_form_bid))
	{
	
		var public_form_message_failure = document.getElementById('public_form_message_failure_' + public_form_bid);
		
	}
	else
	{
		
		if (dm) dm_output('This public form is missing a "form message" area.');
		return;
		
	}
	
	// check for public_form_message_excess
	if(document.getElementById('public_form_message_excess_' + public_form_bid))
	{
	
		var public_form_message_excess = document.getElementById('public_form_message_excess_' + public_form_bid);
		
	}
	else
	{
		
		if (dm) dm_output('This public form is missing a "form message" area.');
		return;
		
	}
	
	// check for public_form_message_success
	if(document.getElementById('public_form_message_success_' + public_form_bid))
	{
	
		var public_form_message_success = document.getElementById('public_form_message_success_' + public_form_bid);
		
	}
	else
	{
		
		if (dm) dm_output('This public form is missing the "success" form-message area.');
		//return;
		
	}
	
	if (dm) dm_output('Public form fields message areas checked.');
	
	
	
	
	
	
	
	
	// check fields area
	
	// check for public_form_fields_area
	if(document.getElementById('public_form_fields_area_' + public_form_bid))
	{
	
		var public_form_fields_area = document.getElementById('public_form_fields_area_' + public_form_bid);
		
	}
	else
	{
		
		if (dm) dm_warning('This public form is missing its "fields area".');
		return;
		
	}
	
	if (dm) dm_output('Public form fields area checked.');
	
	
	
	// check data readout area
	
	// check for public_form_data_readout_area
	if(document.getElementById('public_form_data_readout_area_' + public_form_bid))
	{
	
		var public_form_data_readout_area = document.getElementById('public_form_data_readout_area_' + public_form_bid);
		
	}
	else
	{
		
		if (dm) dm_output('This public form is missing its "data readout" area.');
		return;
		
	}
	
	if (dm) dm_output('Public form data readout area checked.');
	
	
	
	
	
	// MAIN EXECUTION CHAIN
	
	// activate form
	if (public_form_part_passive.style.display != 'none')
	{
		
		// generate fields
		display_fields()
		//alert('fields inserted');
		
		// swap passive with active
		public_form_part_passive.style.display = 'none';
		public_form_part_active.style.display = '';
		
		return;
		
	}
	// complete form
	else if (public_form_part_active.style.display != 'none')
	{
		
		read_field_values();
		
		if (!validate_field_values()) return;
		
		if (evaluate_result(initiate_backend_request(url_builder()))) return;
		
		
	}
	// reset form (switch to passive again)
	else if (public_form_part_completed.style.display != 'none')
	{
		
		// swap completed with passive
		public_form_part_completed.style.display = 'none';
		public_form_part_passive.style.display = '';
		
		if (dm) dm_output('Displaying passive part.');
		
		return;
		
	}
	
	
	
	// FUNCTIONS
	
	function display_fields()
	{
		
		// render fields
		
		// initialize field counter
		//var field_count = 1;
		
		// initialize field build
		var field_build = '';
		
		// initialize field code
		var field_code = '';
		
		// set checked attribute
		//var checked_attribute = '';
		
		// set current_field_id
		var current_field_id = '';
		
		
		// define field templates
		
		// default field template
		var field_template_0 = '#label# #input#<br />';
		
		// default input template
		var input_template_0 = '<input id="#field_id#" type="#type#" value="#value#" #class# #style# #checked# #locked# #onkeyup# onkeypress="javascript: if (checkEnter(event)) public_form_handler(\'public_form_config_' + public_form_bid + '\');"/>';
		
		
		// define input attribute templates
		
		// checkbox checked #checked#
		var checkbox_checked_attribute = ' checked="checked"';
		
		// input locked #locked#
		var input_locked_attribute = ' readonly="readonly"';
		
		
		// typing assistant #onkeyup#
		var typing_assistant_1 = ' onkeyup="javascript: tvgs_typing_assistant(this,\'';
		var typing_assistant_2 = '\');"';
		
		
		// label first
		//var field_template_1 = '<span id="#label_id#" #label_class#>#label##required#</span><input id="#field_id#" class="field_type_text" #style# type="#type#" value="#value#" #checked# #locked# #onkeyup#><br />';
		
		// input first
		//var field_template_2 = '<input id="#field_id#" class="field_type_checkbox" #style# type="#type#" value="#value#" #checked# #locked#><span id="#label_id#" #label_class#>#required##label#</span><br />';
		
		// missing label or field
		//var field_template_no_label = '<input id="#field_id#" class="field_type_text" #style# type="#type#" value="#value#" #checked# #locked# #onkeyup#><br />';
		//var field_template_no_field = '<span id="#label_id#" #label_class#>#label##required#</span><br />';
		
		
		
		
		
		// field required
		//var field_required_extra_content = '<span style="color: #ff0000;">*</span>';
		
		// css classes
		//var field_label_class = ' class="field_label"';
	//	var field_label_required_class = ' class="field_label_required"';
		
		
		
		
		// check config for fields
		for (var i in config)
		{
		
		
		
			// check for field type definition
			if (is_numeric(i) && config[i][4] && config[i][4] != '')
			{
							
							// set current field id
							current_field_id = public_form_bid + '_field_' + i;
							
							
						
				// construct template
				
				// set field mask
				if (config[i][7] && ('' + config[i][7]).length > 0)
				{
				  
				  // mask provided
				  field_build = config[i][7];
				  
				  if(!field_build.match('#label#') && !field_build.match('#input#'))
				  {
				  
				    if (dm) dm_warning('The field mask for field ' + i + ' has no #label# or #input# tags, label and input will not be displayed.');
				
				  }
				
				}
				else
				{
				
				  // no mask specified, use default
				  field_build = field_template_0;
				
				}
				
				
				// insert input template
				field_build = field_build.replace('#input#', input_template_0);
							
					
				
				// wrap label tag if field is required
							if (config[i][5] && config[i][5] == 1)
							{
								
								if (config[i][12] && config[i][12].match('#label#'))
								{
								 
								 field_build = field_build.replace('#label#', config[i][12]);
								
								}
								else if (config[i][12] && config[i][12].length > 0)
								{
								
								 if (dm) dm_warning('The "label_required_mask" for field ' + i + ' has no #label# tag, label will not be displayed.');
								
								 field_build = field_build.replace('#label#', config[i][12]);
								 
								}
								
							}
							
							
				
				
				
				
							
							/*
							
							this has been completely replaced by field masking 
							
							// set template
							if (is_numeric(config[i][7]) && config[i][7] == 0 && is_numeric(config[i][9]) && config[i][9] == 0)
							{
								
								field_build = '';
								
								if (dm) dm_output('Turning off all display for field ' + i + '.');
								
							}
							else if (is_numeric(config[i][9]) && config[i][9] == 0)
							{
								
								field_build = field_template_no_label;
								
								if (dm) dm_output('Turning off label display for field ' + i + '.');
								
							}
							else if (is_numeric(config[i][7]) && config[i][7] == 0)
							{
								
								field_build = field_template_no_field;
								
								if (dm) dm_output('Turning off field display for field ' + i + '.');
								
							}
							else if (config[i][4] == 'text')
							{
								
								field_build = field_template_1;
								
							}
							else if (config[i][4] == 'checkbox')
							{
								
								field_build = field_template_2;
								
								// set checked attribute
								if (config[i][1] == '1')
							    {
									
								 checked_attribute = checkbox_checked_attribute;
									
								}
								
							}
							else
							{
								
								if (dm) dm_warning('Unknown field type "' + config[i][4] + '" requested for field ' + i + '.');
								return;
								
							}
							
							*/
							
							
							
							
							// build field
							
							// insert value
							if (config[i][1])
							{
								
								field_build = field_build.replace('#value#', config[i][1]);
							
							}
							else
							{
								
								field_build = field_build.replace('#value#', '');
							
							}
							
							// handle checkbox value
							// checkbox value operates on the "checked" attribute instead of the "value" attribute
							if (config[i][4] == 'checkbox' && config[i][1] == 1)
							{

						      field_build = field_build.replace('#checked#', checkbox_checked_attribute);
								
							}
							else
							{
							
							 field_build = field_build.replace('#checked#', '');
							
							}
							
							
							
							
							// insert label
							if (config[i][2])
							{
								
								field_build = field_build.replace('#label#', config[i][2]);
							
							}
							else
							{
								
								field_build = field_build.replace('#label#', '');
							
							}
							
						
							
							
							
							
							
							// insert id
							field_build = field_build.replace('#field_id#', current_field_id);
							field_build = field_build.replace('#label_id#', current_field_id + '_label');
							
							
							// field locked
							if (config[i][3] == 1)
							{
								
								field_build = field_build.replace('#locked#', input_locked_attribute);
							
							}
							else
							{
								
								field_build = field_build.replace('#locked#', '');
								
							}
							
							
							
							
							// input type
							
							// warn for unknown input type
							if (config[i][4] == 'text' || config[i][4] == 'checkbox')
							{
								
								field_build = field_build.replace('#type#', config[i][4]);
								
							}
							else
							{
								
								field_build = field_build.replace('#type#', '');
								
								if (dm) dm_warning('Unknown field type "' + config[i][4] + '" requested for field ' + i + '.');
							
							}
							
							
							
					
							
						
							
							
							/*
							
							if (config[i][5] && config[i][5] == 1 && !(is_numeric(config[i][8]) && config[i][8] == 0))
							{
								
								field_build = field_build.replace('#required#', field_required_extra_content);
								field_build = field_build.replace('#label_class#', field_label_required_class);
								
							
							}
							else
							{
								
								// alert message only when required display is actively turned off
							    if(is_numeric(config[i][8]) && config[i][8] == 0)
							    {
							     if (dm) dm_output('Turning off required display for field ' + i + '.');
							    }
								
								field_build = field_build.replace('#required#', '');
								field_build = field_build.replace('#label_class#', field_label_class);
							
							}
							
							*/
							
							
							// insert onkeyup for typing assistant
							if (config[i][6] && config[i][6] != '' && config[i][8] && config[i][8] == 1)
							{
								
								field_build = field_build.replace('#onkeyup#', typing_assistant_1 + (config[i][6]) + typing_assistant_2);								
							
							}
							else
							{
								
								field_build = field_build.replace('#onkeyup#', '');
							
							}
							
							
							
							
							
							// insert input class
							if (config[i][9] && config[i][9] != '')
							{
								
								field_build = field_build.replace('#class#', ' class="' + config[i][9] + '"');								
							
							}
							else
							{
								
								field_build = field_build.replace('#class#', '');
							
							}
							
							
							
							// insert input style
							if (config[i][10] && config[i][10] != '')
							{
								
								field_build = field_build.replace('#style#', ' style="' + config[i][10] + '"');								
							
							}
							else
							{
								
								field_build = field_build.replace('#style#', '');
							
							}
							 
							
							
	
							
							
							if (dm) dm_output('Created field ' + i + ': <span style="color: #6666aa; font-family: monospace; font-size: 0.8em;">' + field_build.replace(/</g, '&lt;') + '</span>.');
							
							
							// stack fields
							field_code += field_build;
					 // alert (field_build);
					
					
					  // clean out variables
							field_build = '';
							
							
				
			}
		
		}
		
		if (field_code != '')
		{
			
			if (dm) dm_output('Field code generated.');
			
		}
		else
		{
			
			if (dm) dm_output('Field code is empty.');
			
		}
		
		
		field_code = '<table>' + field_code + '</table>';
		
		if (dm) dm_output('Field code generated: ' + field_code);
		
		public_form_fields_area.innerHTML = field_code;
		

	} // end function display_fields
	
	
	
	function read_field_values()
	{
	
	 // set current_field_id
	 var current_field_id = '';
	
	 for (var i in config)
	 {
		
			// config structure
			// config[x] = ['data_key','input_field_value','input_field_label','input_field_id','input_field_type','input_field_required','validation_type']
		
			// check for field id
			if (is_numeric(i) && config[i][4] && config[i][4] != '' && config[i][4] != 'layout')
			{
			
			   
		
							// set current field id
							current_field_id = public_form_bid + '_field_' + i;
							
							
							// set field value
							config[i][1] = document.getElementById(current_field_id).value.trim();	
							
							
							
							
							// checkbox handling
							// normally an unchecked box does not return a key/value pair, but instead returns the value only when checked, this one returns 0 as value when unchecked, and 1 when checked.
							if (config[i][4] == 'checkbox' && document.getElementById(current_field_id).checked == false)
							{
								
								config[i][1] = 0;
	
							}
							else if (config[i][4] == 'checkbox' && document.getElementById(current_field_id).checked == true)
							{
							
							    config[i][1] = 1;
							
							}
							
				if (dm) dm_output('Reading value "' + config[i][1] + '" from field "' + current_field_id + '".');
							
							
							
	
			}
			
		}
			
	}
	
	
	
	function validate_field_values()
	{
	
	 // set current_field_id
	 var current_field_id = '';
	
	// set check flags
	 var field_valid = true;
	 var all_fields_valid = true;
		
		
	 for (var i in config)
		{
		
			// config structure
			// config[x] = ['data_key','input_field_value','input_field_label','input_field_id','input_field_type','input_field_required','validation_type']
		
			// check for field id
			if (is_numeric(i) && config[i][4] && config[i][4] != '')
			{
						
						 
						// set current field id
						current_field_id = public_form_bid + '_field_' + i;
						
						
						// run checks
						
							// required check
							if (config[i][5] && config[i][5] == 1)
							{
								
										// trim value and then check
										if (config[i][4] == 'checkbox')
										{
											
											if (config[i][1] == '0') field_valid = false;	
				
										}
										else if (config[i][4] == 'text')
										{
										
											if (config[i][1] == '') field_valid = false;	
										
										}
								
							}
							
							
							
							// email check
							if (config[i][6] && config[i][6] == 'email')
							{
								
								field_valid = check_valid_email(config[i][1]);
								
							}
							
							
							
							// date check
							if (config[i][6] && config[i][6] == 'date')
							{
								
								field_valid = check_valid_date(config[i][1]);
								
							}
							
							
							// evaluate result
							if (field_valid == false)
							{
								
								// check if label exists
								if (document.getElementById(current_field_id + '_label'))
								{
									document.getElementById(current_field_id + '_label').className = 'field_label_error';
								}
								
								all_fields_valid = false;
								
							}
							else
							{
								
								// check if label exists
								if (document.getElementById(current_field_id + '_label'))
								{
									document.getElementById(current_field_id + '_label').className = 'field_label';
								}
								
							}
							
							
							
							
							
							// reset variable
							field_valid = true;
	
			}
			
		}
			
		
		if (all_fields_valid == false)
		{
			
			
			if (dm) dm_output('Displaying "rejected" message, not all fields passed validation.');
			
			
			
			display_rejected();
				
			return false;
			
		}
		else
		{
			
			return true;
			
		}
			
	}
		
		


function url_builder()
{
	
	var url = public_form_btf;
	
	var key = 1;
	
	 var glue = '?';
	
	 for (var i in config)
		{
		
			
		
			// check for field key and data
			if (is_numeric(i) && config[i][0] && config[i][1] && config[i][0] != 'public_form_btf')
			{
						
						 if (key > 1)
							{
								
								glue = '&';
								
							}
							
							url += glue + config[i][0] + '=' + config[i][1];
						
					  key = key + 1;
	
			}
			
		}
		
		url = url.replace(/ /g, '%20');
		
		if (dm) dm_output('Generated URL: ' + url);
		
		 return url;
	
}
		



	
	
	function initiate_backend_request(url)
	{
	
	 // create empty request variable
	 var backend_request = null;
	 
		
		// setup request, alert on fail
		try
		{
		
			// firefox, opera 8.0+, safari, ie7
			backend_request = new XMLHttpRequest();
		
		}
		catch(e)
		{
		
			// old ie
			try
			{
				
				backend_request = new ActiveXObject('Microsoft.XMLHTTP');
			
			}
			catch(e)
			{
				
				alert ('This webbrowser does not support XMLHTTP!');
				return;  
			
			}
	
		}
		
		// send request, wait for response
		backend_request.open('GET',url,false);
		
		backend_request.send(null);
		
		// get result
		if (dm) dm_output('Server response: <span style="color: #6666aa; font-family: monospace; font-size: 0.8em;">' + backend_request.responseText.replace(/</g, '&lt;') + '</span>');
		return backend_request.responseText;
			
	}

	
	
	function display_failure()
	{
		
	
		// set text field border to pulse color to red
		//document.getElementById(input_field_id).style.borderColor = '#ff0000';
		
		/*
		var colors = [];
			colors[0] = '#9bc9ed';
			colors[1] = '#9ec1e3';
			colors[2] = '#a4b7d7';
			colors[3] = '#aaaac8';
			colors[4] = '#b19db9';
			colors[5] = '#b98da6';
			colors[6] = '#c07d94';
			colors[7] = '#c86c80';
			colors[8] = '#d15c6d';
			colors[9] = '#d94c59';
			colors[10] = '#e13b46';
			colors[11] = '#e92c34';
			colors[12] = '#f01e24';
			colors[13] = '#f61216';
			colors[14] = '#fb0809';
			colors[15] = '#fe0101';
		
	
		color_pulse(colors, document.getElementById(input_field_id), 'borderColor', 75)
		
		color_pulse(colors, document.getElementById(form_text_fail_id), 'color', 75)
		*/
		// swap info text with failure text
		public_form_message_default.style.display = 'none';
		public_form_message_rejected.style.display = 'none';
		public_form_message_failure.style.display = '';
		public_form_message_excess.style.display = 'none';
		
		

		
		/*
	
	 // HOW TO READ COLORS FROM DOM FOR ALL BROWSERS
	
		//alert(el.style.borderColor);
		
		if (	el.style.borderColor == '#ff0000' || el.style.borderColor == 'rgb(255, 0, 0)' || el.style.borderColor == 'rgb(255, 0, 0) rgb(255, 0, 0) rgb(255, 0, 0) rgb(255, 0, 0)')
		{
				el.style.borderColor = '#9acaee';
				//alert('making blue');
		}
		else
		{
				el.style.borderColor = '#ff0000';
				//alert('making red');
		}
		
	 */
	
	}
	
	
	
	function display_rejected()
	{
		
		// set text field border to color orange
		//document.getElementById(input_field_id).style.borderColor = 'orange';

		// swap info text with rejected text
		public_form_message_default.style.display = 'none';
		public_form_message_rejected.style.display = '';
		public_form_message_failure.style.display = 'none';
		public_form_message_excess.style.display = 'none';
		
		
	
	}
	
	
	function display_excess()
	{
		
		// set text field border to color orange
		//document.getElementById(input_field_id).style.borderColor = 'orange';

		// swap info text with excess text
		public_form_message_default.style.display = 'none';
		public_form_message_rejected.style.display = 'none';
		public_form_message_failure.style.display = 'none';
		public_form_message_excess.style.display = '';
		
		var review_data = '';
		
		// get review data
		for (var i in config)
		{

			// check for field id
			if (is_numeric(i) && config[i][4] && config[i][4] != '' && config[i][11] == 1)
			{
			    
			    
			    var value_mask = '';
			    
			    // get mask
			    if(config[i][13] && config[i][13] != '')
			    {
			    
			     value_mask = config[i][13];
			     
			    }
			    else
			    {
			    
			     // use default mask
			     value_mask = '#value#<br />';
			     
			    }
			    
			    
			    review_data += value_mask.replace('#value#', config[i][1]);
						
			}
			
		}
		
		
		// show readout data
		public_form_data_readout_area.innerHTML = review_data;
		
		// swap active with completed
		public_form_part_active.style.display = 'none';
		public_form_part_completed.style.display = '';
		
		if (dm) dm_output('Displaying excess part. Now switched to completed display for practical use.');
	
	}
	
	
	
	/* is this still used ???????
	function display_invalid()
	{
		
		// set text field border to color orange
		//document.getElementById(input_field_id).style.borderColor = 'orange';

		// swap info text with error text
		public_form_message_default.style.display = 'none';
		public_form_message_rejected.style.display = '';
		public_form_message_failure.style.display = 'none';
		public_form_message_excess.style.display = 'none';
		
		
	
	}
	 */

	
	function display_success()
	{
		
		
		// display default text
		// this will disappear immediately, but makes sure the default message is shown again when the form is used again
		public_form_message_default.style.display = '';
		public_form_message_rejected.style.display = 'none';
		public_form_message_failure.style.display = 'none';
		public_form_message_excess.style.display = 'none';
		
		
		var review_data = '';
		
		// get review data
		for (var i in config)
		{

			// check for field id
			if (is_numeric(i) && config[i][4] && config[i][4] != '' && config[i][11] == 1)
			{
			    
			    
			    var value_mask = '';
			    
			    // get mask
			    if(config[i][13] && config[i][13] != '')
			    {
			    
			     value_mask = config[i][13];
			     
			    }
			    else
			    {
			    
			     // use default mask
			     value_mask = '#value#<br />';
			     
			    }
			    
			    
			    review_data += value_mask.replace('#value#', config[i][1]);
						
			}
			
		}
		
		
		// show readout data
		public_form_data_readout_area.innerHTML = review_data;
		
		// swap active with completed
		public_form_part_active.style.display = 'none';
		public_form_part_completed.style.display = '';
		
		if (dm) dm_output('Displaying completed part.');
	
	}
	

	
	function evaluate_result(result)
	{
		
		// make sure it is a number
	// result = result * 1;
	 	
	 	// fix for bad return value with comments from testing etc.
	 	result = result.replace(/<!--\+.*?\+-->/, '').trim();
		
		//alert('Server says: "' + result + '"');
		
		if ((result * 1) == 1)
		{
		
	 	 	display_success();
			
		}
		else if ((result * 1) == 2)
		{
		
			display_rejected();
			
		}
		else if ((result * 1) == 3)
		{
		
			display_excess();
			
		}
		else
		{	
			
		 display_failure();
			
		}
		
		return true;
	
	}
	
	
	
	
	
	
	function check_valid_email(input)
	{	
		
		if(/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z]{2,6})+$/.test(input))
		{         

 		//alert('good');
		 return true;
		
		}
		else
		{
			
			//alert('bad');
   return false;   
		
		}
		
 }
	
	function check_valid_date(input)
	{	
	
	
	 
		if(/^[0-3]?[0-9] *[-\/] *[0-1]?[0-9] *[-\/] *[1-2][0-9]{3}$/.test(input))
		{         

 		//alert('good');
		 return true;
		
		}
		else
		{
			
			//alert('bad');
   return false;   
		
		}
		
 }
	
	
	
/*
	
	// HANDLER
	
	// check config id's
	if (!document.getElementById(input_field_id)) missing_id_alert(input_field_id);
	if (!document.getElementById(form_text_info_id)) missing_id_alert(form_text_info_id);
	if (!document.getElementById(form_text_fail_id)) missing_id_alert(form_text_fail_id);
	if (!document.getElementById(form_text_error_id)) missing_id_alert(form_text_error_id);
	if (!document.getElementById(form_text_user_email_id)) missing_id_alert(form_text_user_email_id);
	if (!document.getElementById(form_part_signup_id)) missing_id_alert(form_part_signup_id);
	if (!document.getElementById(form_part_success_id)) missing_id_alert(form_part_success_id);
	
 // stop color pulse in progress
	clearInterval(window.interval_id);
	
	
	// get user entered values
	var user_input = document.getElementById(input_field_id).value.trim();	
	
	// check for valid email address
	if (check_valid_email_address(user_input))
	{
	
		// an array for expanding to multiple values
		var input_field_value = []; 
		input_field_value[input_field_value.length] = [input_field_id, user_input];
		
		// construct backend request url
		var backend_request_url = backend_target_file + '?' + input_field_value[0][0] + '=' + input_field_value[0][1] + '&public_form_id=' + public_form_id;
			
		// send backend request
		//backend_request_result = initiate_backend_request(backend_request_url);
		
		//evaluate_result(backend_request_result);
	
		evaluate_result(initiate_backend_request(backend_request_url));
		
	}
	else
	{
		
		display_failure();
		
	}
	
	*/
	
}

