// validation
// all numbers whittle to 2 dps
// CLEAR button ( & submit button ?)

//define global
var btot = 10;
var linnum = new Array(btot);
var numodd = new Array(btot);
var denodd = new Array(btot);
var decodd = new Array(btot);
var calodd = new Array(btot);
var winlos = new Array(btot);
var winpln = new Array(btot);
var numfld = new Array(btot);
var txtfld = new Array(btot);
var linent = new Array(btot);
var stkfld = new Array(btot);
var cosfld = new Array(btot);
var linwon = new Array(btot);
var runtot = new Array(btot);
var retuni = new Array(btot);
var retmon = new Array(btot);
var fradec;
var betnam;
var mul=new Array(btot);
var mul0=new Array('All          ','Accumulator  ','All -Singles ','Singles Only ','FoldLineLevel')
var mul1=new Array('Single       ','Single       ','No Bets      ','Single       ','FoldLineLevel')
var mul2=new Array('All          ','Double       ','Double       ','Singles Only ','FoldLineLevel')
var mul3=new Array('Patent       ','Treble       ','Trixie       ','Singles Only ','FoldLineLevel')
var mul4=new Array('Lucky 15     ','4-Fold       ','Yankee       ','Singles Only ','FoldLineLevel')
var mul5=new Array('Lucky 31     ','5-Fold       ','Canadian     ','Singles Only ','FoldLineLevel')
var mul6=new Array('Lucky 63     ','6-Fold       ','Heinz        ','Singles Only ','FoldLineLevel')
var mul7=new Array('All          ','7-Fold       ','Super Heinz  ','Singles Only ','FoldLineLevel')
var mul8=new Array('All          ','8-Fold       ','Goliath      ','Singles Only ','FoldLineLevel')
var mul9=new Array('All          ','9-Fold       ','All -Singles ','Singles Only ','FoldLineLevel')
var mul10=new Array('All          ','10-Fold      ','All -Singles ','Singles Only ','FoldLineLevel')
mul[0]=mul0;mul[1]=mul1;mul[2]=mul2;mul[3]=mul3;mul[4]=mul4;mul[5]=mul5;mul[6]=mul6;mul[7]=mul7;mul[8]=mul8;mul[9]=mul9;mul[10]=mul10;

txtfld[0]='Singles';
txtfld[1]='Doubles';
txtfld[2]='Trebles';
txtfld[3]='4-Folds';
txtfld[4]='5-Folds';
txtfld[5]='6-Folds';
txtfld[6]='7-Folds';
txtfld[7]='8-Folds';
txtfld[8]='9-Folds';
txtfld[9]='10-Folds';

// parameter arrays
var aParNam = new Array();
var aParSet = new Array();
var aSeq = new Array();

var bookmarkurl;
var bookmarkname;

//====================================================================
function init() {

getParms();

appParms();

document.inp.dod[0].focus();

chg();

}

//====================================================================
function clr() {
	chg();
}

//====================================================================

