
var pageName = '';
var newURL='';

//*** abracke (11/07/08) variables to see if user changes country/language
var countryChanged='False';
var languageChanged='False';

var languages = new Array(
		new Array('English', 'http://www.atlascopco.com/primeenergy/'));
var selectedCountry = 'Atlas Copco Prime Energy';
var initialCountry = 'Atlas Copco Prime Energy';


var initialSplashCountryList =  '<label for="country-select">Select country</label>' + 
	'<select id="country-select" name="countryselect" onchange="processSplashCountry(this)">' + 
	'<option value=""></option>' +
	'<option value="0">Atlas Copco Prime Energy</option>' +
	'</select>';
var initialSplashLanguageList = '<label  id="label-lang-select" for="splash-lang-select" name="langSelLabel">Select language</label>' +
	'<select id="splash-lang-select" name="langselect" onchange="processSplashLanguage(this)">' +
	'<option value=""></option>' +
	'<option value="http://www.atlascopco.com/primeenergy/" selected="true">English</option>' +
	'</select>';

var initialCountryList = '<label for="country-select">Select country</label>' + 
	'<select id="country-select" name="countryselect" onchange="processCountry(this)">' + 
	'<option value=""></option>' +
	'<option value="0" selected="true">Atlas Copco Prime Energy</option>' +
	'</select>';
var initialLanguageList = '<label for="lang-select">Change language</label>' + 
	'<select id="lang-select" name="langselect" onchange="processLanguage(this)" disabled="true">' +
	'<option value=""></option>' +
	'<option value="http://www.atlascopco.com/primeenergy/" selected="true">English</option>' +
	'</select>';

// shows an optionlist if no country has been selected, the selected country otherwise - uses Prototype
// 0 - country button pressed; 1 - language button pressed
function showSelectedCountry(type) {
	if (type == 1) {
		Element.update('lang-country', '<label></label><label id="country">' + initialCountry + '</label>');
		jQuery('#popup-submit').attr("href", "#");
		jQuery('#popup-submit').attr("id", "popup-submit-disabled");
	} else {
		Element.update('lang-country', initialCountryList);
		processCountry($('country-select'));
	}
	Element.update('langsel-outer', initialLanguageList);
}

function processCountry(objSelect) {
	var selectedValue = objSelect.options[objSelect.selectedIndex].value;
	var objForm = objSelect.form;
	var objLangSel = objForm.langselect;
	selectedCountry = objSelect.options[objSelect.selectedIndex].text;
	pageName = '';
	jQuery('#popup-submit').attr("href", "#");
	jQuery('#popup-submit').attr("id", "popup-submit-disabled");
	for (i = objLangSel.options.length - 1; i >= 0; i--) {
		objLangSel.options[i] = null;
	}
	if (selectedValue == '') {
		objLangSel.disabled = true;
		//objLangSel.options[0] = new Option("", "");
		return;
	}
	if (languages[selectedValue].length > 2) {
		countryChanged = 'True';
		objLangSel.disabled = false;
		objLangSel.options[0] = new Option("", "");
		for (i = 0; i < languages[selectedValue].length / 2; i++) {
			objLangSel.options[i + 1] = new Option(languages[selectedValue][2 * i], languages[selectedValue][2 * i + 1]);
		}
	} else if (languages[selectedValue].length == 2) {
		countryChanged = 'True';
		newURL = languages[selectedValue][1];
		//rmbr(objForm);
		objLangSel.options[0] = new Option(languages[selectedValue][0], languages[selectedValue][1]);
		objLangSel.disabled = true;
		jQuery('#popup-submit-disabled').attr("href", "JavaScript:rmbrOverlayer(this.form); window.location=newURL;");
		jQuery('#popup-submit-disabled').attr("id", "popup-submit");
	} else {
		objLangSel.style["display"] = "none";
	}
}

function processSplashCountry(objSelect) {
	var selectedValue = objSelect.options[objSelect.selectedIndex].value;
	var objForm = objSelect.form;
	var objLangSel = objForm.langselect;
	var objLangSelSpan = $('language-span');
	var objCountryButton = $('submitCountry');
	selectedCountry = objSelect.options[objSelect.selectedIndex].text;
	pageName = '';
	newURL = "";
	for (i = objLangSel.options.length - 1; i >= 0; i--) {
		objLangSel.options[i] = null;
	}
	if (selectedValue == '') {
                                objLangSelSpan.style["display"] = "none";
		objLangSel.style["display"] = "none";
		objCountryButton.style["display"] = "inline";
		objLangSel.options[0] = new Option("", "");
		return;
	}
	if (languages[selectedValue].length > 2) {
                                objLangSel.style["display"] = "inline"; 
		objLangSel.options[0] = new Option("", "");
		for (i = 0; i < languages[selectedValue].length / 2; i++) {
			objLangSel.options[i + 1] = new Option(languages[selectedValue][2 * i], languages[selectedValue][2 * i + 1]);
		}
		objLangSelSpan.style["display"] = "block";
		objCountryButton.style["display"] = "none";
	} else if (languages[selectedValue].length == 2) {
		newURL = languages[selectedValue][1];
		rmbr(objForm);
		objLangSel.options[0] = new Option(languages[selectedValue][0], languages[selectedValue][1]);
		objLangSelSpan.style["display"] = "none";
		objCountryButton.style["display"] = "inline";
	} else {
		objLangSelSpan.style["display"] = "none";
		objCountryButton.style["display"] = "inline";
	}
}



