//************************************************************
//    DT HMS     
//    -------------------------------------------
//************************************************************


//Global Constants
CWIDTH=80
CHEIGHT=140
FANH=15
FANV=20
PACKED='P'
VERTFAN='V'
HORIZFAN='H'
FLOATIMGS=30;

//Pile Rules Constants
NONE='N'
ANY='Y'
FIXED='X'
BOTH='B'
UP='U'
DOWN='D'
SUIT='S'
RANK='R'
COLOR='C'
ALTCOLOR='A'

//Constant Arrays
SUITS=new Array("H");
RANKS=new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V");

//Global Variables



var House=new Array("<b><i>The Magician:</i></b>  Effort is the keyword here. You will improve your financial situation if you make the necessary effort..",
"<b><i>The High Priestess:</i></b>  You have control of your financial life.  The results will be from your actions.  You will have luck regarding contracts or business..",
"<b><i>The Empress:</i></b> Possibility for a small increase of money or a slow improvement concerning your financial goals.",
"<b><i>The Emperor:</i></b> You have control of your financial life.  The results will be from your actions.  Luck or help may come from an important man.",
"<b><i>The Pope:</i></b> Protection is the key, here. Financial luck may come from real estate, family members, or a partner.",
"<b><i>The Lovers:</i></b>  Expect some financial instability.  Usually this card brings a small increase of money and can be favorable concerning any negotiations. ",
"<b><i>The Chariot:</i></b> Financial success or expansion ahead.  Luck with the media, from a distance or with a foreigner.",
"<b><i>The Justice:</i></b> Honesty is the keyword here. You need to balance your financial needs.  You may have a financial or legal settlement ahead.  Don't neglect your responsibilities.",
"<b><i>The Hermit:</i></b>  Patience is the keyword here.  Luck may come with time, research and information.  This card is very favorable regarding long-term investments.",
"<b><i>The Wheel of Fortune:</i></b>  You may expect some fast and positive movement concerning your financial life.  Period of luck.",
"<b><i>The Strength:</i></b>This card represents power and luck.  No matter what happens, you may expect a positive financial outcome.",
"<b><i>The Hanged Man:</i></b> This card represents loss or  illusion.  Be very careful concerning any financial investment or project. ",
"<b><i>The Death:</i></b>This card represents the end of a financial situation and the beginning of another one.  Don't be afraid of change.  Any financial project will be positive.",
"<b><i>The Temperance:</i></b> Moderation is the keyword here.  Your finances improve slowly, leading to security and stability. ",
"<b><i>The Devil:</i></b> Cunning is the keyword here.  Don't try to control everyone or every thing.  Your finances may suffer if you don't  control your financial desires.",
"<b><i>The Broken House:</i></b>  Risk of sudden financial loss or unexpected expenses.  ", 
"<b><i>The Star:</i></b>This card represents hope and opportunities.  Don't worry about your financial matters.  Accept the opportunities that come your way.",
"<b><i>The Moon:</i></b> This card represents confusion or hidden matters.  Be realistic concerning your financial matters; don't be a dreamer.  You risk loss by choosing the wrong path. ",
"<b><i>The Sun:</i></b> This card represents financial success.  No matter what you wish, you will get it!. ",
"<b><i>The Judgment:</i></b> This card represents the unexpected with uncontrollable events.  A sudden income is possible.  Luck may come from a decision from others. ",
"<b><i>The World:</i></b> This card represents success.  Large increase of money ahead.  Luck from others (clientele, media, associate, etc.).  ",
"<b><i>The Fool:</i></b>  This card represents instability or sudden loss.  Don't neglect your financial matters.  Risk of fraud or loss from an impulsive decision.  Don't make any major changes in your life now.")


var aText=new Array("Represents your question ",
"What could affect your finances ",
"What is out of your control ",
"The answer to your question "
)

CurrPile=1;
Dragging=false;
InitLeft=0;
InitTop=0;
InitElleft=0;
InitElTop=0;
SourceI=0;
SourceCard=0;
TargetCard=0;
cards2drag=0;
BaseCard='';
NumDeals=0;
CurrDeal=0;
CompletionURL='';
UndoOK=false;
UndoSrc=0;
UndoTgt=0;
InUndo=false;
SaveNumDeals=0;
results=''
var l = '' 
issame=''
c1=''
c2=''
c3=''
c4=''
c5=''
c6=''
c7=''
c8=''
c9=''
c10=''
c11=''
c12=''
var testit = 1

//Global Arrays
aPiles=new Array();
aPileNames=new Array();
aBehaviours=new Array();
aBuild=new Array();
aGroup=new Array();
aDrag=new Array();
aAutoTgt=new Array();
aMaxLength=new Array();
aUndoSrc=new Array();
aUndoTgt=new Array();

//Global Event Handlers
document.onmousedown=DragStart;
document.onmouseup=DragEnd;
document.onmousemove=DoDrag;
document.onclick=DoClick;
document.ondblclick=AutoTarget;
document.onkeydown=DoCommand;
document.onhelp=DefHelp;

