// 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 = '';
}

function PDF14_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 PDF14_print_name() {
	var name = document.getElementById('input_PDF14_NAME').value;
	document.getElementById('input_PDF14_PRINT_NAME').value = name;
}	

var forms;
var form;
var elem;
function validate_PDF14() {
	//confirm('validate_PDF14')
	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;
				}
			}
		}
	}
}

var input_PDF14_NAME;
input_PDF14_NAME = function() {
	var name = document.getElementById('input_PDF14_NAME').value;
	// the printed name is empty fill
	if(document.getElementById('input_PDF14_PRINT_NAME').value == '') {
		document.getElementById('input_PDF14_PRINT_NAME').value = name;
	}
	tabbing = false;
}
var input_PDF14_NAME_error;
input_PDF14_NAME_error = function() {
	document.getElementById('input_PDF14_NAME').focus();
}

var input_PDF14_BUSINESS_NAME;
input_PDF14_BUSINESS_NAME = function() {
	tabbing = false;
} 
var input_PDF14_BUSINESS_NAME_error; 
input_PDF14_BUSINESS_NAME_error = function() {
	document.getElementById('input_PDF14_BUSINESS_NAME').focus();
}

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

var select_PDF14_BUSINESS_TYPE_error
select_PDF14_BUSINESS_TYPE_error = function() {
	document.getElementById('select_PDF14_BUSINESS_TYPE').focus();
}

var input_PDF14_STREET;
input_PDF14_STREET = function() {
	tabbing = false;
} 
var input_PDF14_STREET_error; 
input_PDF14_STREET_error = function() {
	document.getElementById('input_PDF14_STREET').focus();
}

/*
var input_PDF14_POC;
input_PDF14_POC = function() {
	tabbing = false;
} 
var input_PDF14_POC_error; 
input_PDF14_POC_error = function() {
	document.getElementById('input_PDF14_POC').focus();
}
*/

var input_PDF14_CITY
input_PDF14_CITY = function() {
	proxy_clear('input_PDF14_STREET_error');
	tabbing = false;
}	

var input_PDF14_CITY_error
input_PDF14_CITY_error = function() {
	proxy_set('input_PDF14_CITY_error','input_PDF14_STREET_error');
	document.getElementById('input_PDF14_CITY').focus();
}	

var select_PDF14_STATE
select_PDF14_STATE = function() {
	proxy_clear('input_PDF14_STREET_error');
	tabbing = false;
}

var select_PDF14_STATE_error
select_PDF14_STATE_error = function() {
	proxy_set('select_PDF14_STATE_error','input_PDF14_STREET_error');
	document.getElementById('select_PDF14_STATE').focus();
}

var input_PDF14_ZIP
input_PDF14_ZIP = function() {
	proxy_clear('input_PDF14_STREET_error');
	tabbing = false;
}	

var input_PDF14_ZIP_error
input_PDF14_ZIP_error = function() {
	proxy_set('input_PDF14_ZIP_error','input_PDF14_STREET_error');
	document.getElementById('input_PDF14_ZIP').focus();
}	

var input_PDF14_PHONE
input_PDF14_PHONE = function() {
	proxy_clear('input_PDF14_FAX_error');
	tabbing = false;
}	

var input_PDF14_PHONE_error
input_PDF14_PHONE_error = function() {
	proxy_set('input_PDF14_PHONE_error','input_PDF14_FAX_error');
	document.getElementById('input_PDF14_PHONE').focus();
}	

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

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


var textarea_PDF14_PRODUCT_DESCRIPTION;
textarea_PDF14_PRODUCT_DESCRIPTION = function() {
	proxy_clear('label_PDF14_EVENT_error');
	tabbing = false;
} 

var textarea_PDF14_PRODUCT_DESCRIPTION_error; 
textarea_PDF14_PRODUCT_DESCRIPTION_error = function() {
	proxy_set('textarea_PDF14_PRODUCT_DESCRIPTION_error','label_PDF14_EVENT_error');
	document.getElementById('textarea_PDF14_PRODUCT_DESCRIPTION').focus();
}

var input_PDF14_SPACES_SAT
input_PDF14_SPACES_SAT = function() {
	proxy_clear('label_PDF14_SPACES_SUN_error');
	tabbing = false;
}	

