/**
 * Used to show or hide a panel
 * @param panelId Id of the panel that should be hid or shown.
 * @param action hide or show
 */
function showHidePanel( panelId, action )
{
    var panel = document.getElementById(panelId);
    if( action.indexOf('show') > -1 )
    {
        panel.style.display = '';
    }
    else if( action.indexOf('hide') > -1 )
    {
        panel.style.display = 'none';
    }
}

/**
 * Used to show the multicentre panel and load the multicentre cookies.
 */
function showMultyCenter()
{
    showHidePanel('singleCenterDiv', 'hide');
    showHidePanel('multyCenterDiv', 'show');
    document.getElementById('multyCenter').value = 'true';
    getMultyCenterCookie(2)
}

/**
 * Used to load the show singel centre panel and load the singel centre cookies.
 */
function showSingelCenter()
{
    showHidePanel('singleCenterDiv', 'show');
    showHidePanel('multyCenterDiv', 'hide');
    document.getElementById('multyCenter').value = 'false';
    getPageCookie();
}

/**
 * Used to check a given radio button.
 * @param radioId Id of the radio button.
 */
function checkRadio( radioId )
{
    document.getElementById(radioId).checked = true;
}

/**
 * Used to add a centre to the panel.
 * @param currentValueObject Html object that contains the current number of centres as the value.
 */
function addCenter( currentValueObject )
{
    var maxCount = parseInt(document.getElementById('maxCenters').value);
    var currentValue = currentValueObject.value;
    currentValue = parseInt(currentValue);
    var limit = currentValue + 1;
    for( var i = currentValue; i < limit; i++ )
    {
        if( currentValue < maxCount )
        {
            currentValue++;
            currentValueObject.value = currentValue;
            document.getElementById('noOfCenters').value = currentValue;
            document.getElementById("center" + currentValue).style.display = '';
            setDate(currentValue);
            //cloneNode(currentValue, 'center' + (currentValue - 1), 'passengerDetailsForMC,searchForMC');
        }
        else
        {
            alert("Number of centres are restricted to maximum " + maxCount + " centres.");
        }
    }
}

/**
 * Used to add given number of centres to the multi centre panel.
 * @param currentValueObject Html object that contains the current number of centres as the value.
 * @param noOfCenters Number of centres to be added to the current panel.
 */
function addCenters( currentValueObject, noOfCenters )
{
    var maxCount = parseInt(document.getElementById('maxCenters').value);
    var currentValue = currentValueObject.value;
    currentValue = parseInt(currentValue);
    for( var i = currentValue; i < noOfCenters; i++ )
    {
        if( currentValue < maxCount )
        {
            currentValue++;
            currentValueObject.value = currentValue;
            setDate(currentValue);
            document.getElementById('noOfCenters').value = currentValue;
            document.getElementById("center" + currentValue).style.display = '';
            //cloneNode(currentValue, 'center' + (currentValue - 1), 'passengerDetailsForMC,searchForMC');
        }
        else
        {
            alert("Number of centres are restricted to maximum " + maxCount + " centres.");
        }
    }
}

/**
 * Used to clone the specific node change the all its child id attributes and name attribute to the given value.
 * @param newValue new centre number. All the id and name attributes of the new cloned node will be replaces with this.
 * @param id Id of the node that is used to clone.
 * @param removeNodes Array of ids, that should be removed first and then appen to the end of the node. Thes nodes are at the
 * same level with the cloned node.
 */
function cloneNode( newValue, id, removeNodes )
{
    var dummyNode = document.getElementById(id);
    var oParentNode = dummyNode.parentNode;
    var nodeForRemove = new Array();
    var removeNode = removeNodes.split(",");
    for( var i = 0; i < removeNode.length; i++ )
    {
        var tempRemove = document.getElementById(removeNode[i]);
        if( tempRemove.parentNode == oParentNode )
        {
            nodeForRemove[i] = tempRemove;
            oParentNode.removeChild(tempRemove);
        }
    }
    var nodeToAppend = dummyNode.cloneNode(true);

    changeParentAttribute(nodeToAppend, 'id', newValue);
    changeParentAttribute(nodeToAppend, 'name', newValue);
    changeAttibutes(nodeToAppend, 'id', newValue);
    changeAttibutes(nodeToAppend, 'name', newValue);

    oParentNode.appendChild(nodeToAppend);
    for( var i = 0; i < nodeForRemove.length; i++ )
    {
        if( nodeForRemove[i] != null )
        {
            oParentNode.appendChild(nodeForRemove[i].cloneNode(true));
        }
    }
    document.getElementById('mcCenterNumber' + newValue).innerHTML = 'Center ' + newValue;
    changeJavascriptParameters('mccheckInDateImg' + newValue, "showHotelCheckInCalendar", "'mccheckInDate" + newValue + "','mccheckInDateImg" + newValue + "'", 'onclick');
    changeJavascriptParameters('mchotelName' + newValue, 'onACFocusText', "'mchotelName" + newValue + "', '',document.getElementById('hotelNamesLoaderURL').value, 'getHotels'", 'onfocus')

    setCurrentCenterNo(newValue.toString());
    updateDate((newValue - 1).toString(), true);
}

