// core.js

/*******************
 *                 *
 * JS FORM BUILDER *
 *                 *
 *******************/

// Formfield Definitions

/*
Field Types:
1: text-line 			-required: field_label
2: text-box 			-required: field_label
3: checkbox 			-required: field_label, return_value
4: radio-buttons		-required: field_label, field_options (new Array('val1','val2','...')), free_attributes (new Array('attr1','attr2','...')) !!! number of elements FREE_ATTRIBUTES must equal number of elements FIELD_OPTIONS !!!
5: date-field 			-required: field_label
6: date/time-field 		-required: field_label, free_attributes (new Array('date-button attr','time-button attr')) !!! number of elements FREE_ATTRIBUTES must be TWO(2) !!!
7: drop-down 			-required: field_label, field_options (new Array('val1','val2','...')), free_attributes (new Array('drop-down attr','attr val1','attr val2','...')) !!! number of elements FREE_ATTRIBUTES must equal number of elements FIELD_OPTIONS + 1 !!!
8: check all applicable	-required: field_label, field_options (new Array('val1','val2','...')), free_attributes (new Array('attr1','attr2','...')) !!! number of elements FREE_ATTRIBUTES must equal number of elements FIELD_OPTIONS !!!
9: hidden 				-required: field_label, return_value
10: comment/instruction	-required: field_label
11: e-mail address		-required: field_label, free_attributes (new Array('attr1','attr2')) !!! number of elements FREE_ATTRIBUTES must be TWO(2) !!!
12: password			-required: field_label, free_attributes (new Array('attr1','attr2')) !!! number of elements FREE_ATTRIBUTES must be TWO(2) !!!
13: read-only			-required: field_label, free_attributes (new Array('id="[field_id]")) !!! FREE_ATTRIBUTES must contain element id !!!
14: file				-required: field_label

// Sample Array

field_type[index]		=	1;
field_label[index]		=	'text-line';
db_label[index]			=	'';
linebreak[index]		=	0;
field_size[index]		=	'';
field_rows[index]		=	'';
field_options[index]	=	new Array();
return_value[index]		=	'';
prefill_value[index]	=	new Array('');
prefill_date[index]		=	'';
prefill_time[index]		=	'';
permitted_chars[index]	=	' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890,\'":;,/?!$%&()-+=';
required[index]			=	1;
flagged[index]			=	1;
free_attributes[index]	=	new Array('onkeyup="getElementById(\'value_display\').value=this.value;"');
index++;

field_type[index]		=	2;
field_label[index]		=	'text-box';
db_label[index]			=	'';
linebreak[index]		=	0;
field_size[index]		=	'30';
field_rows[index]		=	'5';
field_options[index]	=	new Array();
return_value[index]		=	'';
prefill_value[index]	=	new Array('');
prefill_date[index]		=	'';
prefill_time[index]		=	'';
permitted_chars[index]	=	' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890,\'":;,/?!$%&()-+=';
required[index]			=	1;
flagged[index]			=	0;
free_attributes[index]	=	new Array('');
index++;

field_type[index]		=	3;
field_label[index]		=	'checkbox';
db_label[index]			=	'';
linebreak[index]		=	1;
field_size[index]		=	'';
field_rows[index]		=	'';
field_options[index]	=	new Array();
return_value[index]		=	'selected';
prefill_value[index]	=	new Array('1');
prefill_date[index]		=	'';
prefill_time[index]		=	'';
permitted_chars[index]	=	'';
required[index]			=	1;
flagged[index]			=	0;
free_attributes[index]	=	new Array('');
index++;

field_type[index]		=	4;
field_label[index]		=	'radio button';
db_label[index]			=	'';
linebreak[index]		=	0;
field_size[index]		=	'';
field_rows[index]		=	'';
field_options[index]	=	new Array('Yes','No','Who knows?');
return_value[index]		=	'';
prefill_value[index]	=	new Array('','1','');
prefill_date[index]		=	'';
prefill_time[index]		=	'';
permitted_chars[index]	=	'';
required[index]			=	1;
flagged[index]			=	0;
free_attributes[index]	=	new Array('','','');
index++;

field_type[index]		=	5;
field_label[index]		=	'date';
db_label[index]			=	'';
linebreak[index]		=	0;
field_size[index]		=	'';
field_rows[index]		=	'';
field_options[index]	=	new Array();
return_value[index]		=	'';
prefill_value[index]	=	new Array('');
prefill_date[index]		=	'';
prefill_time[index]		=	'';
permitted_chars[index]	=	'';
required[index]			=	1;
flagged[index]			=	0;
free_attributes[index]	=	new Array('');
index++;

field_type[index]		=	6;
field_label[index]		=	'date and time';
db_label[index]			=	'';
linebreak[index]		=	1;
field_size[index]		=	'';
field_rows[index]		=	'';
field_options[index]	=	new Array();
return_value[index]		=	'';
prefill_value[index]	=	new Array('');
prefill_date[index]		=	'';
prefill_time[index]		=	'';
permitted_chars[index]	=	'';
required[index]			=	1;
flagged[index]			=	0;
free_attributes[index]	=	new Array('');
index++;

field_type[index]		=	7;
field_label[index]		=	'dropdown list';
db_label[index]			=	'';
linebreak[index]		=	0;
field_size[index]		=	'';
field_rows[index]		=	'';
field_options[index]	=	new Array('Yes','No','Who knows?');
return_value[index]		=	'';
prefill_value[index]	=	new Array('1','','');
prefill_date[index]		=	'';
prefill_time[index]		=	'';
permitted_chars[index]	=	'';
required[index]			=	1;
flagged[index]			=	0;
free_attributes[index]	=	new Array('','','');
index++;

field_type[index]		=	8;
field_label[index]		=	'Check all that apply';
db_label[index]			=	'';
linebreak[index]		=	0;
field_size[index]		=	'';
field_rows[index]		=	'';
field_options[index]	=	new Array('Yes','No','Who knows?');
return_value[index]		=	'';
prefill_value[index]	=	new Array('1','1','1');
prefill_date[index]		=	'';
prefill_time[index]		=	'';
permitted_chars[index]	=	'';
required[index]			=	1;
flagged[index]			=	0;
free_attributes[index]	=	new Array('','','');
index++;

field_type[index]		=	9;
field_label[index]		=	'hidden';
db_label[index]			=	'';
linebreak[index]		=	0;
field_size[index]		=	'';
field_rows[index]		=	'';
field_options[index]	=	new Array();
return_value[index]		=	'This was sent by a form';
prefill_value[index]	=	new Array('');
prefill_date[index]		=	'';
prefill_time[index]		=	'';
permitted_chars[index]	=	'';
required[index]			=	1;
flagged[index]			=	0;
free_attributes[index]	=	new Array('');
index++;

field_type[index]		=	10;
field_label[index]		=	'comment/instruction';
db_label[index]			=	'';
linebreak[index]		=	0;
field_size[index]		=	'';
field_rows[index]		=	'';
field_options[index]	=	new Array();
return_value[index]		=	'';
prefill_value[index]	=	new Array('');
prefill_date[index]		=	'';
prefill_time[index]		=	'';
permitted_chars[index]	=	'';
required[index]			=	0;
flagged[index]			=	0;
free_attributes[index]	=	new Array('');
index++;

field_type[index]		=	11;
field_label[index]		=	'e-mail address';
db_label[index]			=	'';
linebreak[index]		=	0;
field_size[index]		=	'';
field_rows[index]		=	'';
field_options[index]	=	new Array('Yes','No');
return_value[index]		=	'';
prefill_value[index]	=	new Array('');
prefill_date[index]		=	'';
prefill_time[index]		=	'';
permitted_chars[index]	=	'';
required[index]			=	1;
flagged[index]			=	0;
free_attributes[index]	=	new Array('','');
index++;

field_type[index]		=	12;
field_label[index]		=	'password';
db_label[index]			=	'';
linebreak[index]		=	1;
field_size[index]		=	'';
field_rows[index]		=	'';
field_options[index]	=	new Array('Yes','No');
return_value[index]		=	'';
prefill_value[index]	=	new Array('');
prefill_date[index]		=	'';
prefill_time[index]		=	'';
permitted_chars[index]	=	'';
required[index]			=	1;
flagged[index]			=	0;
free_attributes[index]	=	new Array('','');
index++;

field_type[index]		=	13;
field_label[index]		=	'read only';
db_label[index]			=	'';
linebreak[index]		=	0;
field_size[index]		=	'30';
field_rows[index]		=	'';
field_options[index]	=	new Array();
return_value[index]		=	'';
prefill_value[index]	=	new Array('copied from first field');
prefill_date[index]		=	'';
prefill_time[index]		=	'';
permitted_chars[index]	=	'';
required[index]			=	0;
flagged[index]			=	1;
free_attributes[index]	=	new Array('id="value_display" style="background-color:red;"');
index++;

field_type[index]		=	14;
field_label[index]		=	'file';
db_label[index]			=	'';
linebreak[index]		=	0;
field_size[index]		=	'';
field_rows[index]		=	'';
field_options[index]	=	new Array();
return_value[index]		=	'';
prefill_value[index]	=	new Array('');
prefill_date[index]		=	'';
prefill_time[index]		=	'';
permitted_chars[index]	=	'';
required[index]			=	0;
flagged[index]			=	0;
free_attributes[index]	=	new Array('');
index++;


// Submit-Button text (only if mail-function wanted)
submitbutton_txt= 'Go!';
submit_button_id='submit_button'
submit_button_active=1;

// Subject Line text
email_subject= 'Quick Contact Form';

// Success Message
successful_sent_message='<hr />Thank you!<hr />';

// AJAX-Script (only if AJAX function is wanted)
ajax_script='ajax_survey.php';
viewbutton_txt='view';

// Activate
createForm();
*/

