﻿/*JS-VERSION:0.0.1.19*/
/*rosh:27.03.07: global variables*/
var selectionLanguage = readCookie(cookieLanguageName);
var selectionCountry	= readCookie("Country");
/*flags*/
var ddlOnScreen = false;
var onShowLangListActive = false;

/*rosh:27.03.07: validate and set default data*/
if(selectionCountry == null || selectionCountry == '')
{
	selectionCountry = 'ROW';
	createCookie("Country",selectionCountry,365);
}

if(selectionLanguage == null || selectionLanguage == '')
{	
	var result = location.href.replace("#","");
	
	if(result.indexOf('/EN/') != -1)
	{
		selectionLanguage = 'EN';
	}
	else if(result.indexOf('/FR/') != -1)
	{
		selectionLanguage = 'FR';
	}
	else if(result.indexOf('/DE/') != -1)
	{
		selectionLanguage = 'DE';
	}
	else if(result.indexOf('/IT/') != -1)
	{
		selectionLanguage = 'IT';
	}
	else
	{
		selectionLanguage = 'EN';
	}
	createCookie(cookieLanguageName,selectionLanguage,365);
}

//if(selectionLanguage.toUpperCase() == 'ES' || readCookie(cookieLanguageName).toUpperCase() == 'ES')
//{
//	createCookie(cookieLanguageName,'EN',365);
//	// reload page
//}

if(!readCookie(cookieLanguageName))
{
	createCookie(cookieLanguageName,'EN',365);
}

/*rosh:28.05.07: maintain drop-downs on screen*/ 
function maintainDropDowns(val)
{
	if( typeof( HideDropDowns ) != 'undefined' )
	{
		if(val && ddlOnScreen)
		{
			maintainDropDowns(false);
		}
			
		HideDropDowns(val);
		ddlOnScreen = true;
	}
}

/*rosh:5.04.07: validate and set default data*/ 
function showLanguageLayer()
{
	maintainDropDowns(true);
	ddlOnScreen = true;
	if(document.getElementById('languagelayer').className == 'shown'){
		hideLanguageLayer();
	}
	else{
		document.getElementById('languagelayer').className = 'shown';
		document.getElementById('langbutton').className = "hi";
	}
	languageActivation();
}
/*rosh:5.04.07: hide all related language selection related elements*/
function hideLanguageLayer()
{
	maintainDropDowns(false);
	hideContinentList(null, 0);
	hideCountryList(null, 0);
	hideLangList(null);
	document.getElementById('languagelayer').className = '';
	document.getElementById('langbutton').className = "";
}
/*rosh:5.04.07: display countries*/
function showCountryList(){
	hideLangList(null);
	if(document.getElementById('countrytitle').className == 'langlink hi'){
		hideCountryList(null);
	}
	else{
		document.getElementById('countrylist').className = 'shown';
		document.getElementById('countrytitle').className = 'langlink hi';
	}
}

/*rosh:5.04.07: hide country selection*/
function hideCountryList(elm, country, shrt, lang){
	if(elm != null){
		document.getElementById('langlist').innerHTML = document.getElementById('langlist_'+country).innerHTML;
		hideLangList(document.getElementById('langlist_'+country).firstChild.firstChild.firstChild, null);		
		document.getElementById('countrytitle').innerHTML = elm.innerHTML;
	}
	document.getElementById('countrytitle').className = 'langlink';
	document.getElementById('countrylist').className = '';
	/*set cookie values for usage after page reload*/	
	if(shrt){
		selectionCountry = shrt;
		// ROSHTMP:: createCookie("Country",selectionCountry,365);
	}	
	if(lang){
		selectionLanguage = lang;
	}
//	languageActivation();
}
/*rosh:5.04.07: display language*/
function showLangList(){
	hideCountryList(null, 0);
	if(document.getElementById('langtitle').className == 'langlink hi'){
		hideLangList(null);
	}
	else{
		document.getElementById('langlist').className = 'shown';
		document.getElementById('langtitle').className = 'langlink hi';
	}
		languageActivation();
}
/*rosh:5.04.07: hide country language*/
function hideLangList(elm, lang){
	languageActivation();
	if(elm != null){
		document.getElementById('langtitle').innerHTML = elm.innerHTML;
	}
	document.getElementById('langtitle').className = 'langlink';
	document.getElementById('langlist').className = '';
	
	if(lang){
		selectionLanguage = lang;
	}
}