function chg() {
//value changed
//alert('chg');

//get variables

if (document.inp.ford[0].checked) {
	fradec='F';
}
if (document.inp.ford[1].checked) {
	fradec='D';
}
var bettyp=document.inp.typ.value;
var staall=document.inp.sta.value;
for (i = 0; i < btot; i++){
numodd[i]=document.inp.num[i].value;
denodd[i]=document.inp.den[i].value;
decodd[i]=document.inp.dod[i].value;
winlos[i]=document.inp.wls[i].value;
stkfld[i]=document.inp.stk[i].value;
}

//shade/disable alternative fields
//alert(fradec);
for (i = 0; i < btot; i++){
if (fradec=='F'){
	document.inp.den[i].style.background="#DEEEFF";
	document.inp.den[i].disabled=false;
	document.inp.num[i].style.background="#DEEEFF";
	document.inp.num[i].disabled=false;
	document.inp.dod[i].style.background="#dddddd";
	document.inp.dod[i].disabled=true;
	}
else {
	document.inp.den[i].style.background="#dddddd";
	document.inp.den[i].disabled=true;
	document.inp.num[i].style.background="#dddddd";
	document.inp.num[i].disabled=true;
	document.inp.dod[i].style.background="#DEEEFF";
	document.inp.dod[i].disabled=false;
	}
if (document.inp.comc.checked) {
	document.inp.com[i].style.background="#DEEEFF";
	document.inp.com[i].disabled=false;
}
else {
	document.inp.com[i].style.background="#dddddd";
	document.inp.com[i].disabled=true;
	}
if (bettyp=='5'){
	document.inp.stk[i].style.background="#DEEEFF";
	document.inp.stk[i].disabled=false;
	document.inp.sta.style.background="#dddddd";
	document.inp.sta.disabled=true;
	}
else {
	document.inp.stk[i].style.background="#FFEECC";
	document.inp.stk[i].disabled=true;
	document.inp.sta.style.background="#DEEEFF";
	document.inp.sta.disabled=false;
	}
}

//validation
var error=0;
for (i = 0; i < btot; i++){
if (fradec!='F'){
if (decodd[i]>0) {
if (decodd[i]<1.01 || decodd[i]>10000) {
alert('Decimal Odds ['+decodd[i]+'] invalid. Line number '+(i+1)+'.\n\n Must be between 1.01 & 10,000');
document.inp.dod[i].focus();
var error=1;
}
}
}
if (stkfld[i]>0) {
if (stkfld[i]<0.01 || stkfld[i]>10000) {
alert('Stake ['+stkfld[i]+'] invalid. Fold number '+(i+1)+'.\n\n Must be between 0 & 10,000');
document.inp.stk[i].focus();
var error=1;
}
stkfld[i]=parseFloat(fmtNum(stkfld[i]));
stkfld[i]=fmtNum(stkfld[i]);
}
}
if (staall>0) {
if (staall<0 || staall>10000) {
alert('Overall Stake ['+staall+'] invalid.\n\n Must be between 0 & 10,000');
document.inp.sta.focus();
var error=1;
}
staall=parseFloat(fmtNum(staall));
staall=fmtNum(staall);
}
if (error==0){

//set definitions
if (staall==''){
	staall=1;
staall=parseFloat(fmtNum(staall));             //tg 03/03/04
staall=fmtNum(staall);                         //tg 03/03/04
}
for (i = 0; i < btot; i++){
}
var lin=0;
var win=0;

// line by line
for (i = 0; i < btot; i++){

//odds
calodd[i]=' ';
if (fradec!='F'){
	if (decodd[i]>0) {
	calodd[i]=decodd[i];
	}
	//denodd[i]='';
	//numodd[i]='';
}
else {
	if (numodd[i]>0){
//		if (denodd[i]=='') {
//			denodd[i]=1;
//		}
	calodd[i]=(numodd[i]/denodd[i])+1;
	calodd[i]=fmtNum(calodd[i]);
	}
	else {
		denodd[i]='';
	}
	//decodd[i]='';
}


//line# / winlose / winperline
linnum[i]=' ';
winpln[i]=' ';
if (calodd[i]==' ') {
	winlos[i]=2;
}

if (calodd[i]>=1) {
lin=lin+1;
linnum[i]=lin;
//set to win if was blank
if (winlos[i]==2) {
winlos[i]=1;
}
// set to win
//if (winlos[i]!=0) {
//winlos[i]=1;
//}
if (winlos[i]==1) {
win=win+1;
}
winpln[i]=calodd[i]*winlos[i];
//winpln[i]=fmtNum(winpln[i]);
}

}
//bet type
var betnam=mul[lin][bettyp-1];
//alert(bettyp+'..'+mul[lin]+'..'+mul[lin][bettyp-1]+'..'+betnam);

// result table calculations
var linfac=1;
for (f=lin; f>0; f--){
	linfac=linfac*f;
}
var winfac=1;
for (f=win; f>0; f--){
	winfac=winfac*f;
}

var retunitot=1;

for (i = 0; i < btot; i++){

	// #fold
	numfld[i]=' ';
	if (lin>i) {
		//numfld[i]=i+1;
		numfld[i]=txtfld[i];
	}

	// #lines entered
	linent[i]=' ';
	if (lin>=i+1) {
		var ifac=1;
		for (f=i+1; f>0; f--){
			ifac=ifac*f;
		}
		var dfac=1;
		for (f=(lin-(i+1)); f>0; f--){
			dfac=dfac*f;
		}
		var d=lin-(i+1);
		linent[i]=linfac/(ifac*dfac);
	}

	//stake&cost
	cosfld[i]='';
	if (bettyp!='5'){
		stkfld[i]='';
	}
	if (lin>i) {
	//stake
	if (bettyp=='1'){
		stkfld[i]=staall;
	}
	if (bettyp=='2'){
		if (i==(lin-1)){
			stkfld[i]=staall;
		}
		else{
			stkfld[i]=0;
		}
	}
	if (bettyp=='3'){
		stkfld[i]=staall;
		if (i==0){
			stkfld[i]=0;
		}
	}
	if (bettyp=='4'){
		stkfld[i]=0;
		if (i==0){
			stkfld[i]=staall;
		}
	}
	//cost
	if ((lin-1)>=i){
	cosfld[i]=linent[i]*stkfld[i];
	}
	}

	// #lines won
	linwon[i]=' ';
	if (lin>=i+1) {
		linwon[i]=0;
	}
	if (win>=i+1) {
		var ifac=1;
		for (f=i+1; f>0; f--){
			ifac=ifac*f;
		}
		var dfac=1;
		for (f=(win-(i+1)); f>0; f--){
			dfac=dfac*f;
		}
		var d=win-(i+1);
		linwon[i]=winfac/(ifac*dfac);
	}

	//total return per unit
	if (winpln[i]>0) {
		retunitot=retunitot*(winpln[i]+1);
	}

}

// total return per unit line
var retone=0;
for (i = 0; i < btot; i++) {
runtot[i]=winpln[i];
if (winpln[i]>0){
retone=retone+winpln[i]
}
retuni[i]='';
retmon[i]='';
}
retuni[0]=retone;
retmon[0]=retuni[0]*stkfld[0];

for (i = 1; i <= (lin-1); i++) {
var prd=0;
//alert('I='+i+' lin='+lin+' win='+win);
for (j = (lin-1); j >= i; j--) {
var sum=0;
//alert('J='+j);
for (k = j-1; k >= (i-1); k--) {
sum=sum+runtot[k];
//alert('K='+k+'. sum='+sum+'.++ runtotK='+runtot[k]);
}
runtot[j]=winpln[j]*sum;
prd=prd+runtot[j];
//alert('J='+j+'. prd='+prd+'.++ runtotJ='+runtot[j]+'. (=winplnJ='+winpln[j]+' * sum '+sum+')');
}
retuni[i]=prd
retmon[i]=retuni[i]*stkfld[i];
//alert('I='+i+'. retuniI='+retuni[i]);
}

//table totals
var linenttot=0;
var cosfldtot=0;
var linwontot=0;
var retmontot=0;
for (i = 0; i < btot; i++) {
	if (i<lin){
		linenttot=linenttot+linent[i];
		cosfldtot=cosfldtot+cosfld[i];
		linwontot=linwontot+linwon[i];
	}
	if (retmon[i]>0) {
		retmontot=retmontot+retmon[i];
		retmon[i]=fmtNum(retmon[i]);
	}
}

//check btot retuni = retunitot
retunitot=retunitot-1;

prolos=retmontot-cosfldtot;
if (cosfldtot>0)
prolosper=(retmontot/cosfldtot);
else
prolosper=0;

//alert(prolos+'= '+retmontot+' - '+cosfldtot);

//round to 2 dps
retunitot=fmtNum(retunitot);
retmontot=fmtNum(retmontot);
if (prolos>=0){
prolos=fmtNum(prolos);
}
else {
prolos=prolos*-1;
prolos='-' + fmtNum(prolos);
//prolos=(fmtNum(prolos*-1))*-1;
//prolosper='-' + fmtNum(prolosper);
}
prolosper=fmtNum(prolosper) + '@1';

//} //end of error skip

//set variables
for (i = 0; i < btot; i++) {
document.inp.lne[i].value=linnum[i];
document.inp.num[i].value=numodd[i];
document.inp.den[i].value=denodd[i];
document.inp.dod[i].value=decodd[i];
//document.inp.cod[i].value=calodd[i];
document.inp.wls[i].value=winlos[i];
//document.inp.wpl[i].value='';
//if ((lin-1)>=i) {
//document.inp.wpl[i].value=fmtNum(winpln[i]);
//}
document.inp.fld[i].value=numfld[i];
document.inp.len[i].value=linent[i];
document.inp.stk[i].value=stkfld[i];
if ((lin-1)>=i) {
document.inp.csf[i].value=fmtNum(cosfld[i]);
}
else {
document.inp.csf[i].value='';
}
document.inp.lwn[i].value=linwon[i];
//document.inp.run[i].value='';
document.inp.rmo[i].value='';
if ((lin-1)>=i) {
//document.inp.run[i].value=fmtNum(retuni[i]);
document.inp.rmo[i].value=fmtNum(retmon[i]);
}
}

//totals
//document.inp.lin.value=lin;
//document.inp.win.value=win;
document.inp.sta.value=staall;
document.inp.nam.value=betnam;
//document.inp.fl9.value='TOTALS';
document.inp.le9.value=linenttot;
document.inp.cs9.value=fmtNum(cosfldtot);
document.inp.lw9.value=linwontot;
//document.inp.mu9.value=retunitot;
document.inp.ms9.value=retmontot;
document.inp.pc9.value=prolosper;
document.inp.pl9.value=prolos;

} //end of error skip

} //end function chg