//=======================================================================//

// Check Form
function checkForm(){
	var element_trace=0;
	for(field_count=0;field_count<field_type.length;field_count++){
		if(required[field_count]){
			if(field_type[field_count]==3){
				if(!document.jsForm.elements[element_trace].checked){report_missing(field_count,element_trace);return false;}
				element_trace++;
			}else if((field_type[field_count]==4)||(field_type[field_count]==9)){
					var checkmark_check=false;
					for(option_count=0;option_count<field_options[field_count].length;option_count++){
						if(document.jsForm.elements[element_trace].checked){checkmark_check=true;}
						element_trace++;
					}
				if(!checkmark_check){report_missing(field_count,element_trace);return false;}
			}else if(field_type[field_count]==8){
				if(document.jsForm.elements[element_trace].selectedIndex<2){report_missing(field_count,element_trace);return false;}
				element_trace++;
			}else if(field_type[field_count]==6){
				if((!document.jsForm.elements[element_trace].value.length) || (!document.jsForm.elements[element_trace+1].value.length)){report_missing(field_count,element_trace);return false;}
				element_trace+=2;
			}else{
				if(!document.jsForm.elements[element_trace].value.length){report_missing(field_count,element_trace);return false;}
				element_trace++;
				if((field_type[field_count]==5) || (field_type[field_count]==11) || (field_type[field_count]==12) || (field_type[field_count]==14)){
					element_trace++;
				}
			}
		}else{
			if((field_type[field_count]==4)||(field_type[field_count]==7)){
				element_trace+=field_options[field_count].length;
			}else if((field_type[field_count]==5) || (field_type[field_count]==11) || (field_type[field_count]==12) || (field_type[field_count]==14)){
				element_trace+=2;
			}else if(field_type[field_count]==6){
				element_trace+=3;
			}else{
				if(field_type[field_count]!=10){
					element_trace++;
				}
			}
		}
	}
	return true;
}

