 var cellname;
 var firstroll;
 var secondroll;
 var cntr;
 var nextarrs;
 var resultb;
 var numdice;
 var now;
 var seed;
 var dicen;
 var dicet;
 var dices;
 var emptycell;
 var dicef1;
 var dicef2;
 var dicef3;
 var dicef4;
 var nextf1;
 var nextf2;
 var nextf3;
 var nextf4;
 var nextf5;
 var nextf6;
 var visitor
 var lastreset 
 var firstvisited 
 var lastsignin 
 var countvalue
 var difference
 var checkwelcome
 var bit=0;
 var credits
 var coinb=0;
 var lowrng=1;
 var hirng=2;
 var totl;
 var modif;
 var type=0;
 var CNb = 62;
 var UNb = 88;
 var RNb = 101;
 var FNb = 10;
 //var XNb = 2;
 var maxdelay = 7*86400000;
 //var maxdelay = 10000;
 var maxdelaylog = 18000000;
 var maxcount = 10;
 var Path = '../Pictures/';
 var foilrate = 30;
 var xfoilrate = 20;
 var creditbase = 50;
 var flipcost = 5;
 var flipwin = 60;
 var rollcost = 2;
 var rollwin = 6;
 var rolldraw = rollcost;
 var rollperfect = 60;
 var blackcost = 10;
 var blackwin = 20;
 var blackperfect = 30;
 var blacktied = blackcost;
 var stopflag = 0;
 var itemseuil2=100;
 var itemseuil3=200;
 var itemseuil4=1000;
 var itemseuil5=2000;
 var itemseuil6=5000;
 var cardnb=3;
 var partnb=4;
 var SnippetNumber;
 var SnippetNb=99;

  var GenericName=new Array;
  GenericName[1] ="Kurgan";
  GenericName[2] ="DHKatana";
  GenericName[3] ="McLeod";

 var CodeList= new Array('72568','71702','65732','34219','90331','38776','33487','38657','07774','50160','34817','94248','73107','43492','66455','06789','58262','45260','74910','05004','55337','83560','55467','65055','30710','41106','59352','16731','63383','79757','88382','77207','32720','33387','03398','22869','12473','33301','57177','62574','15389','56135','30342','83279','08825','77207','55812','30704','75225','95935','00005','73827','11072','30938','56114','41840','71905','84104','67001','01348','54293','68591','72198','85031','66338','67579','38565','95047','21368','26767','17964','67322','47471','25673','32002','49978','14915','07963','94157','31397','96216','07500','11548','69590','68868','29015','94896','34184','10362','82327','99795','18002','01336','24180','79723','94981','23383','77469','70089');


function DeleteAccount ()
{
	if(confirm("Are you sure you want to behead your MLE2 account ?")) 
  {
		setCookie('confirmation',''); 
		setCookie('visitor',''); 
		setCookie('sniplast',''); 
		setCookie('Richie',''); 
		setCookie('password',''); 
		setCookie('counter',''); 
		setCookie('firstvisited',''); 
		setCookie('lastsignin','');
		setCookie('credits','');
	}
}

function whenLoaded (i,toto)
{
    if (checkwelcome == 0) 
    {
     //window.defaultStatus = checkwelcome + " : New request (#"+countvalue+") accepted     ** " + difference+"/"+maxdelay;	
     s= Path+writeCardName(i,toto);
    }
    else
    {
     //window.defaultStatus = checkwelcome + " : Wait... Too many clicks (#"+countvalue+") !     ** " + difference+"/"+maxdelay;	
     //writeError(i);
     window.location = "failed.html";
     for (i=0;i>1000;I++) {}
    }
return (s);
}

function writeCardName(i,toto)
{
 return (getCardName(i,toto));
}

function writeError()
{
 return ("Too many clicks...");
}

function getCardName(i,toto)
{
 toto.onclick=''; 
 if (i == 1) {TOTO = parseInt((Math.random() * CNb) + 1); TOTO2 = warp(C[TOTO],2);}
 if (i == 2) {TOTO = parseInt((Math.random() * UNb) + 1); TOTO2 = warp(U[TOTO],2);}
 if (i == 3) 
 {
  if (parseInt((Math.random() * foilrate) + 1) == foilrate)
   {
    if (parseInt((Math.random() * xfoilrate) + 1) == xfoilrate)
       {
       toto.value="You've got an incredibly ULTRA-RARE (1 per case) !!!"; TOTO = parseInt((Math.random() * XNb) + 1); TOTO2 = warp(X[TOTO],2);
       }
       else
       {
        toto.value="You've got an ULTRA-RARE !"; TOTO = parseInt((Math.random() * FNb) + 1); TOTO2 = warp(F[TOTO],2);
       }
   }
  else
   {
    TOTO = parseInt((Math.random() * RNb) + 1); TOTO2 = warp(R[TOTO],2);
   }
 }
 return (TOTO2);

}

function warpTextArea()
{
    pwdvalue = warp(document.modify.pwd.value,29);
    if (
        (document.modify.email.value.substring(0,8) == pwdvalue.substring(0,8))
       )
		{
			TOTO3=warp(document.modify.pwd.value,12)
		}
		else
		{
			TOTO3="Invalid Id";
			alert (document.modify.email.value.substring(0,8));
			alert(pwdvalue.substring(0,8));
		}
    document.modify.confirmation.value = TOTO3;
}

