var occupancyUi = {

	_roomCount : null,
	_adults : null,
	_children : null,
	_obj  : null,
	_params : null,
	_maxAdults : null,
	_maxOcc : null,
	
	init: function (obj, roomCount)
	{
		this._maxOcc = 4; //max occupants in a room
		this._obj = obj;
		this._roomCount = roomCount;
		this._maxAdults = new Array(this._roomCount); //holds max adults for each line ( max adult varies based on number of childers)
		this._adults = new Array(this._roomCount);
		this._children = new Array(this._roomCount);
		if(!this._parseQs())
		{
			for(var idx = 0; idx < this._roomCount; ++idx)
			{
				this._adults[idx] = 1;
				this._children[idx] = 0;
				this._maxAdults[idx] = this._maxOcc;
			}
			for(var idx = 0; idx < 5; ++idx)
			{
				this.updateAdultCount(idx, 2);
			}
		}
		else
		{
			for(var idx = 0; idx < this._roomCount; ++idx)
			{
				this._adults[idx] = this._getAdultCount(idx);
				this._children[idx] = this._getChildCount(idx);
				this._maxAdults[idx] = this._maxOcc - this._getChildCount(idx);
			}
		}
		this._render();
	},
	
	updateIsTwin : function(idx, val)
	{
		this._setIsTwin(idx, val);
	},
	
	updateRoomCount : function(val)
	{
		this._roomCount = val;
		this._render();
		
	},
	
	updateAdultCount : function(idx, val)
	{
		this._setAdultCount(idx, val);
		this._render();
	},
	
	updateChildCount : function(idx, val)
	{
		this._setMaxAdultCount(idx, this._maxOcc - val);
		this._setChildCount(idx, val);
		this._render();
	},
	
	updateChildAge : function(idx, jdx, val)
	{
		this._setChildAge(idx, jdx, val);
		this._render();
	},
	_parseQs : function()
	{
		this._params = new Object();
		var qs=location.search.substring(1,location.search.length);
		if (qs.length == 0) return false;
		
		// Turn <plus> back to <space>
		// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
		qs = qs.replace(/\+/g, ' ')
		var args = qs.split('&') // parse out name/value pairs separated via &
		
		//convert '%3a' to ':'
		qs = qs.replace(/%3a/, ':')
		
		// split out each name=value pair
		for (var i=0;i < args.length;i++) {
			var value;
			var pair = args[i].split('=')
			var name = unescape(pair[0])
		
			if (pair.length == 2)
				value = unescape(pair[1])
			else
				value = name
			
			this._params[name] = value
		}
		if (this._params["roomCount"])
    		this._roomCount = this._params["roomCount"];
		return true;
		
	},
	
	_getAdultCount: function (idx)
	{
		return this._params["adultCount:" + idx];
	},
	
	_setAdultCount : function(idx, val)
	{
		this._params["adultCount:" + idx] = val;
	},
	
	_getIsTwin: function (idx)
	{
		return this._params["doubleTwin:" + idx];
	},
	
	_setIsTwin: function (idx, val)
	{
		this._params["doubleTwin:" + idx] = val;
	},
	
	_getChildCount: function (idx)
	{
		return this._params["childCount:" + idx];
	},
	
	_setChildCount : function(idx, val)
	{
		this._params["childCount:" + idx] = val;
	},
	
	_getMaxAdultCount: function (idx)
	{
		return this._maxAdults[idx];
		//return this._params["maxAdultCount:" + idx];
	},
	
	_setMaxAdultCount : function(idx, val)
	{
		this._maxAdults[idx] = val;
		//this._params["maxAdultCount:" + idx] = val;
	},
	
	_getChildAge: function (idx, jdx)
	{
		return this._params["age:" + idx + ":" + jdx];
	},
	
	_setChildAge: function (idx, jdx, val)
	{
		this._params["age:" + idx + ":" + jdx] = val;
	},
	
	_render: function ()
	{
		
		var s = "";
		for(var idx = 0; idx < this._roomCount; ++idx)
		{
			
			s+="<div class=\"occupancy\">";
			if(this._roomCount > 1) s+="<h3> Room " + (idx + 1) + "</h3>";
			s+="<dl><dt>";
			s+="<label for=\"adultCount:" + idx + "\">Adults</label></dt><dd>";
			s+="<select onchange=\"occupancyUi.updateAdultCount( " + idx + ", this.options[this.selectedIndex].value);\" name=\"adultCount:" + idx + "\" id=\"adultCount:" + idx + "\">";
			//get adult count
			var maxAdults;
			if(isNaN(this._maxAdults[idx]))
				maxAdults = this._maxOcc + 1;
			else
				maxAdults = this._maxAdults[idx] + 1;
			for(var i = 1; i < maxAdults; ++i)
			{
				if(i == this._getAdultCount(idx))
					s+="<option selected=\"selected\" value=\"" + i + "\">" + i + "</option>";
				else
					s+="<option value=\"" + i + "\">" + i + "</option>";
			}
			s+="</select></dd></dl><dl><dt>";
			s+="<label for=\"childCount:" + idx + "\">Children</label></dt><dd>";
			s+="<select onchange=\"occupancyUi.updateChildCount( " + idx + ", this.options[this.selectedIndex].value);\"  name=\"childCount:" + idx + "\" id=\"childCount:" + idx + "\">";
			for(var i = 0; i < 3; ++i)
			{
				if(i == this._getChildCount(idx))
					s+="<option selected=\"selected\" value=\"" + i + "\">" + i + "</option>";
				else
					s+="<option value=\"" + i + "\">" + i + "</option>";
			}
			s+="</select></dd></dl>";
			for(var jdx = 0; jdx < this._getChildCount(idx); ++jdx)
			{
				s+="<dl><dt><label for=\"age:" + idx + ":" + jdx + "\">Age</label></dt><dd>";
				s+="<select onchange=\"occupancyUi.updateChildAge( " + idx + "," + jdx + " , this.options[this.selectedIndex].value);\" name=\"age:" + idx + ":" + jdx + "\">";
				for(var i = 1; i < 18; ++i)
				{
					if(i == this._getChildAge(idx, jdx))
						s+="<option selected=\"selected\" value=\"" + i + "\">" + i + "</option>";
					else
						s+="<option value=\"" + i + "\">" + i + "</option>";
				}
				s+="</select></dd></dl>";
			}
			
			var adultCount = parseInt(this._getAdultCount(idx));
			var childCount = !isNaN(this._getChildCount(idx)) ? parseInt(this._getChildCount(idx)) : 0;
			if(adultCount + childCount == 2)
			{ 
				var isTwin = this._getIsTwin(idx) == "t";
				s+="<dl class=\"doubletwin\"><dt><input onchange=\"occupancyUi.updateIsTwin(" + idx + ", 'f' );\" type=\"radio\" value=\"d\" ";
				if(!isTwin)
					s+="checked=\"checked\" "; 
				s+="name=\"doubleTwin:" + idx + "\" id=\"double:" + idx + "\"></dt><dd><label for=\"double:" + idx + "\">Double</label></dd>";
				s+="<dt><input onchange=\"occupancyUi.updateIsTwin(" + idx + ", 't' );\"  type=\"radio\" ";
				if(isTwin)
					s+="checked=\"checked\" ";
				s+="value=\"t\" name=\"doubleTwin:" + idx + "\" id=\"twin:" + idx + "\"></dt><dd><label for=\"twin:" + idx + "\">Twin</label></dd></dl>";
			}
			s+=" <div class=\"clear\"></div></div>";
		}
		this._obj.innerHTML = s;
	}
}

