// ***********************************************************************************
// Cheltenham Betslip - 17 January 2003
// BETSLIP Variables
// ***********************************************************************************
var bsForm = "betslip";				
var bsConfDiv = "betSlipConfirm";
var bsFormConf = "betslipconf";
var bsImagePath = imagepath + "betslip";
var bsInRunningMsgDiv = "BetInRunningMsg";
var inRunningTimeout = 10;

// Reference to bettypes Used by If{} Else{} conditions - changing here changes all.
// Tricast Ids
var Tid = 27;
var CTid = 28;
// Forecast Ids
var FCid = 25;
var RFCid = 26;
var CFCid = 57;
// Sport Ids
var idHorseRacing = 200;
var idUSHorseRacing = 966500;
var idDogs = 433200;
var idHKRacing = 968900;
var idFinancials = 968200;
var idVHorseRacing = 969900;  //Virtual Horse Racing
var idVDogs = 970000; // Virtual Dogs
// OpportunityType Ids
var idTopJockey = 201500;
var idTopTrainer = 201600;

// BETSLIP
//Betslip 
BetSlip = function(mode,minPerm,maxPerm,cookieName) {
	this.opportunities = new ArrayList();
	this.availableBets = new BetsCache();
	this.btc = new BetTypeCache();
	this._MINPERM = minPerm;
	this._MAXPERM = maxPerm;
	//1 - Just Singles, 2 - Singles & Multiples, 3 - Singles, Mechs and Perms
	this._MODE = mode; 
	this._COOKIENAME = cookieName;
}

//Handle The Bet
BetSlip.prototype.handleBet = function(price,pricetype,opp,oppid,action,ewterm,isrunning,fcast,tcast,rev,comb,ap,tp) {

	if (price.search(',')==1) {
		priceSpt = price.split(',');
		if ((priceSpt[0])&&(priceSpt[1])) {
			price = priceSpt[0]+priceSpt[1];
		}
	}
	if((action == true)||(action == "add")||(action=="true")) {
		this.add(null,opp,oppid,price,pricetype,ewterm,isrunning,fcast,tcast,rev,comb,ap,tp);
	}else if((action == false)||(action == "remove")||(action=="false")){
		this.remove(opp);
	}
}

BetSlip.prototype.handleBetDesc = function(desc,price,pricetype,opp,oppid,ewterm,isrunning,fcast,tcast,rev,comb,ap,tp) {

	if (price.search(',')==1) {
		priceSpt = price.split(',');
		if ((priceSpt[0])&&(priceSpt[1])) {
			price = priceSpt[0]+priceSpt[1];
		}
	}

	this.addDesc(desc,opp,oppid,price,pricetype,ewterm,isrunning,fcast,tcast,rev,comb,ap,tp);
}

//Update Price In Betslip
BetSlip.prototype.updatePrice = function(i, opp, pr, pt) {
	var op = this.opportunities.get(i);
	op.opp = opp;
	op.price = pr;
	op.pricetype = pt;
	this.update();
}

//Add To Betslip
BetSlip.prototype.add = function(desc,opp,oppid,price,pricetype,ewterm,isrunning,fcast,tcast,rev,comb,ap,tp) {
	b = new BetOpp(desc,opp,oppid,price,pricetype,ewterm,isrunning,fcast,tcast,rev,comb,ap,tp);
	this.opportunities.add(b);
	this.update();
}

BetSlip.prototype.addDesc = function(desc,opp,oppid,price,pricetype,ewterm,isrunning,fcast,tcast,rev,comb,ap,tp) {
	b = new BetOpp(desc,opp,oppid,price,pricetype,ewterm,isrunning,fcast,tcast,rev,comb,ap,tp);
	this.opportunities.add(b);
	
	switch(this._MODE) {
		case 1:
			this.updateSingles();
			break;
		case 2:
			this.updateSinglesMultiples();
			break;
		case 3:
			this.updateSinglesMechsPerms();
			break;
	}
}

