/**
* Class: PersonSelect
*
* Client side javascript control allowing a user to enter
* the number of adults, children and infants.
* When children are entered, it allows
* the user to enter their ages.
*
* @author Ian Yates
* @requires Common.js
* @requires NumberRangeSelect.js
*
*/
function PersonSelect(id)
{
    this.Id;
    this.MaxRooms;
    this.MaxAdults;
    this.MaxChildren;
    this.MaxInfants;
    this.GroupHeaderText;
    this.GroupSelectHeaderText;
    this.AdultHeaderText;
    this.ChildHeaderText;
    this.InfantHeaderText;
    this.NumRooms;
    this.NumAdultsArray;
    this.NumChildrenArray;
    this.NumInfantsArray;
    this.ChildAgeArrayTable;
    this.ParentNode;
    
    this.enableRooms = false;
    
    this.divPeople;
    this.divAges;
    this.tblPeople;
    this.tblAges;
    
    this.nrsRooms;
    this.nrsAdultsArray;
    this.nrsChildrenArray;
    this.nrsInfantsArray;
    this.nrsChildAgeArrayTable;
    
    this.AppendTo;
    this.EnableRooms;
    this.EnableInfants;
    
    this.createControls;
    this.createPeopleControls;
    this.createAgeControls;
    this.configureControls;
    this.configureArraysAndTables;
    
    this.init = PersonSelect_init;
    this.init(id);
}

function PersonSelect_init(id)
{
    this.Id = id;
    this.MaxRooms = 4;
    this.MaxAdults = 6;
    this.MaxChildren = 6;
    this.MaxInfants = 6;
    this.ChildAgeStart = 2;
    this.ChildAgeEnd = 12;
    this.NumRooms = 1;
    this.NumAdultsArray = new Array();
    this.NumChildrenArray = new Array();
    this.NumInfantsArray = new Array();
    this.ChildAgeArrayTable = new Array();
    this.ParentNode = null;
    
    this.enableGroups = false;
    
    this.divPeople = null;
    this.divChildAges = null;
    this.tblPeople = null;
    this.tblAges = null;
    
    this.nrsRooms = null;
    this.nrsAdultsArray = new Array();
    this.nrsChildrenArray = new Array();
    this.nrsInfantsArray = new Array();
    this.nrsChildAgeArrayTable = new Array();
    
    this.AppendTo = PersonSelect_AppendTo;
    this.EnableRooms = PersonSelect_EnableRooms;
    this.EnableInfants = PersonSelect_EnableInfants;
    
    this.createControls = PersonSelect_createControls;
    this.createPeopleControls = PersonSelect_createPeopleControls;
    this.createAgeControls = PersonSelect_createAgeControls;
    this.configureControls = PersonSelect_configureControls;
    this.configureArraysAndTables = PersonSelect_configureArraysAndTables;
    
    this.configureArraysAndTables();
}

function PersonSelect_AppendTo(parentNode)
{
    //this.configureArraysAndTables();
    this.createControls();
    this.configureControls();
    this.ParentNode = parentNode;
    parentNode.appendChild(this.divPeople);
    parentNode.appendChild(this.divAges);
}

function PersonSelect_EnableRooms(roomsEnabled)
{
    this.enableRooms = roomsEnabled;
    // hide or show the room cells
    var roomCellDisplay = (this.enableRooms == true) ? "" : "none";
    
    // show or hide the room cells in the people table 
    for(var i = 0; i < this.tblPeople.rows.length; i++)
    {
        this.tblPeople.rows[i].cells[0].style.display = roomCellDisplay;
        this.tblPeople.rows[i].cells[1].style.display = roomCellDisplay;
    }
    
    // show or hide the room cells in the ages table
    for(var i = 0; i < this.tblAges.rows.length; i++)
    {
         this.tblAges.rows[i].cells[0].style.display = roomCellDisplay;
    }
    
    if(this.enableRooms == false)
    {
        this.NumRooms = 1;
    }
    this.configureControls();
}

function PersonSelect_createControls()
{
    this.createPeopleControls();
    this.createAgeControls();
}

