/* --- JavaScript --- */
/* --- Passes --- */

var sCurrency = "";




var sQueryCurr = "";
var sCookieCurr = "";
var sIPcurr = "";
var sDefaultCurr = "eur";


if (sQueryCurr.length > 0) {

  sCurrency = sQueryCurr;
}
else if (sCookieCurr.length > 0) {
  sCurrency = sCookieCurr;
}
else if (sIPcurr.length > 0) {
  sCurrency = sIPcurr;
}
else {
  sCurrency = sDefaultCurr;
}


if (sCurrency!=null || sCurrency!='' || typeof(sCurrency)!='undefined') {
  createCookie("currency", sCurrency, 365);
  if (sQueryCurr.length == 0) redirect (sCurrency);
}

function redirect (currency) {
	/* if cookie  exists*/
	if ((typeof(currency) != 'undefined')) {	
		fullURL = parent.document.URL;
		fullURL = fullURL.substring(0,fullURL.indexOf('?'));
		//if (fullURL.indexOf("manager.dws")<0) location.href=fullURL+'?currency='+currency;
		if (fullURL.indexOf("manager.dws")<0) 
		{
			if (fullURL.indexOf("currency")<0) 
			{
				window.location.replace(fullURL+'?currency='+currency);
			}
		}
	}
}

Array.prototype.getUniqueValues = function () {
	var hash = new Object();
	for (j=0;j<this.length;j++) {
		hash[this[j]] = true
	}
	var array = new Array();
	for (value in hash) {
		array.push(value)
	};
	return array;
}

initCurrencySwitch = function() {
	var passes,selectSwitch;
	/* currency change set new cookie */
	var qCur = getQueryVariable('currency');
	if (qCur!='' && typeof(qCur)!='undefined') {
		eraseCookie('currency');
		createCookie('currency',qCur,365);
	}
	if (!(passes = document.getElementById('setCurrency')))
	{	
		if (!(passes = document.getElementById('passes'))) return false;
	}
	if (!(selectSwitch = passes.getElementsByTagName('select'))) return false;
	for (s=0; s<selectSwitch.length; s++) {
		selectSwitch[s].onchange = function() {
			this.form.submit();	
			document.location.href = parent.document.URL.substring(0,parent.document.URL.indexOf('?'))+'?currency='+this.value;			
		}
	}
}

/* Cookie functions */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

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 'usd';
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
}

domReady( function() {

	if ( !languageObj ) languageObj = {};
	
	if ( cssSupport() ) {
		initCurrencySwitch();
	}
} );