//Remove From Betslip
BetSlip.prototype.remove = function(opp, bform, pos) {
	for(var i = 0; i < bs.opportunities.length; i++) {
		var id = this.opportunities.get(i).opp;
		if(opp == id) {
			this.opportunities.remove(i);
			this.update();
			break;
		}
	} 
	updateStakes(bform, pos)
}

//removeAll
BetSlip.prototype.removeAll = function(hide, show) {
	this.opportunities = new ArrayList();
	this.update();
	drawBetslip();
}

//Update - updates betslip after having removed / added an opportunity
BetSlip.prototype.update = function() {
 	var CookieVal = "";
 	if(this.countOpps()>0) {
		for(var i = 0; i < this.countOpps(); i++) {
			var c = this.getBetOpp(i);
			CookieVal += "[";
			CookieVal += c.opp;
			CookieVal += "-";
			CookieVal += c.oppid;
			CookieVal += "-";
			CookieVal += c.price;
			CookieVal += "-";
			CookieVal += c.pricetype;
			CookieVal += "-";
			CookieVal += c.ewterm;
			CookieVal += "-";
			CookieVal += c.isrunning;
			CookieVal += "-";
			CookieVal += c.forecast;
			CookieVal += "-";
			CookieVal += c.tricast;
			CookieVal += "-";
			CookieVal += c.revforecast;
			CookieVal += "-";
			CookieVal += c.combination;					
			CookieVal += "-";
			CookieVal += c.antepost;					
			CookieVal += "-";
			CookieVal += c.takeprice;
			CookieVal += "]";
		}
	}
	setCookie(this._COOKIENAME, CookieVal);
 	switch(this._MODE) {
		case 1:
			this.updateSingles();
			break;
		case 2:
			this.updateSinglesMultiples();
			break;
		case 3:
			this.updateSinglesMechsPerms();
			break;
	}
}

 //countOpps in the betslip
 BetSlip.prototype.countOpps = function() {
 	return this.opportunities.length;
 }
 
 //getBetOpp
 BetSlip.prototype.getBetOpp = function(index) {
 	return this.opportunities.get(index);
 }
 
 //getAllBetOpps
 BetSlip.prototype.getAllBetOpps = function() {
 	return this.opportunities;
 }
 
//Count The Events
BetSlip.prototype.countEvents = function() {
 	var events = new Array();
 	for(var i = 0; i < this.countOpps(); i++) {
		var e = this.getBetOpp(i).eventid;
		if(events.toString().indexOf(e) == -1) {
			events[events.length] = e;
		}
	}
	return events.length;
}

//Run On Each Page - Repopulates The Betslip With Previously Selected Opps
BetSlip.prototype.repopOpps = function() {
 	var s = getCookie(this._COOKIENAME);
	if((s!=null)&&(s!="")) {
		var ind = s.indexOf("[");
		var whichbet = 0;
		while(ind != -1) {
			var end = s.indexOf("]", ind);
			var fb = s.substring(ind+1,end).split("-");
			this.add(null, fb[0],fb[1],fb[2],fb[3],unescape(fb[4]),fb[5],fb[6], fb[7], fb[8], fb[9], fb[10], fb[11]);
			ind = s.indexOf("[", ind + 1);
		}
		this.update();
	}
}
 
//Checks The Current Opps And Sees If The Opp Already Exists.
BetSlip.prototype.checkOppExists = function(oppid) {
 	var exists = false;
 	for(var i = 0; i < this.opportunities.length; i++) {
		var o = this.opportunities.get(i);
		if(o.oppid == oppid) {
			exists = true;
			break;
		}
	}
	return exists;
}

// Reset The BetSlip
BetSlip.prototype.resetBetslip = function() {
	this.removeAll();
 	deleteCookie(this._COOKIENAME);
    this.opportunities = new ArrayList();
	this.availableBets = new BetsCache();
}

//Allow Permutations
BetSlip.prototype.allowPerms = function() {
 	if((this.countOpps()>0)&&(this.countOpps == this.countEvents())) {
		return true;
	} else {
		return false;
	}
}
 
