﻿/******************************************************************************************************************************************/
/***************************************************** HotelSearchControl.js **************************************************************/
/******************************************************************************************************************************************/

/*Control default prefix used by .Net*/
var ControlPrefix = 'ctl';
/*Masterpage Id*/
var MasterPageMainId = 'MasterPage_Main';
/*ResultsPanelRepeater Id that is bound to the Accommodation segment array*/
var ResultsPanelRepeaterId = 'ResultsPanelRepeater';
/*Panel1 Id of asp panel that contains the structure of the data item template*/
var Panel1Id = 'Panel1';
/*Accommodation Repeater Id that is a child repeater in the Results Panel Repeater bound with AccommodationUnit array*/
var AccommodationRepeaterId='AccommodationRepeater';
/*Room Options repeater Id that is a child repeater in the Accommodation Repeater bound with RoomsOptionsAvailable array*/
var RoomOptionsRepeaterId = 'RoomOptionsRepeater';
/*Qty select combobox Id that is for each room type*/
var QtyId = 'Qty';
/*RoomOptionsContainerId of div that has all the data related to a particular Room type*/
var RoomOptionsContainerId = 'RoomOptionsContainer';
/*MaxOccupacy hidden field id that resides in RoomOptionsContainer*/
var MaxOccupancyId = 'MaxOccupancy';
/*MinOccupancy hidden field id that resides in RoomOptionsContainer*/
var MinOccupancyId = 'MinOccupancy';
/*MaxExtraChildren hidden field id that resides in RoomOptionsContainer*/
var MaxExtraChildrenId = 'MaxExtraChildren';
/*MaxExtraInfants hidden field id that resides in RoomOptionsContainer*/
var MaxExtraInfantsId = 'MaxExtraInfants';
/*MaxExtraKids hidden field id that resides in RoomOptionsContainer*/
var MaxExtraKidsId = 'MaxExtraKids';
/*QuantityAvailable hidden field id that resides in RoomOptionsContainer*/
var QuantityAvailableId = 'QuantityAvailable';
/*BoardCode hidden field id that resides in RoomOptionsContainer*/
var BoardCodeId = 'BoardCode';
/*FittingOccupacy hidden field id that resides in RoomOptionsContainer*/
var FittingOccupacyId = 'FittingOccupacy';
/*HotelSearch control id*/
var HotelSearchControl = 'HotelSearchControl'; 
/*GenericHotelSearch control id*/

var CalendarDepartureTxtBoxPrefix;
var CalendarArrivalTxtBoxPrefix;
//Added 5-5-2008
var ParentRepeaterId = 'ParentRepeater';
var AccommodationRepeaterTestId = 'AccommodationRepeaterTest'
var RadioBoardValue ='';
var AccomIndex ='AccomIndex';
var RadioIndex ='RadioIndex';
/*Global Vars to get to all functions*/
/*Segment Board Code to be set*/
var SegmentBoardCode = '';

function CheckValidRoomSelection(SegmentPrefixId)
{
    var RoomsOccupacy;
    RoomsOccupacy = ChangeLimitingOnSelection(SegmentPrefixId,true)
    
    for (i=0;i<RoomsOccupacy.length;i++)
    {
        var Occupied=RoomsOccupacy[i][0];
        var Adults=RoomsOccupacy[i][1];
        var Children=RoomsOccupacy[i][2];
        var Infants=RoomsOccupacy[i][3];
        
        /*If not already occupied then check if it can be occupied in the current room*/
        if (Occupied==0)
        {
            alert('Please select rooms correctly.');
            return false;
        }
    }
    if(document.getElementById("CustomSelectionChange"))
    document.getElementById("CustomSelectionChange").value="1";

    return true;

}



//ShowRoomsControl()
//Shows and hides the Rooms control on basis of the number of rooms selected in the RoomsList
//Usage: HotelSearch Control
function ShowRoomsControl()
{

if(document.getElementById(MasterPageMainId+'_'+HotelSearchControl+'_RoomsList'))
{
    Rooms = document.getElementById(MasterPageMainId+'_'+HotelSearchControl+'_RoomsList').value;
    Final = document.getElementById(MasterPageMainId+'_'+HotelSearchControl+'_RoomsList').item(document.getElementById(MasterPageMainId+'_'+HotelSearchControl+'_RoomsList').length-1).value;
    for(i=document.getElementById(MasterPageMainId+'_'+HotelSearchControl+'_RoomsList').item(0).value;i<=Final;i++)
    {
        if(i<=Rooms)
        {
            if(document.getElementById("Room"+i+"Row"))
                document.getElementById("Room"+i+"Row").style.display='';
        }
        else
        {
            if(document.getElementById("Room"+i+"Row"))
                document.getElementById("Room"+i+"Row").style.display='none';
        }
    }
    }
}

//ValidateCountry()
//Validation of the controls before postback
//Usage:HotelSearch Control
function validateCountry()
{
    if (document.getElementById(MasterPageMainId+"_"+HotelSearchControl+"_txtRegion"))
    {
        if (document.getElementById(MasterPageMainId+"_"+HotelSearchControl+"_txtRegion").value == "")
        {
            alert('Please enter a destination.');
            document.getElementById(MasterPageMainId+"_"+HotelSearchControl+"_txtRegion").focus();
            return false;
        }
//        else if (document.getElementById(MasterPageMainId+"_"+HotelSearchControl+"_RegionList").value == "")
//        {
//            alert('Please select a Region.');
//            return false;
//        }
        else
            
            return true;
    }

    else
    {   
        if(document.getElementById(MasterPageMainId+"_"+HotelSearchControl+"_AccommodationID"))
         {
            if(document.getElementById(MasterPageMainId+"_"+HotelSearchControl+"_AccommodationID").value == "")
            {
                alert('Please enter a Supplier code/AccommodationID.');
                return false;
            }
            else
            {
                var SupplierValue = document.getElementById(MasterPageMainId+"_"+HotelSearchControl+"_AccommodationID").value;
                if(SupplierValue.match("/"))
                {
                        return true;
                }
                else
                {
                    alert('Ensure that specified Supplier code/AccommodationID contain /');
                    return false;
                }
            }
         }
        return false;
    }
}