function DefHelp(){
event.returnValue=false;
showHelp('solhelp.htm');
}


function DoCommand(){
CommandKey=event.keyCode;
switch(CommandKey){
	case 85:DoUndo();break;// U for Undo
	case 77:
 	   if(confirm('Change game mode to '+(NumDeals==-1?'TOURNAMENT?':'PRACTICE?')))	
           {NumDeals==-1?NumDeals=SaveNumDeals:NumDeals=-1;break;}
            //M to switch between practice and tourney modes
	}
}

function SaveUndo(thesrc,thetgt,inDrag){
setTimeout("InUndo=false",50);
UndoSrc=thesrc;UndoTgt=thetgt;
aUndoSrc.length=0;aUndoTgt.length=0;
for(q=0;q<aPiles[thesrc].length;q++)
   aUndoSrc[q]=aPiles[thesrc][q];
if(inDrag){
   	for(q=0;q<aDrag.length;q++)
		aUndoSrc[aUndoSrc.length]=aDrag[q];
	}
for(q=0;q<aPiles[thetgt].length;q++)
   aUndoTgt[q]=aPiles[thetgt][q];
UndoOK=true;
}

function DoUndo(){
if(!UndoOK) return;
aPiles[UndoSrc].length=0;aPiles[UndoTgt].length=0;
for(i=0;i<aUndoSrc.length;i++)
   aPiles[UndoSrc][i]=aUndoSrc[i];
for(i=0;i<aUndoTgt.length;i++)
  aPiles[UndoTgt][i]=aUndoTgt[i];
PaintCards(UndoSrc);PaintCards(UndoTgt);
UndoOK=false;
}


function Check4Completion(){
//Default completion handler - Game over when all foundations have reached their Maxlength
GameComplete=true;
for(i=0;i<aPileNames.length;i++){
	if(aPileNames[i].substr(0,1)=='F'){ //Foundation


            if(!(aPiles[i].length==aMaxLength[i])) GameComplete=false;
		}
	
//**************************
//alert("ziad is here")  
//DoEffect();
//deadend
//************************

}

//alert("c9   {" +c9+ "}")

//if(c1 !== ''){DoEffect();}
//if(c2 !== ''){DoEffect();}
//if(c3 !== ''){DoEffect();}
//if(c4 !== ''){DoEffect();}
//if(c5 !== ''){DoEffect();}
//if(c6 !== ''){DoEffect();}
//if(c7 !== ''){DoEffect();}
//if(c8 !== ''){DoEffect();}
//if(c9 !== ''){DoEffect();}
//if(c10 !== ''){DoEffect();}
//if(c11 !== ''){DoEffect();}
//if(c12 !== ''){DoEffect();}



//if(GameComplete){location.href=CompletionURL;}

}





//Turn card up if down
function AutoFlip(fpile){
FlipCard=(aPiles[SourceI].length>0)?aPiles[fpile][aPiles[fpile].length-1]:"";
if(FlipCard.length==2)      /// changed to 1 from 2  hms
	aPiles[fpile][aPiles[fpile].length-1]=FlipCard+"U";
//*************hms
//testit = testit+1
					
//*****

//alert(TargetI+" | "+ testit+"  "+)

					//****
}

function AutoTarget(){
el=event.srcElement;
if(!(el.tagName=='IMG')){return}
clickdiv=el.parentElement.id.substr(1);
if(clickdiv==0) return; //Deck does not support AutoTargeting
if(!(aAutoTgt[clickdiv].charAt(0)=='Y')) return;//not an Autotgt source

AutoCard=aPiles[SourceI][SourceCard];
if(AutoCard.length<3) return; //Face Down Card // changed to 4 from 3 hms

//Cycle through AutoTgt targets to see which can accomodate the AutoCard
for(i=1;i<aAutoTgt.length;i++){
	if(aAutoTgt[i].charAt(1)=='Y'){ //Valid AutoTgt target
		if(i==SourceI) continue; //Target should not be same as source
		TargetI=i;
		//TargetCard=aPiles[TargetI][aPiles[TargetI].length-1]
		if(IsValidTarget()){//Move Autocard to target
			SaveUndo(SourceI,TargetI,false);
			aPiles[i][aPiles[i].length]=aPiles[SourceI][aPiles[SourceI].length-1];
			aPiles[SourceI].length--;
			FlipCard=(aPiles[SourceI].length>0)?aPiles[SourceI][aPiles[SourceI].length-1]:"";
			if(FlipCard.length==2)
  				aPiles[SourceI][aPiles[SourceI].length-1]=FlipCard+"U";
			PaintCards(i);PaintCards(SourceI);
			Check4Completion();
			break;
			}
		
		}
	}


}

