function getProductNames()
{

    var list = new Object;

    for (var i = 0; i < productlist.length; i++)
    {
        list[productlist[i][1]] = productlist[i][1];
    }
    return list;
}


function getLicenseTypes()
{

    var list = new Object;

    for (var i = 0; i < productlist.length; i++)
    {
        list[productlist[i][2]] = productlist[i][2];
    }

    return list;
}

function getPurchaseTypes(product)
{

    var list = new Object;

    for (var i = 0; i < productlist.length; i++)
    {
        if (productlist[i][1] == product)
        {
            list[productlist[i][3]] = productlist[i][3];
        }
    }
    return list;
}

function getProductVersion(product, purchasetype)
{

    var list = new Object;

    for (var i = 0; i < productlist.length; i++)
    {
        if (productlist[i][1] == product && productlist[i][3] == purchasetype)
        {
            list[productlist[i][4]] = productlist[i][4];
        }
    }
    return list;
}

function getPricePLN(product, license, purchase, version)
{
    for (var i = 0; i < productlist.length; i++)
    {
        if (productlist[i][1] == product &&
            productlist[i][2] == license &&
            productlist[i][3] == purchase &&
            productlist[i][4] == version)
        {

            return fixCents(productlist[i][5]*kurs_dolara);
        }
    }
    return 0;
}

function getPrice(product, license, purchase, version)
{
    for (var i = 0; i < productlist.length; i++)
    {
        if (productlist[i][1] == product &&
            productlist[i][2] == license &&
            productlist[i][3] == purchase &&
            productlist[i][4] == version)
        {

            return productlist[i][5];
        }
    }
    return 0;
}

function getKurs ()
{
    return kurs_dolara;
}

function getID(product, license, purchase, version)
{
    for (var i = 0; i < productlist.length; i++)
    {
        if (productlist[i][1] == product &&
            productlist[i][2] == license &&
            productlist[i][3] == purchase &&
            productlist[i][4] == version)
        {

            return productlist[i][0];
        }
    }
    return 0;
}

function getProductByID(id)
{
    for (var i = 0; i < productlist.length; i++)
    {
        if (productlist[i][0] == id)
        {
            return [productlist[i][1], productlist[i][2], productlist[i][3], productlist[i][4]];
        }
    }
    return 0;
}

// Stores products selected in format [Product Name, License Type, Purchase Type, Version]

var products = [["none", "none", "none", "none"]];
var productIDs = [0,0,0,0,0,0,0,0,0,0,0];

var productbox = document.getElementById("product1");
var licensebox = document.getElementById("license1");
var purchasebox = document.getElementById("purchase1");
var versionbox = document.getElementById("version1");
var price = document.getElementById("price1");

var gst = false;
var paymentMethod = "paynow";

function runfirst()
{
    document.getElementById("javascript_error").style.display = 'none';
}

function runlast()
{
    products = [["none", "none", "none", "none"]];
    productIDs = [0,0,0,0,0,0,0,0,0,0,0];

    for (var i = 1; i < 11; i++)
    {
        document.getElementById("purchaseList" + i).value = 0;
    }
    setAllProductOptions();
    setPurchaseType();
    setGST();
}


function populate(contact)
{
    var purchaseForm = document.forms['purchaseform'];

    if (purchaseForm.elements[contact + 'Same'].checked)
    {
        var firstName = purchaseForm.elements['purchaserContact/firstName'].value;
        var lastName = purchaseForm.elements['purchaserContact/lastName'].value;
        var email = purchaseForm.elements['purchaserContact/email'].value;
        var phone = purchaseForm.elements['purchaserContact/phone'].value;

        var organisationName = purchaseForm.elements['purchaserOrganisation/name'].value;
        var organisationAddress1 = purchaseForm.elements['purchaserOrganisation/address1'].value;
        var organisationAddress2 = purchaseForm.elements['purchaserOrganisation/address2'].value;
        var organisationCity = purchaseForm.elements['purchaserOrganisation/city'].value;
        var organisationState = purchaseForm.elements['purchaserOrganisation/state'].value;
        var organisationPostcode = purchaseForm.elements['purchaserOrganisation/postcode'].value;
        var organisationCountry = purchaseForm.elements['purchaserOrganisation/country'].value;
        var organisationTaxId = purchaseForm.elements['purchaserOrganisation/taxId'].value;

        purchaseForm.elements[contact + 'Contact/firstName'].value = firstName;
        purchaseForm.elements[contact + 'Contact/lastName'].value = lastName;
        purchaseForm.elements[contact + 'Contact/email'].value = email;
        purchaseForm.elements[contact + 'Contact/phone'].value = phone;

        purchaseForm.elements[contact + 'Organisation/name'].value = organisationName;
        purchaseForm.elements[contact + 'Organisation/address1'].value = organisationAddress1;
        purchaseForm.elements[contact + 'Organisation/address2'].value = organisationAddress2;
        purchaseForm.elements[contact + 'Organisation/city'].value = organisationCity;
        purchaseForm.elements[contact + 'Organisation/state'].value = organisationState;
        purchaseForm.elements[contact + 'Organisation/postcode'].value = organisationPostcode;
        if (organisationCountry.length > 0)
            purchaseForm.elements[contact + 'Organisation/country'].value = organisationCountry;
        purchaseForm.elements[contact + 'Organisation/taxId'].value = organisationTaxId;
    }

    if (contact == 'billing')
    {
        setGST();
    }
}