function PersonSelect_createPeopleControls()
{
    //1. Create the div and label
    this.divPeople = document.createElement("div");
    
    //2. Create the table
    this.tblPeople = document.createElement("table");
    this.tblPeople.cellSpacing = 0;
    this.tblPeople.cellPadding = 0;
    this.tblPeople.border = 0;
    
    //3. Create the table header
    var tblHead = this.tblPeople.createTHead();
    var trHead = tblHead.insertRow(0);
    
    //4. Append the header cells
    var thRooms = trHead.insertCell(0);
    var thBlank = trHead.insertCell(1);
    var thAdults = trHead.insertCell(2);
    var thChildren = trHead.insertCell(3);
    var thInfants = trHead.insertCell(4);
    
    //5. Add text to the header cells
    thRooms.vAlign = "bottom";
    var roomHead = "Rooms:";
    var adultHead = "Adults (" + (parseInt(this.ChildAgeEnd) + 1) + "yrs+)";
    var childHead = "Children (" + this.ChildAgeStart + "-" + this.ChildAgeEnd + "yrs)";
    var infantHead = "Infants                       (0-" + (parseInt(this.ChildAgeStart) - 1) + "yrs)";
    thRooms.appendChild(document.createTextNode(roomHead));
    thAdults.appendChild(document.createTextNode(adultHead));
    thChildren.appendChild(document.createTextNode(childHead));
    thInfants.appendChild(document.createTextNode(infantHead));
    
    //6. Insert the first row and the room select cell
    var trPerson = this.tblPeople.insertRow(1);
    var tdRooms = trPerson.insertCell(0);
    tdRooms.className = "rooms";
    
    //7. Create the room NumberRangeSelect and append it to the cell
    this.nrsRooms = new NumberRangeSelect("roomcount");
    this.nrsRooms.SelectedValue = this.NumRooms;
    this.nrsRooms.RangeStart = 1;
    this.nrsRooms.RangeEnd = this.MaxRooms;
    this.nrsRooms.OnChange = PersonSelect_nrsRooms_onChange;
    this.nrsRooms.PersonSelect = this;
    this.nrsRooms.AppendTo(tdRooms);
    
    
    //8. Now create the people entry rows and NumberRangeSelects
    for(var i = 0; i < this.MaxRooms; i++)
    {
        if(i > 0)
        {
            trPerson = this.tblPeople.insertRow(i + 1);
            var tdBlank = trPerson.insertCell(trPerson.cells.length);
        }
        
        // create the room row cell and label
        var tdRowHead = trPerson.insertCell(trPerson.cells.length);
        tdRowHead.className = "row-head";
        tdRowHead.appendChild(document.createTextNode("Room " + (parseInt(i) + 1)));
        
        var nrsAdults = new NumberRangeSelect("adults-" + (i + 1));
        nrsAdults.RangeStart = 0;
        nrsAdults.RangeEnd = this.MaxAdults;
        nrsAdults.SelectedValue = (this.NumAdultsArray[i]) ? this.NumAdultsArray[i] : "";
        nrsAdults.Index = i;
        nrsAdults.OnChange = PersonSelect_nrsAdults_onChange;
        nrsAdults.PersonSelect = this;
        this.nrsAdultsArray[i] = nrsAdults;
        var tdAdult = trPerson.insertCell(trPerson.cells.length);
        nrsAdults.AppendTo(tdAdult);
        
        var nrsChildren = new NumberRangeSelect("children-" + (i + 1));
        nrsChildren.RangeStart = 0;
        nrsChildren.RangeEnd = this.MaxChildren;
        nrsChildren.SelectedValue = (this.NumChildrenArray[i]) ? this.NumChildrenArray[i] : 0;
        nrsChildren.Index = i;
        nrsChildren.OnChange = PersonSelect_nrsChildren_onChange;
        nrsChildren.PersonSelect = this;
        this.nrsChildrenArray[i] = nrsChildren;
        var tdChild = trPerson.insertCell(trPerson.cells.length);
        nrsChildren.AppendTo(tdChild);
        
        var nrsInfants = new NumberRangeSelect("infants-" + (i + 1));
        nrsInfants.RangeStart = 0;
        nrsInfants.RangeEnd = this.MaxInfants;
        nrsInfants.SelectedValue = (this.NumInfantsArray[i]) ? this.NumInfantsArray[i] : 0;
        nrsInfants.Index = i;
        nrsInfants.OnChange = PersonSelect_nrsInfants_onChange;
        nrsInfants.PersonSelect = this;
        this.nrsInfantsArray[i] = nrsInfants;
        var tdInfant = trPerson.insertCell(trPerson.cells.length);
        nrsInfants.AppendTo(tdInfant);
    }
    
    //9. Append
    this.divPeople.appendChild(this.tblPeople);
}

