var ERRORON=1;
var ERROROFF=0;

function BetTicketHit(id) {
var playable=false;
this.bets=null;
var ticketTitle="&nbsp;";
this.betCount=0;
this.currentBetMoney=2;
this.currentBetMoneySummary=0;
this.currentGameQuoteSummary=0;
this.betMoneyArray = new Array();
this.confirmTicket=false;
var STARTMONEY=2;
this.betsOnTicket=0;
var VALIDBETCOUNT=1;

this.setCurrentBetMoneyFromArray=setCurrentBetMoneyFromArray;
this.getCurrentBetMoneyFromArray=getCurrentBetMoneyFromArray;
this.deleteCurrentBetMoneyFromArray=deleteCurrentBetMoneyFromArray;

	this.checkTicket=function(nr) {	
		var amount;
		if((this.readElementById("BETMONEYTICKET"+nr)==null)||(this.readElementById("BETMONEYTICKET"+nr)==undefined)) {
			nr--;
			if((this.readElementById("BETMONEYTICKET"+nr)==null)||(this.readElementById("BETMONEYTICKET"+nr)==undefined))
				return;
		}		
		betMoney=getFloat(this.readElementById("BETMONEYTICKET"+nr));
		var result=''+this.bets.betAmount;
		if(betMoney>(hitMaxamount/100)) {		
			this.setError(INVALID_BETAMOUNT,ERRORON);
		} else if(betMoney<(hitMinamount/100)) {
			this.setError(INVALID_BETAMOUNT,ERRORON);
		} else if(result=='NaN') {
			this.setError(INVALID_BETAMOUNT,ERRORON);
		} else {
			this.setError(INVALID_BETAMOUNT,ERROROFF);	
		}			
		this.renewTitle();	
		this.renewError();	
		return;		
	}

	this.checkBets=function() {
		var actBet=this.bets;
		while(actBet!=null) {
			var tippBoxString=actBet.getTippBoxString();
			for(a=0;a<tippBoxString.length;a++) {
				if(tippBoxString.charAt(a)=='0')
					return false;
			}
			actBet=actBet.nextHitBet;
		}
		return true;
	}

	this.getBetLine=function() {
		var betline="";
		var actBet=this.bets;
		var betNumber=1;
	        while (actBet != null) {
			betline=betline+'<TR>'+
			'	<TD bgcolor="#fff6b9" style="border-bottom: solid 1px #cfc997;">'+
			'		<TABLE width="100%" cellSpacing="0" cellPadding="0" border="0" style="margin-top: 16px; margin-bottom: 5px;">'+
			'			<TR>'+
			'				<TD style="padding-left: 8px;" class="normalTextGreyBold" align="left">'+betNumber+'.&nbsp;'+actBet.shortQuestion+'</TD>'+
			'				<TD style="padding-right: 8px;" align="right" valign="middle">';
						if (this.confirmTicket == false)
							betline=betline+'<IMG style="cursor:pointer;cursor:hand" onClick="handleDeleteBet('+betNumber+','+actBet.gameNumber+')" height="11" hspace="2" src="'+IMAGEPATH+'trash.gif" width="11" border="0">';
						else
							betline=betline+'&nbsp;';
						betline=betline+'</TD>'+
			'			</TR>'+
			'		</TABLE>'+		
			'	</TD>'+
			'</TR>';        	
	        	var questionArray=actBet.questionArray;
	        	var questionArrayS=""+questionArray;	        	
	        	for (a=0; a<questionArray.length; a++) {
				betline=betline+'<TR>'+
				'	<TD bgcolor="#fff6b9" style="border-bottom: solid 1px #cfc997;">'+
				'		<TABLE width="215" cellspacing="0" cellpadding="0" border="0" style="margin: 8px;">';					
	        		var frage=questionArray[a];
				var pos=(a+(questionArray[a+1]*2)+2);
	        		var antwortNummer=parseInt(questionArray[pos]);
	        		var antwort="&nbsp;";
				if (antwortNummer!='0')
		        		antwort=questionArray[(a+1)+(((antwortNummer-1)*2)+1)];
				betline=betline+'<TR>'+		
				'	<TD class="normalTextGrey" align="left">'+frage+'</TD>'+
				'	<TD class="normalTextGreyBold" align="right" valign="bottom" style="padding-left: 3px;">'+antwort+'</TD>'+
				'</TR>';
				a=a+(questionArray[a+1]*2)+3;
				betline=betline+'</table>'+
				'	</TD>'+
				'</TR>\n';
			}	
			betline = betline+'<TR>'+
			'	<TD bgcolor="#ededed">'+
			'		<table width="100%" border="0" cellpadding="0" cellspacing="0" style="margin-top: 18px; margin-bottom: 17px;"">'+
			'			<tr>'+
			'				<td align="left" style="padding-left: 8px;" class="normalTextGreyBold">Quote:</td>'+
			'				<td align="right" style="padding-right: 8px;" class="normalTextGreyBold">'+formatQuote(actBet.getQuote())+'</td>'+
			'			</tr>'+
			'			<tr>'+
			'				<td align="left" style="padding-left: 8px; padding-top:5px;" class="normalTextGreyBold">Einsatz:</td>'+
			'				<td align="right" style="padding-right: 8px; padding-top:5px;" class="normalTextGreyBold">&euro; ';
							if (this.confirmTicket == false)		
								betline=betline+'<input type="text" class="textinputfields" id="BETMONEYTICKET'+betNumber+'" name="BETMONEYTICKET'+betNumber+'" value="'+formatMoney(actBet.betAmount/100)+'" maxlength="7" style="text-align:right; width:46px" onKeyUp="handleChangeValue('+betNumber+','+actBet.betId+');">';
							else
								betline=betline+formatMoney(hitBetTicket.getCurrentBetMoneyFromArray(betNumber));
			betline=betline+'		</td>'+
			'			</tr>'+
			'			<tr>'+
			'				<td align="left" style="padding-left: 8px; padding-top:5px;" class="normalTextGreyBold">möglicher Gewinn:</td>'+
			'				<td align="right" style="padding-right: 8px; padding-top:5px;" class="normalTextGreyBold"><div id="POSSIBLEWIN" name="POSSIBLEWIN">'+formatMoney(Math.round((this.getCurrentBetMoneyFromArray(betNumber)*actBet.getQuote(a)))/100,true)+'</div></td>'+
			'			</tr>'+
			'			<TR>'+
			'				<TD style="padding-left: 8px; padding-top:15px;" class="normalTextGreyBold">Gesamteinsatz:</TD>'+
			'				<TD style="padding-right: 8px; padding-top:15px;" align="right" class="normalTextGreyBold" id="SUMMARY">'+formatMoney(this.bets.betAmount/100,true)+'</TD>'+
			'			</TR>'+	
			'		</table>'+
			'	</TD>'+
			'</TR>\n';	
			if (betNumber > 0)
				this.playable=true;
			else
				this.playable=false;
	            	betNumber=betNumber+1;		
		  	actBet=actBet.nextHitBet;
	        }
	        this.betCount=betNumber-1;	        
		return betline;
	}

	this.getError=function() {	
		var errorFlag=false;
		var message='';
		for (a=0; a<errorFlags.length; a++) {
			if (errorFlags[a] == ERRORON) {
				errorFlag=true;
				if (a != EXCEEDED_BETCOUNT)
					message=message+errorMessages[a]+'<br>';
			}
		}
		if (BETSTATUS == '-1') {
			message=message+SYSTEMERROR;
			errorFlag=true;
		}	
		return message;
	}

		
	this.getBottom=function() {
		var bottom = '<TR><TD style="padding-top: 12px; padding-left: 9px; padding-right: 9px; padding-bottom: 8px;" bgColor="#8e090a">'+
                	'<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.confirmTicket == false) {	
			bottom=bottom+'<INPUT class="buttons" style="WIDTH: 70px" id="DELETEBUTTON" type="button" onClick="handleDeleteTicket('+hitBetTicket.bets.gameNumber+');" value="L&ouml;schen">';
		} else {
			bottom=bottom+'<INPUT class="buttons" style="WIDTH: 70px" id="CHANGEBUTTON" type="button" onClick="resetSystemError();drawTicket(hitBetTicket.getBetTicket(false));keepOpen=true;drawRoundTable(getCode('+openedBetId+'));keepOpen=false;" value="&Auml;ndern"></span>';
		}
		bottom = bottom+'</td><td id="BETBUTTON" align="right">';
		if (BETSTATUS != "" && SYSTEMERROR == "") {
			bottom=bottom+'<INPUT class="buttons" style="width: 70px;" onclick="checkOK('+hitBetTicket.bets.gameNumber+');" type="button" value="OK">';
		} else if (this.playable == true && this.checkError() == false && this.checkBets() == true) {
			if (this.confirmTicket == false) {
				bottom=bottom+'<span id="BETBUTTON"><INPUT class="buttons" style="width:110px;" onclick="drawTicket(hitBetTicket.getBetTicket(true));keepOpen=true;drawRoundTable(getCode('+openedBetId+'));keepOpen=false;" type="button" value="Wette platzieren"></span>';
			} else {
				bottom=bottom+'<span id="BETBUTTON"><INPUT id="CONFIRMBUTTON" class="buttons" style="width:110px;" onclick="sendTicket()" type="button" value="Best&auml;tigen"></span>';
			}																
		}
    	        bottom = bottom+'</td></tr></table></TD></TR>';
		return bottom;	
	}

	this.getBetTicket=function(confirm) {
		this.confirmTicket=confirm;
		var aTicket = '';
		if (this.bets != null || BETSTATUS == '-1') {
			var IMAGENAME='wettschein_hit.jpg';		
			aTicket=aTicket+'<form id="ticketform" name="ticketform" onSubmit="return false;">'+
			'<TABLE cellspacing="0" cellpadding="0" width="215" border="0" style="border: solid 1px #8e090a;">'+
			'	<TR>'+
			'		<TD bgcolor="#8e090a"><IMG src="'+IMAGEPATH+IMAGENAME+'" width="215" height="28" border="0"></TD>'+
			'	</TR>';	
			var icon = 'info.jpg';
			var ticketTitle = 'Beantworten Sie alle<br>Ja/Nein Fragen';
			if (this.confirmTicket == true)
				ticketTitle = 'Angaben überprüfen<br>und bestätigen';
			if (errorFlags[EXCEEDED_BETCOUNT]==ERRORON)
				ticketTitle = errorMessages[EXCEEDED_BETCOUNT];
			if (BETSTATUS == "1" && SYSTEMERROR == "") {
				ticketTitle = 'Wette erfolgreich platziert';
				icon = 'ok.jpg';
			}
			if (SYSTEMERROR != "") {
				ticketTitle = this.getError();
				icon = 'error.jpg';
			}
			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 { 					
				if (this.bets != null) {					
					aTicket=aTicket+this.getBetLine();			
				}
			}
			aTicket=aTicket+this.getBottom();
			aTicket=aTicket+'</TABLE></form>';
		}
		return aTicket;
	}


	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.setError=function(errorNumber,errorFlag){
		errorFlags[errorNumber]=errorFlag;
	}
	
	this.resetErrorFlags=function() {
		 for(a=0;a<errorFlags.length;a++)
		 	this.setError(a,'0');
	}

	this.checkError=function(){
		var errFlag=false;
		for(a=0;a<errorFlags.length;a++) {
			if(a!=EXCEEDED_BETCOUNT) {
				if(errorFlags[a]=='1')
					errFlag=true;
			}
		}
		return errFlag;
	}

	this.renewSummary=function(){
		var amount=document.ticketform.BETMONEYTICKET1.value;
		amount=amount.replace(',','.');	
		this.bets.betAmount=amount*100;
		this.writeElementById('SUMMARY', formatMoney(this.bets.betAmount/100,true));
	}	

	this.renewPossibleWin=function() {
		var amount=document.ticketform.BETMONEYTICKET1.value;
		amount=amount.replace(',','.');		
		this.writeElementById('POSSIBLEWIN',formatMoney(amount*(this.bets.getQuote()/100),true));
	}

	this.renewTitle=function(){
		if (SYSTEMERROR != "" || BETSTATUS != "")
			return;
		var aText='Beantworten Sie alle<br>Ja/Nein Fragen';
		if (errorFlags[EXCEEDED_BETCOUNT] == ERRORON) {
			aText=errorMessages[EXCEEDED_BETCOUNT];
			errorFlags[EXCEEDED_BETCOUNT]=ERROROFF;
		}
		this.writeElementById('IMAGE_TITLEBAR','<img src="../images/info.jpg">');
		this.writeElementById("TITLEBAR",aText);
		if (BETSTATUS != "" && SYSTEMERROR == "") {
			bottom=bottom+'<span style="padding-right: 10px;"><INPUT class="buttons" style="width:70px;" onclick="checkOK('+hitBetTicket.bets.gameNumber+');" type="button" value="OK"></span>';		
		} else if (BETSTATUS != "" && SYSTEMERROR != "") {
			bottom=bottom+'<span style="padding-right: 10px;"><INPUT class="buttons" style="WIDTH: 70px" id="CHANGEBUTTON" type="button" onClick="resetSystemError();drawTicket(hitBetTicket.getBetTicket(false));keepOpen=true;drawRoundTable(getCode('+openedBetId+'));keepOpen=false;" value="&Auml;ndern"></span>';
		} else {
		     	if((this.checkError()==false)&&(this.checkBets()==true)) {
     				this.writeElementById('BETBUTTON','<INPUT class="buttons" style="width:110px;" onclick="drawTicket(hitBetTicket.getBetTicket(true));keepOpen=true;drawRoundTable(getCode('+openedBetId+'));keepOpen=false;" 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.getTicketParams=function(){
		var paramString='&'+this.bets.getParams()+"&j_operation=bet&reqID="+requestID;
		return paramString;
	}	
	
	this.addBet=function(bet) {			
			if(this.bets==null) {
				this.bets= bet.createHitBet(bet);
				this.betMoneyArray.push(this.bets.betAmount/100);
				this.betsOnTicket++;
				markLine(bet.gameNumber);
		        } else {
				if(this.bets.getHitBetByNumber(bet.getGameNumber()) !=null) {				
					this.bets.updateHitBet(bet);
				} else {					
					if(this.betsOnTicket<maxBetcount) {
						var aBet=bet.createHitBet(bet); 
						this.bets.addBet(aBet);
						this.betsOnTicket++;
						this.betMoneyArray.push(aBet.betAmount/100);
						markLine(bet.gameNumber);
					} else {
						this.setError(EXCEEDED_BETCOUNT,ERRORON);	
					}
				}
			}		
		return ;	
	}

	this.deleteBet=function(num){
		if(this.bets!=null) {
			this.betsOnTicket--;
			var thisBet=this.bets.getHitBetByNumber(num);
			this.bets=this.bets.deleteHitBet(num);
			setCookie(cookieName,setCookieValueString(this.bets));
			hitBetList.resetBet(thisBet);
			showRoundTable('',-1);
			unmarkLine(num);
		}
		this.resetBet=new Array();
		return this.redrawTicket();
	}

	this.getBets=function(){
		return this.bets;
	}

	this.deleteAllBets=function(){
		while(this.bets!=null) {
			var num=this.bets.getGameNumber();
			hitBetList.resetBet(this.bets);			
			this.bets=this.bets.deleteHitBet(num);
			unmarkLine(num);
		}
		deleteCookie(cookieName);
		showRoundTable('',-1);
		this.betsOnTicket=0;
		return null;
	}

	this.deleteCurrentBetMoneyFromArray=function(betNumber){
		this.betMoneyArray.splice((betNumber-1),1);

	}	

	this.setCurrentBetMoneyFromArray=function(betNum,betId){
		var elem = document.getElementById("ticketform");
		var amount=eval("elem.BETMONEYTICKET"+betNum+".value");
		if(amount.indexOf(",")>-1)
			amount=amount.substring(0,amount.indexOf(","))+"."+amount.substring(amount.indexOf(",")+1);
		this.betMoneyArray[(betNum-1)]=parseFloat(amount);
		var aBet=this.bets.getHitBetById(betId);
		if(aBet!=null) {
			aBet.betAmount=amount*10;
			aBet.betAmount=aBet.betAmount*10;
		}
		aBet=hitBetList.getHitBetById(betId);
		if(aBet!=null) {
			aBet.betAmount=amount*10;
			aBet.betAmount=aBet.betAmount*10;
		}
	}

	this.getCurrentBetMoneyFromArray=function(betNumber){		
		var amount=this.betMoneyArray[(betNumber-1)]
		return amount;
	}	

	this.getBetList=function(bets){
		var betlist=null;
		var actHitBet=bets;
	        while(actHitBet != null) {	        		        	
			if(actHitBet.checkSelectedBets()>0) {
				var aBet=actHitBet;
				aBet.nextHitBet=null;
				if(betlist==null) {						
					betlist=aBet;
				} else {
					betList.addBet(aBet);
				}	
				
			}
	            actHitBet=actHitBet.getNextHitBet();
	        }		
		return betlist;
	}	

	this.redrawTicket=function(){
		return this.getBetTicket(this.confirmTicket);
	}	
	
	this.getCurrentBetMoney=function(betCount,onlyThisNumber) {
		var elem = document.getElementById("ticketform");
		var totalMoney=0.00;		
		if((elem==null)&&(onlyThisNumber==true))
			totalMoney=defaultBetamount/100;				
		if(onlyThisNumber==false) {
			for(a=0;a<betCount;a++) {				
				totalMoney=totalMoney+parseFloat(this.betMoneyArray[a]);
			}
		} else {
			if(elem!=null) {			
									
				if(eval("elem.BETMONEYTICKET"+betCount)!=undefined) {					
					totalMoney=totalMoney+parseFloat(eval("elem.BETMONEYTICKET"+betCount+".value"));
				} else {
					totalMoney=totalMoney+STARTMONEY;	
				}
			}			
		}
		return totalMoney;
	}	
	
	this.setBetAmount=function(amount) {
		this.currentBetMoney=amount;
	}
	
	this.setGameType=function(type) {
		this.currentGameType=type;
	}
	
	this.getPossibleWin=function() {
		var myBets=this.getBets();
		sWin=''+this.getCurrentBetMoney();
		return sWin.substring(0,sWin.indexOf('.')+3);
	}
}

function getSeperator() {				
	return this.getReperator();		
}		

function getBodyStart() {				
	return this.getBodyStart();		
}

function getBodyEnd() {				
	return this.getBodyEnd();		
}

function getBottom() {				
	return this.getBottom();		
}			

function getBetline() {				
	return this.getBetline();		
}	

function getBetSummary() {
	return this.getBetSummary();
}	

function addBet(bet) {				
	return this.addBet(bet);		
}

function getBetTicket() {	
	return this.getBetTicket();		
}

function deleteBet(num){
	return this.deleteBet(num);	
}
	
function setBetAmount(amount) {
	this.setBetAmount(amount);	
}

function deleteCurrentBetMoneyFromArray(betNumber) {
	this.deleteCurrentBetMoneyFromArray(betNumber);
}

function getCurrentBetMoneyFromArray(betNumber) {
	return this.getCurrentBetMoneyFromArray(betNumber);
}

function setCurrentBetMoneyFromArray(betNumber,betId) {
	this.setCurrentBetMoneyFromArray(betNumber,betId);
}

function setGameType(type) {
	this.setBetAmount(type);	
}

