﻿var aeOL = []; function addEvent(o, n, f, l) { var a = "addEventListener", h = "on" + n, b = "", s = ""; if (o[a] && !l) { return o[a](n, f, false); } o._c |= 0; if (o[h]) { b = "_f" + o._c++; o[b] = o[h]; } s = "_f" + o._c++; o[s] = f; o[h] = function(e) { e = e || window.event; var r = true; if (b) { try { r = o[b](e) != false && r; } catch (ex) { r = false; } } r = o[s](e) != false && r; return r; }; aeOL[aeOL.length] = { o: o, h: h }; }

var gFadeTimeout = null;
var gDelayTimeout = null;
var gOpacity = 0;
var gUrl = '';

function showInformationDelay(url, e, buttons) {
    showInformation(url, e.clientX, e.clientY, buttons);
}

function getScrollLeft() {
    return document.documentElement.scrollLeft;
}

function getScrollTop() {

    return document.documentElement.scrollTop;
}

function getAvailWidth() {
    return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.documentElement != null ? document.documentElement.clientWidth : null;
}

function getAvailHeight() {
    return window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.documentElement != null ? document.documentElement.clientHeight : null;
}

function showInformation(url, clientX, clientY, buttons) {

    if (divInformation != null) {

        divInformation.style.left = ((clientX + getScrollLeft())) + 'px';
        divInformation.style.top = ((clientY + getScrollTop()) - 26) + 'px';
        divPointer.style.paddingTop = '20px';

        if (true || url != gUrl) {
        
            ifInformation.style.display = 'none';
            ifInformation.style.width = '486px';
            ifInformation.style.height = '1px';
            ifInformation.src = 'about:blank';
            ifInformation.src = url;
            gUrl = url;

            if (buttons == null) {
                buttons = new Array({ text: 'Close Window', onclick: 'hideInformation()' });
                document.getElementById('divInformationHeader').innerHTML = '';
            }
            else {
                document.getElementById('divInformationHeader').innerHTML = '<h1>Terms &amp; Conditions</h1><p>Please read the following terms &amp; conditions before completing this booking.<br />Once you have read them, click <b>Accept</b> if you accept.</p>';
            }
    
            document.getElementById('divInformationButtons').innerHTML = '';
            
            for (buttonIndex in buttons) {
                document.getElementById('divInformationButtons').innerHTML += ' <div onclick="' + buttons[buttonIndex].onclick + '" onmouseover="this.style.backgroundColor = \'#cccccc\'" onmouseout="this.style.backgroundColor = \'#eeeeee\'" style="padding: 3px 10px 3px 10px; border: solid 1px #666666; background-color: #eeeeee; display: inline; cursor: pointer">' + buttons[buttonIndex].text + '</div> ';
            }

            showProgress();
            fadeInInformation();
            positionInformation();
        }
        else {
            fadeInInformation();
            hideProgress();
            positionInformation();
        }
    }
}

function childResized(w, h) {

    var availWidth = getAvailWidth();
    var availHeight = getAvailHeight();
    var allowScroll = false;

    if (w > availWidth / 2) { w = availWidth / 2; allowScroll = true };
    if (h > availHeight / 2) { h = availHeight / 2; allowScroll = true };

    frames['ifInformation'].contentWindow.resizeTo(w, h);

    ifInformation.style.overflow = (allowScroll ? 'auto' : 'hidden');
    ifInformation.style.width = w + 'px';
    ifInformation.style.height = (h + 20) + 'px';
    
    hideProgress();
    positionInformation();
}

function positionInformation() {

    var availWidth = getAvailWidth();
    var availHeight = getAvailHeight();
    
    if ((divInformation.offsetLeft - getScrollLeft()) + divInformation.clientWidth > availWidth)
        divInformation.style.left = (getScrollLeft() + ((availWidth - (divInformation.clientWidth)))) + 'px';

    if ((divInformation.offsetTop - getScrollTop()) + divInformation.clientHeight > availHeight) {
        divPointer.style.paddingTop = (((divInformation.offsetTop - getScrollTop()) + divInformation.clientHeight + 14) - availHeight) + 'px';
        divInformation.style.top = (getScrollTop() + ((availHeight - (divInformation.clientHeight)))) + 'px';
    }
}

