﻿var RoomValues = 
{
    maxChildCount: 4,
    maxPossibleRooms: 4,
    maxOccupancyCount: 8,
    
    // array of room objects
    rooms: [
	    {numAdults:2, numChildren:0, childAges:[], totalTravelers:2}
    ],
    
    resetRoomsArray: function()
    {
        var i;
        for(i = 0; i < this.rooms.length; i++)
        {
            this.rooms.pop();
        }
    },
    
    checkOccupancyWarningStateOnSubmit: function()
    {
        var i;
        var totalTravelersAllRooms = 0;
        
        // loop thru rooms, totaling totalTravelers; 
        // if it exceeds maxOccupancyCount, return false
        for (i = 0; i < this.rooms.length; i++)
        {
            totalTravelersAllRooms += this.rooms[i].totalTravelers;
            
            if ( totalTravelersAllRooms > this.maxOccupancyCount )
            {
                return false;
            }
        }
        return true;
    },

    modifyOccupants: function(obj)
    {
        var objName = obj.name;
        var objIdx = parseInt(obj.selectedIndex);
        var roomIdx;
        var roomOccupancyWarningDiv = "";
        var childIdx;
        var i, j;
        var thisTotalAdults = 0;
        var thisTotalChildren = 0;
        var totalTravelersAllRooms = 0;
    
        if ( objName.indexOf("adults") != -1 )
        {
            // get room idx from int in (adultsX)
            roomIdx = parseInt(objName.substr(6,1)) - 1;
            
            // look for rooms object in rooms array
            this.rooms[roomIdx].numAdults = objIdx + 1;
            this.rooms[roomIdx].totalTravelers = this.rooms[roomIdx].numAdults + this.rooms[roomIdx].numChildren;
        }
        else if ( objName.indexOf("children") != -1 )
        {
            // get room idx from int in (childrenX)     
            roomIdx = parseInt(objName.substr(8,1)) - 1;
            
            if ( objIdx < 1 ) // "NA" at idx 0 has been chosen
            {
                // remove child ages from rooms array if there are any
                if ( this.rooms[roomIdx].numChildren > 0 )
                {
                    for(i = 0; i < this.rooms[roomIdx].numChildren; i++)
                    {
                        this.rooms[roomIdx].childAges.pop();
                    }
                    
                    this.rooms[roomIdx].numChildren = 0;
                }
                
                // reset gsNumberOfTravelers to current adult count + 0 (new numChildren)
                this.rooms[roomIdx].gsNumberOfTravelers = this.rooms[roomIdx].numAdults;
            }
            else 
            {
                this.rooms[roomIdx].numChildren = objIdx;
                
                // set default values for ages in childAges array to 1 
                for(j = 0; j < objIdx; j++)
                {
                    //don't copy over age if one exists
                    if( isNaN( this.rooms[roomIdx].childAges[j] ) )
                    {
                        this.rooms[roomIdx].childAges[j] = 1;
                    }
                }
            }
            
            // reset total travelers
            this.rooms[roomIdx].totalTravelers = this.rooms[roomIdx].numAdults + this.rooms[roomIdx].numChildren;
        }
        else if ( ( objName.indexOf("child") != -1 ) && ( objName.indexOf("Age") != -1 ) )
        {
            // get room idx from first int in id (childXAge4)
            roomIdx = parseInt(objName.substr(5,1)) - 1;
                
            // get "position" of child
            childIdx = parseInt(objName.substr(9,1)) - 1;
            
            // set child age at appropriate position to age selected
            // 1 is the offset between the objIndex (0-base) and the age set (starts at 1 yr of age) 
            this.rooms[roomIdx].childAges[childIdx] = objIdx + 1;
        }
        
        // loop thru rooms and count travelers 
        for (i = 0; i < this.rooms.length; i++)
        {
            totalTravelersAllRooms += this.rooms[i].totalTravelers;
        }
        
        // DISPLAY WARNING IF NUMBER OF TRAVELERS EXCEEDS this.maxOccupancyCount
        if ( totalTravelersAllRooms > this.maxOccupancyCount )
        {
            document.getElementById("divOccupancyWarning").style.display = "block";
            CommonResTool.paxCountExceeded = true;
        }
        else 
        {
            document.getElementById("divOccupancyWarning").style.display = "none";
            CommonResTool.paxCountExceeded = false;
        }
            
        /* call function to make pax/child age values ready for booking engine */
        this.createPipeDelimitedStrings();
    },
    
/*-----------------------------------------------------------------------------------------------
 * pipe-delimited age and pax count values to submit to booking engine */
    
    gsAge1: "",
    gsAge2: "",
    gsAge3: "",
    gsAge4: "",
    gsNumberOfTravelers: "",
    
    createPipeDelimitedStrings: function()
    {
        /* reset gsNumberOfTravelers */
        this.gsNumberOfTravelers = "";
        
        /* reset gsAge variables */
        this.gsAge1 = "";
        this.gsAge2 = "";
        this.gsAge3 = "";
        this.gsAge4 = "";
        
        var i,j,k,m,n,p,q,r,t;
        var currentRoomCount = this.rooms.length;
        var thisChildCount = 0;
        var highChildCount = 0;
        var thisPipeDelimitedStr = "";
        var currentPipeDelimitedStr = "";
        var gsAges = new Array();
        
        /* create pipe-delim pax totals string */
        for(i = 0; i < currentRoomCount; i++)
        {
            if(i > 0)
            {
                this.gsNumberOfTravelers += "|";
            }
            
            this.gsNumberOfTravelers += this.rooms[i].totalTravelers; 
        }
        
        /* get highest number children selected */
        for (j = 0; j < currentRoomCount; j++ )
        {
            thisChildCount = this.rooms[j].numChildren;
            
            if ( thisChildCount > highChildCount )
            {
                highChildCount = thisChildCount;
            }
        }
       
        /*--------------------------------------------------------------------------------------*/
        /* create 2-dimensional array of child position/rooms */
        for(k = 0; k < highChildCount; k++)
        {
            gsAges[k] = new Array();
            
            for(m = 0; m < currentRoomCount; m++)
            {
                // if the childAges array idx exists
                if ( this.rooms[m].childAges[k] ) 
                { 
                    gsAges[k][m] = this.rooms[m].childAges[k];
                }
                else {
                    gsAges[k][m] = "";
                }
            }
        }
        
        /*--------------------------------------------------------------------------------------*/
        /* create pipe-delimited child/room values */
        var thisAge = "";
        
        for(p = 0; p < gsAges.length; p++)
        {
            for(q = 0; q < currentRoomCount; q++)
            {
                thisAge = gsAges[p][q];
                
                if ( q > 0 )
                {
                    thisPipeDelimitedStr += "|";
                }
	            
                thisPipeDelimitedStr += thisAge;
            }
            
            eval("this.gsAge" + (p+1) + " = \"" + thisPipeDelimitedStr + "\"");
            
            // reset thisPipeDelimitedStr
            thisPipeDelimitedStr = ""; 
        }
        
        /*--------------------------------------------------------------------------------------*/
        /* set pipes in child age positions which are not used */
        
        //DEBUG
        //alert("highChildCount: " + highChildCount);
        //alert("currentRoomCount: " + currentRoomCount);
        
        var thisAgePosElm;
        
        switch(currentRoomCount)
        {
            case 1:
                currentPipeDelimitedStr = "";
            break;
            case 2:
                currentPipeDelimitedStr = "|";
            break;
            case 3:
                currentPipeDelimitedStr = "||";
            break;
            case 4:
                currentPipeDelimitedStr = "|||";
            break;
        }
                
        for(r = 1; r < (this.maxChildCount + 1); r++ )
        {
           thisAgePosElm = eval("this.gsAge" + r);
           
           if ( thisAgePosElm == "" )
           {
                eval("this.gsAge" + r + " = \"" + currentPipeDelimitedStr + "\"");
           }  
        }
        
        //DEBUG
        //alert(currentPipeDelimitedStr);
        
        // test age return values
        //alert("this.gsAge1 = " + this.gsAge1 + "\n" + "this.gsAge2 = " + this.gsAge2 + "\n" + "this.gsAge3 = " + this.gsAge3 + "\n" + "this.gsAge4 = " + this.gsAge4 + "\n");
        
        document.forms["SBTresTool"].gsAge1.value = this.gsAge1;
        document.forms["SBTresTool"].gsAge2.value = this.gsAge2;
        document.forms["SBTresTool"].gsAge3.value = this.gsAge3;
        document.forms["SBTresTool"].gsAge4.value = this.gsAge4;
        document.forms["SBTresTool"].gsAge5.value = currentPipeDelimitedStr;

        document.forms["SBTresTool"].gsNumberOfTravelers.value = this.gsNumberOfTravelers;
    }
};

