/* localized strings in strings_LOCALE.js */

function openwin(url, name, feat){
	win = window.open(url, name, feat);
	win.focus();
}

function FASH_chkSize(){
	if( document.form.Size.selectedIndex == 0 ){
		alert( ALERT1 );
		return false;  
	}	else return true;
}	

function focusTextfield(){
	document.K19Form.cn.focus();
}


  function popup(breite,hoehe,name) // popup-Funktion mit leicht reduziertem Browsermenu
  {
   fenster=window.open('',name,'width=' + breite + ',height=' + hoehe +',resizable=yes,scrollbars=yes,toolbar=no,status=no,menubar=yes,location=yes');
    fenster.focus();
  }

  function popup0(breite,hoehe,name) // popup-Funktion mit fast komplettem Browsermenu
  {
   fenster=window.open('',name,'width=' + breite + ',height=' + hoehe +',resizable=yes,scrollbars=yes,toolbar=yes,status=no,menubar=yes,location=yes');
    fenster.focus();
  }

	function popup1(breite,hoehe,name,url) // popup-Funktion mit allen Browsermenus weggenommen und ohne Scrollbalken
  {
   if (url==null) url='';
   fenster=window.open(url,name,'width=' + breite + ',height=' + hoehe +',resizable=no,scrollbars=no,toolbar=no,status=no,menubar=no,location=no');
   fenster.focus();
  }

  function popup2(breite,hoehe,name,url) // popup-Funktion mit allen Browsermenus weggenommen
  {
   if (url==null) url='';
   fenster=window.open(url,name,'width=' + breite + ',height=' + hoehe +',resizable=yes,scrollbars=yes,toolbar=no,status=no,menubar=no,location=no');
   fenster.focus();
  }
  
  function popup3(breite,hoehe,name,url) // popup-Funktion mit allen Browsermenus weggenommen
  {
   var x=screen.width/2-300;
   var y=screen.height/2-300;
   if (url==null) url='';
   fenster=window.open(url,name,'width=' + breite + ',height=' + hoehe +',screenX='+x+',screenY='+y+',resizable=yes,scrollbars=yes,toolbar=no,status=no,menubar=no,location=no');
   fenster.focus();
  }

	function popup4(breite,hoehe,name,url) // popup-Funktion mit allen Browsermenus weggenommen und ohne Scrollbalken
	  {
	   if (url==null) url='';
	   fenster=window.open(url,name,'width=' + breite + ',height=' + hoehe +',resizable=yes,scrollbars=yes,toolbar=yes,status=yes,menubar=yes,location=yes');
	   fenster.focus();
	  }
  
  function popunder(from)
  {
	url="k19_popunder.do";
	if(from != 'null') url=url+"?from="+from;	
    var reg = window.open(url,'regist','width=790,height=605,resizable=yes,scrollbars=yes,toolbar=no,status=no,menubar=no,location=no');
  	window.open('','buffer').close();
	  reg.blur();
	  window.focus();
  }
  
  function fromStandalonePopup()
  {
	//self.close();
	var w=screen.width;
	var h=screen.height-40;
	self.moveTo(0,0);
	self.resizeTo(w,h);
	
	//window.open(url,'GAMEDUELL','width='+w+',height='+h+',resizable=yes,scrollbars=yes,toolbar=yes,status=yes,menubar=yes,location=yes,personalbar=yes');
	
  }
  

  
  function fromStandalonePopup3(url)
  {
	
	var w=screen.width;
	var h=screen.height-40;
	
	var next=window.open(url,'GAMEDUELL','width='+w+',height='+h+',resizable=yes,scrollbars=yes,toolbar=yes,status=yes,menubar=yes,location=yes,personalbar=yes');
	self.setTimeout("self.close()",15000);
  }
	
	function proceedToGD(url) {
	var x = screen.availWidth;
	var y = screen.availHeight;
	var gdWindow = window.open(url,"","toolbar=yes, location=yes, status=yes, menubar=yes, personalbar=yes, width=x, height=y, scrollbars=yes, resizable=yes");
	}

  function playWin(url) {
   fenster=window.open(url,'','width=780,height=480,resizable=no,scrollbars=no,toolbar=no,status=no,menubar=no,location=no');
   fenster.focus();
  }

  function infoClose(){
	self.close();
  }

  function profil(url) {
		if (url==null) url='';
    var breite=600;
    var hoehe=410;
    fenster=window.open(url,'Profil','width=' + breite + ',height=' + hoehe +',resizable=no,scrollbars=no,toolbar=no,status=no,menubar=no,location=no');
    fenster.focus();
  }

  //can be removed when new s03.jsp is ready, s11InternalChallenge.jsp should also be reworked in line with
  function setGewinn(players, obj, euroMultiplicator, decimalSeparator) { //  calculates the reward amount of non jackpot tournaments, used in conjunction with amount select-pulldowns in s03 for example.
  	var PROV=25;
  	var einsatz = obj.value ;
	var rawPool = players * einsatz ;
	var pool= ((100.0 - PROV) * rawPool) / 100.0;
	var g = (einsatz*players - (PROV * rawPool) / 100.0) * euroMultiplicator ;
	g = Math.round(g * 1000) / 1000;
	var res=""+g;
	if (euroMultiplicator == 1){
		if (res.indexOf(".")<0 ) res+=".00";
			res=res.replace(/\./,decimalSeparator) ;
		var splitter=res.split(decimalSeparator);
		if ( splitter[1].length<2 ) res+="0";
		if ( splitter[1].length>2 ) {
 	        splitter[1] = splitter[1].substring(0,2);
            res = splitter[0] + decimalSeparator +  splitter[1];
        }
    }
    else {
    	if (res.indexOf(".")>0 ){
    		res=res.replace(/\./,decimalSeparator) ;
			var splitter=res.split(decimalSeparator);
			if ( splitter[1].length<2 ) res+="0";
    	}
    }

	document.getElementById("gewinn").firstChild.nodeValue = res;
  }

  function setReward(players, obj, euroMultiplicator, decimalSeparator) { //  calculates the reward amount of non jackpot tournaments, used in conjunction with amount select-pulldowns in s03 for example.
  	var PROV=25;
  	var einsatz = obj.value;
	var rawPool = players * einsatz;
	var pool= ((100.0 - PROV) * rawPool) / 100.0;
	var g = (einsatz*players - (PROV * rawPool) / 100.0) * euroMultiplicator;
	g = Math.round(g * 1000) / 1000;
	var res=""+g;
	
	if (euroMultiplicator == 1){
		if (res.indexOf(".")<0 ) res+=".00";
		res=res.replace(/\./,decimalSeparator) ;
		var splitter=res.split(decimalSeparator);
		if ( splitter[1].length<2 ) res+="0";
		if ( splitter[1].length>2 ) {
	         splitter[1] = splitter[1].substring(0,2);
	         res = splitter[0] + decimalSeparator +  splitter[1];
	    }
	}
	else {
    	if (res.indexOf(".")>0 ){
    		res=res.replace(/\./,decimalSeparator) ;
			var splitter=res.split(decimalSeparator);
			if ( splitter[1].length<2 ) res+="0";
    	}
    }

	document.getElementById("reward" + players).firstChild.nodeValue = res;
  }

  function setFN(obj) {
  	document.getElementById("fn").firstChild.nodeValue = obj.value ;
  }
	
	function focusOnForm() {
		if (document.K19Form !=null && document.K19Form.cn != null) 
			{document.K19Form.cn.focus()}
	  else if (document.K23Form != null && document.K23Form.cn != null) 
			{document.K23Form.cn.focus()}
	  else if (document.k44 !=null && document.k44.recipient != null)
		  {document.k44.recipient.focus()}
	  else if (document.K23Form != null && document.K23Form.passwd != null) 
			{document.K23Form.passwd.focus()}
	}

	// Controls the input of a textarea by limiting it to a given length
	var formfeld="";
	function setTextCount(obj, customlength) { //  text counter
		var maxlength=customlength;
		var res=obj.value.length;
		if (res>maxlength)	{
			obj.value=formfeld;
			return;
		} else {
			formfeld=obj.value;
			res=maxlength-res;
		}
		// Show the user the available characters
		document.getElementById("textcount").firstChild.nodeValue = res ;
	}
			
	function showForSee(id) {
		fsTimer = setTimeout('document.getElementById("'+id+'").style.display = "block"', 750);
	}

	function hideForSee(id) {
		clearTimeout(fsTimer);
		setTimeout('document.getElementById("'+id+'").style.display = "none"', 300);
	}
	
	function setCoPartnerCookie(partnerStr) {
		ex= new Date;
		// 1month partner cookie timeout
		// 30d * 24h * 60min * 60sec * 1000millies
		ex.setTime(ex.getTime()+(30 * 24 * 60 * 60 * 1000));
		domain= location.hostname;
		dotIndex= domain.indexOf('.');
		if (dotIndex>=0)  // to get domain cookie, cut first part of server name
				domain=domain.substring(dotIndex);
		document.cookie="partner=" + partnerStr
		 + "; Expires=" + ex.toGMTString()+ "; domain="+domain + "; path=/";
	}

	
