if (typeof MBD === "undefined")
{
	var MBD = {};
}

MBD.VehicleCompare = function()
{
	var XML_URL = "xml-vehicle-compare.aspx";

	var m_elForm = null;
	var m_arVehicleIDs = [];
	var m_arCheckboxes = [];
	var m_arCompareNumContainers = [];
	var m_public = null;
	
	var m_fnEnableCheckboxes = function()
	{
		var i;
		
		for (i = 0; i < m_arCheckboxes.length; i++)
		{
			m_arCheckboxes[i].disabled = false;
		}
	}
	
	var m_fnDisableCheckboxes = function()
	{
		var i;
		
		for (i = 0; i < m_arCheckboxes.length; i++)
		{
			if (!m_arCheckboxes[i].checked)
			{
				m_arCheckboxes[i].disabled = true;
			}
		}
	}
	
	var m_fnUpdateTotalDisplay = function()
	{
		var i;
		
		for (i = 0; i < m_arCompareNumContainers.length; i++)
		{
			m_arCompareNumContainers[i].firstChild.nodeValue = m_public.total;
		}
	}
	
	var m_fnUpdateCheckboxes = function()
	{
		var elCheckbox;
		var elRow;
		var i;
		var j;
		
		for (i = 0; i < m_arCheckboxes.length; i++)
		{
			elCheckbox = m_arCheckboxes[i];
			elCheckbox.checked = false;
			
			elRow = elCheckbox.parentNode;
			
			while (elRow)
			{
				if (elRow.nodeName.toLowerCase() === "tr")
				{
					break;
				}
				
				elRow = elRow.parentNode;
			}
			
			removeClassName(elRow, "in-comparison");
			
			for (j = 0; j < m_arVehicleIDs.length; j++)
			{
				if (elCheckbox.value === m_arVehicleIDs[j])
				{
					elCheckbox.checked = true;
					addClassName(elRow, "in-comparison");
				}
			}
		}
	}
	
	var m_fnUpdatePage = function()
	{
		m_fnUpdateTotalDisplay();
		m_fnUpdateCheckboxes();
	}

	return {
	
		MAX : 0,
		total : 0,
	
		init : function()
		{
			var arEls;
			var el;
			var i;
			
			m_public = this;
			
			// find the compare form
			arEls = document.getElementsByTagName("form");
			
			for (i = 0; i < arEls.length; i++)
			{
				el = arEls[i];
				
				if (el.className === "compare")
				{
					m_elForm = el;
					
					m_elForm.onsubmit = function()
					{
						document.location = this.action;
						return false;
					}
				}
			}
			
			if (m_elForm == null)
			{
				return;
			}
			
			// find the checkboxes
			arEls = m_elForm.getElementsByTagName("input");
			
			for (i = 0; i < arEls.length; i++)
			{
				el = arEls[i];
				
				if (el.type === "checkbox")
				{
					el.onclick = function()
					{
						MBD.VehicleCompare.toggleVehicle(this.value, this.checked);
					}
					
					if (el.checked)
					{
						m_arVehicleIDs.push(el.value);
					}
				
					m_arCheckboxes.push(el);
				}
			}
			
			// find the compare total elements
			arEls = m_elForm.getElementsByTagName("span");
			
			for (i = 0; i < arEls.length; i++)
			{
				el = arEls[i];
				
				if (el.className === "compare-num")
				{
					m_arCompareNumContainers.push(el);
				}
			}
			
			// adjust the clear links
			arEls = m_elForm.getElementsByTagName("a");
			
			for (i = 0; i < arEls.length; i++)
			{
				el = arEls[i];
				
				if (el.className === "compare-clear")
				{
					el.onclick = function()
					{
						MBD.VehicleCompare.removeVehicle("all");
						return false;
					}
				}
			}
			
			if (this.total == this.MAX)
			{
				m_fnDisableCheckboxes();
			}
		},
		
		toggleVehicle : function(strID, bAdd)
		{
			if (bAdd)
			{
				return this.addVehicle(strID);
			}
			else
			{
				return this.removeVehicle(strID);
			}
		},
		
		addVehicle : function(strID)
		{
			if (this.total == this.MAX)
			{
				return false;
			}
			
			this.total++;
			m_arVehicleIDs.push(strID);
			
			execute_http_request(XML_URL + "?action=add&ids=" + strID + "&rnd=" + Math.random(), null);
			
			if (this.total == this.MAX)
			{
				m_fnDisableCheckboxes();
			}
			
			m_fnUpdatePage();
			
			return true;
		},
		
		removeVehicle : function(strID)
		{
			var i;
		
			if (this.total == 0)
			{
				return false;
			}
			
			if (strID === "all")
			{
				this.total = 0;
				m_arVehicleIDs = [];
			}
			else
			{
				this.total--;
			}
			
			for (i = 0; i < m_arVehicleIDs.length; i++)
			{
				if (m_arVehicleIDs[i] === strID)
				{
					m_arVehicleIDs.splice(i, 1);
					break;
				}
			}
			
			execute_http_request(XML_URL + "?action=remove&ids=" + strID + "&rnd=" + Math.random(), null);
			
			if (this.total < this.MAX)
			{
				m_fnEnableCheckboxes();
			}
			
			m_fnUpdatePage();
			
			return true;
		}
	
	}
}();