// contact_us.js
// Rev. 1.0 LNixon 07/31/09
// Rev. 1.1 LNixon 08/01/09 added check for blank textarea data in validate_entry()
/*
NOTE: this script is dependent on node.js and Ajax text.php, select.php, checkbox.php etc.
The script name should match the name of the table unless the table name interfers with the step identity as in the case of contact.  All field references should match fields within the table.
*/

var forms;
var form;
var elem;
function validate_contact() {
	//confirm('validate_contact')
	forms = document.forms;
	for (var i = 0; i < forms.length; i++) { 
		form = document.getElementById(forms[i].id)
		form.length
		for(var e = 0; e < form.length; e++) {
			elem = form.elements[e];
			if(elem.type == 'text') {
				//confirm(elem.value);	
				if(elem.value == '') {
					// FF hack: title='optional' prevents hidden fields from getting focus 
					if(elem.title != 'optional') {
						elem.focus();	// go to element 
						return false;
					}
				}
			}
			var en;
			var enc;
			var fe;
			if(elem.type == 'radio') {
				// ARGH!  This won't catch the last set
				//confirm(elem.name+'; '+elem.checked);
				if(!en) {
					//confirm('en empty first time');
					fe = elem;
					en = elem.name;
					enc = elem.checked;
				}
				if(en == elem.name) {
					//confirm('en == elem.name');
					enc = (enc || elem.checked);
				} else {
					//confirm('en != elem.name');
					if(!enc) {
						fe.focus(); 
						return false;	// nothing selected
					}
				}
			}
			if(elem.type == 'select-one') {
				//confirm(elem.id);	
				//confirm(elem.selectedIndex);	
				if(elem.selectedIndex == 0) {
					elem.focus();	// go to element 
					return false;
				}
			}
			if(elem.type == 'textarea') {
				//confirm(elem.id);	
				//confirm(elem.value);	
				if(elem.value == '') {
					elem.focus();	// go to element 
					return false;
				}
			}
		}
	}
}

/* Start contact fields */
var input_contact_NAME
input_contact_NAME = function() {
	var name = document.getElementById('input_contact_NAME').value;
	tabbing = false;
}

var input_contact_NAME_error
input_contact_NAME_error = function() {
	document.getElementById('input_contact_NAME').focus();
}

var input_contact_PHONE
input_contact_PHONE = function() {
	var name = document.getElementById('input_contact_PHONE').value;
	tabbing = false;
}

var input_contact_PHONE_error
input_contact_PHONE_error = function() {
	document.getElementById('input_contact_PHONE').focus();
}

var input_contact_EMAIL
input_contact_EMAIL = function() {
	var name = document.getElementById('input_contact_EMAIL').value;
	tabbing = false;
}

var input_contact_EMAIL_error
input_contact_EMAIL_error = function() {
	document.getElementById('input_contact_EMAIL').focus();
}

var textarea_contact_GENERAL_INFO
textarea_contact_GENERAL_INFO = function() {
	tabbing = false;
}

var textarea_contact_GENERAL_INFO_error
textarea_contact_GENERAL_INFO_error = function() {
	document.getElementById('textarea_contact_GENERAL_INFO').focus();
}

var select_member_MEMBER_ID
select_member_MEMBER_ID = function() {
	tabbing = false;
}

var select_member_MEMBER_ID_error
select_member_MEMBER_ID_error = function() {
	document.getElementById('select_member_MEMBER_ID').focus();
}


/* End contact fields */


// input template
// replace FIELD_1 with a table field value such as NAME
var input_contact_FIELD_1
input_contact_FIELD_1 = function() {
	var name = document.getElementById('input_contact_FIELD_1').value;
	tabbing = false;
}

var input_contact_FIELD_1_error
input_contact_FIELD_1_error = function() {
	document.getElementById('input_contact_FIELD_1').focus();
}

/* PROXY Error Positioning */
// proxy_set is used for the purpose of display error messages in a better position 
function proxy_set(from, to) {
	var innerHTML = document.getElementById(from).innerHTML;
	document.getElementById(to).innerHTML = innerHTML;
	document.getElementById(from).innerHTML = '';
}

// proxy_clear is used for clearing the error message created by the benefactor 
function proxy_clear(to) {
	document.getElementById(to).innerHTML = '';
}