// Get All Field Values for AJAX transmission
function getAllFieldVals(){
	var element_trace=0;
	var submission_string='';
	for(field_count=0;field_count<field_type.length;field_count++){

		if(db_label[field_count]){
			var form_field_label=db_label[field_count];
		}else{
			var form_field_label=field_label[field_count];
		}

		if(field_type[field_count]==3){
			if(document.jsForm.elements[element_trace].checked){submission_string+='&'+encodeURI(form_field_label)+'='+encodeURI(document.jsForm.elements[element_trace].value);}
			element_trace++;
		}else if(field_type[field_count]==4){
				var checkmark_check=false;
				for(option_count=0;option_count<field_options[field_count].length;option_count++){
					if(document.jsForm.elements[element_trace].checked){submission_string+='&'+encodeURI(form_field_label)+'='+encodeURI(document.jsForm.elements[element_trace].value);}
					element_trace++;
				}
		}else if(field_type[field_count]==9){
			submission_string+='&'+encodeURI(form_field_label)+'='+encodeURI(document.jsForm.elements[element_trace].value);
			element_trace++;
		}else if(field_type[field_count]==8){
			submission_string+='&'+encodeURI(form_field_label)+'='+encodeURI(document.jsForm.elements[element_trace].value);
			element_trace++;
		}else if(field_type[field_count]==6){
			submission_string+='&'+encodeURI(form_field_label)+'='+encodeURI(document.jsForm.elements[element_trace].value) + '-' + encodeURI(document.jsForm.elements[element_trace+1].value);
			element_trace+=2;
		}else{
			submission_string+='&'+encodeURI(form_field_label)+'='+encodeURI(document.jsForm.elements[element_trace].value);
			element_trace++;
			if((field_type[field_count]==5) || (field_type[field_count]==11) || (field_type[field_count]==12) || (field_type[field_count]==14)){
				element_trace++;
			}
		}
	}
	return submission_string.substr(1);
}

// Get Field Values for Survey Viewing
function getSurveyViewFieldVals(){
	var element_trace=0;
	var submission_string='&display_results=true';
	for(field_count=0;field_count<field_type.length;field_count++){

		if(db_label[field_count]){
			var form_field_label=db_label[field_count];
		}else{
			var form_field_label=field_label[field_count];
		}

		if(field_type[field_count]==3){
			if(document.jsForm.elements[element_trace].checked){submission_string+='&'+encodeURI(form_field_label)+'='+encodeURI(document.jsForm.elements[element_trace].value);}
			element_trace++;
		}else if(field_type[field_count]==4){
				var checkmark_check=false;
				for(option_count=0;option_count<field_options[field_count].length;option_count++){
					if(document.jsForm.elements[element_trace].checked){submission_string+='&'+encodeURI(form_field_label)+'='+encodeURI(document.jsForm.elements[element_trace].value);}
					element_trace++;
				}
		}else if(field_type[field_count]==9){
			submission_string+='&'+encodeURI(form_field_label)+'='+encodeURI(document.jsForm.elements[element_trace].value);
			element_trace++;
		}else if(field_type[field_count]==8){
			submission_string+='&'+encodeURI(form_field_label)+'='+encodeURI(document.jsForm.elements[element_trace].value);
			element_trace++;
		}else if(field_type[field_count]==6){
			submission_string+='&'+encodeURI(form_field_label)+'='+encodeURI(document.jsForm.elements[element_trace].value) + '-' + encodeURI(document.jsForm.elements[element_trace+1].value);
			element_trace+=2;
		}else{
			submission_string+='&'+encodeURI(form_field_label)+'='+encodeURI(document.jsForm.elements[element_trace].value);
			element_trace++;
			if((field_type[field_count]==5) || (field_type[field_count]==11) || (field_type[field_count]==12) || (field_type[field_count]==14)){
				element_trace++;
			}
		}
	}
	return submission_string.substr(1);
}

