﻿/*
 * This file should contain all javascript dealing with order-related items 
 * such as functions used in:
 *      - Checkout (and Checkout validation related functions)
 *      - Previous Orders
 *      - Active Orders
 *      - Slots
 */


/*
 * ----------------------------------------------------------------------------
 * SLOT FUNCTIONS
 * ---------------------------------------------------------------------------- 
 */

//when the slot view changes(next/previous 7 days) reset the slot that is currently choosen (if any)
//also reset the label that displays the slot time etc

//This is not being called anymore
function SlotViewChanged() {
    var SlotTimeDisplayControlID = $("*[ID$='hfSlotTimeDisplayControlID']").val();
    //when the user clicks next or previous 7 days then we want to reset the slot they have chossen
    $("*[ID$='hfCurrentSlotID']").val("");
    $("*[ID$='" + SlotTimeDisplayControlID + "']").text("");
}


//this is called when the user clicks on a slot
function SlotClicked(newSelectedSlotID, SlotTimeDisplayText,cost,newSelectSlotNearlyFull) {
    var oldTitle, newTitle, oldHrefInnerHtml, newHrefInnerHtml; 
    //get the name of the control that we display the slot date and time to
    var SlotTimeDisplayControlID = $("*[ID$='hfSlotTimeDisplayControlID']").val();

    //get the table cell of the new slot selected
    var tcNewSelectedSlot = $("*[ID$='tcSlotID_" + newSelectedSlotID + "']");
    //get the HtmlAnchor of the new slot selected
    var hrefNewSelectedSlot = $("*[ID$='HrefSlotID_" + newSelectedSlotID + "']");
    
    //check to see if we can find the new slot selected
    if (tcNewSelectedSlot != null) {

        //get the slot id of the previous slot selected
        var previousSelectedSlotID = $("*[ID$='hfCurrentSlotID']").val();
        //get the slot nearlyfull status of the previous selectd slot
        var previousSelectSlotNearlyFull = $("*[ID$='hfSlotIsNearlyFull']").val();
        
        //remove the active css class against the previously selected slot
        var tcPreviousSelectedSlot = $("*[ID$='tcSlotID_" + previousSelectedSlotID + "']");
        var hrefPreviousSelectedSlot = $("*[ID$='HrefSlotID_" + previousSelectedSlotID + "']");
        
        if (tcPreviousSelectedSlot.length > 0) {

            oldTitle = tcPreviousSelectedSlot.attr("title");
            oldHrefInnerHtml = hrefPreviousSelectedSlot.html();

            if (previousSelectSlotNearlyFull.toUpperCase() == "TRUE") {
                //newTitle = oldTitle.replace("Selected", "Nearly Full");
                newHrefInnerHtml = oldHrefInnerHtml.replace("Selected", "Nearly Full");
                hrefPreviousSelectedSlot.removeClass("slotselectedWithWarning");
                hrefPreviousSelectedSlot.addClass("slotfreeWithWarning");
                tcPreviousSelectedSlot.removeClass("slotwarningtablecell");                              
            }
            else {
                newTitle = oldTitle.replace("Selected", "Available");
                newHrefInnerHtml = oldHrefInnerHtml.replace("Selected", "Available");
                hrefPreviousSelectedSlot.removeClass("slotselected");
                hrefPreviousSelectedSlot.addClass("slotfree");
            }
            
            //tcPreviousSelectedSlot.attr("title", newTitle);
            
            hrefPreviousSelectedSlot.html(newHrefInnerHtml);
        }

        $("*[ID$='hfCurrentSlotID']").val(newSelectedSlotID);
        $("*[ID$='hfSlotIsNearlyFull']").val(newSelectSlotNearlyFull);
        
 
        oldHrefInnerHtml = hrefNewSelectedSlot.html();
        newHrefInnerHtml = oldHrefInnerHtml.replace("Available", "Selected");
        newHrefInnerHtml = newHrefInnerHtml.replace("Nearly Full", "Selected");

        //tcNewSelectedSlot.attr("title", newTitle);
        hrefNewSelectedSlot.removeClass("slotfree");
        if (newSelectSlotNearlyFull == "True") {
            //hrefNewSelectedSlot.removeClass("slotcutoff")
            hrefNewSelectedSlot.addClass("slotselectedWithWarning");
            tcNewSelectedSlot.addClass("slotwarningtablecell");
        }
        else {
            hrefNewSelectedSlot.addClass("slotselected");
        }
        
        hrefNewSelectedSlot.html(newHrefInnerHtml);
        //update the label outside the user control to display the slot time text
        $("*[ID$='" + SlotTimeDisplayControlID + "']").text(SlotTimeDisplayText);
    }
}



