/* Copyright June 23, 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 = '';
}

function valueChange(id) {
	if(id == 'input_PDF3_PRINT_NAME') {
		// compare input_PDF3_CARDHOLDER_NAME with input_PDF3_PRINTED_NAME
		var cname = document.getElementById('input_PDF3_CARDHOLDER_NAME').value;
		var pname = document.getElementById(id).value;
		if(cname != pname) {
			document.getElementById('div_PDF3_SIGNATORY_MESSAGE').style.visibility = 'visible';
			document.getElementById('input_PDF3_SIGNATORY_STREET').value = '';
			document.getElementById('input_PDF3_SIGNATORY_CITY').value = '';
			document.getElementById('select_PDF3_SIGNATORY_STATE').value = 'default';
			document.getElementById('input_PDF3_SIGNATORY_ZIP').value = '';
			document.getElementById('input_PDF3_SIGNATORY_PHONE').value = '';
		} else {
			document.getElementById('div_PDF3_SIGNATORY_MESSAGE').style.visibility = 'hidden';
			document.getElementById('input_PDF3_SIGNATORY_STREET').value = document.getElementById('input_PDF3_CARDHOLDER_STREET').value;
			document.getElementById('input_PDF3_SIGNATORY_CITY').value = document.getElementById('input_PDF3_CARDHOLDER_CITY').value;
			document.getElementById('select_PDF3_SIGNATORY_STATE').value = document.getElementById('select_PDF3_CARDHOLDER_STATE').value;
			document.getElementById('input_PDF3_SIGNATORY_ZIP').value = document.getElementById('input_PDF3_CARDHOLDER_ZIP').value;
			document.getElementById('input_PDF3_SIGNATORY_PHONE').value = document.getElementById('input_PDF3_CARDHOLDER_PHONE').value;
		}
	}
}

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

var forms;
var form;
var elem;
function validate_PDF3() {
	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_PDF3_AMOUNT
input_PDF3_AMOUNT = function() {
	var amount = Number(document.getElementById('input_PDF3_AMOUNT').value);
	var fee_rate = Number(document.getElementById('hidden_PDF3_FEE').value)/100;
	var charge = amount*fee_rate;
	var total = amount+(amount*fee_rate);
	
	document.getElementById('input_PDF3_FEE').value = charge.toFixed(2);
	document.getElementById('input_PDF3_TOTAL').value = total.toFixed(2);
	tabbing = false;	// done - allow next field
}	

// error handler for input_PDF3_AMOUNT
var input_PDF3_AMOUNT_error
input_PDF3_AMOUNT_error = function() {
	document.getElementById('input_PDF3_AMOUNT_error').style.left = 75;
	document.getElementById('input_PDF3_AMOUNT').focus();
	document.getElementById('input_PDF3_FEE').value = '';
	document.getElementById('input_PDF3_TOTAL').value = '';
}	

var select_PDF3_CARD_TYPE
select_PDF3_CARD_TYPE = function() {
	proxy_clear('select_PDF3_DEBIT_CREDIT_error');
	tabbing = false;
}	

var select_PDF3_CARD_TYPE_error
select_PDF3_CARD_TYPE_error = function() {
	proxy_set('select_PDF3_CARD_TYPE_error', 'select_PDF3_DEBIT_CREDIT_error');
	document.getElementById('select_PDF3_CARD_TYPE').focus();
}	

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

var select_PDF3_DEBIT_CREDIT_error
select_PDF3_DEBIT_CREDIT_error = function() {
	document.getElementById('select_PDF3_DEBIT_CREDIT').focus();
}	

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

var select_PDF3_EXPIRATION_MONTH_error
select_PDF3_EXPIRATION_MONTH_error = function() {
	document.getElementById('select_PDF3_EXPIRATION_MONTH').focus();
}	

var select_PDF3_EXPIRATION_YEAR
select_PDF3_EXPIRATION_YEAR = function() {
	proxy_clear('select_PDF3_EXPIRATION_MONTH_error');
	tabbing = false;
}	

var select_PDF3_EXPIRATION_YEAR_error
select_PDF3_EXPIRATION_YEAR_error = function() {
	proxy_set('select_PDF3_EXPIRATION_YEAR_error', 'select_PDF3_EXPIRATION_MONTH_error');
	document.getElementById('select_PDF3_EXPIRATION_YEAR').focus();
}	

var input_PDF3_CARD_NUMBER
input_PDF3_CARD_NUMBER = function() {
	proxy_clear('input_PDF3_SECURITY_CODE_error');
	tabbing = false;
}	

var input_PDF3_CARD_NUMBER_error
input_PDF3_CARD_NUMBER_error = function() {
	proxy_set('input_PDF3_CARD_NUMBER_error', 'input_PDF3_SECURITY_CODE_error');
	document.getElementById('input_PDF3_CARD_NUMBER').focus();
}	

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

var input_PDF3_SECURITY_CODE_error
input_PDF3_SECURITY_CODE_error = function() {
	document.getElementById('input_PDF3_SECURITY_CODE').focus();
}	

var input_PDF3_CARDHOLDER_NAME
input_PDF3_CARDHOLDER_NAME = function() {
	var name = document.getElementById('input_PDF3_CARDHOLDER_NAME').value;
	// the printed name is empty fill
	if(document.getElementById('input_PDF3_PRINT_NAME').value == '') {
		document.getElementById('input_PDF3_PRINT_NAME').value = name;
	}
	tabbing = false;
	valueChange('input_PDF3_PRINT_NAME');	// not id sent
}

var input_PDF3_CARDHOLDER_NAME_error
input_PDF3_CARDHOLDER_NAME_error = function() {
	document.getElementById('input_PDF3_CARDHOLDER_NAME').focus();
}

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

var input_PDF3_CARDHOLDER_STREET_error
input_PDF3_CARDHOLDER_STREET_error = function() {
	document.getElementById('input_PDF3_CARDHOLDER_STREET').focus();
}

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

var input_PDF3_CARDHOLDER_PHONE_error
input_PDF3_CARDHOLDER_PHONE_error = function() {
	document.getElementById('input_PDF3_CARDHOLDER_PHONE').focus();
}

var input_PDF3_CARDHOLDER_CITY
input_PDF3_CARDHOLDER_CITY = function() {
	proxy_clear('input_PDF3_CARDHOLDER_STREET_error');
	tabbing = false;
}

var input_PDF3_CARDHOLDER_CITY_error
input_PDF3_CARDHOLDER_CITY_error = function() {
	proxy_set('input_PDF3_CARDHOLDER_CITY_error', 'input_PDF3_CARDHOLDER_STREET_error');
	document.getElementById('input_PDF3_CARDHOLDER_CITY').focus();
}

var select_PDF3_CARDHOLDER_STATE
select_PDF3_CARDHOLDER_STATE = function() {
	proxy_clear('input_PDF3_CARDHOLDER_STREET_error');
	tabbing = false;
}

var select_PDF3_CARDHOLDER_STATE_error
select_PDF3_CARDHOLDER_STATE_error = function() {
	proxy_set('select_PDF3_CARDHOLDER_STATE_error', 'input_PDF3_CARDHOLDER_STREET_error');
	document.getElementById('select_PDF3_CARDHOLDER_STATE').focus();
}

var input_PDF3_CARDHOLDER_ZIP
input_PDF3_CARDHOLDER_ZIP = function() {
	proxy_clear('input_PDF3_CARDHOLDER_STREET_error');
	tabbing = false;
}

var input_PDF3_CARDHOLDER_ZIP_error
input_PDF3_CARDHOLDER_ZIP_error = function() {
	proxy_set('input_PDF3_CARDHOLDER_ZIP_error', 'input_PDF3_CARDHOLDER_STREET_error');
	document.getElementById('input_PDF3_CARDHOLDER_ZIP').focus();
}

var input_PDF3_PRINT_NAME
input_PDF3_PRINT_NAME = function() {
	tabbing = false;
	valueChange('input_PDF3_PRINT_NAME');
}

var input_PDF3_PRINT_NAME_error
input_PDF3_PRINT_NAME_error = function() {
	document.getElementById('input_PDF3_PRINT_NAME').focus();
}

var input_PDF3_SAME_ADDRESS
input_PDF3_SAME_ADDRESS = function() {
	var checked = document.getElementById('input_PDF3_SAME_ADDRESS').checked;
	if(checked) {
		document.getElementById('input_PDF3_SAME_ADDRESS').value ="yes"; 
		document.getElementById('input_PDF3_SIGNATORY_STREET').value = document.getElementById('input_PDF3_CARDHOLDER_STREET').value;
		document.getElementById('input_PDF3_SIGNATORY_CITY').value = document.getElementById('input_PDF3_CARDHOLDER_CITY').value;
		document.getElementById('select_PDF3_SIGNATORY_STATE').selectedIndex = document.getElementById('select_PDF3_CARDHOLDER_STATE').selectedIndex;
		document.getElementById('input_PDF3_SIGNATORY_ZIP').value = document.getElementById('input_PDF3_CARDHOLDER_ZIP').value;
		document.getElementById('input_PDF3_SIGNATORY_PHONE').value = document.getElementById('input_PDF3_CARDHOLDER_PHONE').value;
	} else {
		setSessionVar('input_PDF3_SAME_ADDRESS', "no");	// not yes; need to circumvent post values
		document.getElementById('input_PDF3_SAME_ADDRESS').value ="no"; 
		document.getElementById('input_PDF3_SIGNATORY_STREET').value = '';
		document.getElementById('input_PDF3_SIGNATORY_CITY').value = '';
		document.getElementById('select_PDF3_SIGNATORY_STATE').selectedIndex = 'default'
		document.getElementById('input_PDF3_SIGNATORY_ZIP').value = '';
		document.getElementById('input_PDF3_SIGNATORY_PHONE').value = '';
		
	}
}	

var input_PDF3_SAME_ADDRESS_error
	input_PDF3_SAME_ADDRESS_error = function() {
}	

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

var input_PDF3_SIGNATORY_STREET_error
input_PDF3_SIGNATORY_STREET_error = function() {
	document.getElementById('input_PDF3_SIGNATORY_STREET').focus();
}

var input_PDF3_SIGNATORY_CITY
input_PDF3_SIGNATORY_CITY = function() {
	proxy_clear('input_PDF3_SIGNATORY_STREET_error');
	tabbing = false;
}

var input_PDF3_SIGNATORY_CITY_error
input_PDF3_SIGNATORY_CITY_error = function() {
	proxy_set('input_PDF3_SIGNATORY_CITY_error', 'input_PDF3_SIGNATORY_STREET_error');
	document.getElementById('input_PDF3_SIGNATORY_CITY').focus();
}

var select_PDF3_SIGNATORY_STATE
select_PDF3_SIGNATORY_STATE = function() {
	proxy_clear('select_PDF3_SIGNATORY_STATE_error');
	tabbing = false;
}

var select_PDF3_SIGNATORY_STATE_error
select_PDF3_SIGNATORY_STATE_error = function() {
	proxy_set('select_PDF3_SIGNATORY_STATE_error', 'input_PDF3_SIGNATORY_STREET_error');
	document.getElementById('select_PDF3_SIGNATORY_STATE').focus();
}

var input_PDF3_SIGNATORY_ZIP
input_PDF3_SIGNATORY_ZIP = function() {
	proxy_clear('input_PDF3_SIGNATORY_STREET_error');
	tabbing = false;
}

var input_PDF3_SIGNATORY_ZIP_error
input_PDF3_SIGNATORY_ZIP_error = function() {
	proxy_set('input_PDF3_SIGNATORY_ZIP_error', 'input_PDF3_SIGNATORY_STREET_error');
	document.getElementById('input_PDF3_SIGNATORY_ZIP').focus();
}

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

var input_PDF3_SIGNATORY_PHONE_error
input_PDF3_SIGNATORY_PHONE_error = function() {
	document.getElementById('input_PDF3_SIGNATORY_PHONE').focus();
}

