﻿function hideDesc(sPage, sID) {
    makeRequest('central/booking/nodesc.html', 'details' + sID);
    document.getElementById("link" + sID).innerHTML = "Show Details";
    document.getElementById("link" + sID).onclick = function() { showDesc(sPage, sID); };
}

function showDesc(sPage, sID) {
    makeRequest('/central/booking/' + sPage + '?id=' + sID, 'details' + sID);
    document.getElementById("link" + sID).innerHTML = "Hide Details";
    document.getElementById("link" + sID).onclick = function() { hideDesc(sPage, sID); };
    setHeight();
}


function GetRes() {
    var gform = document.forms['CancelForm'];
    var gResno = gform.Resno.value;
    var gsurname = gform.Surname.value;

    if (gResno == "")
    { alert("Please Enter your reservation number."); }
    else if (gsurname == "")
    { alert("Please Enter your Surname."); }
    else {
        var gQuery = "Resno=" + gResno + "&surname=" + gsurname;
        enforceBookingContainers();
        makeRequest("central/booking/get_booking_detailsd41d.html?" + gQuery, "bookingcontentcontainer");
    }
}

/*****************************/
/* STAGE SWITCHING FUNCTIONS */
/*****************************/

function enforceBookingContainers() {
    /*
    Check that the "content" div contains the
    bookingnavcontainer and bookingcontentcontainer
    */

    var contentElement, bookingNavContainer, bookingContentContainer;
    if (!document.getElementById('bookingnavcontainer') || !document.getElementById('bookingcontentcontainer')) {
        contentElement = document.getElementById('content');
        while (contentElement.childNodes.length > 0)
        { contentElement.removeChild(contentElement.firstChild); }

        if (contentElement.appendChild) {
            bookingNavContainer = document.createElement('div');
            bookingNavContainer.setAttribute('id', 'bookingnavcontainer');
            bookingContentContainer = document.createElement('div');
            bookingContentContainer.setAttribute('id', 'bookingcontentcontainer');

            contentElement.appendChild(bookingNavContainer);
            contentElement.appendChild(bookingContentContainer);

            contentElement.innerHTML = contentElement.innerHTML;
        }
        else {
            bookingNavContainer = '<div id="bookingnavcontainer" />';
            bookingContentContainer = '<div id="bookingcontentcontainer" />';
            contentElement.innerHTML = bookingNavContainer + bookingContentContainer;
        }
    }
}

function currencyRefresh(currencyList, stage) {
    var currencyCode = currencyList[currencyList.selectedIndex].value;
    switch (stage) {
        case 1:
            goToStage1();
            break;
        case 2:
            goToStage2(false, { currency: currencyCode });
            break;
        case 3:
            goToStage3(false, { currency: currencyCode });
            break;
        case 4:
            goToStage4(false, { currency: currencyCode });
            break;
        case 5:
            goToStage5(false, { currency: currencyCode });
            break;
    }
}

function generalStageNodeReplacement(xmlDoc) {
    var navigationNode = xmlDoc.getElementsByTagName('ol')[0];
    var contentNode = xmlDoc.getElementsByTagName('div')[0];

    // clear navigation parent
    var navigationParent = document.getElementById('bookingnavcontainer');
    while (navigationParent.childNodes.length > 0)
    { navigationParent.removeChild(navigationParent.firstChild); }

    // clear content parent
    var contentParent = document.getElementById('bookingcontentcontainer');
    while (contentParent.childNodes.length > 0)
    { contentParent.removeChild(contentParent.firstChild); }

    // insert the navigation
    if (navigationParent.attachEvent) {
        // IE branch
        navigationParent.innerHTML = navigationNode.xml;
    }
    else {
        navigationParent.appendChild(navigationNode);
        navigationParent.innerHTML = navigationParent.innerHTML;
    }

    // insert the content
    if (contentParent.attachEvent) {
        // IE branch
        contentParent.innerHTML = contentNode.xml;
    }
    else {
        contentParent.appendChild(contentNode);
        contentParent.innerHTML = contentParent.innerHTML;
    }
}