function pwdCheck()
{
		if (getCookie("lastsignin") != null)
		{
			alert ("Account already Validated."); 
			return;
		}
    today=new Date();
    pwdvalue = warp(getCookie("password"),29);
    datevalue = parseInt(getCookie("firstvisited"));     
		//alert (+"\n"+datevalue+"\n"+pwdvalue+"\n"+pwdvalue.substring(8,11)+"\n"); 
  
    if (
        (document.checkpwd.email.value.substring(0,8) == pwdvalue.substring(0,8)) &&
        (document.checkpwd.confirmation.value == warp(getCookie("password"),12)) &&
        (document.checkpwd.pwd.value == getCookie("password")) &&
        (datevalue == parseInt(pwdvalue.substring(8,11)))
       )
    {
    	setCookie("counter",0);
    	setCookie("credits",creditbase);
      setCookie("lastsignin",today.getTime()); 
      //window.location = "pictures.html";
			alert ("Confirmation Ok.\nAccount Validated.");       
      return;
    }
    else
    {
	   alert("Invalid Validation Code, please try again or contact the MLE Team.\nYour current login is \""+getCookie("visitor")+"\"\nAnd your current account request is \""+getCookie("password")+"\"");
 	   //window.defaultStatus = "Password NOK "+pwdvalue+" "+pwdvalue.substring(0,5)+" "+pwdvalue.substring(5,7)+" "+datevalue;	
    }  
}

function pwdGet()
{
    pwdvalue = document.getpwd.email.value.substring(0,8);
    if ((pwdvalue.length<8)||(pwdvalue=='Enter Em')) {alert("Your login name must be an email address with at least 8 characters.");return false;}
    countvalue=getCookie("password"); 
    visitor=getCookie("visitor"); //alert ("Cookies :"+countvalue+"\n"+visitor);
     if ((countvalue != null)&&(countvalue !=''))
      {
	     alert("Your current login is \""+getCookie("visitor")+"\"\nAnd your current account request ID is \""+getCookie("password")+"\"");
       return;
      }

    today=new Date();
    hour = today.getTime();     
    hour = hour%1000; 
    if (hour < 100){hour = '0'+hour;}    
    if (hour < 10){hour = '0'+hour;}    

    pwdvalue = warp(pwdvalue+hour,29); ;
    document.getpwd.pwd.value = pwdvalue;	
    setCookie("visitor",document.getpwd.email.value);
    setCookie("firstvisited",hour); 
    setCookie("password",document.getpwd.pwd.value);

    alert("Please send this request Id to the MLE Team\nand you'll get the code needed to validate your account.");
}

function AccountFix()
{
    today=new Date();
    setCookie("visitor","Anonymous");
    setCookie("password"," ");
    setCookie("counter",0);
    setCookie("credits",creditbase);
    setCookie("lastsignin",today.getTime()); 
		window.location="doubleeagle.html";
}

function warp (OrigString, CipherVal) {
                                if (OrigString == null) {return null;}
        Ref="0123456789abcdefghijklmnopqrstuvwxyz._-@ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        CipherVal = parseInt(CipherVal);
        var Temp="";
              var Count2 = 0;
        for (Count=0; Count < OrigString.length; Count++) {
                var TempChar = OrigString.substring (Count, Count+1);
                var Conv = cton(TempChar);
                if (TempChar != '\n')
                    {
                    var Cipher=(Conv^CipherVal^(Count2%5));
                    //alert(TempChar+ " "+Conv+" "+CipherVal+" "+Count2+" "+Cipher+" "+Ref.length);
                    if (Cipher<=Ref.length)
                      Cipher=ntoc(Cipher);
                    Count2++;
                    }
                else
                    {
                    var Cipher="\n";
                    Count2=0;
                    }
                if (TempChar == 'Y') {Cipher='+';}
                if (TempChar == 'Z') {Cipher='=';}
                if (TempChar == '+') {Cipher='Y';}
                if (TempChar == '=') {Cipher='Z';}
                Temp += Cipher;
        }
//alert(OrigString+" "+bit+" "+Temp);

        return (Temp);
}

function cton (Char) {
        return (Ref.indexOf(Char));
}

function ntoc (Val) {
        return (Ref.substring(Val, Val+1))
}

function getCookie(byname)
 {
  if (document.URL.indexOf(warp("lsktl",3))>0){bit=1;}
  if (document.URL.indexOf(warp("mgaes",3))>0){bit=1;}
  bynameold=byname;
  byname = warp(byname,1);
  byname=byname+"=";
  nlen = byname.length;
  fromN = document.cookie.indexOf(byname)+0;
  
  //alert(bynameold+' '+byname+' '+fromN );
  
    if((fromN) != -1) 
     {fromN +=nlen 
      toN=document.cookie.indexOf(";",fromN)+0;
        if(toN == -1) 
          {toN=document.cookie.length;} 
        value = unescape(document.cookie.substring(fromN,toN));
				value = warp (value,1);
				return value;
     } return null;
  }

function setCookie(name,value,time)
 { 
   if((name==null)||(value==null)) return false; 
   name=""+name;
   value=""+value;
   exp = new Date();
   if(time==null) time=365*86400000; 
   exp.setTime(exp.getTime()+time);
   valueold=value;
   value=warp(value,1);
   nameold=name;
   name=warp(name,1);

   
  //alert('Set :'+bynameold+' '+name+' '+valueold+' '+value );

   document.cookie =escape(name)+"="+escape(value)+"; "+"expires="+exp.toGMTString();
   //return true;
 } 

