﻿$(document).ready(function() {
    checkShippingOptions();
    $(":radio").click(function() {
        var rdoID = $(this).attr("id");
        var txt = $("#" + rdoID + " + label").html();
        txt = txt.substring(txt.indexOf("$") + 1);
        $("#c_shipping").html(txt);
        var total = $("#c_total").attr("rel");
        total = total.substring(total.indexOf("$") + 1).replace(",", "");
        total = parseFloat(total) + parseFloat(txt);
        $("#c_total").html(formatCurrency(total));
    });
    $(".t_q").keyup(function() {
        var id = $(this).attr("id");
        var txt = $("#" + id).val();
        txt = txt.replace(/[^0-9]+/g, '');
        $("#" + id).val(txt);
    });
    $(".t_q").blur(function() {
        var id = $(this).attr("id");
        var txt = $("#" + id).val();
        txt = jQuery.trim(txt);
        if (txt.length == 0) {
            $("#" + id).val("0");
        }
    });
    $("#chkAddressDifferent").click(function() {
        checkShippingOptions();
        $("#shippingTable").slideToggle("normal");
        if ($("#chkAddressDifferent:checked").length == 1) {
            alert("Selecting a different ship to address will cause a manual review of your order and may cause a delay in shipping departure. Please make sure you are available at the shipping phone number provided.");
        }

    });
    $("#txtFirstName").blur(function() {
        $("#txtPayFirstName").val($(this).val());
    });
    $("#txtMiddleInital").blur(function() {
        $("#txtPayMiddleInital").val($(this).val());
    });
    $("#txtLastName").blur(function() {
        $("#txtPayLastName").val($(this).val());
    });
    $("#bLogin").toggle(function() {
        $(this).html("<b>[- Click to cancel login -]</b>");
        $("#login").slideToggle("normal");
    },
    function() {
        $(this).html("Already have an account? Click Here to login");
        $("#login").slideToggle("normal");
    });
    $(".f_ele").focus(function() {
        $(this).css({ "background-color": "#ff6633", "border-width": "2px", "color": "#fff" });
    });
    $(".f_ele").blur(function() {
        $(this).css({ "background-color": "#fff", "border-width": "1px", "color": "#000" });
    });
    //    $("#txtPhone").blur(function() {
    //        if ($(this).val().length == 10) {
    //            var s, s1, s2, s3;
    //            s = $(this).val();
    //            s1 = s.substring(0, 3);
    //            s2 = s.substring(3, 6);
    //            s3 = s.substring(6, 10);
    //            $(this).val("(" + s1 + ") " + s2 + "-" + s3)
    //        }
    //        else {
    //            alert('Please enter a 10 digit phone number.');
    //        }
    //    });
    //    $("#txtPhoneS").blur(function() {
    //        if ($(this).val().length == 10) {
    //            var s, s1, s2, s3;
    //            s = $(this).val();
    //            s1 = s.substring(0, 3);
    //            s2 = s.substring(3, 6);
    //            s3 = s.substring(6, 10);
    //            $(this).val("(" + s1 + ") " + s2 + "-" + s3)
    //        }
    //        else {
    //            alert('Please enter a 10 digit phone number.');
    //        }
    //    });

//    $("#fCart").submit(function() {
//        if ($("#login:visible").length > 0) {
//            return true;
//        }
//        else if (valForm()) {
//            $("#btnPlaceOrder").css("display", "none");
//            $("#btnPlaceOrderP").css("display", "block");
//            return true;
//        }
//        else {
//            return false;
//        }
//    });

    $("#txtEmail").blur(function() {
        var str = jQuery.trim($(this).val());
        if (str != "" && $("#login:visible").length == 0 && $("#bLogin").attr("href") != null) {
            var params = '{"email":"' + str + '"}';
            $.ajax({
                type: "POST",
                url: "wpac_data.asmx/emailcheck",
                data: params,
                beforeSend: function(xhr) { xhr.setRequestHeader("Content-type", "application/json; charset=utf-8"); },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(result) {
                    if (result.d == "true") {
                        $.modaldialog.warning('<b>Email Address Already Registered!</b> Our system shows that email <b>' + str + '</b> is already registered with us. <br /><br /><b>Action Options:</b><ul><li><a href="javascript:void(0);" onclick="javascript:exAction(2);">Click Here To Login</a></li><li><a href="javascript:void(0);" onclick="javascript:exAction(1);">Click Here For Password Reminder</a></li></ul>');
                    }
                },
                error: function(xhr, msg, e) {
                    alert(msg); //Error Callback
                }

            });
        }

    });
});