function goToProcessFromRightNav(isForward, otherArgs) {
    //get the url for hotel 
    var hotel_url = XmlHttp.create();
    hotel_url.open('GET', '_xml/hotel_id.xml', false);
    hotel_url.send(null);
    var hotel_response = (hotel_url.responseXML);
    var Hurl = (hotel_response.getElementsByTagName('site')[0].firstChild.nodeValue);




    var sessionReq = XmlHttp.create();
    var sessionObject, key;

    if (Hurl == 'charleshotel') {
        var postContent = '<form id="transfer" method="post" action="https://www.' + Hurl + '.de/central/reservations.asp">';
    }
    else if (Hurl == 'hotelderussie') {
        var postContent = '<form id="transfer" method="post" action="https://www.' + Hurl + '.it/central/reservations.asp">';
    }
    else if (Hurl == 'hotelsavoy') {
        var postContent = '<form id="transfer" method="post" action="https://www.' + Hurl + '.it/central/reservations.asp">';
    }
    else {
        var postContent = '<form id="transfer" method="post" action="https://www.' + Hurl + '.com/central/reservations.asp">';
    }

    // retrieve session variables (via JSON) into an object
    sessionReq.open('GET', 'central/booking/sessionvariables.asp', false);
    sessionReq.send(null);
    eval(sessionReq.responseText);

    // generate hidden form fields
    for (key in sessionObject) {
        if (sessionObject.hasOwnProperty(key))
        { postContent += '<input type="hidden" id="' + key + '" name="' + key + '" value="' + sessionObject[key] + '" />'; }
    }

    // add ratecode
    if (otherArgs && otherArgs.rateCode !== undefined)
    { postContent += '<input type="hidden" id="rcode" name="rcode" value="' + otherArgs.rateCode + '" />'; }

    // add nextstage
    if (otherArgs && otherArgs.nextstage !== undefined)
    { postContent += '<input type="hidden" id="nextstage" name="nextstage" value="' + otherArgs.nextstage + '" />'; }

    // close the form
    postContent += '</form>'
    //alert(postContent);
    // append form into the document somewhere
    document.getElementById('smallgetratescontainer').innerHTML = postContent;

    // POST form
    document.getElementById('transfer').submit();
}



/* STAGE 1 */
function goToStage1(isForward) {
    var url = 'central/booking/checkAvailability.asp';
    var httpRequest = XmlHttp.create();

    httpRequest.onreadystatechange = function() { stage1Completion(httpRequest); }

    httpRequest.open('GET', url, true);
    httpRequest.send(null);
}
function stage1Completion(httpRequest) {
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
            // replace the navigation and content
            var xmlDoc = httpRequest.responseXML;
            generalStageNodeReplacement(xmlDoc);

            // set height correctly
            setHeight();

            // set dates from cookies if available
            initialiseDateDropdowns();

            //set up arrival date calendar
            arrDateSetup();

            // set up departure date calendar
            depDateSetup();
        }
        else {
            alert('There was a problem with the request.' + httpRequest.status);
        }
    }
}

/* STAGE 1 INTERFACE FUNCTIONS */
function arrDateSetup() {
    var curDate = document.getElementById('bookcheckin').value;
    Calendar.setup({
        inputField: 'bookcheckin',
        button: 'bookcheckintrigger',
        date: curDate,
        ifFormat: "%d/%m/%Y",
        onSelect: function(calendar) { changeDate(calendar, 1); }
    });
}

function depDateSetup() {
    var curDate = document.getElementById('bookcheckout').value;
    Calendar.setup({
        inputField: 'bookcheckout',
        button: 'bookcheckouttrigger',
        date: curDate,
        ifFormat: "%d/%m/%Y",
        onSelect: function(calendar) { changeDate(calendar, 0); }
    });
}

/* END STAGE 1 */

function getRates_NET() {


    //get the url for hotel 
    var hotel_url = XmlHttp.create();
    hotel_url.open('GET', '_xml/hotel_id.xml', false);
    hotel_url.send(null);
    var hotel_response = (hotel_url.responseXML);
    var Hurl = (hotel_response.getElementsByTagName('url')[0].firstChild.nodeValue);

    var url = 'https://' + Hurl + '/central/reservations_v2.asp?stage=2';


    url += '&BookCheckInDayDropDownList=' + document.getElementById('availcheckinday').value;
    url += '&BookCheckInMonthDropDownList=' + document.getElementById('availcheckinmonth').value;
    url += '&BookCheckOutDayDropDownList=' + document.getElementById('availcheckoutday').value;
    url += '&BookCheckOutMonthDropDownList=' + document.getElementById('availcheckoutmonth').value;
    
    var roomsElement = document.getElementById('no_of_rooms');
    var grooms = roomsElement[roomsElement.selectedIndex].value;

    url += '&NumberOfRoomsDropDownList=' + grooms;
    url += '&NumberOfGuestsDropDownList=' + document.getElementById('guests').value;
    url += '&VisitorTypeDropDownList=' + document.getElementById('type').value;
    url += '&IPackageHiddenField=0';
    url += '&RefreshSession=1';
    url += '&lang=EN';
    location.href = url;
}