//x - Number Of Interests In Permutations
BetSlip.prototype.calcPermLines = function(x) {
	var bets = this.countOpps();
	var a = 1;
	var b = 1;
	var ints;
	ints = x;
	while(ints > 0) {
		a *= bets--;
		ints--;
	}	
	ints = x;
	while(ints > 1) {
	  b *= ints--;
	}
	return (a/b);
}

	// BETSLIP Singles & Multiples Inc Permutations
	//updateSinglesMultiples - Used To Handle Mechs & Perms Together
	BetSlip.prototype.updateSinglesMultiples = function() {
	 	var x = this.countOpps();
		var y = this.countEvents();
		var totOpps = this.opportunities.length;
		
		this.availableBets = new BetsCache();
		
		//Add All Singles to availble Bets
		for(var i = 0; i < x; i++) {
			var b = this.btc.getBetTypeById(1);
			var opps = new ArrayList();
			opps.add(this.getBetOpp(i));
			var l = 1;
			this.availableBets.addSingle(new Bet(b,opps,null,null,l,false));
		}
		
		//Add All Multiples to availble Bets
		allSP = false;
		use_USHR = true;
		use_HR = true;
		use_Dogs = true;
		use_VHR = true ; // Virtual Horse Racing
		use_VDogs = true; // Virtual Dogs
			
	 	for(var i = 0; i < this.opportunities.length; i++) {
			var o = this.opportunities.get(i);
			split_Opp = o.opp.split(".");
			
			if (split_Opp[5]!=idUSHorseRacing) { use_USHR = false; }
			if (split_Opp[5]!=idHorseRacing) { use_HR = false; }
			if (split_Opp[5]!=idDogs) { use_Dogs = false; }
			if (split_Opp[5]!=idVHorseRacing) { use_VHR = false; }
			if (split_Opp[5]!=idVDogs) { use_VDogs = false; }
			if ((o.price=='SP')||(o.price=='sp')) { allSP = true; }
		}
		//Checks The Current Opps And Sees If The Sport ID, Event ID and opp Type ID are the same.
		var diffSid = true;
		var diffEid = true;
		var diffOtid = true;
		var diffEidOtid = true;
		var sameEidOtid = 0;
		
		var useForecast = true;
		var useRevForecast = true;
		var useTricast = true;		
		var useComb = true;
		var useCombTri = true;
		var RCmode = true;

		var hasFinancials = false;
		var hasHKRacing = false;
		var hasTopJockeyTrainer = false;
		var doneTri = false;
		
		if (this.opportunities.length>0) {
			// Check for same Event Id, Sport Id and Opp Type Id - affects what bettypes are returned.
		 	for(var i = 0; i < this.countOpps(); i++) {
				var e1 = this.getBetOpp(i).eventid;
				var ot1 = this.getBetOpp(i).oppid;
				var s1 = this.getBetOpp(i).sportid;
				var eot1 = ot1 + "." + e1;

				if (s1 == idFinancials) { hasFinancials = true; }
				if (s1 == idHKRacing) { hasHKRacing = true; }

				for(var j = 0; j < this.countOpps(); j++) {
					var e2 = this.getBetOpp(j).eventid;	
					var ot2 = this.getBetOpp(j).oppid;
					var s2 = this.getBetOpp(j).sportid;
					var eot2 = ot2 + "." + e2;
					
					if ((e1==e2)&&(i!=j)) { diffEid=false; }
					if ((eot1==eot2)&&(i!=j)) { diffEidOtid=false; } else { sameEidOtid++; }										
				}
				// Check All ForeCast or Tricast			
				if (this.getBetOpp(i).forecast!='Y') { useForecast=false; RCmode=false;}
				if (this.getBetOpp(i).revforecast!='Y') { useRevForecast=false; RCmode=false;}
				if (this.getBetOpp(i).tricast!='Y') { useTricast=false; useCombTri=false;}
				if (this.getBetOpp(i).combination=='N') { useComb=false; RCmode=false; }
				if (this.getBetOpp(i).combination!=2) { useCombTri=false; }	// 1 = Forecast, 2 = Forecast + Tricast
				if ((this.getBetOpp(i).oppid == idTopJockey) || (this.getBetOpp(i).oppid == idTopTrainer)) { hasTopJockeyTrainer = true; }
			}
		}
		
		//alert (RCmode + "," + diffEidOtid);
		for(var i = 0; i < this.btc.count(); i++) {
			var b = this.btc.getBetType(i);
		
				/* HR RACECARD SPECIFIC - FC, COMB TRI/FC, REV FC bet types */
				if ((RCmode)&&(!diffEidOtid)) {
					if ((b.id==FCid)||(b.id==CFCid)||((b.id==Tid)&&(useTricast))||((b.id==CTid)&&(useCombTri))||(b.id==RFCid)&&(!hasTopJockeyTrainer)) {
						if(b.isValidBet(totOpps,y,b.id)) {
								var opps = this.getAllBetOpps();
								maxBet = bs.opportunities.length;
								if (b.id==CFCid) {
									if (maxBet == 3) {
										var l = maxBet * 2;
									} else if (maxBet == 4) {
										var l = maxBet * 3;
									} else if (maxBet <= 2) {
										// you can't HAVE a combination forecast with 2 or less
										// this is the brute force method, I know, and is only done
										// because jason fa is off sick today! - Phil
										continue;
									} else {
										var l = maxBet * (maxBet - 1);
									}
								// If Combination Tricast then calculate sub bets
								} else if (b.id==CTid) {	
									var l = maxBet * ((maxBet - 1) * (maxBet - 2));
								} else {
									var l = b.subbets;
								}
								this.availableBets.addMultiples(new Bet(b,opps,null,null,l,false));
						} else if((b.isvalidPerm(totOpps)) && (b.id >= this._MINPERM) && (b.id <= this._MAXPERM)){
							var opps = this.getAllBetOpps();
							var l = this.calcPermLines(b.interests);	
							this.availableBets.addMultiples(new Bet(b,opps,null,null,l,true));
						}
					}
				/* END OF : HR RACECARD SPECIFIC */
				}	
				// US HR (Forecast) and SP			
				else if (((use_HR)||(use_VHR)||(use_VDogs)||(use_Dogs)||(use_USHR))&&((useForecast)||(useTricast))&&(!diffEidOtid)&&(hasFinancials==false)&&(hasHKRacing==false)) {
					if ((b.id==FCid)||((b.id==Tid)&&(useTricast))) {	
						if(b.isValidBet(totOpps,y,b.id)) { 
							var opps = this.getAllBetOpps();
							this.availableBets.addMultiples(new Bet(b,opps,null,null,l,false));
						} else if((b.isvalidPerm(totOpps)) && (b.id >= this._MINPERM) && (b.id <= this._MAXPERM)){
							var opps = this.getAllBetOpps();
							var l = this.calcPermLines(b.interests);	
							this.availableBets.addMultiples(new Bet(b,opps,null,null,l,true));
						}
					}					
				}	// HR & Dogs (Forecast and Tricast) and SP
				else if (((use_HR)||(use_VHR)||(use_VDogs)||(use_Dogs)||(use_USHR))&&(useComb)&&(!diffEidOtid)&&(hasFinancials==false)&&(hasHKRacing==false)&&(!hasTopJockeyTrainer)) {
					if ((b.id==CFCid)||((b.id==CTid)&&(useCombTri))||(b.id==RFCid)) {
						if(b.isValidBet(totOpps,y,b.id)) {
							var opps = this.getAllBetOpps();
							maxBet = bs.opportunities.length;
							if (b.id==CFCid) {
								if (maxBet == 3) {
									var l = maxBet * 2;
								} else if (maxBet == 4) {
									var l = maxBet * 3;
								} else {
									var l = maxBet * (maxBet - 1);
								}
							// If Combination Tricast then calculate sub bets
							} else if (b.id==CTid) {	
								var l = maxBet * ((maxBet - 1) * (maxBet - 2));
							} else {
								var l = b.subbets;
							}
							this.availableBets.addMultiples(new Bet(b,opps,null,null,l,false));
						} else if((b.isvalidPerm(totOpps)) && (b.id >= this._MINPERM) && (b.id <= this._MAXPERM)){
							var opps = this.getAllBetOpps();
							var l = this.calcPermLines(b.interests);	
							this.availableBets.addMultiples(new Bet(b,opps,null,null,l,true));
						}
					}
				}	
				// Normal - Multiples Based On All Selections Not Having Same Event Id & Opp Type Id
				else if ((bs.opportunities.length>0)&&(bs.opportunities.length<10)&&(diffEidOtid)&&(hasFinancials==false)&&(hasHKRacing==false)) {			

					if ((b.id!=1)&&(b.id!=Tid)&&(b.id!=CTid)&&(b.id!=FCid)&&(b.id!=RFCid)&&(b.id!=CFCid)) {
						if(b.isvalid(totOpps,sameEidOtid)) {
							var opps = this.getAllBetOpps();
							var l = b.subbets;
							this.availableBets.addMultiples(new Bet(b,opps,null,null,l,false));
						} else if((b.isvalidPerm(totOpps)) && (b.id >= this._MINPERM) && (b.id <= this._MAXPERM)){
							var opps = this.getAllBetOpps();
							var l = this.calcPermLines(b.interests);	
							this.availableBets.addMultiples(new Bet(b,opps,null,null,l,true));
						}
					}
				}
				// More Than 10 Opps - Uses #Fold
				else if ((bs.opportunities.length>=10)&&(diffEidOtid)&&(hasFinancials==false)) {
					if ((b.id>1)&&(b.maxevents==bs.opportunities.length)) {
						if(b.isvalid(totOpps,sameEidOtid)) {
							var opps = this.getAllBetOpps();
							var l = b.subbets;
							this.availableBets.addMultiples(new Bet(b,opps,null,null,l,false));
						} else if((b.isvalidPerm(totOpps)) && (b.id >= this._MINPERM) && (b.id <= this._MAXPERM)){
							var opps = this.getAllBetOpps();
							var l = this.calcPermLines(b.interests);	
							this.availableBets.addMultiples(new Bet(b,opps,null,null,l,true));
						} 
					}
				}  
		}
	}
	