/**
 * Used to change a given attribute of a given node with the new value.
 * @param node Node in which the attribute should be changed.
 * @param attribute Attribute name (id, name)
 * @param newValue New value that is used to replace the given attribute value. This new value is appended to the and of the existing attibute value.
 */
function changeAttibutes( node, attribute, newValue )
{
    var childNodes = node.childNodes;
    if( childNodes != null && childNodes.length > 0 )
    {
        for( var i = 0; i < childNodes.length; i++ )
        {
            var childNode = childNodes[i];
            var attributes = childNode.attributes;
            if( attributes != null && attributes.length > 0 )
            {
                if( attributes.getNamedItem(attribute) != null )
                {
                    var attValue = attributes.getNamedItem(attribute).nodeValue;
                    if( attValue.indexOf(newValue - 1) == attValue.length - 1 )
                    {
                        attValue = attValue.substring(0, attValue.length - 1) + newValue;
                        attributes.getNamedItem(attribute).nodeValue = attValue;
                    }
                }
            }
            changeAttibutes(childNode, attribute, newValue);
        }
    }
    else
    {
        return;
    }
}

/**
 * Used to change only the root level attribute with a newly given value.
 * @param childNode Node in which the attribute should be changed.
 * @param attribute Attribute name (id, name)
 * @param newValue New value that is used to replace the given attribute value. This new value is appended to the and of the existing attibute value.
 */
function changeParentAttribute( childNode, attribute, newValue )
{
    var attributes = childNode.attributes;
    if( attributes != null && attributes.length > 0 )
    {
        if( attributes.getNamedItem(attribute) != null )
        {
            var attValue = attributes.getNamedItem(attribute).nodeValue;
            if( attValue.indexOf(newValue - 1) == attValue.length - 1 )
            {
                attValue = attValue.substring(0, attValue.length - 1) + newValue;
                attributes.getNamedItem(attribute).nodeValue = attValue;
            }
        }
    }
}

/**
 * Used to change a existing javascript with a new javascript
 * @param ownerNodeId Id of the node containing the javascript.
 * @param functionName Name of the jscript function
 * @param newPara Array of new parameters
 * @param event Event to the function should be triggered. Only implemented for 'onclick' and 'onfocus'
 */
function changeJavascriptParameters( ownerNodeId, functionName, newPara, event )
{
    var ownerNode = document.getElementById(ownerNodeId);
    var script = "";
    if( functionName.indexOf('showHotelCheckInCalendar') > -1 )
    {
        script = "javascript:setCurrentCenterNo(this.id);"
    }
    script += functionName + "(" + newPara + ");";
    if( event.indexOf('onclick') > -1 )
    {
        ownerNode.onclick = new Function(script);
    }
    if( event.indexOf('onfocus') > -1 )
    {
        ownerNode.onfocus = new Function(script);
    }
}

/**
 * Used to get the cookies for the multycentre
 * @param noOfCenters No of centres
 */
function getMultyCenterCookie( noOfCenters )
{
    for( var i = 1; i <= noOfCenters; i++ )
    {
        var criteria = getCookie('centerCriteria' + i);
        criteria = criteria.split(",");
        var c_city = criteria[0];
        var c_checkinDate = criteria[1];
        var c_nights = criteria[2];
        var hotelName = criteria[3];
        setMultyCenterData(c_city, c_checkinDate, c_nights, i, hotelName);
    }
    var passengerDetails = getCookie('multyCenterAges');
    passengerDetails = passengerDetails.split("@");
    var adultRoooms = passengerDetails[0];
    var childRooms = passengerDetails[1];
    var infantRooms = passengerDetails[2];
    var ages = passengerDetails[3];
    setMultyCenterPassengerDetails(adultRoooms, childRooms, infantRooms, ages);
}