/*rosh:5.04.07: set language and relead page*/
function SetLanguage(){
	createCookie(cookieLanguageName,selectionLanguage,365);
	createCookie("Country",selectionCountry,365);
	hideLanguageLayer();
	location.href = urlHandler(location.href, selectionLanguage);
}

/*rosh:5.04.07: enables / disables the language */
function languageActivation()
{
	/*hack to clarify in FF and IE the correct amount of childNodes [2 -> ff; 1-> ie] */
	var count = document.getElementById('langlist').childNodes[0].childNodes.length == 0 ? 
		document.getElementById('langlist').childNodes[1].childNodes.length == 2 : 
		document.getElementById('langlist').childNodes[0].childNodes.length == 1;
	
	if(count)
	{
		document.getElementById('selectfakeLanguage').style.background = '#dfdfdf';
		document.getElementById('langtitle').attributes['onclick'].value = 'void(0);';
		if(document.getElementById('langtitle').style)
		{
			document.getElementById('langtitle').style.textDecoration = 'none';
			document.getElementById('langtitle').style.color = 'gray';
			document.getElementById('langtitle').style.cursor = 'default';
		}
		onShowLangListActive = false;
		/*cookie for language*/
	}
	else
	{
		if(onShowLangListActive)
		{
			document.getElementById('langtitle').setAttribute('onClick', "hideLangList(null);"); 
			/*IE6/7 ISSUE!!!*/
			document.getElementById('langtitle').onclick = function() {hideLangList(null);};
			onShowLangListActive = false;
		}
		else
		{
			document.getElementById('langtitle').setAttribute('onClick', "showLangList();"); 
			/*IE6/7 ISSUE!!!*/
			document.getElementById('langtitle').onclick = function() {showLangList();};	
			onShowLangListActive = true;
		}
		document.getElementById('langtitle').style.cursor = 'pointer';
		document.getElementById('langtitle').className = 'langlink';
		document.getElementById('selectfakeLanguage').className = 'selectfake';
		/*IE6/7 ISSUE!!!*/
		if(document.getElementById('selectfakeLanguage').style)
		{
			document.getElementById('selectfakeLanguage').style.background = "transparent url(/Style%20Library/images/language/bg_select.gif) no-repeat scroll right center";
			document.getElementById('selectfakeLanguage').style.border = "1px solid #999999";
			document.getElementById('selectfakeLanguage').style.padding = "2px 0pt 2px 5px";
		}
	}
}

/*
var languageSelectionCountry = new Array(13);

for (i=0;i<13;++i){languageSelectionCountry[i]=new Array(5);}

languageSelectionCountry[0][0]= 'CH';
languageSelectionCountry[0][1] = 'EN';
languageSelectionCountry[0][2] = 'DE';
languageSelectionCountry[0][3] = 'FR';
languageSelectionCountry[0][4] = 'IT';

languageSelectionCountry[1][0]= 'IT';
languageSelectionCountry[1][1] = 'IT';

languageSelectionCountry[2][0]= 'ROW';
languageSelectionCountry[2][1] = 'EN';

languageSelectionCountry[3][0]= 'PL';
languageSelectionCountry[3][1] = 'EN';

languageSelectionCountry[4][0]= 'CA';
languageSelectionCountry[4][1] = 'EN';
languageSelectionCountry[4][2] = 'FR';

languageSelectionCountry[5][0]= 'US';
languageSelectionCountry[5][1] = 'EN';

languageSelectionCountry[6][0]= 'CZ';
languageSelectionCountry[6][1] = 'EN';

languageSelectionCountry[7][0]= 'FR';
languageSelectionCountry[7][1] = 'FR';

languageSelectionCountry[8][0]= 'DE';
languageSelectionCountry[8][1] = 'DE';

languageSelectionCountry[9][0]= 'ES';
languageSelectionCountry[9][1] = 'EN';

languageSelectionCountry[10][0]= 'AE';
languageSelectionCountry[10][1] = 'EN';

languageSelectionCountry[11][0]= 'GB';
languageSelectionCountry[11][1] = 'EN';

languageSelectionCountry[12][0]= 'SE';
languageSelectionCountry[12][1] = 'EN';

*/