function CustomValidationForSlotSelected(source, args) {

    var selectedSlotID = $("*[ID$='hfCurrentSlotID']").val();

    //check to see if the hidden field is a number
    var fValue = parseFloat(selectedSlotID);


    if (isNaN(fValue)) {
        args.IsValid = false;
    }
    else {
        args.IsValid = true;
    }

}

/*
 * ----------------------------------------------------------------------------
 * CHECKOUT FUNCTIONS
 * ---------------------------------------------------------------------------- 
 */
 // Prepares the placing of an order by first validating the page.  If the page is 
// valid, we disable the click on the button to prevent user placing order twice
// and related errors.
function PreparePlaceOrder() {

    var placeOrderBtn = $("*[ID$='btnPlaceOrder']");
    var inputValid = ValidatePage();

    if (inputValid) {
        placeOrderBtn.unbind();
        placeOrderBtn.bind("click", function() {
            return false;
        });
    }

    return inputValid;
}

function ValidateCCSecurityCode_ClientValidate(source, args) {

    var ddl = $("SELECT[ID$='ddlCardType']");
    var cardTypeID = ddl.val();


    //if the card is VISA (1) or MasterCard(2) then make sure that something was entered in the security code textbox
    if ((cardTypeID == 1) || (cardTypeID == 2)) {
        var securityCode = args.Value;

        CheckNum = parseInt(securityCode)
        if (isNaN(CheckNum)) {
            args.IsValid = false;
            return false;
        }
        else {
            args.IsValid = true;
            return true;
        }

    }
    else {
        args.IsValid = true;
        return true;
    }


}
 
/*
 * ----------------------------------------------------------------------------
 * PREVIOUS ORDER FUNCTIONS
 * ---------------------------------------------------------------------------- 
 */

//find the row and show the div with text box and two buttons
function EditOrderName(orderref) 
{
    var mainDiv = $('#divOrderName' + orderref);
    mainDiv.find("DIV[ID$='divOrderNameEdit']").find("INPUT[ID$='txtOrderName']").val(jQuery.trim(mainDiv.find("A[ID$='lnkOrderName']").text()));
    mainDiv.find("A[ID$='lnkOrderName']").hide('slow');
    mainDiv.find("DIV[ID$='divOrderNameEdit']").show('slow');
}

//call web service and hide the
function SaveOrderName(orderref) 
{
    var newName = $('#divOrderName' + orderref).find("DIV[ID$='divOrderNameEdit']").find("INPUT[ID$='txtOrderName']").val();
    
    var params = { 'orderref': orderref, 'newName': jQuery.trim(newName) };
    
    var jsonString = "{JsonOrderref: '" + JSON.stringify(params) + "'}";
    
    var serviceURL = resolveURL("WebServices/OrderServices.asmx/UpdateOrderName");
    $.ajax({
        type: "POST",
        url: serviceURL,
        data: jsonString,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function(xhr, desc, exceptionobj) {
            ShowAlert("Sorry, but there was a problem processing this action. Please try again or contact Helpdesk.\n" + xhr.responseText);
        },
        success: function(result) {
            var mainDiv = $('#divOrderName' + orderref);
            mainDiv.find("DIV[ID$='divOrderNameEdit']").hide('slow');

            if (result.d == 1) {
                var name = $('#divOrderName' + orderref).find("DIV[ID$='divOrderNameEdit']").find("INPUT[ID$='txtOrderName']").val();
                if (name.trim() == "") {
                    name = "Name Order";
                }
                mainDiv.find("A[ID$='lnkOrderName']").text(jQuery.trim(name)).show('slow');
            }
            else {
                ShowAlert("Sorry, but there was a problem processing this action. Please try again or contact Helpdesk.");
                mainDiv.find("A[ID$='lnkOrderName']").show('slow');
            }
        }
    });
}

function CancelOrderNameEdit(orderref) 
{
    var mainDiv = $('#divOrderName' + orderref);
    mainDiv.find("A[ID$='lnkOrderName']").show('slow');
    mainDiv.find("DIV[ID$='divOrderNameEdit']").hide('slow');
}