/* STAGE 2 */
function getRates_small() {
    var gDate1 = document.getElementById('availcheckin').value;
    var gDate2 = document.getElementById('availcheckout').value;

    var guestsElement = document.getElementById('guests');
    var gAdults = guestsElement[guestsElement.selectedIndex].value;

    var typeElement;
    var typeElementRightNav;
    var typeValue;
    var typeValueRightNav;

    var rightNavForm = document.getElementById("checkavailform");
    //In deeplink pages if right nav form exists 
    if (rightNavForm != null) {        
        typeElementRightNav = rightNavForm.elements["type"];
        typeElement = document.getElementById('type');
        typeValue = typeElement[typeElement.selectedIndex].value;
        typeValueRightNav = typeElementRightNav[typeElementRightNav.selectedIndex].value;

        if (typeValue != -1) {
            typeValueRightNav = typeValue;
        }
        if (typeValueRightNav != -1) {
            typeValue = typeValueRightNav;
        }
    }
    else {
        typeElement = document.getElementById('type');
        typeValue = typeElement[typeElement.selectedIndex].value;
    }

    var gType = typeValue;
    
    var roomsElement = document.getElementById('no_of_rooms');
    var grooms = roomsElement[roomsElement.selectedIndex].value;

    var dateParts1 = gDate1.split('index.html');
    reverseDate1 = dateParts1[2] + '/' + dateParts1[1] + '/' + dateParts1[0];

    var dateParts2 = gDate2.split('index.html');
    reverseDate2 = dateParts2[2] + '/' + dateParts2[1] + '/' + dateParts2[0];

    var month1_sel_index = document.getElementById('availcheckinmonth').selectedIndex;
    var month2_sel_index = document.getElementById('availcheckoutmonth').selectedIndex;

    var gQuery = 'date1=' + reverseDate1 + '&date2=' + reverseDate2 + '&rooms=' + grooms + '&adults=' + gAdults + '&children=0' + '&type=' + gType;

    var actualFrom = new Date(dateParts1[2], parseInt(dateParts1[1], 10) - 1, dateParts1[0]);
    var actualTo = new Date(dateParts2[2], parseInt(dateParts2[1], 10) - 1, dateParts2[0]);
    var maxTo = new Date(dateParts1[2], parseInt(dateParts1[1], 10) - 1, parseInt(dateParts1[0], 10) + 7);

    //current date
    var currentDate = new Date();
    currentDate.setMinutes(0);
    currentDate.setHours(0);
    currentDate.setSeconds(0);
    currentDate.setMilliseconds(0);


    if (currentDate <= actualFrom) {
        if (actualTo <= maxTo) {
            if (gType != -1) {
                if (grooms != "3+" && gAdults != "2+") {
                    // display a loading message
                    document.getElementById('smallgetratescontainer').innerHTML = '<p align="center"><object width="90" height="11" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"><param name="wmode" value="transparent"><param name="movie" value="../../../hoteljesolo/central/script/central/flash/loading_side.swf" /><embed src="/central/flash/loading_side.swf" width="90" height="11" type="application/x-shockwave-flash" wmode="transparent" pluginspage= "http://www.macromedia.com/go/getflashplayer" /></object></p>';

                    //Handling this part with dot net
                    getRates_NET();
                    /* var httpRequest = XmlHttp.create();
                    var url = '/central/booking/loadXML.asp?' + gQuery;
                    httpRequest.onreadystatechange = function() { smallSearchMidPoint(httpRequest, gDate1, gDate2); }
                    httpRequest.open('GET', url, true);
                    httpRequest.send(null);*/
                }
                else
                    location.href = 'special/booking_enquiries.htm';
            }
            else {
                alert('Please specify purpose of stay.');
            }
        }
        else {
            // let the user know they can't select more than 7 days
            //alert('Please select a time span of 7 days or less.\n\n(eg. ' + actualFrom.toDateString() + ' to ' + maxTo.toDateString() + ')');
            location.href = 'special/booking_enquiries.htm';
        }
    }
    else {
        alert('The dates you have selected occur in the past please select dates in the future.');
    }
}
function smallSearchMidPoint(interimReq, dateIn, dateOut) {
    var errorContent = '';
    if (interimReq.readyState == 4) {
        if (interimReq.status == 200) {
            // catch result of loadXML.asp, and check for error message
            var dataResponse = interimReq.responseXML;

            if (dataResponse.getElementsByTagName('error').length > 0) {
                var errorType = dataResponse.getElementsByTagName('error')[0].getAttribute('type');
                switch (errorType) {
                    case 'noavail':
                        //stage2DataError(dataResponse);
                        errorContent += '<p>Unfortunately the hotel that you have selected has no rooms available for the dates that you have selected.</p>';
                        errorContent += '<p>Please either try alternative dates or click on the link below to Speak to Hotel immediately and at no cost regarding your request.</p>';
                        errorContent += '<p><a href="../../../hoteljesolo/central/script/special/speak_to_hotel.htm">Speak to Hotel</a></p>';
                        break;
                    case 'parseerror':
                        errorContent += '<p>Unfortunately, we are unable to deal with your request at the moment, please try again later or click <a href="../../../hoteljesolo/central/script/special/speak_to_hotel.htm">here</a> to Speak to Hotel immediately and at no cost</p>';
                        break;
                    default:
                        // nothing as yet
                }
                document.getElementById('smallgetratescontainer').innerHTML = errorContent;
                setHeight();
            }
            else {
                dateIn = dateIn.replace(/\//g, '_');
                dateOut = dateOut.replace(/\//g, '_');

                var url = 'central/booking/small_getRates8758.html?datein=' + dateIn + '&dateout=' + dateOut;
                var httpRequest = XmlHttp.create();
                httpRequest.onreadystatechange = function() { smallSearchCompletion(httpRequest); }
                httpRequest.open('GET', url, true);
                httpRequest.send(null);
            }
        }
        else {
            errorContent += '<p>Unfortunately, we are unable to deal with your request at the moment, please try again later or click <a href="../../../hoteljesolo/central/script/special/speak_to_hotel.htm">here</a> to Speak to Hotel immediately and at no cost</p>';
            document.getElementById('smallgetratescontainer').innerHTML = errorContent;
        }
    }
}
function smallSearchCompletion(httpRequest) {
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
            var dataResponse = httpRequest.responseText;
            document.getElementById('smallgetratescontainer').innerHTML = dataResponse;
            setHeight();
        }
    }
}


