var f = '';

function toggle(elID)
{
	el = document.getElementById(elID);
	if (el.style.display == "none")
		el.style.display = "block";
	else
		el.style.display = "none";
}

function renewal(type)
{
	f = '';
	ap = 'LFS1RjGrBh';
	
	if (document.getElementById('year1').checked)
		f = document.getElementById('year1').value;
	else if (document.getElementById('year2').checked)
		f = document.getElementById('year2').value;
	else if (document.getElementById('year3').checked)
		f = document.getElementById('year3').value;
	
	window.location = "/single/renewal.php?f=" + f + "&ap=" + ap;
}

function calculateApPrice(price)
{
	var apPriceEl = document.getElementById('ap-price');
	var currency = '$';
	apPriceEl.innerHTML = currency + (parseFloat(price) + parseFloat(29.95)).toFixed(2);
}

function validateForm(minimumQuantity, maximumQuantity)
{
	var quantity = document.getElementById("quantity").value;
	var years = document.getElementById("years").options[document.getElementById("years").selectedIndex].value;
	var errorEl = document.getElementById("errormessage");
	
	if (quantity == "" || parseInt(quantity) != quantity-0)
	{
		errorEl.innerHTML = "Please select a quantity.";
		errorEl.style.display = "block";
	}
	else if (quantity < minimumQuantity)
	{
		errorEl.innerHTML = "Please select a quantity of " + minimumQuantity + " or more.";
		errorEl.style.display = "block";
	}
	else if (quantity > maximumQuantity)
	{
		errorEl.innerHTML = "Please contact our sales office <span style='color: red'>(sales@lavasoft.com)</span> for quantities above " + maximumQuantity + ".";
		errorEl.style.display = "block";
	}
	else if (years < 1 || years > 3)
	{
		errorEl.innerHTML = "Please select the number of years.";
		errorEl.style.display = "block";
	}
	else
	{
		var f = "";
		 
		if (years == 1)
			f = "Bb734vFF9";
		else if (years == 2)
			f = "1Opp2p11M";
		else
			f = "a8h99naA1";
		
		window.location = "/single/purchase.php?f=" + f + "&q=" + parseInt(quantity);
		errorEl.innerHTML = "";
		errorEl.style.display = "none";
	}
}

function validateQuantity(minimumQuantity, maximumQuantity, price1, price2, price3, currency)
{
	var quantity = document.getElementById("quantity").value;
	var priceEl = document.getElementById("price");
	var oldPriceEl = document.getElementById("oldPrice");
	var errorEl = document.getElementById("errormessage");
	var submitEl = document.getElementById("submit");
	
	if (quantity == "" || parseInt(quantity) != quantity-0) {
		priceEl.innerHTML = "";
		oldPriceEl.innerHTML = "";
		errorEl.innerHTML = "Please select a quantity.";
		errorEl.style.display = "block";
		submitEl.disabled = true;
	} else if (quantity < minimumQuantity) {
		priceEl.innerHTML = "";
		oldPriceEl.innerHTML = "";
		errorEl.innerHTML = "Please select a quantity of " + minimumQuantity + " or more.";
		errorEl.style.display = "block";
		submitEl.disabled = true;
	} else if (quantity > maximumQuantity) {
		priceEl.innerHTML = "";
		oldPriceEl.innerHTML = "";
		errorEl.innerHTML = "Please contact our sales office <a href='mailto:sales@lavasoft.com' title='E-mail sales@lavasoft.com'>(sales@lavasoft.com)</a> for quantities above " + maximumQuantity + ".";
		errorEl.style.display = "block";
		submitEl.disabled = true;
	} else {
		errorEl.innerHTML = "";
		errorEl.style.display = "none";
		submitEl.disabled = false;
		calculatePrice(price1, price2, price3, currency);
	}
}

function calculatePrice(price1, price2, price3, currency)
{
	var quantity = document.getElementById("quantity").value;
	var years = document.getElementById("years").options[document.getElementById("years").selectedIndex].text;

	var price = getBusinessEditionPrice(parseInt(years), quantity, currency);
	
	document.getElementById("price").innerHTML = currency + price.toFixed(0); // 0 Decimals
}


function getBusinessEditionPrice(years, quantity, currency)
{
	var prices = [
		{"quantity":1, "year":[
				{"us":39.95, "gb":24.95},
				{"us":54.95, "gb":39.95},
				{"us":69.95, "gb":44.95}
		]},
		/* XXX No reson to include these in between here, actually 1 quantity is useless too. */
		{"quantity":10, "year":[
				{"us":22.00, "gb":14.00},
				{"us":35.00, "gb":20.50},
				{"us":40.00, "gb":25.00}
		]},
		{"quantity":11, "year":[
				{"us":20.95, "gb":12.95},
				{"us":31.95, "gb":19.45},
				{"us":38.95, "gb":23.95}
		]},
		{"quantity":26, "year":[
				{"us":19.95, "gb":11.95},
				{"us":28.95, "gb":18.45},
				{"us":35.95, "gb":21.95}
		]},
		{"quantity":51, "year":[
				{"us":18.95, "gb":10.95},
				{"us":25.95, "gb":17.45},
				{"us":32.95, "gb":19.95}
		]},
		{"quantity":101, "year":[
				{"us":16.95, "gb":9.95},
				{"us":22.95, "gb":15.45},
				{"us":28.95, "gb":17.95}
		]},
		{"quantity":201, "year":[
				{"us":14.95, "gb":8.95},
				{"us":19.95, "gb":13.45},
				{"us":24.95, "gb":15.95}
		]},
		{"quantity":501, "year":[
				{"us":12.95, "gb":7.95},
				{"us":16.95, "gb":11.45},
				{"us":20.95, "gb":13.95}
		]}
	];

	var price = 0;
	for (var i = 0; i < prices.length; i++) {
		if (prices[i].quantity > quantity) break;
		price = (currency == '£') ? prices[i].year[years-1].gb : prices[i].year[years-1].us;
	}

	return price * quantity;
}

