onload = function() {

 addRoom("room1");

}

// Add new room option
function addRoom(roomid) {

 var adults = new Array(); 
 var children = new Array();
 var infants = new Array();
 ca = new Array();
 ia = new Array();

  window.alert = function(e) { 
 	if (typeof(console) == 'object') 
 		console.warn(e); 
 	else 
 		return false;
 	}
  if (document.body.__defineGetter__) {

       if (HTMLElement) {

              var element = HTMLElement.prototype;

              if (element.__defineGetter__) {

                     element.__defineGetter__("outerHTML",

                           function () {

                                  var parent = document.createElement('div');

                                  var el = document.createElement('div');

                                  el.appendChild(this);

                                  var shtml = el.innerHTML;

                                  parent.appendChild(this);

                                  return shtml;

                           }

                     );

              }

       }

}

 // Replace text
 var roomnum = roomid.replace("room","");

 // Delete out any elements outside the room num range
 for(var room = 1; room <= 3; room = room+1) {
  if(document.getElementById(room+"_a") != undefined) {
   var oldChildAdults = document.getElementById(room+"_a");
   oldChildAdults.parentNode.removeChild(oldChildAdults);
  }
  if(document.getElementById(room+"_c") != undefined) {
   var oldChildChildren = document.getElementById(room+"_c");
   oldChildChildren.parentNode.removeChild(oldChildChildren);
  }
  if(document.getElementById(room+"_i") != undefined) {
   var oldChildInfants = document.getElementById(room+"_i");
   oldChildInfants.parentNode.removeChild(oldChildInfants);
  }
  document.getElementById("room"+room).innerHTML = "";
  document.getElementById("p_room"+room).style.display="none";
 }

 for(var room = 1; room <= roomnum; room = room+1) {
 
  var x = document.getElementById("room"+room); 

  // Create select element for adults
  var roomselect_adults = document.createElement("select");

  roomselect_adults.id = room+"_a";
  //x.appendChild(roomselect_adults);
  x.innerHTML = x.innerHTML + '<span class="ppl"><span>Adults</span><br />' + roomselect_adults.outerHTML + '</span>';
  alert(roomselect_adults.outerHTML);
  // Create select element for children
  var roomselect_children = document.createElement("select");
  roomselect_children.id = room+"_c";
  //x.appendChild(roomselect_children);
  x.innerHTML = x.innerHTML + '<span class="ppl"><span>Children</span><br />' + roomselect_children.outerHTML + "</span>";
  
  // Create select element for infants
  var roomselect_infants = document.createElement("select");
  roomselect_infants.id = room+"_i";
  //x.appendChild(roomselect_infants);
  x.innerHTML = x.innerHTML + '<span class="ppl"><span>Infants</span><br />' + roomselect_infants.outerHTML + "</span>";
 
  // Add options for adults
  for(var i=1;i<=10;i=i+1) {
   var rsoption1 = document.createElement("option");
   var adultselected = false;
   rsoption1.text = i;
   rsoption1.value = i;
   rsoption1.id = room+"_opta_"+i;
   if(adults[room] != undefined) {
    if(adults[room] == i) {
     var adultselected = true;
    }
    else if(i==2) {
     var adultselected = true;
    }
   }
   else if(i==2) {
    var adultselected = true;
   }
   document.getElementById(room+"_a").options.add(rsoption1);
   if(adultselected) {
    document.getElementById(room+"_opta_"+i).setAttribute("selected","true");
   }
  }
  
  // Add options for children
  for(var i=0;i<=4;i=i+1) {
   var rsoption2 = document.createElement("option");
   var childselected = false;
   rsoption2.text = i;
   rsoption2.value = i;
   rsoption2.id = room+"_optc_"+i;
   if(children[room] != undefined) {
    if(children[room] == i) {
     childselected = true;
     addChildren(room,i);
    }
   }
   document.getElementById(room+"_c").options.add(rsoption2);
   if(childselected) {
    document.getElementById(room+"_optc_"+i).setAttribute("selected","true");
   }
  }
  
  // Add options for infants
  for(var i=0;i<=3;i=i+1) {
   var rsoption3 = document.createElement("option");
   var infantselected = false;
   rsoption3.text = i;
   rsoption3.value = i;
   rsoption3.id = room+"_opti_"+i;
   if(infants[room] != undefined) {
    if(infants[room] == i) {
     infantselected = true;
     addInfants(room,i);
    }
   }
   document.getElementById(room+"_i").options.add(rsoption3);
   if(infantselected) {
    document.getElementById(room+"_opti_"+i).setAttribute("selected","true");
   }
  }
  
  document.getElementById(room+"_c").onchange = function(){ addChildren(this.id.replace("_c",""),this.value); };
  document.getElementById(room+"_i").onchange = function(){ addInfants(this.id.replace("_i",""),this.value); };
  document.getElementById(room+"_i").name = "ifts"+room;
  document.getElementById(room+"_a").name = "adts"+room;
  document.getElementById(room+"_c").name = "chrn"+room;
  
  // if(room > 1) document.getElementById("srchform").style.height="auto";
  
  document.getElementById("p_room"+room).style.display="block";
 
 }
 
 loadTabs();
 
}