function showProgress() {
    if (divProgress != null) {
        divProgress.style.display = 'block';
        divProgress.style.visibility = 'visible';
    }
}

function hideProgress() {
    if (divProgress != null) {
        divProgress.style.display = 'none';
        divProgress.style.visibility = 'hidden';
    }
}

function hideInformation() {
    if (window.gDelayTimeout != null)
        window.clearTimeout(gDelayTimeout);

    fadeOutInformation();
}

function fadeInInformation() {
    if (window.gDelayTimeout != null)
        window.clearTimeout(gDelayTimeout);

    if (divInformation != null) {
        divInformation.style.display = 'block';
        divInformation.style.visibility = 'visible';

        gOpacity += 100;

        if (gOpacity > 100) gOpacity = 100;

        if (!divInformation.style.setProperty)
            divInformation.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + gOpacity + ')';
        else
            divInformation.style.setProperty('-moz-opacity', gOpacity / 100, '');

        if (gOpacity < 100)
            gFadeTimeout = window.setTimeout('fadeInInformation();', 1);
        else
            window.clearTimeout(gFadeTimeout);
    }
}

function fadeOutInformation() {
    if (divInformation != null) {
        divInformation.style.display = 'block';
        divInformation.style.visibility = 'visible';

        gOpacity -= 100;

        if (gOpacity < 0) gOpacity = 0;

        if (!divInformation.style.setProperty)
            divInformation.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + gOpacity + ')';
        else
            divInformation.style.setProperty('-moz-opacity', gOpacity / 100, '');

        if (gOpacity > 0)
            gFadeTimeout = window.setTimeout('fadeOutInformation();', 1);
        else {
            window.clearTimeout(gFadeTimeout);
            divInformation.style.display = 'none';
            divInformation.style.visibility = 'hidden';
        }
    }
}

function containsDOM(container, containee) {
    var isParent = false;
    do {
        if ((isParent = container == containee))
            break;
        containee = containee.parentNode;
    }
    while (containee != null);
    return isParent;
}

function checkMouseEnter(element, evt) {
    if (element.contains && evt.fromElement) {
        return !element.contains(evt.fromElement);
    }
    else if (evt.relatedTarget) {
        return !containsDOM(element, evt.relatedTarget);
    }
}

function checkMouseLeave(element, evt) {
    if (element.contains && evt.toElement) {
        return !element.contains(evt.toElement);
    }
    else if (evt.relatedTarget) {
        return !containsDOM(element, evt.relatedTarget);
    }
}

var gMoveTimeout;