function PersonSelect_createAgeControls()
{
    //1. Create the div
    this.divAges = document.createElement("div");
    this.divAges.className = "child-ages";
    
    //2. Create the table
    this.tblAges = document.createElement("table");
    this.tblAges.cellSpacing = 0;
    this.tblAges.cellPadding = 0;
    this.tblAges.border = 0;
    
    //3. Create the div heading
    var lblAgeHead = document.createElement("span");
    lblAgeHead.className = "label";
    lblAgeHead.appendChild(document.createTextNode("How old are the children on date of return?"));
    
    //4. Insert the header cells
    
    //4. Now append the rows, cells and number selects
    for(var i = 0; i < this.MaxRooms; i++)
    {
        //insert the header row
        var trHead = this.tblAges.insertRow(this.tblAges.rows.length);
        var tdBlank = trHead.insertCell(0);
    
        for(var j = 0; j < this.MaxChildren; j++)
        {
            var tdChildHead = trHead.insertCell(trHead.cells.length);
            tdChildHead.appendChild(document.createTextNode("Child " + (parseInt(j) + 1)));
        }
        //insert the row header
        var tr = this.tblAges.insertRow(this.tblAges.rows.length);
        var tdHead = tr.insertCell(0);
        tdHead.appendChild(document.createTextNode("Room " + (parseInt(i) + 1)));
        
        //insert the NumberRangeSelects and cells
        for(var j = 0; j < this.MaxChildren; j++)
        {
            var td = tr.insertCell(tr.cells.length);
            
            var nrsAge = new NumberRangeSelect("childage-" + (parseInt(i) + 1) + "-" + (parseInt(j) + 1));
            nrsAge.DefaultText = "-?-";
            nrsAge.RangeStart = this.ChildAgeStart;
            nrsAge.RangeEnd = this.ChildAgeEnd;
            nrsAge.SelectedValue = (this.ChildAgeArrayTable[i][j]) ? this.ChildAgeArrayTable[i][j] : "";
            nrsAge.OnClick = PersonSelect_nrsAge_onChange;
            nrsAge.PersonSelect = this;
            nrsAge.Index1 = i;
            nrsAge.Index2 = j;
            this.nrsChildAgeArrayTable[i][j] = nrsAge;
            
            nrsAge.AppendTo(td);
        }
    }
    
    //5. Append
    this.divAges.appendChild(lblAgeHead);
    this.divAges.appendChild(this.tblAges);
}