// BETSLIP ArrayList
// ArrayList - extends Javascript Array
ArrayList = function() {
	this.list = new Array();
	this.length = 0;
}

ArrayList.prototype.add = function(obj) {
	this.list[this.length] = obj;
	this.length++;
}

ArrayList.prototype.remove = function(index) {
	var newArray = new Array();
	var count = 0;
	for(var i=0;i<this.length;i++) {
		if(i != index) {
			newArray[count] = this.list[i];
			count++;
		}
	}
	this.list = newArray;
	this.length = newArray.length;
}
 
ArrayList.prototype.get = function(index) {
 	return this.list[index];
}

// BETSLIP BetsCache
//BetsCache - holds all valid bets singles/mechs/perms etc...
BetsCache = function() {
	this.singles = new ArrayList();
	this.multiples = new ArrayList();
	this.perms = new ArrayList();
}

BetsCache.prototype.addSingle = function(obj) {
	this.singles.add(obj);
}

BetsCache.prototype.addMultiples = function(obj) {
	this.multiples.add(obj);
}

BetsCache.prototype.addPerms = function(obj) {
	this.perms.add(obj);
}

BetsCache.prototype.length = function() {
	var length = 0;
	length += this.singles.length;
	length += this.multiples.length;
	length += this.perms.length;
	return length;
}