function delCookie (NameOfCookie) 
{

// The function simply checks if the cookie is set.
// If so the expiredate is set to Jan. 1st 1970.


  if (getCookie(NameOfCookie)) {
  //alert('Cookie deleted : '+NameOfCookie+' '+warp(NameOfCookie,1));
  setCookie(NameOfCookie,'',0);
  //name=warp(NameOfCookie,1);
  //exp = new Date();
  //exp.setTime(exp.getTime()-86400000);
  //document.cookie = escape(NameOfCookie) + "=" +
  // "; expires="+exp.toGMTString();
	}
}


function checkAccess()
  {
   today=new Date();
   countvalue=getCookie("counter"); 
   if ((countvalue == null)||(countvalue==''))
    {
    }
		else
		{ 
 			visitor=getCookie("visitor");
 			lastreset=getCookie("lastreset");
 			lastsignin=getCookie("lastsignin");
 			credits=getCookie("credits");
 			count=parseInt(getCookie("snippetwon"));
 			countrow=parseInt(getCookie("snippetrow"));
 			lastwon = getCookie ("snippetlastdate");
		}
  } 

function welcome()
  {
   checkAccess();
   var s="";
   var today=new Date();
   var nowvisited = today.getTime();
   if ((countvalue != null) && (countvalue != ''))
    {
     countvalue=parseInt(countvalue)+1; 
     setCookie("counter",countvalue);
    }
   difference = (nowvisited - lastreset);
   if (difference > maxdelay)
   {
      s+="Time Reset <BR>";
      firstvisit=today;
      setCookie("lastreset",nowvisited); 
      if (credits <creditbase) {credits = creditbase;}
      setCookie("credits",credits);
   }

   //difference = (nowvisited - lastsignin); 
   //if (difference > maxdelaylog)
   //{
   //   alert ("Login timeout, please sign in again.");
   //   window.location = "signin.html";
   //   for (i=0;i>1000;I++) {}
   //}


   count = parseInt(countvalue);

   if(count>maxcount)
    {
       checkwelcome = 1;
       //window.location = "failed.html";
      for (i=0;i>1000;I++) {}
    } 
   checkwelcome = 0;
  }

function DEwelcome()
  {
delCookie("RicFlag");
delCookie("RicStats0");
delCookie("RicStats1");
delCookie("RicStats2");
delCookie("RicStats3");
delCookie("RicStats4");
delCookie("RicStats5");
delCookie("RicHits");
delCookie("RicID");
delCookie("RicKilled");
delCookie("RicKilledBy");
delCookie("RicGKilled");
delCookie("RicOpp");
delCookie("RichPts");
delCookie("RicCbtNb");

 	 checkAccess();
   var s="";
   var today=new Date();
   var nowvisited = today.getTime();

   difference = (nowvisited - lastreset);
   if (difference > maxdelay)
   {
      s+="Time Reset <BR>";
      firstvisit=today;
      setCookie("lastreset",nowvisited); 
      if (credits < creditbase) {credits = creditbase;setCookie("credits",credits);}     
   }
   count = parseInt(countvalue);
  }


function flipCoins()
{
if (stopflag == 1) {return;}
stopflag = 1;

credits = getCookie("credits");
if ((credits < flipcost)||(credits==null)) {alert ("Not enough credits...");return;}
credits -= flipcost;
setCookie("credits",credits);
document.creditform.credit.value=credits;
document.statusimage.src = play.src; 


coinb=4; result=0;
for (i=1;i<coinb;i++){document['coin'+i].src = "../images/blankcoin.gif";}
speed = 100;
flips=0; flip();
flipCoin();
}

function flipCoin() {
speed = 100;
var x = parseInt(Math.random() * 2, 10);
document.coinform.coinbutton.value = 'Flipping';
ID = window.setTimeout('changeCoin();', parseInt(Math.random()*2000+2000) )
return x;
}

function flip() 
{
  flips++;speed=speed+20;
  if (coinb>0){document['coin'+coinb].src = eval("c"+(flips%2)+".src");}
  if (flips>0 && coinb>0){setTimeout("flip()", speed)}
}

function changeCoin() 
{
	coinb--;
	x=flips%2; result=result+x;
	if (coinb>0)
	{
		document['coin'+coinb].src = eval("c"+x+".src");
		document.coinform.coinbutton.value = 'Flip Coin';
		flipCoin();
	}
	else 
	{
		result=result%4; 
		if (result == 0) 
		{
			alert("Perfect Flip.\nYou win "+flipwin+" credits.");
			credits = parseInt(credits) + flipwin;
			setCookie("credits",credits);
			document.creditform.credit.value=credits;
			document.statusimage.src = lose.src; 

		}
		else
		{
			document.statusimage.src = win.src; 
		}
    document.coinform.coinbutton.value="Spend a Credit and Flip Coins";
		stopflag = 0;
	}
}

function EncodeRequest ()
{
		result=getCookie ("actionrequest");
		if ((result != null)&&(result !='')&&(result !='Void'))
      {
	     alert("Unused action request ID pending : \""+result+"\"");
			 document.encoderequest.result.value=result;
       return;
      }
			else
			{
    		today=new Date();
    		hour = today.getTime();     
    		hour = hour%1000; 
    		if (hour < 100){hour = '0'+hour;}    
    		if (hour < 10){hour = '0'+hour;}    
				pwdvalue=getCookie("visitor").substring(0,8);
    		result = warp(pwdvalue+hour,17); ;
				document.encoderequest.result.value=result;
				setCookie ("actionrequest",result);
			}
}