function goToStage2(isForward, otherArgs) {
    var url;

    if (isForward) {
        // load XML from protel into the database before proceeding

        var fromDate = document.getElementById('bookcheckin').value;
        var toDate = document.getElementById('bookcheckout').value;

        var typeElement = document.getElementById('type');
        var gType = typeElement[typeElement.selectedIndex].value;

        // check the dates are not more than 7 days apart before sending off for the data
        var fromParts = fromDate.split('index.html');
        var toParts = toDate.split('index.html');
        var actualFrom = new Date(fromParts[2], parseInt(fromParts[1], 10) - 1, fromParts[0]);
        var actualTo = new Date(toParts[2], parseInt(toParts[1], 10) - 1, toParts[0]);
        var maxTo = new Date(fromParts[2], parseInt(fromParts[1], 10) - 1, parseInt(fromParts[0], 10) + 7);


        var fromMonth = parseInt(fromParts[1], 10);
        var toMonth = parseInt(toParts[1], 10);
        var reverseFrom = fromParts[2] + '/' + (fromMonth < 10 ? '0' : '') + fromMonth + '/' + fromParts[0];
        var reverseTo = toParts[2] + '/' + (toMonth < 10 ? '0' : '') + toMonth + '/' + toParts[0];


        //current Date
        var currentDate = new Date();
        currentDate.setMinutes(0);
        currentDate.setHours(0);
        currentDate.setSeconds(0);
        currentDate.setMilliseconds(0);

        if (currentDate <= actualFrom) {
            if (actualTo <= maxTo) {
                if (gType != -1) {
                    // less than 7 days selected, so off we go
                    roomsList = document.getElementById('ddrooms');
                    var rooms = roomsList[roomsList.selectedIndex].value;

                    adultsList = document.getElementById('ddadults');
                    var adults = adultsList[adultsList.selectedIndex].value;

                    //childrenList = document.getElementById('ddchildren');
                    //var children = childrenList[childrenList.selectedIndex].value;

                    typeList = document.getElementById('type');
                    var type = typeList[typeList.selectedIndex].value;

                    var iPackage = document.getElementById('ipackage').value;

                    var query = 'date1=' + reverseFrom + '&date2=' + reverseTo + '&rooms=' + rooms + '&adults=' + adults + '&children=0' + '&type=' + type + '&ipackage=' + iPackage;
                    url = 'central/booking/loadXMLd41d.asp?' + query;

                    // show loading graphic
                    document.getElementById('bookingcontent').innerHTML = '<p align="center"><object width="282" height="145" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"><param name="wmode" value="transparent"><param name="movie" value="../../../hoteljesolo/central/script/central/flash/loading_main.swf" /><embed src="/central/flash/loading_main.swf" width="282" height="145" type="application/x-shockwave-flash" wmode="transparent" pluginspage= "http://www.macromedia.com/go/getflashplayer" /></object></p>'; ;
                    if (document.getElementsByTagName('body')[0].attachEvent) {
                        // IE branch
                        if (otherArgs)
                        { var timer = setTimeout('triggerSearch(\'' + url + '\',' + otherArgs.toString + ')', 50); }
                        else
                        { var timer = setTimeout('triggerSearch(\'' + url + '\')', 50); }
                    }
                    else {
                        // everyone else
                        var timer = setTimeout(triggerSearch, 50, url, otherArgs);
                    }
                }
                else {
                    alert('Please specify purpose of stay.');
                }
            }
            else {
                // let the user know they can't select more than 7 days
                alert('Please select a time span of 7 days or less.\n\n(eg. ' + actualFrom.toDateString() + ' to ' + maxTo.toDateString() + ')');
            }
        }
        else {
            alert('The dates you have selected occur in the past please select dates in the future.');
        }
    }
    else {
        // send off for page refresh without going to database
        stage2PageRefresh(otherArgs);
    }
}
function triggerSearch(url, otherArgs) {
    var httpRequest = XmlHttp.create();
    httpRequest.onreadystatechange = function() { stage2MidPoint(httpRequest, otherArgs); }
    httpRequest.open('GET', url, true);
    httpRequest.send(null);
}
function stage2MidPoint(interimReq, otherArgs) {
    if (interimReq.readyState == 4) {
        if (interimReq.status == 200) {
            // catch result of loadXML.asp, and check for error message
            var dataResponse = interimReq.responseXML;

            if (dataResponse.getElementsByTagName('error').length > 0) {
                var errorType = dataResponse.getElementsByTagName('error')[0].getAttribute('type');

                switch (errorType) {
                    case 'noavail':
                        stage2DataError(dataResponse);
                        break;
                    case 'parseerror':
                        makeRequest('central/Showerror.html', 'bookingcontent');
                        break;
                    default:
                        // nothing as yet
                }
            }
            else
            { stage2PageRefresh(otherArgs); }
        }
        else {
            makeRequest('central/Showerror.html', 'bookingcontent');
        }
    }
}
function stage2PageRefresh(otherArgs) {
    var url = 'central/booking/getRates.asp';
    if (otherArgs && otherArgs.currency !== undefined)
    { url += '?currency=' + otherArgs.currency; }

    var httpRequest = XmlHttp.create();
    httpRequest.onreadystatechange = function() { stage2Completion(httpRequest, otherArgs); }
    httpRequest.open('GET', url, true);
    httpRequest.send(null);
}