var input_PDF14_SPACES_SAT_error
input_PDF14_SPACES_SAT_error = function() {
	proxy_set('input_PDF14_SPACES_SAT_error','input_PDF14_SPACES_SUN_error');
	document.getElementById('input_PDF14_SPACES_SAT').focus();
}	

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

var input_PDF14_SPACES_SUN_error
input_PDF14_SPACES_SUN_error = function() {
	document.getElementById('input_PDF14_SPACES_SUN').focus();
}	


function PDF14_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_PDF14_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_PDF14_SIGNATURE').innerHTML ="By checking this box, in lieu of my signature, I approve of this transaction."; 
	}
}

function PDF14_checkbox_with_effect (id) {
	var name = document.getElementById(id).name;
	if(document.getElementById(id).checked) 
	{ 
		getSessionVar(name+'_yes', 'label_'+name+'_effect')	// performs innerHTML on arg 2
		setSessionVar(id, "yes");	// not yes; need to circumvent post values
		document.getElementById(id).value ="yes"; 
	} else {
		getSessionVar(name+'_no', 'label_'+name+'_effect')	
		setSessionVar(id, "no");	// not yes; need to circumvent post values
		document.getElementById(id).value ="no"; 
	}
}

/*
// test case
var input_PDF14_QUERY_EFFECT
input_PDF14_QUERY_EFFECT = function() {
	// checkbox_with_effect($name, $query, $answer, $attributes, $top) sets vars _query & _answer
	var checked = document.getElementById('input_PDF14_QUERY_EFFECT').checked;
	if(checked) {
		getSessionVar('PDF14_QUERY_EFFECT_answer', 'label_PDF14_QUERY_EFFECT');
	} else {
		getSessionVar('PDF14_QUERY_EFFECT_query', 'label_PDF14_QUERY_EFFECT');
	}
	tabbing = false;
}	

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

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

var textarea_PDF14_TEXTAREA_REQUIRED
textarea_PDF14_TEXTAREA_REQUIRED = function() {
	tabbing = false;
}
var textarea_PDF14_TEXTAREA_REQUIRED_error
textarea_PDF14_TEXTAREA_REQUIRED_error = function() {
	document.getElementById('textarea_PDF14_TEXTAREA_REQUIRED').focus();
}

*/

/*
var PDF14_clicked_signature;
function PDF14_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."; 
	}
}
*/

/* 
Lots of hair for gathering year, month, day to set end date.  Operator selects a year.  The year if current only returns remaining months else all twelve.  The month if current returns remaining days else days for that year/month (accounts for leap years).  Any out of order or non-selection will redirect the operator to the proper field, in short, the year, month, day, then days are selected in sequence.  For year, month, day, and days, two Ajax passes are performed.  The first checks the value integrity as other fields are checked, the second alters month if year, day if month, to_year/to_month/to_day if days.  

input_PDF14_EVENT_DAYS is called by field input_PDF14_EVENT_DAYS onblur via text.php; get the FROM_YEAR, MONTH, and DAY; set session vars year, month, day, and days for text.php get_to_date process; if year, month, and day supplied call PDF14_get_to_date; clear the proxy error message if it had been set.

input_PDF14_EVENT_DAYS_error is called by field input_PDF14_EVENT_DAYS onblur via text.php; text.php returned an error; send the error to proxy; set focus back to input_PDF14_EVENT_DAYS.

PDF14_get_to_date is called by input_PDF14_EVENT_DAYS explicitly; args id is set to get_to_date, name to days, and tag to tag; text.php is called via Ajax; text.php returns array of month, day, and year (to_date[0],[1],[2] respectively); 

select_PDF14_DATE_FROM_MONTH is called by field select_PDF14_DATE_FROM_MONTH onblur via select.php; this calls PDF14_get_from_month explicit.

PDF14_get_from_month is called by select_PDF14_DATE_FROM_MONTH explicitly; if year has not been set (default), return focus to select_PDF14_DATE_FROM_YEAR and send error message; set the id, name, and tag to be used by select_one.php; get the month value and set session var; get the tabindex from select_PDF14_DATE_FROM_DAY and set session var; get the year value and set session var; set session var table_id to PDF14; clear select_PDF14_DATE_FROM_DAY list; call select_one.php that will use session vars set here to recreate the select_PDF14_DATE_FROM_DAY list according to year/month; if select_one.php returns error go to select_PDF14_DATE_FROM_MONTH_error; else rewrite span_PDF14_DATE_FROM_DAY with new select_PDF14_DATE_FROM_DAY list.  

select_PDF14_DATE_FROM_DAY is called by field select_PDF14_DATE_FROM_DAY onblur via select.php; if month is not selected, change focus back to month list and send eror message (proxy), else clear tab to move to next field.  

select_PDF14_DATE_FROM_DAY_error is called by field select_PDF14_DATE_FROM_DAY onblur via select.php; select.php returned and error; move the error to proxy.  

select_PDF14_DATE_FROM_YEAR is called by field select_PDF14_DATE_FROM_YEAR onblur via select.php; this calls PDF14_get_from_year explicit.

PDF14_get_from_year is called by select_PDF14_DATE_FROM_YEAR explicitly; session vars year, next_name, tabindex, and table_id set for use by select_one.php; month list is cleared for new value returned by select_one.php; if select_one.php returns an error, error is sent and select_PDF14_DATE_FROM_YEAR_error is called, else a new month list is written to the page and the proxy is cleared.  

select_PDF14_DATE_FROM_YEAR_error is called by PDF14_get_from_year error; move the message to proxy.  

*/
var month; 
var day;
var year;
var days;
var tabindex;

