﻿

// ********************************************************************************************
// Index.aspx
//
function loadDeliveryCountry() {
    $("div[id^=DeliveryMethod]").hide();
    var IndexDeliverCountry = document.getElementById("DeliveryCountry").selectedIndex
    if (IndexDeliverCountry < 1) {
        $("div[id=DeliveryMethodAustralia]").css("display", "block");
    }
    else {
        $("div[id=DeliveryMethodWorld]").css("display", "block");
    }
}

function alertDeskFlagsNoBase() {
    var result = document.getElementById("showWarnDF").value;
    if (result == "True") {
        return confirm("WARNING\nYour cart has Desk-flags, but NO bases\nBases are sold seperately\n" +
                                    "Click OK to continue checkout, Click Cancel to continue shopping");
    }
}

// ********************************************************************************************
// Checkout.aspx
//

function checkoutDummyValues() {
    $("[id=Surname]").val("Bunny");
    $("[id=Firstname]").val("Bugs");
    document.getElementById("Email").value = "b.bunny@storefront.com.au";
    document.getElementById("Telephone").value = "9999 8888";
    document.getElementById("Company").value = "Acme Pty Ltd";
    document.getElementById("Line1").value = "111 Kirribilli Av";
    document.getElementById("City").value = "Kirribilli";
    document.getElementById("State").value = "NSW";
    document.getElementById("Zip").value = "2061";

    document.getElementById("ShipSurname").value = "Kelly";
    document.getElementById("ShipFirstname").value = "Ned";
    document.getElementById("ShipCompany").value = "ABC123 and Sons";
    document.getElementById("ShipLine1").value = "13 Ranger St";
    document.getElementById("ShipLine2").value = "unit 3";
    document.getElementById("ShipCity").value = "Glenrowan";
    document.getElementById("ShipState").value = "Vic";
    document.getElementById("ShipZip").value = "3675";
    document.getElementById("Comments").value = "This online shopping cart is so simple to use, i will buy from you again";
}

function shipDetailVisibility() {
    result = document.getElementById("customerDetail").ShipDetailsSame[0].checked;
    if (result) {
        $("fieldset[id=shippingDetails]").hide();
    } else {
        $("fieldset[id=shippingDetails]").css("display", "block");
    }
}

// validation routines from here:
// http://www.w3schools.com/js/js_form_validation.asp

function validateCheckOut(thisform) {
    with (thisform) {
        if (!isValidEmail(Email.value)) {
            Email.focus();
            return false;
        }
        else if (!validate_required(Firstname, "Firstname is Required")) {
            Firstname.focus();
            return false;
        }
        else if (!validate_required(Surname, "Surname is Required")) {
            Surname.focus();
            return false;
        }
        else if (!validate_required(Line1, "Address Line1 is Required")) {
            Line1.focus();
            return false;
        }
        else if (!validate_required(City, "Suburb is Required")) {
            City.focus();
            return false;
        }
        else if (!validate_required(State, "State is Required")) {
            State.focus();
            return false;
        }
        else if (!validate_required(Zip, "Postcode is Required")) {
            Zip.focus();
            return false;
        }
        else if (Country.value == "Australia") {
            if(!australiaPostCodeIsValid(Zip.value, "Postcode should be between 0 and 9999")){
                Zip.focus();
                return false;
            }
        }

        if (! ShipDetailsSame[0].checked) {
            if (!validate_required(ShipFirstname, "Ship to Firstname is Required")) {
                ShipFirstname.focus();
                return false;
            }
            else if (!validate_required(ShipSurname, "Ship to Surname is Required")) {
                ShipSurname.focus();
                return false;
            }
            else if (!validate_required(ShipLine1, "Ship to Address Line1 is Required")) {
                ShipLine1.focus();
                return false;
            }
            else if (!validate_required(ShipCity, "Ship to Suburb is Required")) {
                ShipCity.focus();
                return false;
            }
            else if (!validate_required(ShipState, "Ship to State is Required")) {
                ShipState.focus();
                return false;
            }
            else if (!validate_required(ShipZip, "Ship to Postcode is Required")) {
                ShipZip.focus();
                return false;
            }
            else if (ShipCountry.value == "Australia") {
            if (!australiaPostCodeIsValid(ShipZip.value, "Ship To Postcode should be between 0 and 9999")) {
                    ShipZip.focus();
                    return false;
                }
            }
        }
        return true;
    }
}


function isValidEmail(_email) {
    var reEmail = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    if (!reEmail.test(_email)) {
        alert("email address Not valid");
        return false;
    }
    return true;
}

function validate_required(field, alerttxt) {
    with (field) {
        if (value == null || $.trim(value) == "") {
            alert(alerttxt);
            return false;
        }
        else {
            return true;
        }
    }
}

function australiaPostCodeIsValid(_pcode, alerttxt) {
    if ( isNaN(Number(_pcode)) || _pcode < 0 || _pcode > 9999) {
        alert(alerttxt);
        return false;
    }
    return true;
}


// ********************************************************************************************
// Review.aspx
//

function paymentType(type) {
    $("fieldset[id^=payment]").hide();
    switch (type) {
        case "cc":
            $("fieldset[id=paymentCC]").css("display", "block");
            break;
        case "dd":
            $("fieldset[id=paymentDD]").css("display", "block");
            break;
        case "mf":
            $("fieldset[id=paymentMF]").css("display", "block");
            break;
        default:
            alert("Error, with payment selection");
    }
}

function validateReview(thisform) {
    with (thisform) {
        if (PaymentMethod[0].checked) {
            if (!validate_required(CardNumber, "CreditCard Number is Required")) {
                CardNumber.focus();
                return false;
            }
            else if (!isValidCardNum(CardNumber.value)) {
                alert("Credit Card number is invalid");
                CardNumber.focus();
                return false;
            }
            else if (!validate_required(CardholderName, "Card Holder Name is Required")) {
                CardholderName.focus();
                return false;
            }
//            else if (!ccExpiryIsInTheFuture(ExpiryMonth.value, ExpiryYear.value)) {
//                alert("Credit Card Expiry must be in the future");
//                ExpiryMonth.focus();
//                return false;
//            }
        }
        return true;
    }
}

function isValidCardNum(sText) {
    // remove dash and spaces if any
    sText = sText.replace(/[\s,-]/g, "");
    var reCardNum = /^[4-5]\d{15}$/;
    if (reCardNum.test(sText)) {
        return luhnCheckSum(sText);
    } else {
        return false;
    }
}

function luhnCheckSum(sCardNum) {
    var iOddSum = 0;
    var iEvenSum = 0;
    var bIsOdd = true;
    for (var i = sCardNum.length - 1; i >= 0; i--) {
        var iNum = parseInt(sCardNum.charAt(i));
        if (bIsOdd) {
            iOddSum += iNum;
        } else {
            iNum = iNum * 2;
            if (iNum > 9) {
                iNum = eval(iNum.toString().split("").join("+"));
            }
            iEvenSum += iNum;
        }
        bIsOdd = !bIsOdd;
    }
    return ((iEvenSum + iOddSum) % 10 == 0);
}

function ccExpiryIsInTheFuture(expireMonth, expireYear) {
    var now = new Date();
    var day1ThisMonth = new Date(now.getFullYear(), now.getMonth(), 01);
    var ccExpireDate = new Date(expireYear, expireMonth - 1, 01);
    return ccExpireDate >= day1ThisMonth;
}