/* Check flash available at client */
var ShockMode = 0;
var plug = false;

if(getBrowserVersionNumber() > 2)
	doFlashCheck();

function doFlashCheck(){
	plug = hasFlashPlugin();
}

function isFlashInstalled(){
	return plug;
}

function getBrowserVersionNumber(){
	return parseInt(navigator.appVersion.substring(0, 1));
}

function hasFlashPlugin(){
	if(isNetscapeCompatibleBrowser() && hasPlugins())
		return findFlashPlugin();
	if(isInternetExplorer() && isWindows())
		return findFlashByVB();
	else
		return false;
}

function isNetscapeCompatibleBrowser(){
	return navigator.appName == "Netscape";
}

function hasPlugins(){
	return navigator.plugins && navigator.plugins.length;
}

function isInternetExplorer(){
	if(navigator.userAgent)
		return navigator.userAgent.indexOf("MSIE") > -1;
	else
		return false;
}

function findFlashPlugin(){
	for(i = 0; i < navigator.plugins.length; i++)
		if(isFlashPlugin(i))
			return true;
	return false;
}

function isFlashPlugin(i){
	return navigator.plugins[i].name.indexOf('Shockwave Flash') > -1;
}

function isWindows(){
	return navigator.platform.indexOf("Win") > -1;
}

function findFlashByVB(){
	renderVisualBasicScript();
	return ShockMode;
}

function renderVisualBasicScript(){
	document.write('<SCR'+'IPT LAN'+'GUAGE="VB'+'Scr'+'ipt"\> \n');
	document.write('on error resume next \n');
	document.write('ShockMode = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.3")))\n');
	document.write('</SCR'+'IPT>');
}

function initialize(){
	registerEventHandlerAndResizeContent();
	loadMarketSelector();
}

function registerEventHandlerAndResizeContent(){
	attachAsOnResizeHandler();
	resizeContent();
}

function loadMarketSelector(){
	if(!isFlashInstalled())
		createScriptTag("/fr/javascript/marketselector_noflash.js");
}

function attachAsOnResizeHandler(){
	window.onresize = resizeContent;
}

function resizeContent(){
	if(isShowingIntro())
		correctIntroAndColumnHeight();
	else if(isShowingMarketSelectorGlobe())
		correctMarketSelectorAndColumnHeight();
	else if(isShowingMarketSelectorMap())
		correctMarketSelectorMapAndColumnHeight();
	else
		correctColumnHeight();
}

function isShowingIntro(){
	var intro = document.getElementById('intro');
	if(intro != null)
		return intro.style.display == 'block';
	else
		return false;
}

function isShowingMarketSelectorGlobe(){
	var globe = document.getElementById('market-selector-globe');
	if(globe != null)
		return globe.style.display == 'block';
	else
		return false;
}

function isShowingMarketSelectorMap(){
	var map = document.getElementById('market-selector-map');
	if(map != null)
		return map.style.display == 'block';
	else
		return false;
}

function correctIntroAndColumnHeight(){
	var correctedColumnHeight = Math.max(getMainContentHeight(), getInnerHeight()-370-126, 800-280-126);
	setColumnHeight(correctedColumnHeight);
	var correctedMarketSelectorHeight = Math.max(getMainContentHeight() + 406, getInnerHeight() - 90, 800);
	setIntroHeight(correctedMarketSelectorHeight);
}

function correctMarketSelectorAndColumnHeight(){
	var correctedColumnHeight = Math.max(getMainContentHeight(), getInnerHeight()-370-126, 800-280-126);
	setColumnHeight(correctedColumnHeight);
	var correctedMarketSelectorHeight = Math.max(getMainContentHeight() + 406, getInnerHeight() - 90, 800);
	setMarketSelectorGlobeHeight(correctedMarketSelectorHeight);
}

function correctMarketSelectorMapAndColumnHeight(){
	var correctedColumnHeight = Math.max(getMainContentHeight(), getInnerHeight()-370-126, 500);
	setColumnHeight(correctedColumnHeight);
	var correctedMarketSelectorHeight = Math.max(getMainContentHeight(), getInnerHeight()-370-126, 500);
	setMarketSelectorMapHeight(correctedMarketSelectorHeight);
}

function correctColumnHeight(){
	var correctedColumnHeight = Math.max(getMainContentHeight(), getInnerHeight()-370-126);
	setColumnHeight(correctedColumnHeight);
}

function getMainContentHeight(){
	return document.getElementById('main-content').offsetHeight;
}

function getInnerHeight(){
	if(isInternetExplorer())
		return document.documentElement.clientHeight;
	else
		return window.innerHeight;
}

function setColumnHeight(columnHeight){
	document.getElementById('main-left-column').style.height = columnHeight + "px";
	document.getElementById('main-left-content-padding').style.height = columnHeight + "px";
	document.getElementById('main-right-column').style.height = columnHeight + "px";
	document.getElementById('main-right-content-padding').style.height = columnHeight + "px";
}