//Updates A Stake --  s = The Stake, which - 0 = Singles, 1 = Multiples, 2 = Perms, id = The id To Update.
BetsCache.prototype.updateStake = function(s,id,which) {
	var stake = parseFloat(s);
	if(isNaN(stake)) {
		stake = 0;
	}
	if(which == 0) {
		this.singles.get(id).stake = parseFloat(stake);
	} else if(which == 1) {
		this.multiples.get(id).stake = parseFloat(stake);
	} else if(which == 2) {
		this.perms.get(id).stake = parseFloat(stake);
	} else {
		alert("[BetsCache.updateStake]\nError: Invalid which passed in!");
	}
}

//Updates EW -- ew = The EW Value True/False, which - 0 = Singles, 1 = Multiples, 2 = Perms, id = The Id To Update.
BetsCache.prototype.updateEW = function(ew,id,which) {
	if(which == 0) {
		this.singles.get(id).isew = ew;
	} else if(which == 1) {
		this.multiples.get(id).isew = ew;
	} else if(which == 2) {
		this.perms.get(id).isew = ew;
	} else {
		alert("[BetsCache.updateStake]\nError: Invalid which passed in!");
	}	
}

//Counts bets that have a valid stake.
BetsCache.prototype.countBets = function() {
	var retVal = 0;
	for(var i = 0; i < this.singles.length; i++) {
		if(this.singles.get(i).stake > 0) {
			retVal++;
		}
	}
	for(var i = 0; i < this.multiples.length; i++) {
		if(this.multiples.get(i).stake > 0) {
			retVal++;
		}
	}	
	for(var i = 0; i < this.perms.length; i++) {
		if(this.perms.get(i).stake > 0) {
			retVal++;
		}
	}
	return retVal;
}