var input_contact_FIELD_2
input_contact_FIELD_2 = function() {
	proxy_clear('input_contact_FIELD_3_error');
	tabbing = false;
}	

var input_contact_FIELD_2_error
input_contact_FIELD_2_error = function() {
	proxy_set('input_contact_FIELD_2_error','input_contact_FIELD_3_error');
	document.getElementById('input_contact_FIELD_2').focus();
}	

/*  End PROXY Error Positioning */

// select template such as a list of states
var select_contact_FIELD_4
select_contact_FIELD_4 = function() {
	tabbing = false;
}

var select_contact_FIELD_4_error
select_contact_FIELD_4_error = function() {
	document.getElementById('select_contact_FIELD_4').focus();
}

// input template not required such as a fax number
var input_contact_FIELD_4
input_contact_FIELD_4 = function() {
	tabbing = false;
}	

var input_contact_FIELD_4_error
input_contact_FIELD_4_error = function() {
	// not required
	//document.getElementById('input_contact_FIELD_4').focus();
	var innerHTML = document.getElementById('input_contact_FIELD_4_error').innerHTML;
	document.getElementById('input_contact_FIELD_4_error').innerHTML = 'Optional: '+innerHTML;
	tabbing = false;
}	

// test case of a query effect such as a checkbox altering the form
var input_contact_QUERY_EFFECT
input_contact_QUERY_EFFECT = function() {
	// checkbox_with_effect($name, $query, $answer, $attributes, $top) sets vars _query & _answer
	var checked = document.getElementById('input_contact_QUERY_EFFECT').checked;
	if(checked) {
		getSessionVar('contact_QUERY_EFFECT_answer', 'label_contact_QUERY_EFFECT');
	} else {
		getSessionVar('contact_QUERY_EFFECT_query', 'label_contact_QUERY_EFFECT');
	}
	tabbing = false;
}	

// test case
var input_contact_QUERY_REQUIRED
input_contact_QUERY_REQUIRED = function() {
	tabbing = false;
}
var input_contact_QUERY_REQUIRED_error
input_contact_QUERY_REQUIRED_error = function() {
	document.getElementById('input_contact_QUERY_REQUIRED').focus();
}

// test case
var textarea_contact_TEXTAREA
textarea_contact_TEXTAREA = function() {
	tabbing = false;
}
var textarea_contact_TEXTAREA_REQUIRED_error
textarea_contact_TEXTAREA_REQUIRED_error = function() {
	confirm('textarea_contact_TEXTAREA_REQUIRED - This field is not required.  Should not get here.');
}

var textarea_contact_TEXTAREA_REQUIRED
textarea_contact_TEXTAREA_REQUIRED = function() {
	tabbing = false;
}
var textarea_contact_TEXTAREA_REQUIRED_error
textarea_contact_TEXTAREA_REQUIRED_error = function() {
	document.getElementById('textarea_contact_TEXTAREA_REQUIRED').focus();
}

/* Application Specific Stuff */

var contact_clicked_signature;
/*
function contact_clicked_signature (id) {
	if(document.getElementById(id).checked) 
	{ 
		setSessionVar(id, "yes");	// not yes; need to circumvent post values
		document.getElementById(id).value ="yes"; 
		document.getElementById('label_SIGNATURE').innerHTML ="I approve of this transaction."; 
	} else {
		setSessionVar(id, "no");	// not yes; need to circumvent post values
		document.getElementById(id).value ="no"; 
		document.getElementById('label_SIGNATURE').innerHTML ="By checking this box, in lieu of my signature, I approve of this transaction."; 
	}
}
*/

var month; 
var day;
var year;
var days;
var tabindex;

var input_contact_EVENT_DAYS;
input_contact_EVENT_DAYS = function() {
	// calculate calendar date from to
	//month = parseInt(document.getElementById('select_contact_DATE_FROM_MONTH').value);
	month = document.getElementById('select_contact_DATE_FROM_MONTH').value;
	day = document.getElementById('select_contact_DATE_FROM_DAY').value;
	//year = parseInt(document.getElementById('select_contact_DATE_FROM_YEAR').value);
	year = document.getElementById('select_contact_DATE_FROM_YEAR').value;
	days = document.getElementById('input_contact_EVENT_DAYS').value;
	setSessionVar('month',month);
	setSessionVar('day',day);
	setSessionVar('year',year);
	setSessionVar('days',days);
	//confirm(month+':'+day+':'+year+'plus'+days);
	if((month != 0) && (day != 0) && (year != 'default')) {
		var to_date = contact_get_to_date('get_to_date','days','tag');
	}
	tabbing = false;
}	

