﻿function GetExpress2(country) {
    //alert("here");
    ///country==null in checkout.aspx
    if (country == null) {
        Clear("shipmethod");
        var dropdown = document.getElementById("ShipToCountryDropDownList");
        var myindex = dropdown.selectedIndex;
        var SelValue = dropdown.options[myindex].value;
        if (SelValue != "USA") {

            if (SelValue == "Select Country...") {
                alert("Please select a country");
                return;
            }
            //alert("Express Shipping Available For US Orders Only");
            var sm = document.getElementById("expressoptions");
            if (sm != null) {
                if (sm.style.cssText != null)
                    sm.style.cssText = "visibility:hidden;";
                else
                    sm.setAttribute("style", "visibility: hidden");
            }
            return;

        }

    } //oncountryselect in cart determine if express available
    else if (country == "check" || country == "check2") {
        var dropdown = document.getElementById("SelectShipping");
        var myindex = dropdown.selectedIndex;
        var SelValue = dropdown.options[myindex].value;
        if (SelValue != "USA") {
            //alert("Express Shipping Available For US Orders Only");
            var sm = document.getElementById("expressoptions");
            if (sm != null) {
                if (sm.style.cssText != null)
                    sm.style.cssText = "visibility:hidden;";
                else
                    sm.setAttribute("style", "visibility: hidden");
            }
            return;
        }
    }
    //checkout or check options determine if zip is good
    if (country == "USA" || country == null) {
        var st = document.getElementById("ShipToZipCodeTextBox").value;
        if (st.length < 5 || !IsNumeric(st.slice(0, 4))) {
            //alert("Not A Valid Zip");
            return;
        }
    }

    var xmlhttp;
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else {
        ;
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            var response = xmlhttp.responseText;
            //checking options are available in cart.aspx
            if (country == "check" || country=="check2") {

                if (response == "true") {
                    var sm = document.getElementById("expressoptions");
                    if (sm != null) {
                        if (sm.style.cssText != null)
                            sm.style.cssText = "visibility:visible;";
                        else
                            sm.setAttribute("style", "visibility: visible");
                    }

                }
            } else {
                //USA is cart (what's available, null is checkout (create dropdown options)
                var responseElements = response.split("|");
                if (country == "USA") {//clear
                    var txt = document.getElementById("shipmethodtxt");
                    if (txt != null)
                        txt.innerHTML = "Ship Options(Available At Checkout)<br />";

                }
                for (var x = 0; x < responseElements.length; x++) {
                    if (country == null)//checkout
                        addOption(document.getElementById("shipmethod"), responseElements[x], responseElements[x]);
                    else {//cart
                        var txt = document.getElementById("shipmethodtxt");
                        txt.innerHTML += responseElements[x] + "<br />";
                    }

                }



                var sm2 = document.getElementById("shipmethod");
                if (sm2 != null) {
                    if (sm2.style.cssText != null)
                        sm2.style.cssText = "visibility:visible;";
                    else
                        sm2.setAttribute("style", "visibility: visible");
                }
            }

        }

    }


    if (country == "check" || country=="check2") {//just return true or false if items in stock express available
        if(country == "check")
            xmlhttp.open("GET", "shipmethod.aspx?ajax=true&cid=" + Get_Cookie("cid") + "&checkexpress=true&ms=" + new Date().getTime());
        else
            xmlhttp.open("GET", "shipmethod.aspx?ajax=true&cid=" + Get_Cookie("cid") + "&checkexpress2=true&ms=" + new Date().getTime());

    }
    else {//return options either parsed into text or dropdown

        xmlhttp.open("GET", "shipmethod.aspx?ms=" + new Date().getTime() + "&ajax=true&cid=" + Get_Cookie("cid") + "&zip=" + document.getElementById("ShipToZipCodeTextBox").value, true);

    }
    xmlhttp.send(null);
}

function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}



function clearShipMethods() {
    Clear("shipmethod");
    GetExpress();
    ShipMethodChanged();
    return;
    // alert('here');
    if (document.getElementById("shipmethod").selectedIndex != null) {

        if (document.getElementById("shipmethod").selectedIndex <= 0)
            return;
    }
    else
        return;


    var dropdown = document.getElementById("ShipToCountryDropDownList")
    var myindex = dropdown.selectedIndex;
    var SelValue = dropdown.options[myindex].value;

    if (Trim(SelValue) == "USA") {

        document.getElementById("CartShipping").innerHTML = trim("$5.00");
        updateTotals();
    }
    else {
        document.getElementById("CartShipping").innerHTML = trim("$15.00");
        updateTotals();
    }



}

function ShipMethodChanged() {

    var dropdown = document.getElementById("shipmethod");
    var myindex = dropdown.selectedIndex;
    var SelValue = dropdown.options[myindex].value;
    var SelArray2 = SelValue.split("-");

    document.getElementById("CartShipping").innerHTML = trim(SelArray2[1]);
    updateTotals();
}

function IsNumeric(sText) {
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;


    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;

}

function Clear(element) {
    var cell = document.getElementById(element);
    if (cell == null)
        return;
    if (cell.hasChildNodes()) {
        while (cell.childNodes.length >= 1) {
            cell.removeChild(cell.firstChild);
        }
    }
}


function addOption(selectbox, text, value) {
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    selectbox.options.add(optn);
}

function Get_Cookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}


function GetExpress() {
    //  Clear("shipmethod");
    var dropdown = document.getElementById("ShipToCountryDropDownList");
    var myindex = dropdown.selectedIndex;
    var SelValue = dropdown.options[myindex].value;
    if (SelValue != "USA") {

        if (SelValue == "Select Country...") {
            //alert("Please select a country");
            return;
        }
        // alert("Express Shipping Available For US Orders Only");
        //return;

    }

    var st = document.getElementById("ShipToZipCodeTextBox").value;
    if (st.length < 5 || !IsNumeric(st.slice(0, 4))) {
        if (SelValue == "USA") {
            alert("Not A Valid Zip");
            return;
        }
    }

    var xmlhttp;
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else {
        ; //alert("Your browser does not support XMLHTTP!");
    }


    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {


            var response = xmlhttp.responseText;
            var responseElements = response.split("|");
            for (var x = 0; x < responseElements.length; x++) {
                if (x == 0) {
                    var stime = document.getElementById("shiptime");
                    stime.innerHTML = responseElements[x];
			continue;

                }
                addOption(document.getElementById("shipmethod"), responseElements[x], responseElements[x]);
            }



                var sm2 = document.getElementById("s1");
                if (sm2 != null) {
			
                    if (sm2.style.cssText != null)
                        sm2.style.cssText = "visibility:visible;";
                    else
                        sm2.setAttribute("style", "visibility: visible");
                }

            var sm = document.getElementById("shipmethod");

            if (sm.style.cssText != null)
                sm.style.cssText = "visibility:visible;";
            else
                sm.setAttribute("style", "visibility: visible");

        }

    }
    if (SelValue != "USA") {

        xmlhttp.open("GET", "shipmethod.aspx?country=" + SelValue, true); //get country ship option
    }
    else
        xmlhttp.open("GET", "shipmethod.aspx?ms=" + new Date().getTime() + "&ajax=true&cid=" + Get_Cookie("cid") + "&zip=" + document.getElementById("ShipToZipCodeTextBox").value, true);
    xmlhttp.send(null);
}
    