var input_PDF14_EVENT_DAYS;
input_PDF14_EVENT_DAYS = function() {
	// returned from text.php without error - get year/month/day and days to get_to_date
	// calculate calendar date from to
	tabbing = false;
	month = document.getElementById('select_PDF14_DATE_FROM_MONTH').value;
	day = document.getElementById('select_PDF14_DATE_FROM_DAY').value;
	year = document.getElementById('select_PDF14_DATE_FROM_YEAR').value;
	days = document.getElementById('input_PDF14_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')) {
		// go to text.php with id as get_to_date
		var to_date = PDF14_get_to_date('get_to_date','days','tag');	
	}
	proxy_clear('label_PDF14_DATE_FROM_TO_error');
}	

var input_PDF14_EVENT_DAYS_error
input_PDF14_EVENT_DAYS_error = function() {
	proxy_set('input_PDF14_EVENT_DAYS_error','label_PDF14_DATE_FROM_TO_error');
	document.getElementById('input_PDF14_EVENT_DAYS').focus();
}	

function PDF14_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");
	//confirm('PDF14_get_to_date');
	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_PDF14_DATE_TO_MONTH').value = to_date[0];
			document.getElementById('select_PDF14_DATE_TO_DAY').value = to_date[1];
			document.getElementById('select_PDF14_DATE_TO_YEAR').value = to_date[2];
			// insures values are available since disabled lists are not returned by Submit
			month = document.getElementById('select_PDF14_DATE_TO_MONTH').value;
			day = document.getElementById('select_PDF14_DATE_TO_DAY').value;
			year = document.getElementById('select_PDF14_DATE_TO_YEAR').value;
			setSessionVar('PDF14_DATE_TO_MONTH',month);
			setSessionVar('PDF14_DATE_TO_DAY',day);
			setSessionVar('PDF14_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 select_PDF14_DATE_FROM_MONTH
select_PDF14_DATE_FROM_MONTH = function() {
	PDF14_get_from_month();
}

var PDF14_get_from_month
PDF14_get_from_month = function() {
	// make sure year has been set first
	//confirm('select_PDF14_DATE_FROM_MONTH');
	if(document.getElementById('select_PDF14_DATE_FROM_YEAR').value == 'default') {
		document.getElementById('select_PDF14_DATE_FROM_YEAR').focus();
		document.getElementById('label_PDF14_DATE_FROM_TO_error').innerHTML = "Please, select a year prior to selecting a month.";
		tabbing = false;
	}
	id = 'PDF14_get_from_month';
	name = 'PDF14_DATE_FROM_MONTH';
	tag = 'select';

	month = document.getElementById('select_PDF14_DATE_FROM_MONTH').value;
	setSessionVar('month',month);
	tabindex = document.getElementById('select_PDF14_DATE_FROM_DAY').tabIndex;
	setSessionVar('tabindex',tabindex);
	//year = document.getElementById('select_PDF14_DATE_FROM_YEAR').selectedIndex;
	year = document.getElementById('select_PDF14_DATE_FROM_YEAR').value;
	setSessionVar('year',year);
	setSessionVar('table_id','PDF14');
	
	// clear out old day list
	document.getElementById('span_PDF14_DATE_FROM_DAY').innerHTML = '';	
	//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_PDF14_DATE_FROM_DAY').innerHTML = xmlHttp.responseText;	
				//document.getElementById(id+'_error').innerHTML = '';	
				proxy_clear('label_PDF14_DATE_FROM_TO_error');
			}
		}
	}
	//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_PDF14_DATE_FROM_MONTH_error