function extendDatesDelay(pos, dir, appValidUserId) {

    var isUpdateRequired = false;

    if (tblSearchResults.rows.length > 1 && (noNights > 1 || pos * dir > 0)) {

        var tempDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate());
        var tempToday = new Date();

        if (pos < 0)
            tempDate.setDate(tempDate.getDate() + ((pos * dir) * -1));
        else if (pos > 0)
            tempDate.setDate(tempDate.getDate() + ((noNights + (pos * dir)) - 1));

        tempToday.setHours(0, 0, 0, 0);

        if (tempDate < tempToday && !appValidUserId) {
            alert('Only bookings from today\'s date forward can be made');
            return;
        }

        if (pos < 0)
            startDate.setDate(startDate.getDate() + ((pos * dir) * -1));

        noNights += (pos * dir);

        var ddlNumberOfNights = document.getElementById('ddlNumberOfNights');
        
        if (ddlNumberOfNights)
            ddlNumberOfNights.value = noNights;

        var currentDate = new Date(startDate);

        if (pos > 0) {
            currentDate.setDate(currentDate.getDate() + (noNights - 1));
        }

        for (var rowIndex = 0; rowIndex < tblSearchResults.rows.length; rowIndex++) {

            var row = tblSearchResults.rows[rowIndex];
            if (row.cells.length > 2) {
                isUpdateRequired = true;

                var cell = null;
                if (dir < 0) {
                    if (pos < 0)
                        cell = row.insertCell(2);
                    else if (pos > 0)
                        row.removeChild(row.cells[row.cells.length - 4]);
                }
                else if (dir > 0) {
                    if (pos < 0)
                        row.removeChild(row.cells[2]);
                    else if (pos > 0)
                        cell = row.insertCell(row.cells.length - 3);
                }

                if (cell != null) {
                    cell.style.width = '46px';

                    if (row.id.indexOf('operator') == 0) {
                        cell.rowSpan = 2;
                        cell.className = ((currentDate.getDay() == 0 || currentDate.getDay() == 6) ? 'weekEnd' : 'weekDay');
                        if (dir < 0) {
                            cell.innerHTML = currentDate.format('ddd<br/>d<br/>mmm');
                        }
                        else if (dir > 0) {
                            cell.innerHTML = currentDate.format('ddd<br/>d<br/>mmm');
                        }
                    }
                    else {
                        cell.className = ((currentDate.getDay() == 0 || currentDate.getDay() == 6) ? 'loadingWeekEnd' : 'loadingWeekDay');
                        cell.innerHTML = 'TBA';
                    }
                }

                if (row.id.indexOf('operator') != 0) {
                    for (var x = 2; x < row.cells.length - 2; x++) {

                        var tempDate = new Date(startDate);

                        tempDate.setDate(tempDate.getDate() + (x - 2));

                        if (x < row.cells.length - 3)
                            row.cells[x].className = ((tempDate.getDay() == 0 || tempDate.getDay() == 6) ? 'loadingWeekEnd' : 'loadingWeekDay');
                        else
                            row.cells[x].className = 'loadingTotal';

                        row.cells[x].innerHTML = 'TBA';
                    }

                    row.cells[row.cells.length - 1].innerHTML = 'TBA';
                }
            }
            else if (row.cells.length == 1)
                row.cells[0].colSpan += (pos * dir);
        }
    }

    if (isUpdateRequired) {
        var txtArrivalDate = document.getElementById('txtArrivalDate');

        if (txtArrivalDate)
            txtArrivalDate.value = startDate.format('d/mm/yyyy');

        var ddlNumberOfNights = document.getElementById('ddlNumberOfNights');

        if (ddlNumberOfNights)
            ddlNumberOfNights.value = noNights;

        window.clearTimeout(gMoveTimeout);
        window.gMoveTimeout = window.setTimeout('moveDates(' + pageIndex + ', \'' + startDate.format('yyyy/mm/dd') + '\', ' + noNights + ');', 1000);
    }
}