function DoClick(){
el=event.srcElement;
if(!(el.tagName=='IMG')){return}
clickdiv=el.parentElement.id.substr(1);

if(event.shiftKey){ //Cheat key support
  // alert(aPiles[clickdiv].join("\n"))
   }
else{
   if((clickdiv==0)&&(aPiles[0].length>0)&&(aPiles[0][0].substr(2,1)=='')){//clicked on Face-down Deck
      if(!(aAutoTgt[0]=='')){ //If deck has AutoDealing
		for(i=0;i<aPileNames.length;i++){
                  	if(aPileNames[i]==aAutoTgt[0]){ //found target
				//Undo Support
				SaveUndo(0,i,false);
                        	//Transfer card, turning face up
                        	for(j=1;(j<=aMaxLength[0])&&(aPiles[0].length>0);j++){
					aPiles[i][aPiles[i].length]=aPiles[0][aPiles[0].length-1]+'U';
         				aPiles[0].length--;
                        	PaintCards(0);PaintCards(i);
					

					}
				}
			}
               }
       }
    else{
    //Redeal support-only for single waste pile
    if((clickdiv==0)&&(aPiles[0].length==0)&&!(NumDeals==CurrDeal)){
      CurrDeal++;
      //First find the waste pile
      wp=-1;
      for(i=0;i<aPileNames.length;i++)
          {if(aPileNames[i]==aAutoTgt[0]){wp=i}}
      if(wp>0){
		SaveUndo(wp,0,false);
		//Transfer cards from Waste to Deck
		for(i=0;i<aPiles[wp].length;i++){
                  aPiles[0][i]=aPiles[wp][aPiles[wp].length-i-1].substr(0,2);
 			}
            aPiles[wp].length=0;
            PaintCards(0);PaintCards(wp);
		



}
	}
	}
    
   
    }


}

function IsValidSource(){
retval=true;
tmpRank=aGroup[SourceI].substr(0,1);
tmpSuit=aGroup[SourceI].substr(1,1);
//Check if group movement allowed 
if((tmpRank==NONE)||(tmpSuit==NONE)){
   if(aPiles[SourceI].length==SourceCard+1)//Single card movement
    	{return true}
   else
	{return false} 
   }
if((tmpRank=='')||(tmpSuit=='')){return false}

//CurrCard=aPiles[SourceI][SourceCard].substr(0,2);
for(i=SourceCard+1;i<aPiles[SourceI].length;i++){
	ar=GetAllowableRanks(aPiles[SourceI][i-1].substr(0,1),tmpRank);
      if(ar.lastIndexOf(aPiles[SourceI][i].substr(0,1))<0){return false};
      as=GetAllowableSuits(aPiles[SourceI][i-1].substr(1,1),tmpSuit);
      if(as.lastIndexOf(aPiles[SourceI][i].substr(1,1))<0){return false};
	
	}

return retval;
}

function GetAllowableRanks(tgtrank,piledir,pilelast){
retstr='';
tmpRankStr=RANKS.join('');
if(piledir==ANY){return tmpRankStr}
if(tgtrank==pilelast){return ''}
tmpTgtPos=tmpRankStr.lastIndexOf(tgtrank);
if((piledir==UP)||(piledir==BOTH)){
	retstr+=(tmpTgtPos==tmpRankStr.length-1)?tmpRankStr.charAt(0):tmpRankStr.charAt(tmpTgtPos+1);
    	}
if((piledir==DOWN)||(piledir==BOTH)){
	retstr+=(tmpTgtPos==0)?tmpRankStr.charAt(tmpRankStr.length-1):tmpRankStr.charAt(tmpTgtPos-1);
    	}
return retstr;
}

function GetAllowableSuits(tgtsuit,pilemode){
retstr='';
if(pilemode==SUIT){return tgtsuit}
if(pilemode==ANY){return 'HDCS'}
if(pilemode==COLOR){
   if((tgtsuit=='H')||(tgtsuit=='D')){return 'HD'}
   else{return 'CS'}
   }
if(pilemode==ALTCOLOR){
   if((tgtsuit=='H')||(tgtsuit=='D')){return 'CS'}
   else{return 'HD'}
   }
return '';
}