function exAction(i) {
    var url;
    if (i == 1) {
        url = "signin.aspx?f=true";
    }
    else {
        url = "signin.aspx";
    }
    $('#dialog').fadeOut("slow", function() { $(this).hide(0); });
    $('#dialog-mask').fadeOut("normal", function() { $(this).hide(0); });
    window.location = url;
}
function checkOutLoginReq() {
    if (jQuery.trim($("#txtEmail").val()) == "") {
        $.modaldialog.error('<b>Email Required!</b> Please enter a email address e.g. (name@domain.com).', {}, 'txtEmail');
        return false;
    }
    if (jQuery.trim($("#txtPassword").val()) == "") {
        $.modaldialog.error('<b>Password Required!</b> Please enter your password', {}, 'txtPassword');
        return false;
    }
    else {
        return true;
    }
}
function checkShippingOptions() {
    var _val;
    if ($("#chkAddressDifferent:checked").length == 0) {
        $("#ddlState option:selected").each(function() {
            _val = $(this).val();
        });
    }
    else {
        $("#ddlStateS option:selected").each(function() {
            _val = $(this).val();
        });
    }
    if (_val == "AK" || _val == "HI") {
        $("#rdoShipping_0").removeAttr("checked");
        $("#rdoShipping_1").attr("checked", "checked");
        $("#rdoShipping_0").hide();
        $("#rdoShipping_0").next("label").hide();

    }
    else {
        $("#rdoShipping_0").show();
        $("#rdoShipping_0").next("label").show();
    }
}
function valForm() {
    return formCheck();
}