function moveDatesDelay(dir, appValidUserId) {

    var tempDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate());
    var tempToday = new Date();

    tempDate.setDate(tempDate.getDate() + dir);
    tempToday.setHours(0, 0, 0, 0);

    if (tempDate < tempToday && !appValidUserId) {
        alert('Only bookings from today\'s date forward can be made');
        return;
    }
    
    startDate.setDate(startDate.getDate() + dir);

    var txtArrivalDate = document.getElementById('txtArrivalDate');
    
    if (txtArrivalDate)
        txtArrivalDate.value = startDate.format('d/mm/yyyy');
        
    for (var rowIndex = 0; rowIndex < tblSearchResults.rows.length; rowIndex++) {
        var row = tblSearchResults.rows[rowIndex];
        if (row.cells.length > 2) {
            if (dir == -1) {
                row.removeChild(row.cells[row.cells.length - 4]);
                var cell = row.insertCell(2);
            }
            else if (dir == 1) {
                row.removeChild(row.cells[2]);
                var cell = row.insertCell(row.cells.length - 3);
            }

            cell.style.width = '46px';

            var currentDate = startDate;

            if (dir > 0) {
                currentDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() + (noNights - 1));
            }

            if (row.id.indexOf('operator') == 0) {
                cell.rowSpan = 2;
                cell.className = ((currentDate.getDay() == 0 || currentDate.getDay() == 6) ? 'weekEnd' : 'weekDay');
                if (dir < 0) {
                    cell.innerHTML = currentDate.format('ddd<br/>d<br/>mmm');
                }
                else if (dir > 0) {
                    cell.innerHTML = currentDate.format('ddd<br/>d<br/>mmm');
                }
            }
            else {
                cell.className = ((currentDate.getDay() == 0 || currentDate.getDay() == 6) ? 'loadingWeekEnd' : 'loadingWeekDay');
                cell.innerHTML = 'TBA';
            }

            if (row.id.indexOf('operator') != 0) {
                for (var x = 2; x < row.cells.length - 2; x++) {

                    var tempDate = new Date(startDate);

                    tempDate.setDate(tempDate.getDate() + (x - 2));

                    if (x < row.cells.length - 3)
                        row.cells[x].className = ((tempDate.getDay() == 0 || tempDate.getDay() == 6) ? 'loadingWeekEnd' : 'loadingWeekDay');
                    else
                        row.cells[x].className = 'loadingTotal';

                    row.cells[x].innerHTML = 'TBA';
                }

                row.cells[row.cells.length - 1].innerHTML = 'TBA';
            }
        }
    }
    
    window.clearTimeout(gMoveTimeout);
    window.gMoveTimeout = window.setTimeout('moveDates(' + pageIndex + ', \'' + startDate.format('yyyy/mm/dd') + '\', ' + noNights + ');', 1000);
}
function moveDates(pageIndex, arrivalDate, numberOfNights) {
    disablePage();
    ScriptLoader.LoadFile('accomgrid_searchupdate.asp?PageIndex=' + pageIndex + '&ArrivalDate=' + arrivalDate + '&NumberOfNights=' + numberOfNights);
}

var scrollTop;

function disablePage() {
    var divLoading = document.getElementById('divLoading');
    var divPageDisabler = document.getElementById('divPageDisabler');

    document.documentElement.className = 'disablePage';

    if (document.getElementById('divContainer')) {
        scrollTop = getScrollTop();
        document.getElementById('divContainer').className = 'disablePage';
    }
    
    divLoading.style.display = 'block';
    divLoading.style.left = (((document.documentElement.clientWidth - divLoading.clientWidth) / 2) + getScrollLeft()) + 'px';
    divLoading.style.top = (((getPageHeight() - divLoading.clientHeight) / 2) + getScrollTop()) + 'px';

    divPageDisabler.style.display = 'block';
    divPageDisabler.style.width = (document.documentElement.clientWidth + getScrollLeft()) + 'px';
    divPageDisabler.style.height = (document.documentElement.clientHeight + ((isIE && document.getElementById('divContainer')) ? 46 : 0)) + 'px';
}

function enablePage() {

    var divLoading = document.getElementById('divLoading');
    var divPageDisabler = document.getElementById('divPageDisabler');

    divLoading.style.display = 'none';
    divPageDisabler.style.display = 'none';
    document.documentElement.className = 'enablePage';

    if (document.getElementById('divContainer')) {
        document.getElementById('divContainer').className = 'wsContainer';
        document.getElementById('divContainer').scrollTop = scrollTop + 46;
    }
}

function setMedal(operatorId, isGoldMedal) {
    var medal = document.getElementById('medal' + operatorId);

    if (medal != null) {
        if (isGoldMedal) {
            medal.src = './images/bookable.gif';
            medal.alt = 'Rates and availability are 100% up to date and your booking will be instantly confirmed.';
        }
        else {
            medal.src = './images/24hour.gif';
            medal.alt = 'Availability will be confirmed within 24 hours.';
        }
    }
}

