﻿var inventoryPoolAsyncUpdates = 0;

function AddInventoryPoolDate(selectedCell, calendarControlId, dateValue, cartIndex, currentOrderId, currentDatesSelectedControlId, totalDatesSelectedControlId) {
    var currentDaysSelectedControl = $get(currentDatesSelectedControlId);
    var currentDaysSelected = currentDaysSelectedControl.innerHTML;
    var totalDaysToSelect = $get(totalDatesSelectedControlId).innerHTML;
    var calendarControl = $find(calendarControlId);
    
    if (currentDaysSelected >= totalDaysToSelect && selectedCell.className == 'cellDateAvailable') {
        alert(strInventoryPoolDaysExceeded);
        return;
    }

    var selectedDateArr = new Array()
    selectedDateArr[0] = selectedCell;
    selectedDateArr[1] = currentDatesSelectedControlId;
    selectedDateArr[2] = totalDatesSelectedControlId;
    selectedDateArr[3] = calendarControlId;
    
    inventoryPoolAsyncUpdates++;
    RTP.Web.WebStore.Site.Scripts.Services.WebStore.SelectInventoryDate(dateValue, cartIndex, currentOrderId, SuccessAddInventoryPoolDate, FailedAddInventoryPoolDate, selectedDateArr);

    if (selectedCell.className == 'cellDateSelected') {
        selectedCell.className = 'cellDateAvailable';
        currentDaysSelected--;
    }
    else {
        selectedCell.className = 'cellDateSelected';
        currentDaysSelected++;
    }
}

function SuccessAddInventoryPoolDate(result, returnArray, methodName) {
    inventoryPoolAsyncUpdates--;

    if (result == null) {
        UndoUpdates(returnArray);
    }
    else {

        var cells = new Array();
        $("#" + returnArray[3] + " td").each(function() {
            cells.push($(this));
        });

        for (index = 0; index < cells.length; index++) {
            var dateFound = false;
            var cellDate = cells[index].context.getAttribute('cellDate');
            if (cellDate != null && cellDate != 'undefined') {
                for (dateIndex = 0; dateIndex < result.length; dateIndex++) {
                    if (cellDate == result[dateIndex]) {
                        dateFound = true;
                        break;
                    }   
                }
            }

            if (dateFound == true) {
                cells[index].context.className = 'cellDateSelected';
            } 
            else {
                if (cells[index].context.className == 'cellDateSelected') {
                    cells[index].context.className = 'cellDateAvailable'
                }
            }
        }
        
        $get(returnArray[1]).innerHTML = result.length;
    }
}

function FailedAddInventoryPoolDate(result, stateControls, methodName) {
    inventoryPoolAsyncUpdates--;
    UndoUpdates(returnArray);
}

function UndoUpdates(returnArray) {
    var currentDaysSelectedControl = $get(returnArray[1]);
    var currentDaysSelected = currentDaysSelectedControl.innerHTML;

    var totalDaysToSelect = $get(returnArray[2]).innerHTML;

    if (returnArray[0].className == 'cellDateSelected') {
        returnArray[0].className == 'cellDateAvailable';
        currentDaysSelected--;
    }
    else {
        returnArray[0].className == 'cellDateSelected';
        currentDaysSelected++;
    }
    currentDaysSelectedControl.innerHTML = currentDaysSelected;

    alert('Unable to edit selected date');
}

//The logic here is a bit tough to follow, but the next two validation functions work in conjunction
//to make sure that all async postbacks have completed and that the number of selected dates is valid.


//This function checks to make sure all of the async postbacks have compeleted.  If they haven't then, we set a timeout
//to reclick the submit button to give the async postback time to complete.
function validateAsyncCompleted(sender, args) {
    if (inventoryPoolAsyncUpdates != 0) {
        var submitButtonId = sender.attributes["submitControlId"].value;
        setTimeout("$get(\'" + submitButtonId + "\').click();", 500);
        args.IsValid = false;
    }
    else {
        args.IsValid = true;
    }
}

//Now we check to make sure that the total selected dates equals the number of dates that we need.
function validateSelectedDates(sender, args) {
    var selectedDates = $get(sender.attributes["selectedDatesControlId"].value).innerHTML;
    var totalDates = $get(sender.attributes["totalDatesControlId"].value).innerHTML;

    args.IsValid = selectedDates == totalDates || inventoryPoolAsyncUpdates != 0;
}