//Creates a correct money value by getting a double value
function getMoneyValue(value,currency, withFractions, decimalSeparator){

	var amountValue=Math.round(value*100)/100 +'';
	
	if (withFractions == true){
		if (amountValue.indexOf(".")<0 ) amountValue+=".00";
		amountValue=amountValue.replace(/\./,decimalSeparator) ;
		var splitter=amountValue.split(decimalSeparator);
		if ( splitter[1].length<2 ) amountValue+="0";
		if ( splitter[1].length>2 ) {
			splitter[1] = splitter[1].substring(0,2);
			amountValue = splitter[0] + decimalSeparator +  splitter[1];
		}
	}
	return amountValue+" "+currency;
}
	
	
// Remember selected amount 
function saveAmount() {
	document.S37LeagueCreateForm.amountIndex.value = document.S37LeagueCreateForm.amount.selectedIndex;
	//alert("Saving " +  document.S37LeagueCreateForm.amountIndex.value) ;	
}


//Creates a list of amount- or acount-options in a selection-box
function setAmountOrAccountOptions(maxAmount,euroMultiplicator,currency,decimalSeparator, useAmountOptions) {
	if (useAmountOptions) {
		var index = document.S37LeagueCreateForm.amountIndex.value;
	}

	var selbox = document.S37LeagueCreateForm.amountOrAccount;
	selbox.options.length=0;
	
	var firstAmount = roundMoney ( (document.S37LeagueCreateForm.memberNumber.value-1)*0.4 );
	var counter=0;
	var tmpValue;
	
	var withFractions = (euroMultiplicator == 1) ? true : false;
	
	if(firstAmount%0.5!=0 && firstAmount!=4.4){ //4.4 cannot be selected.
		if (useAmountOptions) {
			tmpValue = firstAmount;
		} else {
			tmpValue = roundMoney(firstAmount*document.S37LeagueCreateForm.memberNumber.value*0.75);
		}
		//the euroMultiplicator is just for view purpose
		selbox.options[0] = new Option(getMoneyValue(tmpValue*euroMultiplicator,currency,withFractions,decimalSeparator),firstAmount);
		counter=1;
	}
	
	
	// var maxAmount=5;
	for(i=0;i<=maxAmount;i+=0.5){
		
		if(firstAmount<=i){
			if (useAmountOptions) {
				tmpValue = i;
			} else {
				tmpValue = roundMoney(i*document.S37LeagueCreateForm.memberNumber.value*0.75);
			}
			selbox.options[counter] = new Option(getMoneyValue(tmpValue*euroMultiplicator,currency,withFractions,decimalSeparator),roundMoney(i));
			counter++;
		}
	}

	if (useAmountOptions && ( index < selbox.options.length)) {
		// set remembered amount
		selbox.selectedIndex = index;
	} else {
		// set remembered account
		selbox.selectedIndex = 0;
	}
	setLeagueRewardOrFee(euroMultiplicator, currency, decimalSeparator, useAmountOptions);
}

