
var OfficeLocatorObject = OfficeLocatorObject ? OfficeLocatorObject : function() {	

	var lang 						= "en_US";
	var specializationValidationMsg = "Please select a Specialization";
	var countryValidationMsg		= "Please select a Country";
	var provinceValidationMsg		= "Please select a Province";
	
	var specializationXMLDoc = null;
	var provinceXMLDoc = null;
	
	var relativePath = "/External_Sites/content/Shared/Scripts/";
	var specialityOptionItems;

	var loadXmlDoc = function() 
	{
		//if variable "langCd_OfficeLocator" exists
		if(typeof langCd_OfficeLocator != 'undefined') 
		{
			lang = langCd_OfficeLocator;
		}
		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);
		loadCountryOptions();
  
  		//if province comboBox exists
  		if(typeof document.officeFrm.provinceCmbo != 'undefined') 
  		{
  			loadProvinceOptions();
		}	
		
	}();

	function setLanguage(langCd)
	{
		if(langCd == "fr_CA")
		{		
			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 loadCountryOptions()
	{	
	
		//Hides country menu & submit button and shows speciality menu only, if USCA_OfficeLocator is not null 
		if(USCA_OfficeLocator != null)
		{		
			loadSpecialityOptions(findCountryIndex(USCA_OfficeLocator));

			document.officeFrm.redirectURL.value = specialityOptionItems[0].getAttribute("lobDomain") + specialityOptionItems[0].getAttribute("officeLocatorURL");

			document.getElementById("olCountryCell").style.display = "none";
			document.getElementById("hideablearea").style.display = "block";

		}	
		else
		{	//prepare country combo box
			var countryOptionItems = specializationXMLDoc.getElementsByTagName("specializations");
			var x = 0;
			for (var i=0; i<countryOptionItems.length; i++)
			{
				if(countryOptionItems[i].getAttribute("countryCode") != "CA-QC")
				{
					document.officeFrm.olCountryCmbo.options[x++] = new Option(countryOptionItems[i].getAttribute("country"),countryOptionItems[i].getAttribute("countryCode"));
				}
			}
	  }	  
	}

	function findCountryIndex(countryStr)
	{
		var countries = specializationXMLDoc.getElementsByTagName("specializations");
		var len = countries.length;
		for(var i=0; i<len; i++)
		{		
			if(countries[i].getAttribute("countryCode") == countryStr)
			{
				specialityOptionItems = countries[i].getElementsByTagName("speciality");
			}
		} 
	}

	function loadProvinceOptions()
	{			
		var optionItems = getRegionSet(lang);
		var x = 0; //options index

		for (var i=0; i<optionItems.length; i++)
		{
		  document.officeFrm.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 loadSpecialityOptions(selectedCountry)
	{		 
		//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.officeFrm.officeSpecialityCmbo.options.length = 0;
		//initializes hidden field value
		document.officeFrm.redirectURL.value = null;

		var url = "";
		for (var i=0; i<specialityOptionItems.length; i++)
		{
		var olurl = specialityOptionItems[i].getAttribute("officeLocatorURL");
		if(olurl != "null")
		{
			url = specialityOptionItems[i].getAttribute("lobDomain") + olurl;
			document.officeFrm.officeSpecialityCmbo.options[i] = new Option(specialityOptionItems[i].getAttribute("lob"),url);
			}
		}
	}	

	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 showHideFormField(field)
	{
		if(navigator.appName == "Microsoft Internet Explorer")
		{
		document.getElementById(field).style.display = "block";
		}
		else
		{
		document.getElementById(field).style.display = "table-row";
		}
	}
	
	function findProvinceIndex(provinceCd)
	{
		for(var i=0; i<document.officeFrm.provinceCmbo.options.length; i++)
		{
			if(document.officeFrm.provinceCmbo.options[i].value == provinceCd)
			{
				return document.officeFrm.provinceCmbo.options[i].index;
			}
		}
	}
	
	function enableDisableQuebecFields()
	{
		var selIndex = document.officeFrm.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 == QCIndex)
		{			
			selectedCountryCd = "CA-QC";			
		}
		loadSpecialityOptions(selectedCountryCd);

	}
	
	return {
		/* Public API */
		
		enableDisableInputFields: function()
		{
			enableDisableQuebecFields();
		},
		
		showhidefield: function() 
		{
			var selIndex = document.officeFrm.olCountryCmbo.selectedIndex;
			var selectCountry = document.officeFrm.olCountryCmbo.options[selIndex].value;

			if (selIndex > 0)
			{
				if (navigator.userAgent.indexOf('MSIE') !=-1)
				{
					document.getElementById("olSpecialityCell").style.display = "block";
				} 
				else
				{
					document.getElementById("olSpecialityCell").style.display = "table-row";
				}
			  loadSpecialityOptions(selectCountry);
			}
			else
			{
			  document.getElementById("olSpecialityCell").style.display = "none";
			}
		},

		validateOfficeLocator: function() 
		{
			var specialityCmbo = document.officeFrm.officeSpecialityCmbo;
			if(document.officeFrm.olCountryCmbo.selectedIndex == 0)
			{
				alert(countryValidationMsg);
				document.officeFrm.olCountryCmbo.focus();
				return;
			}
			//if province comboBox exists
			if(typeof document.officeFrm.provinceCmbo != 'undefined') 
			{
				if(document.officeFrm.provinceCmbo.selectedIndex == 0)
				{
					alert(provinceValidationMsg);
					document.officeFrm.provinceCmbo.focus();
					return;
				}
			}
			if(specialityCmbo.length > 1 && specialityCmbo.selectedIndex == 0)
			{
				alert(specializationValidationMsg);
				specialityCmbo.focus();
			}
			else
			{
				window.open(specialityCmbo.options[specialityCmbo.selectedIndex].value);
			}
		}
	};
	
	
}();	