// BETSLIP Bet Type Cache
//BetType Object - Contains Details About Various Bet Types.
BetType = function(id, description, interests, maxinterests, maxevents, subbets, layer) {
	this.id				= id;
	this.desc			= description;
	this.interests		= interests;
	this.maxinterests	= maxinterests;
	this.maxevents		= maxevents;
	this.subbets		= subbets;
	this.mldesc			= (layer!=null) ? layer : "";
}

BetType.prototype.setMlDesc = function(layer) {
 	this.mldesc = layer;
}
 
// x = Number of Bets -- y = Number of Events -- btid = Bet Type ID -- btype = CTR = Comb. Tricast, CF = Comb. Forecast, null = normal
BetType.prototype.isValidBet = function(x,y,btid) {
 	if ((x >= this.interests)&&(x <= this.maxinterests)&&(this.maxevents == y)) {
 		return true;
	} else {
		return false;
	}
}
 
// x = Number of Bets --    y = Number of Events
BetType.prototype.isvalid = function(x,y) {
 	if((this.interests == x)&&(this.maxevents == (y/bs.opportunities.length))) {
 		return true;
	} else {
		return false;
	}
}
 
// x = Number of Bets
BetType.prototype.isvalidPerm = function(x) {
	if(this.interests < x) {
		return true; 
	} else {
		return false;
	}
}

// BETSLIP Bet Type Cache
//bettypecache object declaration
BetTypeCache = function() {
	this.bettypes = new ArrayList();
}

// Adds bettypes to the array
BetTypeCache.prototype.addBetType = function(id,description,interests,maxinterests,maxevents,subbets,layer) {
	this.bettypes.add(new BetType(id,description,interests,maxinterests,maxevents,subbets,layer));
}

// Update function
BetTypeCache.prototype.update = function() {
}

// Return Bet Type 
BetTypeCache.prototype.getBetType = function(index) {
	return this.bettypes.get(index);
}

// Return Bet Type 
BetTypeCache.prototype.getBetTypeById = function(idValue) {
	for(var i = 0; i < this.bettypes.length; i++) {
		if(this.bettypes.get(i).id == idValue) {
			return this.bettypes.get(i);
		}
	}
}

// Returns The Bet Type By Mechanism
BetTypeCache.prototype.getBetTypeByMech = function(mech) {
	for(var i = 0; i < this.bettypes.length; i++) {
		if(this.bettypes.get(i).id == mech) {
			return this.bettypes.get(i);
		}
	}
}

// Resets the Bet Type Cache
BetTypeCache.prototype.reset = function() {
	this.bettypes = new ArrayList();
}

// Count Bet Types
BetTypeCache.prototype.count = function() {
	return this.bettypes.length;
}

// BETSLIP Opportunity
//Bet Opportinites - setup Opportunity
BetOpp = function(desc,opp,oppid,price,pricetype,ewterm,isrunning, forecast, tricast, revfc, comb, antepst, tp) {
	this.desc 		= desc;
 	this.opp 		= opp;
	this.oppid		= oppid;
	this.price 		= price;
	this.pricetype 	= pricetype;
	this.ewterm		= ewterm;
	this.isrunning  = isrunning;

	id = opp.split(".");
		this.priceid 	= id[1];
		this.eventid 	= id[3];
		this.meetingid 	= id[4];
		this.sportid	= id[5];
		
	this.forecast	= forecast;
	this.tricast	= tricast;
	this.revforecast	= revfc;
	this.combination	= comb;
	this.antepost = antepst;
	this.takeprice = tp;
}

