/* Copyright December 14, 2009 Adapti Tech, Inc.  All Rights Reserved. */

// 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 forms;
var form;
var elem;
function validate_PDF2() {
	//confirm('validate_PDF2')
	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_PDF2_NAME
input_PDF2_NAME = function() {
	tabbing = false;
}	

var input_PDF2_NAME_error
input_PDF2_NAME_error = function() {
	document.getElementById('input_PDF2_NAME').focus();
}	

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

var input_PDF2_BUSINESS_NAME_error
input_PDF2_BUSINESS_NAME_error = function() {
	document.getElementById('input_PDF2_BUSINESS_NAME').focus();
}	

var input_PDF2_PHONE
input_PDF2_PHONE = function() {
	proxy_clear('input_PDF2_FAX_error');
	tabbing = false;
}	

var input_PDF2_PHONE_error
input_PDF2_PHONE_error = function() {
	proxy_set('input_PDF2_PHONE_error', 'input_PDF2_FAX_error');
	document.getElementById('input_PDF2_PHONE').focus();
}	

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

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

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

var input_PDF2_EVENT_NAME_error
input_PDF2_EVENT_NAME_error = function() {
	document.getElementById('input_PDF2_EVENT_NAME').focus();
}	

var input_PDF2_CERTIFIED_HOLDER
input_PDF2_CERTIFIED_HOLDER = function() {
	proxy_clear('input_PDF2_EVENT_NAME_error');
	tabbing = false;
}	

var input_PDF2_CERTIFIED_HOLDER_error
input_PDF2_CERTIFIED_HOLDER_error = function() {
	proxy_set('input_PDF2_CERTIFIED_HOLDER_error', 'input_PDF2_EVENT_NAME_error');
	document.getElementById('input_PDF2_CERTIFIED_HOLDER').focus();
}	

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

var input_PDF2_ATTENTION_error
input_PDF2_ATTENTION_error = function() {
	document.getElementById('input_PDF2_ATTENTION').focus();
}	

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

var input_PDF2_CERTIFIED_HOLDER_ADDRESS_error
input_PDF2_CERTIFIED_HOLDER_ADDRESS_error = function() {
	document.getElementById('input_PDF2_CERTIFIED_HOLDER_ADDRESS').focus();
}	

var input_PDF2_CERTIFIED_HOLDER_CITY
input_PDF2_CERTIFIED_HOLDER_CITY = function() {
	proxy_clear('input_PDF2_CERTIFIED_HOLDER_ADDRESS_error');
	tabbing = false;
}	

var input_PDF2_CERTIFIED_HOLDER_CITY_error
input_PDF2_CERTIFIED_HOLDER_CITY_error = function() {
	proxy_set('input_PDF2_CERTIFIED_HOLDER_CITY_error', 'input_PDF2_CERTIFIED_HOLDER_ADDRESS_error');
	document.getElementById('input_PDF2_CERTIFIED_HOLDER_CITY').focus();
}	

var select_PDF2_CERTIFIED_HOLDER_STATE
select_PDF2_CERTIFIED_HOLDER_STATE = function() {
	proxy_clear('input_PDF2_CERTIFIED_HOLDER_ADDRESS_error');
	tabbing = false;
}	

var select_PDF2_CERTIFIED_HOLDER_STATE_error
select_PDF2_CERTIFIED_HOLDER_STATE_error = function() {
	proxy_set('select_PDF2_CERTIFIED_HOLDER_STATE_error', 'input_PDF2_CERTIFIED_HOLDER_ADDRESS_error');
	document.getElementById('select_PDF2_CERTIFIED_HOLDER_STATE').focus();
}	

var input_PDF2_CERTIFIED_HOLDER_ZIP
input_PDF2_CERTIFIED_HOLDER_ZIP = function() {
	proxy_clear('input_PDF2_CERTIFIED_HOLDER_ADDRESS_error');
	tabbing = false;
}	

var input_PDF2_CERTIFIED_HOLDER_ZIP_error
input_PDF2_CERTIFIED_HOLDER_ZIP_error = function() {
	proxy_set('input_PDF2_CERTIFIED_HOLDER_ZIP_error', 'input_PDF2_CERTIFIED_HOLDER_ADDRESS_error');
	document.getElementById('input_PDF2_CERTIFIED_HOLDER_ZIP').focus();
}	

var input_PDF2_CERTIFIED_HOLDER_PHONE
input_PDF2_CERTIFIED_HOLDER_PHONE = function() {
	proxy_clear('input_PDF2_CERTIFIED_HOLDER_FAX_error');
	tabbing = false;
}	

var input_PDF2_CERTIFIED_HOLDER_PHONE_error
input_PDF2_CERTIFIED_HOLDER_PHONE_error = function() {
	proxy_set('input_PDF2_CERTIFIED_HOLDER_PHONE_error', 'input_PDF2_CERTIFIED_HOLDER_FAX_error');
	document.getElementById('input_PDF2_CERTIFIED_HOLDER_FAX_error').style.left = 180;
	document.getElementById('input_PDF2_CERTIFIED_HOLDER_PHONE').focus();
}	

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

var input_PDF2_CERTIFIED_HOLDER_FAX_error
input_PDF2_CERTIFIED_HOLDER_FAX_error = function() {
	// if optional then it is not required else ASAP is checked so FAX is required
	document.getElementById('input_PDF2_CERTIFIED_HOLDER_FAX_error').style.left = 180;
	if(document.getElementById('input_PDF2_CERTIFIED_HOLDER_FAX').title == "optional") {
		document.getElementById('input_PDF2_CERTIFIED_HOLDER_FAX_error').style.color = '#660000';
		var innerHTML = document.getElementById('input_PDF2_CERTIFIED_HOLDER_FAX_error').innerHTML;
		document.getElementById('input_PDF2_CERTIFIED_HOLDER_FAX_error').innerHTML = 'Optional: '+innerHTML;
		tabbing = false;
	} else {
		document.getElementById('input_PDF2_CERTIFIED_HOLDER_FAX_error').style.color = '#cc0000';
		document.getElementById('input_PDF2_CERTIFIED_HOLDER_FAX').focus();
	}
}	

function PDF2_clicked_asap (id) {
	// works with input_PDF2_CERTIFIED_HOLDER_FAX
	document.getElementById('input_PDF2_CERTIFIED_HOLDER_FAX_error').style.left = 180;
	if(document.getElementById(id).checked) 
	{ 
		document.getElementById(id).value ="yes"; 
		document.getElementById('input_PDF2_CERTIFIED_HOLDER_FAX').title =""; // clear optional
		document.getElementById('input_PDF2_CERTIFIED_HOLDER_FAX_error').innerHTML = '';
		document.getElementById('input_PDF2_CERTIFIED_HOLDER_FAX').focus(); 
	} else {
		setSessionVar(id, "no");	// not yes; need to circumvent post values
		document.getElementById(id).value ="no"; 
		document.getElementById('input_PDF2_CERTIFIED_HOLDER_FAX').title ="optional"; 
	}
}

/* 
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_PDF2_EVENT_DAYS is called by field input_PDF2_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 PDF2_get_to_date; clear the proxy error message if it had been set.

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

PDF2_get_to_date is called by input_PDF2_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_PDF2_DATE_FROM_MONTH is called by field select_PDF2_DATE_FROM_MONTH onblur via select.php; this calls PDF2_get_from_month explicit.

PDF2_get_from_month is called by select_PDF2_DATE_FROM_MONTH explicitly; if year has not been set (default), return focus to select_PDF2_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_PDF2_DATE_FROM_DAY and set session var; get the year value and set session var; set session var table_id to PDF2; clear select_PDF2_DATE_FROM_DAY list; call select_one.php that will use session vars set here to recreate the select_PDF2_DATE_FROM_DAY list according to year/month; if select_one.php returns error go to select_PDF2_DATE_FROM_MONTH_error; else rewrite span_PDF2_DATE_FROM_DAY with new select_PDF2_DATE_FROM_DAY list.  

select_PDF2_DATE_FROM_DAY is called by field select_PDF2_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_PDF2_DATE_FROM_DAY_error is called by field select_PDF2_DATE_FROM_DAY onblur via select.php; select.php returned and error; move the error to proxy.  

select_PDF2_DATE_FROM_YEAR is called by field select_PDF2_DATE_FROM_YEAR onblur via select.php; this calls PDF2_get_from_year explicit.

PDF2_get_from_year is called by select_PDF2_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_PDF2_DATE_FROM_YEAR_error is called, else a new month list is written to the page and the proxy is cleared.  

select_PDF2_DATE_FROM_YEAR_error is called by PDF2_get_from_year error; move the message to proxy.  

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

var input_PDF2_EVENT_DAYS;
input_PDF2_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_PDF2_DATE_FROM_MONTH').value;
	day = document.getElementById('select_PDF2_DATE_FROM_DAY').value;
	year = document.getElementById('select_PDF2_DATE_FROM_YEAR').value;
	days = document.getElementById('input_PDF2_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 = PDF2_get_to_date('get_to_date','days','tag');	
	}
	proxy_clear('label_PDF2_DATE_FROM_TO_error');
}	

var input_PDF2_EVENT_DAYS_error
input_PDF2_EVENT_DAYS_error = function() {
	proxy_set('input_PDF2_EVENT_DAYS_error','label_PDF2_DATE_FROM_TO_error');
	document.getElementById('input_PDF2_EVENT_DAYS').focus();
}	

function PDF2_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('PDF2_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_PDF2_DATE_TO_MONTH').value = to_date[0];
			document.getElementById('select_PDF2_DATE_TO_DAY').value = to_date[1];
			document.getElementById('select_PDF2_DATE_TO_YEAR').value = to_date[2];
			// insures values are available since disabled lists are not returned by Submit
			month = document.getElementById('select_PDF2_DATE_TO_MONTH').value;
			day = document.getElementById('select_PDF2_DATE_TO_DAY').value;
			year = document.getElementById('select_PDF2_DATE_TO_YEAR').value;
			setSessionVar('PDF2_DATE_TO_MONTH',month);
			setSessionVar('PDF2_DATE_TO_DAY',day);
			setSessionVar('PDF2_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_PDF2_DATE_FROM_MONTH
select_PDF2_DATE_FROM_MONTH = function() {
	PDF2_get_from_month();
}

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

	month = document.getElementById('select_PDF2_DATE_FROM_MONTH').value;
	setSessionVar('month',month);
	tabindex = document.getElementById('select_PDF2_DATE_FROM_DAY').tabIndex;
	setSessionVar('tabindex',tabindex);
	//year = document.getElementById('select_PDF2_DATE_FROM_YEAR').selectedIndex;
	year = document.getElementById('select_PDF2_DATE_FROM_YEAR').value;
	setSessionVar('year',year);
	setSessionVar('table_id','PDF2');
	
	// clear out old day list
	document.getElementById('span_PDF2_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_PDF2_DATE_FROM_DAY').innerHTML = xmlHttp.responseText;	
				//document.getElementById(id+'_error').innerHTML = '';	
				proxy_clear('label_PDF2_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_PDF2_DATE_FROM_MONTH_error
select_PDF2_DATE_FROM_MONTH_error = function() {
	proxy_set('select_PDF2_DATE_FROM_MONTH_error','label_PDF2_DATE_FROM_TO_error');
}

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

var select_PDF2_DATE_FROM_DAY_error
select_PDF2_DATE_FROM_DAY_error = function() {
	//document.getElementById('select_PDF2_DATE_FROM_MONTH').focus();
	proxy_set('select_PDF2_DATE_FROM_DAY_error','label_PDF2_DATE_FROM_TO_error');
}

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

var PDF2_get_from_year
PDF2_get_from_year = function() {
	//year = document.getElementById('select_PDF2_DATE_FROM_YEAR').selectedIndex;
	year = document.getElementById('select_PDF2_DATE_FROM_YEAR').value;
	setSessionVar('year',year);
	next_name = 'PDF2_DATE_FROM_MONTH';
	setSessionVar('next_name',next_name);
	//document.getElementById('select_PDF2_DATE_FROM_YEAR_error').innerHTML = '';

	tabindex = document.getElementById('select_PDF2_DATE_FROM_MONTH').tabIndex;
	// clear the old month list
	document.getElementById('span_PDF2_DATE_FROM_MONTH').innerHTML = '';	
	setSessionVar('tabindex',tabindex);
	setSessionVar('table_id','PDF2');
	//var id = 'select_PDF2_DATE_FROM_YEAR';
	var id = 'PDF2_get_from_year';
	var name = 'PDF2_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_PDF2_DATE_FROM_MONTH').innerHTML = xmlHttp.responseText;	
				//document.getElementById(id+'_error').innerHTML = '';	
				proxy_clear('label_PDF2_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_PDF2_DATE_FROM_YEAR_error
select_PDF2_DATE_FROM_YEAR_error = function() {
	proxy_set('select_PDF2_DATE_FROM_YEAR_error','label_PDF2_DATE_FROM_TO_error');
	//document.getElementById('select_PDF2_DATE_FROM_MONTH').focus();
}

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

var select_PDF2_DATE_TO_MONTH_error
select_PDF2_DATE_TO_MONTH_error = function() {
	//document.getElementById('select_PDF2_DATE_FROM_MONTH').focus();
}

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

var select_PDF2_DATE_TO_DAY_error
select_PDF2_DATE_TO_DAY_error = function() {
	//document.getElementById('select_PDF2_DATE_TO_MONTH').focus();
}

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

var select_PDF2_DATE_TO_YEAR_error
select_PDF2_DATE_TO_YEAR_error = function() {
	//document.getElementById('select_PDF2_DATE_TO_MONTH').focus();
}