﻿var CODE = 0;
var NAME = 1;
var LANGUAGE = 2;

var countries = new Array();

var counter = 0;

countries[counter] = new Array("BE","Belgium",new Array());
countries[counter][LANGUAGE][0] = new Array("nl","Nederlands");
countries[counter++][LANGUAGE][1] = new Array("fr","Français");

countries[counter] = new Array("DK","Danmark",new Array());
countries[counter++][LANGUAGE][0] = new Array("da","Dansk");

countries[counter] = new Array("DE","Deutschland",new Array());
countries[counter++][LANGUAGE][0] = new Array("de","Deutsch");

countries[counter] = new Array("ES","España",new Array());
countries[counter++][LANGUAGE][0] = new Array("es","Español");

countries[counter] = new Array("FR","France",new Array());
countries[counter++][LANGUAGE][0] = new Array("fr","Français");

countries[counter] = new Array("IE","Ireland",new Array());
countries[counter++][LANGUAGE][0] = new Array("en","English");

countries[counter] = new Array("IT","Italia",new Array());
countries[counter++][LANGUAGE][0] = new Array("it","Italiano");

countries[counter] = new Array("LU","Luxembourg",new Array());
countries[counter++][LANGUAGE][0] = new Array("fr","Français");

countries[counter] = new Array("NL","Nederland",new Array());
countries[counter++][LANGUAGE][0] = new Array("nl","Nederlands");

countries[counter] = new Array("NO","Norge",new Array());
countries[counter++][LANGUAGE][0] = new Array("no","Norsk");

countries[counter] = new Array("AT","Österreich",new Array());
countries[counter++][LANGUAGE][0] = new Array("de","Deutsch");

countries[counter] = new Array("PT","Portugal",new Array());
countries[counter++][LANGUAGE][0] = new Array("pt","Português");

countries[counter] = new Array("CH","Schweiz",new Array());
countries[counter][LANGUAGE][0] = new Array("fr","Français");
countries[counter++][LANGUAGE][1] = new Array("de","Deutsch"); 

countries[counter] = new Array("FI","Suomi",new Array());
countries[counter++][LANGUAGE][0] = new Array("fi","Suomi");

countries[counter] = new Array("SE","Sverige",new Array());
countries[counter++][LANGUAGE][0] = new Array("sv","Svenska");

countries[counter] = new Array("GB","United Kingdom",new Array());
countries[counter++][LANGUAGE][0] = new Array("en","English");

countries[counter] = new Array("GR","Ελλάδα",new Array());
countries[counter++][LANGUAGE][0] = new Array("el","ελληνικά");

function getCountryByCode(countryCode){

	for(var i=0;i<countries.length;i++){
		if(countries[i][CODE] == countryCode){
			return countries[i];
		} 
	}
	return null;
}

function changeCountry(countrySelect,languageSelect){

	var countryCode = countrySelect.options[countrySelect.selectedIndex].value; 	
	var language = getCountryByCode(countryCode)[LANGUAGE];

	languageSelect.length = language.length; 	
		
	for(var i=0;i<language.length;i++){
		languageSelect.options[i].value = language[i][CODE];
		languageSelect.options[i].text = language[i][NAME];
	}
}

function setCountry(countrySelect){
	countrySelect.length = countries.length;
 
	for(var i=0;i<countries.length;i++){
		countrySelect.options[i].value = countries[i][CODE];
		countrySelect.options[i].text = countries[i][NAME];
	}
}


function selectCountry(countrySelect,countryCode){

	for(var i=0;i<countrySelect.length;i++){
		if(countrySelect.options[i].value == countryCode){
			countrySelect.selectedIndex = i;
			return;
		}
	}
}

function selectLanguage(languageSelect,languageCode){

	for(var i=0;i<languageSelect.length;i++){
		if(languageSelect.options[i].value == languageCode){
			languageSelect.selectedIndex = i;
			return;
		}
	}
}

function initLocale(countrySelect,languageSelect,countryCode,languageCode){

	setCountry(countrySelect);
	selectCountry(countrySelect,countryCode);
	changeCountry(countrySelect,languageSelect);
	selectLanguage(languageSelect,languageCode);	
}

function submitLocaleToEntrance(countrySelect,languageSelect){

	location.href = "/index.html?locale="
					+ languageSelect.options[languageSelect.selectedIndex].value
					+ "_"
					+ countrySelect.options[countrySelect.selectedIndex].value;
}