function PersonSelect_configureControls()
{
    // first configure the class names for the people and child-ages divs
    if(this.enableRooms == true)
    {
        this.divPeople.className = "rooms-people field";
        this.divAges.className = "rooms-child-ages field";
    }
    else
    {
        this.divPeople.className = "people field";
        this.divAges.className = "child-ages field";
    }
    
    var rowsAreVisible = false;
    
    var ageHeadRowIndex = 0;
    var ageRowIndex = 1;
    
    // now configure the room display 
    for(var i = 1; i <= this.MaxRooms; i++)
    {
        // first hide the row if the number of children for that row
        // is not set or if it is 0
        //first configure the child age cells
        var numChildren = this.NumChildrenArray[i - 1];
        if(!numChildren || numChildren == null || numChildren == 0)
        {
            this.tblAges.rows[ageHeadRowIndex].style.display = "none";
            this.tblAges.rows[ageRowIndex].style.display = "none";
            
        }
        else
        {
            this.tblAges.rows[ageHeadRowIndex].style.display = "";
            this.tblAges.rows[ageRowIndex].style.display = "";
            rowsAreVisible = true;
        }
        
        // next we need to show or hide the individual cells in the table
        for(var j = 1; j <= this.MaxChildren; j++)
        {
            if(j > numChildren)
            {
                this.tblAges.rows[ageHeadRowIndex].cells[j].style.display = "none";
                this.tblAges.rows[ageRowIndex].cells[j].style.display = "none";
                this.nrsChildAgeArrayTable[i - 1][j - 1].SelectedValue = "";
                this.nrsChildAgeArrayTable[i - 1][j - 1].ddlNumber.options[0].selected = true;
                this.ChildAgeArrayTable[i - 1][j - 1] = null;
            }
            else
            {
                this.tblAges.rows[ageHeadRowIndex].cells[j].style.display = "";
                this.tblAges.rows[ageRowIndex].cells[j].style.display = "";
            }
        }
  
        // now show and hide rows in the people table
        if(i > this.NumRooms)
        {
            this.tblPeople.rows[i].style.display = "none";
            this.tblAges.rows[ageHeadRowIndex].style.display = "none";
            this.tblAges.rows[ageRowIndex].style.display = "none";
            
            this.nrsAdultsArray[i - 1].ddlNumber.options[0].selected = true;
            this.nrsInfantsArray[i - 1].ddlNumber.options[0].selected = true;
            this.nrsChildrenArray[i - 1].ddlNumber.options[0].selected = true;
            
            this.nrsAdultsArray[i - 1].SelectedValue = 0;
            this.nrsInfantsArray[i - 1].SelectedValue = 0;
            this.nrsChildrenArray[i - 1].SelectedValue = 0;
            
            this.NumAdultsArray[i - 1] = 0;
            this.NumChildrenArray[i - 1] = 0;
            this.NumInfantsArray[i - 1] = 0;
        }
        else
        {
            this.tblPeople.rows[i].style.display = "";
            
            if(this.NumChildrenArray[i - 1] > 0)
            {
                this.tblAges.rows[ageHeadRowIndex].style.display = "";
                this.tblAges.rows[ageRowIndex].style.display = "";
            }
        }
        
        ageHeadRowIndex += 2;
        ageRowIndex += 2;
    }
    
    this.divAges.style.display = (rowsAreVisible == true) ? "block" : "none";
}

function PersonSelect_configureArraysAndTables()
{
    for(var i = 0; i < this.MaxRooms; i++)
    {
        this.ChildAgeArrayTable[i] = new Array();
        this.nrsChildAgeArrayTable[i] = new Array();
        for(var j = 0; j < this.MaxChildren; j++)
        {
            this.ChildAgeArrayTable[i][j] = null;
        }
        
        this.NumAdultsArray[i] = 0;
        this.NumChildrenArray[i] = 0;
        this.NumInfantsArray[i] = 0;
    }
}

function PersonSelect_EnableInfants(enable)
{
    var display = (enable == true) ? "" : "none";
    for(var i = 0; i < this.tblPeople.rows.length; i++)
    {
        this.tblPeople.rows[i].cells[4].style.display = display;
    }
    
}

//Event Handlers
function PersonSelect_nrsRooms_onChange(obj)
{
    obj.PersonSelect.NumRooms = obj.SelectedValue;
    obj.PersonSelect.configureControls();
}

function PersonSelect_nrsAdults_onChange(obj)
{
    obj.PersonSelect.NumAdultsArray[obj.Index] = obj.SelectedValue;
}

function PersonSelect_nrsChildren_onChange(obj)
{
    obj.PersonSelect.NumChildrenArray[obj.Index] = obj.SelectedValue;
    obj.PersonSelect.configureControls();
}


function PersonSelect_nrsInfants_onChange(obj)
{
    obj.PersonSelect.NumInfantsArray[obj.Index] = obj.SelectedValue;
}

function PersonSelect_nrsAge_onChange(obj)
{
    obj.PersonSelect.ChildAgeArrayTable[obj.Index1][obj.Index2] = obj.SelectedValue;
}