// mickweb© 2003

String.prototype.colour = function (c){
return '<span style=" color:'+c+'">'+this+'</span>';
}


// Shuffle the entries in an Array
Array.prototype.shuffle= function(times){
var i,j,t,l=this.length;
while(times--){
with(Math){i=floor(random()*l);j=floor(random()*l);}
t=this[i];this[i]=this[j];this[j]=t;
}
return this;
}

// Extract index (if any) from an Array of a given value
Array.prototype.getIndex=function(val){
for(i=0;i<this.length;i++){
if(this[i]==val) return i;
}
return -1;
}

// Create deck of cards "A"
S=["&hearts;","&diams;","&clubs;","&spades;"];
N=[2,3,4,5,6,7,8,9,10,"J","Q","K","A"];
A= new Array();
for(j=0;j<S.length;j++){
for(i=0;i<N.length;i++){
A[A.length]=S[j]+ " "+N[i];
}
}

B=A.shuffle(300);// A hefty shuffle!!!
p1=B.slice(0,13);// First hand
p2=B.slice(13,26);
p3=B.slice(26,39);
p4=B.slice(39);

function numSort(a,b){
// Compare the card numbers, Ace high, deuce low 
// Parse the value of the card
// Sort according to its index in the Array N
return N.getIndex(b.substring((b.indexOf(" "))+1)) - N.getIndex(a.substring((a.indexOf(" "))+1));
}

function sortHand(array){
//sort the 13 cards
var d=new Array(),s=new Array(),h=new Array(),c=new Array();
// Create an array for each suit
for(i=0;i<array.length;i++){
// Check the second character in each array entry, "h","s","d" or "c"
k=eval(array[i].charAt(1));
// Convert the string to an Array object reference (h,s,d or c)
k[k.length]=array[i];
// add the card to the appropriate Array
}
hands = [
h[0]?h.sort(numSort).join(" ").red():"&hearts;".red(), 
s[0]?s.sort(numSort).join(" "):"&spades;",
d[0]?d.sort(numSort).join(" ").red():"&diams;".red(), 
c[0]?c.sort(numSort).join(" "):"&clubs;"
];
// Sort each of the four suits, if the suit is void, display only the suit.
return hands.join("<br>")
}