function SetSubmitMode(val, formname) {

        var modeElements = document.getElementsByName("mode");
        if (modeElements != null) {
            var modeCount = modeElements.length;
            for (var i = 0; i < modeCount; i++) {
                if (modeElements[i].type == "hidden") {
                    modeElements[i].value = val;
                    return;
                }
            }
        }
        var modeElement = document.createElement("input");
        modeElement.setAttribute("type", "hidden");
        modeElement.setAttribute("name", "mode");
        modeElement.setAttribute("id", "mode");
        modeElement.setAttribute("value", val);
        if (formname != null)
            document.forms[formname].appendChild(modeElement);
        else
            cartform.appendChild(modeElement);
}

function CheckEnterKey(e) {
    if (e.keyCode == 13) {
        SetSubmitMode('coupon', 'cartform');
        document.forms['cartform'].submit();
        return false;
    }
    return true;
}

function setShipping(shippingid, shippingprice, total, shipname) {
    talktoServer(
        "Ajax\\setShipping.aspx",
        "shippingid=" + shippingid,
        function shippingCallback(ajaxResponse) {
            var results = ajaxResponse.responseText.split("|");

            var shipControl = document.getElementById("shippingTotal");
            var totalControl = document.getElementById("orderTotal");
            var shipLabel = document.getElementById("shippingName");

            if (results.length = 2) //valid response: (ok/error)|(message)
            {
                if (results[0] == "ok") {
                    shipControl.innerHTML = "$" + shippingprice.toFixed(2);
                    totalControl.innerHTML = "$" + (total + shippingprice).toFixed(2);
                    if (shipLabel != null)
                        shipLabel.innerHTML = shipname + " Shipping:";
                }
            }
        },
        null);
}

function selectCountry(control, msgbox, val, postCallback) {
    talktoServer(
        "Ajax\\selectStates.aspx",
        "country=" + val + "&amp;control=" + control,
        function countryCallback(ajaxResponse) {
            //var stateDrop = document.getElementById(dropdown);
            //stateDrop.innerHTML = response.responseText;


            var results = ajaxResponse.responseText.split("|");

            var cs = document.getElementById(control);
            while (cs.options.length > 0) {
                cs.remove(cs.options[0]);
            }

            for (var i = 0; i < results.length; i++) {
                if (results[i] == "")
                    continue;
                var newOpt = document.createElement('option');
                var tmp = results[i].split(":");
                newOpt.text = tmp[1]; //thetextwillbethepersonsname

                try {
                    cs.add(newOpt, null); //standardscompliant
                }
                catch (ex) {
                    cs.add(newOpt); //IEonly
                }
                cs.options[i].value = tmp[0]; //thevaluewillbethepersonsemail
                cs.selectedIndex = 0; //selectthefirstoption,shouldbedefaultbutmakesure
            }

            if (postCallback != null)
                postCallback();
        },
        msgbox /*document.getElementById(msgbox)*/);
}

function billingSame() {
    var disabled = document.getElementById('ssame').checked;
    var sFirst = document.getElementById('sfirstname');
    sFirst.disabled = disabled;

    var sLast = document.getElementById('slastname');
    sLast.disabled = disabled;

    var sComp = document.getElementById('scompany');
    sComp.disabled = disabled;

    var sAdd1 = document.getElementById('saddress1');
    sAdd1.disabled = disabled;

    var sAdd2 = document.getElementById('saddress2');
    sAdd2.disabled = disabled;

    var sCity = document.getElementById('scity');
    sCity.disabled = disabled;

    var sZip = document.getElementById('szip');
    sZip.disabled = disabled;

    var sPhone = document.getElementById('bphone');
    sPhone.disabled = disabled;

    if (disabled) {
        sFirst.value = document.getElementById('bfirstname').value;
        sLast.value = document.getElementById('blastname').value;
        sComp.value = document.getElementById('bcompany').value;
        sAdd1.value = document.getElementById('baddress1').value;
        sAdd2.value = document.getElementById('baddress2').value;
        sCity.value = document.getElementById('bcity').value;
        sZip.value = document.getElementById('bzip').value;
        sPhone.Value = document.getElementById('bphone').value;
    }


    var sCountry = document.getElementById('scountry');
    var bCountry = document.getElementById('bcountry');
    var sState = document.getElementById('sstate');

    sCountry.disabled = disabled;
    sState.disabled = disabled;
    if (disabled) {
        if (sCountry.value == bCountry.value) {
            sState.value = document.getElementById('bstate').value;
        }
        else {
            sCountry.value = bCountry.value;

            selectCountry('sstate', 'countryError', bCountry.value,
				function setSState() {
				    sState.value = document.getElementById('bstate').value;
				}
			);
        }
    }
}

