<!--

function Blackjack()
{
    this.bjdivs = '<div id="dealerbrd"></div><div id="stackbrd"></div><div id="infobrd_2"><div id="dlrcnt" style="float:left"></div><div id="plrsts" style="float:right"></div></div><div id="infobrd_1"><div id="curcnt" style="float:left"></div><div id="curbal" style="float:right;color:#ffff00;"></div></div><div id="playerbrd"></div>';

    this.cardArr = new Array("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K");
    this.shfarr = new Array();
    this.autoHits = new Array();
    this.cardClrs = new Array("card_s", "card_h", "card_d", "card_c");
    this.txtClrs1 = new Array("txt_b", "txt_r", "txt_r", "txt_b");
    this.txtClrs2 = new Array("txt_w_s", "txt_w_h", "txt_w_d", "txt_w_c");
    this.masterDgn = 0;this.playerCount = 0;this.playerMoney = 0;
    this.DEAL = 1; this.HIT=2;this.STAND=4;this.SPLIT=8;this.DOUBLE=16;this.INSURANCE=32;this.FINISH = 64;
    this.gameCount = 0;this.insurance = false;this.doubleDown = false;this.bet;this.doubleArr = new Array();
    this.prevbtns = this.DEAL;
    
    this.PLAYER = 100; this.DEALER = 200;
    
    this.playerElements = new Array();
    this.dealerElements = new Array();
    
    var self = this;
    
    this.getPlayerX = function(id)
    {
        id = id % 9;
        return 10 + id * 30;
    }
    
    this.getPlayerY = function(id)
    {
        var topoff = 235 + Math.floor(id/9)*30;
        id = id % 9;
        return topoff + id * 5;
    }

    this.getDealerX = function(id)
    {
        id = id % 6;
        return 10 + id * 30;
    }
    
    this.getDealerY = function(id)
    {
        var topoff = 55 + Math.floor(id/6)*30;
        id = id % 6;
        return topoff + id * 5;
    }

	this.shuffleArray = function(a)
	{
		for (var m = 0; m < a.length; m++)
		{
			t1 = this.getRandom(0,a.length-1);t2 = this.getRandom(0,a.length-1);
			if (t1 != t2) {tt = a[t1];a[t1]=a[t2];a[t2]=tt;}
		}
	}

	this.getRandom = function(min,max)
	{
		return min + Math.floor(Math.random()*(max-min+1))
	}
    
    
    this.spliceArray = function(arr)
    {
        if (arr.length > 0) {arr.splice(0,arr.length);}
        if (arr.length > 0) {while(arr.length > 0) {arr.pop();}}
    }
    
    this.makeDeck = function(ndecks)
    {
        this.spliceArray(this.shfarr);
		for (var i = 0; i < 52*ndecks; i++) {this.shfarr.push(i%52);}
		this.shuffleArray(this.shfarr);
    }

	this.initScreen = function(ndecks)
	{
	    this.playerMoney = 100;this.doubleArr.push(10);this.doubleArr.push(11);
	    this.bet = 10;
	    this.gameCount = 0;
	    var o = document.getElementById("blackjackbrd"); if (o != null) {o.innerHTML = this.bjdivs;}
	    this.makeDeck(ndecks);
	    this.newDeal();
	}
    
    this.play2Cards = function()
    {
        this.spliceArray(this.autoHits);
        this.autoHits.push([this.DEALER, false]);
        this.autoHits.push([this.PLAYER, true]);
        this.autoHits.push([this.DEALER, true]);
        this.hitPlayerCard();
    }
    
    this.cleanBoard = function()
    {
		var o = document.getElementById("playerbrd");
		if (o != null)
		{
		    while (this.playerElements.length > 0)
		    {
		        var obj = this.playerElements.pop();
		        var el = obj[0];
		        o.removeChild(el);
		    }
		}

		o = document.getElementById("dealerbrd");
		if (o != null)
		{
		    while (this.dealerElements.length > 0)
		    {
		        var obj = this.dealerElements.pop();
		        var el = obj[0];
		        o.removeChild(el);
		    }
		}
    }
    
	this.newDeal = function()
	{
	    this.playerCount = 0;
	    this.insurance = false;this.doubleDown=false;
	    this.gameCount++;	    
	    this.cleanBoard();
	    this.showPlayerCount("");
	    this.showDealerCount("");
	    this.showButtons(this.prevbtns,0);
	    var newdgn = this.getRandom(1,4);while (this.masterDgn == newdgn) {newdgn = this.getRandom(1,4);}
	    this.masterDgn = newdgn;
		var stkobj = document.getElementById("stackbrd");
		if (stkobj != null) { stkobj.innerHTML = '<div class="cardof_' + this.masterDgn + '"></div>'; }			
	    this.play2Cards();
	}

	
	this.getButtonStr = function(flag, title, btnid)
	{
	    var htmStr = "";var sufix = (flag ? "e" : "d");
	    htmStr += '<div class="left_' + sufix + '"></div><div class="mid_' + sufix + '"';
	    if (flag) {htmStr += ' id="' + btnid + '"';}
	    htmStr += ('>' + title + '</div><div class="right_' + sufix + '"></div>');
	    return htmStr;
	}
	
	this.showButtons = function(n, m)
	{
	    if (m > 0) {this.prevbtns = n;}

	    var str = '<div style="float:right;">';

	    if (n & this.DOUBLE)
	    {
	        if (m & this.DOUBLE) {str += this.getButtonStr(true, "Double", "dblbtn");}
	        else {str += this.getButtonStr(false, "Double", null);}			
	    }

	    if (n & this.SPLIT)
	    {
	        if (m & this.SPLIT) {str += this.getButtonStr(true, "Split", "splitbtn");}
	        else {str += this.getButtonStr(false, "Split", null);}			
	    }

	    if (n & this.STAND)
	    {
	        if (m & this.STAND) { str += this.getButtonStr(true, "Stand", "standbtn"); }
	        else { str += this.getButtonStr(false, "Stand", null); }
	    }

	    if (n & this.DEAL)
	    {
	        if (m & this.DEAL) { str += this.getButtonStr(true, "Deal", "dealbtn"); }
	        else { str += this.getButtonStr(false, "Deal", null); }
	        
	    }

	    if (n & this.HIT)
	    {
	        if (m & this.HIT) {str += this.getButtonStr(true, "Hit", "hitbtn");}
	        else {str += this.getButtonStr(false, "Hit", null);}			
	    }

	    if (n & this.FINISH)
	    {
	        str += ('<div class="btntxt">Game over:&nbsp;&nbsp;</div>' + this.getButtonStr(true, "OK", "okbtn"));
	    }

	    if (n & this.INSURANCE)
	    {
	        str += ('<div class="btntxt">Insurance?&nbsp;&nbsp;</div>' + this.getButtonStr(true, "Yes", "yesbtn") + this.getButtonStr(true, "No", "nobtn"));
	    }

	    str+= '</div>';
	    
		var btnobj = document.getElementById("btnbrd");
		if (btnobj != null)
		{
			btnobj.innerHTML = str;
		}
		
	    if (m & this.STAND){btnobj = document.getElementById("standbtn"); if (btnobj != null) {btnobj.onclick = function(){self.playerStand();} }}
	    if (m & this.DEAL){btnobj = document.getElementById("dealbtn"); if (btnobj != null) {btnobj.onclick = function(){self.newDeal();} }}
	    if (m & this.HIT){btnobj = document.getElementById("hitbtn"); if (btnobj != null) {btnobj.onclick = function(){self.hitCard();} }}
	    if (m & this.FINISH){btnobj = document.getElementById("okbtn"); if (btnobj != null) {btnobj.onclick = function(){self.submitfrm();} }}
	    if (m & this.INSURANCE){btnobj = document.getElementById("yesbtn"); if (btnobj != null) {btnobj.onclick = function(){self.continueInsurance(true);} }}
	    if (m & this.INSURANCE){btnobj = document.getElementById("nobtn"); if (btnobj != null) {btnobj.onclick = function(){self.continueInsurance(false);} }}
	    if (m & this.DOUBLE){btnobj = document.getElementById("dblbtn"); if (btnobj != null) {btnobj.onclick = function(){self.doDouble();} }}
	    if (m & this.SPLIT){btnobj = document.getElementById("splitbtn"); if (btnobj != null) {btnobj.onclick = function(){self.doSplit();} }}
	}

	this.doDouble = function()
	{
	    this.showButtons(this.prevbtns, 0);
	    this.doubleDown = true;
		this.hitPlayerCard();
	}
	
	this.doSplit = function()
	{
	    this.showButtons(this.prevbtns, 0);
	}

	
	this.submitfrm = function()
	{
		var f = document.getElementById("blackjackbrd");
		if (f != null) 
		{
		    var str = "Blackjack Slice<br>";
		    this.playerMoney -= 100;
		    if (this.playerMoney > 0) {str += ("Congratulations!<br/> You won $" + this.playerMoney);}
		    else if (this.playerMoney < 0) {str += ("Sorry!<br/> You lost $" + Math.abs(this.playerMoney));}
		    else {str += ("Congratulations!</br> You lost no money");}
		    
			f.innerHTML = "";
			var o = f.appendChild(document.createElement('div'));
			o.innerHTML='<input type="hidden" name="helppage" value="blackjackhelp.html" />';
			var o = f.appendChild(document.createElement('div'));
			o.innerHTML='<input type="hidden" name="gamepage" value="blackjack.html" />';
			o = f.appendChild(document.createElement('div'));
			o.innerHTML='<input type="hidden" name="msg" value="' + str + '" />';
		}

		document.blackjack_frm.submit();
	}
	
    this.checkDealerHand = function()
    {
	    var allcnt = this.cardCount(this.dealerElements);
	    var ace = Math.floor((allcnt%1000)/100);var cnt = allcnt % 100;
	    var dblmny = 1; if (this.doubleDown) {dblmny = 2;}
	    var plrstr = "";
	    if ((cnt < 17) || (cnt == 17 && ace == 1))
	    {
	        this.showDealerCount(allcnt);
	        this.hitDealerCard(true);
	        return;
	    }
	    else if (allcnt == 2121)
	    {
	        if (this.insurance) {this.playerMoney += (this.bet * 1.5);}
	        if (this.playerCount == 2121) {plrstr = "Push";} else {plrstr = "Lose";this.playerMoney -= (this.bet*dblmny);}
	    }
	    else if (this.playerCount == 2121) {plrstr=" Win";this.playerMoney += ((this.bet*dblmny) * 1.5);}
	    else if (cnt > 21) {plrstr="Win";this.playerMoney += (this.bet*dblmny);}
	    else if (cnt > (this.playerCount%100)) {plrstr = "Lose";this.playerMoney -= (this.bet*dblmny);}
	    else if (cnt < (this.playerCount%100)) {plrstr = "Win";this.playerMoney += (this.bet*dblmny);}
	    else {plrstr = "Push";}
	    
        this.showDealerCount(allcnt);
        this.showPlayerCount(plrstr);
        if (this.gameCount > 5) {this.showButtons(this.FINISH, this.FINISH);}
        else {this.showButtons(this.DEAL, this.DEAL);}        
    }
    
    this.checkInsuranceMoney = function()
    {
	    this.showCard(this.dealerElements[0], this.DEALER);
	    var allcnt = this.cardCount(this.dealerElements);
	    if (allcnt == 2121) {this.playerMoney += (this.bet * 1.5);this.showPlayerCount("");}
    }
    
	this.playerStand = function()
	{
	    this.showButtons(this.prevbtns, 0);
	    this.showCard(this.dealerElements[0], this.DEALER);
		this.checkDealerHand();
	}
	
	this.pickNextCard = function()
	{
		var el = document.createElement("div");
		el.className = "card_float";
		el.style.left = "240px";
		el.style.top = "89px";
		el.innerHTML = '<div class="cardof_' + this.masterDgn + '"></div>';
		return el;
	}

	this.hitCard = function()
	{
	    this.showButtons(this.prevbtns, 0);
		this.hitPlayerCard();
	}

	this.hitPlayerCard = function()
	{
		var el = this.pickNextCard();
		var o = document.getElementById("playerbrd");
		if (o != null) {o.appendChild(el);}
		var n = this.shfarr.shift();
		this.playerElements.push([el, n]);
		this.movePlayerCard();
	}
	
	
	this.hitDealerCard = function(flag)
	{
		var el = this.pickNextCard();
		var o = document.getElementById("dealerbrd");
		if (o != null) {o.appendChild(el);}
		var n = this.shfarr.shift();
		this.dealerElements.push([el, n]);
		if (flag) {this.moveDealerCard();} else {this.moveDealerCardNS();}
	}

	this.continuePlay = function(who)
	{
	    if (this.autoHits.length > 0)
	    {
			var nc = this.autoHits.shift();
			if (nc[0] == this.PLAYER) {this.hitPlayerCard();} 
			else if (nc[0] == this.DEALER)
			{
				if (nc[1]) {this.hitDealerCard(true);} else {this.hitDealerCard(false);}
			}
	    }
	    else if (who == this.PLAYER)
	    {
	        this.checkPlayerCards(this.playerElements);
	    }
	    else if (this.dealerElements.length == 2) {this.tryInsurance();}
	    else {this.checkDealerHand();}
	}

	this.showCard = function(arr, who)
	{
	    var n = arr[1]; var el = arr[0];	    
	    var faceValue = n % 13;
	    var cardType  = Math.floor(n/13);
        el.innerHTML = '<div class="' + this.cardClrs[cardType] + '"><div class="' + this.txtClrs1[cardType] + '">' + this.cardArr[faceValue] + '</div><div class="' + this.txtClrs2[cardType] + '">' + this.cardArr[faceValue] + '</div></div></div>';
        if (who == this.PLAYER && this.playerElements.length < 3) 
        {
            this.playerCount = this.cardCount(this.playerElements);this.showPlayerCount("");
        }
	}

	this.showDealerCount = function(cnt)
	{
		var o = document.getElementById("dlrcnt");
		if (o != null)
		if (o != null) 
		{
		    str = "Dealer: " + cnt % 100;
		    if (cnt == 2121) {str += " Blackjack";} else if (cnt%100 > 21) {str += " Bust";}
		    o.innerHTML = str;
		}
	}
		
	this.showPlayerCount = function(optstr)
	
	{
		var o = document.getElementById("curcnt");
		if (o != null) 
		{
		    str = "Player: " + this.playerCount % 100;
		    if (this.playerCount == 2121) {str += " Blackjack";} else if (this.playerCount%100 > 21) {str += " Bust";}
		    o.innerHTML = str + " " + optstr;
		}
		o = document.getElementById("curbal"); if (o != null)  {o.innerHTML = "Wallet: $" + this.playerMoney;}
	}
	
	this.cardCount = function(eleArr)
	{
	    var cnt = 0;
	    var ace = 0;
	    for (var i = 0; i < eleArr.length; i++)
	    {
	        var faceValue = eleArr[i][1] % 13;faceValue++;
	        if (faceValue > 10) {faceValue = 10;}
	        cnt += faceValue;
	        if (faceValue == 1) {ace = 10;}
	    }
	    	    
		if (cnt + ace <= 21) {cnt += ace;}
		cnt = 10*ace + cnt;
		cnt += eleArr.length * 1000;
		
	    return cnt;
	}
	
	this.checkPlayerCards = function(eleArr)
	{
	    
	    var btns = this.HIT | this.STAND;
	    var state = this.HIT | this.STAND;
	    var cnt = this.cardCount(eleArr);
	    this.playerCount = cnt;
	    var ncards = Math.floor(cnt/1000);
        if (cnt == 2121) {state=0;} else {cnt = cnt % 100;}
	    if (ncards == 2)
	    {
		   for (var k = 0; k < this.doubleArr.length; k++) {if (cnt == this.doubleArr[k]) {btns |= this.DOUBLE;state |= this.DOUBLE;}}
	    }
	    else if (cnt == 21) {state = 0;}
	    else if (cnt > 21) {btns = this.DEAL;state=this.DEAL;this.playerMoney -= this.bet; if (this.gameCount > 5) {btns = this.FINISH;state=this.FINISH;}}
	    
	    this.showPlayerCount("");
	    if (this.doubleDown) {state=0;}
	    this.showButtons(btns, state);
	    if (state == 0) {this.playerStand();}
	    if (cnt > 21 && this.insurance) {this.checkInsuranceMoney();}
	}
	
	this.movePlayerCard = function()
	{
	    var el = self.playerElements[self.playerElements.length-1][0];
	    var pcIndex = self.playerElements.length-1;
	    var xpos = self.getPlayerX(pcIndex);
	    var ypos = self.getPlayerY(pcIndex);
	    
		var x = parseInt(el.style.left);
		var y = parseInt(el.style.top);
		var off_x = xpos - x; if (Math.abs(off_x) > 10) {off_x = off_x/10;}
		var off_y = ypos - y; if (Math.abs(off_y) > 10) {off_y = off_y/10;}
		x += off_x; y += off_y;
		el.style.left = x + 'px';el.style.top = y + 'px';
		if ((x != xpos) || (y != ypos)) {setTimeout(self.movePlayerCard, 10);}
		else {self.showCard(self.playerElements[self.playerElements.length-1], self.PLAYER);self.continuePlay(self.PLAYER);}
	}

	this.moveDealerCard = function()
	{
	    var el = self.dealerElements[self.dealerElements.length-1][0];
	    var pcIndex = self.dealerElements.length-1;
	    var xpos = self.getDealerX(pcIndex);
	    var ypos = self.getDealerY(pcIndex);
	    
		var x = parseInt(el.style.left);
		var y = parseInt(el.style.top);
		var off_x = xpos - x; if (Math.abs(off_x) > 10) {off_x = off_x/10;}
		var off_y = ypos - y; if (Math.abs(off_y) > 10) {off_y = off_y/10;}
		x += off_x; y += off_y;
		el.style.left = x + 'px';el.style.top = y + 'px';
		if ((x != xpos) || (y != ypos)) {setTimeout(self.moveDealerCard, 10);}
		else {self.showCard(self.dealerElements[self.dealerElements.length-1], self.DEALER);self.continuePlay(self.DEALER);}
	}

	this.moveDealerCardNS = function()
	{
	    var el = self.dealerElements[self.dealerElements.length-1][0];
	    var pcIndex = self.dealerElements.length-1;
	    var xpos = self.getDealerX(pcIndex);
	    var ypos = self.getDealerY(pcIndex);
	    
		var x = parseInt(el.style.left);
		var y = parseInt(el.style.top);
		var off_x = xpos - x; if (Math.abs(off_x) > 10) {off_x = off_x/10;}
		var off_y = ypos - y; if (Math.abs(off_y) > 10) {off_y = off_y/10;}
		x += off_x; y += off_y;
		el.style.left = x + 'px';el.style.top = y + 'px';
		if ((x != xpos) || (y != ypos)) {setTimeout(self.moveDealerCardNS, 10);}
		else {self.continuePlay(self.DEALER);}
	}
	
	this.tryInsurance = function()
	{
	    if (this.dealerElements[1][1]%13 == 0) {this.showButtons(this.INSURANCE,this.INSURANCE);}
		else {this.checkPlayerCards(this.playerElements);}
	}
	
	this.continueInsurance = function(flag)
	{
	    this.insurance = flag; if (flag) {this.playerMoney -= this.bet/2;}
	    this.checkPlayerCards(this.playerElements);
	}
}



-->