function initRooms() {
	var rc = document.getElementById("roomCountSelect");
	var div = document.getElementById("rooms");
	occupancyUi.init(div, rc.options[rc.selectedIndex].value);
	
	rc.onchange = function()
	{
		occupancyUi.updateRoomCount(rc.options[rc.selectedIndex].value);
	}
}

onload = function() {
	initRooms();
}

function updateArrival(obj) {
	if(!bajax.supports())
		return false;
		
	//check for http
	var myargs = new Array();		
	myargs['ticks'] = obj.options[obj.options.selectedIndex].value;
    var func = function(content)
    {
        var selects = document.getElementsByTagName('select');
        for (var j = 0; j < selects.length; j++)
        {
            if (selects[j].id == 'arrival')
            {
	            var selectOptions = selects[j].options;
	            selectOptions.length = 0;
	            eval(content);
	            for(var i = 0; i < obj.options.length; ++i)
	            {
	    	        var text = obj.options[i].text;
	   	 	        var value = obj.options[i].value;
	    	        selectOptions[i] = new Option(text, value);
	            }
	            break;
            }
        }
    }
	bajax.call(_appPath + '/arrival.html', func, myargs);
	return true;
}

function updateDeparture(obj) {
	if(!bajax.supports())
		return false;
		
	var myargs = new Array();
	var selects = document.getElementsByTagName('select');
	for (var i = 0; i < selects.length; i++)
	{
	    if (selects[i].id == 'arrival')
	        myargs['arrival'] = selects[i].options[selects[i].options.selectedIndex].value;
	    else if (selects[i].id == 'numberOfNights')
	        myargs['numberOfNights'] = selects[i].options[selects[i].options.selectedIndex].value;
	    // could add a break check if numberOfNights and arrival have been set...
	}
	var func = function(content)
	{
		document.getElementById('departureDiv').innerHTML = content;
	}
	bajax.call(_appPath + '/departure.html', func, myargs);
	return true
}
