﻿var TE_locale;
var TE_selectedLang;
var langIds = new Array();
	langIds[0] = 1;
	langIds[1] = 2;
	langIds[2] = 3;
	langIds[3] = 4;
	langIds[4] = 5;
	langIds[5] = 6;
	langIds[6] = 8;
	langIds[7] = 9;
	langIds[8] = 10;
	langIds[9] = 14;
	langIds[10] = 15;
	langIds[11] = 16;
	langIds[12] = 17;

function TE_initLanguageSelection() {
	TE_selectedLang = dojo.cookie("language_id");
	
	// if language cookie doesn't exist, check whether language_id cookie 
	//		can be written and read
	if ( ! TE_selectedLang) {
		TE_setLanguageCookie(1);
  		TE_selectedLang = dojo.cookie("language_id");
		// cookies are not enabled or domain is not tycoelectronics.com, 
		//	so don't show language selector
		if ( ! TE_selectedLang || TE_selectedLang != 1) {
			return;
		}
 	}
	
	var isLangIdValid = false;	

	if (TE_selectedLang) {
		for (i = 0; i < langIds.length; i++) {
			if (langIds[i] == TE_selectedLang) {
				isLangIdValid = true;
				break;
			}
		}
	}
	
	if ( ! isLangIdValid) {
		TE_setLanguage(1);
		TE_selectedLang = 1;
	}
	
	TE_locale  = TE_getLocale();	// set global TE_locale based on language_id cookie
	
	var te_lang_span = dojo.query("#te-lang-sel-nav > span")[0];
	if (te_lang_span)
	{
		var parentSelNode;
		var selNode = dojo.query("#langId" + TE_selectedLang + " > a")[0];
		if (selNode) {
			te_lang_span.appendChild(document.createTextNode(selNode.firstChild.nodeValue));
		}
	}
	
	dojo.addOnLoad(function() {
		dojo.require("dijit.Dialog");
		
		var parentSelNode;
		var selNode = dojo.query("#langId" + TE_selectedLang + " > a")[0];
		if (selNode) {
			parentSelNode = selNode.parentNode;
			dojo.addClass(parentSelNode, "te-langSelected");
			parentSelNode.innerHTML = selNode.firstChild.nodeValue;
		}
		
		TE_showLangSelectorText();
		
		dojo.connect(dojo.byId("teLangSelect"), "onclick", function() {			
			if (!dijit.byId("teLangSelN"))
			{
				var langSel = new dijit.Dialog({
					title: "Choose Your Language or Country",
					style: "padding-bottom: 10px",
					id: "teLangSelN",
					autofocus:false
				}, dojo.byId("te-langsel-node"));
				
				langSel.show();	
			}
			else
			{
				dijit.byId("teLangSelN").show();
			}
		});
	});

}

function TE_setLanguageCookie(langId){
	TE_SetCookie("language_id",langId,TE_GetCookieExpiration(365),".tycoelectronics.com","/");
}

function TE_setLanguage(langId){
	TE_setLanguageCookie(langId);
	console.log("just entered TE_set Language with param" + langId);
	var sURL = window.location;
	sURL = TE_languageReWriteURL(sURL,langId);
	var re = new RegExp("/catalog/.*/(en|cs|de|es|fr|hu|it|ja|ko|pl|pt|zh-hans|ru)", "ig");
	if (re.test(sURL)) {
		window.location.reload(true);
	}
	else {
		window.location.replace(sURL);
	}
}

// Function to rewrite the URL and add or replace a
// LG parameter with the new language id
function TE_languageReWriteURL(url,id) {
	var re,str,newurl;
	// default set the return url to be the incomming url
	newurl=url;
	// create a RegExp object to handle the functionality
	// initial pattern looking for an existing LG parameter, either first or somewhere
	// on the line.  Pattern ignores case and is global (finds all occurences).
	re = new RegExp("([\?\&]LG=)([1-9][0-9]?)","ig");
	// check the URL to see if it matches
	if (re.test(url))
	{
		// found a match, then we have a parameter already in place
		// lets replace it.
		// create a String object
		str = new String(url);
		// Ask the string object to do the replace for us giving it the regular expression
		// the replacement string is to return the &LG= or ?LG= ($1) followed by our new language id
		newurl = str.replace(re,"$1"+id);
	}
	else { // we didn't match, so no LG parameter on the URL
		var checkParam;
		// look to see if anchor fragment on the url - remove for now
		checkParam = new RegExp("([\#].*$)","ig");
		if (checkParam.test(url))
		{
		    // Yes, there is a # present, so remove
			str = new String(url);
			newurl = str.replace(checkParam,"");
			// put this back in the url so we can contine to check for other parameters
			url = newurl;
		}
		// look to see if other parameters on the string, respect them...
		checkParam = new RegExp("([\?])","ig");
		if (checkParam.test(url))
		{
		    // Yes, there is a ? already present, so append &LG=N
			//unless we are in the /catalog/*/language path
			var ree = new RegExp("/catalog/.*/(en|cs|de|es|fr|hu|it|ja|ko|pl|pt|zh-hans|ru)", "ig");
			var chk = new RegExp("([\?\&]LG=)([1-9][0-9]?)","ig");
			if(ree.test(url) && chk.test(url)) {
				var stree = new String(url);
				newurl = stree.replace(chk, "");
			}
			else {
				newurl = url + "&LG="+id;
			}
		}
		else {
		    // No there wasn't a ? found on the URL so append ?LG=N
			//unless we are in the /catalog/*/language path
			var noq = new RegExp("/catalog/.*/(en|cs|de|es|fr|hu|it|ja|ko|pl|pt|zh-hans|ru)", "ig");
			if(noq.test(url)) {
				newurl = url;
			}
			else {
				newurl = url + "?LG="+id;
			}
		} 
	} // end of if didn't find LG paramenter
	// return the transformed url
	return newurl;
}

function TE_showLangSelectorText() {
	var languagesNode = dojo.byId("TE_langSelector_langHdg");
	var countrySitesNode = dojo.byId("TE_langSelector_countryHdg");

	dojo.requireLocalization("te", "ewt", TE_locale);
	var ewt = dojo.i18n.getLocalization("te", "ewt", TE_locale);

	languagesNode.appendChild(document.createTextNode(ewt.languagesMessage));
	countrySitesNode.appendChild(document.createTextNode(ewt.countrySitesMessage));
}
