﻿// Checkout scripts

function removeitem(iid) {
    // greyout the content
    greyoutContent(true);
    // load the list
    // set the variables to pass
    var ctx = {
        greyoutcontent: "true"
    };
    PageMethods.Remove_Item(iid, basketsuccess, ajaxfailure, ctx);
}

function basketsuccess(value, ctx, methodName) { 
    // Create a table for the new content
    var newtable = document.createElement("div");
    newtable.innerHTML = value;
    // Remove the current content
    for (var i=0; i<document.getElementById("yourbasket").childNodes.length; i++) {
        if (document.getElementById("yourbasket").childNodes[i].tagName == "TABLE") {
            document.getElementById("yourbasket").removeChild(document.getElementById("yourbasket").childNodes[i]);
            break;
        }
    }
    // Remove the buttons
    //document.getElementById("yourbasket").removeChild(document.getElementById("buttons"));
    // Add the new content
    document.getElementById("yourbasket").insertBefore(newtable.firstChild, document.getElementById("buttons"));
    
    // If basket is empty rmove the proceed button
    if (value.indexOf("Your shopping bag is currently empty") > -1) {
        document.getElementById("buttons").removeChild(document.getElementById("proceedcheckout"));
    }

    // Hide the loading notice
    greyoutContent(false);
}


function updatebasket() {
    // Get the quantities of each item
    var itemarray = new Array();
    var inputs = document.getElementById("yourbasket").getElementsByTagName("INPUT");
    
    for (var i=0; i<inputs.length; i++) {
        // Process quantity elements
        if (inputs[i].getAttribute("id").startswith("qty")) {
            // Validate
            if (inputs[i].value == "" || !ValidateItem("integer", inputs[i].value)) {
                alert("Quantities must be an integer.");
                return;
            }
            var iitem = {
                "ID": inputs[i].id.replace("qty", ""),
                "Quantity": inputs[i].value
            };
            itemarray[itemarray.length] = iitem;
        }
    
    }

    // greyout the content
    greyoutContent(true);

    // Get edit form
    // set the variables to pass
    var ctx = {
        greyoutcontent: "true"
    };
    PageMethods.Update_Basket(itemarray, basketsuccess, ajaxfailure, ctx);
}

function buyerregister() {
    // Get form values
    var mtitle, mfname, msname, madd1, madd2, madd3, mcity, mcounty, mpcode, mlocation, memail, mtel, mmob, mpass, mcpass;

    mtitle = document.getElementById("ddlTitle").value;
    mfname = document.getElementById("txtForename").value;
    msname = document.getElementById("txtSurname").value;
    madd1 = document.getElementById("txtAddress1").value;
    madd2 = document.getElementById("txtAddress2").value;
    madd3 = document.getElementById("txtAddress3").value;
    mcity = document.getElementById("txtCity").value;
    mcounty = document.getElementById("txtCounty").value;
    mpcode = document.getElementById("txtPostcode").value;
    mlocation = document.getElementById("ddlLocation").value;
    memail = document.getElementById("txtEmail").value;
    mtel = document.getElementById("txtTelephone").value;
    mmob = document.getElementById("txtMobile").value;
    mpass = document.getElementById("txtPassword").value;
    mcpass = document.getElementById("txtConfirmPassword").value;

    // Validate form
    var valForm = new Array();
    if (mfname == "") {
        valForm[valForm.length] = '\r\n Forename is a required field';
    }
    if (msname == "") {
        valForm[valForm.length] = '\r\n Surname is a required field';
    }
    if (madd1 == "") {
        valForm[valForm.length] = '\r\n Address Line 1 is a required field';
    }
    if (mcity == "") {
        valForm[valForm.length] = '\r\n City is a required field';
    }
    if (mpcode == "") {
        valForm[valForm.length] = '\r\n Postcode is a required field';
    }
    if (memail == "") {
        valForm[valForm.length] = '\r\n E-mail Address is a required field';
    }
    else if (!ValidateItem("email", memail)) {
        valForm[valForm.length] = '\r\n Please enter a valid e-mail address';
    }
    if (mtel == "") {
        valForm[valForm.length] = '\r\n Telephone No. is a required field';
    }
    if (mpass == "") {
        valForm[valForm.length] = '\r\n Password is a required field';
    }
    if (mcpass == "") {
        valForm[valForm.length] = '\r\n Confirm Password is a required field';
    }
    else if(mpass != mcpass) {
        valForm[valForm.length] = '\r\n Your passwords do not match';
    }

    // Check for invalid entries
    if (valForm.length > 0) {
        alert('Some errors occurred in your submission: \r\n' + valForm.toString());
    }
    else {
        // Show the loading notice
        greyoutContent(true);

        // set the variables to pass
        var ctx = {
            greyoutcontent: "true"
        };
        PageMethods.Register_Buyer(mtitle, mfname, msname, madd1, madd2, madd3, mcity, mcounty, mpcode, mlocation, memail, mtel, mmob, mpass, buyerregistersuccess, ajaxfailure, ctx);
    }
}

function buyerregistersuccess(value, ctx, methodName) { 
    // Create a table for the new content
    if (value == "email") {
        alert("The e-mail address specified has already been registered. Please use the sign in form to the right to sign in.")
        // Hide the loading notice
        greyoutContent(false);
        return;
    }
    else if (value == "basket") {
        // Redirect to basket page
        window.location.href = "view-bag.aspx";
        // Hide the loading notice
        greyoutContent(false);
        return;
    }
    
    window.location.href = "checkout-confirm.aspx";
    
    // Hide the loading notice
    greyoutContent(false);
}