//====================================================================
//calc factorial
//function fac(f) {
//for (x=f; f=; f--){
//}
//}

//====================================================================
function setUrl() {
//set bookmark

bookmarkurl = "http://www.tisnt.com/bet/index.html";
if (document.inp.ford[0].checked)
   bookmarkurl = bookmarkurl + "?ford=F";
if (document.inp.ford[1].checked)
   bookmarkurl = bookmarkurl + "?ford=D";
if (document.inp.comc.checked)
   bookmarkurl = bookmarkurl + "&comc=Y";
bookmarkurl = bookmarkurl + "&typ=" + document.inp.typ.value;
bookmarkurl = bookmarkurl + "&sta=" + document.inp.sta.value;
for (i = 0; i < btot; i++) {
if (document.inp.num[i].value!='')
   bookmarkurl = bookmarkurl + "&num" + i + "=" + document.inp.num[i].value;
if (document.inp.den[i].value!='')
   bookmarkurl = bookmarkurl + "&den" + i + "=" + document.inp.den[i].value;
if (document.inp.dod[i].value!='')
   bookmarkurl = bookmarkurl + "&dod" + i + "=" + document.inp.dod[i].value;
if (document.inp.wls[i].value=='0')
   bookmarkurl = bookmarkurl + "&wls" + i + "=" + document.inp.wls[i].value;
if (document.inp.com[i].value!='')
   bookmarkurl = bookmarkurl + "&com" + i + "=" + document.inp.com[i].value;
if (document.inp.stk[i].value!='')
   bookmarkurl = bookmarkurl + "&stk" + i + "=" + document.inp.stk[i].value;
}
bookmarkname = "tisnt Bet ";
var d = new Date()
//bookmarkname = bookmarkname + d;
bookmarkname = bookmarkname + d.getFullYear() + "." + (d.getMonth()+1) + "." + d.getDate();
//bookmarkname = bookmarkname + d.getDate() + "/" + (d.getMonth()+1) + "/" + d.getFullYear();
//document.write(d.getDate())
//document.write(".")
//document.write(d.getMonth() + 1)
//document.write(".")
//document.write(d.getFullYear())
var tm=(d.getMinutes())
tm =((tm<10)?"0":"")+tm;
bookmarkname = bookmarkname + " " + d.getHours() + "." + tm;
//document.write(d.getHours() + ":" + tm)
//**document.write(".")
//**document.write(d.getSeconds())
for (i = 0; i < btot; i++) {
if (document.inp.com[i].value!='')
bookmarkname = bookmarkname + " " + document.inp.com[i].value;
}

}

