	var defaultKeyword 							= "Keyword";
	var defaultLocation 						= "Location";

var RHJobSearchObject = function() {

	var USCA_URL1 = "/JobQuery?location=";
	var USCA_URL2 = "&distance=&daysold=&emptypes=&keywords=";
	var USCA_URL3 = "&jobtypes=&source=";

	var DELIMITER = "|";

	var specializationValidationMsg = "Please select a Specialization";
	var countryValidationMsg		= "Please select a Country.";

	var xmlDoc = null;
	var countryOptionItems;
	var specialityOptionItems;
	
	var loadXmlDoc = function()
	{
	  var xmlFile = "/External_Sites/content/Shared/Scripts/specialization_mapping.xml";

		showHideCountry();
		populateFields();
	
		if(window.XMLHttpRequest&&!window.ActiveXObject)
		{    
			var Gz=new XMLHttpRequest();
			Gz.open('GET',xmlFile,false);
			Gz.send(null);xmlDoc=Gz.responseXML;
		}
		else
		{    
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async=false;
			xmlDoc.load(xmlFile);
		}
		loadCountryOptions();

	}();
	
	function populateFields()
	{
		document.jobSearchFrm.keywords.value = defaultKeyword;
		document.jobSearchFrm.location.value = defaultLocation;
	}
	
	function loadCountryOptions()
	{	
	  countryOptionItems = xmlDoc.getElementsByTagName("specializations");
	  var x = 0; //options index
	  
	  for (var i=0; i<countryOptionItems.length; i++)
	  {
		  document.jobSearchFrm.countryCmbo.options[i] = new Option(countryOptionItems[x++].getAttribute("country"),countryOptionItems[i].getAttribute("countryCode"));
	  }

	  if(USCA_JobSearch != null) { document.jobSearchFrm.countryCmbo.selectedIndex = findCountryIndex(USCA_JobSearch); }
	  enableDisableSpeciality();
	}

	function enableDisableSpeciality()
	{
		var selIndex = document.jobSearchFrm.countryCmbo.selectedIndex;
		if(selIndex == -1) selIndex = 0;

		loadSpecialityOptions(selIndex);

		if (selIndex > 0)
		{
			document.jobSearchFrm.specialityCmbo.disabled=false;
			switchLocationsHintText();
		}
		else
		{
			document.jobSearchFrm.specialityCmbo.disabled=true;
		}

	}
	
	function findCountryIndex(countryStr)
	{
		for(var i=0; i<document.jobSearchFrm.countryCmbo.options.length; i++)
		{
			if(document.jobSearchFrm.countryCmbo.options[i].value == countryStr)
			{
				return document.jobSearchFrm.countryCmbo.options[i].index;
			}
		}
	}

	function loadSpecialityOptions(countryIndex)
	{
	  specialityOptionItems = xmlDoc.getElementsByTagName("specializations")[countryIndex].getElementsByTagName("speciality");

	  //clears options list
	  document.jobSearchFrm.specialityCmbo.options.length = 0;

	  var value = "";
	  for (var i=0; i<specialityOptionItems.length; i++)
	  {
		//store lobDomain & job search URL
		var jsurl = specialityOptionItems[i].getAttribute("jobSearchURL");
		if(jsurl != "null")
		{
			value = specialityOptionItems[i].getAttribute("lobDomain") + DELIMITER + jsurl;
			document.jobSearchFrm.specialityCmbo.options[i] = new Option(specialityOptionItems[i].getAttribute("lob"),value);
		}
	  }
	}

	function switchLocationsHintText()
	{
		if(document.jobSearchFrm.countryCmbo.value == "US" || document.jobSearchFrm.countryCmbo.value == "CA")
		{
			document.getElementById("USCanadaLocationLabel").style.display = "block";
			document.getElementById("defaultLocationLabel").style.display = "none";
		}
		else
		{
			document.getElementById("USCanadaLocationLabel").style.display = "none";
			document.getElementById("defaultLocationLabel").style.display = "block";
		}
	}

	function showHideCountry()
	{
		if (USCA_JobSearch != null)
		{
			document.getElementById("hideCountryCmbo").style.display = "none";
			document.jobSearchFrm.countryCmbo.selectedIndex = 19;
		}
		else
		{
			if(navigator.appName == "Microsoft Internet Explorer")
			{
			document.getElementById("hideCountryCmbo").style.display = "block";
			}
			else
			{
			document.getElementById("hideCountryCmbo").style.display = "table-row";
			}
		}
	}
	
	return {
		/*Public API*/

		enableDisableSpeciality: function()
		{
			enableDisableSpeciality();
		},

		validateJobSearchForm: function()
		{
			var specialityCmbo = document.jobSearchFrm.specialityCmbo;
			if(document.jobSearchFrm.countryCmbo.selectedIndex == 0)
			{
				alert(countryValidationMsg);
				document.jobSearchFrm.countryCmbo.focus();
				return;
			}
			if(specialityCmbo.length > 1 && specialityCmbo.selectedIndex == 0)
			{
				alert(specializationValidationMsg);
				specialityCmbo.focus();
			}
			else
			{
				var splitValues = specialityCmbo.options[specialityCmbo.selectedIndex].value.split(DELIMITER)
				var url = splitValues[0];
				var cleanedKeyword = "";
				var cleanedLocation = "";

				//clear default values from keywords & location fields
				if(document.jobSearchFrm.keywords.value != defaultKeyword)
				{
					cleanedKeyword = document.jobSearchFrm.keywords.value;
				}
				if(document.jobSearchFrm.location.value != defaultLocation)
				{
					cleanedLocation = document.jobSearchFrm.location.value;
				}						

				if(splitValues[1] != "")
				{
					url += splitValues[1] + cleanedKeyword + "%20" + cleanedLocation;
				}
				else
				{
					//assumes empty jobSearchURL attribute follows US & Canada URL pattern
					url += USCA_URL1 + cleanedLocation + USCA_URL2 +  cleanedKeyword + USCA_URL3 + TRKSOURCE;
				}
				window.open(url);
			}
		},

		doFocus: function(field,strValue)
		{
			field.value= (field.value==strValue) ? '' : field.value	
		},

		doBlur:	function(field,strValue)
		{
			field.value= (field.value=='') ? strValue : field.value
		}
		
	};

}();