function setMarketSelectorGlobeHeight(marketSelectorHeight){
	document.getElementById('market-selector-globe').style.height = marketSelectorHeight + "px";
}

function setMarketSelectorMapHeight(marketSelectorHeight){
	document.getElementById('market-selector-map').style.height = marketSelectorHeight + "px";
}

// Taken from: http://blog.stevenlevithan.com/archives/faster-trim-javascript
function trim(str){
	if(str != null){
		var str = str.replace(/^\s\s*/, ''), ws = /\s/, i = str.length;
		while (ws.test(str.charAt(--i)));
		return str.slice(0, i + 1);
	}
	return null;
}

function openMarketSelector(){
	renderFlashMarketSelector();
	var totalHeight = Math.max(getMainContentHeight() + 406, getInnerHeight() - 90, 800);
	document.getElementById('market-selector-globe').style.height = totalHeight + "px";
	correctColumnHeight();
}

function renderFlashMarketSelector(){
	document.getElementById('market-selector-globe').style.display='block';
	document.getElementById('market-selector-globe').innerHTML = "<div class='flash'><object width='882' height='800' data='http://vs796.ims-firmen.de/marketselector/preloader_globus_v2.swf' type='application/x-shockwave-flash'><param name='movie' value='http://vs796.ims-firmen.de/marketselector/preloader_globus_v2.swf' /><param name='bgcolor' value='#000000' /><param name='wmode' value='opaque' /><param name='allowScriptAccess' value='always' /></object></div>";
}

function closeMarketselector(){
	document.getElementById('market-selector-globe').style.display = 'none';
	document.getElementById('market-selector-globe').innerHTML = '';
}

function createScriptTag(fileName){
	var element = document.createElement("script");
	element.src = fileName;
	document.body.appendChild(element);
}

function hideSubmenu(){ // TODO check if menu3 and menu4 are needed, too
	if(document.getElementById('menu2')){
		document.getElementById('menu2').style.display = 'none';
		document.getElementById('bgmenu').style.display = 'none';
	}
}

function showSubmenu(){
	if(document.getElementById('menu2')){
		document.getElementById('menu2').style.display = 'block';
		document.getElementById('bgmenu').style.display = 'block';
	}
}

function hasCookiesEnabled(){
	return getCookie("KX_CMS_SID") != null;
}

function getCookie(name){
	var key = name + "=";
	for(i = 0; i < document.cookie.length; i++){
		if(cookieContainsAt(key, i))
			return getValueAt(key, i);
	}
	return null;
}

function cookieContainsAt(text, index){
	return document.cookie.substring(index, index + text.length) == text;
}

function getValueAt(key, index){
	var end = document.cookie.indexOf(";", index + key.length);
	end = (end > -1)? end : document.cookie.length;
	var cook = document.cookie.substring(index + key.length, end);
	return unescape(cook);
}

// BELOW FUNCTIONS ARE CANDIDATES FOR DEFFERED LOADING
function resetLoginBox(){
	closeLoginBox();
	showOpenLoginBoxButton();
}

function showCloseLoginBoxButton(){
	document.getElementById('open-login-box-button').style.display = 'none';
	document.getElementById('close-login-box-button').style.display = 'block';
}

function showOpenLoginBoxButton(){
	document.getElementById('open-login-box-button').style.display = 'block';
	document.getElementById('close-login-box-button').style.display = 'none';
}

function showLoginBox(){
	document.getElementById('loginbox').style.display = 'block';
	document.forms.formlogin.reset();
}

function hideLoginBox(){
	document.getElementById('loginbox').style.display = 'none';
}

function showMarketSelector(){
	if(isFlashInstalled())
		showFlashMarketSelector();
	else
		showClickMapMarketSelector();
}

function showFlashMarketSelector(){
	openMarketSelector();
}

function showClickMapMarketSelector(){
	preloadClickMap();
	showClickMap();
	correctMarketSelectorMapAndColumnHeight();
}

function clearSearchField(){
	setSearchField('');
}

function fillSearchField(text){
	if(isSearchFieldEmpty())
		setSearchField(text);
}

function isSearchFieldEmpty(){
	return getSearchField().value == '';
}

function getSearchField(){
	return document.getElementById('search-textfield');
}

function setSearchField(text){
	getSearchField().value = text;
}

function menuStatus(zeiger){
	if(zeiger == "auf")
		document.getElementById('submenu').style.display = "block";
	if(zeiger == "zu")
		document.getElementById('submenu').style.display = "none";
}

function checkformular(formname, elements){
	if(elements.length > 0 && formname != null){
		var form = document.getElementById('form_'+formname);
		for(var i=0;i<elements.length;i++){
			var element = document.getElementById(elements[i]+'_'+formname);
			if(element == null || element.value == "" || element.value == null || !element.value.length){
				element.style.border = "1px solid #ff0000";
				return false;
			}
		}
		return true;
	}
}

