
//<script>
var oddlist
function formatBSMultiW(grp)
// USED FOR ADDING BETS TO THE BETTING SLIP
{
var mf;
var i;
var nb =0;
var nr =0;
var t;
oddlist = "";
for (i=0;i<document.forms.length;i++) {
mf = document.forms[i];
if(mf.name.indexOf("SubmitBetsW")>=0) {
if(mf.bslist != null){
if(mf.bslist.value){
nr = 1;
}else{
//it's an array
nr = mf.bslist.length;
}
if (nr==1) {
if (mf.bslist.checked == true) {
nb++;
if (mf.curroddlist!=null) {
if (mf.curroddlist.checked == true) {
getoddlist(mf.curroddlist.value);
}
else {
// else use the SP price
getoddlist(mf.bslist.value);
}
}
else {
getoddlist(mf.bslist.value);
}
} // end if addtobetslip checked
}
else {
//alert(" bslist length ="+nr);
for(var j=0; j<nr;j++) {
// 		if the add to betslip is checked for this row
if (mf.bslist[j].checked == true) {
nb++;
if (mf.curroddlist!=null) {
if (mf.curroddlist[j].checked == true) {
// if the current odd is chosen
getoddlist(mf.curroddlist[j].value);
}
else {
// else SP value used
getoddlist( mf.bslist[j].value);
} // get currentodd checked
} 
else {
getoddlist(mf.bslist[j].value);	
}	
}
// add the selection box option to the oddlist (if > 1 dropdown)
if (mf.selectsign != null) {
if(!mf.selectsign.value) {
t=mf.selectsign[j];	
if (t != null) {
if (t.value != "0") {	
nb++;
getoddlist(t.value);
}
}
}
}
}
}
// if just one dropdown
if (mf.selectsign != null) {
if(mf.selectsign.value) {
// if just one dropdown
t=mf.selectsign;	
if (t != null) {
if (t.value != "0") {	
nb++;
getoddlist(t.value);
}
}
}
}
}
if (mf.favbslist!=null) {
if (mf.favbslist.checked == true) {
nb++;
getoddlist(mf.favbslist.value);
}
}	
}
}
if (nb == 0) {
alert("Please highlight a selection to add to the betslip.");
return false;
}
document.forms["SubmitBetsW"+grp].oddlist.value = oddlist;
return true;
}
function getoddlist(tempstr) {	
position = tempstr.search(':');
if (oddlist == "")
oddlist = tempstr.substr(0,position);
else
oddlist += "^" +tempstr.substr(0,position);
}


function jsTryAddToSlip(grp){

	if(formatBSMultiW(grp)){
		//document.getElementById("SubmitBetsW"+grp).submit();
        f = document.getElementById("SubmitBetsW"+grp);
        var strOdd = '' ;
        for(var x=0;x<f.length;x++){
            strOdd += f[x].name+'='+f[x].value+'&';
        }
		try {
			addToMiniSlip(strOdd)
		} catch (err) {
			document.getElementById("SubmitBetsW"+grp).submit();
		}
	}

}


function jsTryReset(grp){document.getElementById("SubmitBetsW"+grp).reset();}