// Place focus and create alert
function report_missing(fc,et){
	document.jsForm.elements[et].focus();
	alert(field_label[fc] + " is required!");
}
// Check for illegal characters
function check_chars(entered,permitted,label){
	if(permitted.length){
		var detected='';
		for(x=0;x<entered.length;x++){
			for(y=0;y<permitted.length;y++){
				// search illegal
				if(permitted.indexOf(entered.charAt(x))<0){
					var illegal_char=entered.charAt(x);
					//remove illegal
					var buffer='';
					for(z=0;z<entered.length;z++){
						if(entered.charAt(z)!=illegal_char){
							buffer=buffer+entered.charAt(z);
						}
					}
					entered=buffer;
					detected = detected+ illegal_char;
				}else{
					// terminate inner loop
					y=permitted.length;
				}
			}
		}
		if(detected.length){
			alert(label.toUpperCase() + " contained the following illegal characters:\n' " + detected + " '\nAllowed are only the following characters:\n'" + permitted + "'\nNOTICE: ALL ILLEGAL CHARACTERS WILL BE REMOVED!");
		}
	}
	return entered;
}
// Create Form
function createForm(){
	var form_html='';
	var enctype='';
	var form_fields;

	for(field_count=0;field_count<field_type.length;field_count++){
		if((required[field_count]) && field_type[field_count]!=3){
			required_label='<span style="color:red;">*</span>';
		}else{
			required_label='';
		}
		if(flagged[field_count]){
			if(required_label.length){required_label=required_label+'/';}
			required_label=required_label+'<span style="color:red;">**</span>';
		}
		if(db_label[field_count]){
			var form_field_label=db_label[field_count];
		}else{
			var form_field_label=field_label[field_count];
		}
		//--- FORMFIELD TYPES ---
		// textline
		if(field_type[field_count]==1){
			form_html=form_html+'<td valign="top"><b>'+field_label[field_count]+required_label+'</b> <input type="text" name="'+form_field_label+'" size="'+field_size[field_count]+'" value="'+prefill_value[field_count][0]+'" onblur="this.value=check_chars(this.value,\''+permitted_chars[field_count]+'\',\''+field_label[field_count]+'\');" '+free_attributes[field_count][0]+' /></td>';
		}
		// textbox
		else if(field_type[field_count]==2){
			form_html=form_html+'<td valign="top"><b>'+field_label[field_count]+required_label+'</b><br /><textarea name="'+form_field_label+'" cols="'+field_size[field_count]+'" rows="'+field_rows[field_count]+'" onblur="this.value=check_chars(this.value,\''+permitted_chars[field_count]+'\',\''+field_label[field_count]+'\');" '+free_attributes[field_count][0]+'>'+prefill_value[field_count][0]+'</textarea></td>';
		}
		// checkbox
		else if(field_type[field_count]==3){
			if(prefill_value[field_count][0]){prefill_value[field_count][0]='checked="true"';}
			form_html=form_html+'<td valign="top"><table cellpadding="0" cellspacing="0" style="font-family:sans-serif;font-size:1.0em;"><tr><td><input type="checkbox" name="'+form_field_label+'" '+prefill_value[field_count][0]+' value="'+return_value[field_count]+'" '+free_attributes[field_count][0]+' /></td><td><b>'+field_label[field_count]+required_label+'</b></td></tr></table></td>';
		}
		// radio buttons
		else if(field_type[field_count]==4){
			form_html=form_html+'<td valign="top"><b>'+field_label[field_count]+required_label+'</b><table style="font-family:sans-serif;font-size:1.0em;">';
			for(option_count=0;option_count<field_options[field_count].length;option_count++){
				sub_section=field_options[field_count][option_count].split('|');
				if(prefill_value[field_count][option_count]){prefill_value[field_count][option_count]='checked="true"';}
				if(sub_section.length>1){
					form_html=form_html+'<tr><td><input type="radio" name="'+form_field_label+'" value="'+sub_section[1]+'" '+prefill_value[field_count][option_count]+' '+free_attributes[field_count][option_count]+' /> </td><td>'+sub_section[0]+'</td></tr>';
				}else{
					form_html=form_html+'<tr><td><input type="radio" name="'+form_field_label+'" value="'+sub_section[0]+'" '+prefill_value[field_count][option_count]+' '+free_attributes[field_count][option_count]+' /> </td><td>'+sub_section[0]+'</td></tr>';
				}
			}
			form_html=form_html+'</table></td>';
		}
		// date-field
		else if(field_type[field_count]==5){
			form_html=form_html+'<td valign="top"><table style="font-family:sans-serif;font-size:1.0em;" cellpadding="0" cellspacing="0"><tr><td><b>'+field_label[field_count]+required_label+'</b> <table cellpadding="0" cellspacing="0"><tr><td nowrap="true"><input type="text" name="'+form_field_label+'" id="'+field_label[field_count]+'" size="10" readonly="true" value="'+prefill_date[field_count]+'" onclick="form_calendar(\''+field_label[field_count]+'\',\'\',\'calendar'+calendar_count+'\',\''+prefill_date[field_count]+'\')" '+free_attributes[field_count][0]+' /><input type="button" value="[+]" onclick="form_calendar(\''+field_label[field_count]+'\',\'\',\'calendar'+calendar_count+'\',\''+prefill_date[field_count]+'\')" style="font-size:10px;" /></td></tr></table><div id="calendar'+calendar_count+'"></div></td></tr></table>';
			calendar_count++;
		}
		// date/time-field
		else if(field_type[field_count]==6){
			form_html=form_html+'<td valign="top"><table style="font-family:sans-serif;font-size:1.0em;" cellpadding="0" cellspacing="0"><tr><td><b>'+field_label[field_count]+required_label+'</b> <table cellpadding="0" cellspacing="0"><tr><td nowrap="true"><input type="text" name="'+form_field_label+'_date" id="'+field_label[field_count]+'_date" size="10" readonly="true" value="'+prefill_date[field_count]+'" onclick="form_calendar(\''+field_label[field_count]+'_date\',\''+field_label[field_count]+'_time\',\'calendar'+calendar_count+'\',\''+prefill_date[field_count]+'\')" '+free_attributes[field_count][0]+' /><input type="text" name="'+form_field_label+'_time" id="'+field_label[field_count]+'_time" size="6" readonly="true" value="'+prefill_time[field_count]+'" onclick="form_calendar(\''+field_label[field_count]+'_date\',\''+field_label[field_count]+'_time\',\'calendar'+calendar_count+'\',\''+prefill_date[field_count]+'\')" '+free_attributes[field_count][1]+' /><input type="button" value="[+]" onclick="form_calendar(\''+field_label[field_count]+'_date\',\''+field_label[field_count]+'_time\',\'calendar'+calendar_count+'\',\''+prefill_date[field_count]+'\')" /></td></tr></table><div id="calendar'+calendar_count+'"></div></td></tr></table>';
			calendar_count++;
		}
		// drop-down
		else if(field_type[field_count]==7){
			form_html=form_html+'<td valign="top"><b>'+field_label[field_count]+required_label+'</b><table><tr><td><select name="'+form_field_label+'" style="font-family:sans-serif;font-size:0.8em;" '+free_attributes[field_count][0]+'><option value="">- Please Select -</option><option value="">==================</option>';
			for(option_count=0;option_count<field_options[field_count].length;option_count++){
				sub_section=field_options[field_count][option_count].split('|');
				if(prefill_value[field_count][option_count]){prefill_value[field_count][option_count]='selected="true"';}
				if(sub_section.length>1){
					form_html=form_html+'<option value="'+sub_section[1]+'" '+prefill_value[field_count][option_count]+' '+free_attributes[field_count][option_count+1]+' />&nbsp;'+sub_section[0]+'</option>';
				}else{
					form_html=form_html+'<option value="'+sub_section[0]+'" '+prefill_value[field_count][option_count]+' '+free_attributes[field_count][option_count+1]+' />&nbsp;'+sub_section[0]+'</option>';
				}
			}
			form_html=form_html+'</td></tr></table></td>';
		}
		// applicable
		else if(field_type[field_count]==8){
			form_html=form_html+'<td valign="top"><input type="hidden" name="'+form_field_label+'" value="-CHOICES BELOW-" /><b>'+field_label[field_count]+required_label+'</b><table style="font-family:sans-serif;font-size:1.0em;">';
			for(option_count=0;option_count<field_options[field_count].length;option_count++){
				sub_section=field_options[field_count][option_count].split('|');
				if(prefill_value[field_count][option_count]){prefill_value[field_count][option_count]='checked="true"';}
				if(sub_section.length>1){
					form_html=form_html+'<tr><td><input type="checkbox" name="'+field_options[field_count][option_count]+'" '+prefill_value[field_count][option_count]+' value="'+sub_section[1]+'" '+free_attributes[field_count][option_count]+' /> </td><td>'+sub_section[0]+'</td></tr>';
				}else{
					form_html=form_html+'<tr><td><input type="checkbox" name="'+field_options[field_count][option_count]+'" '+prefill_value[field_count][option_count]+' value="-YES-" '+free_attributes[field_count][option_count]+' /> </td><td>'+field_options[field_count][option_count]+'</td></tr>';
				}
			}
			form_html=form_html+'</table></td>';
		}
		// hidden
		else if(field_type[field_count]==9){
			form_html=form_html+'<input type="hidden" name="'+form_field_label+'" value="'+return_value[field_count]+'" '+free_attributes[field_count][0]+' />';
		}
		// comment
		else if(field_type[field_count]==10){
			form_html=form_html+'</tr><tr><td valign="top" colspan="10" '+free_attributes[field_count][0]+'>'+field_label[field_count]+'</td></tr></table><table cellpadding="5" style="font-family:sans-serif;font-size:0.8em;"><tr>';
		}
		// e-mail
		else if(field_type[field_count]==11){
			form_html=form_html+'<td valign="top"><table style="font-family:sans-serif;font-size:1.0em;"><tr><td valign="top"><b>'+field_label[field_count]+required_label+'</b> </td><td valign="top"><input type="text" id="e_mail_field'+field_count+'" name="'+form_field_label+'" size="'+field_size[field_count]+'" onkeyup="javascript:if(this.value){if(!((this.value.indexOf(\'@\')>1)&&(this.value.indexOf(\'.\')<this.value.length-2)&&(this.value.indexOf(\'@\')<this.value.indexOf(\'.\')))){document.getElementById(\'alert_msg'+field_count+'\').innerHTML=\'Currently invalid e-mail address\';document.getElementById(\''+submit_button_id+'\').disabled=true;}else{document.getElementById(\'alert_msg'+field_count+'\').innerHTML=\'\';if(this.value!=document.getElementById(\'e_mail_field_verify'+field_count+'\').value){document.getElementById(\'alert_msg_verify'+field_count+'\').innerHTML=\'Currently e-mail address verification not matching!\';document.getElementById(\''+submit_button_id+'\').disabled=true;}else{document.getElementById(\'alert_msg'+field_count+'\').innerHTML=\'\';document.getElementById(\'alert_msg_verify'+field_count+'\').innerHTML=\'\';document.getElementById(\''+submit_button_id+'\').disabled=false;}}}else{document.getElementById(\'alert_msg'+field_count+'\').innerHTML=\'\';document.getElementById(\'alert_msg_verify'+field_count+'\').innerHTML=\'\';document.getElementById(\''+submit_button_id+'\').disabled=false;}" '+free_attributes[field_count][0]+' /><div id="alert_msg'+field_count+'" style="color:#ff0000;font-size:0.8em;"></div></td></tr><tr><td valign="top"><b>'+field_label[field_count]+' (verify)'+required_label+'</b> </td><td valign="top"><input type="text" id="e_mail_field_verify'+field_count+'" name="dummyfield" size="'+field_size[field_count]+'" onkeyup="javascript:if(this.value!=document.getElementById(\'e_mail_field'+field_count+'\').value){document.getElementById(\'alert_msg_verify'+field_count+'\').innerHTML=\'Currently e-mail address verification not matching!\';document.getElementById(\''+submit_button_id+'\').disabled=true;}else{document.getElementById(\'alert_msg_verify'+field_count+'\').innerHTML=\'\';document.getElementById(\''+submit_button_id+'\').disabled=false;}" '+free_attributes[field_count][1]+' /><div id="alert_msg_verify'+field_count+'" style="color:#ff0000;font-size:0.8em;"></div></td></tr></table></td>';
		}
		// password
		else if(field_type[field_count]==12){
			form_html=form_html+'<td valign="top"><table style="font-family:sans-serif;font-size:1.0em;"><tr><td valign="top"><b>'+field_label[field_count]+required_label+'</b> </td><td valign="top"><input type="password" id="password_field'+field_count+'" name="'+form_field_label+'" size="'+field_size[field_count]+'" onkeyup="javascript:if(this.value){if(this.value!=document.getElementById(\'password_field_verify'+field_count+'\').value){document.getElementById(\'alert_msg_verify'+field_count+'\').innerHTML=\'Currently password verification not matching!\';document.getElementById(\''+submit_button_id+'\').disabled=true;}else{document.getElementById(\'alert_msg_verify'+field_count+'\').innerHTML=\'\';document.getElementById(\''+submit_button_id+'\').disabled=false;}}else{document.getElementById(\'alert_msg_verify'+field_count+'\').innerHTML=\'\';document.getElementById(\''+submit_button_id+'\').disabled=false;}" onblur="this.value=check_chars(this.value,\''+permitted_chars[field_count]+'\',\''+field_label[field_count]+'\');" '+free_attributes[field_count][0]+' /></td></tr><tr><td valign="top"><b>'+field_label[field_count]+' (verify)'+required_label+'</b> </td><td valign="top"><input type="password" id="password_field_verify'+field_count+'" name="dummyfield" size="'+field_size[field_count]+'" onkeyup="javascript:if(this.value!=document.getElementById(\'password_field'+field_count+'\').value){document.getElementById(\'alert_msg_verify'+field_count+'\').innerHTML=\'Currently password verification not matching!\';document.getElementById(\''+submit_button_id+'\').disabled=true;}else{document.getElementById(\'alert_msg_verify'+field_count+'\').innerHTML=\'\';document.getElementById(\''+submit_button_id+'\').disabled=false;}" onblur="this.value=check_chars(this.value,\''+permitted_chars[field_count]+'\',\''+field_label[field_count]+'\');" '+free_attributes[field_count][1]+' /><div id="alert_msg_verify'+field_count+'" style="color:#ff0000;font-size:0.8em;"></div></td></tr></table></td>';
		}
		// read_only
		else if(field_type[field_count]==13){
			form_html=form_html+'<td valign="top"><b>'+field_label[field_count]+required_label+'</b> <input type="text" name="'+form_field_label+'" size="'+field_size[field_count]+'" value="'+prefill_value[field_count][0]+'" onblur="this.value=check_chars(this.value,\''+permitted_chars[field_count]+'\',\''+field_label[field_count]+'\');" '+free_attributes[field_count][0]+' /></td>';
		}
		// file
		else if(field_type[field_count]==14){
			ajax_script='';
			form_html=form_html+'<td valign="top"><b>'+field_label[field_count]+required_label+'</b> <input type="file" name="'+form_field_label+'" size="'+field_size[field_count]+'" value="'+prefill_value[field_count][0]+'" onblur="this.value=check_chars(this.value,\''+permitted_chars[field_count]+'\',\''+field_label[field_count]+'\');" '+free_attributes[field_count][0]+' /></td>';
			enctype='enctype="multipart/form-data"';
		}

		// --- LINEBREAK ---
		if(linebreak[field_count]){
			form_html=form_html+'</tr></table><table cellpadding="5" style="font-family:sans-serif;font-size:0.8em;"><tr>';
		}

	}
	// --- submit_button ---
	if(submit_button_active){
		submit_button_active='';
	}else{
		submit_button_active='disabled="true"';
	}

	if(ajax_script){
		form_html='<form '+enctype+' name="jsForm" method="post" onsubmit="return checkForm();"><table cellpadding="5" style="font-family:sans-serif;font-size:0.8em;"><tr>'+form_html+'</tr></table><table width="100%"><tr><td colspan="10"><div align="right">';
		if(getCookie("voted")!="true"){
			form_html+='<input type="button" id="'+submit_button_id+'" value="'+submitbutton_txt+'" '+submit_button_active+' onclick="setTimedCookie(\'voted\',\'true\',\'300\');makePOSTRequest(ajax_script,getAllFieldVals());" />&nbsp;&nbsp;&nbsp;';
		}else{
			form_html+='<input type="button" value="You participated! Voting is disabled for 5 minutes!" disabled="true" />&nbsp;&nbsp;&nbsp;';
		}
		form_html+='<input type="button" id="'+submit_button_id+'" value="'+viewbutton_txt+'" '+submit_button_active+' onclick="makePOSTRequest(ajax_script,getSurveyViewFieldVals());" />&nbsp;&nbsp;&nbsp;</div></td></tr></table></form>';
	}else{
		form_html='<form '+enctype+' name="jsForm" method="post" onsubmit="return checkForm();"><input type="hidden" name="email_subject" value="'+email_subject+'" /><table cellpadding="5" style="font-family:sans-serif;font-size:0.8em;"><tr>'+form_html+'</tr></table><table width="100%"><tr><td colspan="10"><div align="right"><input type="submit" id="'+submit_button_id+'" value="'+submitbutton_txt+'" '+submit_button_active+' />&nbsp;&nbsp;&nbsp;</div></td></tr></table></form>';
	}

	// replace form with message
	if(mail_sent==1){
		document.getElementById('mainform').innerHTML='<div align="center" style="font-weight:bold;color:#009900; padding:10px;">'+successful_sent_message+'</div>';
	}else if(mail_sent==2){
		document.getElementById('mainform').innerHTML='<div align="center" style="font-weight:bold;color:#ff0000; padding:10px;">Problem with the submission!</div>';
	}else{
		document.getElementById('mainform').innerHTML=form_html;
	}
}