function validateDate()
{
    var departure = CalendarDepartureTxtBoxPrefix + '_FlightDate';
   
    var arrival = CalendarArrivalTxtBoxPrefix + '_FlightDate';
    if (document.getElementById(departure) && document.getElementById(arrival))
    {

        var Start = document.getElementById(departure).value
        var StartDay = Start.substring('0','2')
        var StartMonth = Start.substring('3','5')
        var StartYear = Start.substring('6')
        
        var startDate = new Date();
        startDate.setMonth(StartMonth-1);
        startDate.setFullYear(StartYear);
        startDate.setDate(StartDay);

        var End = document.getElementById(arrival).value
        var EndDay = End.substring('0','2')
        var EndMonth = End.substring('3','5')
        var EndYear = End.substring('6')
        
        var endDate = new Date();
        endDate.setMonth(EndMonth-1);
        endDate.setFullYear(EndYear);
        endDate.setDate(EndDay);


        if(startDate >= endDate)
        {
            alert('Check out date should be greater than Check in date');
            return false;
        }
       else
            return true;
    }
    else
        return false;
}
//onBodyload()
//get initial things setup for the search control on body onload
//Usage: HotelSearch Control
function onbodyload()
{
    ShowRoomsControl();
    //populateDays(document.getElementById('CheckInML').value, document.getElementById('CheckInDL'),document.getElementById('CheckInDayOfMonth').value);
    //populateDays(document.getElementById('CheckOutML').value, document.getElementById('CheckOutDL'),document.getElementById('CheckOutDayOfMonth').value);
    //initialise();
}

//GetRoomQty(QtyAvailable,Object)
//QtyAvailable: Max available number of rooms
//Object: Qty select control that has to be filled with available rooms
//Fills the Object(Assumed as a select control) with rooms to select according to the max available rooms provided in QtyAvailable
//Usage: NOT USED
function GetRoomQty(QtyAvailable,Object)
{
    if (Object)
    {
        for (;Object.children.length!=0;)
        {
            Object.removeChild(Object.children[0]);
        }

        for (i=0;i<=QtyAvailable;i++)
        {
            Object.add(new Option(i,i));
        }
        Object.selectedIndex=0;
    }
}

//SelectRooms()
//Selects the default Rooms for each accommodation displayed that best fits the users desired occupacy
//Usage: HotelResults page
function SelectRooms()
{
    document.getElementById(MasterPageMainId+'_DefaultRoomSelection').value='1';
    var NumRoomsRequired = document.getElementById(MasterPageMainId+'_'+HotelSearchControl+'_SearchNumOfRooms').value;
    var RoomsOccupacy=fillOccupacy(RoomsOccupacy,NumRoomsRequired)
    var i=1;

    do
    {
        var ElementId = MasterPageMainId + "_";
        var ParentElementSegmentId =''
        ElementId =ElementId + ResultsPanelRepeaterId + "_";
        ElementId = ElementId + ControlPrefixWithIndex(i);
        ParentElementSegmentId = ElementId  + ParentRepeaterId;
    
        //Call fuction for default room selection 
        TestSelectRoomsForSegment(ParentElementSegmentId,null,false);    
    
        var PanelElementId =ElementId + Panel1Id;
        i = i+2;
        var QtyArray=SelectRoomsForSegment(ElementId,RoomsOccupacy,false);
        LimitRoomsForSegment(QtyArray);
        SegmentBoardCode='';
    }while (document.getElementById(PanelElementId))
}

//fillOccupacy(RoomsOccupacy,NumRoomsRequired,MasterPageMainId)
//RoomsOccupacy: Occupacy array that will hold the desired occupacy room details
//NumRoomsRequired: Max number of rooms required by the party
//MasterPageMainId: Id prefix for the controls
//RETURN: 2D array of room occupacy selected by the user
//gets the values of selected occupacy from hidden controls and fills a 2D array with it
//Usage: Called from javascript function SelectRooms
function fillOccupacy(RoomsOccupacy,NumRoomsRequired)
{
    RoomsOccupacy=new Array();
    if (NumRoomsRequired)
    {
        for(i=0;i<NumRoomsRequired;i++)
        {
            var Adults=0;
            var Children=0;
            var Infants=0;
            
            if (document.getElementById(MasterPageMainId+'_'+HotelSearchControl+'_Room'+i+'Adults'))
            {
                if(document.getElementById(MasterPageMainId+'_'+HotelSearchControl+'_Room'+i+'Adults').value !="")
                    Adults=parseInt(document.getElementById(MasterPageMainId+'_'+HotelSearchControl+'_Room'+i+'Adults').value);
            }

            if (document.getElementById(MasterPageMainId+'_'+HotelSearchControl+'_Room'+i+'Children'))
            {
                if(document.getElementById(MasterPageMainId+'_'+HotelSearchControl+'_Room'+i+'Children').value !="")
                    Children=parseInt(document.getElementById(MasterPageMainId+'_'+HotelSearchControl+'_Room'+i+'Children').value);
            }


            if (document.getElementById(MasterPageMainId+'_'+HotelSearchControl+'_Room'+i+'Infants'))
            {
                if(document.getElementById(MasterPageMainId+'_'+HotelSearchControl+'_Room'+i+'Infants').value !="")
                    Infants=parseInt(document.getElementById(MasterPageMainId+'_'+HotelSearchControl+'_Room'+i+'Infants').value);
            }

            RoomsOccupacy[i] = new Array(0,Adults,Children,Infants);
        }
    }
    return RoomsOccupacy;
}