function stage2DataError(errorDocument) {
    // do something with errorDocument to pass through to the asp
    var url = 'central/booking/dataloaderror.asp';
    var httpRequest = XmlHttp.create();
    httpRequest.onreadystatechange = function() { stage2Completion(httpRequest); }
    httpRequest.open('GET', url, true);
    httpRequest.send(null);
}

function stage2Completion(httpRequest, otherArgs) {
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
            // replace the navigation and content
            var xmlDoc = httpRequest.responseXML;
            generalStageNodeReplacement(xmlDoc);

            if (otherArgs && otherArgs.rateCode !== undefined)
            { showDesc('ratedesc.html', otherArgs.rateCode); }

            // set height correctly
            setHeight();
        }
        else {
            alert('There was a problem with the request.' + httpRequest.status);
        }
    }
}
/* END STAGE 2 */


/* STAGE 3 */
function goToStage3(isForward, otherArgs) {
    var url = 'central/booking/getRooms.asp';

    if (isForward) {
        if (otherArgs && otherArgs.rateCode !== undefined) {
            // add the ratecode to the querystring
            url += '?ratecode=' + otherArgs.rateCode;
        }
        else {
            // error: going forward with no ratecode
            alert('No ratecode supplied');
            return false;
        }
    }
    else {
        if (otherArgs && otherArgs.currency !== undefined)
        { url += '?currency=' + otherArgs.currency; }
    }

    var httpRequest = XmlHttp.create();
    httpRequest.onreadystatechange = function() { stage3Completion(httpRequest); }
    httpRequest.open('GET', url, true);
    httpRequest.send(null);
}
function stage3Completion(httpRequest) {
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
            // replace the navigation and content
            var xmlDoc = httpRequest.responseXML;
            generalStageNodeReplacement(xmlDoc);

            // set height correctly
            setHeight();
        }
        else {
            alert('There was a problem with the request.' + httpRequest.status);
        }
    }
}
/* END STAGE 3 */


