/*************** ONLINE PAYMENT INPUTS ***************/
var onlinePaymentInputs = [
	'BillingInformation',
	'BillingSameAsTransaction',
	'BillingFirstName',
	'BillingLastName',
	'BillingAddress1',
	'BillingAddress2',
	'BillingAddress3',
	'BillingCity',
	'BillingState',
	'BillingZip',
	'BillingCountry',
	'BillingProvince',
	'CreditCardInformation',
	'HideCreditCardInformation',
	'CreditCardType',
	'CreditCardNumber',
	'CreditCardExpirationDate',
	'CreditCardCIVNumber'
];

/*************** SET PAYMENT TYPE ***************/
function setPaymentType(element){
	form = element.form || element;
	displayFormElementsByInputNames(
		form, 
		onlinePaymentInputs, 
		getValue(form, 'PaymentType') == 'Online'
	);
}

/*************** SAME INFORMATION INPUTS ***************/
var sameInformationInputs = {
	BillingFirstName: 'FirstName', 
	BillingLastName: 'LastName',
	BillingAddress1: 'Address1',
	BillingAddress2: 'Address2',
	BillingAddress3: 'Address3',
	BillingCity: 'City',
	BillingState: 'State',
	BillingProvince: 'Province',
	BillingZip: 'Zip',
	BillingCountry: 'Country'
};

/*************** TOGGLE SAME INFORMATION ***************/
function toggleSameInformation(element){
	var form = element.form;

	var inputs = sameInformationInputs;
	for(var each in inputs){

		var sameInput = form.elements[ inputs[each]];
		var billingInput = form.elements[each];

		if(!sameInput || !billingInput){continue; }

		if(element.checked){
			setValue(billingInput, getValue(sameInput));

			billingInput.style.backgroundColor = '#EEEEEE';
			billingInput.readOnly = true;
			billingInput.onfocus = function(){this.blur()}

			sameInput.sameElement = each;
			sameInput.onblur = function(){ 
				setValue(this.form, 
					this.sameElement, 
					getValue(this.form, this.name)
				); 
			};

		} else {
			setValue(billingInput, '');
			billingInput.style.backgroundColor = '';
			billingInput.readOnly = false;
			billingInput.onfocus = null

			sameInput.onblur = null;
		}
	}

	// execute country onchange
	executeFormElementEventHandler( form['BillingCountry'], 'change');
}

/*************** SHIPPING COUNTRY ONCHANGE ***************/
function shippingCountryOnChange(element){
	var form = element.form;
	var country = getValue(element);
	var isUSA = (country == 'United States of America');

	displayFormElements(form, 'State', isUSA);
	if(form.elements['Province']){
		displayFormElements(form, 'Province', !isUSA);
	}
	
	setPaymentInformation(form);
}

/*************** SHIPPING STATE ONCHANGE ***************/
function shippingStateOnChange(element){
	setPaymentInformation(element.form);
}

/*************** SET PAYMENT INFORMATION ***************/
function setPaymentInformation(form){
	if(!form.elements['ShippingAndHandlingAmount'] && !form.elements['SalesTax']){return;}

	// Country/State
	var country = getValue(form, 'Country');
	var state = getValue(form, 'State');

	// Amount
	var amount = parseFloat( getValue(form, "ProductAmount") );

	// Shipping
	var shipping = (window.ShippingAndHandlingRates)?ShippingAndHandlingRates[country] || ShippingAndHandlingRates['International'] : 0;
	
	// Sales Tax
	var salestaxrate =  (window.SalesTaxRates)? SalesTaxRates[state] || 0 : 0 ;
	var salestax = parseFloat( ((salestaxrate)?amount * salestaxrate:0).toFixed(2) );
	
	// Total
	var total = (amount + salestax + shipping).toFixed(2);

	// set shipping
	if( window.ShippingAndHandlingRates ){
		setValue(form, 'ShippingAndHandlingAmount', shipping);
		document.getElementById("ShippingAndHandlingAmount_Text").innerHTML = (shipping)?'$' + shipping:'';
	}
	
	// set sales tax
	if( window.SalesTaxRates ){
		setValue(form, 'SalesTax', salestax || '');
		document.getElementById("SalesTax_Text").innerHTML = (salestax)?'$' + salestax:'';
		displayFormElements(form, 'SalesTax', salestax)
	}

	// set total
	setValue(form, 'PaymentAmount', total);
	document.getElementById("PaymentAmount_Text").innerHTML = (total)?'$' + total:'';
}

/*************** BILLING COUNTRY ONCHANGE ***************/
function billingCountryOnChange(element){
	var form = element.form;
	var isUSA = (getValue(element) == 'United States of America');

	displayFormElements(form, 'BillingState', isUSA);
	if(form.elements['BillingProvince']){
		displayFormElements(form, 'BillingProvince', !isUSA);
	}
}


/*************** HIDE CREDIT CARD INFORMATION ONCLICK ***************/
function hideCreditCardInformationOnClick(element){
	displayFormElements(form, ['CreditCardType', 'CreditCardNumber', 'CreditCardCIVNumber', 'CreditCardExpirationDate'], !element.checked);
}

// init payment type
function initializePaymentType(){
	var form = document.forms[document.forms.length-1] ;

	// check inputs
	var foundInputs = [];
	for(var i=0,len=onlinePaymentInputs.length;i<len;i++){
		if(form[onlinePaymentInputs[i]]){
			foundInputs.push( onlinePaymentInputs[i] );
		}
	}
	onlinePaymentInputs = foundInputs;

	// exit if not payment type set
	if(!form.PaymentType){return; }

	// If edit mode then do not require payment
	if(getValue(form, 'ID') != '' && getValue(form, 'ID') != 0){

		// hide online payment and disable
		displayFormElementsByInputNames(
			form, 
			onlinePaymentInputs, 
			false
		);

		document.getElementsByName('PaymentType')[0].parentNode.style.display='none';
		setFormElementDisable(form, 'PaymentType', true);
		
	} else {
		setPaymentType( form );
	}


}
initializePaymentType();

