﻿// JScript File
//*************** REMOVE CART ITEM ******************
function removeCartItem(itemId)
{
    $get("ctl00_cph1_hdnUpdateValue").value = itemId;
    $get("ctl00_cph1_btnUpdate").click();
}

function viewProductPicture(productID)        
{            
    window.open('viewPicture.aspx?productID=' + productID,null,'height=540,width=540,status=yes,toolbar=no,menubar=no,location=no');        
}


//**************** ADD CART ITEM *************************
function addCartItem(productId)
{
    window.document.getElementById("ctl00_cph1_hdnVal").value = productId;
    window.document.getElementById("ctl00_cph1_btnAddToCart").click();
}

//**************** FREIGHT COUNTRY *************************
function freightCountry(list)
{
    var opt = list.options[list.selectedIndex];
    var txt = document.getElementById('ctl00_cph1_txtPostCode');
    var rdo = document.getElementById('ctl00_cph1_lstFreightMethod');
    var o;
    rdo.length = 0;
    
    if (opt.text != 'Australia') 
    {
        txt.value = 'n/a';
        txt.disabled = true;
        o = new Option('Air', '1');
        rdo.options.add(o);
        o = new Option('Sea', '2');
        rdo.options.add(o);
    } else {
        txt.value = '';
        txt.disabled = false;
        o = new Option('Standard', '1');
        rdo.options.add(o);
        o = new Option('Express', '2');
        rdo.options.add(o);

    }
}

//********************** UPDATE COSTS ***********************
function updateCost(metres, price) {
    if (isNaN(metres)) {
        window.document.getElementById('lblCost').innerHTML = '$0.00';
        $get('ctl00_cph1_ucAdd_hdnQty').value = 0;
    }
    else 
    {

        var test = 2.3;
        if ((metres.length < 4) || metres.charAt(metres.length - 1) == "0" || metres.charAt(metres.length - 1) == "5") {
            window.document.getElementById('lblCost').innerHTML = formatNumber((metres * price), 2, true, false, false, '$');
            $get('ctl00_cph1_ucAdd_hdnQty').value = metres;
        }
        else {
            window.document.getElementById('lblCost').innerHTML = ' Please enter in increments of 5';
            $get('ctl00_cph1_ucAdd_hdnQty').value = 0;
        }
    }
}

// FORMAT NUMBER **********************************************************************
function formatNumber(num, decimalNum, bolLeadingZero, bolParens, bolCommas, symbol) {

    if (isNaN(parseInt(num))) return "NaN";

    var tmpNum = num;
    var iSign = num < 0 ? -1 : 1;                    // Get sign of number

    // Adjust number so only the specified number of numbers after

    // the decimal point are shown.

    tmpNum *= Math.pow(10, decimalNum);

    tmpNum = Math.round(Math.abs(tmpNum))

    tmpNum /= Math.pow(10, decimalNum);

    tmpNum *= iSign;                                                          // Re-adjust for sign

    // Create a string object to do our formatting on

    var tmpNumStr = new String(tmpNum);

    // See if we need to strip out the leading zero or not.

    if (!bolLeadingZero && num < 1 && num > -1 && num != 0)

        if (num > 0)
            tmpNumStr = tmpNumStr.substring(1, tmpNumStr.length);
        else
            tmpNumStr = "-" + tmpNumStr.substring(2, tmpNumStr.length);

    // See if we need to put in the commas
    if (bolCommas && (num >= 1000 || num <= -1000)) {
        var iStart = tmpNumStr.indexOf(".");

        if (iStart < 0)
            iStart = tmpNumStr.length;
        iStart -= 3;

        while (iStart >= 1) {
            tmpNumStr = tmpNumStr.substring(0, iStart) + "," + tmpNumStr.substring(iStart, tmpNumStr.length)
            iStart -= 3;
        }
    }

    // See if we need to use parenthesis
    if (bolParens && num < 0)
        tmpNumStr = "(" + tmpNumStr.substring(1, tmpNumStr.length) + ")";

    if (decimalNum > 0) {
        var pos = tmpNumStr.indexOf('.');

        if (pos > 0) {

            for (var i = 0; i < decimalNum - (tmpNumStr.length - (pos + 1)); i++) {
                tmpNumStr += '0';

            }

        }
        else {
            tmpNumStr += '.';

            for (var i = 0; i < decimalNum; i++) {
                tmpNumStr += '0';

            }

        }
    }

    if (symbol == '$') {
        return symbol + tmpNumStr;                     // Return our formatted string!
    }
    if (symbol == '%') {
        return tmpNumStr + symbol;
    }
    else {
        return tmpNumStr;                     // Return our formatted string!
    }
}

//SCRIPT MANAGER END
if(typeof(Sys) !== 'undefined'){
    Sys.Application.notifyScriptLoaded();
}