/**
 * Used to set the multicenter cookie values to the form.
 * @param city City
 * @param checkinDate checkin date
 * @param nights Nights
 * @param centerNo centre number
 */
function setMultyCenterData( city, checkinDate, nights, centerNo, hotelName )
{
    var searchByName = false;
    if((hotelName && hotelName != ""))
    {
        searchByName = true;
        document.getElementById('mchotelName' + centerNo).value = hotelName;
    }
    if( city && city != "" )
    {
        var options = document.getElementById('mccity' + centerNo).options;
        for( var i = 0; i < options.length; i++ )
        {
            if( !searchByName )
            {
                if( options[i].value == city )
                {
                    document.getElementById('mccity' + centerNo).selectedIndex = i;
                }
            }
            else
            {
                if(options[i].value == "" )
                {
                    document.getElementById('mccity' + centerNo).selectedIndex = i;
                }
            }
        }
    }
    if( checkinDate && checkinDate != "" )
    {
        var dt = new Date();
        var c_date = checkinDate;
        checkinDate = checkinDate.split("/");
        dt.setFullYear(checkinDate[2], parseInt(checkinDate[1]) - 1, checkinDate[0]);
        var today = new Date();
        today.setDate(parseInt(today.getDate()) + 3);
        if( dt > today )
        {
            document.getElementById('mccheckInDate' + centerNo).value = c_date;
        }
        else
        {
            var month = parseInt(today.getMonth()) + 1;
            if( month < 10 )
            {
                month = '0' + month;
            }
            var date = parseInt(today.getDate());
            if( date < 10 )
            {
                date = '0' + date;
            }

            var newDate = date + "/" + month + "/" + today.getFullYear();
            document.getElementById('mccheckInDate' + centerNo).value = newDate;
        }
    }
    if( nights && nights != "" )
    {
        document.getElementById('mcnoOfNights' + centerNo).value = nights;
    }
}

/**
 * Used to set the multicenter cookie values to the passenger details.
 * @param adultRooms Adult rooms
 * @param childRooms Child rooms
 * @param infantRooms Infant rooms
 * @param childAges Child ages
 */
function setMultyCenterPassengerDetails( adultRooms, childRooms, infantRooms, childAges )
{
    if( adultRooms && adultRooms != "" )
    {
        document.getElementById('mcAdultsRoom1').selectedIndex = adultRooms - 1;
    }
    if( childRooms && childRooms != "" )
    {
        document.getElementById('mcChildsRoom1').selectedIndex = childRooms;
        showAgesChildren('mc');
    }
    if( infantRooms && infantRooms != "" )
    {
        document.getElementById('mcInfantsRoom1').selectedIndex = infantRooms;
        showAgesInfant('mc');
    }
    if( childAges && childAges != "" )
    {
        childAges = childAges.split(",");
        var colonCount = 0;
        var infantStart = 0;
        for( var i = 0; i < childAges.length; i++ )
        {
            var prefix = childAges[i].indexOf(":");
            if( prefix > -1 )
            {
                colonCount = colonCount + 1;
                infantStart = i;
                if( colonCount == 2 )
                    break;
            }
            if( childAges[i] != "" && colonCount == 1 && prefix == -1 )
            {
                document.getElementById("mcchild" + i).value = childAges[i];
            }
        }
        var length = infantStart;
        for( var i = 0; i < (childAges.length - length - 1); i++ )
        {
            if( childAges[infantStart + 1] != "" )
            {
                document.getElementById("mcinfant" + (i + 1)).value = childAges[infantStart + 1];
                infantStart += 1;
            }
        }
    }
}

/**
 * Used to remove a centre from the panel.
 * @param currentValueObject Html object that contains the current number of centres as the value.
 */
function removeCenter( currentValueObject )
{
    var minCount = document.getElementById('minCenters').value;
    var currentValue = currentValueObject.value;
    currentValue = parseInt(currentValue);
    var temp = currentValue - 1
    for( var i = currentValue; i > temp; i-- )
    {
        if( currentValue > minCount )
        {
            document.getElementById("center" + currentValue).style.display = 'none';
            //removeNode(currentValue);
            currentValue--;
            currentValueObject.value = currentValue;
            document.getElementById('noOfCenters').value = currentValue;
        }
        else
        {
            alert("Number of centres are restricted to minimum " + minCount + " centres.");
        }
    }
}