function buyersignin() {
    var memail, mpass;
    memail = document.getElementById("txtSIEmail").value;
    mpass = document.getElementById("txtSIPassword").value;

    // Validate form
    var valForm = new Array();
    if (memail == "") {
        valForm[valForm.length] = '\r\n E-mail Address is a required field';
    }
    else if (!ValidateItem("email", memail)) {
        valForm[valForm.length] = '\r\n Please enter a valid e-mail address';
    }
    if (mpass == "") {
        valForm[valForm.length] = '\r\n Password is a required field';
    }

    // Check for invalid entries
    if (valForm.length > 0) {
        alert('Some errors occurred in your submission: \r\n' + valForm.toString());
    }
    else {
        // Show the loading notice
        greyoutContent(true);
        // set the variables to pass
        var ctx = {
            greyoutcontent: "true"
        };
        PageMethods.Buyer_Signin(memail, mpass, buyersigninsuccess, ajaxfailure, ctx);
    }
}

function buyersigninsuccess(value, ctx, methodName) { 
    // Create a table for the new content
    if (value == "false") {
        alert("The e-mail address and password combination was incorrect.")
        // Hide the loading notice
        greyoutContent(false);
        return;
    }
    else if (value == "inactive") {
        alert("Your account has been disabled. Please contact us for further information.")
        // Hide the loading notice
        greyoutContent(false);
        return;
    }
    else if (value == "basket") {
        // Redirect to basket page
        window.location.href = "view-bag.aspx";
        // Hide the loading notice
        greyoutContent(false);
        return;
    }

    document.getElementById("yourdetails").innerHTML = '<div id="greyoutcontent" class="hidden"><img src="/_resources/files/ajax-loader.gif" alt="Loading, please wait" title="Loading, please wait" /></div>' + value;
    // Hide the loading notice
    greyoutContent(false);
}

function buyerupdate() {
    // Get form values
    var mtitle, mfname, msname, madd1, madd2, madd3, mcity, mcounty, mpcode, mlocation, mtel, mmob;

    mtitle = document.getElementById("ddlTitle").value;
    mfname = document.getElementById("txtForename").value;
    msname = document.getElementById("txtSurname").value;
    madd1 = document.getElementById("txtAddress1").value;
    madd2 = document.getElementById("txtAddress2").value;
    madd3 = document.getElementById("txtAddress3").value;
    mcity = document.getElementById("txtCity").value;
    mcounty = document.getElementById("txtCounty").value;
    mpcode = document.getElementById("txtPostcode").value;
    mlocation = document.getElementById("ddlLocation").value;
    mtel = document.getElementById("txtTelephone").value;
    mmob = document.getElementById("txtMobile").value;

    // Validate form
    var valForm = new Array();
    if (mfname == "") {
        valForm[valForm.length] = '\r\n Forename is a required field';
    }
    if (msname == "") {
        valForm[valForm.length] = '\r\n Surname is a required field';
    }
    if (madd1 == "") {
        valForm[valForm.length] = '\r\n Address Line 1 is a required field';
    }
    if (mcity == "") {
        valForm[valForm.length] = '\r\n City is a required field';
    }
    if (mpcode == "") {
        valForm[valForm.length] = '\r\n Postcode is a required field';
    }
    if (mtel == "") {
        valForm[valForm.length] = '\r\n Telephone No. is a required field';
    }

    // Check for invalid entries
    if (valForm.length > 0) {
        alert('Some errors occurred in your submission: \r\n' + valForm.toString());
    }
    else {
        // Show the loading notice
        greyoutContent(true);

        // set the variables to pass
        var ctx = {
            greyoutcontent: "true"
        };
        PageMethods.Update_Buyer_Details(mtitle, mfname, msname, madd1, madd2, madd3, mcity, mcounty, mpcode, mlocation, mtel, mmob, buyerupdatesuccess, ajaxfailure, ctx);
    }
}

function buyerupdatesuccess(value, ctx, methodName) { 
    // Create a table for the new content
    if (value == "login") {
        alert("Your session has ended, please login again.")
        window.location.href = "ciccia-checkout-yourdetails.aspx"
        // Hide the loading notice
        greyoutContent(false);
        return;
    }
    
    document.getElementById("yourdetails").innerHTML = '<div id="greyoutcontent" class="hidden"><img src="/_resources/files/ajax-loader.gif" alt="Loading, please wait" title="Loading, please wait" /></div>' + value;
    // Hide the loading notice
    greyoutContent(false);
}

function togglechangedetails(showform) {
    if (showform) {
        document.getElementById("memberdetails").className = "hidden";
        document.getElementById("changememberdetails").className = "register";
    }
    else {
        document.getElementById("memberdetails").className = "";
        document.getElementById("changememberdetails").className = "hidden";
    }
}

function createorder() {
    // Show the loading notice
    greyoutContent(true);

    // set the variables to pass
    var ctx = {
        greyoutcontent: "true"
    };
    PageMethods.Create_Order(createordersuccess, ajaxfailure, ctx);
}

function createordersuccess(value, ctx, methodName) { 
    // Hide the loading notice
    greyoutContent(false);
    return true;
}
