/*
Author: Dr Ron Knott, email RandomiserCalc@ronknott.com
copyright Dr R Knott.
Contact details:  http://www.mcs.surrey.ac.uk/Personal/R.Knott/contactron.html
For Maths Year 2000,  www.mathsyear2000.org
*/

function error(m){var s="";for(var i=0;i<arguments.length;i++)s=s+arguments[i];
  alert(s);return false
};

function map(F,L){ //apply F to each item of L  
  //alert(L)
  var newL=new Array(L.length);
  for(var i=0;i<L.length;i++)
      newL[i]=F(L[i]);
  return newL
};

function endswith(a,b){  //alert("endswith "+a+"  "+b);
    if(typeof(a)!="string")a=a.toString();
    if(typeof(b)!="string")b=b.toString();
    var la=a.length; var lb=b.length;
    if(la<lb)return false
    else return(a.substring(la-lb)==b)
}

function randint(lo,hi){
  if(lo>hi)alert("randint error: lo exceeds hi! "+lo+","+hi)
  else {do
          {var r=Math.floor(Math.random()*(hi-lo+1)+lo)}
        while(r>hi);  //random can return 1.00!!
        //alert("randint("+lo+","+hi+")="+r);
        return r
}      }

function randelt(l){
   if(arguments.length>1)l=arguments;
   return l[randint(0,l.length-1)] 
};

var timerID=null;

function dicespots(dnb,spots,doct){
  if(doct)
    {if(isNaN(eval(document.inp.diceTotal.value)))document.inp.diceTotal.value=spots
     else document.inp.diceTotal.value=eval(document.inp.diceTotal.value)+spots;
    };
  setdicespots(dnb,["","22","11-33","11-22-33","11-13-31-33",
    "11-13-31-33-22",
    "11-21-31-13-23-33",
    "11-21-31-13-23-33-22",
    "11-21-31-13-23-33-12-32",
    "11-21-31-13-23-33-12-22-32"][spots])
};

function setdicespots(dnb,imgList){
  for(var i=1;i<=3;i++)
    for(var j=1;j<=3;j++)
     { //alert("d"+dnb+"s"+i.toString()+j.toString());
      document["d"+dnb+"s"+i.toString()+j.toString()].src
        =( imgList.indexOf(i.toString()+j.toString())==-1 ? "/images/explorer/randomiser/empty.gif" : "/images/explorer/randomiser/dot.gif")
     };
};

function throwD(ct){
  for(var i=1;i<=7;i++)
       if(eval("document.inp.throw"+i).checked)  dicespots(i,randint(1,6),ct==6);  
  if(ct==6)return;
  timerID=setTimeout("throwD("+eval(ct+1)+")",250); 
};

function throwdice(){  
    document.inp.diceTotal.value="";
    var tnb=0;
    for(var i=1;i<=7;i++)if(eval("document.inp.throw"+i).checked)tnb++; 
    if(tnb==0){alert("Please select at least one dice to throw (click in a box above a dice).");return}
    document.inp.diceTotal.value="";
    clearTimeout(timerID);
    throwD(1)
};

function throwcoins(){
  var any=false;
  for(var i=1;i<=10;i++)any=any || document.inp["throwc"+i].checked;
  if(! any) {error("You haven't selected any coins.  Check some boxes to show which coins to throw.");return};
   clearTimeout(timerID);
   throwC(1)
};
  
function throwC(ct){
  if(ct<8)
    {for(var i=1;i<=10;i++)coinflip(i);
     timerID=setTimeout("throwC("+eval(ct+1)+")",150)
    }
  else randomiseCoins();
};

function randomiseCoins(){
  for(var i=1;i<=10;i++)
       if( eval("document.inp.throwc"+i).checked )   coin(  i,  (randint(1,2)==1?"H":"T")  )
};

function coinflip(i){
   if(eval("document.inp.throwc"+i).checked)
       document["coin"+i].src= (endswith(document["coin"+i].src,"1.gif")?"flip2.gif":"flip1.gif");
};

function coin(cnb,which){
   //for(var i=1;i<100;i++)document["coin"+cnb].src=(randint(0,1)==1?"head":"tail")+".gif"
   document["coin"+cnb].src="/images/explorer/randomiser/"+(which=="H"?"head":"tail")+".gif"
};

function dchk(n){
  document.inp.diceTotal.value="";
  if(!eval("document.inp.throw"+n).checked)setdicespots(n,"")
};

function cchk(nb){
  document["coin"+nb].src="/images/explorer/randomiser/"+(eval("document.inp.throwc"+nb).checked?"head.gif":"empty.gif")
};


function pickInt(){
    if(document.inp.lo.value.indexOf(".")!=-1){alert("Please give a whole number as the Minimum");return};
  if(document.inp.hi.value.indexOf(".")!=-1){alert("Please give a whole number as the Maximum");return};

  var l=parseInt(document.inp.lo.value), 
      h=parseInt(document.inp.hi.value);
  if(isNaN(l)){alert("Please give me a value for the minimum whole number to choose from");return};
  if(isNaN(h)){alert("Please give me a value for the maximum whole number to choose from");return};
  if(h>99999999999999999){alert("The maximum is too large to return whole random numbers.");return};
  if(h<l){alert("The maximum ("+h+") is less than the minimum ("+l+")! Try again.");return};
  if(h==l){alert("The maximum and minimum numbers are the same! Try again.");return;};
  
  pickI(0);
};