function shippingSame() {
    var disabled = document.getElementById('bsame').checked;
    var bFirst = document.getElementById('bfirstname');
    bFirst.disabled = disabled;

    var bLast = document.getElementById('blastname');
    bLast.disabled = disabled;

    var bComp = document.getElementById('bcompany');
    bComp.disabled = disabled;

    var bAdd1 = document.getElementById('baddress1');
    bAdd1.disabled = disabled;

    var bAdd2 = document.getElementById('baddress2');
    bAdd2.disabled = disabled;

    var bCity = document.getElementById('bcity');
    bCity.disabled = disabled;

    var bZip = document.getElementById('bzip');
    bZip.disabled = disabled;

    var bPhone = document.getElementById('fax');
    bPhone.disabled = disabled;

    var bCountry = document.getElementById('bcountry');
    var sCountry = document.getElementById('scountry');
    var bState = document.getElementById('bstate');

    if (disabled) {
        bFirst.value = document.getElementById('sfirstname').value;
        bLast.value = document.getElementById('slastname').value;
        bComp.value = document.getElementById('scompany').value;
        bAdd1.value = document.getElementById('saddress1').value;
        bAdd2.value = document.getElementById('saddress2').value;
        bCity.value = document.getElementById('scity').value;
        bZip.value = document.getElementById('szip').value;
        bPhone.value = document.getElementById('bphone').value;
    }

    bCountry.disabled = disabled;
    bState.disabled = disabled;
    if (disabled) {
        if (bCountry.value == sCountry.value) {
            bState.value = document.getElementById('sstate').value;
        }
        else {
            bCountry.value = sCountry.value;

            selectCountry('bstate', 'countryError', sCountry.value,
					function setBState() {
					    bState.value = document.getElementById('sstate').value;
					}
				);
        }
    }
}

var emailRegEx = new RegExp("[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}", "i");
var checkFields = true;