/**
 * Used to remove a node having the id as 'center'+ centerNumber
 * @param centerNumber Centre number
 */
function removeNode( centerNumber )
{
    var node = document.getElementById('center' + centerNumber);
    node.parentNode.removeChild(node);
}

/**
 * Updates the next date text boxes in the next centers only if the next date is empty or less than the current date plus
 * number of nights selected in the current centre.
 * @param currentIndex Current center index.
 * @param validate do the validation or not
 */
function updateDate( currentIndex, validate )
{
    var currentId = parseInt(currentIndex);
    var noOfCenters = parseInt(document.getElementById('noOfCenters').value);

    if( validate )
    {
        if( !validateDate(currentId) )
        {
            alert("Selected date is invalid due to the previously selected date");
            var preDate = document.getElementById('mccheckInDate' + (currentId - 1)).value;
            document.getElementById('currentCenterDate').value = preDate;
            var preNights = document.getElementById('mcnoOfNights' + (currentId - 1)).value;
            var dt = getDate(preDate, preNights);
            document.getElementById('mccheckInDate' + (currentId)).value = dateToString(dt);
        }
    }
    var currentCheckingDate = document.getElementById('mccheckInDate' + currentId).value;
    var date = currentCheckingDate.substring(0, 2);
    var month = currentCheckingDate.substring(3, 5);
    var year = currentCheckingDate.substring(6, 10);
    var dt = new Date();
    dt.setFullYear(year, month - 1, date);
    var preDate = document.getElementById('currentCenterDate').value;
    var preDt = new Date();
    preDt.setFullYear(preDate.substring(6, 10), preDate.substring(3, 5) - 1, preDate.substring(0, 2));

    var currentNoOfNights = document.getElementById('mcnoOfNights' + currentId).value;
    dt.setDate(parseInt(dt.getDate()) + parseInt(currentNoOfNights));
    for( var i = currentId + 1; i <= noOfCenters; i++ )
    {
        var nextDate = document.getElementById('mccheckInDate' + i).value;
        var dt2 = new Date();
        if( nextDate == null || nextDate == "" )
        {
            nextDate = dateToString(dt2);
        }

        dt2 = getDate(nextDate, 0);
        if( dt2 < dt )
        {
            nextDate = dateToString(dt);
            document.getElementById('mccheckInDate' + i).value = nextDate;
        }
        dt.setDate(parseInt(dt.getDate()) + parseInt(document.getElementById('mcnoOfNights' + i).value));
    }

}

function validateDate(centreNo)
{
    var currentDateObject = document.getElementById('mccheckInDate' + centreNo);
    var currentDate = currentDateObject.value;
    if(!(document.getElementById('mccheckInDate' + (centreNo - 1))) || currentDate.length == 0)
    {
        return true;
    }
    else
    {
        var preDate = document.getElementById('mccheckInDate' + (centreNo - 1)).value;
        var preNights = document.getElementById('mcnoOfNights' + (centreNo - 1)).value;

        var oPreDate = getDate(preDate, preNights) 
        var oCurrentDate = getDate(currentDate, 0);
        return oCurrentDate >= oPreDate;
    }
}

function setDate(currentId)
{
    var nextDateObject = document.getElementById('mccheckInDate' + (currentId - 1));
    if(  nextDateObject )
    {
        var currentDate = nextDateObject.value;
        var currentNights = document.getElementById('mcnoOfNights' + (currentId - 1)).value;
        var dt = getDate(currentDate, currentNights);
        dt = dateToString(dt);
        document.getElementById('mccheckInDate' + currentId).value = dt;
    }
}

function getDate(currentDate, nights)
{
    var dt = new Date();
    dt.setFullYear(currentDate.substring(6, 10), currentDate.substring(3, 5) - 1, (parseFloat(currentDate.substring(0, 2)) + parseFloat(nights)));
    return dt;
}

function dateToString(dt)
{
    var date = (dt.getDate() <= 9 ? ("0" + dt.getDate()) : dt.getDate()) + "/" + ((dt.getMonth() + 1) < 10 ? ("0" + (dt.getMonth() + 1)) : (dt.getMonth() + 1) ) + "/" + dt.getFullYear();
    return date;
}