//====================================================================
function addbookmark() {
//set bookmark

setUrl();
//input alert - suggest title and get changes to title
if (document.all)
  window.external.AddFavorite(bookmarkurl, bookmarkname);
}

function sendemail() {
//supply subject and body (html with link)
setUrl();
bookmarkurl = Switch(bookmarkurl,"&","%26");
bookmarkname = Switch(bookmarkname,"&","%26");
var emailbody="<a href=" + bookmarkurl + ">" + bookmarkname + "</a>" + "%0A%0Awww.tisnt.com/bet";
var emailbody=bookmarkurl  + "%0A%0Awww.tisnt.com/bet";
var emailsubject="tisnt bet";
var emailsubject=bookmarkname;
//alert(emailbody);
//window.open("mailto:<emailaddress>?Cc=betcc@tisnt.com&subject=" + emailsubject + "&body=" + emailbody);
window.open("mailto:<emailaddress>?subject=" + emailsubject + "&body=" + emailbody);
}

function copytoClipboard() {
setUrl();
window.clipboardData.setData("Text", bookmarkurl);
alert("The url link for your calculations has been copied to your clipboard.\n This can now be pasted into any email, forum posting, or browser address.\t");
}

//function hlp() {
//tisntbetwin=window.open("help.htm", "tisntbetwin", "width=350, height=600, left=0, top=0, menubar=no, status=no, location=no, toolbar=no, scrollbars=yes, resizable=yes");
//tisntbetwin.focus();
////parent.href.location="help.htm";
//}