function processLanguage(objSelect) {
	var objForm = objSelect.form;
	newURL = objSelect.options[objSelect.selectedIndex].value;
	if (newURL == '') {
		jQuery('#popup-submit').attr("href", "#");
		jQuery('#popup-submit').attr("id", "popup-submit-disabled");
	} else {
		jQuery('#popup-submit-disabled').attr("href", "JavaScript:rmbrOverlayer(this.form); window.location=newURL;");
		jQuery('#popup-submit-disabled').attr("id", "popup-submit");
		//rmbr(objForm);
		newURL = newURL + pageName;
		languageChanged = 'True';
	}
}
function processSplashLanguage(objSelect) {
	var objForm = objSelect.form;
	newURL = objSelect.options[objSelect.selectedIndex].value;
	if (newURL != '') {
		rmbr(objForm);
		newURL = newURL + pageName;
	}
}

function processSplashCorporate(strRedirectTo) {
	var objSelect = $('lang-select');
	newURL = objSelect.options[objSelect.selectedIndex].value;
	if (newURL != '') {
		rmbr(objSelect.form);
		window.location = newURL + strRedirectTo;
	}
	return false;
}

function processSplashCountryLanguage(strRedirectTo) {
	if (newURL != '') {
		var objSelect = $('splash-lang-select');
		rmbr(objSelect.form);
		window.location = newURL + strRedirectTo;
	}
	return false;
}


/********************
* Cookie Management *
********************/


function setCookie(name, value, expires, path, domain, secure) {
	curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "")
	document.cookie = curCookie
}


function getCookie(name) {
	prefix = name + '='
	c = document.cookie
	nullstring = ''
	cookieStartIndex = c.indexOf(prefix)
	if (cookieStartIndex == -1) {
		return nullstring
	}
	cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length)
	if (cookieEndIndex == -1) {
		cookieEndIndex = c.length
	}
	return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}


function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT"
	}
}


function fixDate(date) {
	base = new Date(0)
	skew = base.getTime()
	if (skew > 0) {
		date.setTime(date.getTime() - skew)
	}
}


function rmbr(form) {
	if (form) {
		if( 'Local' == 'Corporate') {
			deleteCookie("AtlasCopcoCorporate", "/", "")
			if (form.remember.checked) {
				now = new Date()
				fixDate(now)
				now.setTime(now.getTime() + 31536000000) // 1 year
				setCookie("AtlasCopcoCorporate", newURL, now, "/", "", "")
			} 
		}
		else
		{
			deleteCookie("AtlasCopcoAtlas Copco Prime Energy", "/", "")
			if (form.remember.checked) {
				now = new Date()
				fixDate(now)
				now.setTime(now.getTime() + 31536000000) // 1 year
				setCookie("AtlasCopcoAtlas Copco Prime Energy", newURL, now, "/", "", "")
			} 
		}
	}
}

//** abracke (11/07/08)
// new functionality for 'Select country', 'Change language'
function rmbrOverlayer(form) {
	
		//On local sites: only remember when language only has changed: write local cookie
		if (countryChanged == 'False' && languageChanged == 'True')
		{
			deleteCookie("AtlasCopcoAtlas Copco Prime Energy", "/", "");
			now = new Date();
			fixDate(now);
			now.setTime(now.getTime() + 31536000000); // 1 year
			setCookie("AtlasCopcoAtlas Copco Prime Energy", newURL, now, "/", "", "");
		}
	

	countryChanged = 'False';
	languageChanged = 'False';
}


window.onload = function () {
	var divs = document.getElementsByTagName('div');
	var blnFirst = false;
	for (var i = 0; i < divs.length; i++) {

	if (divs[i].id == 'modules') {
		blnFirst = true;
		continue;
	}
	if (divs[i].className == 'geheel') {
		if (blnFirst) {
			divs[i].className = '';
			blnFirst = false;
			}
		}
	}
}


var hexVals = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
var unsafeString = "\"<>%\\^[]`\+\$\,";
// deleted these chars from the include list ";", "/", "?", ":", "@", "=", "&" and #
// so that we could analyze actual URLs

function isUnsafe(compareChar) {
// this function checks to see if a char is URL unsafe.
// Returns bool result. True = unsafe, False = safe

	if (unsafeString.indexOf(compareChar) == -1 && compareChar.charCodeAt(0) > 32 && compareChar.charCodeAt(0) < 123) {
		return false; // found no unsafe chars, return false
	} else {
		return true;
	}
}


function decToHex(num, radix) { // part of the hex-ifying functionality
	var hexString = "";

	while (num >= radix) {
		temp = num % radix;
		num = Math.floor(num / radix);
		hexString += hexVals[temp];
	}
	hexString += hexVals[num];

	return reversal(hexString);
}


function reversal(s) { // part of the hex-ifying functionality
	var len = s.length;
	var trans = "";

	for (i = 0; i < len; i++) {
		trans = trans + s.substring(len - i - 1, len - i);
		s = trans;
	}

	return s;
}


function convert(val) { // this converts a given char to url hex form
	return "%" + decToHex(val.charCodeAt(0), 16);
}


function encodeIt(val) {
	var len = val.length;
	var backlen = len;
	var i = 0;

	var newStr	= "";
	var frag		= "";
	var encval	= "";
	var original = val;

	for (i=0; i < len; i++) {
		if (val.substring(i, i + 1).charCodeAt(0) < 255) { // hack to eliminate the rest of unicode from this
			if (isUnsafe(val.substring(i, i + 1)) == false) {
				newStr = newStr + val.substring(i, i + 1);
			} else {
				newStr = newStr + convert(val.substring(i, i + 1));
			}
		} else { // woops! restore.
			newStr = original;
			i = len; // short-circuit the loop and exit
		}
	}
	return newStr;
}