var STARTMONEY=defaultBetamount/100;
STARTMONEY=STARTMONEY.toFixed(2);
var ERRORON=1;
var ERROROFF=0;


function BetTicketTop(id) {
var playable=false;
this.bets=null;
var TICKETWIDTH=215;
this.betCount=0;
this.currentBetMoney=2;
this.currentBetMoneySummary=0;
this.currentGameQuoteSummary=0;

this.currentGameType=0; //Single bet
this.betMoneyArray = new Array();
this.alreadyDrawn=false;
this.confirm=false;
var VALIDBETCOUNT=1;

var ERRORON=1;
var ERROROFF=0;

var JAVASCRIPTCORRECTION=0.0000000000001;

	this.checkTicket=function(nr) {	
		var amount;
		var flag=true;
		for(a=1;a<=this.betCount;a++) {
			if((this.readElementById("BETMONEYTICKET"+a)==null)||(this.readElementById("BETMONEYTICKET"+a)==undefined))
				return;
			amount=this.readElementById("BETMONEYTICKET"+a);
			if(amount.indexOf(',')>-1)
				amount=amount.replace(',','.');
			var result=''+(amount*amount);
			if(parseFloat(amount)>(topMaxamount/100)) {		
				this.setError(INVALID_BETAMOUNT,ERRORON);
				flag=false;
			} else if(parseFloat(amount)<(topMinamount/100)) {
				this.setError(INVALID_BETAMOUNT,ERRORON);
				flag=false;
			} else if(result=='NaN'){
				this.setError(INVALID_BETAMOUNT,ERRORON);
				flag=false;
			} else if(amount==''){
				this.setError(INVALID_BETAMOUNT,ERRORON);
				flag=false;
			}  
		}	
		if(flag==true) {
			this.setError(INVALID_BETAMOUNT,ERROROFF);	
		} 	
		this.renewTitle();
		this.renewError();		
		return flag;
	}

	
	this.getBetLine=function() {
		var betline = '<TABLE class="normalTextGrey" cellspacing="0" cellpadding="0" width="100%" border="0">';
		var actBet = this.bets;
		var betNumber = 1;
		var betColor = '#fff6b9';
	        while (actBet != null) {
	        	var tippList = actBet.tipp;        
			var cntIt = tippList.checkSelectedBets(tippList);
	        	for (a=0; a<cntIt; a++) {	
	        		if (tippList.selected == '1') {
					var headLine = actBet.getShortDescription();
					if (headLine == '')
						headLine = actBet.getBetEvent();
					betline=betline+'<TR>'+
						'<TD bgcolor="'+betColor+'">'+
							'<TABLE cellspacing="0" cellpadding="0" border="0" width="100%" style="margin-top: 16px">'+
								'<TR>'+
									'<TD class="normalTextGreyBold" style="padding-left: 8px" align="left">'+betNumber+'.&nbsp;'+headLine+'</TD>'+
									'<TD style="padding-right: 8px" align="right" valign="middle">';
									if (this.confirm == false)
										betline=betline+'<IMG style="cursor:pointer; cursor:hand;" onClick="handleDeleteBet(\''+actBet.gameNumber+'\',\''+tippList.tippNr+'\',\''+betNumber+'\');" src="'+IMAGEPATH+'trash.gif" width="11" height="11" hspace="2" border="0">';
									else
										betline=betline+'&nbsp;';
									betline=betline+'</TD>'+
								'</TR>' +
							'</TABLE>'+		
						'</TD>'+
					'</TR>'+
					'<TR>'+
						'<TD bgcolor="'+betColor+'">'+
							'<TABLE cellspacing="0" cellpadding="0" border="0" width="100%">'+
								'<TR>'+			
									'<TD class="normalTextGrey" style="padding-left: 8px" align="left" nowrap="nowrap">'+actBet.getTopBetType()+'</TD>'+
									'<TD class="normalTextGrey" style="padding-right: 8px" align="right">'+tippList.tipp+'</TD>'+
								'</TR>' +	
							'</TABLE>'+		
						'</TD>'+
					'</TR>'+
					'<TR>'+
						'<TD style="border-bottom: solid 1px #cfc997;" bgcolor="'+betColor+'">'+
							'<TABLE style="margin-bottom: 17px" cellspacing="0" cellpadding="0" border="0" width="100%">'+					
								'<TR>'+			
									'<TD class="normalTextGreyBold" style="padding-left: 8px; padding-top: 5px;" align="left">Quote:</TD>'+
									'<TD class="normalTextGreyBold" style="padding-right: 8px; padding-top: 5px;" align="right">'+formatQuote(tippList.quote)+'</TD>'+
								'</TR>' +	
								'<tr>'+
									'<td class="normalTextGreyBold" style="padding-left: 8px; padding-top: 5px;" align="left">Einsatz:</td>';
									if (this.confirm == false) {
										var myMoney = 0;
										if (tippList.betAmount != 0)
											myMoney = formatMoney((tippList.betAmount/100)+JAVASCRIPTCORRECTION);
										else
											myMoney = formatMoney(myMoney);		
										betline=betline+'<TD class="normalTextGreyBold" style="PADDING-RIGHT: 8px; padding-top: 5px;" align="right">&euro;&nbsp;'+
										'<input AUTOCOMPLETE="OFF" type="text" class="textinputfields" id="BETMONEYTICKET'+betNumber+'" name="BETMONEYTICKET'+betNumber+'" value="'+myMoney+'" maxlength="7" style="text-align:right; width:46px" onClick="document.getElementById(\'BETMONEYTICKET'+betNumber+'\').onkeydown=trapKey;" onkeyup="handleChangeValue(\''+actBet.gameNumber+'\',\''+tippList.tippNr+'\',\''+betNumber+'\')">'+		
										'<input type="text" name="hide_field" style="display:none">'+
										'</TD>';
									} else {
										betline=betline+'<TD class="normalTextGreyBold" style="PADDING-RIGHT: 8px; padding-top: 5px;" align="right">'+formatMoney((tippList.betAmount/100)+JAVASCRIPTCORRECTION,true)+'</TD>';									
									}
								var possibleWin = ((tippList.betAmount/100)*(tippList.quote/100))+JAVASCRIPTCORRECTION;
								possibleWin = possibleWin.toFixed(2);
								betline=betline+'</tr>'+
								'<tr>'+
									'<td class="normalTextGreyBold" style="PADDING-LEFT: 8px; padding-top: 5px;" align="left" nowrap="nowrap"><b>möglicher Gewinn:</b></td>'+
									'<td class="normalTextGreyBold" style="PADDING-RIGHT: 8px; padding-top: 5px;" align="right"><div id="POSSIBLEWIN'+betNumber+'" name="POSSIBLEWIN'+betNumber+'">'+formatMoney(possibleWin,true)+'</div></td>'+
								'</tr>'+
							'</table>'+
						'</td>'+
					'</tr>';				
					if (betNumber >= VALIDBETCOUNT)
						this.playable=true;
					else
						this.playable=false;
			        	betNumber = betNumber+1;
				}
				if (tippList.nextTipp != null)
					tippList = tippList.nextTipp;				
		  	}
		  	actBet = actBet.nextTopBet;
	        }
	        this.betCount = betNumber-1;	        
		betline=betline+'</table>';           	
		return betline;
	}

	this.getError=function() {	
		var errorFlag = false;
		var message= '';
		for (a=0; a<errorFlags.length; a++) {
			if (errorFlags[a] == ERRORON) {
				errorFlag = true;
				message = message+errorMessages[a]+'<br>';
			}
		}
		if (BETSTATUS == '-1') {
			message = message+SYSTEMERROR;
		}
		return message;
	}	
	
	this.getBetSummary=function() {
		var summary='<TABLE cellspacing="0" cellpadding="0" border="0" width="100%" style="margin-top: 18px; margin-bottom: 17px;">'+
		'	<TR>'+
		'		<TD class="normalTextGreyBold" style="padding-left: 8px">Anzahl Wetten:</TD>'+
		'		<TD class="normalTextGreyBold" style="padding-right: 8px" align="right">'+this.betCount+'</TD>'+						
		'	</TR>'+										
		'	<TR>'+
		'		<TD class="normalTextGreyBold" style="padding-left: 8px; padding-top: 5px;">Gesamteinsatz:</TD>'+
		'		<TD class="normalTextGreyBold" style="padding-right: 8px; padding-top: 5px;" align="right">'+formatMoney(this.getCurrentBetMoney(),true)+'</TD>'+						
		'	</TR>'+	
		'</TABLE>';
		return summary;													
	}
	
	this.getBottom=function() {
		var bottom = '<TR><TD style="padding-top: 12px; padding-left: 9px; padding-right: 9px; padding-bottom: 8px;" bgColor="#d78d10">'+
                	'<table cellpaddin="0" cellspacing="0" border="0" width="100%">'+
                    		'<tr>'+
					'<td align="left">';
		if (BETSTATUS != "" && SYSTEMERROR == "") {
			bottom = bottom+'<INPUT class="buttons" style="width:110px;" onclick="javascript:window.open(printURL, \'print\', \'scrollbars=yes,resizable=yes\'); return false;" type="button" value="Wette drucken">';			
		} else if (this.confirm == false) {															
			bottom=bottom+'<INPUT class="buttons" style="WIDTH: 70px" id="DELETEBUTTON" type="button" onClick="handleDeleteAllBets();" value="L&ouml;schen">';
		} else {
			bottom=bottom+'<INPUT class="buttons" style="WIDTH: 70px" id="CHANGEBUTTON" type="button" onClick="drawTicket(topBetTicket.getBetTicket(false))" value="&Auml;ndern">';							
		}
		bottom = bottom+'</td><td id="BETBUTTON" align="right">';
		if (BETSTATUS != "" && SYSTEMERROR == "") {
			bottom = bottom+'<INPUT class="buttons" style="width:70px;" onclick="handleDeleteAllBets();resetBetStatus();deleteCookie(cookieName);topBetTicket.betCount=0;" type="button" value="OK">';			
		} else if (this.playable == true) {
			if (this.confirm != true) {
				bottom=bottom+'<INPUT class="buttons" style="width:110px;" onclick="handlePlaceBet();" type="button" value="Wette platzieren">';
			} else {
				bottom=bottom+'<INPUT id="CONFIRMBUTTON" class="buttons" style="width:110px;" onclick="sendTicket()" type="button" value="Best&auml;tigen">';
			}																
		}
    	        bottom = bottom+'</td></tr></table></TD></TR>';
		return bottom;			
	}

	this.getBetTicket=function(conf) {
	    	if (conf == undefined)
	    		this.confirm = false;
	    	else
			this.confirm = conf;
		var headerImage=IMAGEPATH+'wettschein_top.jpg';
		if (this.confirm == true) {
			headerImage=IMAGEPATH+'wettschein_top.jpg';
		} 
		if (BETSTATUS != "" && SYSTEMERROR == "") {
			headerImage=IMAGEPATH+'wettschein_top.jpg';
		}
		var aTicket = "";
		if (this.bets != null || FORCE_REDRAW == true) {			
			aTicket = aTicket+'<form id="ticketform" name="ticketform"><TABLE style="border: solid 1px #d78d10;" cellSpacing="0" cellPadding="0" width="215" border="0">'+
				'<TR>'+
					'<TD valign="top" bgColor="#d78d10"><IMG src="'+headerImage+'" width="215" height="28" border="0"></TD>'+
				'</TR>';
			var icon = 'info.jpg';
			var ticketTitle = 'Spieleinsatz eingeben<br>und Wette platzieren';
			if (this.confirm == true) {
				ticketTitle = 'Angaben überprüfen<br>und bestätigen';
			} else {
				if (exceededBetCount == true) {
					ticketTitle = 'Sie haben die maximale Anzahl von '+MAXBETSONTICKET+' Tipps auf dem Wettschein platziert';
					exceededBetCount = false;
				}
			}
			if (BETSTATUS == "1" && SYSTEMERROR == "") {
				icon = 'ok.jpg';
				ticketTitle = 'Wette erfolgreich platziert';
			}
			aTicket=aTicket+'<TR>'+
						'<TD bgcolor="#ffd306" class="normalTextGreyBold" style="border: solid 1px #ffffff;">'+
							'<table cellpadding="0" cellspacing="0" border="0" style="margin-top: 5px; margin-bottom: 5px;">'+
					                    	'<tr>'+
                        						'<td id="IMAGE_TITLEBAR"><img src="../images/'+icon+'"></td>'+
                            						'<td id="TITLEBAR" class="normalTextGreyBold" style="padding-left: 2px;">'+ticketTitle+'</td>'+
					                        '</tr>'+
                    					'</table>'+
						'</TD>'+
					'</TR>';
			if (BETSTATUS == "1") {
				if (SYSTEMERROR == "") {
					aTicket=aTicket+'<TR>'+
						'<TD bgcolor="#ededed" class="normalTextGreyBold" style="padding: 10px;">Danke f&uuml;r Ihre Wettabgabe!</TD>'+
					'</TR>';
				}
			} else { 
				// the betlines
				aTicket = aTicket+'<TR>'+
						'<TD>'+
							'<DIV id="BETLINES" name="BETLINES">'+this.getBetLine()+'</DIV>'+
						'</TD>'+
					'</TR>';
				// the summary
				aTicket = aTicket+'<TR>'+
						'<TD bgColor="#ededed">'+
							'<DIV id="SUMMARY" name="SUMMARY">'+this.getBetSummary()+'</DIV>'+
						'</TD>'+
					'</TR>';
			}
			aTicket = aTicket+this.getBottom();
			aTicket = aTicket+'</TABLE></form>';
		}
		return aTicket;	
	}

	
	this.getTicketParams=function(){
		var paramString='&'+this.bets.getParams()+'&j_operation=bet&reqID='+requestID;
		return paramString;
	}			


	this.renewPossibleWin=function(ID,gameNumber,tippNumber) {
		var amount=eval('document.ticketform.BETMONEYTICKET'+ID+'.value');
		amount=amount.replace(',','.');	
		var myBet=this.bets.getTopBetByNumber(gameNumber);	
		var myTipp=myBet.tipp.getTippByNumber(myBet.tipp,tippNumber);
		this.writeElementById('POSSIBLEWIN'+ID,formatMoney(amount*(myTipp.quote/100),true));
	}

	this.addBet=function(bet) {
		this.betMoneyArray.push(defaultBetamount/100);
		if(this.bets==null) {
			this.bets=bet.createTopBet(bet);
			return this.redrawTicket();
	        } else {
			if(this.bets.getTopBetByNumber(bet.getGameNumber()) !=null) {				
				this.bets.updateTopBet(bet);
			} else {
				var aBet=bet.createTopBet(bet);
				this.bets.addBet(aBet);
			}

	        }
		return this.redrawTicket();	
	}

	this.deleteBet=function(num,tipp){
		if(this.bets!=null) {
			var thisBet=this.bets.getTopBetByNumber(num);
			var thisTipp=thisBet.tipp.getTippByNumber(thisBet.tipp,tipp);
			thisTipp.selected='0';
			var numRemain=thisBet.tipp.countSelectedBets(thisBet.tipp);
			if((thisBet.isOpened='1')&&(eval('document.GAMES.tipp_'+num+'_'+tipp)!=undefined)&&(eval('document.GAMES.tipp_'+num+'_'+tipp)!=null)) {
				eval('document.GAMES.tipp_'+num+'_'+tipp+'.checked=false');
			}
			if(numRemain==0) {
				this.bets=this.bets.deleteTopBet(num);				
				unmarkLine(num);	
			}

		}
		return this.redrawTicket();
	}

	this.getBets=function(){
		return this.bets;
	}

	this.deleteAllBets=function(){
		var actTopBet = this.bets;
	    	while (actTopBet != null) {
			unmarkLine(actTopBet.gameNumber);
       			actTopBet = actTopBet.nextTopBet;
    		}	
		this.bets=null;
		return null;
	}

	this.redrawTicket=function(){
		return this.getBetTicket(false);
	}

	this.setError=function(errorNumber,errorFlag){
		errorFlags[errorNumber]=errorFlag;
	}

	this.checkError=function(){
		var errFlag=false;
		for(a=0;a<errorFlags.length;a++) {
			if(errorFlags[a]=='1')
				errFlag=true;
		}
		return errFlag;
	}
	
	this.renewTitle=function(){
		if (SYSTEMERROR != "" || BETSTATUS != "")
			return;
		aText="Spieleinsatz eingeben<br>und Wette platzieren";
		if (exceededBetCount == true) {
			aText='Sie haben die maximale Anzahl von '+MAXBETSONTICKET+' Tipps auf dem Wettschein platziert';
			exceededBetCount=false;
		}		
		this.writeElementById('IMAGE_TITLEBAR','<img src="../images/info.jpg">');
		this.writeElementById("TITLEBAR",aText);
	     	if (this.betCount > 0 && this.checkError() == false) {
     			this.writeElementById('BETBUTTON','<INPUT class="buttons" style="width:110px;" onclick="handlePlaceBet();" type="button" value="Wette platzieren">');
     		} else {
     			this.writeElementById('BETBUTTON','');
     		}		
	}
	
	this.writeElementById=function(id,source) {
	   	if (document.getElementById) {
	   		if(document.getElementById(id)!=null) {
	     			document.getElementById(id).innerHTML=source;	
	     		}
	     	}
	}
	
	this.readElementById=function(id) {
	   	if (document.getElementById) {
	   		if(document.getElementById(id)!=null) {
	     			return document.getElementById(id).value;	
	     		}
	     	}
	}	
	
	this.renewBetList=function(){
		var allBetLinesOnTicket=this.getBetLine();
		this.writeElementById('BETLINES',allBetLinesOnTicket);
	}

	this.renewBet=function(bet){
		var betOnTicket=this.getBet(bet);
		var betNumber=1;
		var aList=this.bets;
		while(aList!=null) {
			if(aList.gameNumber==act.gameNumber) {
				aList=null;
			} else {
				aList=aList.getNextTopBet();
				betNumber++;
			}
		}
		if(betOnTicket!='')
			this.writeElementById('BET'+betNumber,betOnTicket);
	}		
	
	this.renewError=function() {
		var errorline=this.getError();
		if (errorline.length > 0) {
			this.writeElementById('TITLEBAR',errorline);
			this.writeElementById('IMAGE_TITLEBAR','<img src="../images/error.jpg">');
		}
	}	
	
	this.renewSummary=function(){
		var summaryOfTicket=this.getBetSummary();
		this.writeElementById('SUMMARY',summaryOfTicket);
	}					
	
	this.getCurrentBetMoney=function() {
		var totalBetAmount=0;
		var myBets=this.bets;
		while(myBets!=null) {
			var myTipps=myBets.tipp;
			while(myTipps!=null) {
				if(myTipps.selected=='1')
					totalBetAmount=parseFloat(totalBetAmount)+parseFloat(myTipps.betAmount);
				myTipps=myTipps.nextTipp;
			}
			myBets=myBets.nextTopBet;
		}
		return totalBetAmount/100;
	}	
	
	this.setGameType=function(type) {
		this.currentGameType=type;
	}
	
	this.getPossibleWin=function() {
		var myBets=this.getBets();
		//----------------------------------------------
		// turn it into a string an cut to xxxx.xx
		//----------------------------------------------
		sWin=''+this.getCurrentBetMoney(betCount,false);
		return sWin.substring(0,sWin.indexOf('.')+3);
	}
}