function ControlPrefixWithIndex(index)
{
	var ElementId = ControlPrefix
	
	if (index <10){ElementId = ElementId + "0";}
	ElementId = ElementId + index+ "_";
	
    return ElementId;
}

//SelectRoomsForSegment(SegmentPrefixId,RoomsOccupacy)
//SegmentPrefixId: Prefix for a particular segment(Accommodation)
//RoomsOccupacy: Array of Desired Occupacy
//Selects the best fit Rooms for a particular Accommodation asked for
//Usage: Called from Javascript function SelectRooms
function SelectRoomsForSegment(SegmentPrefixId,RoomsOccupacy,customSelection)
{
    var LocalRoomsOccupacy;
    if (customSelection == false)
        LocalRoomsOccupacy = MakeCopyOfOccupacy(RoomsOccupacy);
    else if (customSelection == true)
        LocalRoomsOccupacy = RoomsOccupacy;
    
    var i=1;
	var j=0;

	var ElementId = AccommodationRoomsOptionPrefix(SegmentPrefixId,i,j);
	
	var QtySubelementId = ElementId + QtyId;
	var DivAId = ElementId + RoomOptionsContainerId;
	var QtyArray=new Array();
	var QtyIndex=0;

	while(document.getElementById(DivAId))
	{
		while(document.getElementById(QtySubelementId ))
        {
            var RoomDetailArray=FillRemainingRoomOccupacy(DivAId,QtySubelementId ,LocalRoomsOccupacy,customSelection);
            QtyArray[QtyIndex++] = RoomDetailArray;
            
            j= j + 2;
            ElementId = AccommodationRoomsOptionPrefix(SegmentPrefixId,i,j);
            DivAId = ElementId + RoomOptionsContainerId;
            QtySubelementId = ElementId + QtyId;
        }
        i = i + 2;
        j=0;
        ElementId = AccommodationRoomsOptionPrefix(SegmentPrefixId,i,j);
		
		QtySubelementId = ElementId + QtyId;
		DivAId = ElementId + RoomOptionsContainerId;
    }
    return QtyArray;
}

//MakeCopyOfOccupacy(SourceArray)
//SourceArray: The array that has to be copied
//RETURN: A copy of the array that will have a saperate memory allocated
//Makes a copy of the source array to a new array so that we can make changes in the new array keeping the source array intact
//Usage: Called from Javascript function LimitRoomsForSegment
function MakeCopyOfOccupacy(SourceArray)
{
    var DestinationArray;
    if(SourceArray)
    {
        if(SourceArray.length>0)
        {
            DestinationArray = new Array();
            var i;
            for(i=0;i<SourceArray.length;i++)
            {
                if(SourceArray[i].length>0)
                {
                    DestinationArray[i] = new Array();
                    var j;
                    for (j=0;j<SourceArray[i].length;j++)
                    {
                        DestinationArray[i][j]=SourceArray[i][j];
                    }
                }
            }
        }
    }
    return DestinationArray;
}

