var ecomutil = {};
ecomutil.init = function (minDate, maxDate) {
    ecomutil.initDatePicker(minDate, maxDate);
};
ecomutil.dayOfWeekBlackouts = [];
ecomutil.blackouts = [];
ecomutil.initDatePicker = function (minDate, maxDate) {
    $('#startDate').datepicker({
        minDate: new Date(minDate),
        maxDate: new Date(maxDate),
        beforeShowDay: function (date) {
            ecomutil.log('beforeShowDay day of mo: ' + date.getDate());
            // date range blackouts
            for (var j = 0, len = ecomutil.blackouts.length; j < len; j++) {
                ecomutil.log('DateRange blackout start/end: ' + ecomutil.blackouts[j][0] + ' / ' + ecomutil.blackouts[j][1] + ' calCheckDate:' + date);
                if (date.getTime() >= new Date(ecomutil.blackouts[j][0]).getTime() && date.getTime() <= new Date(ecomutil.blackouts[j][1]).getTime()) {
                    ecomutil.log('DateRange Blackedout');
                    return [false, '', 'Not available ' + ecomutil.blackouts[j][0] + ' - ' + ecomutil.blackouts[j][1]];
                }
            }
            // day of week blackouts
            for (var i = 0, dlen = ecomutil.dayOfWeekBlackouts.length; i < dlen; i++) {
                ecomutil.log('Weekday blackout:' + ecomutil.dayOfWeekBlackouts[i] + ' calCheckDay:' + date.getDay());
                if (ecomutil.dayOfWeekBlackouts[i] == date.getDay()) {
                    ecomutil.log('Weekday Blackedout');
                    return [false, '', 'Not available on ' + ecomutil.getFriendlyDayOfWk(date.getDay())];
                }
            }
            return [true];
        }
    });
};
ecomutil.setQuantity = function (node) {
    var total = node.parentNode.previousSibling.innerHTML.replace('$', '') * node.options[node.selectedIndex].value;
    node.parentNode.nextSibling.innerHTML = '$' + total.toFixed(2);
};
ecomutil.bDebug = true;
ecomutil.log = function (out) {
    if (this.bDebug && typeof console !== 'undefined') {
        console.log(out);
    }
}
ecomutil.getFriendlyDayOfWk = function (dayIdx) {
    switch (dayIdx) {
        case 0:
            return 'Sunday';
        case 1:
            return 'Monday';
        case 2:
            return 'Tuesday';
        case 3:
            return 'Wednesday';
        case 4:
            return 'Thursday';
        case 5:
            return 'Friday';
        case 6:
            return 'Saturday';
    }
    return 'unknown';
};
ecomutil.blockUI = function (prodType) {
    $.blockUI({ message: '<div class="detailsLoading">' +
		'<div class="details" style="padding:5px;">' +
			'<div style="padding-right:10px; float:left;">' +
				'<img src="/e/Mmsa.Ecom.Web.UI/Full/images/Spinners/whitebg.gif" />' +
			'</div>' +
			'<div class="msg" style="line-height:16px;">' +
				prodType +
			'</div>' +
		'</div>' +
	'</div>'
    });
};

ecomutil.searchingBlockUI = function (prodType) {
    ecomutil.blockUI('searching for ' + prodType + ' - please wait...');
};
ecomutil.addingBlockUI = function (prodType) {
    ecomutil.blockUI('adding ' + prodType + ' to cart - please wait...');
};
ecomutil.removingBlockUI = function (prodType) {
    ecomutil.blockUI('removing ' + prodType + ' from cart - please wait...');
};
ecomutil.genericAutoPostback = function (prodType, autoSearch) {
    $(document).ready(function () {
        if (autoSearch) {
            $('#search input, #search select').change(function () {
                ecomutil.searchingBlockUI(prodType);
                $('#btnSearch').click();
            });
        }
        else {
            $('#search input, #search select').change(function () {
                $('#results input')
                    .attr('disabled', 'disabled')
                    .attr('title', 'Click Update Results')
                    .css('background-color', '#CCCCCC')
                    ;
            });
            $('#btnSearch').click(function () {
                ecomutil.searchingBlockUI(prodType);
            });
        }
        $('#results input').click(function () {
            ecomutil.addingBlockUI(prodType);
        });
    });
};

ecomutil.liftTicketsAutoPostback = function () {
    $('#startDate,#DOBMo,#DOBDay,#DOBYear,#ageTypeDD').change(function () {
        ecomutil.searchingBlockUI('RFID Tickets');
        //$('#SearchButton').click();
        $('form')[0].submit();
    });
};
ecomutil.liftReloadsAutoPostback = function () {
    $('#SearchButton').click(function () {
        ecomutil.searchingBlockUI('RFID Reloads');
    });
    $('#wtp1,#wtp2,#wtp3,#startDate,#DOBMo,#DOBDay,#DOBYear').change(function () {
        // make sure all wtp fields have valid value before posting
        if ($('#wtp1').val().length == 0 || $('#wtp2').val().length == 0 || $('#wtp3').val().length == 0) {
            return;
        }
        if ($('#DOBMo').length > 0) {
            // mo,day,yr dropdowns are present so make sure all have valid value before posting
            if ($('#DOBMo').val().length == 0 || $('#DOBDay').val().length == 0 || $('#DOBYear').val().length == 0) {
                return;
            }
        }
        else if ($('#startDate').length > 0) {
            if ($('#startDate').val().length == 0) {
                return;
            }
        }
        //            $('#SearchButton').click();
        ecomutil.searchingBlockUI('RFID Reloads');
        $('form')[0].submit();

    });
};