/* SUMMARISE STAGE */
function goToStageSummary(isForward, otherArgs) {
    var url = 'central/booking/summarise.asp';

    if (isForward) {
        if (otherArgs && otherArgs.roomCode !== undefined) {
            // add the ratecode to the querystring
            url += '?roomcode=' + otherArgs.roomCode;
        }
        else {
            // error: going forward with no ratecode
            alert('No roomcode supplied');
            return false;
        }
    }

    var httpRequest = XmlHttp.create();
    httpRequest.onreadystatechange = function() { stageSummaryCompletion(httpRequest); }
    httpRequest.open('GET', url, true);
    httpRequest.send(null);
}
function stageSummaryCompletion(httpRequest) {
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
            // replace the navigation and content
            var xmlDoc = httpRequest.responseXML;
            generalStageNodeReplacement(xmlDoc);

            // set height correctly
            setHeight();
            setHeight();
        }
        else {
            alert('There was a problem with the request.' + httpRequest.status);
        }
    }
}
/* END SUMMARISE STAGE */


/* STAGE 4 */
function goToStage4(isForward, otherArgs) {
    var url = 'central/booking/completeRes.asp';

    if (isForward) {
        // nothing needs to be passed forward here
    }

    var httpRequest = XmlHttp.create();
    httpRequest.onreadystatechange = function() { stage4Completion(httpRequest); }
    httpRequest.open('GET', url, true);
    httpRequest.send(null);
}
function stage4Completion(httpRequest) {
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
            // replace the navigation and content
            var xmlDoc = httpRequest.responseXML;
            generalStageNodeReplacement(xmlDoc);

            // set height correctly
            setHeight();
            setHeight();
        }
        else {
            alert('There was a problem with the request.' + httpRequest.status);
        }
    }
}
/* END STAGE 4 */


/* STAGE 5 */
function goToStage5(isForward, otherArgs) {
    document.getElementById('submitbutton').disabled = true;
    var url = 'central/booking/processApp.asp';

    if (isForward) {
        // If not going forward, MUST NOT RESUBMIT THE RESERVATION
        // Otherwise, validate then create a postable chunk from the fields
        if (validateGuestInfo()) {
            var formContent = '';
            formContent += 'title=' + document.getElementById('title')[document.getElementById('title').selectedIndex].value;
            formContent += '&fname=' + document.getElementById('fname').value;
            formContent += '&sname=' + document.getElementById('sname').value;
            formContent += '&address1=' + document.getElementById('address1').value.replace(/\s/g, '_');
            formContent += '&address2=' + document.getElementById('address2').value.replace(/\s/g, '_');
            formContent += '&pcode=' + document.getElementById('pcode').value;
            formContent += '&city=' + document.getElementById('city').value;
            formContent += '&country=' + document.getElementById('country')[document.getElementById('country').selectedIndex].value;
            formContent += '&tel=' + document.getElementById('tel').value;
            formContent += '&email=' + document.getElementById('email').value;
            //formContent += '&purpose=' + document.getElementById('purpose').value;
            formContent += '&creditno=' + document.getElementById('creditno').value;
            formContent += '&nameoncard=' + document.getElementById('nameoncard').value.replace(/\s/g, '_');
            formContent += '&cardtype=' + document.getElementById('cardtype').value;
            formContent += '&cc_exp_m=' + document.getElementById('cc_exp_m').value;
            formContent += '&cc_exp_y=' + document.getElementById('cc_exp_y').value;

            var prefString = '';
            if (document.getElementById('prefs1').checked)
            { prefString += document.getElementById('prefs1').value; }
            for (var n = 2; n <= 10; n++) {
                if (document.getElementById('prefs' + n).checked)
                { prefString += ',' + document.getElementById('prefs' + n).value; }
            }
            formContent += '&prefs=' + prefString;

            var httpRequest = XmlHttp.create();
            httpRequest.onreadystatechange = function() { stage5Completion(httpRequest); }
            httpRequest.open('POST', url, true);
            httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            httpRequest.send(formContent);
        }
    }
}
function stage5Completion(httpRequest) {
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
            // replace the navigation and content
            var xmlDoc = httpRequest.responseXML;
            generalStageNodeReplacement(xmlDoc);

            // set height correctly
            setHeight();
        }
        else {
            alert('There was a problem with the request.' + httpRequest.status);
        }
    }
}