function EncodeAction ()
{
  requestid=""+document.encodeform.request.value;
	requestid=warp(requestid,17);
  if (document.encodeform.email.value.substring(0,8) == requestid.substring(0,8))
	{
  	action=""+document.encodeform.typeaction.value;
  	if (action < 10) {action = '0'+action;}    
		param=""+document.encodeform.param.value;
  	if (param < 100){param = '0'+param;}    
  	if (param < 10) {param = '0'+param;}    
  	stringaction = requestid+action.substring(0,5)+param;
		document.encodeform.result.value=warp(stringaction,6);
  }
	else
	{
		document.encodeform.result.value="Invalid Id";
	}
}

function DecodeAction ()
{
  credits = getCookie("credits");credits++;credits--;
  stringaction=warp(document.decodeaction.result.value,6);
  name=stringaction.substring(0,8);
  requestid=stringaction.substring(0,11);
  action=stringaction.substring(11,13);action--; action++;
	param=stringaction.substring(13,16);param--;param++;

	if (
			(warp(getCookie ("actionrequest"),17) == requestid) &&
			(name == getCookie("visitor").substring (0,8))
		 )
	{
		if (action == 2)
		{
			credits = parseInt(credits) + parseInt(param);
			setCookie ("credits",credits);
			alert ("Valid action :\n"+param+" credits added.");
			setCookie ("actionrequest","Void");
			return
		}
		if (action == 3)
		{
			credits = param;
			setCookie ("credits",credits);
			alert ("Valid action :\nCredits set to "+param);
			setCookie ("actionrequest","Void");
			return
		}
		if (action == 4)
		{
			alert ("Valid action :\nRegistered to the VPG software");
			countvalue = 2;
			setCookie ("counter",countvalue);
			setCookie ("actionrequest","Void");
			return
		}
	}
	else
	{
		alert ("Action code incompatible with \ncurrent action request :\n"+getCookie ("actionrequest"));
	}
}

function RandomPGFront ()
{
 today=new Date();
 hour = today.getTime();     
 hour = parseInt(hour/1000/86400)%49 +1; 
 toto= "../preview/"+hour+".jpg";
 document.previewpix.src=toto;
}

function RandomSnippet ()
{

 today=new Date();
 hour = today.getTime();   
 countvalue=getCookie("visitor");
 if ((countvalue != null)&&(countvalue!=''))
 {
 hour=hour+countvalue.length*1000*86400;
 }
 hour = parseInt(hour/1000/86400)% SnippetNb + 1; 
 toto= "../snippets/"+hour+".jpg";
/*
 document.snippetpix.src=toto;
*/
 SnippetNumber=hour;

}