// Returns The Price
BetOpp.prototype.getPrice = function() {
 	var retVal = 0;
	if((this.pricetype == 'F')||(this.pricetype == 'f')) {
		var f = this.price.split('/');
		var n = parseInt(f[0]);
		var d = parseInt(f[1]);
		retVal = (n/d)+1;
	} else if((this.pricetype == 'SP')||(this.pricetype == 'sp')) {
		retVal = 0;
	} else {
		retVal = this.price;
	}
	return retVal;
} 

// BETSLIP Objects
//Bet Object - Holds a possible bet
 Bet = function(bt,p,s,mw,l,c) {
 	this.betid		= this.genId();
 	this.bettype	= (bt!=null) ? bt : new BetType();
 	this.opps	 	= (p!=null) ? p : new ArrayList();
	this.stake 		= (s!=null) ? s : 0;
	this.maxwin		= (mw!=null) ? mw : 0;
	this.lines		= (l!=null) ? l : 0;
	this.isperm		= c;
	this.isew		= false;
}

// Checks Bet For InRunning
Bet.prototype.isRunning = function() {
 	for(var i = 0; i < this.opps.length; i++) {
		var o = this.opps.get(i);
		if(o.isrunning == "true") {
			return true;
		}
	}
	return false;
}

// Checks Bet For EW 
Bet.prototype.isEachWay = function() {
 	for(var i = 0; i < this.opps.length; i++) {
		var o = this.opps.get(i);
		if ((o.ewterm == "true")||(o.ewterm == true)||(o.ewterm == "Y")) {
			return true;
		}
	}
	return false;
}
 
// Generates An ID
Bet.prototype.genId = function() {
 	var d = new Date();
	return d.getTime();
}
 
// Clears The Stakes
function clearStakes() {
	deleteCookie("_singlestakes");
	deleteCookie("_multistakes");
	deleteCookie("_eachway");
	deleteCookie("_meachway");
	deleteCookie("usePromotionalCash");
}

// Resets The Betslip
function bsReset() {
	deleteCookie(bs._COOKIENAME);
	deleteCookie("_singlestakes");
	deleteCookie("_multistakes");
	deleteCookie("_eachway");
	deleteCookie("_meachway");
	deleteCookie("usePromotionalCash");
	bs.resetBetslip();
	bs = new BetSlip(2,1,10,'betslip');
}

// Deletes Cookies When The User Logs Out
function logout(url) {
	deleteCookie(bs._COOKIENAME);
	deleteCookie("_singlestakes");
	deleteCookie("_multistakes");
	deleteCookie("_eachway");
	deleteCookie("_meachway");
	deleteCookie("usePromotionalCash");
	bs.resetBetslip();
	bs = new BetSlip(2,1,10,'betslip');
	location.href = url;
}

//Function decRound - Rounds A Number & Pads With Zero's To The Number Of Decimal Places
function decRound(num, places) {
	//Round decimal places
    var x = num * Math.pow(10, places);
    x = Math.round(x);
    x = x / Math.pow(10, places);
	//Pad with zero's
	var a = x.toString();
	var b = (a.indexOf(".") == -1) ? 0 : (a.length - a.indexOf(".") - 1);
	a += ((b == 0) ? "." : "");
	var c = places - b;
	if(c > 0) {
		for(var i=0;i<c;i++) {
			a += "0";		
		}
	}	
    return a;
}

// Converts the stake to 2 decimal places
function twoDecimals(inputObj) {
	var newValue = decRound(inputObj.value,2);
	inputObj.value = newValue;
}

