var strFoodRootPath = "/microsites";
var strURLRootPath = "http://www.unilever.com";

if (window.location.href.indexOf('://www-stage') >= 0) {
  strURLRootPath = "http://www-stage.unilever.com";
}

/* foodList array is supplied by country script on default page */
/* strCountry and strLanguage are set by the page template in case the querystring is preselected */

/* Function detectFood
   Detects if the current language has the Food Nutritional Ingredients online available */
	function detectFood(langCode)
	{
		var iFoodList;
		var iLangList;
		var countryCode;
		var tmpLangCode;
		var bFood = false;
		var currentCountry = getElement('selectCountry').options[getElement('selectCountry').selectedIndex].value;
		var countryFoodList;
		
		//alert("foodList: " + foodList.length);
		
		for(iFoodList=0;iFoodList < foodList.length;iFoodList++)
		{
			if (foodList[iFoodList].split("_")[0] == currentCountry) 
			{
					tmpLangCode = foodList[iFoodList].split("_")[1];
					if (langCode == tmpLangCode) {bFood = true;}
			}
		}
		return bFood;
	}
	
/* Function langChange
   Detects the new langauge and enables or disables the Food Nutritional Ingredients online option */
   function langChange(currentLang)
   {
	var newLang;
	var iLen = String(currentLang).length;
	
	if (iLen >= 1) {
		if (currentLang.substr(iLen-1, 1) == "*") {
			newLang = currentLang.substr(0, (iLen-1));
		} else {
			newLang = currentLang;
		}
	}

   	var oRadioChoices = getElement('radioChoices');
   	var oHpc = getElement('choiceHpc');
   	var oFood = getElement('choiceFood');
   	
   	if (detectFood(newLang) == true) {
   		oFood.disabled = false;
		oFood.checked = true;
   		oHpc.checked = false;
 		oRadioChoices.style.visibility = 'visible';

   	} else {
		oFood.checked = false;
   		oFood.disabled = true;
   		oHpc.checked = true;
 		oRadioChoices.style.visibility = 'hidden';
   	}
   }

	function buildCountry(form,countryNumber)
	{
   	var oHpc = getElement('choiceHpc');
   	var oFood = getElement('choiceFood');

		for(i=0;i < CountryList.length;i++)
		{
			SubCountryArray = CountryList[i].split("%");
			SplitSubCountryArray = SubCountryArray[0].split("_");
			thisSelected = false;
			if(countryNumber == SplitSubCountryArray[1]) thisSelected = true;
			thisObj = new Option(SplitSubCountryArray[0],SplitSubCountryArray[1],thisSelected,false);
			form.selectCountry.options[i +1] = thisObj;
		}

		form.language.options[0].selected = true;

		oFood.checked = false;
 		oFood.disabled = true;
 		oHpc.checked = true;

 		/* Check to see if country and language have already been supplied using querystring */
 		if (strCountrySelected != '') 
 		{
 			getElement('selectCountry').value = strCountrySelected;
			buildLanguage(getElement('selectCountry').form,"");
 			if (strLanguageSelected != '') 
 			{
 				getElement('language').value = strLanguageSelected;
 				langChange(strLanguageSelected);
 			}
 		}
	}

	function buildLanguage(form,languageNumber)
	{
   	var oRadioChoices = getElement('radioChoices');
   	var oHpc = getElement('choiceHpc');
   	var oFood = getElement('choiceFood');
   	
		if(form.language)
		{
	
			// find currently user selected Country
			thisCountry = form.selectCountry.options[form.selectCountry.selectedIndex].text;
			thisCountryLength = thisCountry.length;
		
			// remove existing Languages
			form.language.options.length = 1;
		
			// try matching Country with array of Countries

			for(i=0;i < CountryList.length;i++)
			{
				if(thisCountry.toUpperCase() == CountryList[i].substring(0,thisCountryLength).toUpperCase())
					{
							// This is a Region match - know which element it was so extract corresponding Sub Region selection
							SubCountryArray = CountryList[i].split("%");
			
							if(SubCountryArray.length == 2)
								{
									thisObj = new Option(defLang,0,true,false);
									form.language.options[0] = thisObj;
			
									SplitSubCountryArray = SubCountryArray[1].split("_");;
									thisObj = new Option(SplitSubCountryArray[0],SplitSubCountryArray[1],false,false);
									form.language.options[1] = thisObj;				
								}
							else
								{
								thisObj = new Option(defLang,0,true,false);
								form.language.options[0] = thisObj;
			
								for(j=1; j < SubCountryArray.length;j++)
									{ 
									SplitSubCountryArray = SubCountryArray[j].split("_");;
									thisObj = new Option(SplitSubCountryArray[0],SplitSubCountryArray[1],false,false);
									form.language.options[j] = thisObj;
									}
								}
								
							form.language.options[0].selected = true;
								
					}
		}

		oFood.checked = false;
 		oFood.disabled = true;
 		oHpc.checked = true;
 		oRadioChoices.style.visibility = 'hidden';
 		
	}
}

function submitForm(form,thisAction)
	{
	if(form.selectCountry.options[form.selectCountry.selectedIndex].value == "0")
		{
			alert('Please select a country');
			form.selectCountry.focus();
			return false;
		}
	else if(form.language.options[form.language.selectedIndex].value == "0")
		{
			alert('Please select a language');
			form.language.focus()	
			return false;
		}
	else
		{
			form.selectCountry.value = form.selectCountry.options[form.selectCountry.selectedIndex].value;	
			form.selectCountryName.value = form.selectCountry.options[form.selectCountry.selectedIndex].text;	
			form.language.value = form.language.options[form.language.selectedIndex].value;

			//check to see if country language includes an '*' which means it's not ready
			var lang = form.language;
			var langcode = lang.value;
			var iLen = String(langcode).length;
			var products;

			if (iLen >= 1) {
				if (lang.value.substr(iLen-1, 1) == "*") {
					thisAction = "/comingsoon.asp";
					langcode = lang.value.substr(0, (iLen-1));
					lang.value = langcode;
				}
			}

			if (getElement('choiceFood').checked) {
				if (thisAction != "/comingsoon.asp") {
					/* thisAction = "/ni/brands.asp"; */
					thisAction = "/ni/search.asp";
				}
				//form.action = strFoodRootPath + "/" + langcode  + thisAction;
				form.action = strURLRootPath + strFoodRootPath + thisAction;
				//form.action = strFoodRootPath + thisAction;
			} else {
				form.action = strURLRootPath + "/PIOTI/" + langcode  + thisAction;
				//form.action = "/PIOTI/" + langcode  + thisAction;
			}

			form.submit();
		}
	}


function submitThis(form,thisAction,country, countryName, language, brandid, productid)
{
form.selectCountry.value = country;
form.selectCountryName.value = countryName;
form.language.value = language;
form.brandid.value = brandid;
form.productid.value = productid;

if(thisAction != "") form.action = thisAction;
form.submit();
}

function submitBrandSelect(thisLine)
{
thisArray = thisLine.split(",");
document.form1.selectCountry.value = thisArray[2];
document.form1.selectCountryName.value = thisArray[3];
document.form1.language.value = thisArray[4];
document.form1.brandid.value = thisArray[5];
document.form1.productid.value = thisArray[6];

if(thisLine == "") return false;

if(thisArray[1] != "") document.form1.action = thisArray[1];
document.form1.submit();
}