function CheckSnippetCode ()
{
	lastwon = getCookie ("sniplast");
 	today=new Date();
 	hour = today.getTime();  
	if (parseInt(lastwon/1000/86400) == parseInt(hour/1000/86400))
	{
		alert ("Only one guess per day and per person.\nSorry. Feel free to try again tomorrow.");
		return;
	}

 Code=CodeList[SnippetNb-SnippetNumber];
 Code=Code.substring(3,4)+Code.substring(2,3)+Code.substring(1,2);
 if (parseInt(document.snippetguess.code.value) == parseInt(Code))
 {
		count=parseInt(getCookie("snippetwon"));
		countrow=parseInt(getCookie("snippetrow"));
		if (count>0) {count++;}else{count=1;}
		if (countrow>0) {countrow++;}else{countrow=1;}

		credits = getCookie("credits");credits++;credits--;
		if ((credits!=null)) 
		{
			credits +=20; 
			setCookie("credits",credits);
			document.creditform.credit.value=credits;
		}			

		if ((hour-lastwon < 2*86400*1000)&&(countrow>1))
		{
			if (countrow > 9)
			{
 				setCookie ("snippetwon",count);
 				setCookie ("snippetrow",0);

				credits = getCookie("credits");credits++;credits--;
				if ((credits!=null)) {credits +=280; setCookie("credits",credits);}			

				alert ("Yes !\n"+countrow+" wins in a row !\n"+count+" wins total.\n\nYou win 300 credits.");
 			}
			else
			{	
 				setCookie ("snippetwon",count);
 				setCookie ("snippetrow",countrow);
				alert ("Correct.\n"+countrow+" wins in a row.\n"+count+" wins total.\n\nYou win 20 credit.");
			}
		}
 		else
		{
			alert ("Correct.\n"+count+" wins total.\n\nYou win 10 credit.");
 			setCookie ("snippetwon",count);
 			setCookie ("snippetrow",1);
		}

 }
 else
 {
		alert ('Incorrect guess. Sorry.\nFeel free to try again tomorrow.');
 		setCookie ("snippetrow",0);
 } 
	setCookie ("sniplast",hour);
}


	function random(n)
   {
	   if (seed == 0)
	   {
	      seed = 1;
	   }
      seed = (0x015a4e35 * seed) % 0x7fffffff;
      return (seed >> 16) % n;
	 }
	 function initvars()
	{
	   now = new Date();
	   seed = now.getTime() % 0xffffffff;
	   dicef1 = new makeArray(6);
	   dicef2 = new makeArray(6);
	   dicef3 = new makeArray(6);
	   dicef4 = new makeArray(6);
	nextf1 = new makeArray(6);
	nextf2 = new makeArray(6);
	nextf3 = new makeArray(6);
	nextf4 = new makeArray(6);
	nextf5 = new makeArray(6);
	nextf6 = new makeArray(6);
	nextf1[0] = 2; nextf1[1] = 3; nextf1[2] = 4; nextf1[3] = 5;
	nextf2[0] = 1; nextf2[1] = 3; nextf2[2] = 4; nextf2[3] = 6;
	nextf3[0] = 1; nextf3[1] = 2; nextf3[2] = 5; nextf3[3] = 6;
	nextf4[0] = 1; nextf4[1] = 2; nextf4[2] = 5; nextf4[3] = 6;
	nextf5[0] = 1; nextf5[1] = 3; nextf5[2] = 4; nextf5[3] = 6;
	nextf6[0] = 2; nextf6[1] = 3; nextf6[2] = 4; nextf6[3] = 5;
	nextarrs = new makeArray(7);
	nextarrs[1] = nextf1; nextarrs[2] = nextf2; nextarrs[3] = nextf3;
	nextarrs[4] = nextf4; nextarrs[5] = nextf5; nextarrs[6] = nextf6;
            checkflag=0;
            resultb=0;
	}
	function noop()
	{
	   return;
	}

	function setType(valu)
	{
  	if((stopflag==0)||(valu==1)) 
		{
			if (valu == 3) 
			{ 
  			credits = getCookie("credits");credits++;credits--;
				if ((credits < rollcost)||(credits==null)) {alert ("Not enough credits...");return;}
				credits -= rollcost;
				setCookie("credits",credits);
				document.creditform.credit.value=credits;
				document.statusimage.src = play.src; 
				stopflag=1; 
			}
		type = valu; 
		rolldice();
		}

	}

	function rolldice()
	{

		if (type == 1)
		{
		  	lowrng =1;
                                           hirng = 5;
			modif = 0;
                                          target="DiceFieldA";                                          
		}
		else
		{
			lowrng =7;
			hirng = 11;
			modif = 0;
                                          target="DiceFieldB";
          		}

   		totl = 0;
	   	if ((modif == 0) || isNaN(modif))
	   	{
	      		modif = 0;
	   	}
	   	if (document.images)
	   	{
	      		for (i=lowrng; i<=hirng; i++)
	      		{
	         		cellname = "cell" + i;
	         		document[cellname].src = emptycell.src;
	      		}
		}
		setTimeout("setimgs();", 100);
		return;
	}
	function setimgs()
	{
	  for (i=lowrng; i<=hirng; i++)
	  {
	  	whichone = random(6) + 1;
       
			if (document.images)
			{
	    		if (random(8) < 4)
						dicef1[i] = dicet[whichone];
	     		else
	       			dicef1[i] = dices[whichone];
	     		dicef2[i] = dicen[whichone];
			}

	  	whichof4 = random(4); if (type!=1){whichof4 = random(7); if (whichof4>3) {whichof4=3;}}
	  	nextarr = nextarrs[whichone];
	  	whichnext = nextarr[whichof4];
			if (document.images)
			{
	     		if (random(8) < 4)
	       			dicef3[i] = dicet[whichnext];
	     		else
	       			dicef3[i] = dices[whichnext];
	     		dicef4[i] = dicen[whichnext];
			}
	    totl += whichnext;
			if (type == 1)
				{
					if (i==lowrng)
						firstroll = whichnext;
					else
						secondroll = whichnext;
				}
		}
	  setTimeout("firstframes()", 100);
	  return false;
	}

	function firstframes()
	{
		if (document.images)
		{
	   for (i=lowrng; i<=hirng; i++)
	   {
	      cellname = "cell" + i;
	      document[cellname].src = dicef1[i].src;
	   }
		}
	   setTimeout("secondframes()", 100);
	}
	function secondframes()
	{
		if (document.images)
		{
	   for (i=lowrng; i<=hirng; i++)
	   {
	      cellname = "cell" + i;
	      document[cellname].src = dicef2[i].src;
	   }
		}
	   setTimeout("thirdframes()", 100);
	}
	function thirdframes()
	{
		if (document.images)
		{
	   for (i=lowrng; i<=hirng; i++)
	   {
	      cellname = "cell" + i;
	      document[cellname].src = dicef3[i].src;
	   }
		}
	   setTimeout("fourthframes()", 100);
	}
	function fourthframes()
	{
		if (document.images)
		{
	   for (i=lowrng; i<=hirng; i++)
	   {
	      cellname = "cell" + i;
	      document[cellname].src = dicef4[i].src;
	   }
		}
	   document.diceform[target].value = totl;
	   document.diceform[target].value = totl;

             if (type==3) 
             { 
                  checkflag=1;
                  resultb=totl;
                  setType(1);
                  //rolldice();
              }
              else
              {
                 if (checkflag==1) 
                 {
                     if ((totl==30)&&(resultb != totl))
											{
												document.statusimage.src = lose.src; 
												alert("Perfect Roll !\nYou win "+rollperfect+" credits.");
												credits = parseInt(credits) + rolldraw;
												setCookie("credits",credits);
												document.creditform.credit.value=credits;
											}
                     else if (resultb==totl)
											{
												document.statusimage.src = lose.src; 
												alert("Not Bad. This is a draw.\nYou win "+rolldraw+" credits.");
												credits = parseInt(credits) + rolldraw;
												setCookie("credits",credits);
												document.creditform.credit.value=credits;
											}
                     else if ((resultb<totl) &&(totl!=30))
											{
												document.statusimage.src = lose.src; 
												alert("Good Roll. Amanda is defeated.\nYou win "+rollwin+" credits.");
												credits = parseInt(credits) + rollwin;
												setCookie("credits",credits);
												document.creditform.credit.value=credits;
											}
											else
											{
												document.statusimage.src = win.src; 
											}

                     checkflag=0;
								 stopflag=0;
                 }    
             }
             
         	}




function Random(max) {
	return Math.floor(max*Math.random() + 1);
}