function IsValidTarget(){
if(TargetI<=0){return false}
tmpBase=aBuild[TargetI].substr(0,1);
tmpDir=aBuild[TargetI].substr(1,1);
tmpMode=aBuild[TargetI].substr(2,1);
tmpLast=aBuild[TargetI].substr(3,1);
srcSuit=aDrag[0].substr(1,1);
srcRank=aDrag[0].substr(0,1);

//******HMSTargetCard**************************************************%%%%%%%%%%%%%%%%%%%%%%%%%***
//alert(TargetCard+TargetI + srcRank+ dgif)

//if(testit == 0){return false}
//else{it = testit+1}

//alert(TargetI)

//************************************************************************
ValTgt=true;
//If Target Pile is packed, Targetcard will not hold right value
if(aBehaviours[TargetI].substr(0,1)==PACKED)
   {TargetCard=aPiles[TargetI].length-1}

//Check for violations of MaxLength and Grouping Rules
if(aMaxLength[TargetI]>0){
	if(aPiles[TargetI].length+aDrag.length>aMaxLength[TargetI])
		 {return false;}
	}
if(cards2drag>1){
      grpRank=aGroup[TargetI].substr(0,1);
      grpSuit=aGroup[TargetI].substr(1,1);
	if((grpRank=='')||(grpSuit=='')||(grpRank==NONE)||(grpSuit==NONE))
           {return false}
	}








//Check if Target Pile is empty-apply empty rule
if(aPiles[TargetI].length==0){
   //if  Empty, Piles are not allowed to be filled...
   if(tmpBase==NONE){return false}
   if(tmpBase==ANY){return true}
   if(tmpBase==FIXED){
      if(BaseCard==''){BaseCard=srcRank;return true;}
      else{return srcRank==BaseCard?true:false} 
	}
   //Not NONE, ANY or FIXED
   if(!(srcRank==tmpBase)){return false}
   }
else {//Target Pile not empty - apply build rule
     //Dont allow build on face down cards
    if(aPiles[TargetI][TargetCard].substr(2,1)==''){return false}

    tgtSuit=aPiles[TargetI][TargetCard].substr(1,1);
    tgtRank=aPiles[TargetI][TargetCard].substr(0,1);

   
   ar=GetAllowableRanks(tgtRank,tmpDir,tmpLast);
   if(ar.lastIndexOf(srcRank)<0){return false};
   as=GetAllowableSuits(tgtSuit,tmpMode);
   if(as.lastIndexOf(srcSuit)<0){return false};

   }


//******************DD


return ValTgt;




}

function DragStart(){
if((event.button==2)&&(NumDeals==-1)){
	clickdiv=event.srcElement.parentElement.id.substr(1);
	if (aPiles[clickdiv])
	   //   alert(aPiles[clickdiv].join("\n"));
	return;
	}
   
el=event.srcElement;
//Ignore event if el is not an image
//Note-don't use any images in doc apart from card imgs
if(!(el.tagName=='IMG')){return}

//Get SourceI - index of card to move
pel=el.parentElement;
SourceI=parseInt(pel.id.substr(1));
if(aBehaviours[SourceI].substr(0,1)=='V')
   {SourceCard=el.offsetTop/FANV;}
if(aBehaviours[SourceI].substr(0,1)=='H')
   {SourceCard=el.offsetLeft/FANH;}
if(aBehaviours[SourceI].substr(0,1)=='P')
   {SourceCard=aPiles[parseInt(pel.id.substr(1))].length-1;}

cards2drag=1;

if(el.draggable){
    if(!IsValidSource()){return;}

    InitLeft=event.clientX;
    InitTop=event.clientY;
    Dragging=true;
    //Align the floating DIV
    floater.style.pixelTop=el.style.pixelTop+el.parentElement.style.pixelTop;
    InitEltop=floater.style.pixelTop;
    floater.style.pixelLeft=el.style.pixelLeft+el.parentElement.style.pixelLeft;
    InitElleft=floater.style.pixelLeft;
    //Get Source elements image
    floater.children[0].src=el.src;
    floater.children[0].style.visibility='visible';
    el.style.visibility='hidden';
    //Get how may cards to move
    chels=el.parentElement.children;
    for(i=1;i<chels.length;i++){
      if((i>SourceCard)&&(!(chels[i].style.visibility=='hidden'))){
	    floater.children[cards2drag].src=chels[i].src;
	    floater.children[cards2drag].style.visibility='visible';
		//chels visibility
          chels[i].style.visibility='hidden';    
	    cards2drag++;
          }
      }
    //Finally move cards to temporary aDrag array
    for(i=0;i<cards2drag;i++)
	  aDrag[i]=aPiles[SourceI][SourceCard+i]
    aDrag.length=cards2drag;	     
    aPiles[SourceI].length-=cards2drag;    
    PaintCards(SourceI);        

    }
else //Not Draggable
  {cards2drag=0;}

}