function populateBilling()
{
    var purchaseForm = document.forms['purchaseform'];

    if (document.getElementById("billingAddressSame").checked)
    {
        var name = purchaseForm.elements['technicalOrganisation/name'].value;
        var address1 = purchaseForm.elements['technicalOrganisation/address1'].value;
        var address2 = purchaseForm.elements['technicalOrganisation/address2'].value;
        var city = purchaseForm.elements['technicalOrganisation/city'].value;
        var state = purchaseForm.elements['technicalOrganisation/state'].value;
        var postcode = purchaseForm.elements['technicalOrganisation/postcode'].value;
        var country = purchaseForm.elements['technicalOrganisation/country'].value;
        var taxId = purchaseForm.elements['technicalOrganisation/taxId'].value;
        purchaseForm.elements['billingOrganisation/name'].value = name;
        purchaseForm.elements['billingOrganisation/address1'].value = address1;
        purchaseForm.elements['billingOrganisation/address2'].value = address2;
        purchaseForm.elements['billingOrganisation/city'].value = city;
        purchaseForm.elements['billingOrganisation/state'].value = state;
        purchaseForm.elements['billingOrganisation/postcode'].value = postcode;
        purchaseForm.elements['billingOrganisation/country'].value = country;
        purchaseForm.elements['billingOrganisation/taxId'].value = taxId;
    }

    setGST();
}

function selectRow()
{
    var radio = document.forms['purchaseform'].elements['paymentMethod'];
    radio[4].checked = true;
}

function toggleVersionOtherTextfield(disable)
{
    document.forms['purchaseform'].elements['otherVersionNotes'].disabled = disable;
}

function setVariables(item)
{

    getDiscount();
    getTotal();
    getGST();

    purchasebox = document.getElementById("purchase" + item);
    licensebox = document.getElementById("license" + item);
    productbox = document.getElementById("product" + item);
    versionbox = document.getElementById("version" + item);
    price = document.getElementById("price" + item)
}

function setAllProductOptions()
{
    for (var i = 1; i < 11; i++)
    {
        setProductOptions(i);
    }
}

function setProductOptions(item)
{

    products[item] = ["none", "none", "none", "none"];

    setVariables(item);

    productbox.options.length = 0;

    productbox.options[productbox.options.length] = new Option('Brak', 'none');

    var dropdown = getProductNames();
    for (var i in dropdown)
    {
        productbox.options[productbox.options.length] = new Option(dropdown[i], dropdown[i]);
    }
}

function setLicenseOptions(chosen, item)
{

    products[item] = ["none", "none", "none", "none"];
    setID(item, 0, 0, 0, 0);
    setVariables(item);

    licensebox.options.length = 0;

    licensebox.style.display = 'none';
    purchasebox.style.display = 'none';
    versionbox.style.display = 'none';
    price.style.display = 'none';

    if (chosen == "none")
    {
        licensebox.options[licensebox.options.length] = new Option('Wybierz', 'none');
        purchasebox.options[purchasebox.options.length] = new Option('Wybierz', 'none');
        versionbox.options[versionbox.options.length] = new Option('Wybierz', 'none');

    }
    else
    {
        licensebox.style.display = 'block';

        products[item] = [chosen, "none", "none", "none"];

        licensebox.options[licensebox.options.length] = new Option('Wybierz', 'none');

        var dropdown = getLicenseTypes();

        for (var i in dropdown)
        {
            licensebox.options[licensebox.options.length] = new Option(dropdown[i], dropdown[i]);
        }
    }
}