/* STAGE 5 INTERFACE FUNCTIONS */
function validateGuestInfo() {
    var sTitle = document.getElementById('title')[document.getElementById('title').selectedIndex].value;
    var sfname = document.getElementById('fname').value;
    var ssname = document.getElementById('sname').value;
    var saddress1 = document.getElementById('address1').value;
    var saddress2 = document.getElementById('address2').value;
    var spcode = document.getElementById('pcode').value;
    var scity = document.getElementById('city').value;
    var scountry = document.getElementById('country')[document.getElementById('country').selectedIndex].value;
    var stel = document.getElementById('tel').value;
    var semail = document.getElementById('email').value;
    var screditno = document.getElementById('creditno').value;
    var snameoncard = document.getElementById('nameoncard').value;
    var sCardType = document.getElementById('cardtype').value;
    var scc_Exp_M = document.getElementById('cc_exp_m').value;
    var scc_Exp_Y = document.getElementById('cc_exp_y').value;

    if (sfname == '') {
        document.getElementById('submitbutton').disabled = false;
        alert('Please enter your First name.'); return false;
    }

    if (ssname == '') {
        document.getElementById('submitbutton').disabled = false;
        alert('Please enter your Surname.'); return false;
    }

    if (saddress1 == '') {
        document.getElementById('submitbutton').disabled = false;
        alert('Please enter a valid Address.'); return false;
    }

    if (spcode == '') {
        document.getElementById('submitbutton').disabled = false;
        alert('Please enter a valid Post Code.'); return false;
    }

    if (scity == '') {
        document.getElementById('submitbutton').disabled = false;
        alert('Please enter a valid city name.'); return false;
    }

    if (semail == '') {
        document.getElementById('submitbutton').disabled = false;
        alert('Please enter a valid email address.'); return false;
    }

    if (scountry == '0') {
        document.getElementById('submitbutton').disabled = false;
        alert('Please Select a country'); return false;
    }

    if (document.getElementById('terms').checked == false) {
        document.getElementById('submitbutton').disabled = false;
        alert('Please Confirm that you agree to the Terms and Conditions.'); return false;
    }

    if (screditno == '') {
        document.getElementById('submitbutton').disabled = false;
        alert('Please enter a valid credit card number.'); return false;
    }

    var credit_length = (screditno.length);
    var credit_checker = 0;
    var the_char;
    var reverse_counter = 0;
    var char_counter = 0;

    for (var iDx = credit_length; iDx > 0; iDx--) {
        char_counter = iDx - 1;
        the_char = parseInt(screditno.charAt((char_counter)));
        reverse_counter += 1;

        if ((reverse_counter % 2) != 0) {
            credit_checker += the_char
        }
        else {
            the_char = the_char * 2;
            if (the_char > 9) {
                temp_value = parseInt(the_char / 10) + (the_char % 10);
                the_char = temp_value;
                credit_checker += the_char;
            }
            else
            { credit_checker += the_char }
        }
    }
    if ((credit_checker % 10) != 0) {
        document.getElementById('submitbutton').disabled = false;
        alert('The credit card number you entered is invalid.'); return false;
    }

    if (snameoncard == '') {
        document.getElementById('submitbutton').disabled = false;
        alert('Please enter name on credit card.'); return false;
    }

    if (scc_Exp_M == 'Month') {
        document.getElementById('submitbutton').disabled = false;
        alert('Please select a valid credit card expiry month.'); return false;
    }

    if (scc_Exp_Y == 'Year') {
        document.getElementById('submitbutton').disabled = false;
        alert('Please enter a valid credit card expiry year.'); return false;
    }

    if (stel == '') {
        document.getElementById('submitbutton').disabled = false;
        alert('Please enter a telephone number.'); return false;
    }

    return true;
}
/* END STAGE 5 */