/**
 * Used to set the current centre number to 'currentCenterNo' element and current centre date
 * @param id Id having the current centre number as the last charactor.
 */
function setCurrentCenterNo( id )
{
    var index = id.substring(id.length - 1, id.length);
    document.getElementById('currentCenterNo').value = index;
    document.getElementById('currentCenterDate').value = document.getElementById('mccheckInDate' + index).value;
}

/**
 * Used to handdle the tabs in the multicentre results page
 * @param lightTabIds Array of tab ids that should be marked as selected;
 * @param grayTabIds Array of tab ids that should be marked as unselected;
 * @param hideIds Array of tab ids that should be hidden.
 * @param showIds Array of tab ids that should be shown.
 * @param centerNo Centre number
 */
function showDiv( lightTabIds, grayTabIds, hideIds, showIds, showTabClass, hideTabClass , centerNo)
{
    if( !centerNo )
    {
        centerNo = '';
    }
    lightTabIds = lightTabIds.split(',');
    for( var i = 0; i < lightTabIds.length; i++ )
    {
        document.getElementById(lightTabIds[i] + centerNo).style.display = '';
        document.getElementById(lightTabIds[i] + centerNo).className = showTabClass;
    }

    var grayDivs = grayTabIds.split(',');
    for( var i = 0; i < grayDivs.length; i++ )
    {
        document.getElementById(grayDivs[i] + centerNo).className = hideTabClass;
    }
    var hideDivs = hideIds.split(',');
    for( var i = 0; i < hideDivs.length; i++ )
    {
        document.getElementById(hideDivs[i] + centerNo).style.display = 'none';
    }
    showIds = showIds.split(',');
    for( var i = 0; i < showIds.length; i++ )
    {
        document.getElementById(showIds[i] + centerNo).style.display = '';
    }
}

function showOverView( no )
{
    showDiv('overviewTab', 'PhotoGalleryTab,RoomFacTab,GEOTab,HtlFacTab', 'photoGalleryDiv,HotelFacilityDiv,roomFacilityDiv,googleMapDiv', 'overViewDiv', no, 'selected2', 'selected');
}

function showPhotoGellery( no )
{
    showDiv('PhotoGalleryTab', 'overviewTab,RoomFacTab,GEOTab,HtlFacTab', 'HotelFacilityDiv,roomFacilityDiv,googleMapDiv,overViewDiv', 'photoGalleryDiv', no, 'selected2', 'selected');
}

function showHotelFacility( no )
{
    showDiv('HtlFacTab', 'overviewTab,PhotoGalleryTab,GEOTab,RoomFacTab', 'googleMapDiv,overViewDiv,photoGalleryDiv,roomFacilityDiv', 'HotelFacilityDiv', no, 'selected2', 'selected');
}

function showRoomFacility( no )
{
    showDiv('RoomFacTab', 'overviewTab,PhotoGalleryTab,GEOTab,HtlFacTab', 'HotelFacilityDiv,googleMapDiv,overViewDiv,photoGalleryDiv', 'roomFacilityDiv', no, 'selected2', 'selected');
}

function showGoogleMap( no )
{
    showDiv('GEOTab', 'overviewTab,PhotoGalleryTab,RoomFacTab,HtlFacTab', 'overViewDiv,photoGalleryDiv,HotelFacilityDiv,roomFacilityDiv', 'googleMapDiv', no, 'selected2', 'selected');
}

/**
 * Used to show the overview tab of the all centres
 * @param centres Number of centres
 */
function showOverviewOnload( centres )
{
    centres = parseInt(centres);
    for( var i = 1; i <= centres; i++ )
    {
        showOverView(i)
    }
}

/**
 * Used to set the current hotel indexes to the parameter string.
 * @param resultIndex Result index of the hotel that should be added to the current hotels list.
 * @param centerNo Centre number of the hotel.
 */
