function getHTTPObject() 
{
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) { 
			try { 
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	@else
		xmlhttp = false;
	@end @*/
	
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

function formatPrice(price) {
    var cent = "" + Math.floor(price % 100);
    if (cent.length == 1) {
        cent = "0" + cent;
    }
    return Math.floor(price / 100) + "." + cent;
}

function setPrice(price, couponPercent) {
    price = price * (100 - couponPercent) / 100;

    document.getElementById("total").innerHTML = "EUR " + formatPrice(price);
    
    for (currency in currencies) {
        document.getElementById("total-" + currency).innerHTML = formatPrice(price * currencies[currency]);
    }
}

function updateTotal() {
    var auroraCount = document.getElementById("prod-count-1").value;
    if (auroraCount < 1) {
        document.getElementById("prod-count-1").value = 1;
        auroraCount = 1;
    }
    var coupon = document.getElementById("coupon").value;
    var eduopt = document.getElementById("eduopt");
    var price = auroraCount * 1500;
    if (eduopt.checked) {
        price = price * 0.8;
    }
    if (coupon.length) {
        var http = getHTTPObject();
        http.open("POST", "/sales/coupon.php", true);
        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http.onreadystatechange = function () {
            if (http.readyState == 4) {
                var coupons = eval(http.responseText);
                if (coupons.length) {
                    var percent = coupons[0]["percent_off"];
                    setPrice(price, percent);
                }
                else {
                    setPrice(price, 0);
                }
            }
        }
        http.send("coupon=" + coupon);        
    }
    else {
        setPrice(price, 0);
    }
}

function toggleEdu(checkbox) {
  var edudiv = document.getElementById("edu-details");
  if (checkbox.checked) {
    edudiv.style.display = "block";
  }
  else {
    edudiv.style.display = "none";
  }
  updateTotal();
}