function setPurchaseOptions(chosen, item)
{

    products[item][1] = chosen;
    products[item][2] = "none";
    products[item][3] = "none";
    setID(item, 0, 0, 0, 0);

    setVariables(item);

    purchasebox.options.length = 0;

    purchasebox.style.display = 'none';
    versionbox.style.display = 'none';
    price.style.display = 'none';

    purchasebox.options[purchasebox.options.length] = new Option('Wybierz', 'none');

    if (chosen != "none")
    {
        var dropdown = getPurchaseTypes(products[item][0]);

        for (var i in dropdown)
        {
            purchasebox.options[purchasebox.options.length] = new Option(dropdown[i], dropdown[i]);
        }

        purchasebox.style.display = 'block';
    }
}

function setVersionOptions(chosen, item)
{

    products[item][2] = chosen;
    products[item][3] = "none";
    setID(item, 0, 0, 0, 0);

    setVariables(item);

    versionbox.options.length = 0;

    versionbox.style.display = 'none';
    price.style.display = 'none';

    versionbox.options[versionbox.options.length] = new Option('Wybierz', 'none');

    if (chosen != "none")
    {
        var dropdown = getProductVersion(products[item][0], chosen);

        for (var i in dropdown)
        {
            versionbox.options[versionbox.options.length] = new Option(dropdown[i], dropdown[i]);
        }

        versionbox.style.display = 'block';

        if (chosen == 'Upgrade < Sep-06')
           document.getElementById("cenqua_clover_text").style.display = '';
    }
}

function setPrice(chosen, item)
{

    products[item][3] = chosen;

    setID(item, products[item][0], products[item][1], products[item][2], products[item][3]);

    setVariables(item);

    if (chosen != "none")
    {
        price.style.display = 'block';
        price.value =  getPricePLN(products[item][0], products[item][1], products[item][2], products[item][3]) + 'zł / $' + getPrice(products[item][0], products[item][1], products[item][2], products[item][3]);
    }
    else
    {
        price.style.display = 'none';
    }

    getDiscount();
    getTotal();
    getGST();

    if (item < 10)
    {
        var row = item + 1;
        var rowid = "row" + row;
        displayRow(rowid);
    }
}

function setID(item, product, license, purchase, version)
{
    if (product != 0)
    {
        productIDs[item] = getID(product, license, purchase, version);
    }
    else
    {
        productIDs[item] = 0;
    }
    document.getElementById("purchaseList" + item).value = productIDs[item];
}


function getDiscount()
{

    var items = 0;


    for (var i = 0; i < products.length; i++)
    {
        if (products[i][3] != "none")
            items++;
    }

    if (items <= 1)
    {
        document.getElementById("discount").value = "";
        document.getElementById("discountrow").style.display = 'none';
        return 0;
    }

    if (items == 2)
    {
        document.getElementById("discount").value = "10%";
        displayRow("discountrow");
        return 0.1;
    }
    if (items == 3 || items == 4)
    {
        document.getElementById("discount").value = "12.5%";
        displayRow("discountrow");
        return 0.125;
    }
    if (items >= 5 && items < 10)
    {
        document.getElementById("discount").value = "15%";
        displayRow("discountrow");
        return 0.15;
    }
    if (items >= 10)
    {
        document.getElementById("discount").value = "20%";
        displayRow("discountrow");
        return 0.2;
    }

    return 0;
}

function getTotal()
{

    var total = 0;
    var totalPLN = 0;

    for (var i = 0; i < products.length; i++)
    {
        if (products[i][3] != "none")
        {
            var productprice = new Number(getPrice(products[i][0], products[i][1], products[i][2], products[i][3]));
            var productpricePLN = new Number(getPricePLN(products[i][0], products[i][1], products[i][2], products[i][3]));
            total = total + productprice;
            totalPLN = totalPLN + productpricePLN;
        }
    }

    var discount = new Number(getDiscount());

    total = total * (1 - discount);
    totalPLN = totalPLN * (1 - discount);

    if (gst)
    {
        total = total * 1.1;
        totalPLN = totalPLN * 1.1;
    }

    total = fixCents(total);
    totalPLN = fixCents(totalPLN);
    // The second statement below was freezing IE6 in certain instances
    // I've delayed the execution of that line by 50 milliseconds, which seems to solve the problem.
     document.getElementById("total").value = totalPLN + 'zł / $' + total;
     setTimeout(function() { document.getElementById("cardtotal").innerHTML = totalPLN + 'zł / $' + total; }, 50);
}