function DragEnd(){
if(!Dragging){return;}
Dragging=false;
InitTop=0;
InitLeft=0;
InitEltop=0;
InitElleft=0;
for(i=0;i<floater.children.length;i++)
  {floater.children[i].style.visibility='hidden';}
if(cards2drag==0){return}
//Detect target
InsideArea=0;
TargetI=-1;

ftop=floater.style.pixelTop;
fleft=floater.style.pixelLeft;
for(i=0;i<zone.children.length;i++){
     LastVisCard=0;
     for(j=0;j<zone.children[i].children.length;j++)
       {if(!(zone.children[i].children[j].style.visibility=='hidden')){LastVisCard=j}}
    TopElem=zone.children[i].children[LastVisCard];
    tmpleft=zone.children[i].style.pixelLeft+TopElem.offsetLeft;
    tmptop=zone.children[i].style.pixelTop+TopElem.offsetTop;
    //Thanks to Stanley Rajasingh for this algorithm
    hOverlap=CWIDTH-Math.abs(fleft-tmpleft);
    vOverlap=CHEIGHT-Math.abs(ftop-tmptop); 
    if((hOverlap>0)&&(vOverlap>0)){
       tmpArea=hOverlap*vOverlap;
       if(tmpArea>InsideArea){InsideArea=tmpArea;TargetI=i;TargetCard=LastVisCard}
       } 

    }

// alert(SourceCard)

if(!IsValidTarget()){
    tmpTargetLength=aPiles[SourceI].length;
    for(i=0;i<cards2drag;i++){
      aPiles[SourceI].length++;
      aPiles[SourceI][tmpTargetLength]=aDrag[i];
      tmpTargetLength++;
	}
    
    PaintCards(SourceI);

   }
else{ //Valid move--Transfer cards
    InUndo=true;
    SaveUndo(SourceI,TargetI,true);
    tmpTargetLength=aPiles[TargetI].length;
    for(i=0;i<cards2drag;i++){
      aPiles[TargetI].length++;
      aPiles[TargetI][tmpTargetLength]=aDrag[i];
      tmpTargetLength++;
	}
    
    PaintCards(TargetI);
    AutoFlip(SourceI);
    PaintCards(SourceI);
    
    }

Check4Completion();

//alert(aPiles[SourceI].join(","))
//alert(aPiles[TargetI].join(","))

//*******************HMS Capture card values****************zxc
 
var y = aPiles[TargetI]
var x = TargetI
var z =aPiles[TargetI].join(",")
var t = z.substr(2)
var m = z.substr(0,1)
var stat = x
var value = ''

if(t=="U" && m != issame){;
l += m;
//c1=l.substring(0,1);
//c2=l.substring(1,2);
//c3=l.substring(2,3);
//c4=l.substring(3,4);
//c5=l.substring(4,5);
//c6=l.substring(5,6);
//c7=l.substring(6,7);
//c8=l.substring(7,8);
//c9=l.substring(8,9);
//c10=l.substring(9,10);
//c11=l.substring(10,11);
//c12=l.substring(11,12);



//alert(x+m+"-c1---"+ c1+"-c2---"+ c2)


if (x==1) {c1=m;DoEffect()}
else{
	if (x==2) {c2=m;DoEffect()}
	else{
		if (x==3) {c3=m;DoEffect()}
		else{
			if (x==4) {c4=m;DoEffect()}
			else{
				if (x==5) {c5=m;DoEffect()}
				else{
				}
			}
		}			
	}
}


results = l.indexOf(m);
//alert('l->'+l+' targetI->'+TargetI+' issame->' + issame+' results->'+results +'-c1-->'+c1+ '-c2-->'+c2+'-c3-->'+c3+'-c4-->'+c4');
//alert('-c1-->'+c1+ '-c2-->'+c2+'-c3-->'+c3+'-c4-->'+c4)

issame = m;

//if(c3 == ''){}

}


}



//if(TargetI) == 3){results='Fucked Up';}
//alert('I say it is '+ results)


function DoDrag(){
if(Dragging){
  floater.style.pixelLeft=event.clientX+InitElleft-InitLeft;
  floater.style.pixelTop=event.clientY+InitEltop-InitTop;
  return false;
  }

}

//The most horrible example of spaghetti coding :-) 
function PaintCards(pileno){
ImgStr="";
TargetDiv=eval("document.all.P"+pileno);
pilelength=aPiles[pileno].length;
numimages=TargetDiv.children.length;
freeimages=0;
pilestruct=aBehaviours[pileno].substr(0,1);
//alert(pilelength)
//***********Paint proc for packed piles (1 image)
if(pilestruct=='P'){ 
    TargetDiv.children[0].style.visibility='visible';
    //************* Which card to paint
    //if pile is Empty paint suitable marker 
    if(pilelength==0) //No cards in pile
       {TargetDiv.children[0].src="cards\\"+aBehaviours[pileno].substr(1)+".gif";  ///hms1
        TargetDiv.children[0].draggable=false; }
    else { //Pile not empty - paint top card(Last card in Array)
         if(aPiles[pileno][pilelength-1].substr(2)=='U')//Card faces up
		{TargetDiv.children[0].src="cards\\"+aPiles[pileno][pilelength-1].substr(0,2)+".gif"
		 TargetDiv.children[0].draggable=true;
             }	
         else //Card faces down   //HMS
	      {TargetDiv.children[0].src="cards\\b.gif";
             TargetDiv.children[0].draggable=true;}

	   }
    }

//*************Paint proc for fanned piles(indeterminate images)
else{
    //**PART I**********Adjust length of pile********************
    //***First find no of free and used images
    for(i=0;i<numimages;i++){
	if(TargetDiv.children[i].style.visibility=='hidden'){freeimages++;}
	} 
    usedimages=TargetDiv.children.length-freeimages; 
    //alert(freeimages+","+usedimages+","+pilelength)
    //***If Pile Length has increased
    if(pilelength>usedimages){
	//***Use available images, if any
      for(i=usedimages;i<TargetDiv.children.length;i++)
	   {TargetDiv.children[i].style.visibility='';if(i==pilelength-1){break}}
      //***Create new images
      if(pilelength>TargetDiv.children.length){
          for(i=TargetDiv.children.length;i<pilelength;i++){
		if(pilestruct=='V'){ImgStr+="<IMG style='position:absolute;top:"+FANV*i+"' WIDTH='"+CWIDTH+"' HEIGHT='"+CHEIGHT+"' SRC='cards\\b.gif'>"}
            else{ImgStr+="<IMG style='position:absolute;left:"+FANH*i+"' WIDTH='"+CWIDTH+"' HEIGHT='"+CHEIGHT+"' SRC='cards\\b.gif'>"}

		}
          TargetDiv.innerHTML+=ImgStr;
          }
	}
    //***If Pile Length has decreased
    else{
	  //***Hide images
        
	  for(i=pilelength;i<usedimages;i++)
   		{TargetDiv.children[i].style.visibility='hidden';}
	  }
    //**PART II**********Draw Cards****************************
    //***Draw suitable marker if pile empty
    if(pilelength==0){
        TargetDiv.children[0].src="cards\\"+aBehaviours[pileno].substr(1)+".gif";TargetDiv.children[0].style.visibility='visible';
        TargetDiv.children[0].draggable=false;}
    //***Draw appropriate card 
    for(i=0;i<pilelength;i++){
	if(aPiles[pileno][i].substr(2)=='U'){ //***Face Up
	     TargetDiv.children[i].src="cards\\"+aPiles[pileno][i].substr(0,2)+".gif";
	     TargetDiv.children[i].draggable=true;
           }
	  else{ //***Face Down
	     TargetDiv.children[i].src="cards\\b.gif";
	     TargetDiv.children[i].draggable=false;
		}

        }		
    }
}