// function used when opportunity is clicked on the coupon coupon
function cbClickBet(price,pricetype,opp,oppid,action,ewterm,isrunning, fc, tc, rfc, comb, antepost) {
	var ir = false;
	
		if((isrunning == "Y")||(isrunning == "y")) {
		ir = true;
	}
	if((antepost == null)||(antepost == "")) {
		antepost = 'N';
	}

	// Check not more than 25 selections in betslip - if so, do not add and uncheck checkbox
	if ((bs.opportunities.length >= 25)&&(action==true)) {
		showDialog(getElText('bsmsg4'));
		tmpPriceId = opp.split(".");
		getEl(tmpPriceId[1]).checked=false;
	} else {
		// Check if exists to update price
		var oppInBS = false;
		var _theOpp = opp.split(".");
		var _pid = _theOpp[1];
		var _opp = _theOpp[2] + "." + _theOpp[3] + "." + _theOpp[4] + "." + _theOpp[5];
		
		for (var i = 0; i < bs.opportunities.length; i++) {
			var op = bs.opportunities.get(i).opp;
			var _tTheOpp = op.split(".");
			var _tPid = _tTheOpp[1].toString();
			var _tOpp = _tTheOpp[2].toString() + "." + _tTheOpp[3].toString() + "." + _tTheOpp[4].toString() + "." + _tTheOpp[5].toString();
			
			// _pid = new price id      _tPid= price id in Betslip
			//      _opp = new opp      _tOpp= opp in betslip
			if ((_pid != _tPid) && (_opp == _tOpp)) {
				oppInBS = true;
				bs.updatePrice(i, "O." + _pid + "." + _tOpp, price, pricetype);
			} else if ((_pid == _tPid) && (_opp == _tOpp)) {
				oppInBS = action;
			}
		}
		if (oppInBS == false) {
			bs.handleBet(price,pricetype,opp,oppid,action,ewterm,ir,fc, tc, rfc, comb, antepost, 'Y');
		}
	}
}

// Updates The Coupon When Opps Are Removed
function updateCoupon(form) {
	if(form!=null && form.elements) {
		for(var j = 0; j < form.elements.length; j++) {
			if(form.elements[j].type == "checkbox") {
				form.elements[j].checked = false;
			}
		}			
	 	for(var i = 0; i < bs.opportunities.length; i++) {
			var tmp_Id = bs.opportunities.get(i).opp.split(".");
			var tmp_PriceId = tmp_Id[1];
			var tmpElement = document.getElementById(tmp_PriceId);
			
			if (tmpElement) {
				tmpElement.checked = true;
			}
		}
	}
}

//updates the stakes when an opportunity is removed
function updateStakes(bform, pos) {
var ss = '';
	for(var i = 0; i < pos; i++) {
		if (getEl('stake'+i)) {
				ss += "[";
				ss += getEl('stake'+i).name;
				ss += "::";
				ss += getEl('stake'+i).value;
				ss += "]";
		}
	}
	for(var i = pos; i < bs.opportunities.length; i++) {
		if (getEl('stake'+i)) {
			if (getEl('stake'+(i+1))) {
				getEl('stake'+i).value = getEl('stake'+(i+1)).value;
			}
				ss += "[";
				ss += getEl('stake'+i).name;
				ss += "::";
				ss += getEl('stake'+i).value;
				ss += "]";
		}
	}
	
	setCookie("_singlestakes",ss);
	deleteCookie("_eachway");
	deleteCookie("_multistakes");
	deleteCookie("_meachway");
	drawBetslip();
}

// Needs to be here for coupons.  Proper function called on the betslip page
function drawBetslip() {};

//view the betslip button - will show an alert if no entries are found in the betslip
function viewBetSlip() {
		switchDiv('mainContent','fullBetSlip');
}

// betslip succes page - checks for retain selections
function checkAfterBet() {
	tmpCookie = getCookie('retainSelections');
	if (tmpCookie != 'Y') {
		deleteCookie('retainSelections');
		bsReset();
	}
}

// Returns The Value of An Element
function getElText(elementName) {
	return document.getElementById(elementName).innerHTML
}

// Returns An Element
function getEl(elementName) {
	return document.getElementById(elementName)
}

// Initialise the betslip
var bs = new BetSlip(2,1,10,'betslip');