function contact_get_to_date(id, name, tag) {
	//mywindow = window.open("","_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800");
	var xmlHttp = newXmlHttp();
	var post_pairs = 'sid='+Math.random()+
		'&id='+id+
		'&name='+name+
		'&tag='+tag
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			if(mywindow) mywindow.document.write(xmlHttp.responseText);
			var to_date = xmlHttp.responseText.split(",");
			//confirm(to_date);
			document.getElementById('select_contact_DATE_TO_MONTH').value = to_date[0];
			document.getElementById('select_contact_DATE_TO_DAY').value = to_date[1];
			document.getElementById('select_contact_DATE_TO_YEAR').value = to_date[2];
			// insures values are available since disabled lists are not returned by Submit
			month = document.getElementById('select_contact_DATE_TO_MONTH').value;
			day = document.getElementById('select_contact_DATE_TO_DAY').value;
			year = document.getElementById('select_contact_DATE_TO_YEAR').value;
			setSessionVar('contact_DATE_TO_MONTH',month);
			setSessionVar('contact_DATE_TO_DAY',day);
			setSessionVar('contact_DATE_TO_YEAR',year);
		}
	}
	//confirm(post_pairs);	
	xmlHttp.open("POST", "Ajax/text.php", true); 
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	xmlHttp.send(post_pairs);  	
}

var input_contact_EVENT_DAYS_error
input_contact_EVENT_DAYS_error = function() {
	document.getElementById('input_contact_EVENT_DAYS').focus();
}	

var select_contact_DATE_FROM_MONTH
select_contact_DATE_FROM_MONTH = function() {
	// make sure year has been set first
	if(document.getElementById('select_contact_DATE_FROM_YEAR').value == 'default') {
		document.getElementById('select_contact_DATE_FROM_YEAR').focus();
		document.getElementById('select_contact_DATE_FROM_YEAR_error').innerHTML = "Please, select a year prior to selecting a month.";
		tabbing = false;
	}
	id = 'select_contact_DATE_FROM_MONTH';
	name = 'contact_DATE_FROM_MONTH';
	tag = 'select';
	//month = document.getElementById('select_contact_DATE_FROM_MONTH').selectedIndex;
	month = document.getElementById('select_contact_DATE_FROM_MONTH').value;
	setSessionVar('month',month);
	tabindex = document.getElementById('select_contact_DATE_FROM_DAY').tabIndex;
	setSessionVar('tabindex',tabindex);
	//year = document.getElementById('select_contact_DATE_FROM_YEAR').selectedIndex;
	year = document.getElementById('select_contact_DATE_FROM_YEAR').value;
	setSessionVar('year',year);
	setSessionVar('table_id','contact');
	//mywindow = window.open("","_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800");
	var xmlHttp = newXmlHttp();
	var post_pairs = 'sid='+Math.random()+
		'&id='+id+
		'&name='+name+
		'&tag='+tag
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			if(mywindow) mywindow.document.write(xmlHttp.responseText);
			if(xmlHttp.responseText.indexOf("ERROR:") == 0)	{
				document.getElementById(id+'_error').innerHTML = xmlHttp.responseText.replace(/ERROR:/, "");
				eval(id+'_error()');
			} else {
				document.getElementById('span_contact_DATE_FROM_DAY').innerHTML = xmlHttp.responseText;	
				document.getElementById(id+'_error').innerHTML = '';	
			}
		}
	}
	//confirm(post_pairs);	
	xmlHttp.open("POST", "Ajax/select_one.php", true); 
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	xmlHttp.send(post_pairs);  	
	tabbing = false;
}

var select_contact_DATE_FROM_MONTH_error
select_contact_DATE_FROM_MONTH_error = function() {
	//document.getElementById('select_contact_DATE_FROM_YEAR').focus();
}