function BuildPile(ddown,dup){
aPiles[CurrPile]=new Array();
ddown=parseInt(ddown);
dup=parseInt(dup);
i=0;
//***Deal Cards
while(i<ddown){
  aPiles[CurrPile][i]=aPiles[0][aPiles[0].length-1];
  aPiles[0].length--;i++;  
  }

while(i<(ddown+dup)){
  aPiles[CurrPile][i]=aPiles[0][aPiles[0].length-1]+"U";
  aPiles[0].length--;i++;  
  }
CurrPile++;
}


function BuildStock(stocktype){
StockIndex=0;
StartRank=0;
aPiles[0]=new Array();
//Provision for German Deck
if((stocktype==3)||(stocktype==4)){
  StartRank=6;
  }

// HMS ///Numbor of cards to random 22
for(i=0;i<1;i++){
   for(j=StartRank;j<22;j++){
      aPiles[0][StockIndex]=RANKS[j]+SUITS[i];
	StockIndex++;
      if((stocktype%2)==0){
         aPiles[0][StockIndex]=RANKS[j]+SUITS[i];
	   StockIndex++;
	   }
      }
   }



//Shuffle
DeckLen=aPiles[0].length;
for(i=0;i<DeckLen;i++){
  j=(Math.random()*100)%(DeckLen-1);
  j=(Math.round(j));
  temp=aPiles[0][i];
  aPiles[0][i]=aPiles[0][j];
  aPiles[0][j]=temp;
  }

}


//Read Game Data File and Initialize Game Layout

function LoadGame(){ 

LayoutStr="";
while(!Rules.recordset.EOF){
    divID=Rules.recordset.absolutePosition-1; 
    divX=Rules.recordset.Fields("PosX").value;
    divY=Rules.recordset.Fields("PosY").value;
    dtype=Rules.recordset.Fields("Type").value;
    ddown=Rules.recordset.Fields("DealDn").value;
    dup=Rules.recordset.Fields("DealUp").value;
    dstruct=Rules.recordset.Fields("Structure").value;
    dgif=Rules.recordset.Fields("EmptyGIF").value;


    aBehaviours[divID]=dstruct+dgif;
    aBuild[divID]=Rules.recordset.Fields("Build").value;
    aGroup[divID]=Rules.recordset.Fields("Grp").value;
    aAutoTgt[divID]=Rules.recordset.Fields("AutoTgt").value;
    aMaxLength[divID]=Rules.recordset.Fields("MaxLength").value;
    aPileNames[divID]=dtype;
		
    if(dtype=="D"){ //***Deck
	//***Build the deck based on deck type in DealDn field
	//***Deck type 1=Std Single,2=Std Double, 3=German Sing, 4=German Dbl
      BuildStock(ddown);
      NumDeals=1;SaveNumDeals=dup;
      CompletionURL=Rules.recordset.Fields("Build").value;
      }
    else{ //***Foundation or Pile
      //***Load the cards into F or P using DealUp and DealDn info
      BuildPile(ddown,dup);
	}
    LayoutStr+="<DIV ID='P"+divID+"' style='position:absolute;left:"+divX+";top:"+divY+"'>"+
    "<IMG style='position:absolute;zorder:0' WIDTH='"+CWIDTH+"' HEIGHT='"+CHEIGHT+"' SRC='cards\\trans.gif'>"+"</DIV>";
     //Src needs to be set because of a bug in IE4 which prevents dynamically
	//setting the source if original source is not provided.
    Rules.recordset.MoveNext();	
    }


if(document.HasSpecialDeal){DoSpecialDeal()};
zone.innerHTML=LayoutStr;
numpiles=Rules.recordset.recordCount;
for(i=0;i<numpiles;i++)
 {setTimeout("PaintCards("+i+");",10);}
 //{PaintCards(i);}//bug-wont work
//****Populate the Floating Div
LayoutStr="";
for(i=0;i<FLOATIMGS;i++)
   {LayoutStr+="<IMG style='position:absolute;visibility:hidden;top:"+FANV*i+"' WIDTH='"+CWIDTH+"' HEIGHT='"+CHEIGHT+"' SRC='cards\\trans.gif'>"}
floater.innerHTML=LayoutStr;
}