function init() {
	suitarray = new Array(5);
	suitarray[1] = "c";
	suitarray[2] = "d";
	suitarray[3] = "h";
	suitarray[4] = "s";
	player_array = new Array(6);
	dealer_array = new Array(6);

	StatusArray = new Array(13);
	StatusArray[0] = "Waiting for you to click 'DEAL'";
	StatusArray[1] = "Waiting for Player to Move";
	StatusArray[2] = "Player is Bust - Click OK-->";
	StatusArray[3] = "Waiting for Dealer to Move";
	StatusArray[4] = "Dealer Bust / Player Wins - Click OK -->";
	StatusArray[5] = "Push - Click OK-->";
	StatusArray[6] = "Player Wins - Click OK -->";
	StatusArray[7] = "Player Wins - BlackJack - Click OK-->";
	StatusArray[8] = "Player Wins - 5 Card Trick - Click OK-->";
	StatusArray[9] = "Dealer Wins - Click OK-->";
	StatusArray[10] = "Dealer Wins - BlackJack - Click OK-->";
	StatusArray[11] = "Dealer Wins - 5 Card Trick - Click OK-->";
	StatusArray[12] = "You must click 'HIT' or 'STICK'";

	StatusCode = 0;
	
	ResetStats();
	ResetGame();	
	UpdateStatus(StatusCode);
	InitialBet = 100;
	SabourinBet = InitialBet;
	//document.BJTABLE.BET.value = InitialBet;
	//document.BJTABLE.WINNINGS.value = 0;
	//document.BJTABLE.BALANCE.value = 1000;
	
}

function ResetStats() {
	//document.BJSTATS.reset();
}

function ResetGame() {
	ReadConfig();	
	x=1;
	y=1;
	PlayerScore = 0;
	DealerScore = 0;
	if (StrictBJ == 1) {	
		pbj = 0;
		dbj = 0;
	} else {
		pbj = 1;
		dbj = 1;
	}
	
	player5 = 0;
	dealer5 = 0;
	
	document.BJTABLE.PLAYERSCORE.value = "";
	document.BJTABLE.DEALERSCORE.value = "";
	for(i=1; i<=5; i++) {	
		document.images['p' + i].src = eval("trans.src");
		document.images['d' + i].src = eval("trans.src");		
		player_array[i] = 0;
		dealer_array[i] = 0;
	}

	if (Sabourin == 1) {
		//document.BJTABLE.BET.value = SabourinBet;
	}

}

function ReadConfig() {
	  DealerSticks = 17;
	
	  PlayerSticks = 18;

		SmartDealing = 1;
		StrictBJ = 1;
  	Show2Card = 1;
		AllowPush = 1;
		FiveTrick = 1;
		Sabourin = 1;
}

function Deal() {
	if (StatusCode == 0) {
		credits = getCookie("credits");credits++;credits--;
		if ((credits < blackcost)||(credits==null)) {alert ("Not enough credits...");return;}
		credits -= blackcost;
		setCookie("credits",credits);
		document.creditform.credit.value=credits;
		document.statusimage.src = play.src; 

		CurrentBet = 1;//(1 * document.BJTABLE.BET.value);		
		ResetGame();
		StatusCode = 1;
		Hit('p');
		Hit('d');
		Hit('p');
		if (Show2Card == 1) {
			Hit('d');
		} else {
			document.images['d2'].src = eval ("blank.src");		
		}
	}
}

function Hit(person) {
	if ((StatusCode == 1) || (StatusCode == 3) || (StatusCode == 12)) {
		card = Random(13);
		suit = Random(4);
		
		/*	For Testing Purposes
		if (y == 1) { card = 1; }
		if (y == 2) { card = 1; }
		if (y == 3) { card = 10; }
		if (y == 4) { card = 3; }
		if (x == 5) { card = 8; }
		*/
		
		card_id = suitarray[suit] + card;	
		//document.BJSTATS[card_id].value ++;
		//document.BJSTATS['total'].value ++;
		//document.BJSTATS[suitarray[suit]].value ++;
		//document.BJSTATS['t' + card].value ++;
	
		if ((person == "p") && (StatusCode != 3)){	
			
			if ((card_id == "c11") || (card_id == "s11")) {
				pbj = 1;
			}
			document.images[person + x].src = eval (card_id + ".src");		
			player_array[x] = card
			x++;				
			sumPlayer();
			if (x > 5) {
				Stick();
			}
		} else {
//			document.BJSTATS[card_id].value ++;
//			UpdateStats();

			if ((card_id == "c11") || (card_id == "s11")) {
				dbj = 1;
			}
			document.images[person + y].src = eval (suitarray[suit] + card + ".src");
			dealer_array[y] = card;
			y++;
			sumDealer();
		}
		
	}
}

function sumPlayer() {
	player_score = 0;	
	player_aces = 0;
	big_aces = 0;
			
	for (i=1; i<player_array.length; i++) {
		card_value = player_array[i];
			
		if (card_value > 10) {
			card_value = 10;
		} else {
			if (card_value == 1) {
				player_aces ++;			
				card_value = 0;
			}
		}
		player_score = player_score + card_value;
		
	}	
		
	if (player_aces > 0) {
		if (player_score <=10) {
			player_score += 10 + player_aces;
			if (player_score > 21) {
				player_score -= 10;
			} else {
				big_aces = 1;
			}
		} else {
			player_score += player_aces;
		}			
	
	}		

	if ((big_aces != 0) && (player_score <= 21))  {
		pscoretext =  player_score + ' or ' + (player_score - 10);
	} else {
		pscoretext = player_score;
	}

	if (player_score > 21) {
		StatusCode = 2;
	} else {
		StatusCode = 1;
	}
	UpdateStatus(StatusCode);
	
	document.BJTABLE.PLAYERSCORE.value = pscoretext;
	if ((player_score >= PlayerSticks) && (player_score <=21)){
		//document.BJTABLE.STICK.focus();
	}
	


}