//FillRemainingRoomOccupacy(ParentId,SelectQtyId,RoomsOccupacy)
//ParentId: Parent div id which has the whole RoomsOptions section
//SelectQtyId: Qty in that particular Parent div that has to be given the best fit room selection
//RoomsOccupacy: Array of Desired Occupacy
//Marks the unoccupied occupacy rows to occupied if they fit into the given room
//Usage: Called from Javascript function LimitRoomsForSegment
function FillRemainingRoomOccupacy(ParentId,SelectQtyId,RoomsOccupacy,customSelection)
{
    var MaxOccupacyValue=0;
    var MinOccupacyValue=0;
    var MaxExtraChildrenValue=0;
    var QuantityAvailableValue=1;
    var BoardCodeValue='';
    var FittingOccupacyObject;
    var i;
    var RoomDetailsArray;

    if (document.getElementById(ParentId))
    {
        if(document.getElementById(SelectQtyId))
        {
            var DivA = document.getElementById(ParentId);
            var InputsArray= DivA.getElementsByTagName("input");
            var Qty = document.getElementById(SelectQtyId);
            var FittlingOccupacyFullId = SelectQtyId.replace(QtyId ,FittingOccupacyId );

            if(InputsArray)
            {
                for (i=0;i<InputsArray.length;i++)
                {
                    if(InputsArray[i].id==MaxOccupancyId)
                    {
                        if(InputsArray[i].value!=""){MaxOccupacyValue=parseInt(InputsArray[i].value);}
                    }
                    if(InputsArray[i].id==MinOccupancyId)
                    {
                        if(InputsArray[i].value!=""){MinOccupacyValue =parseInt(InputsArray[i].value);}
                    }
                    if(InputsArray[i].id==MaxExtraChildrenId )
                    {
                        if(InputsArray[i].value!=""){MaxExtraChildrenValue =parseInt(InputsArray[i].value);}
                    }
                    if(InputsArray[i].id==QuantityAvailableId )
                    {
                        if(InputsArray[i].value!=""){QuantityAvailableValue =parseInt(InputsArray[i].value);}
                    }
                    if(InputsArray[i].id==BoardCodeId )
                    {
                        if(InputsArray[i].value!=""){BoardCodeValue =InputsArray[i].value;}
                    }
                    if(InputsArray[i].id==FittlingOccupacyFullId)
                    {
                        FittingOccupacyObject =InputsArray[i];
                        FittingOccupacyObject.value="";
                    }
                }
            }
            
            RoomDetailsArray = new Array;
            
            RoomDetailsArray[RoomDetailsArray.length]=Qty;
            RoomDetailsArray[RoomDetailsArray.length]=MinOccupacyValue;
            RoomDetailsArray[RoomDetailsArray.length]=MaxOccupacyValue;
            RoomDetailsArray[RoomDetailsArray.length]=MaxExtraChildrenValue;
            RoomDetailsArray[RoomDetailsArray.length]=QuantityAvailableValue;
            RoomDetailsArray[RoomDetailsArray.length]=BoardCodeValue;
            
            var RoomsSelected=0;
            if(RoomsOccupacy)
            {
                for (i=0;i<RoomsOccupacy.length;i++)
                {
                    if (customSelection == true && RoomsSelected >=Qty.selectedIndex)
                            break;

                    var Occupied=RoomsOccupacy[i][0];
                    var Adults=RoomsOccupacy[i][1];
                    var Children=RoomsOccupacy[i][2];
                    var Infants=RoomsOccupacy[i][3];
                    
                    /*If not already occupied then check if it can be occupied in the current room*/
                    if (Occupied==0 && RoomsSelected<QuantityAvailableValue && (BoardCodeValue==SegmentBoardCode || SegmentBoardCode==''))
                    {
                        var TotalPassengers = Adults + Children;
                        
                        var MaxPassengerSpace = MaxOccupacyValue + MaxExtraChildrenValue;
                        var MinPassengerSpace = MinOccupacyValue; 
             
                        /*If the total passengers lies between the max and min possible passengers in the room then the occupacy fits this room*/           
                        if ( TotalPassengers<= MaxPassengerSpace && TotalPassengers >= MinPassengerSpace)
                        {
                            Occupied = 1;
                            RoomsSelected++;
                            if (FittingOccupacyObject)
                            {
                                if(FittingOccupacyObject.value =="")
                                    FittingOccupacyObject.value =FittingOccupacyObject.value + i;
                                else
                                    FittingOccupacyObject.value =FittingOccupacyObject.value +";"+i;
                            }
                        }
                        RoomsOccupacy[i][0] = Occupied;
                        if(SegmentBoardCode=="" && BoardCodeValue!=""){SegmentBoardCode=BoardCodeValue;}
                    }
                }
            }
            if(FittingOccupacyObject)
                RoomDetailsArray[RoomDetailsArray.length]=FittingOccupacyObject.value;

            if (customSelection == false)
                Qty.selectedIndex=RoomsSelected;
        }
    }
    return RoomDetailsArray;
}


//LimitRoomsForSegment(SegmentPrefixId,RoomsOccupacy)
//SegmentPrefixId: Prefix for a particular segment(Accommodation)
//RoomsOccupacy: Array of Desired Occupacy
//Selects the best fit Rooms for a particular Accommodation asked for
//Usage: Called from Javascript function SelectRooms
function GetTotalRoomsOccupied(QtyArray)
{
    var TotalRoomsOccupied=0;
    
    if (QtyArray)
    {
        if(QtyArray.length>0)
        {
            var i;
	        for (i=0;i<QtyArray.length;i++)
	        {
	            if(QtyArray[i])
	            {
	                if(QtyArray[i].length>0)
	                {
                        var QtyValue=0;
                        if (QtyArray[i][0].value != ""){QtyValue=parseInt(QtyArray[i][0].value);}

                        TotalRoomsOccupied = TotalRoomsOccupied + QtyValue;
	                }
	            }
	        }
        }
    }

    return TotalRoomsOccupied;
}


function LimitRoomsForSegment(QtyArray)
{
    var TotalRoomsOccupied = GetTotalRoomsOccupied(QtyArray);
    var NumRoomsRequired = document.getElementById(MasterPageMainId+'_'+HotelSearchControl+'_SearchNumOfRooms').value;
    
    
    if(QtyArray)
    {
        if(QtyArray.length>0)
        {
            var i;
            for (i=0;i<QtyArray.length;i++)
            {
                if (QtyArray[i])
                {
                    if(QtyArray[i].length>0)
                    {
                        var Selected=QtyArray[i][0].selectedIndex;
                        var BoardCodeValue=QtyArray[i][5];
                        if(TotalRoomsOccupied<=NumRoomsRequired && (BoardCodeValue==SegmentBoardCode||SegmentBoardCode==''))
                        {
                            var ExtraRoomsOffered=NumRoomsRequired-TotalRoomsOccupied;
                            GetRoomQty(Selected+ExtraRoomsOffered,QtyArray[i][0]);
                            QtyArray[i][0].selectedIndex = Selected;
                        }
                        else
                        {
                            GetRoomQty(0,QtyArray[i][0]);
                        }
                    }
                }    
            }
        }
    }
}

function AccommodationRoomsOptionPrefix(SegmentPrefixId,i,j)
{
    var ElementId = SegmentPrefixId;
    ElementId = ElementId + AccommodationRepeaterId + "_";
    ElementId = ElementId + ControlPrefixWithIndex(i);
    
    ElementId = ElementId;
	ElementId = ElementId + RoomOptionsRepeaterId + "_";
	ElementId = ElementId + ControlPrefixWithIndex(j);
	
	return ElementId;
}