select_PDF14_DATE_FROM_MONTH_error = function() {
	proxy_set('select_PDF14_DATE_FROM_MONTH_error','label_PDF14_DATE_FROM_TO_error');
}

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

var select_PDF14_DATE_FROM_DAY_error
select_PDF14_DATE_FROM_DAY_error = function() {
	//document.getElementById('select_PDF14_DATE_FROM_MONTH').focus();
	proxy_set('select_PDF14_DATE_FROM_DAY_error','label_PDF14_DATE_FROM_TO_error');
}

var select_PDF14_DATE_FROM_YEAR
select_PDF14_DATE_FROM_YEAR = function() {
	// select_one.php has returned no error
	PDF14_get_from_year();	
}

var PDF14_get_from_year
PDF14_get_from_year = function() {
	//year = document.getElementById('select_PDF14_DATE_FROM_YEAR').selectedIndex;
	year = document.getElementById('select_PDF14_DATE_FROM_YEAR').value;
	setSessionVar('year',year);
	next_name = 'PDF14_DATE_FROM_MONTH';
	setSessionVar('next_name',next_name);
	//document.getElementById('select_PDF14_DATE_FROM_YEAR_error').innerHTML = '';

	tabindex = document.getElementById('select_PDF14_DATE_FROM_MONTH').tabIndex;
	// clear the old month list
	document.getElementById('span_PDF14_DATE_FROM_MONTH').innerHTML = '';	
	setSessionVar('tabindex',tabindex);
	setSessionVar('table_id','PDF14');
	//var id = 'select_PDF14_DATE_FROM_YEAR';
	var id = 'PDF14_get_from_year';
	var name = 'PDF14_DATE_FROM_YEAR';
	var tag = 'select';
	
	var xmlHttp = newXmlHttp();
	var post_pairs = 'sid='+Math.random()+
		'&id='+id+
		'&name='+name+
		'&tag='+tag
	//mywindow = window.open("","_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800");
	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_PDF14_DATE_FROM_MONTH').innerHTML = xmlHttp.responseText;	
				//document.getElementById(id+'_error').innerHTML = '';	
				proxy_clear('label_PDF14_DATE_FROM_TO_error');
			}
		}
	}
	//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_PDF14_DATE_FROM_YEAR_error
select_PDF14_DATE_FROM_YEAR_error = function() {
	proxy_set('select_PDF14_DATE_FROM_YEAR_error','label_PDF14_DATE_FROM_TO_error');
	//document.getElementById('select_PDF14_DATE_FROM_MONTH').focus();
}

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

var select_PDF14_DATE_TO_MONTH_error
select_PDF14_DATE_TO_MONTH_error = function() {
	//document.getElementById('select_PDF14_DATE_FROM_MONTH').focus();
}

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

var select_PDF14_DATE_TO_DAY_error
select_PDF14_DATE_TO_DAY_error = function() {
	//document.getElementById('select_PDF14_DATE_TO_MONTH').focus();
}

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

var select_PDF14_DATE_TO_YEAR_error
select_PDF14_DATE_TO_YEAR_error = function() {
	//document.getElementById('select_PDF14_DATE_TO_MONTH').focus();
}

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

var input_PDF14_PRINT_NAME_error
input_PDF14_PRINT_NAME_error = function() {
	document.getElementById('input_PDF14_PRINT_NAME').focus();
}