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, pricemonthly, 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, pricemonthly, currency);
	}
}

function calculatePrice(price1, price2, price3, pricemonthly, currency)
{
	var priceEl = document.getElementById("price");
	var oldPriceEl = document.getElementById("oldPrice");
	var quantity = document.getElementById("quantity").value;
	var years = document.getElementById("years").options[document.getElementById("years").selectedIndex].text;
	
	var price = 0;
	var oldPrice = 0;

	if (years == "1 year") {
		price = price1;
	} else if (years == "2 years") {
		price = price2;
	} else if (years == "3 years") {
		price = price3;
	} else if (years == "Monthly") {
		price = pricemonthly;
	}	

	oldPrice = price * quantity;

	price = (((2*quantity) - Math.abs(quantity-2) - quantity + 2)/2)*price
			+ (((Math.abs(quantity-2) + (quantity - 2)) - Math.abs(quantity-10) - quantity + 10)/2) * (price*0.5695)
			+ (((Math.abs(quantity-10) + (quantity - 10)) - Math.abs(quantity-100) - quantity + 100)/2) * (price*0.4026)
			+ (((Math.abs(quantity-100) + (quantity - 100)) - Math.abs(quantity-500) - quantity + 500)/2) * (price*0.3385)
			+ (((Math.abs(quantity-500) + (quantity - 500)) - Math.abs(quantity-1000) - quantity + 1000)/2) * (price*0.1681);

	if (years == "Monthly") {
		priceEl.innerHTML = currency + price.toFixed(2) + "/month";	
	} else {
		priceEl.innerHTML = currency + price.toFixed(2);	
	}
}