function validateCountryLanguage(country, lang)
{
	for(var a=0;a<languageSelectionCountry.length;++a)
	{
		if( languageSelectionCountry[a][0].toUpperCase() == country.toUpperCase() )
		{
			for(var b=0;b<languageSelectionCountry[a].length;++b)
			{
				if(languageSelectionCountry[a][b] != null)
				{
					if( languageSelectionCountry[a][b].toUpperCase() == lang.toUpperCase() )
					{
						return true;
					}
				}
			}
		}
	}
	return false;
}

function validateLanguageCombination()
{
	var selectionLanguageCheckQuery = null;
	var selectionLanguageCheckCookie = readCookie(cookieLanguageName);
	var selectionCountryCheck = readCookie("Country");
	
	var resultCookieQuerySync = location.href.replace("#","");
	
	if(resultCookieQuerySync.toUpperCase().indexOf('/EN/') != -1)
	{
		selectionLanguageCheckQuery = 'EN';
	}
	else if(resultCookieQuerySync.toUpperCase().indexOf('/FR/') != -1)
	{
		selectionLanguageCheckQuery = 'FR';
	}
	else if(resultCookieQuerySync.toUpperCase().indexOf('/DE/') != -1)
	{
		selectionLanguageCheckQuery = 'DE';
	}
	else if(resultCookieQuerySync.toUpperCase().indexOf('/IT/') != -1)
	{
		selectionLanguageCheckQuery = 'IT';
	}

	if(	selectionLanguageCheckQuery && selectionLanguageCheckQuery != null && 
			selectionLanguageCheckCookie && selectionLanguageCheckCookie!=null )
	{
		if( selectionLanguageCheckQuery.toUpperCase() == selectionLanguageCheckCookie.toUpperCase() )
		{
			if( validateCountryLanguage(selectionCountryCheck, selectionLanguageCheckCookie) )
			{
				/* do nothing everything is fine */
				return;
			}
		}
		else
		{
			/*search for first language for the current country*/
			manipulateLanguage(resultCookieQuerySync, selectionLanguageCheckQuery, selectionLanguageCheckCookie, selectionCountryCheck);
		}	
	}
		
}

function manipulateLanguage(queryString, queryLang, cookieLang, country)
{
	var cacheIssue = (new Date()).getTime();
	var result = queryString.replace("#","");
	
	if(result.toUpperCase().indexOf('/'+queryLang+'/') != -1)
	{
		result = result.substring( 0, result.indexOf('?') == -1 ? result.length : result.indexOf('?') );
		
		if(queryString.indexOf('?') != -1)
		{ 
			var queryParameters = validateQueryParameters(queryString.substring( queryString.indexOf('?')+1 ), country);
			
			var resArray = validateCaseSensitive(queryLang);
			for(var n=0;n<resArray.length;++n)
			{
				result = replace(result,'/'+resArray[n]+'/', '/'+cookieLang+'/'); 
			}
			
			result += '?tci='+ cacheIssue +'&'+ queryParameters;
		}
		else
		{
			/*temporalry cache issue*/
			result = replace(result,'/'+queryLang+'/', '/'+cookieLang+'/') + '?tci=' + cacheIssue;
		}
	}
	location.href = result;
}

/* options to validate EN / en / En / eN */
function validateCaseSensitive(source)
{
	
	var array = new Array(4);
	if(source.length == 2)
	{
		var tmp0 = source.substr(0,1);
		var tmp1 = source.substr(1,1);
		for(var n=0;n<array.length;++n)
		{
			switch(n)
			{
				case 0:
				{
					tmp0 = tmp0.toUpperCase();
					tmp1 = tmp1.toUpperCase();
					break;
				}
				case 1:
				{
					tmp0 = tmp0.toLowerCase();
					tmp1 = tmp1.toUpperCase();								
					break;
				}
				case 2:
				{
					tmp0 = tmp0.toLowerCase();
					tmp1 = tmp1.toLowerCase();								
					break;
				}
				case 3:
				{
					tmp0 = tmp0.toUpperCase();
					tmp1 = tmp1.toLowerCase();															
					break;
				}																			
			}
			array[n] = tmp0+tmp1;
		}				
	}
	else
	{
		return new Array();
	}
	
	return array;
	
}

