﻿/*JS-VERSION:0.0.1.19*/
function doSearch() 
{
    // debugger;
    var language = readCookie(cookieLanguageName);
    var searchString = $('ctl00_PlaceHolderSearchArea_searchBox').value;
    var uri = "/web/"+language.toUpperCase()+"/Pages/searchresults.aspx?s="+language+"&k="+searchString;
    var popup = window.open(uri, "searchwindow", "width=448,height=450");
	popup.focus();
	return false;
}
/*rosh:13.03.07: retrieve an element based on markup id - crossbrowser*/
function getElement(o){return document.all?document.all[o]:document.getElementById(o);}
/*rosh:13.03.07: toggles a div based on markup id - crossbrowser; to hide an element per default the div need to set: style='display:none'*/
function toggleDIV(v){var o = getElement(v);if(o.style){if(o.style.display != "none") o.style.display = "none"; else o.style.display = "";}}
/*xolh:13.03.07: toggles a Plus/Minus-CSS-Class on markup id - crossbrowser; to toggle an element per default the div need to set: class='plus'*/
function togglePlus(v){var o = getElement(v);if(o)      {if(o.className != "minus") o.className = "minus"; else o.className = "plus";}}
/* rosh:16.03.07:
function setDropDownValue(o,v) o:object v:value
selects the requested value in a dropdown list.
ex. setDropDownValue(document.getElementById('iLang'),'yourValue');*/
function setDropDownValue(o,v){if(!o) return; for (var i=0; i < o.options.length;++i){if (o.options[i].value == v) o.options[i].selected = true;}}
/*rosh:16.03.07: test if the user enables popups*/
function popUpTest(){
	if(popupChecked == true)
		return popupChecked;
	var success = false;
	var result = window.open("/_layouts/1033/popuptest.html", "popped", "left=1000,top=20, width=10, height=10, location=no, menubar=no, status=no, toolbar=no, scrollbars=no, resizable=no");
	window.focus();
	if (result != null) 
	{
		success = true;
		popupChecked = true;
		result.close();
	}
	else {success = false;}
	return success;
}
/* ROSH: 20.03.07: Cookie handle create */
function createCookie(name,value,days) {
	if(value.toUpperCase() == 'ES' && name == cookieLanguageName)
	{
		// alert('Lang in es');
		return;
	}
		
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/"; //domain=" + cookieDomain;
}
/* ROSH: 20.03.07: Cookie handle read */
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
/*rosh:13.03.07: restore markup form default data*/
function submitFormRestore(){
	$('aspnetForm').method		= formSubmitMethod;
	$('aspnetForm').action		= formSubmitAction;
	$('aspnetForm').onsubmit	= formSubmitOnSubmit;
}
/*rosh:13.03.07: set args as markup form parameters*/
function submitFormEdit(action, onsubmit, method)
{
	$('aspnetForm').method		= method;
	$('aspnetForm').action		= action;
	$('aspnetForm').onsubmit	= onsubmit;
}
/*
ROSH: 23.03.07
function replace(s, t, u)
Replace a token in a string
s:string to be processed
t:token to be found and removed
u:token to be inserted
returns new String
*/
function replace(s, t, u){i = s.indexOf(t);r = "";if (i == -1) return s;r += s.substring(0,i) + u;if ( i + t.length < s.length) r += replace(s.substring(i + t.length, s.length), t, u);return r;}