function setCurrentHotels( resultIndex, centerNo )
{
    showOverView(centerNo);
    centerNo = parseInt(centerNo)
    var currentHotels = document.getElementById('currentHotels').value;
    var currentHotelsArray = currentHotels.split(",");
    for( var i = 0; i < currentHotelsArray.length; i++ )
    {
        if( currentHotelsArray[i].length == 0 )
        {
            continue;
        }
        var center = currentHotelsArray[i].split("_")[1];
        if( centerNo == parseInt(center) )
        {
            //            if(document.getElementById("selectedHotels").value.indexOf((resultIndex + "_" + centerNo)) > -1)
            //            {
            //                selectHotel("selectedHotel", (resultIndex + "_" + centerNo));
            //            }
            currentHotels = currentHotels.replace(currentHotelsArray[i], resultIndex + "_" + centerNo);
            break;
        }
    }
    document.getElementById('currentHotels').value = currentHotels;


}

/**
 * Used to hilight the hotel selected.
 * @param divId Div id of the hotel selected. Content of this div is selected.
 * @param resultIndexWithCenter Result index of the hotel with the centre number seperated with a '_'
 */
function selectHotel( divId, resultIndexWithCenter )
{
    var currentCenter = resultIndexWithCenter.split("_")[1];
    currentCenter = parseInt(currentCenter);
    divId = divId + resultIndexWithCenter;
    document.getElementById(divId).className = "selectThis";
    var selectedHotels = document.getElementById('selectedHotels').value;
    if( selectedHotels.length == 0 )
    {
        document.getElementById('selectedHotels').value = resultIndexWithCenter;
    }
    var selectedHotelsArray = selectedHotels.split(",");
    var setSelected = false;
    for( var i = 0; i < selectedHotelsArray.length; i++ )
    {
        if( selectedHotelsArray[i].length == 0 )
        {
            continue;
        }
        var center = selectedHotelsArray[i].split("_")[1];
        if( currentCenter == parseInt(center) )
        {
            setSelected = true;
            selectedHotels = selectedHotels.replace(selectedHotelsArray[i], resultIndexWithCenter);
            document.getElementById('selectedHotels').value = selectedHotels;
            break;
        }
    }
    if( selectedHotels.length > 0 && (!setSelected) )
    {
        document.getElementById('selectedHotels').value += "," + resultIndexWithCenter;
    }
}

/**
 * Proceed the result page with highlighting the selected hotels.
 */
function proceedWithHighlight()
{
    var selected = document.getElementById('selectedHotels').value;
    var current = document.getElementById('currentHotels').value;
    var selectedTemp = selected.split(",");
    var currentTemp = current.split(",");
    var currentAreSelected = true;
    for( var i = 0; i < selectedTemp.length; i++ )
    {
        if( selectedTemp[i].length == 0 )
        {
            continue;
        }
        if( current.indexOf(selectedTemp[i]) <= -1 )
        {
            currentAreSelected = false;
            var ans = confirm("You have already selected some other hotels, Do you need to proceed with the current selection ?");
            if( ans )
            {
                for( var x = 0; x < currentTemp.length; x++ )
                {
                    if( currentTemp[x].length == 0 )
                    {
                        continue;
                    }
                    if( selected.indexOf(currentTemp[i]) <= -1 )
                    {
                        alert(currentTemp[i])
                        selectHotel("selectedHotel", currentTemp[i]);
                    }
                }
                document.frmSelectPkg.submit();
                break;
            }
            else
            {
                var sCentre = selectedTemp[i].split("_")[1];
                var position = current.indexOf('_' + sCentre);
                var start = current.indexOf(',', (position - 4))
                if( start > position )
                {
                    start = 0;
                }
                else
                {
                    start++;
                }
                var end = current.indexOf(',', position);
                var currentResultIndex = current.substring(start, end);
                var sDiv = 'PkgRoot' + currentResultIndex;

                showPage("MulticentreAlternatives;jsessionid=" + document.getElementById('sessionId').value + "?action=submitpage&resultIndex=" + selectedTemp[i].split("_")[0] + "&currentIndex=" + -1 + "&centre=" + selectedTemp[i].split("_")[1] + "&currentResIndex=" + currentResultIndex.split('_')[0], "GET", sDiv, selectedTemp[i].split("_")[0], sCentre);
                break;
            }
        }
    }
    if( currentAreSelected )
    {
        document.frmSelectPkg.submit();
    }
}

/**
 * Proceed with the current selecttion
 * @param url Url of the proceeding servlet. 
 */
function proceed( url )
{
    document.frmSelectPkg.action = url + ";" + sessionId() + "?cssPath=" + document.getElementById('cssPath').value + "&doUpdateMcRooms=true";
    document.frmSelectPkg.submit();
}

