/* Generic */
function fmtCurr(num) {
    // US & UK - 1,234.56
    // EU - 1.234,56
    // JP - 1,234
    var thou = ',';
    var dec = '.';
    if (region == "EU") {
        thou = '.';
        dec = ',';
    }
    
    if (isNaN(num)) num = "0";
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    if (region == "JP" && cents > 50) num += 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10) cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + thou + num.substring(num.length - (4 * i + 3));
    if (region == "JP") {
        return (symbol + num);
    }
    else {
        return (symbol + num + dec + cents);
    }
}

/* Step 1 */
function toggleStep1H2() {
    h2 = document.getElementById('h2step');
    els = document.getElementsByName("product");
    if (els[els.length - 1].checked == true) {
        h2.innerHTML = h2.innerHTML.replace("7", "8");
    } else {
        h2.innerHTML = h2.innerHTML.replace("8", "7");
    }
}

/* Step 3 */
function act24x7() {
    if (document.getElementById('rbStep3Row1') != null) {
        if (document.getElementById('rbStep3Row1').checked == true) {
            document.getElementById('chk24x7').disabled = true;
            document.getElementById('chk24x7').checked = false;
            //document.getElementById('lbl24x7').style.color = "#AAA";
        }
        else {
            document.getElementById('chk24x7').disabled = false;
            //document.getElementById('lbl24x7').style.color = "#000";
        }
    }
}

/* Step 4 */
function initEAS() {
    qty = document.getElementById('txtEAS').value;
    if (qty > 0) chgEAS(qty);
    actEAS();
}
function actEAS() {
    if (document.getElementById('rbStep4Row1').checked == true) {
        document.getElementById('txtEAS').disabled = true;
        document.getElementById('eas').style.color = "#AAA";
        try { document.getElementById('csEAS').style.color = "#AAA"; } catch(e) {}
    }
    else if (document.getElementById('rbStep4Row2').checked == true) {
        document.getElementById('txtEAS').disabled = false;
        document.getElementById('txtEAS').focus();
        document.getElementById('eas').style.color = "#000";
        try { document.getElementById('csEAS').style.color = "#000"; } catch(e) {}
    }    
}

function chgEAS(qty) {
    qty = parseInt(qty);
    if (qty > userCnt) {
        qty = userCnt;
        document.getElementById('txtEAS').value = userCnt;
    }
    if (qty < 1) {
        qty = 1;
        document.getElementById('txtEAS').value = 1;
    }
    if (isNaN(qty)) {
        document.getElementById('cost').innerHTML = '';
        return;
    }
    tmp = qty * rate;
    if (isNaN(tmp)) { return; }
    document.getElementById('cost').innerHTML = '[Add ' + fmtCurr(tmp) + ']';
}

/* Step 5 */
function toggleAvChecks(checker) {
    sym = document.getElementById('ckStep5sym');
    bd = document.getElementById('ckStep5bd');
    cav = document.getElementById('ckStep5cav');
    zh = document.getElementById('ckStep5zh');
    no = document.getElementById('ckStep5no');
    zhc = document.getElementById('spZh');
    zhddl = document.getElementById('ddlStep5zh');
    cavddl = document.getElementById('ddlStep5cav');

    sym.disabled = bd.checked;
    bd.disabled = sym.checked;
    zh.disabled = cav.checked;

    if (cav.checked == true) {
        zh.checked = true;
        zhc.innerHTML = '[Included]';
        if (zhddl != null) {
            zhddl.disabled = true;
            zhddl.selectedIndex = cavddl.selectedIndex;
        }
    } else {
        var tmp;
        if (zhddl != null) {
            tmp = price[zhddl.options[zhddl.selectedIndex].value];
            zhddl.disabled = false;
        }
        else {
            tmp = price[document.getElementById('hidStep5zh').value];
        }
        zhc.innerHTML = '[Add ' + fmtCurr(tmp) + ']';
    }
    if (sym.checked || bd.checked || cav.checked || zh.checked) {
        no.checked = false;
    }
}
function toggleNoAV() {
    bd = document.getElementById('ckStep5bd');
    cav = document.getElementById('ckStep5cav');
    zh = document.getElementById('ckStep5zh');
    no = document.getElementById('ckStep5no');
    if (no.checked == true) {
        sym.checked = false;
        bd.checked = false;
        cav.checked = false;
        zh.checked = false;
    }
    toggleAvChecks();
}

function chgBdPrice() {
    ddl = document.getElementById('ddlStep5bd');
    cost = document.getElementById('spBd');
    tmp = price[ddl.options[ddl.selectedIndex].value];
    cost.innerHTML = '[Add ' + fmtCurr(tmp) + ']';
}
function chgZhPrice() {
    ddl = document.getElementById('ddlStep5zh');
    cost = document.getElementById('spZh');
    tmp = price[ddl.options[ddl.selectedIndex].value];
    cost.innerHTML = '[Add ' + fmtCurr(tmp) + ']';
}
function chgCavPrice() {
    ddl = document.getElementById('ddlStep5cav');
    cost = document.getElementById('spCav');
    tmp = price[ddl.options[ddl.selectedIndex].value];
    cost.innerHTML = '[Add ' + fmtCurr(tmp) + ']';
    if (document.getElementById('ckStep5cav').checked) {
        document.getElementById('ddlStep5zh').selectedIndex = ddl.selectedIndex;
    }
}
function initAvPrices() {
    if (document.getElementById('ddlStep5bd') != null)
        chgBdPrice();
    if (document.getElementById('ddlStep5zh') != null)
        chgZhPrice();
    if (document.getElementById('ddlStep5cav') != null)
        chgCavPrice();
}

/* Step 6 */
function initArch() {
    pn = document.getElementById('ddlArchiva').value;
    if (pn != "") chgArchiva();
    actArch();
}