/*FLMO: 27.03.07 functions to setting css styles for active/deactive menu items*/
var navActive = false;

		function setActiveNav(item) {
			var nav = getElement('nav'+item);
			var counter = parseInt(item);
			if(nav) {
				if(counter == 0) {
					nav.className = "firstLi hi";
				}
				else
					nav.className = "hi";
			}
		}
		
		function setInactiveNav(item) {
			var nav = getElement('nav'+item);
			var counter = parseInt(item);
			if(nav) {
				if(counter == 0){
					nav.className = "firstLi";
				}
				else
					nav.className = "";
			}
		}
		
		function compareStr(compareValue, item) {
			var docLoc = "";
			docLoc = document.location.href;
            if (docLoc.indexOf(compareValue)!=-1) {
				setActiveNav(item);
				navActive = true;
			}
		    else {
			   setInactiveNav(item);
		    }	
		}
		//function called in Masterpage body onload
		function loadTopNavClass() {

		}
		
	/* FLMO: 13.04.07 functions for enabling window.open() function via cms */	
	function enablePopup(){
        a = document.getElementsByTagName("a");
        
        for (i=0; i<a.length; i++)
        {
            var id = a[i].getAttribute("id");
            if (id && id.indexOf("popup") != -1 && id.length > 5 && id.indexOf("x")){
                var x = id.substring(5);
                var size = x.split("x");
                var width = size[0];
                var height = size[1];
                a[i].onclick = function(){
                
                url = this.getAttribute("href");
                window.open(url,id,'height=' + height + ',width=' + width + ',status=no,scrollbars=yes,toolbar=no,menubar=no,location=no,titlebar=no,resizable=yes');
                return false;
                }
            }
            else if (id == "popup"){
                a[i].onclick = function(){
                
                url = this.getAttribute("href");
                window.open(url,'popup','height=200,width=400,status=no,scrollbars=yes,toolbar=no,menubar=no,location=no,titlebar=no,resizable=yes');
                return false;
                }
            }
        }
     }
     /*FLMO Function for printing the page */
     function varitext(text) {
        text=document
        print(text)
     }
     /*FLMO Function for forwarding page */
    function setForwardLink() {
	    var url = document.location.href;
	    var title = document.getElementsByTagName("title")[0].text;
	        if(title != null){
			    var link = "mailto:?subject=" + escape(title) + "&body=" + url;
			    var linkObject = getElement("forward");
			    linkObject = linkObject.getAttributeNode("href");
			    linkObject.nodeValue = link;
	        }
}
	/* End FLMO functions */
	
/*ROSH: 12.04.07 extract javascript exception details*/
function errorCatchHandler(ex)
{
	var err = 'name: ' + ex.name + '\n';
	err += 'message: ' + ex.message + '\n';
	
	if (ex instanceof TypeError)
	{
		err += 'info: ' + "Variable type problem, check your variable definitions!" + '\n';
	}
	else if (ex instanceof RangeError)
	{
		err += 'info: ' + "Number out of range!" + '\n';
	}
	else if (ex instanceof SyntaxError)
	{
		err += 'info: ' + "Syntax error in code!" + '\n';
	}
	else
	{
		err += 'info: ' + "An unspecified error occurred!" + '\n';
	}
	return err;
}

//FLMO 26.04.07 added functions from main.js
/* Popup 2 cols. Used in 601_personal_details.html to open popup_address popup */
function openPop_address(uri){
	popup = window.open(uri, "popup", "width=448,height=450");
	popup.focus();
	return false;
}

/* red plus */
function rp_toggleHi(elm){
	if(elm.parentNode.className == 'hi')
		elm.parentNode.className = '';
	else
		elm.parentNode.className = 'hi';
	return false;
}


/*ROSH 18.04.07: receive a number between 0 - 9  and returns 0X */
function formatDezimalNumberTo2Dig(data)
{
	try
	{
		var dd = parseInt(data);
		if(dd>9)
			return dd;
		else
			return '0'+dd;
	}
	catch(ex)
	{
		return data;
	}
}

/*ROSH 14.04.06: creates a new window and set's focus */
function openWindow(url, setFocus)
{
	if(url)
	{
		var result = window.open(unescape(url));
		if(result && setFocus)
		{
			result.focus();
		}
	}
}