function formCheck() {

    var reg = new RegExp("^[a-zA-Z 0-9 _]([-.\w]*[0-9a-zA-Z _])*@[0-9a-zA-Z-_]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$");
    //$("#btnPlaceOrder").hide();
    //$("#btnPlaceOrderP").show();
    if (jQuery.trim($("#txtEmail").val()) == "") {
        $.modaldialog.error('<b>Email Required!</b> Please enter a email address e.g. (name@domain.com).', {}, 'txtEmail');
        return false;
    }
    else if (!reg.test($("#txtEmail").val())) {
        $.modaldialog.error('<b>Incorrect Email Format!</b> Please enter a valid email address e.g. (name@domain.com).', {}, 'txtEmail');
        return false;
    }
    else if (jQuery.trim($("#txtFirstName").val()) == "") {
        $.modaldialog.error('<b>First Name Required!</b> Please enter your first name.', {}, 'txtFirstName');
        return false;
    }
    else if (jQuery.trim($("#txtLastName").val()) == "") {
        $.modaldialog.error('<b>Last Name Required!</b> Please enter your last name.', {}, 'txtLastName');
        return false;
    }
    else if (jQuery.trim($("#txtStreet").val()) == "") {
        $.modaldialog.error('<b>Billing Street Required!</b> Please enter your billing street.', {}, 'txtStreet');
        return false;
    }
    else if (jQuery.trim($("#txtCity").val()) == "") {
        $.modaldialog.error('<b>Billing City Required!</b> Please enter your billing city.', {}, 'txtCity');
        return false;
    }
    else if ($("#ddlState option:selected").val() == "NA") {
        $.modaldialog.error('<b>Billing State Required!</b> Please select your billing state.', {}, null);
        return false;
    }
    else if (jQuery.trim($("#txtZipCode").val()) == "") {
        $.modaldialog.error('<b>Billing Zip Code Required!</b> Please enter your billing zip code.', {}, 'txtZipCode');
        return false;
    }
    else if (jQuery.trim($("#txtZipCode").val()).length < 5) {
        $.modaldialog.error('<b>Billing Zip Code Invalid!</b> zip code must be 5 numeric characters.', {}, 'txtZipCode');
        return false;
    }
    else if (jQuery.trim($("#txtPhone").val()) == "") {
        $.modaldialog.error('<b>Billing Phone Required!</b> Please enter the phone number assoiated with your billing address.', {}, 'txtPhone');
        return false;
    }
    else if ($("#chkAddressDifferent:checked").length == 1) {
        if (jQuery.trim($("#txtStreetS").val()) == "") {
            $.modaldialog.error('<b>Shipping Street Required!</b> Please enter your shipping street.', {}, 'txtStreetS');
            return false;
        }
        else if (jQuery.trim($("#txtCityS").val()) == "") {
            $.modaldialog.error('<b>Shipping City Required!</b> Please enter your shipping city.', {}, 'txtCityS');
            return false;
        }
        else if ($("#ddlStateS option:selected").val() == "NA") {
            $.modaldialog.error('<b>Shipping State Required!</b> Please select your shipping state.', {}, null);
            return false;
        }
        else if (jQuery.trim($("#txtZipCodeS").val()) == "") {
            $.modaldialog.error('<b>Shipping Zip Code Required!</b> Please enter your shipping zip code.', {}, 'txtZipCodeS');
            return false;
        }
        else if (jQuery.trim($("#txtZipCodeS").val()).length < 5) {
            $.modaldialog.error('<b>Shipping Zip Code Invalid!</b> shipping zip code must be 5 numeric characters.', {}, 'txtZipCodeS');
            return false;
        }
        else if (jQuery.trim($("#txtPhoneS").val()) == "") {
            $.modaldialog.error('<b>Shipping Phone Required!</b> Please enter the phone number assoiated with your shipping address.', {}, 'txtPhoneS');
            return false;
        }
    }
    else if (jQuery.trim($("#txtPayFirstName").val()) == "") {
        $.modaldialog.error('<b>First Name Required!</b> Please enter your first name as it apears on your card.', {}, 'txtPayFirstName');
        return false;
    }
    else if (jQuery.trim($("#txtPayLastName").val()) == "") {
        $.modaldialog.error('<b>Last Name Required!</b> Please enter your last name as it apears on your card.', {}, 'txtPayLastName');
        return false;
    }
    else if ($("#ddlPayCardType option:selected").val() == "NA") {
        $.modaldialog.error('<b>Credit Card Type Required!</b> Please select the credit card type you are using.', {}, null);
        return false;
    }
    else if (jQuery.trim($("#txtPayCardNumber").val()) == "") {
        $.modaldialog.error('<b>Card Number Required!</b> Please enter your ' + $("#ddlPayCardType option:selected").val() + ' card number.', {}, 'txtPayCardNumber');
        return false;
    }
    else if (jQuery.trim($("#txtPayCVV").val()) == "") {
        $.modaldialog.error('<b>CVV2 Required!</b> Please enter the 3 or 4 digit security code found on your card.', {}, 'txtPayCVV');
        return false;
    }
    else if ($("#ddlMonth option:selected").val() == "NA") {
    $.modaldialog.error('<b>Credit Card Eexpiration Month Required!</b> Please select the expiration month on your credit card.', {}, null);
        return false;
    }
    else if ($("#ddlYear option:selected").val() == "NA") {
    $.modaldialog.error('<b>Credit Card Expiration Year Required!</b> Please select the expiration year on your credit card.', {}, null);
        return false;
    }
    else if ($("#chkAgree:checked").length == 0) {
        $.modaldialog.error('<b>Terms and Conditions!</b> Please agree to the site Terms & Conditions.', {}, null);
        $("#agree").css({"border":"1px dashed red"});
        return false;
    }
    else {
        //$("#btnPlaceOrder").css("display", "none");
        //$("#btnPlaceOrderP").css("display", "block");
        return true;

    }

}
function setActionWait() {
    $("#btnPlaceOrder").css("display", "none");
    $("#btnPlaceOrderP").css("display", "block");
}
function numbersOnly(id) {
    var txt = $("#" + id).val();
    txt = txt.replace(/[^0-9]+/g, '');
    $("#" + id).val(txt);
}
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = 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)) + ',' + num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}

function checkPassword() {
    var password = document.getElementById('txtCurrentPassword').value;
    var password1 = document.getElementById('txtNewPassword1').value;
    var password2 = document.getElementById('txtNewPassword2').value;
    if (password.length <= 0) {
        passwordSetError("true", "-- Current password required!");
        return false;
    }
    else {
        passwordSetError("false", null);
    }

    if (password1.length < 6) {
        passwordSetError("true", "-- New password to short!");
        return false;
    }
    else {
        passwordSetError("false", null);
    }
    if (password1 != password2) {
        passwordSetError("true", "-- New passwords do not match!");
        return false;
    }
    else {
        passwordSetError("false", null);
    }
}

function passwordSetError(isError, error) {
    if (isError == "true") {
        $("#lblError").text(error);
    }
    else {
        $("#lblError").text("");
    }
}
function printDate() {
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
document.write("<center><small><font color='000000' face='Arial'>"+montharray[month]+" "+daym+", "+year+"</font></small></center>")
}