function DoEffect(){
//a place for get card values from conn ///hms


//See what the user has chosen for Position 1;

//alert(Nineth.children[0].innerHTML)


if(TargetI==1){
if(c1=="A") Bubble.children[0].innerHTML= "<B>1. "+ aText[0] + "</B>  "+ House[0];
if(c1=="B") Bubble.children[0].innerHTML= "<B>1. "+ aText[0] +"</B>   "+ House[1];
if(c1=="C") Bubble.children[0].innerHTML= "<B>1. "+ aText[0] +"</B>   "+ House[2];
if(c1=="D") Bubble.children[0].innerHTML= "<B>1. "+ aText[0] +"</B>   "+ House[3];
if(c1=="E") Bubble.children[0].innerHTML= "<B>1. "+aText[0] +"</B>   "+ House[4];
if(c1=="F") Bubble.children[0].innerHTML= "<B>1.  "+ aText[0]  +"</B>  "+ House[5];
if(c1=="G") Bubble.children[0].innerHTML= "<B>1.  "+ aText[0] +"</B> "+ House[6];
if(c1=="H") Bubble.children[0].innerHTML= "<B>1. "+ aText[0] +"</B>  "+ House[7];
if(c1=="I") Bubble.children[0].innerHTML= "<B>1. "+ aText[0] +"</B>  "+ House[8];
if(c1=="J") Bubble.children[0].innerHTML= "<B>1. "+aText[0] +"</B>  "+ House[9];
if(c1=="K") Bubble.children[0].innerHTML= "<B>1. "+aText[0] +"</B>  "+ House[10];
if(c1=="L") Bubble.children[0].innerHTML= "<B>1. "+aText[0]+"</B>  "+ House[11];
if(c1=="M") Bubble.children[0].innerHTML= "<B>1. "+ aText[0] +"</B>  "+ House[12];
if(c1=="N") Bubble.children[0].innerHTML= "<B>1. "+aText[0] +"</B>  "+ House[13];
if(c1=="O") Bubble.children[0].innerHTML= "<B>1. "+aText[0] +"</B>  "+ House[14];
if(c1=="P") Bubble.children[0].innerHTML= "<B>1. "+ aText[0]+"</B>  "+ House[15];
if(c1=="Q") Bubble.children[0].innerHTML= "<B>1. "+aText[0] +"</B>  "+ House[16];
if(c1=="R") Bubble.children[0].innerHTML= "<B>1. "+ aText[0]+"</B>  "+ House[17];
if(c1=="S") Bubble.children[0].innerHTML= "<B>1. "+aText[0] +"</B>  "+ House[18];
if(c1=="T") Bubble.children[0].innerHTML= "<B>1. "+ aText[0]+"</B>  "+ House[19];
if(c1=="U") Bubble.children[0].innerHTML= "<B>1. "+ aText[0] +"</B>  "+ House[20];
if(c1=="V") Bubble.children[0].innerHTML= "<B>1. "+aText[0]+"</B>  "+ House[21];
}

if(TargetI==2){
if(c2=="A") Second.children[0].innerHTML="<B>2. "+aText[1]+"</B>  "+ House[0];
if(c2=="B") Second.children[0].innerHTML="<B>2. "+aText[1] +"</B>  "+ House[1];
if(c2=="C") Second.children[0].innerHTML="<B>2. "+aText[1]+"</B>  "+ House[2];
if(c2=="D") Second.children[0].innerHTML="<B>2. "+ aText[1] +"</B>  "+ House[3];
if(c2=="E") Second.children[0].innerHTML="<B>2. "+ aText[1] +"</B>  "+ House[4];
if(c2=="F") Second.children[0].innerHTML="<B>2. "+ aText[1] +"</B>  "+ House[5];
if(c2=="G") Second.children[0].innerHTML="<B>2. "+ aText[1] +"</B>  "+ House[6];
if(c2=="H") Second.children[0].innerHTML="<B>2. "+aText[1] +"</B>  "+ House[7];
if(c2=="I") Second.children[0].innerHTML="<B>2. "+aText[1]+"</B>  "+ House[8];
if(c2=="J") Second.children[0].innerHTML="<B>2. "+aText[1]+"</B>  "+ House[9];
if(c2=="K") Second.children[0].innerHTML="<B>2. "+ aText[1] +"</B> "+ House[10];
if(c2=="L") Second.children[0].innerHTML="<B>2. "+ aText[1]+"</B> "+ House[11];
if(c2=="M") Second.children[0].innerHTML="<B>2. "+ aText[1]+"</B> "+ House[12];
if(c2=="N") Second.children[0].innerHTML="<B>2. "+aText[1] +"</B> "+ House[13];
if(c2=="O") Second.children[0].innerHTML="<B>2. "+ aText[1] +"</B> "+ House[14];
if(c2=="P") Second.children[0].innerHTML="<B>2. "+ aText[1] +"</B> "+ House[15];
if(c2=="Q") Second.children[0].innerHTML="<B>2. "+ aText[1] +"</B> "+ House[16];
if(c2=="R") Second.children[0].innerHTML="<B>2. "+ aText[1] +"</B> "+ House[17];
if(c2=="S") Second.children[0].innerHTML="<B>2. "+aText[1] +"</B> "+ House[18];
if(c2=="T") Second.children[0].innerHTML="<B>2. "+ aText[1] +"</B> "+ House[19];
if(c2=="U") Second.children[0].innerHTML="<B>2. "+ aText[1] +"</B> "+ House[20];
if(c2=="V") Second.children[0].innerHTML="<B>2. "+aText[1] +"</B> "+ House[21];
//alert(Second.children[0].innerHTML)
}

if(TargetI==3){
if(c3=="A") Third.children[0].innerHTML= "<B>3. "+ aText[2]  +"</B> "+ House[0];
if(c3=="B") Third.children[0].innerHTML="<B>3. "+ aText[2]  +"</B> "+ House[1];
if(c3=="C") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[2];
if(c3=="D") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[3];
if(c3=="E") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[4];
if(c3=="F") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[5];
if(c3=="G") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[6];
if(c3=="H") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[7];
if(c3=="I") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[8];
if(c3=="J") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[9];
if(c3=="K") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[10];
if(c3=="L") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[11];
if(c3=="M") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[12];
if(c3=="N") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[13];
if(c3=="O") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[14];
if(c3=="P") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[15];
if(c3=="Q") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[16];
if(c3=="R") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[17];
if(c3=="S") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[18];
if(c3=="T") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[19];
if(c3=="U") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[20];
if(c3=="V") Third.children[0].innerHTML="<B>3. "+ aText[2]+"</B> "+ House[21];
}

if(TargetI==4){
if(c4=="A") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[0];
if(c4=="B") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[1];
if(c4=="C") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[2];
if(c4=="D") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[3];
if(c4=="E") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[4];
if(c4=="F") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[5];
if(c4=="G") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[6];
if(c4=="H") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[7];
if(c4=="I") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[8];
if(c4=="J") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[9];
if(c4=="K") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[10];
if(c4=="L") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[11];
if(c4=="M") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[12];
if(c4=="N") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[13];
if(c4=="O") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[14];
if(c4=="P") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[15];
if(c4=="Q") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[16];
if(c4=="R") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[17];
if(c4=="S") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[18];
if(c4=="T") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[19];
if(c4=="U") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[20];
if(c4=="V") Fourth.children[0].innerHTML="<B>4. "+ aText[3] +"</B> "+ House[21];
}

if(TargetI==5){
if(c5=="A") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[0];
if(c5=="B") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[1];
if(c5=="C") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[2];
if(c5=="D") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[3];
if(c5=="E") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[4];
if(c5=="F") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[5];
if(c5=="G") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[6];
if(c5=="H") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[7];
if(c5=="I") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[8];
if(c5=="J") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[9];
if(c5=="K") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[10];
if(c5=="L") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[11];
if(c5=="M") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[12];
if(c5=="N") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[13];
if(c5=="O") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[14];
if(c5=="P") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[15];
if(c5=="Q") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[16];
if(c5=="R") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[17];
if(c5=="S") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[18];
if(c5=="T") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[19];
if(c5=="U") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[20];
if(c5=="V") Fifth.children[0].innerHTML="<B>5. "+ aText[4] +"</B> "+ House[21];
}




Bubble.style.pixelLeft = 115;
Bubble.style.pixelTop=185;
Bubble.style.pixelWidth=180;


Second.style.pixelLeft =515;
Second.style.pixelTop=185;
Second.style.pixelWidth=180;


Third.style.pixelLeft = 115;
Third.style.pixelTop=325;
Third.style.pixelWidth=180;

Fourth.style.pixelLeft = 515 ;
Fourth.style.pixelTop=325;
Fourth.style.pixelWidth=180;

Fifth.style.pixelLeft = 275 ;
Fifth.style.pixelTop=320;
Fifth.style.pixelWidth=210;



///box effect hms 

//if(Bubble.children[0].innerHTML=""){

//}

//if(readme.style.pixelTop<document.body.scrollTop) 
//readme.style.pixelTop=document.body.scrollTop;
//readme.style.visibility="visible";
//TheTimer=setTimeout("readme.style.visibility='hidden'",00000);

//alert("outof step")

}
function DoHow(){
divbox.style.visibility="visible";
TheTimer=setTimeout("divbox.style.visibility='hidden'",10000)
}