//Round to the nearest cent
function roundMoney(money) {
	var res=""+( Math.round(money*100) ) / 100;
	if (res.indexOf(".")<0 ) res+=".00";
	return res;
}

//Sets Reward or Fee in League-Tournaments
function setLeagueRewardOrFee(euroMultiplicator,currency,decimalSeparator,chooseAmounts){
	var withFractions = (euroMultiplicator == 1) ? true : false;
	var res;

	if (document.S37LeagueCreateForm.amountOrAccount.value) {
	    if (chooseAmounts) {
		res= roundMoney(document.S37LeagueCreateForm.amountOrAccount.value*document.S37LeagueCreateForm.memberNumber.value*0.75);
	    } else {
		res= roundMoney(document.S37LeagueCreateForm.amountOrAccount.value);
	    }
	    document.getElementById("leagueRewardOrFee").firstChild.nodeValue=getMoneyValue(res*euroMultiplicator,currency,withFractions,decimalSeparator);
        }
}

//Sets the day of selected date
function setDate(){
	var dateString=document.S37LeagueCreateForm.endDate.value;
	var splitter=dateString.split(".");
	var date=new Date(splitter[2],splitter[1],splitter[0]);
	var dayOfWeek=new Array(SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY);
	document.getElementById("dayOfWeek").firstChild.nodeValue=dayOfWeek[date.getDay()];
	document.getElementById("year").firstChild.nodeValue=splitter[2];
}