function ParentRepeaterSufix(SegmentPrefixId,i,j)
{
    var ElementId = SegmentPrefixId;
     ElementId = ElementId + "_" +  ControlPrefixWithIndex(i);
    
//    ElementId = ElementId;
//	ElementId = ElementId + RoomOptionsRepeaterId + "_";
//	ElementId = ElementId + ControlPrefixWithIndex(j);
//	
	return ElementId;
}


function AccommodationTestRoomsOptionPrefix(SegmentPrefixId,i,j)
{

  var ElementId = SegmentPrefixId;
    ElementId = ElementId + AccommodationRepeaterTestId + "_";
    ElementId = ElementId + ControlPrefixWithIndex(i);
    
    ElementId = ElementId;
	ElementId = ElementId + RoomOptionsRepeaterId + "_";
	ElementId = ElementId + ControlPrefixWithIndex(j);
	
	return ElementId;
}

function GetAccomodationIdValue(SegmentPrefixId,i,j)
{
    var ElementId = SegmentPrefixId;
    ElementId = ElementId + AccommodationRepeaterTestId + "_";
    ElementId = ElementId + ControlPrefixWithIndex(i);
    
    ElementId = ElementId + AccomIndex;
    return ElementId;
}
function ChangeLimitingOnSelection(SegmentPrefixId,customSelection)
{
    SegmentPrefixId = SegmentPrefixId + "_";
    var NumRoomsRequired = document.getElementById(MasterPageMainId+'_'+HotelSearchControl+'_SearchNumOfRooms').value;
    var RoomsOccupacy=fillOccupacy(RoomsOccupacy,NumRoomsRequired);
    var QtyArray=SelectRoomsForSegment(SegmentPrefixId,RoomsOccupacy,customSelection);
    if (customSelection==true)
    {
        LimitQtyOnCustomSelection(RoomsOccupacy,QtyArray);
        SegmentBoardCode='';
        return RoomsOccupacy;
    }
    else
    {
        LimitRoomsForSegment(QtyArray);   
        SegmentBoardCode='';
    }
}
function ReverseMapRadioSelection(SegmentPrefixId,RadioObject,customSelection)
{

    var i=1;
	var j=0;
    var k=0;   
    
    var RoomsCollectionArray = new Array();
    var RoomsCollectionIndex = 0;
    
    
    var RoomsTypeArray = new Array();
    var RoomsTypeIndex = 0;
    
    var AccomIdHiddenVarValue;
    var RadioIdHiddentVarValue;
    
    if (customSelection)
    {
        RadioBoardValue = RadioObject.value;
    }
   
    
    var SegmentPrefixId1 = ParentRepeaterSufix(SegmentPrefixId,i,j);
    
    //To store the hidden variable value of accomodation id 
    AccomIdHiddenVarValue = GetAccomodationIdValue(SegmentPrefixId1,k,j);
    
    var ElementId = AccommodationTestRoomsOptionPrefix(SegmentPrefixId1,k,j);
    
    //To store the hidden variable value of Radioid
    RadioIdHiddentVarValue = ElementId + RadioIndex;
    
    var SelectedRoomsId = SegmentPrefixId1  + "SelectedRoom";
    var DivAId = ElementId + RoomOptionsContainerId;
    var RoomsSegment = DivAId;
    
     while(document.getElementById(DivAId))
     {
            //For traversing each accomodation in every room type 
            while(document.getElementById(DivAId))
            {
                    ElementId = AccommodationTestRoomsOptionPrefix(SegmentPrefixId1,k,j);
                    
                     //To store the hidden variable value of Radioid
                     RadioIdHiddentVarValue = ElementId + RadioIndex;
                     
                     
                    DivAId = ElementId + RoomOptionsContainerId;
                    while(document.getElementById(DivAId))
                    {   
                            RoomsTypeArray[RoomsTypeIndex++]= FillRoomArray(DivAId,AccomIdHiddenVarValue,RadioIdHiddentVarValue,SelectedRoomsId);
                            j=j+2;
                            ElementId = AccommodationTestRoomsOptionPrefix(SegmentPrefixId1,k,j);
                            
                             //To store the hidden variable value of Radioid
                             RadioIdHiddentVarValue = ElementId + RadioIndex;
                            
                            DivAId = ElementId + RoomOptionsContainerId;
                     }
                     k = k+2;
                     j=0;
                     AccomIdHiddenVarValue = GetAccomodationIdValue(SegmentPrefixId1,k,j);
                     ElementId = AccommodationTestRoomsOptionPrefix(SegmentPrefixId1,k,j);
                     
                      //To store the hidden variable value of Radioid
                      RadioIdHiddentVarValue = ElementId + RadioIndex;
                     
                     DivAId = ElementId + RoomOptionsContainerId;
            
                   
            }
            
            RoomsCollectionArray[RoomsCollectionIndex++] = RoomsTypeArray;
            RoomsTypeArray= new Array();
            RoomsTypeIndex = 0;
            
            //write the  code for going to the next room 
            k = 0;
            i=i+2;
            j=0;
            SegmentPrefixId1 = ParentRepeaterSufix(SegmentPrefixId,i,j);
            AccomIdHiddenVarValue = GetAccomodationIdValue(SegmentPrefixId1,k,j);
            SelectedRoomsId = SegmentPrefixId1 + "SelectedRoom";
            var ElementId = AccommodationTestRoomsOptionPrefix(SegmentPrefixId1,k,j);
            var DivAId = ElementId + RoomOptionsContainerId;
      }
      
     //To recheck those rooms that have been selected 
     RecheckRoomSelection(RoomsCollectionArray);
     
}