// Calendar Implementation
// USAGE: form_calendar([Date-Field ID, (opt.)], [Time-Field ID, (opt.)], [Calendar-Display-Field ID (usually <div></div>)], [preset date, (opt.)])

function form_calendar(datefield_id,timefield_id,calendarfield_id,new_date){
	var content='<table style="background-color:#ffffff;border: 1px solid #000000;""><tr><td valign="top" style="border: 1px solid #000000;">';
	if(!new_date){
		var new_date='';
	}
	if(datefield_id){
		var sdt=new Date();
		o_month=sdt.getMonth();
		o_year=sdt.getFullYear();
		if(new_date.indexOf('/')>0){
			date_in=new_date.split('/');
			if((date_in[0]-1==o_month) && (date_in[2]==o_year)){
				cday=sdt.getDate();
			}else{
				cday='0';
			}
			sdt.setDate(1);
			sdt.setYear(date_in[2]);
			sdt.setMonth(date_in[0]-1);
		}else{
			cday=sdt.getDate();
		}
		sdt.setDate(1);
		wkday=sdt.getDay();
		var mnth_name=new Array('January','February','March','April','May','June','July','August','September','October','November','December');
		var mnth_days=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
		cyear=sdt.getFullYear();
		cmonth=sdt.getMonth();
		if(!(cyear%4) && (cyear!=2000)){mnth_days[1]++;}
		if(cmonth<1){
			var prev_month=12;
			var prev_year=cyear-1;
		}else{
			var prev_month=cmonth;
			var prev_year=cyear;
		}
		if(cmonth>11){
			var next_month=1;
			var next_year=cyear+1;
		}else{
			var next_month=cmonth+2;
			var next_year=cyear;
		}
		var prev_date=prev_month+'/1/'+prev_year;
		var next_date=next_month+'/1/'+next_year;
		content=content+'<table width="125" cellpadding="0" cellspacing="3" border="0" style="font-family: \'lucida console\',\'Bitstream Vera Sans Mono\',monospaced;font-size:10px;">';
		content=content+'<tr><td style="cursor:pointer;" onclick="form_calendar(\''+datefield_id+'\',\''+timefield_id+'\',\''+calendarfield_id+'\',\''+prev_date+'\')">&lt;</td><td colspan="5" align="center">'+mnth_name[cmonth]+' '+cyear+'</td><td align="right" style="cursor:pointer;" onclick="form_calendar(\''+datefield_id+'\',\''+timefield_id+'\',\''+calendarfield_id+'\',\''+next_date+'\')">&gt;</td></tr>';
		content=content+'<tr><td style="color:#ff0000;">Su</td><td>Mo</td><td>Tu</td><td>We</td><td>Th</td><td>Fr</td><td>Sa</td></tr>';
		start=sdt.getDay();
		for(x=0;x<(mnth_days[cmonth]+wkday);x++){
			content=content+'<td align="right"';
			if(x>=wkday){
				content=content+' style="cursor:pointer;';
				if(!(x%7)){
					content=content+'color:#ff0000;';
				}
				if((x-wkday+1)==cday){
					content=content+'background-color:#cccccc;';
				}
				content=content+'" onClick="javascript:document.getElementById(\''+datefield_id+'\').value=\''+(cmonth+1)+'/'+(x-wkday+1)+'/'+cyear+'\'">'+(x-wkday+1)+'</td>';
				if(!((x+1)%7)){
					content=content+'</tr><tr>';
				}
			}else{
				content=content+'>';
			}
		}
		content=content+'</table>';
		document.getElementById(calendarfield_id).innerHTML=content;
	}
	content=content+'</td>';
	if(timefield_id){
		var apm = new Array('A.M.','P.M.');
		content=content+'<td valign="top" style="border: 1px solid #000000;"><table cellpadding="0" cellspacing="3" border="0" style="font-family: \'lucida console\',\'Bitstream Vera Sans Mono\',monospaced;font-size:10px;">';
		content=content+'<tr><td colspan="7" align="center">Time</td></tr>';
		start=sdt.getDay();
		for(x=0;x<24;x++){
			if(!x){
				y=12;
			}else{
				y=x
			}
			content=content+'<td width="12" align="right" style="cursor:pointer;';
			if((x<6)||(x>17)){
				content=content+'background-color:#cccccc;';
			}
			content=content+'" onClick="javascript:document.getElementById(\''+timefield_id+'\').value=\''+(y-13*Math.floor(x/13)+Math.floor(x/13))+':00 '+apm[Math.floor(x/12)]+'\'">'+(y-13*Math.floor(x/13)+Math.floor(x/13))+'</td>';
			if(!((x+1)%6)){
				if(x==5){
					content=content+'<td style="cursor:not-allowed;border-left: 1px solid #000000;" rowspan="2">'+apm[0]+'</td>';
				}else if(x==23){
					content=content+'</tr><tr><td colspan="7"><hr /></td>';
				}else if(x==17){
					content=content+'<td style="cursor:not-allowed;border-left: 1px solid #000000;" rowspan="2">'+apm[1]+'</td>';
				}
				content=content+'</tr><tr>';
			}
		}
		content=content+'<td align="center" colspan="2" style="border: 1px solid #000000;cursor:pointer;" onclick="javascript:var s=document.getElementById(\''+timefield_id+'\').value;if(s.length){var n=s.substring(0,s.indexOf(\':\')+1)+\'00\'+s.substring(s.indexOf(\':\')+3); document.getElementById(\''+timefield_id+'\').value=n;}">:00</td><td align="center" colspan="2" style="border: 1px solid #000000;cursor:pointer;" onclick="javascript:var s=document.getElementById(\''+timefield_id+'\').value;if(s.length){var n=s.substring(0,s.indexOf(\':\')+1)+\'15\'+s.substring(s.indexOf(\':\')+3); document.getElementById(\''+timefield_id+'\').value=n;}">:15</td><td align="center" colspan="2" style="border: 1px solid #000000;cursor:pointer;" onclick="javascript:var s=document.getElementById(\''+timefield_id+'\').value;if(s.length){var n=s.substring(0,s.indexOf(\':\')+1)+\'30\'+s.substring(s.indexOf(\':\')+3); document.getElementById(\''+timefield_id+'\').value=n;}">:30</td><td align="center" colspan="2" style="border: 1px solid #000000;cursor:pointer;" onclick="javascript:var s=document.getElementById(\''+timefield_id+'\').value; if(s.length){var n=s.substring(0,s.indexOf(\':\')+1)+\'45\'+s.substring(s.indexOf(\':\')+3); document.getElementById(\''+timefield_id+'\').value=n;}">:45</td></tr>';
		content=content+'</table></td>';
	}
	content=content+'</tr><tr><td colspan="2" align="center"><span style="font-family: \'lucida console\',\'Bitstream Vera Sans Mono\',monospaced;font-size:10px;cursor:pointer;" onclick="javascript:document.getElementById(\''+calendarfield_id+'\').innerHTML=\'\';">[close]</span></td></tr></table>';
	document.getElementById(calendarfield_id).innerHTML=content;
}