function jsStakeSynchronize(sElementIdFrom, sElementIdTo) {
	var oFrom 	= document.getElementById(sElementIdFrom);
	var oTo 	= document.getElementById(sElementIdTo);
	if (oTo && oFrom) {
		oTo.value = oFrom.value;	
	}
}//<script>
function submitsinglebets(grp,btype) {
	// USED FOR SUBMITTING SINGLE BETS TO THE CONFIRM PAGE
	var mf=document.forms["SubmitBetsW"+grp];	
	var tempstr;
	var tempstr2;	
	var pos1;
	var winodd;
	var ewodd;
	var arcwinodd;
	var nb = 0;
	var cp = 0;
	var p_sing = "";
	var p_sing_stake = "";
	var p_arc = "";
	var p_arcew = "";
	var p_singew = "";
	
	for (i=0;i<document.forms.length;i++) {
		if(document.forms[i].name=='minislip'){ continue; }
		mf = document.forms[i];
		
		if(mf.name.indexOf("SubmitBetsW")>=0){	
			if(mf.bslist.value){
				nr = 1;
			}else{
				//it's an array
				nr = mf.bslist.length;
			}
			
		if (nr == 1) {
			if (mf.p_stk != null) {	
				if (mf.p_stk.value > 0) {
					nb = 1;
					cp=0;
					tempstr = mf.bslist.value;
					if (mf.curroddlist!=null) {
						if (mf.curroddlist.checked == true) {
							tempstr = mf.curroddlist.value;
							cp=1;
						}
					}
					pos1 = tempstr.search(',');	
					// get the win oddid and append it to the p_sing variable
					winodd = tempstr.substr(0,pos1);
					p_sing += winodd + "^";
					// get the win archiveodd and append to the p_arc variable
					tempstr2 = tempstr.substr(pos1+1);
					pos1 = tempstr2.search(',');
					tempstr2 = tempstr2.substr(pos1+1);
					pos1 = tempstr2.search(':');
					arcwinodd = tempstr2.substr(0,pos1);
					p_arc += arcwinodd + "^";
					if (mf.ewoddlist != null) {
						if (mf.ewoddlist.checked == true) {
							archiveoddid = buildsingstake(tempstr,winodd,mf.p_stk.value,1);
							p_arcew += archiveoddid + "^";
							p_sing_stake += winodd + "=" +mf.p_stk.value + "=1=" + archiveoddid + "^";
							if (cp == 1) {
								tempstr = mf.ewoddlist.value;
								pos1 = tempstr.search(',');	
								// get the ew oddid and append it to the p_singew variable
								Ewodd = tempstr.substr(0,pos1);
								p_singew += ewodd + "^";	
							}else{
								pos1 = tempstr.search(':');
								tempstr2 = tempstr.substr(pos1+1);
								pos1 = tempstr2.search(',');
								ewodd = tempstr2.substr(0,pos1);
								p_singew += ewodd + "^";	
							}
						}else{
							archiveoddid = buildsingstake(tempstr,winodd,mf.p_stk.value,0);
							p_arcew += "0^";
							p_sing_stake += winodd + "=" +mf.p_stk.value + "=1=" + archiveoddid + "^";
							p_singew += "0^";
						}
					}else{
						archiveoddid = buildsingstake(tempstr,winodd,mf.p_stk.value,0);
						p_sing_stake += winodd + "=" +mf.p_stk.value + "=0=" + archiveoddid + "^";
					}	
				}
			}
			
		// more than 1 row	
		}else{
			for(var j=0; j<nr;j++) {
				if (mf.p_stk != null) {
					if (mf.p_stk[j].value > 0) {
						nb++;
						cp=0;
						tempstr = mf.bslist[j].value;
						if (mf.curroddlist!=null) {
							if (mf.curroddlist[j].checked == true) {
								tempstr = mf.curroddlist[j].value;
								cp=1;
							}
						}
						pos1 = tempstr.search(',');	
						// get the win oddid and append it to the p_sing variable
						winodd = tempstr.substr(0,pos1);
						p_sing += winodd + "^";
						// get the win archiveodd and append to the p_arc variable
						tempstr2 = tempstr.substr(pos1+1);
						pos1 = tempstr2.search(',');
						tempstr2 = tempstr2.substr(pos1+1);
						pos1 = tempstr2.search(':');
						arcwinodd = tempstr2.substr(0,pos1);
						p_arc += arcwinodd + "^";
						if (mf.ewoddlist != null) {
							if (mf.ewoddlist[j].checked == true) {
								archiveoddid = buildsingstake(tempstr,winodd,mf.p_stk[j].value,1);
								p_arcew += archiveoddid + "^";
								p_sing_stake += winodd + "=" +mf.p_stk[j].value + "=1=" + archiveoddid + "^";
								if (cp == 1) {
									tempstr = mf.ewoddlist[j].value;
									pos1 = tempstr.search(',');	
									// get the ew oddid and append it to the p_singew variable
									ewodd = tempstr.substr(0,pos1);
									p_singew += ewodd + "^";
								}else{
									pos1 = tempstr.search(':');
									tempstr2 = tempstr.substr(pos1+1);
									pos1 = tempstr2.search(',');
									ewodd = tempstr2.substr(0,pos1);
									p_singew += ewodd + "^";
								}
							}else{
								archiveoddid = buildsingstake(tempstr,winodd,mf.p_stk[j].value,0);
								p_arcew += "0^";
								p_sing_stake += winodd + "=" +mf.p_stk[j].value + "=0=" + archiveoddid + "^";
								p_singew += "0^";
							}
						}else{
								archiveoddid = buildsingstake(tempstr,winodd,mf.p_stk[j].value,0);
								p_sing_stake += winodd + "=" +mf.p_stk[j].value + "=0=" + archiveoddid + "^";
						}
					}
				}	
			}
		}	// end else 1 row

		// unnamed favourite 
		if(mf.favbslist!= null) {
			if (mf.favp_stk.value > 0) {
				nb++;
				tempstr = mf.favbslist.value;
				pos1 = tempstr.search(',');	
				// get the win oddid and append it to the p_sing variable
				winodd = tempstr.substr(0,pos1);
				p_sing += winodd + "^";
				// get the win archiveodd and append to the p_arc variable
				tempstr2 = tempstr.substr(pos1+1);
				pos1 = tempstr2.search(',');
				tempstr2 = tempstr2.substr(pos1+1);
				pos1 = tempstr2.search(':');
				arcwinodd = tempstr2.substr(0,pos1);
				p_arc += arcwinodd + "^";
			if (mf.favewoddlist != null) {
				if (mf.favewoddlist.checked == true) {
					archiveoddid = buildsingstake(tempstr,winodd,mf.favp_stk.value,1);
					p_arcew += archiveoddid + "^";
					p_sing_stake += winodd + "=" +mf.p_stk.value + "=1=" + archiveoddid + "^";

					pos1 = tempstr.search(':');
					tempstr2 = tempstr.substr(pos1+1);
					pos1 = tempstr2.search(',');
					ewodd = tempstr2.substr(0,pos1);
					p_singew += ewodd + "^";	
				}else {
					archiveoddid = buildsingstake(tempstr,winodd,mf.favp_stk.value,0);
					p_arcew += "0^";
					p_sing_stake += winodd + "=" +mf.favp_stk.value + "=1=" + archiveoddid + "^";
					p_singew += "0^";
				}
			}else{
				archiveoddid = buildsingstake(tempstr,winodd,mf.favp_stk.value,0);
				p_sing_stake += winodd + "=" +mf.favp_stk.value + "=0=" + archiveoddid + "^";
				if (mf.ewoddlist != null) {
					p_arcew += "0^";
					p_singew += "0^";
				}	
			}	
		}
	}
}
}

	if (nb == 0) {
		alert("Please enter a stake before placing a bet.");
		return false;
	}
	
	//mf = document.forms["SubmitBetsW"+grp];
	if (btype =='U') {
		mf.p_uniq_stake.value = p_sing_stake;
		mf.p_uniq.value = p_sing;
	} else {
		mf.p_sing_stake.value = p_sing_stake;
		mf.p_sing.value = p_sing;
	}

	mf.p_arc.value = p_arc;
	mf.p_arcew.value = p_arcew;
	mf.p_singew.value = p_singew;
	mf.action = "/sbuk.go?page=betconfirm";
	mf.submit();
	return true;
}

