var debug = false;

function doClickOnEnter(buttonName, e) {
//the purpose of this function is to allow the enter key to 
//point to the correct button to click.
	var	key;
	
	if (window.event)
		key	= window.event.keyCode;		//IE
	else
		key	= e.which;	   //firefox

	if (key	== 13)
	{
		//Get the button the user wants	to have	clicked
		var	btn	= document.getElementById(buttonName);
		if (btn	!= null)
		{ //If we find the button click	it
			btn.click();
			event.keyCode =	0
		}
	}
}

function formatToCurrencyString(amount, vatString)
{	
	var result = "FREE";
	var addPoint = true;
	var zeros = 0;
	var count;
	
	if (amount > 0)
	{
		result = amount.toString();
		
		for (count = 0; count < result.length; count++)
		{
			if (result.charAt(count) == ".")
			{
				addPoint = false;
				zeros = result.length - count - 1;
				
				if (zeros >= 2)
				{
					zeros = 0;
				}
				
				break;
			}
		}

		if (addPoint)
		{
			result += '.00';
		}
		else
		{
			for (count = 0; count < zeros; count++)
			{
				result += '0';
			}
		}

		result = '&pound;' + result + ' ' + vatString;
	}
	
	return result;
}

function bindTotalPrice(total)
{
	var productID = getProductID();
	var id = 'net' + productID;
	var netControl = getControlByID(id);
	id = 'gross' + productID;
	var grossControl = getControlByID(id);
	
	if (netControl != null)
		netControl.innerHTML = formatToCurrencyString(total, '<span>ex VAT</span>');
	
	if (total > 0)
	{
		var vatRate = getVatRate();
		var vat = (total / 100) * vatRate;
		
		vat = vat.toFixed(2);
		
		var temp = parseFloat(total) + parseFloat(vat)
		
		if (grossControl != null)
			grossControl.innerHTML = formatToCurrencyString(temp.toFixed(2), '<span>inc VAT</span>');
	}
	else
	{
		if (grossControl != null)
			grossControl.innerHTML = '';
	}	
}

function getProductID()
{
	var result = document.getElementById('productid').value;
	
	return result;
}

function getStockCode()
{
	var result = '';
	var stockCodeControl = document.getElementById('stockcode')
	
	if (stockCodeControl != null)
		result = stockCodeControl.value;
	
	return result;
}

function getControlByID(id)
{
	return document.getElementById(id);
}

function getControlByName(name)
{
	return document.getElementsByName(name);
}

function getQuantity()
{
	return getSelectControlValue('quantity', 1);
}

function getSelectControlValue(id, defaultValue)
{
	var control = getControlByID(id);
	var result = (control == null) ? defaultValue : control.options[control.selectedIndex].value;
	
	if (debug)
		alert(id + ' = ' + result);
		
	var selectedID = 'selected' + id;
	var selectedControl = getControlByID(selectedID);
	
	if (selectedControl == null)
		alert('No ' + selectedID);
	else
		if (debug)
			alert(selectedID + ' = ' + result);
		
	selectedControl.value = result;
	
	return result;
}

function getRadioControlValue(id)
{
	var result = 0;
	var control = getControlByName(id);
	
	for (var x = 0; x < control.length; x ++)
	{
		if (control[x].checked)
			result = control[x].value;
	}

	var selectedID = 'selected' + id;
	var selectedControl = getControlByID(selectedID);

	if (selectedControl == null)
		alert('No ' + selectedID);
		
	selectedControl.value = result;
	
	return result;
}

function isRadioControlSelected(id)
{
	var result = false;
	var control = getControlByName(id);
	
	for (var x = 0; x < control.length; x ++)
	{
		result = (control[x].checked);
		
		if (result)
			break;
	}
	
	return result;
}

function getVatRate()
{
	var result = 17.5;
	var stockCode = getStockCode();
	
	switch (stockCode)
	{
		case "013614" :
		case "023614" :
		case "219214" :
		case "219215" :
		case "219025" :
		case "219026" :
		case "219027" :
		case "219028" :
		case "219216" :
			result = 0;
			break;

		case "219028" :
			result = 1.75;
			break;
		
		default :
			result = 17.5;
			break;
	}
	
	return result;
}

function validateConfiguration()
{
	var result = true;
	var error = "Please complete your selections:\r\n";
	var controls;
	
	controls = getControlByName('sagecover');
	if (controls != null && controls.length > 0)
	{
		if (!isRadioControlSelected('sagecover'))
		{
			result = false;
			error += "\r\nSageCover";
		}
	}
	
	controls = getControlByName('delivery');
	if (controls != null && controls.length > 0)
	{
		if (!isRadioControlSelected('delivery'))
		{
			result = false;
			error += "\r\nDelivery method";
		}
	}
	
	controls = getControlByID('service');
	if (controls != null)
	{
		if (controls.value.trim().length == 0)
		{
			result = false;
			error += "\r\nEnter details of the software or service that you intend to provide to Sage customers";
		}
	}
	
	controls = getControlByID('terms');
	if (controls != null)
	{
		if (!controls.checked)
		{
			result = false;
			error += "\r\nConfirm you have read and agree to the Sage Developers Programme Agreement";
		}
	}
	
	if (!result)
	{
		alert(error);
	}
	
	return result;
}

function onclickDelivery()
{
	var method = getRadioControlValue('delivery');

	if (method == "downonly")
		message = "Follow the instructions after the checkout page to download this software.";
	else if (method == "downship")
		message = "Follow the instructions after the checkout page to download this software.\r\n\r\nSince you've also requested a boxed copy of the software, we'll send it to you in the post (a carriage charge will apply).";
	else if (method == "shiponly")
		message = "We'll send your software to you in the post, as requested (a carriage charge will apply).";
	
	if (message.length > 0)
	{
		alert(message);
	}
}
