/*******************************************************************************
** Country selection form                                                     **
*******************************************************************************/

function initialiseCountryForm() {
	var countrySelect = document.getElementById("countrySelect");
	
	if (countrySelect) {
		onCountrySelectChange(countrySelect);
	}
}

function onCountrySelectChange(countrySelect) {
	var selectedCountryCode = countrySelect.options[countrySelect.selectedIndex].value;
	
	var list = document.getElementById("distributors");
	var items = list.childNodes;
	
	for (i = 0; i < items.length; i++) {
		if (items[i].nodeName != "LI") continue;
		
		var countryCode = items[i].getAttribute("id");		
		items[i].style.display = (countryCode == selectedCountryCode ? "block" : "none");
	}
}



/*******************************************************************************
** Product form                                                               **
*******************************************************************************/

function initialiseProductForm() {
	calculateProductForm();
}

function sanitizeProductForm() {
	//sanitiseIntegerInput(document.getElementById("printDevizorQuantity"), 1, 9, 1);
	//sanitiseIntegerInput(document.getElementById("subscriptionQuantity"), 0, document.getElementById("printDevizorQuantity").value, 0);
	var printDevizorQuantity = document.getElementById("printDevizorQuantity").value;
	sanitiseIntegerInput(document.getElementById("printDevizorQuantity"), 0, 9, 0);
	sanitiseIntegerInput(document.getElementById("subscriptionQuantity"), 0, 9, 0);
}

function calculateProductForm() {
	sanitizeProductForm();
	
	var finalTotal = 0;

	// Calculate print devizor prices
	var printDevizorQuantityInput = document.getElementById("printDevizorQuantity");
	var printDevizorQuantity = printDevizorQuantityInput.value;
	
	var printDevizorUnitPrice = getUnitPrice(printDevizorQuantity, printDevizorPriceList);
	var printDevizorSubTotal = printDevizorUnitPrice * printDevizorQuantity;

	var printDevizorUnitPriceElement = document.getElementById("printDevizorUnitPrice");
	printDevizorUnitPriceElement.firstChild.nodeValue = formatCurrencyAmount(printDevizorUnitPrice, currency);

	var printDevizorSubTotalElement = document.getElementById("printDevizorSubTotal");
	printDevizorSubTotalElement.firstChild.nodeValue = formatCurrencyAmount(printDevizorSubTotal, currency);

	finalTotal += printDevizorSubTotal;

	// Calculate subscription prices
	var subscriptionQuantityInput = document.getElementById("subscriptionQuantity");
	var subscriptionQuantity = subscriptionQuantityInput.value;
	
	var subscriptionUnitPrice = getUnitPrice(subscriptionQuantity, subscriptionPriceList);
	var subscriptionSubTotal = subscriptionUnitPrice * subscriptionQuantity;

	var subscriptionUnitPriceElement = document.getElementById("subscriptionUnitPrice");
	subscriptionUnitPriceElement.firstChild.nodeValue = formatCurrencyAmount(subscriptionUnitPrice, currency);

	var subscriptionSubTotalElement = document.getElementById("subscriptionSubTotal");
	subscriptionSubTotalElement.firstChild.nodeValue = formatCurrencyAmount(subscriptionSubTotal, currency);

	finalTotal += subscriptionSubTotal;

	// Calculate boxed shipping prices
	var boxedDeliveryYesRadio = document.getElementById("boxedDeliveryYes");
	if (boxedDeliveryYesRadio != null) {
		var boxedDelivery = boxedDeliveryYesRadio.checked;
			
		var boxedDeliveryUnitPrice = getUnitPrice(printDevizorQuantity, boxedDeliveryPriceList);
		var boxedDeliverySubTotal = boxedDelivery ? (boxedDeliveryUnitPrice) : 0;
	
		var boxedDeliveryUnitPriceElement = document.getElementById("boxedDeliveryUnitPrice");
		boxedDeliveryUnitPriceElement.firstChild.nodeValue = formatCurrencyAmount(boxedDeliveryUnitPrice, currency);
	
		var boxedDeliverySubTotalElement = document.getElementById("boxedDeliverySubTotal");
		boxedDeliverySubTotalElement.firstChild.nodeValue = formatCurrencyAmount(boxedDeliverySubTotal, currency);
	
		finalTotal += boxedDeliverySubTotal;
	}

	// Display final total
	var finalTotalElement = document.getElementById("finalTotal");
	finalTotalElement.firstChild.nodeValue = formatCurrencyAmount(finalTotal, currency);
}

