﻿
function ajaxLoadDivTag(pageToLoad, pageToRefresh, postData, loader)
{        
    loader.post(pageToLoad, pageToRefresh, postData);
}

function checkDiscountCode()
{
    var discountMessage = document.getElementById("message");
    
    var quantity = document.getElementById("txtQuantity");
    
    var discountCode = document.getElementById("txtDiscountCode").value; 
    var check = document.getElementById("txtCheck");
    var price = document.getElementById("txtPrice");
    var priceDiv = document.getElementById("price");
    var discountField = document.getElementById("txtDiscount");
    var productCode = document.getElementById("txtProductCode");
    
    var discountLoader = new ajaxLoader();
    var quantityVal = parseInt(quantity.value, 10);
    
    if (discountMessage && discountCode.length >= 5 && quantity.value != "" && !isNaN(quantityVal))
    {
        var postData = null;
        
        postData = "discountCode=" + discountCode + "&quantity=" + quantityVal + "&productCode=" + productCode.value + "&price=" + price.value;

        //discountMessage.style.display = 'block';
        discountMessage.style.visibility = 'visible';
           
        discountLoader.onLoadComplete = function()
        {   
            discountMessage.innerHTML = "";
            
            var xmlResponse = discountLoader.responseHTML;
            
            // code for IE
            if (window.ActiveXObject)
            {
                var doc=new ActiveXObject("Microsoft.XMLDOM");
                doc.async="false";
                doc.loadXML(xmlResponse);
            }
            // code for Mozilla, Firefox, Opera, etc.
            else
            {
                var parser=new DOMParser();
                var doc=parser.parseFromString(xmlResponse,"text/xml");
            }

            var x=doc.documentElement;

            var discount = x.childNodes[0].childNodes[0].nodeValue;
            var message = x.childNodes[1].childNodes[0].nodeValue;
            var checkValue = x.childNodes[2].childNodes[0].nodeValue;
            
            discountField.value = Math.round(discount);
            priceDiv.innerHTML = (price.value - Math.round(discount)).toFixed(2);
            check.value = checkValue;
            discountMessage.innerHTML = message;
            
            calculateAmount(Math.round(discount));
           switch(checkValue)
            {
                case '-1':
                        document.getElementById("quantity").style.color = errorColor;
                        document.getElementById("discount").style.color = errorColor;
                        
                        break;
                case '1':
                       discountMessage.style.color = validColor;
                       document.getElementById("discount").style.color = validColor;
                    break;
                    
                default:
                        document.getElementById("discount").style.color = errorColor;
                    break;
            }
            
        };
        
        ajaxLoadDivTag("response.aspx", null, postData, discountLoader);
    }
    else if (discountCode.length != 0 && quantity.value != "" && !isNaN(quantityVal))
    {
        discountMessage.innerHTML = "Discount Code Invalid";
        document.getElementById("discount").style.color = errorColor;
        discountField.value = 0;
        priceDiv.innerHTML = price.value;
        check.value = 0;
        calculateAmount(0);
    }
    else
    {
        discountMessage.innerHTML = "&nbsp;";
        document.getElementById("discount").style.color = defaultColor;
        discountField.value = 0;
        priceDiv.innerHTML = price.value;
        check.value = 1;
        calculateAmount(0);
    }
    discountMessage.style.color = errorColor;
}

function calculateAmount(discount)
{
    var quantityInput = document.getElementById("txtQuantity");
    var intValue = parseInt(quantityInput.value, 10);
    var quantityText = document.getElementById("quantity");
    var amount = document.getElementById("amount");
    var amountText = document.getElementById("txtAmount");
    var price = Number(document.getElementById("txtPrice").value);
    
    var check = true;
    
    if (isNaN(intValue))
    {
      // clear text box
      quantityInput.value = "";
      check = false;
    }
   // if this is an integer
    else
    {
      switch (true)
         {
         case (intValue == 0) :
            // clear text box
            quantityInput.value = "";
            check = false;
            break;
         case (intValue > 0) :
            // put the parsed integer value in the text box
            quantityInput.value = intValue.toString();
            break;
         case (intValue < 0) :
            // put the positive parsed integer value in the text box
            quantityInput.value = (-1 * intValue).toString();
            break;
         }    
    }

    if (check)
    {
        var amountValue = ((quantityInput.value) * (price-discount)).toFixed(2);
        amount.innerHTML = "<b>&pound;" + (amountValue) + "</b>";
        amountText.value = amountValue;
        quantityText.style.color = defaultColor;
    }
    else
    {
        amount.innerHTML = "";
        quantityText.style.color = errorColor;
        amountText.value = "";
        quantityInput.value = "";
    }
}

function singleLicense()
{
    var price = document.getElementById("txtPrice");
    var priceDiv = document.getElementById("price");
    var license = document.getElementById("txtLicense");
    var productCode = document.getElementById("txtProductCode");

    price.value = 14.99;
    priceDiv.innerHTML = 14.99;
    license.value = "Single User License";
    productCode.value = "Blocker14.99";

    checkDiscountCode();
}

function threeLicense()
{
    var price = document.getElementById("txtPrice");
    var priceDiv = document.getElementById("price");
    var license = document.getElementById("txtLicense");
    var productCode = document.getElementById("txtProductCode");

    price.value = 19.99;
    priceDiv.innerHTML = 19.99;
    license.value = "3 User License";
    productCode.value = "Blocker29.99";
    
    checkDiscountCode();
}

function orderHome()
{
    var homeBlockerVersion = document.getElementById("rdoThree");
    var license = 0;
    var productCode = "Blocker14.99";
    
    if (homeBlockerVersion.checked)
    {
       license = 1;
       productCode = "Blocker29.99";
    }
    
    window.open('orderDetails.aspx?blockerType=Home&license=' + license + '&productCode=' + productCode, '_self', null);
}