function setCell(operatorId, roomId, date, isAvailable, isConstrained, isSpecial, isHotDeal, cost, altText, isValidUser) {
    var row = document.getElementById('room' + roomId);

    if (row != null) {
        var cellIndex = noNights;

        if (date != null)
            cellIndex = ((date - startDate) / (24 * 60 * 60 * 1000));

        if (cellIndex >= 0) {
            row.cells[cellIndex + 2].innerHTML = cost;
            row.cells[cellIndex + 2].title = altText;

            if (date != null) {
                var className = ((date.getDay() == 0 || date.getDay() == 6) ? 'weekEnd' : 'weekDay');

                if (isHotDeal) className += ' hotdeal';
                if (isSpecial) className += ' special';
                if (isConstrained) className += ' constrained';
                if (!isAvailable) className += ' unavailable';

                row.cells[cellIndex + 2].className = className;
            }
            else {
                var className = 'total';

                if (isHotDeal) className += ' hotdeal';
                if (isSpecial) className += ' special';
                if (isConstrained) className += ' constrained';
                if (!isAvailable) className += ' unavailable';

                row.cells[cellIndex + 2].className = className;

                if ((!isValidUser && isConstrained) || !isAvailable)
                    row.cells[cellIndex + 4].innerHTML = 'N/A';
                else
                    row.cells[cellIndex + 4].innerHTML = '<a href="javascript:void(0);" onclick="displayTermsConditions(' + operatorId + ', ' + roomId + ')"><img src="./images/bookonline.png" border="0" /></a>';
            }
        }
    }
}

function openDetails(h) {
    ifInformation.style.height = h + 'px';
    divProgress.style.display = 'none';
}


function getPageHeight() {    
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE        
        return window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'        
        return document.documentElement.clientHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 4 compatible        
        return document.documentElement.clientHeight;
    }        
}


function displayTermsConditions(operatorId, roomId) {

    var divTermsConditions = document.getElementById('divTermsConditions');
    var termsConditionsForm = document.forms['termsConditionsForm'];
    var ifTermsConditions = document.getElementById('ifTermsConditions');

    termsConditionsForm.action = 'addtocart.asp?code=' + operatorId + '&roomid=' + roomId;
    ifTermsConditions.src = 'accomgrid_searchtermsconditions.asp?OperatorID=' + operatorId + '&RoomID=' + roomId;

    disablePage();

    divTermsConditions.style.display = 'block';
    divTermsConditions.style.left = (((document.documentElement.clientWidth - divTermsConditions.clientWidth) / 2) + getScrollLeft()) + 'px';
    divTermsConditions.style.top = (((getPageHeight() - divTermsConditions.clientHeight) / 2) + getScrollTop()) + 'px';

    doResize();
}

function closeTermsConditions(accept) {

    var divTermsConditions = document.getElementById('divTermsConditions');
    var ifTermsConditions = document.getElementById('ifTermsConditions');

    divTermsConditions.style.display = 'none';
    ifTermsConditions.src = 'about:blank';

    enablePage();

    alert('You must accept the Terms and Conditions before continuing.');
}

function doResize()
{
    var divTermsConditions = document.getElementById('divTermsConditions');
    var ifTermsConditions = document.getElementById('ifTermsConditions');
    var divPageDisabler = document.getElementById('divPageDisabler');

    var maxSpace = divTermsConditions.clientHeight - ifTermsConditions.height;

    if (divTermsConditions.clientHeight > document.documentElement.clientHeight)
    {
	var newSize = (document.documentElement.clientHeight - maxSpace);

	if (newSize < 50)
	    newSize = 50;

	ifTermsConditions.height = newSize;
    }
    else if (ifTermsConditions.height < 300)
    {
	var newSize = (document.documentElement.clientHeight - maxSpace);

	if (newSize > 300) newSize = 300;
	ifTermsConditions.height = newSize;
    }

    // divTermsConditions.style.left = (((document.documentElement.clientWidth - divTermsConditions.clientWidth) / 2) + getScrollLeft()) + 'px';
    divTermsConditions.style.top = (((getPageHeight() - divTermsConditions.clientHeight) / 2) + getScrollTop()) + 'px';
    //divPageDisabler.style.width = (document.documentElement.clientWidth + getScrollLeft()) + 'px';
    divPageDisabler.style.height = (document.documentElement.clientHeight + getScrollTop() + ((isIE && document.getElementById('divContainer')) ? 46 : 0)) + 'px';

}

addEvent(window, 'resize', doResize);