function RecheckRoomSelection(RoomsCollectionArray)
{

  var i;
  var j;
  var SelectedRoomValue;
  var RoomValueBoolean=false;
  
  var RoomsArray =new Array();
  var RoomsTypeArray =new Array();
  //Loop through the room collection array 
  for (i=0;i<RoomsCollectionArray.length;i++)
   {
        //Take all the accomodation details of a particular room 
         SelectedRoomValue = RoomsCollectionArray[i][0][2].value;
         
           RoomsTypeArray = RoomsCollectionArray[i];
         
          //From the selected value check the specific radio button. 
           for (j=0;j<RoomsTypeArray.length;j++)
            {
                //Get the value for the particular room 
                RoomsArray = RoomsTypeArray[j];
                RoomValueBoolean = MatchSelectedRoomValue(SelectedRoomValue,RoomsArray[3]+ ";" + RoomsArray[4]);
                
                if(RoomValueBoolean)
                {
                    RoomsArray[1].checked = true;
                    RoomsArray[1].parentElement.parentElement.style.backgroundColor = "#FEF3C6";
                    break;
                }
            }
            
     }
    
}

function MatchSelectedRoomValue(SelectedRoomValue,RadioRoomValue)
{
    if(SelectedRoomValue == RadioRoomValue)
    {
        return true;
    }   
    else
    {
        return false;
    }
}

function TestSelectRoomsForSegment(SegmentPrefixId,RadioObject,customSelection)
{

    var i=1;
	var j=0;
    var k=0;   
    
    var RoomsCollectionArray = new Array();
    var RoomsCollectionIndex = 0;
    
    
    var RoomsTypeArray = new Array();
    var RoomsTypeIndex = 0;
    
    var AccomIdHiddenVarValue;
    var RadioIdHiddentVarValue;
    
    if (customSelection)
    {
        RadioBoardValue = RadioObject.value;
    }
   
    
    var SegmentPrefixId1 = ParentRepeaterSufix(SegmentPrefixId,i,j);
    
    //To store the hidden variable value of accomodation id 
    AccomIdHiddenVarValue = GetAccomodationIdValue(SegmentPrefixId1,k,j);
    
    var ElementId = AccommodationTestRoomsOptionPrefix(SegmentPrefixId1,k,j);
    
    //To store the hidden variable value of Radioid
    RadioIdHiddentVarValue = ElementId + RadioIndex;
    
    var SelectedRoomsId = SegmentPrefixId1  + "SelectedRoom";
    var DivAId = ElementId + RoomOptionsContainerId;
    var RoomsSegment = DivAId;
    
     while(document.getElementById(DivAId))
     {
            //For traversing each accomodation in every room type 
            while(document.getElementById(DivAId))
            {
                    ElementId = AccommodationTestRoomsOptionPrefix(SegmentPrefixId1,k,j);
                    
                     //To store the hidden variable value of Radioid
                     RadioIdHiddentVarValue = ElementId + RadioIndex;
                     
                     
                    DivAId = ElementId + RoomOptionsContainerId;
                    while(document.getElementById(DivAId))
                    {   
                            RoomsTypeArray[RoomsTypeIndex++]= FillRoomArray(DivAId,AccomIdHiddenVarValue,RadioIdHiddentVarValue,SelectedRoomsId);
                            j=j+2;
                            ElementId = AccommodationTestRoomsOptionPrefix(SegmentPrefixId1,k,j);
                            
                             //To store the hidden variable value of Radioid
                             RadioIdHiddentVarValue = ElementId + RadioIndex;
                            
                            DivAId = ElementId + RoomOptionsContainerId;
                     }
                     k = k+2;
                     j=0;
                     AccomIdHiddenVarValue = GetAccomodationIdValue(SegmentPrefixId1,k,j);
                     ElementId = AccommodationTestRoomsOptionPrefix(SegmentPrefixId1,k,j);
                     
                      //To store the hidden variable value of Radioid
                      RadioIdHiddentVarValue = ElementId + RadioIndex;
                     
                     DivAId = ElementId + RoomOptionsContainerId;
            
                   
            }
            
            RoomsCollectionArray[RoomsCollectionIndex++] = RoomsTypeArray;
            RoomsTypeArray= new Array();
            RoomsTypeIndex = 0;
            
            //write the  code for going to the next room 
            k = 0;
            i=i+2;
            j=0;
            SegmentPrefixId1 = ParentRepeaterSufix(SegmentPrefixId,i,j);
            AccomIdHiddenVarValue = GetAccomodationIdValue(SegmentPrefixId1,k,j);
            SelectedRoomsId = SegmentPrefixId1 + "SelectedRoom";
            var ElementId = AccommodationTestRoomsOptionPrefix(SegmentPrefixId1,k,j);
            var DivAId = ElementId + RoomOptionsContainerId;
      }
      
      //Only if the rooms segment exists do the further processing
      if(document.getElementById(RoomsSegment))
      {
      
              //Check whether the radio selection is custom or default 
              if(!customSelection)
               { 
                    RadioBoardValue=RoomsCollectionArray[0][0][0];
               }
               //Call the function which will either select the default value for the radio button  or 
              //Set the value for the radio button based on the board basis 
               SetRoomSelection(RoomsCollectionArray,customSelection);
       
       }
   
}