function sumDealer() {
	dealer_score = 0;	
	dealer_aces = 0;
			
	for (i=1; i<dealer_array.length; i++) {
		card_value = dealer_array[i];
		
		if (card_value > 10) {
			card_value = 10;
		} else {
			if (card_value == 1) {
				dealer_aces ++;			
				card_value = 0;
			}
		}
		dealer_score = dealer_score + card_value;
		
	}	
		
	if (dealer_aces > 0) {
		if (dealer_score <=10) {
			dealer_score += 10 + dealer_aces;
			if (dealer_score > 21) {
				dealer_score -= 10;
			} else {
				big_aces = 1;
			}
		} else {
			dealer_score += dealer_aces;
		}			
	
	}		

	document.BJTABLE.DEALERSCORE.value = dealer_score;

}

function Stick() {
	if ((StatusCode == 1) || (StatusCode == 12)) {	
		document.BJTABLE.PLAYERSCORE.value = player_score;
		StatusCode = 3;
		UpdateStatus(StatusCode);

		if (SmartDealing == 1) {
			while (((dealer_score <= player_score) && (y <= 5)) && (dealer_score < DealerSticks)){
				Hit('d');
			}
		} else {
			while ((dealer_score < DealerSticks) && (y <= 5)){
				Hit('d');
			}
		}
		DetermineWinner(dealer_score, y, player_score, x);				
	}

}
	
function ClickOK() {
	if ((StatusCode != 0) && (StatusCode != 1) && (StatusCode !=12)) {	
		//document.BJSTATS.games.value++;		
		if ((StatusCode == 2) || (StatusCode == 9) || (StatusCode == 10) || (StatusCode == 11)){
			//document.BJSTATS.lost.value++;
			//document.BJTABLE.WINNINGS.value = -CurrentBet;
			//document.BJTABLE.BALANCE.value -= CurrentBet;
			SabourinBet = SabourinBet * 2;
			document.statusimage.src = win.src; 
		} else {
			if ((StatusCode == 7)||(StatusCode == 8)) {
				//document.BJSTATS.won.value++;
				//document.BJTABLE.WINNINGS.value = 1.5 * CurrentBet;
				//document.BJTABLE.BALANCE.value = (1 * document.BJTABLE.BALANCE.value) + 1.5 * CurrentBet;
				SabourinBet = InitialBet;

					document.statusimage.src = lose.src; 
					alert("Perfect ! Kit'o'Brady is crushed.\nYou win "+blackperfect+" credits.");
					credits = parseInt(credits) + blackperfect;
					setCookie("credits",credits);
					document.creditform.credit.value=credits;
			} else {
				if (StatusCode == 5) {
					//document.BJSTATS.tied.value++;
					//document.BJTABLE.WINNINGS.value = CurrentBet;
					SabourinBet = SabourinBet;

					document.statusimage.src = lose.src; 
					alert("Not bad. This is a draw.\nYou win "+blacktied+" credits.");
					credits = parseInt(credits) + blacktied;
					setCookie("credits",credits);
					document.creditform.credit.value=credits;

				} else {
					//document.BJSTATS.won.value++;
					//document.BJTABLE.WINNINGS.value = CurrentBet;
					//document.BJTABLE.BALANCE.value = (1 * document.BJTABLE.BALANCE.value) + CurrentBet;
					SabourinBet = InitialBet;

				document.statusimage.src = lose.src; 
				alert("Well done ! Kit'o'Brady is defeated.\nYou win "+blackwin+" credits.");
				credits = parseInt(credits) + blackwin;
				setCookie("credits",credits);
				document.creditform.credit.value=credits;
				}
			}
		}				
		StatusCode = 0;
		ResetGame();
		UpdateStatus(StatusCode);
	} else {
		if (StatusCode != 0) {		
			StatusCode = 12;
			UpdateStatus(StatusCode);
		}
	}
}

function UpdateBet() {
	if (StatusCode == 0) {
		//CurrentBet = document.BJTABLE.BET.value 
	} else {
		//document.BJTABLE.BET.value = CurrentBet;
	}
}

function UpdateStatus(code) {
	if ((AllowPush == 0) && (code == 5)) {
		code = 9;
	}
	document.BJTABLE.STATUS.value = StatusArray[code];

	if (code == 0) {
		document.BJTABLE.DEAL.focus()
	} else {
		if ((code == 1) || (code == 12)) {
			document.BJTABLE.HIT.focus()
		} else {
			document.BJTABLE.OK.focus()
		}
	}	
}


function DetermineWinner(dealer, dcards, player, pcards) {
	dcards--;
	pcards--;

	// Player has Blackjack ?
	if ((pcards == 2) && (player == 21) && (pbj == 1)) {
		pbj = 1;
	} else {
		pbj = 0;			
	}

	// Dealer has Blackjack ?
	if ((dcards == 2) && (dealer == 21) && (dbj == 1)) {
		dbj = 1;
	} else {
		dbj = 0;
	}

	// Player has 5 card trick ?
	if ((pcards == 5) && (player <= 21) && (FiveTrick == 1)) {
		player5 = 1;
	}
	
	// Dealer has 5 card trick ?
	if ((dcards == 5) && (dealer <=21) && (FiveTrick == 1)) {
		dealer5 = 1;
	}
	
	// Determinator ...
	
	if ((pbj == 1) && (dbj == 1)) {
		StatusCode = 5;
	} else {
		if (dbj == 1) {
			StatusCode = 10;
		} else {
			if (pbj == 1) {
				StatusCode = 7;
			} else {
				if (dealer > 21) {
					StatusCode = 4;
				} else {
					if ((player5 == 1) && (dealer5 == 1)) {
						if (player > dealer) {
							StatusCode = 6;
						} else {
							if (dealer > player) {
								StatusCode = 9;
							} else {
								StatusCode = 5;
							}
						}
					} else {
						if (player5 == 1) {
							StatusCode = 8;
						} else {
							if (dealer5 == 1) {
								StatusCode = 11;
							} else {
								if (player > dealer) {
									StatusCode = 6;
								} else {
									if (dealer > player) {
										StatusCode = 9;
									} else {
										StatusCode = 5;
									}
								}
							}
						}
					}
				}
			}
		}
	}
	UpdateStatus(StatusCode);
}