function getBodyStart() {				
	return this.getBodyStart();		
}

function getBodyEnd() {				
	return this.getBodyEnd();		
}

function getBottom() {				
	return this.getBottom();		
}			

function getBetline() {				
	return this.getBetline();		
}

function getBet(a,betNumber) {				
	return this.getBet(a,betNumber);		
}

function getTicketParams() {	
	return this.getTicketParams();		
}

function getBetSummary() {
	return this.getBetSummary();
}	

function renewPossibleWin(ID,gameNumber,tippNumber) {	
	return this.renewPossibleWin(ID,gameNumber,tippNumber);		
}

function addBet(bet) {				
	return this.addBet(bet);		
}

function getBetTicket(confirm) {	
	return this.getBetTicket(confirm);		
}

function getCurrentBetMoney(betCount,onlyThis) {	
	return this.getCurrentBetMoney(betCount,onlyThis);		
}

function deleteBet(num,tipp){
	return this.deleteBet(num,tipp);	
}

function deleteCurrentBetMoneyFromArray(betNumber) {
	this.deleteCurrentBetMoneyFromArray(betNumber);
}

function getCurrentBetMoneyFromArray(betNumber) {
	return this.getCurrentBetMoneyFromArray(betNumber);
}

function setCurrentBetMoneyFromArray(betNumber) {
	this.setCurrentBetMoneyFromArray(betNumber);
}

function setGameType(type) {
	this.setGameType(type);	
}