function getUnitPrice(quantity, priceList) {
	var i = 0;
	
	while (quantity > priceList[i].quantity && i < priceList.length - 1) {
		i++;
	}

	return priceList[i].unitPrice;
}



/*******************************************************************************
** Customer detail form                                                       **
*******************************************************************************/

function initialiseCustomerDetailsForm() {
	var deliverToInvoiceAddressCheckbox = document.getElementById("deliverToInvoiceAddress");
	if (deliverToInvoiceAddressCheckbox) {
		onDeliverToInvoiceAddressCheckboxChange(deliverToInvoiceAddressCheckbox);
	}
	
	var refererRadio = document.getElementById("refererOther");
	if(refererRadio) {
		onRefererRadioChange(refererRadio);
	}
}

function onDeliverToInvoiceAddressCheckboxChange(deliverToInvoiceAddressCheckbox) {
	var deliverToInvoiceAddress = deliverToInvoiceAddressCheckbox.checked;
	var deliveryAddressElement = document.getElementById("deliveryAddress");

	deliveryAddressElement.style.display = deliverToInvoiceAddress ? "none" : "block";
}

function onRefererRadioChange(refererRadio) {
	if(refererRadio == "undefined" || refererRadio == null) {
		refererRadio = document.getElementById("refererOther");
	}
	
	var refererSelected = refererRadio.checked;
	var refererOtherElement = document.getElementById("refererOtherInputDiv");

	refererOtherElement.style.display = refererSelected ? "block" : "none";
}


/*******************************************************************************
** Order summary form                                                         **
*******************************************************************************/

function initialiseOrderSummaryForm() {
}








/*******************************************************************************
** Helper functions                                                           **
*******************************************************************************/

function formatCurrencyAmount(amount, currency) {
	var	currencySymbol = "£";
	var	decimalSeparator = ".";
	var	thousandsSeparator = ",";
	
	switch(currency) {
		case "EUR":
			currencySymbol = "EUR ";
			decimalSeparator = ".";
			thousandsSeparator = ",";
		break;
		
		case "USD":
			currencySymbol = "US$ ";
			decimalSeparator = ".";
			thousandsSeparator = ",";
		break;
	}

	return currencySymbol + numberFormat(amount, 2, decimalSeparator, thousandsSeparator);
}


function numberFormat(amount, precision, decimalSeparator, thousandsSeparator) {
	amount = parseFloat(amount);
	if (isNaN(amount)) amount = 0;

	if (amount === null) amount = 0;
	if (precision === null) precision = 2;
	if (decimalSeparator === null) decimalSeparator = '.';
	if (thousandsSeparator === null) thousandsSeparator = ',';

	var result = "";

	var whole = Math.floor(amount);
	var part = Math.round((amount - whole) * Math.pow(10, precision));

	var wholeStr = String(whole);
	for (i = 0; i < wholeStr.length; i++) {
		if (i % 3 == 0 && i > 0) {
			result = thousandsSeparator + result;
		}

		var index = wholeStr.length - (i + 1);
		result = wholeStr.charAt(index) + result;		
	}
	
	if (precision > 0) {
		result += decimalSeparator + (part < 10 ? "0" : "") + part;
	}
	
	return result;
}


function sanitiseIntegerInput(input, min, max, defaultValue) {
	var value = parseInt(input.value);
	
	if (isNaN(value)) value = 0;
	
	if (min !== null) value = Math.max(value, min);
	if (max !== null) value = Math.min(value, max);

	input.value = value;
}