function setTimeToEnd(currentDate){
	var selectedDateString=document.S37LeagueCreateForm.endDate.value;
	var splitter=selectedDateString.split(".");
	var selectedHour=document.S37LeagueCreateForm.endHour.value;
	var selectedDate=new Date(splitter[2],splitter[1],splitter[0],selectedHour,0,0);
	var allMinutesLeft=(selectedDate.getTime()-currentDate.getTime())/1000/60;
	var allHoursLeft=(allMinutesLeft/60);
	var hoursLeft=(allHoursLeft+"").substring(0,(allHoursLeft+"").indexOf("."));
	var minutesLeft=allMinutesLeft%60;
	var timeToEndString=LEFT+" "+hoursLeft+" "+HOURS+" "+AND+" "+minutesLeft+" "+MINUTES;
	if(allHoursLeft>120) {
		timeToEndString=ENDTIME_5DAYS;
		}
	else if(allHoursLeft<12) {
		timeToEndString=ENDTIME_12HOURS;
		}
	document.getElementById("timeToEnd").firstChild.nodeValue=timeToEndString;
}

// Selects the private league radio button if the text field for entering new players 
// gets foucs
function selectPrivateRadio() {
	document.getElementById("private").checked=true;
}


function repopulateEndHour() {
	var selectedDateString=document.S37LeagueCreateForm.endDate.value;
	var splitter=selectedDateString.split(".");
	// populate hours drop down list so that no illegal values can be selected
	// Work out max possible hours 
	var maxDate=new Date(splitter[2],splitter[1],splitter[0],0,0,0); // Midnight of the selected day
	var maxHours = Math.ceil(23 + (maxDate.getTime()-currentDate.getTime())/1000/60/60); // Add 23 to get maxHours
	if (maxHours > 120) {
//		alert("maxHours = " + maxHours);
		var newEndHour = currentDate.getHours();
		populateEndHour(0,newEndHour);
	} else
		populateEndHour(0,23);
}


function populateEndHour(begin, end) {
 	var selbox = document.S37LeagueCreateForm.endHour;
	selbox.options.length = 0;	// Scrubs all entries from selbox
	if ( (begin < 0) || (begin > 23) )
		begin = 0;
	if ( (end < begin) || (end > 23) )
		end = 23;
	var i;
	for (i=begin;i<=end;i++) {	
 		selbox.options[selbox.options.length] = new Option(i+":00",i);
 	}	
 	// Select an entry 12 hours in the future or last entry
 	var hours = currentDate.getHours() + 13;
 	if ( hours >= 24)
 		hours = hours - 24;
	// 	alert("No options = " + selbox.options.length);
	if (hours > selbox.options.length-1)
		selbox.selectedIndex = selbox.options.length-1;
	else
	 	selbox.selectedIndex = hours;
	setTimeToEnd(currentDate)		
}