/*
function urlHandler(url, lang)
ex: urlHandler('http://swrbslcms01:8086/web/DE/Pages/index.aspx#', 'EN')
result: 'http://swrbslcms01:8086/web/EN/Pages/index.aspx#
url: the string (url) to proceed on it
lang: the new language which will be replaced
returns a new string which used as location.href of the current window.
*/	
function urlHandler(url, lang)
{

	var result = new String(url);
	var newLanguage = lang.toUpperCase();
	var cacheIssue = (new Date()).getTime();
	
	createCookie(cookieLanguageName,lang,365);
	result = result.replace("#","");	
		
	var localCountryFix = "/countries/";
	if(result.indexOf(localCountryFix, 0) != -1)
	{
		result = result.substring( 0, result.indexOf(localCountryFix, 0) );
		return result;
	}
	/*tem. save without addition of an country*/
	var tmpCacheIssue = cacheIssue;
	var newCountry = readCookie("Country");
	
	if(newCountry != 'undefined' && newCountry != null && newCountry != '')
	{
		cacheIssue += "&Country=" + newCountry;
	}	
	/*debugger;*/
	/*same language but differnt country*/
	if(result.indexOf('/EN/') != -1)
	{
		result = result.substring( 0, result.indexOf('?') == -1 ? result.length : result.indexOf('?') );
		return replace(result,'/EN/', '/'+newLanguage+'/') + '?' + cacheIssue;
	}
	else if(result.indexOf('/FR/') != -1)
	{
		result = result.substring( 0, result.indexOf('?') == -1 ? result.length : result.indexOf('?') );
		return replace(result,'/FR/', '/'+newLanguage+'/') + '?' + cacheIssue;
	}
	else if(result.indexOf('/DE/') != -1)
	{
		result = result.substring( 0, result.indexOf('?') == -1 ? result.length : result.indexOf('?') );
		return replace(result,'/DE/', '/'+newLanguage+'/') + '?' + cacheIssue;
	}
	else if(result.indexOf('/IT/') != -1)
	{
		result = result.substring( 0, result.indexOf('?') == -1 ? result.length : result.indexOf('?') );
		return replace(result,'/IT/', '/'+newLanguage+'/') + '?' + cacheIssue;
	}
	else
	{
		// http://swrbslsourcew02/Relaunch/bso.htm?market=ch&offer=ch07&tci=12313213213
		// http://swrbslsourcew02/Relaunch/bso.htm?12313213213
		if(result.indexOf('?') == -1)
		{ 
			return result + '?tci='+ tmpCacheIssue;
		}
		else
		{
			/*temporalry cache issue*/
			return result + '&tci=' + tmpCacheIssue;
		}
		return;
		/*
		return result.substring( 0, result.indexOf('?') == -1 ? result.length : result.indexOf('?') )  + '?' + cacheIssue;
		
		var other = null;
		if(result.indexOf('?') == -1)
		{ 
			sub = result;
		}
		else
		{
			sub = result.substring( 0, result.indexOf('?') == -1 ? result.length : result.indexOf('?') );
			other = result.substring( result.indexOf('?')+1 );
		}
		if(other) 
		{
			var keyValue = other.split("&");
			var newUriPara = sub + '?';
			var returnIntern = false;
			
			if(keyValue)
			{
				for(var i=0;i<keyValue.length;++i)
				{
					var keyPair = keyValue[i].split("=");
					if(keyPair)
					{
						if(keyPair[0]=='lang')
						{
							newUriPara += keyPair[0]+'='+beLang;
							returnIntern = true;
						}
						else
						{
							newUriPara += keyPair[0]+'='+keyPair[1];
						}
						
						if(i<keyValue.length-1)
							newUriPara += '&';
					}
				}
				if(returnIntern)
				{	
					return newUriPara;
				}
			}
		}
		*/
		
		
	}
}



/*ROSH 18.04.06: function reads all data from the query string and returns the data of the specifc key.*/
function PageQuery(q) {
if(q.length > 1) this.q = q.substring(1, q.length);
else this.q = null;
this.keyValuePairs = new Array();
if(q) {
for(var i=0; i < this.q.split("&").length; i++) {
this.keyValuePairs[i] = this.q.split("&")[i];
}
}
this.getKeyValuePairs = function() { return this.keyValuePairs; }
this.getValue = function(s) {
for(var j=0; j < this.keyValuePairs.length; j++) {
if(this.keyValuePairs[j].split("=")[0] == s)
return this.keyValuePairs[j].split("=")[1];
}
return false;
}
this.getParameters = function() {
var a = new Array(this.getLength());
for(var j=0; j < this.keyValuePairs.length; j++) {
a[j] = this.keyValuePairs[j].split("=")[0];
}
return a;
}
this.getLength = function() { return this.keyValuePairs.length; } 
}

/*ROSH 18.04.06: receiving a key from the current QueryString; sample call: alert(queryString(sessionID));*/
function queryString(key){
var page = new PageQuery(window.location.search); 
return unescape(page.getValue(key)); 
}

/*ROSH 19.04.06: function reads all data from the query string and returns the data of the specifc key.*/
function parseCookie(q)
{
	if(q && q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++){this.keyValuePairs[i] = this.q.split("&")[i];}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++){
			if(this.keyValuePairs[j].split("=")[0] == s)
			return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function(){
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++){
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; }
}

/*ROSH 19.04.06: receiving a key from the current cookie; sample call: getCookieValue('Youths',readCookie(name));*/
function getCookieValue(key,win){
	var page = new parseCookie(win);
	return unescape(page.getValue(key));
}

/*ROSH:20.04.07: post-load images for presenter*/
function applyPicture(picId)
{
	for(var n=1;n<imagePresenterArray.length;++n)
	{
		if(n==picId)
			if($('presenterImage'+n))
				$('presenterImage'+n).src = imagePresenterArray[n];
	}	
}

/*rosh:17.04.2007: retrieve the selected element id*/
function getRadioSelection(eleId)
{
	var selection = document.getElementsByName(eleId);
	var selected = '';
	for (i=0; i<selection.length; i++)
		if (selection[i].checked == true)
		{
			selected = selection[i].id;
		}
				
	return selected;
}