function validateQueryParameters(query, country)
{
	var keyValue = query.split("&");
	var newUriPara = '';
	if(keyValue)
	{
		for(var i=0;i<keyValue.length;++i)
		{
			var keyPair = keyValue[i].split("=");
			
			if(keyPair)
			{
				if(keyPair.length == 1)
				{
					continue;
				}
				
				if( keyPair[0].toUpperCase() == 'Country'.toUpperCase() )
				{
					newUriPara += keyPair[0]+'='+country;
					returnIntern = true;
				}
				else if( keyPair[0].toUpperCase() != 'tci'.toUpperCase() )
				{
					newUriPara += keyPair[0]+'='+keyPair[1];
				}
				
				if(i<keyValue.length-1)
					newUriPara += '&';
			}
		}
		
		return newUriPara;
	}
}

function hideContinentList(elm, continent, shrt, lang)
{
	if(elm != null)
	{
	    document.getElementById('countrylist').innerHTML = document.getElementById('countrylist_'+continent).innerHTML;
//	    document.getElementById('countryTitle').innerHTML = document.getElementById('countrylist_'+continent).firstChild.firstChild.firstChild.innerHTML;
	    countryshrt = document.getElementById('countrylist_'+continent).lastChild.firstChild.attributes['countryShrt'].value;
	    country = document.getElementById('countrylist_'+continent).lastChild.firstChild.attributes['country'].value;
	    countryLang = document.getElementById('countrylist_'+continent).lastChild.firstChild.attributes['countryLang'].value
	    hideCountryList(document.getElementById('countrylist_'+continent).firstChild.firstChild.firstChild, country, countryshrt, countryLang);
//		document.getElementById('langlist').innerHTML = document.getElementById('countrylist_'+continent).firstChild.firstChild.lastChild.innerHTML;
//		hideLangList(document.getElementById('countrylist_'+continent).firstChild.firstChild.firstChild, null);		
		document.getElementById('continenttitle').innerHTML = elm.innerHTML;
	}
	document.getElementById('continenttitle').className = 'langlink';
	document.getElementById('continentlist').className = '';
	/*set cookie values for usage after page reload*/	
	if(shrt)
	{
//		selectionContinent = shrt;
		selectionCountry = shrt;
	}	
	if(lang)
	{
		selectionLanguage = lang;
	}
//	countryActivation();
//	languageActivation();
	
}

function showContinentList()
{
	hideLangList(null, 0);
	hideCountryList(null, 0);
	if(document.getElementById('continenttitle').className == 'langlink hi')
	{
		hideContinentList(null);
	}
	else
	{
		document.getElementById('continentlist').className = 'shown';
		document.getElementById('continenttitle').className = 'langlink hi';
	}
}

function countryActivation()
{
	/*hack to clarify in FF and IE the correct amount of childNodes [2 -> ff; 1-> ie] */
	var count = document.getElementById('countrylist').childNodes[0].childNodes.length == 0 ? 
		document.getElementById('countrylist').childNodes[1].childNodes.length == 2 : 
		document.getElementById('countrylist').childNodes[0].childNodes.length == 1;
	
	if(count)
	{
/*		document.getElementById('countryfake').style.background = '#dfdfdf';
		document.getElementById('countrytitle').attributes['onclick'].value = 'void(0);';
		if(document.getElementById('langtitle').style)
		{
			document.getElementById('langtitle').style.textDecoration = 'none';
			document.getElementById('langtitle').style.color = 'gray';
			document.getElementById('langtitle').style.cursor = 'default';
		}
		onShowLangListActive = false;
*/		/*cookie for language*/
	}
	else
	{
		if(onShowLangListActive)
		{
			document.getElementById('countrytitle').setAttribute('onClick', "hideCountryList(null);"); 
			/*IE6/7 ISSUE!!!*/
			document.getElementById('countrytitle').onclick = function() {hideCountryList(null);};
			onShowLangListActive = false;
		}
		else
		{
			document.getElementById('countrytitle').setAttribute('onClick', "showCountryList();"); 
			/*IE6/7 ISSUE!!!*/
			document.getElementById('countrytitle').onclick = function() {showCountryList();};	
			onShowLangListActive = true;
		}
		document.getElementById('countrytitle').style.cursor = 'pointer';
		document.getElementById('countrytitle').className = 'langlink';
		document.getElementById('countryfake').className = 'selectfake';
		/*IE6/7 ISSUE!!!*/
		if(document.getElementById('countryfake').style)
		{
			document.getElementById('countryfake').style.background = "transparent url(/Style%20Library/images/language/bg_select.gif) no-repeat scroll right center";
			document.getElementById('countryfake').style.border = "1px solid #999999";
			document.getElementById('countryfake').style.padding = "2px 0pt 2px 5px";
		}
	}
}