function SetRoomSelectionColor(RoomsCollectionArray,customSelection)
{
    
  var i;
  var j;
  var RoomsTypeArray = new Array();
  var RoomsTypeIndex = 0;
  
  var CurrentBoardValue;
  var BoardValueBoolean=false;
  
  var RoomsArray =new Array();
 //Loop through the room collection array 
 for (i=0;i<RoomsCollectionArray.length;i++)
   {
        //Take all the accomodation details of a particular room 
        RoomsTypeArray = RoomsCollectionArray[i];
        if (customSelection)
        {
            for (j=0;j<RoomsTypeArray.length;j++)
            {
                 //Get the details of the first radio button of that particular accomodation   
                 RoomsArray = RoomsTypeArray[j];
                 if(RoomsArray[1].checked)
                    {
                        RoomsArray[1].parentElement.parentElement.style.backgroundColor = "#FEF3C6";
                            
                    }
                    else
                    {
                         RoomsArray[1].parentElement.parentElement.style.backgroundColor = "#FFFFFF";
                    }
            }
        }
        
   }  
    
}
//This function is used to set the room selection 
function SetRoomSelection(RoomsCollectionArray,customSelection)
{
  var i;
  var j;
  var RoomsTypeArray = new Array();
  var RoomsTypeIndex = 0;
  
  var CurrentBoardValue;
  var BoardValueBoolean=false;
  
 var RoomsArray =new Array();
   
  
  //Loop through the room collection array 
 
   for (i=0;i<RoomsCollectionArray.length;i++)
   {
        //Take all the accomodation details of a particular room 
        RoomsTypeArray = RoomsCollectionArray[i];
        if (customSelection)
        {
            for (j=0;j<RoomsTypeArray.length;j++)
            {
                 //Get the details of the first radio button of that particular accomodation   
                 RoomsArray = RoomsTypeArray[j];
                 
                 //Compare board values 
                 BoardValueBoolean = CompareRadioValues(RoomsArray);
                 
                 if(BoardValueBoolean)
                 {
                         break;
                 }
             }
        }
         //Code to retain those values of the hotelresults page on the back button navigation
        else
        {
             RoomsTypeArray = RoomsCollectionArray[i];
             for (j=0;j<RoomsTypeArray.length;j++)
             {
                 //Get the details of the first radio button of that particular accomodation   
                 RoomsArray = RoomsTypeArray[j];
                 if(RoomsArray[1].checked == true)
                 {
                    BoardValueBoolean = true;
                    break;
                 }
             }
         }
 
        if(!BoardValueBoolean)
        {
            SetRadioAsSelectedBoardValue(RoomsTypeArray);
        }
   }  
  SetRoomSelectionColor(RoomsCollectionArray,customSelection);
 
  
}


//This function is called if the board values for the selected room
//and the room under consideration is not the same.- it will then 
//select that radio button which has the same board value as the one
//previously selected.
function SetRadioAsSelectedBoardValue(RoomsTypeArray)
{
    var j;
    var RoomsArray =new Array();
    var BoardValueBoolean=false;
    var SelectedRadioSegment;
   
                for (j=0;j<RoomsTypeArray.length;j++)
                {
                     //Get the details of the first radio button of that particular accomodation   
                     RoomsArray = RoomsTypeArray[j];
                     //Compare board values 
                   
                        //If the board values are the same 
                        if(RadioBoardValue==RoomsArray[0])
                        {
                            RoomsArray[1].checked = true;
                            RoomsArray[2].value = RoomsArray[3]+ ";" + RoomsArray[4];
                            //To get the RoomOptionsContainer to set its style for the selected radio
                            SelectedRadioSegment = RoomsArray[1].parentElement.parentElement.id;
                            
                            RoomsArray[1].parentElement.parentElement.style.backgroundColor = "#FEF3C6";
                            break;
                        }
                     
                }
    
}

//This function is used to compare the board values
function CompareRadioValues(RoomsArray)
{
            var BoardValueSameBoolean;
            
             //Check if the radio button is selected 
             if(RoomsArray[1].checked)
             {
                    //Get its board value 
                  var  CurrentBoardValue = RoomsArray[0];
                  BoardValueSameBoolean= CompareBoardValues(CurrentBoardValue);
                  return BoardValueSameBoolean;
                  
                           
             }
             else
             {
                  RoomsArray[1].parentElement.parentElement.style.backgroundColor = "#FFFFFF";
                       
             }
             return false;
                          
}