var first = "true";

function changeIcon(mediaDirectory) {
	var game = document.S37LeagueCreateForm.gameType.value;
	var iconPath = mediaDirectory + "/" + LANGUAGE_PREFIX + "/website/images-games/" + game + "/" + game + "_logo_kl_txt.gif";
	var nfrBold = "";
	var nfrFirstRed="";
	var nfrRedLink="";
	var nfrRedLinkHref="";
	var nfrSecondRed="";
	var nfrFirstBlack="";
	var nfrBlackLink="";
	var nfrSecondBlack="";
	if(game.indexOf("nfr")>0){
		iconPath=mediaDirectory + "/" + LANGUAGE_PREFIX + "/website/icons/ic_21x21_error.gif";
		nfrBold = NEWBIES;
		nfrFirstRed=TOO_LITTLE;
		nfrRedLink=DUELS.replace(/\$GAMENAME\$/,document.getElementById(game).firstChild.nodeValue);
		nfrRedLinkHref="/gd/s03.do?gametype="+game.substring(0,game.indexOf("nfr"));
		nfrSecondRed=OPEN_MINILEAGUE.replace(/\$GAMENAME\$/,document.getElementById(game).firstChild.nodeValue);
		nfrFirstBlack=YOU_FIND;
		nfrBlackLink=CONDITIONS;
		nfrSecondBlack=FOR_MINILEAGUES;
	}
	document.getElementById("nfrBold").firstChild.nodeValue=nfrBold;
	document.getElementById("nfrFirstRed").firstChild.nodeValue=nfrFirstRed;
	document.getElementById("nfrRedLink").firstChild.nodeValue=nfrRedLink;
	var href=document.createAttribute("Href");
	href.nodeValue=nfrRedLinkHref;
	document.getElementById("nfrRedLink").setAttributeNode(href);
	document.getElementById("nfrSecondRed").firstChild.nodeValue=nfrSecondRed;
	document.getElementById("nfrFirstBlack").firstChild.nodeValue=nfrFirstBlack;
	document.getElementById("nfrBlackLink").firstChild.nodeValue=nfrBlackLink;
	document.getElementById("nfrSecondBlack").firstChild.nodeValue=nfrSecondBlack;

	/* We need to show a blank image if "Please Select is selected"
	 * since a PleaseSelect_logo_kl_txt.gif does not exist.
	 * Method String.equalsIgnoreCase() is defined below !
	 */
	if(!game.equalsIgnoreCase("PleaseSelect")){
		document.gameIcon.src=iconPath;
	} else{
		document.gameIcon.src=mediaDirectory + "/" + "img/x.gif";
	}

	if (first == "false" && document.getElementById("noGame") && document.getElementById("noGame2")) {
		document.getElementById("noGame").style.display='none';
		document.getElementById("noGame2").style.display='none';
	} else {
		first = "false";
	}
}
/*add new Method to all instances of String*/
String.prototype.equalsIgnoreCase=gdEqualsIgnoreCase;
/**
  * Compares current String with another String ignoring the case
  * @param arg The String to compare.
  */
function gdEqualsIgnoreCase(arg){

	if((new String(this.toLowerCase())==(new String(arg)).toLowerCase())){
		return true;
	} else{
		return false;
	}


}

  function openNewStandardWindow(url)
  {
	NewWindow = window.open(url,'','resizable=yes,scrollbars=yes,toolbar=yes,status=yes,menubar=yes,location=yes,personalbar=yes');
	NewWindow.focus();
  }

// close the actual window and send the parent to "URL"
// if no parent window exists or it is not GameDuell, a new window is opened
function sendParentAndClose(URL) {
	if (opener!=null){
		try {
			if (opener.name=="GAMEDUELL"){
		 		opener.location.href=URL;
		 		opener.focus();
	 			self.close();
			}else{
				openNewStandardWindow(URL);
			}
		} catch (e){
			openNewStandardWindow(URL);
		}
 	}else{
 		openNewStandardWindow(URL);
 	}
 	
}