//function abt() {
//tisntbetwin=window.open("about.htm", "tisntbetwin", "width=350, height=600, left=0, top=0, menubar=no, status=no, location=no, toolbar=no, scrollbars=yes, resizable=yes");
//tisntbetwin.focus();
//}

//function lnk() {
//tisntbetwin=window.open("links.htm", "tisntbetwin", "width=350, height=600, left=0, top=0, menubar=no, status=no, location=no, toolbar=no, scrollbars=yes, resizable=yes");
//tisntbetwin.focus();
//}

function sam() {
tisntbetwin=window.open("sample.htm", "tisntbetwin", "width=150, height=600, left=0, top=0, menubar=no, status=no, location=no, toolbar=no, scrollbars=yes, resizable=yes");
tisntbetwin.focus();
//parent.href.location="links.htm";
}

function fmtNum(value) {
	result=Math.floor(value)+".";
	var decimalplaces=100* (value-Math.floor(value)) + 0.5;

	if(decimalplaces > 100){
		result = Math.floor(value)+1+".00";
	}else{
	   	result += Math.floor(decimalplaces/10);
		result += Math.floor(decimalplaces%10);
	}
	//var numresult=parseFloat(result);
	return result;
}

//====================================================================
function appParms() {

//alert(aParNam.length);
var j=0;
for (j=0; j<aParNam.length; ++j) {
//swap %20 for blanks 
aParSet[j] = Switch(aParSet[j],"%20","-");
//test if xxx[0] exists ?
//alert("a document.inp."+aParNam[j].substr(0,3)+"["+aParNam[j].substr(3,1)+"]"+".value='"+aParSet[j]+"'");
//if (aParNam[j].length==4) {
if (aParNam[j]=='ford') {
//alert(aParSet[j]);
if (aParSet[j]=='F')
document.inp.ford[0].checked=true;
if (aParSet[j]=='D')
document.inp.ford[1].checked=true;
}
else {
if (aParNam[j]=='comc') {
if (aParSet[j]=='Y')
document.inp.comc.checked=true;
}
else {
if (aParNam[j].substr(3,1)>='0' && aParNam[j].substr(3,1)<='9') {
//  alert("4 document.inp."+aParNam[j].substr(0,3)+"["+aParNam[j].substr(3,1)+"]"+".value='"+aParSet[j]+"'");
  eval("document.inp."+aParNam[j].substr(0,3)+"["+aParNam[j].substr(3,1)+"]"+".value='"+aParSet[j]+"'");
}
else {
//  alert("3 document.inp."+aParNam[j].substr(0,3)+".value='"+aParSet[j]+"'");
  eval("document.inp."+aParNam[j].substr(0,3)+".value='"+aParSet[j]+"'");
}
}
}
}

//alert(pfd[0]);
//document.inp.ford.value=pfd[0];
//document.inp.comc.value=pcm[0];
//document.inp.typ.value=ptp[0];
//document.inp.sta.value=pst[0];

}