function CompareBoardValues(CurrentBoardValue)
{
         //Check if a board value is already selected 
                    if(RadioBoardValue!= '')
                    {
                        //Compare the two board values
                        //If the two board values are not the same 
                        if(CurrentBoardValue == RadioBoardValue)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
}

//This function is used to fill an accomodation segment 
function FillRoomArray(DivAId,AccomIdHiddenVarValue,RadioIdHiddentVarValue,SelectedRoomsId)
{
    var RoomsArray =new Array();
    var AccomodationIdHiddenObject;
    var SelectedRoomObject;
    var RadioIdHiddenObject;
    var DivA = document.getElementById(DivAId);
    
    
    //Get the hidden variable values for 'AccomIndex' and 'RadioIndex'
    AccomodationIdHiddenObject = document.getElementById(AccomIdHiddenVarValue);
    RadioIdHiddenObject = document.getElementById(RadioIdHiddentVarValue);
    
	//Get the hidden variable object of ''
	SelectedRoomObject = document.getElementById(SelectedRoomsId);
	 
	   var Input1sArray= DivA.getElementsByTagName("input");
             
              for (i=0;i<Input1sArray.length;i++)
               {
                    if (Input1sArray[i].getAttribute('type')=='radio')
                    {
                         var element = Input1sArray[i];
                    
                         // RadioBoardValue = element.value;
                          
                          RoomsArray[0] = element.value;
                          RoomsArray[1] = element;
                          RoomsArray[2] = SelectedRoomObject;
                          RoomsArray[3] = AccomodationIdHiddenObject.value;
                          RoomsArray[4] = RadioIdHiddenObject.value;
                         RoomsArray[1].parentElement.parentElement.style.backgroundColor = "#FFFFFF";
                          if (RoomsArray[1].checked==true)
                          {
                            RoomsArray[2].value = RoomsArray[3]+ ";" + RoomsArray[4];
                            RoomsArray[1].parentElement.parentElement.style.backgroundColor = "#FEF3C6";
                          }
                          else
                          {
                              
                               RoomsArray[1].parentElement.parentElement.style.backgroundColor = "#FFFFFF";
                        
                          }
                    }
                }
		
	return RoomsArray;
   
}



function LimitQtyOnCustomSelection(RoomsOccupacy,QtyArray)
{
    var i;
    var j;
    
    if (QtyArray)
    {
        if(QtyArray.length>0)
        {
            for (j=0;j<QtyArray.length;j++)
            {
                if(QtyArray[j])
                {
                    if(QtyArray[j].length>0)
                    {
                        var Qty=QtyArray[j][0];
                        var MinOccupacyValue=QtyArray[j][1];
                        var MaxOccupacyValue=QtyArray[j][2];
                        var MaxExtraChildrenValue=QtyArray[j][3];
                        var QuantityAvailabeValue=QtyArray[j][4];
                        var BoardCodeValue=QtyArray[j][5];
                        var RoomsSelected=0;
                        
                        for (i=0;i<RoomsOccupacy.length;i++)
                        {
                            var Occupied=RoomsOccupacy[i][0];
                            var Adults=RoomsOccupacy[i][1];
                            var Children=RoomsOccupacy[i][2];
                            var Infants=RoomsOccupacy[i][3];
                            
                            /*If not already occupied then check if it can be occupied in the current room*/
                            if (Occupied==0 && (BoardCodeValue==SegmentBoardCode||SegmentBoardCode==''))
                            {
                                var TotalPassengers = Adults + Children;
                                
                                var MaxPassengerSpace = MaxOccupacyValue + MaxExtraChildrenValue;
                                var MinPassengerSpace = MinOccupacyValue; 
                     
                                /*If the total passengers lies between the max and min possible passengers in the room then the occupacy fits this room*/           
                                if ( TotalPassengers<= MaxPassengerSpace && TotalPassengers >= MinPassengerSpace)
                                    RoomsSelected++;
                            }
                        }
                        
                        var SelectedIndex = Qty.selectedIndex;
                        var QtyAvailable=SelectedIndex + RoomsSelected;
                        GetRoomQty(QtyAvailable,Qty);
                        Qty.selectedIndex = SelectedIndex;
                        Qty.value = SelectedIndex
                    }
                }
            }
        }
    }

}

function loadLargeImage(ImageUrl)
{
    var a=document.getElementById("LargeHotelImage");
    if (a)
        a.src= ImageUrl;
}
    
function SetMeActive(SelectedTab)
{
    document.getElementById(MasterPageMainId + "_ActiveIndex").value=SelectedTab;
    ClearAllTabs();
}
            
function ClearAllTabs()
{
    var i=0;
    var ActiveIndex=-1;

    if(document.getElementById(MasterPageMainId + "_ActiveIndex"))
        ActiveIndex = parseInt(document.getElementById(MasterPageMainId + "_ActiveIndex").value);

    var pre=ActiveIndex-1;
    var post=ActiveIndex+1;

    i=0;
    
    while(document.getElementById("Tab" + i + "Left"))
    {
        var MyActiveStatus="";
        var PreActiveStatus="";
        var PostActiveStatus="";
        
        if (i==pre)
        {
            PreActiveStatus="InActive";
            MyActiveStatus="InActive";
            PostActiveStatus="Active";
        }
        else if (i==ActiveIndex)
        {
            PreActiveStatus="InActive";
            MyActiveStatus="Active";
            PostActiveStatus="InActive";                    
        }
        else if (i==post)
        {
            PreActiveStatus="Active";
            MyActiveStatus="InActive";
            PostActiveStatus="InActive";                                            
        }
        else
        {
            PreActiveStatus="InActive";
            MyActiveStatus="InActive";
            PostActiveStatus="InActive";                                            
        }
        
        if (document.getElementById(MasterPageMainId + "_lnkTab"+i))
            document.getElementById(MasterPageMainId + "_lnkTab"+i).className = MyActiveStatus+"TabButton";

        if(i==0)
            document.getElementById("Tab" + i + "Left").className="FirstTab"+MyActiveStatus+"Left";
        else
            document.getElementById("Tab" + i + "Left").className="MiddleTab"+PreActiveStatus + MyActiveStatus +"Left";
        
        if(document.getElementById("Tab" + i + "Middle"))
            document.getElementById("Tab" + i + "Middle").className="Tab"+MyActiveStatus+"Middle";

        if(document.getElementById("Tab" + i + "Right"))
        {
            var next=i+1;
            if(document.getElementById("Tab" + next + "Right"))
                document.getElementById("Tab" + i + "Right").className="MiddleTab"+MyActiveStatus+PostActiveStatus+"Right";
            else
                document.getElementById("Tab" + i + "Right").className="LastTab"+MyActiveStatus+"Right";
        }
        if(document.getElementById("Tab"+i+"Div"))
        {
            if(MyActiveStatus == "Active")
                document.getElementById("Tab" + i + "Div").style.display='block';
            else
                document.getElementById("Tab" + i + "Div").style.display='none';
        }
        if(document.getElementById("lnkTab"+i))
            document.getElementById("lnkTab"+i).className=MyActiveStatus + "TabButton";

        i++;
    }
}