/* NEW FUNCTIONS MOHAMMAD */
function special_rates_page(sPage, sRate, sHotel) {
    makeRequest('central/booking/new_rates.html', 'bookingcontentcontainer');
    //replace_rate(sPage,sRate,sHotel);
}
function goto_new_rooms_page() {
    makeRequest('getroomse025.html?room_suite=room', 'content')
}
function goto_new_suites_page() {
    makeRequest('getroomsc395.html?room_suite=suite', 'content')
}
function goto_new_rates_page() {
    makeRequest('new_rates.html', 'content')
}
function showDesc_rooms(sPage, sID, sHotel) {
    document.getElementById("room_desc_" + sID).style.display = 'block';
    document.getElementById("link" + sID).innerHTML = "Hide Details";
    document.getElementById("link" + sID).onclick = function() { hideDesc_rooms(sPage, sID, sHotel); };
    setHeight();
}
function hideDesc_rooms(sPage, sID, sHotel) {
    document.getElementById("room_desc_" + sID).style.display = 'none';
    document.getElementById("link" + sID).innerHTML = "Show Details";
    document.getElementById("link" + sID).onclick = function() { showDesc_rooms(sPage, sID, sHotel); };
}
function replace_rate(sPage, sRate, sHotel) {
    makeRequest('/central/booking/' + sPage + '?id=' + sRate + '&hotel=' + sHotel, 'rate_div');
    setHeight();
}
function goToStage2_new(isForward, otherArgs) {
    var url;

    if (isForward) {
        // load XML from protel into the database before proceeding

        var fromDate = document.getElementById('bookcheckin').value;
        var toDate = document.getElementById('bookcheckout').value;

        var typeElement = document.getElementById('type');
        var gType = typeElement[typeElement.selectedIndex].value;



        // check the dates are not more than 7 days apart before sending off for the data
        var fromParts = fromDate.split('index.html');
        var toParts = toDate.split('index.html');
        var actualFrom = new Date(fromParts[2], parseInt(fromParts[1], 10) - 1, fromParts[0]);
        var actualTo = new Date(toParts[2], parseInt(toParts[1], 10) - 1, toParts[0]);
        var maxTo = new Date(fromParts[2], parseInt(fromParts[1], 10) - 1, parseInt(fromParts[0], 10) + 7);


        var fromMonth = parseInt(fromParts[1], 10);
        var toMonth = parseInt(toParts[1], 10);
        var reverseFrom = fromParts[2] + '/' + (fromMonth < 10 ? '0' : '') + fromMonth + '/' + fromParts[0];
        var reverseTo = toParts[2] + '/' + (toMonth < 10 ? '0' : '') + toMonth + '/' + toParts[0];


        //current Date
        var currentDate = new Date();
        currentDate.setMinutes(0);
        currentDate.setHours(0);
        currentDate.setSeconds(0);
        currentDate.setMilliseconds(0);

        if (currentDate <= actualFrom) {
            if (actualTo <= maxTo) {
                if (gType != -1) {
                    // less than 7 days selected, so off we go
                    roomsList = document.getElementById('ddrooms');
                    var rooms = roomsList[roomsList.selectedIndex].value;

                    adultsList = document.getElementById('ddadults');
                    var adults = adultsList[adultsList.selectedIndex].value;

                    //childrenList = document.getElementById('ddchildren');
                    //var children = childrenList[childrenList.selectedIndex].value;

                    typeList = document.getElementById('type');
                    var type = typeList[typeList.selectedIndex].value;

                    var iPackage = document.getElementById('ipackage').value;



                    var query = 'date1=' + reverseFrom + '&date2=' + reverseTo + '&rooms=' + rooms + '&adults=' + adults + '&children=0' + '&type=' + type + '&ipackage=' + iPackage;
                    url = 'central/booking/loadXMLd41d.asp?' + query;

                    // show loading graphic
                    document.getElementById('bookingcontentcontainer').innerHTML = '<p align="center"><object width="282" height="145" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"><param name="wmode" value="transparent"><param name="movie" value="../../../hoteljesolo/central/script/central/flash/loading_main.swf" /><embed src="/central/flash/loading_main.swf" width="282" height="145" type="application/x-shockwave-flash" wmode="transparent" pluginspage= "http://www.macromedia.com/go/getflashplayer" /></object></p>';
                    if (document.getElementsByTagName('body')[0].attachEvent) {
                        // IE branch
                        if (otherArgs)
                        { var timer = setTimeout('triggerSearch(\'' + url + '\',' + otherArgs.toString + ')', 50); }
                        else
                        { var timer = setTimeout('triggerSearch(\'' + url + '\')', 50); }
                    }
                    else {
                        // everyone else
                        var timer = setTimeout(triggerSearch, 50, url, otherArgs);
                    }
                }
                else {
                    alert('Please specify purpose of stay.');
                }
            }
            else {
                // let the user know they can't select more than 7 days
                alert('Please select a time span of 7 days or less.\n\n(eg. ' + actualFrom.toDateString() + ' to ' + maxTo.toDateString() + ')');
            }
        }
        else {
            alert('The dates you have selected occur in the past please select dates in the future.');
        }
    }
    else {
        // send off for page refresh without going to database
        stage2PageRefresh(otherArgs);
    }
}