//====================================================================
function getParms() {

//get parameters 2
var _l = document.location.toString();
//alert(_l);
    // This next line is here for this test for show only.
    // normally you would remove it.
//////    _l = "http://www.js-examples.com/?param1=newval1&param2=secondval&param3=thirdone&param4=numberfour";
// Parameter/Value pairs follow a "?" and then are delinated by "&"
// they have a "=" between them.

// for this example - we assume that the parameter name exists in the form.
var PS = new Array();
var _i = _l.indexOf("?");
if (_i != -1) {
  // then there is a "?" in the url
  _l = _l.substr(_i+1,_l.length);  // lets only deal with the parameters
//alert("before loop logic: _l="+_l+"<BR>");
  while (_l.indexOf("&") != -1) {
    // pull each param/value pair out until there is only 1 left.
	var _a = _l.substr(0,_l.indexOf("&"));
//alert("#1:"+_a+"<BR>");
	PS[PS.length?PS.length:0] = _a;
	var _m = _l.substr(1,_l.length).indexOf("&");
	    _m++; // move 1 past the &
//alert("_m="+_m+"<BR>");
	if (_m == 0) _m = _l.length; // if there are no more & - get the rest of the url string.
	_l = _l.substr(_m+1,_l.length);
//alert("_l = "+_l+"<BR>");
  }
  // pull off the last param/value pair.
//alert("#2:"+_l+"<BR>");
  PS[PS.length?PS.length:0] = _l;
}
//alert("DONE<BR>");
var _j=0;
for (var _i=0;_i<PS.length;_i++) {
  var _a = PS[_i].indexOf("=");
  var _b = PS[_i].substr(0,_a);
  var _c = PS[_i].substr(_a+1,PS[_i].length);
// swap '+' in values for ' '
//set localised fMod back to default list
if (_b=='fMod') {
//_c = chkmod(_c);
//alert(_c);
}
//alert(_c);
//////if (_b!='fSrt') {
_c = Switch(_c,"+"," ");
_c = Switch(_c,"%2B","+");
_c = Switch(_c,"&"," ");
//_c = Switch(_c,"."," ");
_c = Switch(_c,"%20"," ");
_c = Switch(_c,"'"," ");
//////}
//alert("P/V #"+_i+": "+PS[_i]+"==> "+_b+"="+_c+"<BR>");
//  if (document.inp)
//  if (eval("document.inp."+_b))
//  eval("document.inp."+_b+".value='"+_c+"'");
  aParNam[_j]=_b;
  aParSet[_j]=_c;
  _j=_j+1;
}
//alert(document.inp.fMod.value);
//alert(aParNam + ':' + aParSet);
}

//====================================================================
//replace chars in text string
function Switch(item,OldChar,NewChar)
{
var _ONE=0;
  var _ret="";
  var _flag=0;
  var _item=item.split("");
  for(var i=0;i<_item.length;i++)
  {
    if(!_flag&&_item[i]==OldChar)
	{
	  _item[i]=NewChar;
      _flag=_ONE;
	}
	_ret+=_item[i];
  }
  return(_ret);
}

//====================================================================
function getRand(pMax)
{
    if (pMax<0) pMax=0;
    var pRand = Math.round(Math.random()*pMax);
    return pRand;
}
//====================================================================