function buildsingstake(tempstr,winodd,stk,ew) {
var archiveoddid;	
pos1 = tempstr.search(':');
if (ew)
	tempstr2 = tempstr.substr(pos1);
else
	tempstr2 = tempstr.substr(0,pos1);	
// until first comma (oddsvalue)
pos1 = tempstr2.search(',');
tempstr = tempstr2.substr(pos1+1);
// until second comma archive odd id (better way?)
pos1 = tempstr.search(',');
tempstr2 = tempstr.substr(pos1+1);
archiveoddid = tempstr2;
return archiveoddid;
}

function formatOdd(price, frac){
if (price == -5.35) return 'SP';
if (price <= 1) return '';

p = price;

switch (frac)
{
case 'D': //decimal display
if (price <= 1) $p = '';
else if( Math.floor(price) == price) p = price+'.00';
else if( Math.floor(10*price) == 10*price) p = price+'0';
break;

case 'F': //fraction display
switch (price)
{
case 1.03:
p = '1/33';
break;
case 1.07:
p = '1/16';
break;
case 1.08:
p = '1/14';
break;
case 1.09:
p = '1/12';
break;
case 1.12:
p = '1/9';
break;
case 1.13:
p = '1/8';
break;
case 1.14:
p = '2/15';
break;
case 1.15:
p = '1/7';
break;
case 1.16:
p = '2/13';
break;
case 1.17:
p = '1/6';
break;
case 1.19:
p = '2/11';
break;
case 1.23:
p = '2/9';
break;
case 1.29:
p = '2/7';
break;
case 1.34:
p = '1/3';
break;
case 1.37:
p = '4/11';
break;
case 1.40:
p = '2/5';
break;
case 1.45:
p = '4/9';
break;
case 1.54:
p = '8/15';
break;
case 1.58:
p = '4/7';
break;
case 1.62:
p = '8/13';
break;
case 1.67:
p = '4/6';
break;
case 1.73:
p = '8/11';
break;
case 1.84:
p = '5/6';
break;
case 1.91:
p = '10/11';
break;
case 2:
p = 'Evs';
break;
case 2.38:
p = '11/8';
break;
case 2.5:
p = '6/4';
break;
case 2.63:
p = '13/8';
break;
case 2.80:
p = '9/5';
break;
case 2.88:
p = '15/8';
break;
case 4.34:
p = '10/3';
break;
case 1:
p = "";
break;
default:
var n = (price-1)*1000.0;
var d = 1000.0;	 						
for (var i=100; i>1; i--)
{
x = n%i;
y = d%i;
if ( (x==0)&&(y==0) ){
n /= i;
d /= i;
}
while (x==0&&y==0){
x = n%i;
y = d%i;
if (x==0&&y==0){
n /= i;
d /= i;
}
}
}
p = n+'/'+d;
break;
}
break;

case 'A': //american display
if(price<=1) p="";
else if (price == 2) p = 'Evens';
else if (price > 2) p = '+'+((price-1)*100);
else p = Math.floor(-100/(price-1));
break;

default:
p = price;
break;
}

return p;
}

function notloggedin() {
alert('Please login to place a bet');
}

function jsBetNow(grp,btype)  { jsTryBetNow(grp,btype) }
function jsTryBetNow(grp,btype){
	if(!btype){
		btype='S';
	}
	submitsinglebets(grp,btype)}
//function jsTryBetNow(grp,btype){if(!btype){btype='S';}if(submitsinglebets(grp,btype)){document.getElementById("SubmitBetsW"+grp).submit();}}