function actArch() {
    if (document.getElementById('rbStep6Row1').checked == true) {
        document.getElementById('ddlArchiva').disabled = true;
        document.getElementById('archiva').style.color = "#AAA";
        document.getElementById('csMA').style.color = "#AAA";        
    }
    else if (document.getElementById('rbStep6Row3').checked == true) {
        document.getElementById('ddlArchiva').disabled = false;
        document.getElementById('ddlArchiva').focus();
        document.getElementById('archiva').style.color = "#000";
        document.getElementById('csMA').style.color = "#000";                  
    }    
}

function chgArchiva() {
    try {
        ddl = document.getElementById("ddlArchiva");
        if (ddl.value == 0) {
            document.getElementById('costMA').innerHTML = '';
            return;
        }
        qty = ddl.options[ddl.selectedIndex].text;
        tmp = price[qty];
        tmp = parseFloat(tmp);
        document.getElementById('costMA').innerHTML = '[Add ' + fmtCurr(tmp) + ']';
    }
    catch (ex) {
        //  alert(ex.message);
        return;
    }
}

/* Step Altaro */
function toggleAltChecks(checker) {
    fs = document.getElementById('ckAltaroFS');
    oops = document.getElementById('ckAltaroOops');
    no = document.getElementById('ckAltaroNo');

    if (fs.checked) {
        document.getElementById('AltaroFS').style.color = "#000";
        document.getElementById('csAltFS').style.color = "#000";
        document.getElementById('txtAltaroFS').disabled = false;
    }
    else {
        document.getElementById('AltaroFS').style.color = "#AAA";
        document.getElementById('csAltFS').style.color = "#AAA";
        document.getElementById('txtAltaroFS').disabled = true;
    }

    if (oops.checked) {
        document.getElementById('AltaroOops').style.color = "#000";
        document.getElementById('csAltOops').style.color = "#000";
        document.getElementById('txtAltaroOops').disabled = false;
    }
    else {
        document.getElementById('AltaroOops').style.color = "#AAA";
        document.getElementById('csAltOops').style.color = "#AAA";
        document.getElementById('txtAltaroOops').disabled = true;
    }

    if (checker == "ckAltaroFS" && fs.checked) {
        document.getElementById('txtAltaroFS').focus();
    }
    else if (checker == "ckAltaroOops" && oops.checked) {
        document.getElementById('txtAltaroOops').focus();
    }
        
    if (fs.checked || oops.checked) {
        no.checked = false;
    }
}
function toggleNoAlt() {
    fs = document.getElementById('ckAltaroFS');
    oops = document.getElementById('ckAltaroOops');
    no = document.getElementById('ckAltaroNo');
    if (no.checked == true) {
        fs.checked = false;
        oops.checked = false;
    }
    toggleAltChecks();
}
function initAltaro() {
    qty = document.getElementById('txtAltaroFS').value;
    if (qty > 0) chgAltaroFS(qty);
    qty2 = document.getElementById('txtAltaroOops').value;
    if (qty2 > 0) chgAltaroOops(qty2);
    toggleAltChecks();
}
function chgAltaroFS(qty) {
    cost = document.getElementById('costFS');
    qty = parseInt(qty);
    var rateS;
    if (qty > 1000) {
        qty = 1000;
        document.getElementById('txtAltaroFS').value = qty;
    }
    if (qty < 1) {
        cost.innerHTML = '';
        return;
    }
    if (isNaN(qty)) {
        cost.innerHTML = '';
        return;
    }
    tmp = 0;
    if (qty > 0 && qty < 4) {
        tmp = eval("rateS" + qty);
    }
    else {
        if (qty >= 4 && qty < 11) rateS = rateS4;
        else if (qty >= 11 && qty < 21) rateS = rateS11;
        else if (qty >= 21 && qty < 51) rateS = rateS21;
        else if (qty >= 51 && qty < 101) rateS = rateS51;
        else rateS = rateS101;
        tmp = qty * rateS;
    }
    if (isNaN(tmp)) { return; }
    cost.innerHTML = '[Add ' + fmtCurr(tmp) + ']';
}
function chgAltaroOops(qty) {
    cost = document.getElementById('costOops');
    qty = parseInt(qty);
    var rateD;
    if (qty > 1000) {
        qty = 1000;
        document.getElementById('txtAltaroOops').value = qty;
    }
    if (qty < 1) {
        cost.innerHTML = '';
        return;
    }
    if (isNaN(qty)) {
        cost.innerHTML = '';
        return;
    }
    tmp = 0;
    if (qty > 0 && qty < 4) {
        tmp = eval("rateD" + qty);
    }
    else {
        if (qty >= 4 && qty < 11) rateD = rateD4;
        else if (qty >= 11 && qty < 21) rateD = rateD11;
        else if (qty >= 21 && qty < 51) rateD = rateD21;
        else if (qty >= 51 && qty < 101) rateD = rateD51;
        else rateD = rateD101;
        tmp = qty * rateD;
    }
    if (isNaN(tmp)) { return; }
    cost.innerHTML = '[Add ' + fmtCurr(tmp) + ']';
}

/* Session Keepalive */
setInterval("ajaxKeepAlive()", 60000 * 55); // 55 Mins (session timeout is 60)

var xmlHttp;
var timeout;
function ajaxKeepAlive() {
    var url = "keepalive.aspx?" + Math.random();
    xmlHttp = GetXmlHttpObject()
    if (xmlHttp == null) {
        return;
    }
    timeout = setTimeout('xmlHttp.abort()', 5000);
    xmlHttp.onreadystatechange = stateChanged;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}
function stateChanged() {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        clearTimeout(timeout);
    }
}
function GetXmlHttpObject() {
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        //Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