/*rosh:20.06.2007: define pre-defined elements to validate date input fields on start page*/
var digits = "0123456789";
var delimiters = ".,-/";
var maxLength = 8;

function isInteger(s)
{   
	var i;
  for (i = 0; i < s.length; i++)
  {   
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) 
		{
			return false;
		}
  }
  return true;
}
/*rosh:20.06.2007: strips out any chars, exceptions defind in array [digits]*/
function stripCharsInBag(s, bag)
{   
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++)
	{   
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) 
		{
			returnString += c;
		}
	}
	return returnString;
}
/*rosh:20.06.2007: main validation functionlity*/
function inputValidationDate(str)
{
	var s=stripCharsInBag(str,delimiters);
	return (isInteger(s) && s.length >= maxLength);
}
/*rosh:20.06.2007: formats date input*/
function formatInput(str)
{
	var formatted = new String();
	for (i = 0; i < str.length; i++)
  {   
		var c = str.charAt(i);
		if ( ((c >= "0") && (c <= "9")) || (delimiters.indexOf(c) != -1) ) 
		{
			formatted += c;
		}
  }
  return formatted;
}
/*rosh:20.06.2007: find pressed keycode from ASCII*/
function kc(e)
{
	var keycode;

	if (window.event)
		keycode = window.event.keyCode;

	else if (e)
		keycode = e.which;

	else
		return true;
	
	/* Numbers */
	if (47 < keycode && keycode < 58)
		return true;

	/* control chars [added 37,38,39,40] */
	var kcok = new Array(8, 10, 13, 46, 0, 37, 38, 39, 40);
	while (kcok.length > 0)
	{
		if (keycode == kcok.pop())
			return true;
	}

	return false;
}

/*rosh:20.06.2007: retrive pressed keycode*/
function getKeyCode(e)
{
	var keycode;

	if (window.event)
	{
		keycode = window.event.keyCode;
	}
	else
	{
		keycode = e.which;
	}
	return keycode;
}
/*rosh:20.06.2007: does some basic input checks */
function check4Numbers(ele, eve)
{
	ele.focus();
	
	var kcok = new Array(8, 10, 13, 46, 0, 37, 38, 39, 40);
	var kc = getKeyCode(eve);
	var contains = false;
	
	for(var i=0;i<kcok.length;++i)
	{
		if(kc == kcok[i])
		{
			contains = true;
			break;
		}	
	}
	
	if(contains)
		return true;
	else
		// debugger;

	if (inputValidationDate(ele.value)==false)
	{	
		ele.value = formatInput(ele.value);
		return true;
	}
	return true;
}

/*
input -> output
5.7.2007 -> 05.07.2007 
5-7-2007 -> 05-07-2007 
5,7,2007 -> 05,07,2007 
5/7/2007 -> 05/07/2007 
*/
function formatDateFormat(ele)
{
	var result='';
	/*loop over delimiter options*/
	for(var d=0;d<delimiters.length;++d)
	{
		var tmp = ele.value.split(delimiters.charAt(d));
		if(tmp.length == 1)
			continue;
		/*create from day and month formatted numbers, e.g. 3.4.2008 => 03.04.2008*/
		for (var i=0;i<tmp.length-1;++i)
		{	
			result += fillNumber(tmp[i], delimiters.charAt(d))
		}
		var tmpDate = new Date();
		if(tmp[tmp.length-1].length == 2)
		{
			tmpDate = new Date('20'+tmp[tmp.length-1], tmpDate.getMonth()+1, tmpDate.getDate());
			tmp[tmp.length-1] = tmpDate.getFullYear();
		}		
		var nextYear = parseInt(tmpDate.getFullYear()) + 1;
		
		if( tmp[tmp.length-1] == nextYear.toString() ) {
			result += tmp[tmp.length-1];
		}
		else{
			result += tmpDate.getFullYear(); /*tmp[tmp.length-1];*/
		}
		
		ele.value = result;
		break;
	}	
	if(ele.value == '')
	{
		setDefaultValue(ele);
	}
	return false;
}

function setDefaultValue(ele)
{
	var now = new Date();
		ele.value = 
			fillNumber(now.getDate()+1, delimiters.charAt(0))
			+
			fillNumber((now.getMonth()+1), delimiters.charAt(0))
			+
			now.getFullYear();
}

function fillNumber(val, del)
{
	var result = '';
	if( parseInt(val) > 0 && parseInt(val) <= 9  )
	{
		result = '0'+parseInt(val)+delimiters.charAt(0);
	}
	else 
	{
		result = val + delimiters.charAt(0);
	}
	return result;
}