/**
* @author cuong.tran
* @email cuong.tran@enclaveit.com, cuongtranquoc@gmail.com
* @function Booking
* @last update: 09/01/2009
*/
IncludeJavaScript('/Js/Tooltip/Tooltip.js');
//------------------------------------------------
var oldRoom = '';
var oldMessage = '';
var isFullcharter = false;

function registerDepartureDatesAction() {
    var selectTag = document.getElementById("cruise_id");
    XBrowserAddHandler(selectTag, "change", function() {
        selectDepartureDates(this)
    });
};
function registerStreamAction() {
    var selectTag = document.getElementById("upstream");
    var selectTag2 = document.getElementById("downstream");
    XBrowserAddHandler(selectTag, "click", function() {
        selectStream(this)
    });
    XBrowserAddHandler(selectTag2, "click", function() {
        selectStream(this)
    });
};
function registerCabinAction() {
    if (document.getElementById("deck_plan")) {
        var i, divTagCount;
        var allDivTag = document.getElementById("deck_plan").getElementsByTagName("div");
        divTagCount = allDivTag.length;
        for (i = 0; i < divTagCount; i++) {
            if (allDivTag[i].id.match(/\bc_/ig)) {
                if ((allDivTag[i].className.match(/c_unavailable/ig)) || (allDivTag[i].className.match(/c_temporality/ig)) || (allDivTag[i].className.match(/c_locked/ig))) {
                    if ((allDivTag[i].className.match(/c_temporality/ig)) || (allDivTag[i].className.match(/c_locked/ig))) {
                        XBrowserAddHandler(allDivTag[i], "mouseover", function() {
                            try {
                                showtip(getMessage(this));
                            }
                            catch (e) {
                            }
                        });
                        XBrowserAddHandler(allDivTag[i], "mouseout", function() {
                            try {
                                hidetip();
                            }
                            catch (e) {
                            }
                        });
                    }
                }
                else {
                    XBrowserAddHandler(allDivTag[i], "click", function() {
                        addToBookingSummary(this)
                    });
                    XBrowserAddHandler(allDivTag[i], "mouseover", function() {
                        try {
                            showtip(getMessage(this));
                        }
                        catch (e) {
                        }
                    });
                    XBrowserAddHandler(allDivTag[i], "mouseout", function() {
                        try {
                            hidetip();
                        }
                        catch (e) {
                        }
                    });
                }
            }
        }
    }
};
function registerFullCharterAction() {
    var btnfullcharter = document.getElementById('btnfullcharter');
    if (btnfullcharter) {
        XBrowserAddHandler(btnfullcharter, "click", function() {
            try {
                return fullCharterBooking(this);
            }
            catch (e) {
            }
        });
        if (btnfullcharter.title != 'Full Charter Booking') {
            isFullcharter = true;
        }
    }
};
function fullCharterBooking(field) {
    if (isFullcharter) {
        var bookingErrorMessage = 'Unknow error occur in disable full charter booking progress!\nPlease try again!';
        if (confirm('Disable full charter booking in this cruise.  Are you sure?')) {
            hidetip();
            iShowMessage('Disable full charter booking', '<img src="/Themes/Ship1/Images/loading.gif" alt="waiting.."/><br />Disable full charter booking in progress, please wait..');
            var returnData = GetData(0, 'http://' + fnGetDomain(window.location.href) + '/Handler/FullBooking.aspx' + '?sid=' + getSecCode() + '&action=3');
            if ((returnData <= 0)) {
                alert(unSuccessMessage);
                hideMessage();
            } else {
                window.location.href = window.location.href;
            }
        }
        return false;
    } else {
        var bookingErrorMessage = 'Unknow error occur in Full charter booking progress!\nPlease try again!';
        var unSuccessMessage = 'Some cabin(s) is being selected by other customer. So you can not full charter booking!';
        if (confirm('Full charter booking will book all cabins in this cruise.  Are you sure?')) {
            hidetip();
            iShowMessage('Full charter booking', '<img src="/Themes/Ship1/Images/loading.gif" alt="waiting.."/><br />Full charter booking in progress, please wait..');
            var returnData = GetData(0, 'http://' + fnGetDomain(window.location.href) + '/Handler/FullBooking.aspx' + '?sid=' + getSecCode() + '&action=2');
            if ((returnData <= 0)) {
                alert(unSuccessMessage);
                //window.location.href = window.location.href;
            } else if (!isNumber(returnData)) {
                alert(bookingErrorMessage);
                //window.location.href = window.location.href;
            }
            window.location.href = window.location.href;
        }
        return false;
    }
};
function isNumber(value) {
    return isFinite((value * 1.0));
};
function checkfullCharterCondition(field) {
    hidetip();
    iShowMessage('Full charter booking', 'Checking Cruise cabin status, please wait..');
    var returnData = GetData(0, 'http://' + fnGetDomain(window.location.href) + '/Handler/FullBooking.aspx' + '?sid=' + getSecCode() + '&action=1');
    hideMessage();
    return (returnData == '1');
};
function getMessage(field) {
    var returnValue = '';
    try {
        var desk = 0;
        var productInfo;
        var temporalityMode = 0;
        var allATag = field.getElementsByTagName("a");
        var aTagCount = allATag.length;
        if (field.className.match(/c_locked/ig)) {
            temporalityMode = 3;
        }
        if (field.className.match(/c_ownertemporality/ig)) {
            temporalityMode = 2;
        }
        if (field.className.match(/c_temporality/ig)) {
            temporalityMode = 1;
        }
        for (var i = (aTagCount - 1); i >= 0; i++) {
            if (allATag[i].className.match(/pInfo$/ig)) {
                productInfo = allATag[i].title.split('|');
                break;
            }
        };
        desk = productInfo[2].substring(0, 1);
        if (productInfo[2] != oldRoom) {
            /*Cabin Info*/
            returnValue += '<p class="title"><b>Cabin ' + productInfo[2] + '</b>';
            switch (temporalityMode) {
                case 1:
                    returnValue += ' (Temporarily booked) <i class="temporality">&nbsp;</i>';
                    break;
                case 2:
                    returnValue += '<i class="cartadd">&nbsp;</i>';
                    break;
                case 3:
                    returnValue += ' (Locked) <i class="locked">&nbsp;</i>';
                    break;
            }
            returnValue += '</p>';
            returnValue += '<p class="info">';
            switch (desk) {
                case '0':
                    returnValue += 'Mekong Deck - Porthole - 248 sq.ft.';
                    break;
                case '1':
                    returnValue += 'Tonle Deck - Balcony & large Fixed Window - 226 sq.ft.';
                    break;
                case '2':
                    returnValue += 'Saigon Deck - Balcony & large Fixed Window - 226 sq.ft.';
                    break;
                case '3':
                    if ((productInfo[2] == '308') || (productInfo[2] == '307')) {
                        returnValue += 'Sadec Deck - Balcony & large Fixed Window - 443 sq.ft.';
                    }
                    else {
                        returnValue += ' Sadec Deck - Balcony & large Fixed Window - 248 sq.ft.';
                    }
                    break;
            }
            returnValue += '</p>';
            /*Prices*/
            returnValue += '<p class="title"><b>Prices</b></p>';
            returnValue += '<table width="100%;" cellspacing="0" cellpadding="0" border="0">';
            if (field.className.match(/c_1p/ig)) {
                returnValue += '<tr><td>&nbsp;</td><td><i class="Single">&nbsp;</i></td><td class="booked">Single:</td><td class="booked">' + productInfo[3] + '</td>';
            }
            else {
                returnValue += '<tr><td>&nbsp;</td><td><i class="Single">&nbsp;</i></td><td>Single:</td><td>' + productInfo[3] + '</td>';
            }
            if (field.className.match(/c_2p/ig)) {
                returnValue += '<td>&nbsp;</td><td><i class="Double">&nbsp;</i></td><td class="booked">Double:</td><td class="booked">' + productInfo[4] + '</td></tr>';
            }
            else {
                returnValue += '<td>&nbsp;</td><td><i class="Double">&nbsp;</i></td><td>Double:</td><td>' + productInfo[4] + '</td></tr>';
            }
            returnValue += '</table>';
            if (temporalityMode == 1) {
                returnValue += '<p class="hint"><i>(Cabin is being selected by other customer)</i></p>';
            }
            oldMessage = returnValue;
            oldRoom = productInfo[2];
        }
        else {
            returnValue = oldMessage;
        }
    }
    catch (e) {
    }
    return returnValue;
};
function getSecCode() {
    var secutitycode = document.getElementById('secutitycode');
    if (secutitycode) {
        return secutitycode.value;
    } else {
        return '';
    }
};
function addToBookingSummary(field) {
    if (isFullcharter) {
        if (confirm('You are using full charter booking mode in this cruise.\nTo add or remove booking for this cabin, you must disable full charter mode.\nDo you want to disable it?')) {
            iShowMessage('Disable full charter booking', '<img src="/Themes/Ship1/Images/loading.gif" alt="waiting.."/><br />Disable full charter booking in progress, please wait..');
            var returnData = GetData(0, 'http://' + fnGetDomain(window.location.href) + '/Handler/FullBooking.aspx' + '?sid=' + getSecCode() + '&action=3');
            if (returnData <= 0) {
                alert(unSuccessMessage);
                hideMessage();
            } else {
                window.location.href = window.location.href;
            }
        }
        return;
    }
    //Show Message
    iShowMessage('Booking', '<img src="/Themes/Ship1/Images/loading.gif" alt="waiting.."/><br />Booking in progress, please wait..');
    //Process...
    var i, aTagCount;
    var productInfo;
    var basketPageURL = '/Basket.aspx?';
    var urlAddToBasket = '';
    var urlRemoveFromBasket = '';
    var ullRemove1Add2ToBasket = '';
    var cruiseAndStream = document.getElementById('cruiseandstream').value;
    var allATag = field.getElementsByTagName("a");
    aTagCount = allATag.length;
    for (i = 0; i < aTagCount; i++) {
        if (allATag[i].className.match(/pInfo$/ig)) {
            productInfo = allATag[i].title.split('|');
        }
        if (allATag[i].className.match(/URLA$/ig)) {
            urlAddToBasket = decryptURL(allATag[i].title);
        }
        if (allATag[i].className.match(/URLR$/ig)) {
            urlRemoveFromBasket = decryptURL(allATag[i].title);
        }
        if (allATag[i].className.match(/URLR1A2$/ig)) {
            ullRemove1Add2ToBasket = decryptURL(allATag[i].title);
        }
    };
    var bookingURL = '';
    if (field.className.match(/cabin c_0p/ig)) {
        field.className = field.className.replace(/cabin c_0p/ig, "cabin c_1p");
        bookingURL = basketPageURL + urlAddToBasket + '&basket_quantity=1' + '&basket_product=' + productInfo[0] + '&productcode=' + productInfo[2] + '_1' + cruiseAndStream;
    }
    else {
        if (field.className.match(/cabin c_1p/ig)) {
            field.className = field.className.replace(/cabin c_1p/ig, "cabin c_2p");
            bookingURL = basketPageURL + ullRemove1Add2ToBasket + '&basket_product=' + productInfo[0] + '&basket_product2=' + productInfo[1] + '&productcode=' + productInfo[2] + '_1' + '&productcode2=' + productInfo[2] + '_2' + cruiseAndStream;
        }
        else {
            field.className = field.className.replace(/cabin c_2p/ig, "cabin c_0p");
            bookingURL = basketPageURL + urlRemoveFromBasket + '&basket_quantity=1' + '&basket_product=' + productInfo[1] + '&productcode=' + productInfo[2] + '_2' + cruiseAndStream;
        }
    }
    window.location.href = bookingURL;
};
function showLoadingPanel(mainElementID, loadingPanelID) {
    var loadingPanel = document.getElementById(loadingPanelID);
    var LoadingPanelDivDesc = document.getElementById(loadingPanelID + '_desc');
    if (loadingPanel && LoadingPanelDivDesc) {
        var mainDivWidth = getElementWidth(mainElementID);
        var mainDivHeight = getElementHeight(mainElementID);

        loadingPanel.style.width = mainDivWidth + 'px';
        loadingPanel.style.height = mainDivHeight + 'px';

        loadingPanel.style.display = 'block';

        LoadingPanelDivDesc.style.top = '100px';
        LoadingPanelDivDesc.style.left = ((mainDivWidth - getElementWidth(loadingPanelID + '_desc')) / 2) + 'px';
    }
};
function decryptURL(inputURL) {
    return inputURL.replace('b_a=', 'basket_action=').replace('b_c=', 'basket_category=').replace('b_i=', 'basket_item=');
};
function selectStream(field) {
    var RQStream = trim(getRequestValue('stream')).toLowerCase();
    try {
        showMask();
    } catch (e) { }
    if (RQStream != field.value) {
        window.location.href = replaceURLPara(removeURLPara('cruise'), 'stream', field.value);
    } else {
        try {
            hideMask();
        } catch (e) { }
    }
};
function selectDepartureDates(field) {
    try {
        showMask();
    } catch (e) { }
    window.location.href = replaceWindowLocationURLPara('cruise', field.value);
};
function iShowMessage(title, content) {
    showMessage(title, content, (((screen.availHeight - getElementHeight('mess')) / 3) + getScrollXY()[1]));
    moveMessage();
};
function showMask() {
    var MessageBodyMask = document.getElementById('bodymask');
    if (MessageBodyMask) {
        MessageBodyMask.style.display = 'block';
        MessageBodyMask.style.zindex = '9000';
        MessageBodyMask.style.width = document.body.clientWidth + 'px';
        MessageBodyMask.style.height = document.body.clientHeight + 'px';
    }
};
function hideMask() {
    var MessageBodyMask = document.getElementById('bodymask');
    if (MessageBodyMask) {
        MessageBodyMask.style.display = 'none';
        MessageBodyMask.style.zindex = '-1';
    }
};

/*Register Main action*/
window.onload = function() {
    registerStreamAction();
    registerDepartureDatesAction();
    //registerRelatedProductAction();
    registerFullCharterAction();
    registerCabinAction();
    hideAllMask();
};