/**
 * Used to send the ajax request the specified url.
 * @param sURL Url of the servlet that should be called by the ajax request.
 * @param sMethod Action of the request. (GET or POST)
 * @param sFrame Div id that should be replaced with the new content.
 * @param resultIndex Result index of the previous hotel. (The hotel which was selected before the ajax request is sent)
 * @param centre Centre number
 */
function ChangeHotel( centerNo, pageIndex)
{
    if(!pageIndex)
    {
        pageIndex = "";
    }
    var sFrame = "HotelAlt"+centerNo;
    document.getElementById(sFrame).style.display = "none";
    var sURL = "MulticentreAlternatives;" + sessionId() + "?cssPath=" + document.getElementById('cssPath').value+"&centre="+centerNo + "&pageIndex=" + pageIndex;
    document.getElementById('HotelAltHead'+centerNo).style.display = "";
    document.getElementById('HotelAltAniImg'+centerNo).style.display = "";
    var sMethod = "GET"
    try
    {
        xmlHttp1 = GetXmlHttpObject()
        if( xmlHttp1 == null )
        {
            alert("Browser does not support HTTP Request")
            return
        }

        xmlHttp1.onreadystatechange = function()
        {
            stateChangedMC(sFrame, xmlHttp1,centerNo);
        }
        xmlHttp1.open(sMethod, sURL, true);
        xmlHttp1.send(null);
    }
    catch( err )
    {
        alert(err);
    }
}

function closeAlt( centerNo){
    document.getElementById('HotelAltHead'+centerNo).style.display = "none";
}

/**
 * Used to get XmlHttpObject for the browser
 */
function GetXmlHttpObject()
{
    var oXMLHttp = null;

    if( window.XMLHttpRequest )
    {
        oXMLHttp = new XMLHttpRequest();
    }
    else if( window.ActiveXObject )
    {
        oXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return oXMLHttp;
}

/**
 * Used to change the hotel content div, after the ajax call is completed.
 * @param sElementName Div id that should be replaced with the new content.
 * @param oXmlHttp XmlHttpObject object used to send the ajax request.
 * @param resultIndex Result index of the previous hotel. (The hotel which was selected before the ajax request is sent)
 * @param centre Centre number
 */
function stateChangedMC(sElementName, oXmlHttp,centerNo)
{

    try
    {
        if( oXmlHttp.readyState == 4 || oXmlHttp.readyState == "complete" )
        {
            document.getElementById(sElementName).innerHTML = oXmlHttp.responseText;
            Effect.SlideDown(sElementName);
            document.getElementById('HotelAltAniImg'+centerNo).style.display = "none";

        }
    }
    catch( err )
    {
        alert(err);
    }
}



/**
 * This function is used to add to the tabs of the multicentre result page, when a tab should be disabled, since if the href
 * if removed from the tab the hight of the tab is changed and if the # is set as the href and if the tab is clicked
 * the tab content permenanty gets bold.
 */
function doNothing()
{

}

/**
 * Used to send the ajax request the specified url and disable the image after one click.
 * @param sURL Url of the servlet that should be called by the ajax request.
 * @param sMethod Action of the request. (GET or POST)
 * @param sFrame Div id that should be replaced with the new content.
 * @param resultIndex Result index of the previous hotel. (The hotel which was selected before the ajax request is sent)
 * @param centre Centre number
 */
function loadAlternative(sURL, sMethod, sFrame, resultIndex, centre)
{
    var tempUrl = sURL;
    var startIndex = tempUrl.indexOf('action') + 7;
    var endIndex = tempUrl.indexOf("&", startIndex);
    var action = tempUrl.substring(startIndex, endIndex);

    startIndex = tempUrl.indexOf('&centre') + 8;
    endIndex = tempUrl.indexOf('&', startIndex);
    var centre = tempUrl.substring(startIndex, endIndex);
    var linkObject;
    if(action.indexOf("next") > -1)
    {
        linkObject = document.getElementById('alternativePrev_' + centre);
    }
    else if(action.indexOf("prev") > -1)
    {
        linkObject = document.getElementById('alternativeNext_' + centre);
    }
    if(linkObject)
    {
        linkObject.href = "#"
    }
    showPage(sURL, sMethod, sFrame, resultIndex, centre)
}

function ShowDepartDateMc(){
    updateDate(document.getElementById('currentCenterNo').value, true);
}

