//var timHeaderBand = setInterval(timerTick, 6000);
var intHeaderBand = 2;
var maxHeaderBand = 4;
var blnEmptyCartRedirect = true;

var strBaseURL = "";


function body_Load(argEnableSearchAutofocus, argBaseURL) {

    strBaseURL = argBaseURL;

    if (argEnableSearchAutofocus == true) {

        document.getElementById("txtSearch").focus();

    }

}


function btnSearch_Click() {
    
    document.getElementById("btnSearch").disabled = true;

    var xmlhttp;

    if (window.XMLHttpRequest) {

        xmlhttp = new XMLHttpRequest();

    } else {

        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

    }

    xmlhttp.onreadystatechange=function() {

        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

            window.location = xmlhttp.responseText;

        }

    }

    xmlhttp.open("POST", strBaseURL + "/function/get-search-url/", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    var postStr;

    postStr =  "txtSearch=" + document.getElementById("txtSearch").value;

    xmlhttp.send(postStr);
    
}


function changeHeaderBand() {

    var xmlhttp;

    if (window.XMLHttpRequest) {

        xmlhttp = new XMLHttpRequest();

    } else {

        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

    }

    xmlhttp.onreadystatechange=function() {

        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

            document.getElementById("header_band").innerHTML = xmlhttp.responseText;

        }

    }
    
    xmlhttp.open("GET", "/function/get-header-band/", true);
    xmlhttp.send();

}


function spnAdditionalContent_Toggle(argTargetElementID) {
    
    if (document.getElementById(argTargetElementID).className == "spnAdditionalContent_Hidden") {
        
        // Show
        document.getElementById(argTargetElementID).className = "spnAdditionalContent_Visible";
        
    } else {
        
        // Hide
        document.getElementById(argTargetElementID).className = "spnAdditionalContent_Hidden";
        
    }
    
}



function timerTick() {
    
    changeHeaderBand();
    updateViewCart();
    
}


function txtSearch_KeyPress(e) {
    
    if (e.keyCode == 13) {
        
        btnSearch_Click();
        
    }

}


function updateViewCart() {
    
    if ((window.location.pathname == "/check-out/") || (window.location.pathname == "/check-out/#aTop")) {
        
        return false;
        
    }
    
    var xmlhttp;

    if (window.XMLHttpRequest) {

        xmlhttp = new XMLHttpRequest();

    } else {

        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

    }

    xmlhttp.onreadystatechange=function() {

        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

            document.getElementById("header_view_cart").innerHTML = "View Cart (" + xmlhttp.responseText + ")";
            
            if (xmlhttp.responseText > 0) {
                
                document.getElementById("header_check_out").style.display = 'inline';
                
            } else {
                
                document.getElementById("header_check_out").style.display = 'none';
                
                //Check if we are on checkout page
                if ((window.location.pathname == "/check-out/") || (window.location.pathname == "/check-out/#aTop")) {
                    
                    if (blnEmptyCartRedirect == true) {
                        
                        //window.location = "../";
                        
                    }

                }
                
            }

        }

    }

    xmlhttp.open("GET", "/function/get-cart-quantity/", true);
    xmlhttp.send();
    
}
