﻿function saSelectRoom(Reference, CacheID, Price) {
    $('Selected_' + Reference).value = CacheID;
    $('BasePrice_' + Reference).value = Price;

    saHighLight(Reference);

    if ($F('OptionsVisible_' + Reference) == 'true') {
        saHideOptions(Reference);
        saLoadOptions(Reference);
    } else {
        saUpdatePrices(Reference);
    };
};


function saHighLight(Reference) {
    var i = 0;

    while ($('tr_r_' + Reference + '_' + i)) {
        if ($('chk_r_' + Reference + '_' + i).checked) {
            $('tr_r_' + Reference + '_' + i).setStyle({ background: '#efefef' });
        } else {
            $('tr_r_' + Reference + '_' + i).setStyle({ background: '#fff' });
        };
        i++;
    };

    i = 0;

    while ($('tr_s_' + Reference + '_' + i)) {
        if ($('chk_s_' + Reference + '_' + i).checked) {
            $('tr_s_' + Reference + '_' + i).setStyle({ background: '#efefef' });
        } else {
            $('tr_s_' + Reference + '_' + i).setStyle({ background: '#fff' });
        };
        i++;
    };
};

function saToggleOptions(Reference) {
    if ($F('OptionsVisible_' + Reference) == 'true') {
        saHideOptions(Reference);        
    } else {
        saLoadOptions(Reference);       
    };    
};

function saHideOptions(Reference) {
    $('Options_' + Reference).hide();
    $('OptionsVisible_' + Reference).value = 'false';
    $('ShowHideOptions_' + Reference).update('Change Board Basis');
    saUpdatePrices(Reference);
};

function saLoadOptions(Reference) {   
    saLoadSupplements($F('Selected_' + Reference), Reference);
};

function saShowOptions(Reference) {
    $('Options_' + Reference).blindDown();
    $('OptionsVisible_' + Reference).value = 'true';
    $('ShowHideOptions_' + Reference).update('Hide Board Basis');
};

function saLoadSupplements(cacheID, Reference) {
    new Ajax.Request('/ajax/Search_Supplements.aspx?cacheID=' + cacheID + '&Adults=' + $F('Adults_' + Reference) + '&Children=' + $F('Children_' + Reference) + '&Infants=' + $F('Infants_' + Reference) + '&AccommodationReference=' + Reference, {
        method: 'get',
        onSuccess: function(transport) {
            $('Options_' + Reference).update(getContent(transport.responseText));
            saShowOptions(Reference);
            saUpdatePrices(Reference);
        }
    });
};

function saSelectSupplement(Reference) {
    saHighLight(Reference);
    saUpdatePrices(Reference);
    
};

function saUpdatePrices(Reference) {
    var base = parseFloat($F('BasePrice_' + Reference));
    var p = 0;
    var i = 0

    if ($F('OptionsVisible_' + Reference) == 'true') {
        while ($('chk_r_' + Reference + '_' + i)) {
            if ($('chk_r_' + Reference + '_' + i).checked) {
                base = parseFloat($F('chk_r_' + Reference + '_' + i));
            };
            i++;
        };

        i = 0;

        while ($('chk_s_' + Reference + '_' + i)) {
            if ($('chk_s_' + Reference + '_' + i).checked) {
                p += parseFloat($F('chk_s_' + Reference + '_' + i));
            };
            i++;
        };
    };

    $('TotalPrice_' + Reference).update(formatCurrency(base + p));

    saHighLight(Reference);
};

function saBasket(Reference) {
    wait('Adding', 'Please wait while we add your accommodation to your basket');

    var s = 'addToBasket.aspx?';

    s += 'cacheID=';
    s += $F('Selected_' + Reference);

    if ($F('OptionsVisible_' + Reference) == 'true') {        
        s += '&Supplements=';

        var i = 0;
        var added = 0;

        while ($('chk_s_' + Reference + '_' + i)) {
            if ($('chk_s_' + Reference + '_' + i).checked) {
                if (added > 0) {
                    s += ',';
                };

                s += $F('cache_s_' + Reference + '_' + i);

                added++;
            };
            i++;
        };
    };

    s += '&Adults=';
    s += $F('Adults_' + Reference);
    s += '&Children=';
    s += $F('Children_' + Reference);
    s += '&Infants=';
    s += $F('Infants_' + Reference);

    document.location.href = s;
};