function pickI(i){
  var rotator= //"???????????? ".substring(0,12-i)+" ";
        //["\\\\\\\\","-----","////","-----"][i%4]+" "; 
        //["|@Oo.oO@|","|Oo.oO@@|","|o.oO@@O|","|.oO@@Oo|","|oO@@Oo.|","|O@@Oo.o|","|@@Oo.oO|"][i%7];
        //["","( - - )","( o o )","( O O )","( @ @ )","( @ @ )","( @ - )","( @ @ )","( @ @ )","( O O )","( o o )","( - - )",""][i%12]
        //["++++","xxxx","****"][i%3]+" "; //["\\\\\\\\","|||||","////","|||||"][i%4]+" ";  
        //[" ^ ","  >"," v ","<  "][i%4]
        //["ob","op","qo","do"][i%4]
          ["boo","doo","obo","odo","oob","ood","ooq","oop","oqo","opo","qoo","poo"][i%12]
        //["qb","dp"][i%2];
        //["qb","db","dp","qp"][i%4];
        //["qb","qd","pd","pq","bq","bp","dp","db"][i%8];
        //["b","o","d","o","q","o","p","o"][i%8];
        //["b","o","p","o"][i%4];
        //["^^^^^^^^^","*********","+++++++++","---------","_________","_ _ _ _ _","_   _   _","_   _","_","","",""][i%12]
        //["ddddddd","ddddddq","dddddqq","ddddqqq","dddqqqq","ddqqqqq","dqqqqqq","qqqqqqq","qqqqq","qqq","q",""][i%12]
        //["odbo","obdo","opqo","oqpo"][i%4]
        //["od","ob","do","bo","po","qo","op","oq"][i%8]
        //["/"," \\","  /","   \\"][i%4]
  if(i<12){document.inp.randI.value = rotator //+randint(parseInt(document.inp.lo.value),parseInt(document.inp.hi.value));
           timerID=setTimeout("pickI("+eval(i+1)+")",200)}
  else document.inp.randI.value = randint(parseInt(document.inp.lo.value),parseInt(document.inp.hi.value));
  ;
};

function permuteBetween(from,to){
  var p=new Array(to-from+1); var q=new Array(to-from+1);
  for(var i=0;i<q.length; i++)  q[i]=from+i;
  for(var i=0;i<p.length; i++,q.length--)
     {var j=randint(0,q.length-1);   p[i]=q[j];  q[j]=q[q.length-1]
     }
  return p
};

function pickcards(){
  var n=parseInt(document.inp.nbcards.value);
  
  if(isNaN(n) || document.inp.nbcards.value.indexOf(".")!=-1 || n<=0)
            {alert("Please give a WHOLE number of cards to pick!");document.inp.nbcards.value="";return};
  if(n>52){alert("Too many! There are only 52 cards in the pack.");document.inp.nbcards.value="";return};

  for(var i=0;i<52;i++)
    {var h=Math.floor(i/13)+1,v=i%13+1;
     document["h"+h+"s"+v].src="/images/explorer/randomiser/empty.gif";
     document["h"+h+"p"+v].src="/images/explorer/randomiser/empty.gif";
    };
  dealC(1);
};

function dealC(ct){
    //document.h1s1.src=["clubs","hearts","diamonds","spades"][ct%4]+".gif";
    //document.h1p1.src=randint(1,13)+".gif";
    showCards();
    if(ct<3)timeID=setTimeout("dealC("+eval(ct+1)+")",100)
    else showCards()
};

function showCards(){
  var    n=parseInt(document.inp.nbcards.value),
      deck=permuteBetween(0,51);
  for(var i=0;i<=51;i++)
       {var c=deck[i],h=Math.floor(i/13)+1,v=i%13+1;
        document["h"+h+"s"+v].src="/images/explorer/randomiser/"+(i<n?(c%4==0?"hearts.gif":c%4==1?"clubs.gif":c%4==2?"diamonds.gif":"spades.gif"):"empty.gif");
        document["h"+h+"p"+v].src="/images/explorer/randomiser/"+(i<n?(c%13+1)+".gif":"empty.gif")
       };
};

var LIST=[];

function getLIST(){
  var l=document.inp.list.value;
  while(l.indexOf(", ")!=-1)l=l.replace(/\, /g,",");
  LIST= eval('["'+  l.replace(/\"/g,"'").replace(/,/g,'","')  +'"]'); 
};

function pickItem(){
  document.inp.list1.value="";
  var l=document.inp.list.value;
  if(l==""){
      var egs=["bag, bar, bat, ball","John Brown, June White, Joan Green, Jim Black","p,q,r,s,t","12,25,8,20,14",
               "pear, pear, apple, orange, orange, orange","Homer, Marge, Bart, Lisa, Maggie",
               "pizza, pasta, potato, rice", "red, green, blue, yellow","46, 2, 999, 1, 432",
               "h,i,p,p,o,p,o,t,a,m,u,s","Jim, Jean, Jane, John, June, Joan"]; 
      document.inp.list.value=egs[randint(0,egs.length-1)];
     alert("Please type in some items to choose from like the example I've filled in for you."); 
     return};
   getLIST();
   pickIt(1)
};


function shuffleItem(){
  getLIST();
  document.inp.list1.value=map(function(i){return LIST[i]},permuteBetween(0,LIST.length-1))
};

function pickIt(ct){  
    var rotator=["/ "," |","  \\"," |"][ct%4];
    if(ct<8){document.inp.list1.value=rotator //+"  "+LIST[randint(0,LIST.length-1)] ;
             timerID=setTimeout( "pickIt("+eval(ct+1)+")",100) }
    else document.inp.list1.value=LIST[randint(0,LIST.length-1)];
};