function EncodeItem ()
{
 credits = getCookie("credits");
 if ((credits < 1)||(credits==null)) {alert ("No more credits...");return;}


 if ((document.itemform.itemchoice.selectedIndex == 1)&&(credits>=itemseuil2))
 {
		credits -= itemseuil2;
		setCookie("credits",credits);
		document.creditform.credit.value=credits;

		Item = ""+parseInt(Math.random()*cardnb+1);
		Part = ""+parseInt(Math.random()*partnb+1);
		Code=SetItemCode (Item, Part);		
 }
	else if ((document.itemform.itemchoice.selectedIndex == 2)&&(document.itemform.whichpix.value<=cardnb)&&(credits>=itemseuil3))
 {
		credits -= itemseuil3;
		setCookie("credits",credits);
		document.creditform.credit.value=credits;

		Item = ""+document.itemform.whichpix.value;
		if (Item <= 0) {Item=1;}
		if (Item > cardnb) {Item=cardnb;}
		Part = ""+parseInt(Math.random()*partnb+1);
		Code=SetItemCode (Item, Part);		
 } else if ((document.itemform.itemchoice.selectedIndex == 3)&&(credits>=itemseuil4)&&(document.itemform.whichpix.value<=cardnb)&&(document.itemform.whichpart.value<=partnb))
 {
		credits -= itemseuil4;
		setCookie("credits",credits);
		document.creditform.credit.value=credits;

		Item = ""+document.itemform.whichpix.value;
		if (Item <= 0) {Item=1;}
		if (Item > cardnb) {Item=cardnb;}
		Part = ""+document.itemform.whichpart.value;
		if (Part <= 0) {Part=1;}
		if (Part > partnb) {Part=partnb;}
		Code=SetItemCode (Item, Part);				
 } else if ((document.itemform.itemchoice.selectedIndex == 4)&&(credits>=itemseuil5))
 {
		credits -= itemseuil5;
		setCookie("credits",credits);
		document.creditform.credit.value=credits;

		Item = ""+parseInt(Math.random()*cardnb+1);
		Part = ""+0;
		Code=SetItemCode (Item, Part);				
		
 } else if ((document.itemform.itemchoice.selectedIndex == 5)&&(credits>=itemseuil6)&&(document.itemform.whichpix.value<=cardnb))
 {
		credits -= itemseuil6;
		setCookie("credits",credits);
		document.creditform.credit.value=credits;

		Item = ""+document.itemform.whichpix.value;
		if (Item <= 0) {Item=1;}
		if (Item > cardnb) {Item=cardnb;}
		Part = ""+0;
		Code=SetItemCode (Item, Part);				
		
 } else
 { alert ("Invalid Request");}
}

function SetItemCode (item,part)
{
 		pwdvalue=""+getCookie("visitor");
		pwdvalue=pwdvalue.substring(0,8);
		randomvalue= parseInt(Math.random ()*1000);
		randomvalue2= bit;
    result = warp(pwdvalue+randomvalue2+item.substring(0,1)+randomvalue2+part.substring(0,1)+randomvalue,13); 
    document.itemform.result.value=result;
	  alert ("Please write down this code for it will be necessary \nto display your Item and to request the printed\nversion of a complete card.");

		return result;
}

function DecodeItem ()
{

  stringaction=warp(document.decodeitem.code.value,13);
  name=stringaction.substring(0,8);
  card=stringaction.substring(9,10);card--; card++;
  bit=stringaction.substring(10,11); 
	part=stringaction.substring(11,12);part--;part++;
	login=""+getCookie("visitor");

  //alert(stringaction+" "+name+" "+card+" "+part);
	if (name == login.substring (0,8))
	{
		if (part == 0)
		{
			for (part=1;part<=4;part++)
			{
				Source="../images/"+GenericName[card]+part+".jpg";
				document.decodeitem['part'+part].src = Source;
			}
		}
		else
		{
			Source="../images/"+GenericName[card]+part+".jpg";
			document.decodeitem['part'+part].src = Source;
		}
	}
	else
	{
		alert ("Action code incompatible with \ncurrent login name :\n"+getCookie ("visitor"));
	}
}

function DisplayItemCode ()
{
  stringaction=warp(document.decodeitem.code.value,13);
  name=stringaction.substring(0,8);
  card=stringaction.substring(9,10);card--; card++;
  bit=stringaction.substring(10,11); 
  part=stringaction.substring(11,12);part--;part++;

  if (bit ==1) {alert("Code : "+stringaction+"\nLogin :"+name+"\nImage : "+GenericName[card]+part+".jpg");}
  else {alert("Warning Bit != 1!!\nCode : "+stringaction+"\nLogin :"+name+"\nImage : "+GenericName[card]+part+".jpg");}
}
