//Note:
//We are using core.js which is a javascript object that 
//contains common functions and handles browser differences.

var selectLicenses = 
{
	//begin init
	init: function()
	{
		//Add an event listener to the occupation dropdown box
		var occupationDropdown = document.getElementById("occupation");
		Core.addEventListener(occupationDropdown,"change",selectLicenses.occupationChangeListener)
		
		//Add an event listener for the form submission
		var licenseForm = document.getElementById("select_licenses_form");
		Core.addEventListener(licenseForm,"submit", selectLicenses.formSubmitListener);
		
		selectLicenses.formLoadListener();
	},
	//end init
	
	//begin occupationChangeListener
	occupationChangeListener: function()
	{
		//Submit the form so that the appropriate licenses can be loaded
		this.form.submit();
	},
	//end occupationChangeListener
	
	//begin formLoadListener
	formLoadListener: function()
	{
		var occupationDropdown = document.getElementById("occupation");
		
		//Check the appropriate checkbox based on the selected occupation if there is a matching license.
		switch(occupationDropdown.value) //switch the occupation or profession id
		{
			case "1":
			  document.getElementById("license4").checked = true;
			  break;    
			case "2":
			  document.getElementById("license9").checked = true;
			  break;
			case "3":
			  document.getElementById("license4").checked = true;
			  break;
			case "4":
			  document.getElementById("license11").checked = true;
			  break;
			case "7":
			  document.getElementById("license10").checked = true;
			  break;
			case "8":
			  document.getElementById("license16").checked = true;
			  break;
			case "9":
			  document.getElementById("license17").checked = true;
			  break;
			case "10":
			  document.getElementById("license17").checked = true;
			  break;
		}
	},
	//end formLoadListener
	
	//begin formSubmitListener
	formSubmitListener: function(event)
	{
		var errorMessage = document.getElementById("errormessage");
		errorMessage.innerHTML = "";
		
		//Check if the user has selected an occupation.
		var occupation = document.getElementById("occupation");
		if (occupation != null) 
			if (occupation.value == "")
			{
				errorMessage.innerHTML = "Please select an occupation<br/>";
				occupation.style.backgroundColor = "#FFCCCC";
				Core.preventDefault(event);
			}	
		
		//Check if the user has selected at least one license.
		var licenseList = document.select_licenses_form.license;
		
		if(licenseList != null)
		{
			var licenseCount = 0;
			if(licenseList.length != null) //more than one license
			{
				for(var i = 0; i < licenseList.length; i++)
				{
					if(licenseList[i].checked == true)
						licenseCount++;
				}
			}		
			else //only one license
			{
				if(licenseList.checked == true)	
					licenseCount++;
			}
			
			if(licenseCount < 1)
			{
					errorMessage.innerHTML += "Please select one or more licenses";
					Core.preventDefault(event);
			}
		}//licenseList != null
	}
	//end formSubmitListener
};

Core.start(selectLicenses);