/*******************
 *                 *
 *   JS COOKIES    *
 *                 *
 *******************/

function setTimedCookie(c_name,c_value,expire_secs){
	var expiredate=new Date();
	expiredate.setDate(expiredate.getSeconds()+expire_secs);
	document.cookie=c_name+ "=" +escape(c_value)+((expire_secs==null) ? "" : ";expires="+expiredate.toGMTString());
}

function setSessionCookie(c_name,c_value){
	document.cookie=c_name+ "=" +escape(c_value);
}

function getCookie(c_name){
	if(document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if(c_start!=-1){
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if(c_end==-1){c_end=document.cookie.length;}
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return false;
}

/*******************
 *                 *
 * AJAX SITE-HIT   *
 *                 *
 *******************/

function sitehits(hit_id){
	var xmlHttp;
	try{
	  // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}catch (e){
	  // Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				//alert("Your browser does not support AJAX!"); << only on response required
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function(){
		//if(xmlHttp.readyState==4){document.myForm.time.value=xmlHttp.responseText;} << only on response required
	}
	// do not execute if hit-cookie set
	if(!getCookie(hit_id)){
		xmlHttp.open("GET","php_hitcounter.php?id="+hit_id,true);
		xmlHttp.send(null);
		setSessionCookie(hit_id,'x');
	}
}

/**************************************
 *                                    *
 * AJAX FORM SUBMISSION WITH FEEDBACK *
 *                                    *
 **************************************/

var http_request = false;
function makePOSTRequest(url, parameters) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}

	http_request.onreadystatechange = alertContents;
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}
function alertContents() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			document.getElementById('mainform').innerHTML = http_request.responseText;
		} else {
			alert('There was a problem with the request.');
		}
	}
}