function displayRow(id)
{

    document.getElementById(id).style.display = '';

}

function setGST()
{
    if (document.forms['purchaseform'].elements['billingOrganisation/country'].value == "Australia")
    {
        gst = true;
        document.getElementById("gstrow").style.display = '';
        document.getElementById("gstbox").style.display = '';
    }
    else
    {
        gst = false;
        document.getElementById("gstrow").style.display = 'none';
        document.getElementById("gstbox").style.display = 'none';
    }
    setCurrencyType();
    getTotal();
    getGST();
}

function setCurrencyType()
{
    if (document.forms['purchaseform'].elements['billingOrganisation/country'].value == "Australia"
            && document.purchaseform.currency[1].checked)
    {
        document.getElementById("audreference").style.display = '';
        document.getElementById("audcreditcard").style.display = '';
        document.getElementById("audtotal").style.display = '';
        document.getElementById("othercreditcard").style.display = 'none';
    }
    else
    {
        document.getElementById("audreference").style.display = 'none';
        document.getElementById("audcreditcard").style.display = 'none';
        document.getElementById("audtotal").style.display = 'none';
        document.getElementById("othercreditcard").style.display = '';
    }
}

function getGST()
{

    if (gst)
    {

        var total = 0;

        for (var i = 0; i < products.length; i++)
        {
            if (products[i][3] != "none")
            {
                var productprice = new Number(getPrice(products[i][0], products[i][1], products[i][2], products[i][3]));
                total = total + productprice;
            }
        }

        var discount = new Number(getDiscount());

        total = total * (1 - discount) * 0.1;

        total = fixCents(total);

        document.getElementById("gstamount").innerHTML = "$" + total;
    }

}

function fixCents(strValue)
{
    dblValue = parseFloat(strValue);

    dblValue = Math.floor(dblValue * 100 + 0.50000000001);
    intCents = dblValue % 100;
    strCents = intCents.toString();
    dblValue = Math.floor(dblValue / 100).toString();
    if (intCents < 10)
        strCents = "0" + strCents;
    return (dblValue + '.' + strCents);
}

function setPurchaseType()
{

    var option = document.purchaseform.paymentMethod;

    for (i = 0; i < option.length; i++)
    {
        if (option[i].checked)
        {
            paymentMethod = option[i].value;
        }
    }

    if (paymentMethod == "quote")
    {
        document.getElementById("quotebox").style.display = '';
        document.getElementById("payment_options").style.display = 'none';
    }
    else
    {
        document.getElementById("quotebox").style.display = 'none';
        document.getElementById("payment_options").style.display = '';
        if (paymentMethod == "paynow")
        {
            document.purchaseform.payment[0].checked = true;
            document.getElementById("creditcard").style.display = '';
        }
        else
        {
            document.purchaseform.payment[1].checked = true;
            document.getElementById("creditcard").style.display = 'none';
        }
    }
}

function setPaymentOptions()
{

    var option = document.purchaseform.payment;

    for (i = 0; i < option.length; i++)
    {
        if (option[i].checked)
        {
            paymentMethod = option[i].value;
        }
    }

    if (paymentMethod == "paynow")
    {
        document.purchaseform.paymentMethod[0].checked = true;
        document.getElementById("creditcard").style.display = '';
    }
    else
    {
        document.purchaseform.paymentMethod[1].checked = true;
        document.getElementById("creditcard").style.display = 'none';
    }
}

function loadItem(id)
{
    if (id > 0)
    {
        var product = getProductByID(id);
        for (i = 1; i < productIDs.length; i++)
        {
            if (productIDs[i] == 0)
            {
                productIDs[i] = id;
                setVariables(i);
                productbox.value = product[0];
                setLicenseOptions(product[0], i);
                licensebox.value = product[1];
                setPurchaseOptions(product[1], i);
                purchasebox.value = product[2];
                setVersionOptions(product[2], i);
                versionbox.value = product[3];
                setPrice(product[3], i);
                return;
            }
        }
    }
}

function setCountry()
{
    setGST();
}