function checkoutSubmit() {
    if (!checkFields)
        return true;

    var paypal = document.getElementById('paymentpaypal');
    if (paypal != null && paypal.checked)
        return true;

    var normalColor = "";
    var errorColor = "#ff8888";
    var billingErrors = "";
    var shippingErrors = "";
    /*
    Handle billing
    */
    var first = document.getElementById('bfirstname');
    var last = document.getElementById('blastname');
    var address = document.getElementById('baddress1');
    var city = document.getElementById('bcity');
    var zip = document.getElementById('bzip');
    var country = document.getElementById('bcountry');
    var state = document.getElementById('bstate');
    var phone = document.getElementById('fax');
    var email = document.getElementById('email');

    first.style.backgroundColor = normalColor;
    last.style.backgroundColor = normalColor;
    address.style.backgroundColor = normalColor;
    city.style.backgroundColor = normalColor;
    zip.style.backgroundColor = normalColor;
    country.style.backgroundColor = normalColor;
    state.style.backgroundColor = normalColor;
    phone.style.backgroundColor = normalColor;
    email.style.backgroundColor = normalColor;

    if (first.value.length == 0) { billingErrors += "First name is required\r\n"; first.style.backgroundColor = errorColor; }
    if (last.value.length == 0) { billingErrors += "Last name is required\r\n"; last.style.backgroundColor = errorColor; }
    if (address.value.length == 0) { billingErrors += "Address is required\r\n"; address.style.backgroundColor = errorColor; }
    if (city.value.length == 0) { billingErrors += "City is required\r\n"; city.style.backgroundColor = errorColor; }
    if (zip.value.length < 5) { billingErrors += "Invalid postal code entered\r\n"; zip.style.backgroundColor = errorColor; }
    if (country.value.length < 2) { billingErrors += "Country selection required\r\n"; country.style.backgroundColor = errorColor; }
    if (state.value.length < 2) { billingErrors += "State selection required\r\n"; state.style.backgroundColor = errorColor; }
    if (phone.value.length < 10) { billingErrors += "Complete phone number required\r\n"; phone.style.backgroundColor = errorColor; }
    if (email.value.length < 6 || !emailRegEx.test(email.value)) { billingErrors += "Email required\r\n"; email.style.backgroundColor = errorColor; }

    /*
    Handle shipping
    */
    first = document.getElementById('sfirstname');
    last = document.getElementById('slastname');
    address = document.getElementById('saddress1');
    city = document.getElementById('scity');
    zip = document.getElementById('szip');
    country = document.getElementById('scountry');
    state = document.getElementById('sstate');
    phone = document.getElementById('bphone');

    first.style.backgroundColor = normalColor;
    last.style.backgroundColor = normalColor;
    address.style.backgroundColor = normalColor;
    city.style.backgroundColor = normalColor;
    zip.style.backgroundColor = normalColor;
    country.style.backgroundColor = normalColor;
    state.style.backgroundColor = normalColor;

    if (!document.getElementById('ssame').checked) {
        if (first.value.length == 0) { shippingErrors += "First name is required\r\n"; first.style.backgroundColor = errorColor; }
        if (last.value.length == 0) { shippingErrors += "Last name is required\r\n"; last.style.backgroundColor = errorColor; }
        if (address.value.length == 0) { shippingErrors += "Address is required\r\n"; address.style.backgroundColor = errorColor; }
        if (city.value.length == 0) { shippingErrors += "City is required\r\n"; city.style.backgroundColor = errorColor; }
        if (zip.value.length < 5) { shippingErrors += "Invalid postal code entered\r\n"; zip.style.backgroundColor = errorColor; }
        if (country.value.length < 2) { shippingErrors += "Country selection required\r\n"; country.style.backgroundColor = errorColor; }
        if (state.value.length < 2) { shippingErrors += "State selection required\r\n"; state.style.backgroundColor = errorColor; }
    }

    if (billingErrors.length > 0 || shippingErrors.length > 0) {
        var message = "Please correct these before continuing:\r\n\r\n";
        if (billingErrors.length > 0) {
            message += "___Billing Information___\r\n" + billingErrors + "\r\n";
        }
        if (shippingErrors.length > 0) {
            message += "___Shipping Information___\r\n" + shippingErrors + "\r\n";
        }
        alert(message);
        return false;
    }
    return true;
}