/**************************
 *                        *
 * DYNAMIC UPDATE BY AJAX *
 *                        *
 *************************/

function ajaxUpdate(url,target){
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.open("GET",url,false);
	xmlhttp.send(null);
	document.getElementById(target).innerHTML=xmlhttp.responseText;
}


/*********************************************************************************
 *                                                                               *
 * CONTENT TIMER (use only for non-critical apps, client system-time dependent!) *
 * Shows/hides content segment between given times                               *
 *                                                                               *
 * PARAMETERS:                                                                   *
 * container id	(content will NOT break before/after)                            *
 * mode			('show'/'hide')                                                  *
 * active		(time/date when mode activated: 	'mm/dd/yyyy[ hh:mm AM/PM]')  *
 * incative		(time/date when mode deactivated: 	'mm/dd/yyyy[ hh:mm AM/PM]')  *
 *                                                                               *
 *********************************************************************************/

 function contentTimer(container,mode,active,inactive){
	now_date=new Date();
	in_date=new Date(active);
	out_date=new Date(inactive);
	// hide from active to inactive
	if(mode=='hide'){
		var show_hide1='none';
		var show_hide2='inline';
	// show from active to inactive
	}else{
		var show_hide1='inline';
		var show_hide2='none';
	}
	if((in_date.getTime() <= now_date.getTime()) && (out_date.getTime() > now_date.getTime())){
		document.getElementById(container).style.display=show_hide1;
	}else{
		document.getElementById(container).style.display=show_hide2;
	}
}