// Add children
function addChildren(room,childnum) {
 
 if(document.getElementById("room"+room+"_children") != null) {
 
  var oldChildrenSpan = document.getElementById("room"+room+"_children");
  oldChildrenSpan.parentNode.removeChild(oldChildrenSpan);
 
 }
 
 if(childnum > 0) {
 
  // document.getElementById("srchform").style.height="auto";
 
  var childrenspan = document.createElement("span");
  childrenspan.name = "room"+room+"_children";
  childrenspan.id = "room"+room+"_children";
  childrenspan.innerHTML = "<label>Child ages?</label>";
  
  if(document.getElementById("room"+room+"_infants") != null) {
  
   var infants = document.getElementById("room"+room+"_infants");
   document.getElementById("room"+room).insertBefore(childrenspan,infants);
  
  }
  else {
  
   document.getElementById("room"+room).appendChild(childrenspan);
   
  }

  var childagedropdown = new Array();
  
  for(var i=1;i<=childnum;i=i+1) {
  
   // Create child age dropdown
   childagedropdown[i] = document.createElement("select");
   childagedropdown[i].className = "aged";
   childagedropdown[i].name = "ca"+room+"[]";
   childagedropdown[i].id = "room"+room+i+"_childages";
   document.getElementById("room"+room+"_children").appendChild(childagedropdown[i]);
  
   // Add child age options to dropdown
   for(var ii=2;ii<=16;ii=ii+1) {
  
    var caoption = document.createElement("option");
    var setchildage = false;
    caoption.text = ii;
    caoption.value = ii;
    caoption.id = "cage_"+room+i+ii;
    if(eval(ca[room+"_"+i]) != undefined) {
     if(ca[room+"_"+i] == ii) {
      setchildage = true;
     } 
    }
   
    document.getElementById("room"+room+i+"_childages").options.add(caoption);

    if(setchildage) {
     document.getElementById("cage_"+room+i+ii).setAttribute("selected","true");
    }
	else {
	 if(ii == 8 && !setchildage) {
	  document.getElementById("cage_"+room+i+ii).setAttribute("selected","true");
	 }
	}
  
   }
  
  }
 
 }

}

// Add infants
function addInfants(room,infantnum) {
 
 if(document.getElementById("room"+room+"_infants") != null) {
 
  var oldInfantsSpan = document.getElementById("room"+room+"_infants");
  oldInfantsSpan.parentNode.removeChild(oldInfantsSpan);
 
 }
 
 if(infantnum > 0) {
 
  // document.getElementById("srchform").style.height="auto";
 
  var infantsspan = document.createElement("span");
  infantsspan.name = "room"+room+"_infants";
  infantsspan.id = "room"+room+"_infants";
  infantsspan.innerHTML = "<label>Infant ages?</label>";
  document.getElementById("room"+room).appendChild(infantsspan);

  var infantagedropdown = new Array();
  
  for(var i=1;i<=infantnum;i=i+1) {
  
  // Create infant age dropdown
  infantagedropdown[i] = document.createElement("select");
  infantagedropdown[i].name = "ia"+room+"[]";
  infantagedropdown[i].className = "aged";
  infantagedropdown[i].id = "room"+room+i+"_infantages";
  document.getElementById("room"+room+"_infants").appendChild(infantagedropdown[i]);

  // Add infant age options to dropdown
  for(var ii=0;ii<=1;ii=ii+1) {
  
   var iaoption = document.createElement("option");
   var setinfantage = false;
   iaoption.text = ii;
   iaoption.value = ii;
   iaoption.id = "iage_"+room+i+ii;
   if(eval(ia[room+"_"+i]) != undefined) {
    if(ia[room+"_"+i] == ii) {
     setinfantage = true;
    } 
   }
   document.getElementById("room"+room+i+"_infantages").options.add(iaoption);
   
   if(setinfantage) {
    document.getElementById("iage_"+room+i+ii).setAttribute("selected","true");
   }
  
  }
  
  }
 
 }

}