function checkoutSubmitShippingPrimary() {
    if (!checkFields)
        return true;

    var paypal = document.getElementById('paymentpaypal');
    if (paypal != null && paypal.checked)
        return true;

    var normalColor = "";
    var errorColor = "#ff8888";
    var billingErrors = "";
    var shippingErrors = "";
    /*
    Handle shipping
    */
    var first = document.getElementById('sfirstname');
    var last = document.getElementById('slastname');
    var address = document.getElementById('saddress1');
    var city = document.getElementById('scity');
    var zip = document.getElementById('szip');
    var country = document.getElementById('scountry');
    var state = document.getElementById('sstate');
    var phone = document.getElementById('bphone');
    var email = document.getElementById('email');

    first.style.backgroundColor = normalColor;
    last.style.backgroundColor = normalColor;
    address.style.backgroundColor = normalColor;
    city.style.backgroundColor = normalColor;
    zip.style.backgroundColor = normalColor;
    country.style.backgroundColor = normalColor;
    state.style.backgroundColor = normalColor;
    phone.style.backgroundColor = normalColor;
    email.style.backgroundColor = normalColor;

    if (first.value.length == 0) { shippingErrors += "First name is required\r\n"; first.style.backgroundColor = errorColor; }
    if (last.value.length == 0) { shippingErrors += "Last name is required\r\n"; last.style.backgroundColor = errorColor; }
    if (address.value.length == 0) { shippingErrors += "Address is required\r\n"; address.style.backgroundColor = errorColor; }
    if (city.value.length == 0) { shippingErrors += "City is required\r\n"; city.style.backgroundColor = errorColor; }
    if (zip.value.length < 5) { shippingErrors += "Invalid postal code entered\r\n"; zip.style.backgroundColor = errorColor; }
    if (country.value.length < 2) { shippingErrors += "Country selection required\r\n"; country.style.backgroundColor = errorColor; }
    if (state.value.length < 2) { shippingErrors += "State selection required\r\n"; state.style.backgroundColor = errorColor; }
    if (phone.value.length < 10) { shippingErrors += "Complete phone number required\r\n"; phone.style.backgroundColor = errorColor; }
    if (email.value.length < 6 || !emailRegEx.test(email.value)) { shippingErrors += "Email required\r\n"; email.style.backgroundColor = errorColor; }

    /*
    Handle billing
    */
    first = document.getElementById('bfirstname');
    last = document.getElementById('blastname');
    address = document.getElementById('baddress1');
    city = document.getElementById('bcity');
    zip = document.getElementById('bzip');
    country = document.getElementById('bcountry');
    state = document.getElementById('bstate');
    phone = document.getElementById('fax')

    first.style.backgroundColor = normalColor;
    last.style.backgroundColor = normalColor;
    address.style.backgroundColor = normalColor;
    city.style.backgroundColor = normalColor;
    zip.style.backgroundColor = normalColor;
    country.style.backgroundColor = normalColor;
    state.style.backgroundColor = normalColor;
    phone.style.backgroundColor = normalColor;

    if (!document.getElementById('bsame').checked) {
        if (first.value.length == 0) { billingErrors += "First name is required\r\n"; first.style.backgroundColor = errorColor; }
        if (last.value.length == 0) { billingErrors += "Last name is required\r\n"; last.style.backgroundColor = errorColor; }
        if (address.value.length == 0) { billingErrors += "Address is required\r\n"; address.style.backgroundColor = errorColor; }
        if (city.value.length == 0) { billingErrors += "City is required\r\n"; city.style.backgroundColor = errorColor; }
        if (zip.value.length < 5) { billingErrors += "Invalid postal code entered\r\n"; zip.style.backgroundColor = errorColor; }
        if (country.value.length < 2) { billingErrors += "Country selection required\r\n"; country.style.backgroundColor = errorColor; }
        if (state.value.length < 2) { billingErrors += "State selection required\r\n"; state.style.backgroundColor = errorColor; }
        //if (phone.value.length < 10) { billingErrors += "Complete phone number required\r\n"; phone.style.backgroundColor = errorColor; }
    }

    if (billingErrors.length > 0 || shippingErrors.length > 0) {
        var message = "Please correct these before continuing:\r\n\r\n";
        if (billingErrors.length > 0) {
            message += "___Billing Information___\r\n" + billingErrors + "\r\n";
        }
        if (shippingErrors.length > 0) {
            message += "___Shipping Information___\r\n" + shippingErrors + "\r\n";
        }
        alert(message);
        return false;
    }
    return true;
}

function SetCardEnabled(numberField, expField, expField2, enabled) {
    var f1 = document.getElementById(numberField);
    if (f1 != null)
        f1.disabled = !enabled;
    var f2 = document.getElementById(expField);
    if (f2 != null)
        f2.disabled = !enabled;
    var f3 = document.getElementById(expField2);
    if (f3 != null)
        f3.disabled = !enabled;
}

function CheckQty(quantityField) {
    var qty = document.getElementById(quantityField);
    if (qty != null && qty.value == "0")
        qty.value = "1";
}