var select_contact_DATE_FROM_DAY
select_contact_DATE_FROM_DAY = function() {
	// make sure month has been set first
	if(document.getElementById('select_contact_DATE_FROM_MONTH').value == 'default') {
		document.getElementById('select_contact_DATE_FROM_MONTH').focus();
		document.getElementById('select_contact_DATE_FROM_MONTH_error').innerHTML = "Please, select a month prior to selecting a day.";
	}
	tabbing = false;
}

var select_contact_DATE_FROM_DAY_error
select_contact_DATE_FROM_DAY_error = function() {
	//document.getElementById('select_contact_DATE_FROM_MONTH').focus();
}

var select_contact_DATE_FROM_YEAR
select_contact_DATE_FROM_YEAR = function() {
	//year = document.getElementById('select_contact_DATE_FROM_YEAR').selectedIndex;
	year = document.getElementById('select_contact_DATE_FROM_YEAR').value;
	setSessionVar('year',year);
	next_name = 'contact_DATE_FROM_MONTH';
	setSessionVar('next_name',next_name);
	document.getElementById('select_contact_DATE_FROM_YEAR_error').innerHTML = '';

	tabindex = document.getElementById('select_contact_DATE_FROM_MONTH').tabIndex;
	setSessionVar('tabindex',tabindex);
	setSessionVar('table_id','contact');
	var id = 'select_contact_DATE_FROM_YEAR';
	var name = 'contact_DATE_FROM_YEAR';
	var tag = 'select';
	
	var xmlHttp = newXmlHttp();
	var post_pairs = 'sid='+Math.random()+
		'&id='+id+
		'&name='+name+
		'&tag='+tag
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			if(mywindow) mywindow.document.write(xmlHttp.responseText);
			if(xmlHttp.responseText.indexOf("ERROR:") == 0)	{
				document.getElementById(id+'_error').innerHTML = xmlHttp.responseText.replace(/ERROR:/, "");
				eval(id+'_error()');
			} else {
				document.getElementById('span_contact_DATE_FROM_MONTH').innerHTML = xmlHttp.responseText;	
				document.getElementById(id+'_error').innerHTML = '';	
			}
		}
	}
	//confirm(post_pairs);	
	xmlHttp.open("POST", "Ajax/select_one.php", true); 
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	
	xmlHttp.send(post_pairs);  	
	tabbing = false;

}

var select_contact_DATE_FROM_YEAR_error
select_contact_DATE_FROM_YEAR_error = function() {
	//document.getElementById('select_contact_DATE_FROM_MONTH').focus();
}

var select_contact_DATE_TO_MONTH
select_contact_DATE_TO_MONTH = function() {
	tabbing = false;
}

var select_contact_DATE_TO_MONTH_error
select_contact_DATE_TO_MONTH_error = function() {
	//document.getElementById('select_contact_DATE_FROM_MONTH').focus();
}

var select_contact_DATE_TO_DAY
select_contact_DATE_TO_DAY = function() {
	tabbing = false;
}

var select_contact_DATE_TO_DAY_error
select_contact_DATE_TO_DAY_error = function() {
	//document.getElementById('select_contact_DATE_TO_MONTH').focus();
}

var select_contact_DATE_TO_YEAR
select_contact_DATE_TO_YEAR = function() {
	tabbing = false;
}

var select_contact_DATE_TO_YEAR_error
select_contact_DATE_TO_YEAR_error = function() {
	//document.getElementById('select_contact_DATE_TO_MONTH').focus();
}

function contact_clicked_claim (id, label, dest) {
	if(document.getElementById(id).checked) 
	{ 
		document.getElementById(id).value ="yes"; 
		document.getElementById('div_'+dest).innerHTML ="<label id='label_"+dest+"'>"+label+"</label>"; 
	} else {
		var name = document.getElementById(id).name; 
		setSessionVar(name, "no");	// not yes; need to circumvent post values
		document.getElementById(id).value ="no"; 
		document.getElementById('div_'+dest).innerHTML =""; 
	}
}

function contact_print_name() {
	var name = document.getElementById('input_contact_NAME').value;
	document.getElementById('input_entry_PRINT_NAME').value = name;
}	

