//
// common.js - Generic functions that can apply to any page on the site.
//

//----------------------------
// DOM manipulation functions.
//----------------------------
function e(id) {
	return document.getElementById(id);
}

function hide(id) {
	var el = e(id);
	if (el)
		el.style.display = 'none';
	return el;
}

function show(id) {
	var el = e(id);
	if (el)
		el.style.display = 'block';
	return el;
}

function isShown(id) {
	var el = e(id);
	if (! el)
		return true;
    return el.style.display != 'none';
}

function isHidden(id) {
	return ! isShown(id);
}

function getSelected(field) {
	if (! field) return null;
	var index = field.selectedIndex;
	if (! index) return null;
	return field.options[index].value
}

function isChecked(field) {
	if (! field) return false;
	return field.checked == true;
}

function isEmpty(value) {
	return value == null || value == "";
}

function getEventTarget(ev) {
	var event = ev ? ev : window.event;
	var target = event.target ? event.target : event.srcElement;
	if (target.nodeType == 3) target = target.parentNode; // handle Safari bug
	return target;
}

function showSelect(id, show) {
	if (show == null) show = true;
	hideSelect(id, !show);
}

function hideSelect(id, hide) {
	if (! isIE6()) return; // Only Internet Explorer 6 needs this hack.
	var field = e(id);
	if (! field || ! field.options) return;
	if (hide == null) hide = true;
	var replacementField = e(id + "_replacement");
	if (! hide) {
		if (replacementField) replacementField.style.display = "none";
		field.style.display = "";
	} else {
		var tmpValue = field.options[field.selectedIndex].text;
		var tmpWidth = field.offsetWidth;
		field.style.display = "none";
		if (replacementField) {
			 replacementField.style.width = tmpWidth + "px";
			 replacementField.style.paddingLeft = "3px";
			 replacementField.style.paddingTop = "1px";
			 replacementField.style.paddingBottom = "0px";
			 replacementField.style.marginTop = "0px";
			 replacementField.style.marginBottom = "1px";
			 replacementField.style.display = "";
			 replacementField.value = tmpValue;
		}
	}
}

function isIE6() {
	var ua = navigator.userAgent.toLowerCase();
	return /msie 6/.test(ua) && ! /msie 7/.test(ua);
}

//----------------------------
// Cookie manipulation functions.
//----------------------------
function getCookie(name) {
	var results = document.cookie.match ( name + '=(.*?)(;|$)' );
	if (! results || results.length < 2)
		return null;
	return (unescape(results[1]));
}

function setCookie(name, value, days, path) {
	var cookieExpires = "";
	var cookiePath = "; path=/";
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days*24*60*60*1000));
		cookieExpires = "; expires=" + date.toGMTString();
	}
	if (! path) {
		path = "/";
	} 
	cookiePath = "; path=" + path;
	var domain = "; domain=.swiregroupinc.com";
	document.cookie = name + "=" + escape(value) + cookieExpires + cookiePath + domain;
}

// Write a cookie and sets its life time to 5 years
function writeCookie(name, value) {
	setCookie(name, value, 5 * 365, '/');
}

function deleteCookie(name, path, domain) {
    var value = getCookie(name);
    if (value) {
        document.cookie = name + "=" + escape(value)
            + "; path=" + ((path) ? path : "/")
            + "; domain=" + ((domain) ? domain : ".swiregroupinc.com")
            + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

//----------------------------
// Locale handling functions.
//----------------------------
var sitePaths = {
	de_CH: 'ch/de', de_DE: 'de', en_AU: 'au/en', en_CA: 'en', en_CH: 'ch/en', en_DE: 'de/en', en_DK: 'dk/en',
	en_FR: 'france/en', en_HK: 'hk/en', en_IE: 'ie/en', en_IL: 'il/en', en_NL: 'nl/en', en_NO: 'no/en', en_SE: 'se/en',
	en_GB: 'uk/en', en_US: 'en/us', fr_CA: 'fr', fr_CH: 'ch/fr', fr_FR: 'france', it_EN: 'it/en', it_IT: 'it'
};

// Global 'locale' variables.
var siteLang, siteCountry, siteLocale, sitePath;

// Use these functions instead of reading global variables directly.
function getSiteLang()    { return siteLang; }
function getSiteCountry() { return siteCountry; }
function getSiteLocale()  { return siteLocale; }
function getSitePath(locale) {
	if (typeof sitePaths[locale] == "undefined") return "en";
	return sitePaths[locale];
}

function getLangCookie() {
	var lang = getCookie('new_lang_pref');
	if (! lang) return null;
	else if (lang == "french") return "fr";
	else if (lang == "english") return "en";
	else if (lang == "us") return "en";
	else return lang.toLowerCase();
}
function getCountryCookie() {
	var country = getCookie('country_pref');
	if (! country) return null;
	return country.toUpperCase();
}
function setLangCookie(lang) {
	if (getCountryCookie() == "US")
 	  writeCookie("new_lang_pref", "us");
	else if (lang == "en")
 	  writeCookie("new_lang_pref", "english");
 	else if (lang == "fr")
 	  writeCookie("new_lang_pref", "french");
 	else
 	  writeCookie("new_lang_pref", lang);
}
function setCountryCookie(country) {
	writeCookie('country_pref', siteCountry);
 	if (country == "US")
 	  writeCookie('new_lang_pref', "us");
 	else if (getLangCookie() == "us")
 	  writeCookie('new_lang_pref', "en");
}

// Every page should call this function to get the locale besing used.
function getSite() {
	setSiteVariables(getLangCookie(), getCountryCookie());
}

function setSite(lang, country) {
	lang = lang.toLowerCase();
	country = country.toUpperCase();
	setSiteVariables(lang, country);
	setCountryCookie(country);
	setLangCookie(lang);
}

function setSiteVariables(lang, country) {
	siteLang = lang;
	siteCountry = country;
	siteLocale = siteLang + "_" + siteCountry;
	sitePath = sitePaths[siteLocale];
	if (sitePath = null || sitePath == "") {
		// Requested locale is unkown, use default values.
		siteLang = "en";
		siteCountry = "CA";
		siteLocale = "en_CA";
		sitePath = "en";
	}
	setLegacyVariables();
}

// Legacy variables still used by some of the shared includes.
var language_path, IsUSBanner, countryCookieValue, Language, cCookie, countryCookie, langCookie;
function setLegacyVariables() {
	if (siteLang == "fr") Language = "french";
	else if (siteLang == "en") Language = "english";
	else 	Language = siteLang;
	language_path = siteLang;
	countryCookieValue = siteCountry;
	IsUSBanner = (siteCountry == "us");
	cCookie = siteCountry.toLowerCase();
	countryCookie = siteCountry;
	langCookie = getCookie('new_lang_pref');
}

//----------------------------
// Other functions.
//----------------------------



