	
	var defaultKeyword 							= "Keyword";
	var defaultLocation 						= "Location";	
	
var RHJobSearchObject = function() {

	var lang 						= "en_US";
	var specializationValidationMsg = "Please select a Specialization";
	var countryValidationMsg		= "Please select a Country";
	var provinceValidationMsg 		= "Please select a Province";

	var USCA_URL1 = "/JobQuery?location=";
	var USCA_URL2 = "&distance=&daysold=&emptypes=&keywords=";
	var USCA_URL3 = "&jobtypes=&source=";

	var DELIMITER = "|";
	var relativePath = "/External_Sites/content/Shared/Scripts/";
		
	var specializationXMLDoc = null;
	var provinceXMLDoc = null;

	var loadXmlDoc = function()
	{				
		//if variable "langCd_JobSearch" exists
		if(typeof langCd_JobSearch != 'undefined') 
		{
			lang = langCd_JobSearch;
		}
		var specializationXMLFile = loadSpecializationXML();
		var provinceXMLFile = relativePath + "regionsList.xml";
		
		if(window.XMLHttpRequest&&!window.ActiveXObject)
		{    
			var Gz=new XMLHttpRequest();
			Gz.open('GET',specializationXMLFile,false);
			Gz.send(null);specializationXMLDoc=Gz.responseXML;
			
			Gz.open('GET',provinceXMLFile,false);
			Gz.send(null);provinceXMLDoc=Gz.responseXML;
		}
		else
		{    
			specializationXMLDoc=new ActiveXObject("Microsoft.XMLDOM");
			specializationXMLDoc.async=false;
			specializationXMLDoc.load(specializationXMLFile);
			
			provinceXMLDoc=new ActiveXObject("Microsoft.XMLDOM");
			provinceXMLDoc.async=false;
			provinceXMLDoc.load(provinceXMLFile);
		}
 	  
	  setLanguage(lang);
	  populateFields();
		showHideCountry();
		
		//if province comboBox exists
		if(typeof document.jobSearchFrm.provinceCmbo != 'undefined') 
		{
			loadProvinceOptions();
		}		

	}();
	
	function setLanguage(langCd)
	{
		if(langCd == "fr_CA")
		{
			defaultKeyword				= "Mot-clé";
			defaultLocation				= "Lieu";	
			specializationValidationMsg	= "S'il vous plaît choisir une spécialisation";
			countryValidationMsg		= "S'il vous plaît choisir un Pays";
			provinceValidationMsg		= "S'il vous plaît choisir une province";
		}
	}
	
	function loadSpecializationXML()
	{
		//default file
		var file = "specialization_mapping.xml"; 
	
		if(lang == "fr_CA")
		{
			file = "specialization_mapping_fr_CA.xml";
		} 
		else if (lang == "en_CA")
		{
			file = "specialization_mapping_en_CA.xml"
		}
		
		return relativePath + file;
	}
	
	function populateFields()
	{
		document.jobSearchFrm.keywords.value = defaultKeyword;
		document.jobSearchFrm.location.value = defaultLocation;
	}
	
	function loadCountryOptions()
	{	
	  var optionItems = specializationXMLDoc.getElementsByTagName("specializations");
	  var x = 0; //options index
	  	  
	  for (var i=0; i<optionItems.length; i++)
	  {
	  	//don't load Canada-Quebec
	  	if(optionItems[i].getAttribute("countryCode") != "CA-QC")
	  	{
				document.jobSearchFrm.countryCmbo.options[x++] = new Option(optionItems[i].getAttribute("country"),optionItems[i].getAttribute("countryCode"));
		  }		  
	  }
	  
	}

	function loadProvinceOptions()
	{			
	  var optionItems = getRegionSet(lang);
	  var x = 0; //options index
  
	  for (var i=0; i<optionItems.length; i++)
	  {
		  document.jobSearchFrm.provinceCmbo.options[i] = new Option(optionItems[x++].getAttribute("name"),optionItems[i].getAttribute("code"));
	  }

		showHideFormField("provinceCmbo");		
	}

	function getRegionSet(langCode)
	{
		//find region set
		var optionItems = provinceXMLDoc.getElementsByTagName("regions");
		
		for(var i=0; i<optionItems.length; i++)
		{		
			if(optionItems[i].getAttribute("languageCode") == langCode)
			{
				return optionItems[i].getElementsByTagName("region");				
			}
		}
	}
	
	function enableDisableSpeciality()
	{
		var selIndex = document.jobSearchFrm.countryCmbo.selectedIndex;
		if(selIndex == -1) selIndex = 0;		
		var selectCountry = document.jobSearchFrm.countryCmbo.options[selIndex].value;
		loadSpecialityOptions(selectCountry);
		
		if (selIndex > 0)
		{				
			document.jobSearchFrm.specialityCmbo.disabled=false;
			switchLocationsHintText();
		}
		else
		{
			document.jobSearchFrm.specialityCmbo.disabled=true;
		}
		
	}
	
	function enableDisableQuebecFields()
	{
		var selIndex = document.jobSearchFrm.provinceCmbo.selectedIndex;		
		var QCIndex = findProvinceIndex("QC");
		if(selIndex == -1) selIndex = 0;
		var selectedCountryCd = "CA"; //default
		
		//show keywords & location fields to all provinces except Quebec
		if(selIndex > 0 && selIndex != QCIndex)
		{
			showHideFormField("hideKeywords");
			showHideFormField("hideLocation");
		}
		else
		{
			selectedCountryCd = "CA-QC";
			document.getElementById("hideKeywords").style.display = "none";
			document.getElementById("hideLocation").style.display = "none";			
		}
		loadSpecialityOptions(selectedCountryCd);

	}
	
	function loadSpecialityOptions(selectedCountry)
	{ 
	  var specialityOptionItems = null
		
		//find country xml index & load specializations
		var countriesList = specializationXMLDoc.getElementsByTagName("specializations");
		for(var i=0; i<countriesList.length; i++)
		{
			if(countriesList[i].getAttribute("countryCode") == selectedCountry)
			{
				specialityOptionItems = specializationXMLDoc.getElementsByTagName("specializations")[i].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 findCountryIndex(countryCd)
	{
		for(var i=0; i<document.jobSearchFrm.countryCmbo.options.length; i++)
		{
			if(document.jobSearchFrm.countryCmbo.options[i].value == countryCd)
			{
				return document.jobSearchFrm.countryCmbo.options[i].index;
			}
		}
	}

	function findProvinceIndex(provinceCd)
	{
		for(var i=0; i<document.jobSearchFrm.provinceCmbo.options.length; i++)
		{
			if(document.jobSearchFrm.provinceCmbo.options[i].value == provinceCd)
			{
				return document.jobSearchFrm.provinceCmbo.options[i].index;
			}
		}
	}
	
	function showHideCountry()
	{
		loadCountryOptions();
		document.jobSearchFrm.countryCmbo.selectedIndex = findCountryIndex(USCA_JobSearch);
		enableDisableSpeciality();

		if (USCA_JobSearch != null)
		{
			document.getElementById("hideCountryCmbo").style.display = "none";
		}
		else
		{
			showHideFormField("hideCountryCmbo");
		}
	}
	
	function showHideFormField(field)
	{
		if(navigator.appName == "Microsoft Internet Explorer")
		{
		document.getElementById(field).style.display = "block";
		}
		else
		{
		document.getElementById(field).style.display = "table-row";
		}
	}

	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";
		}
	}
	
	return {
		/*Public API*/

		enableDisableSpeciality: function()
		{
			enableDisableSpeciality();
		},
		
		enableDisableInputFields: function()
		{
			enableDisableQuebecFields();
		},
		
		validateJobSearchForm: function()
		{
			var specialityCmbo = document.jobSearchFrm.specialityCmbo;
			if(document.jobSearchFrm.countryCmbo.selectedIndex == 0)
			{
				alert(countryValidationMsg);
				document.jobSearchFrm.countryCmbo.focus();
				return;
			}
			//if province comboBox exists
			if(typeof document.jobSearchFrm.provinceCmbo != 'undefined') 
			{
				if(document.jobSearchFrm.provinceCmbo.selectedIndex == 0)
				{
					alert(provinceValidationMsg);
					document.jobSearchFrm.provinceCmbo.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
		}
		
	};

}();
