//CHANGES TO MAKE ON FINAL
//disable highlighting text/images
//disable right-click
//replace placeholder graphics

//chess rules to add
//castling and en passe
//fix pawn movement

//Ideas to Simplify Code
//can combind clickrules(a) and yourpiece(a)

//Instead of updating board every second, get the opponents last move, while its their turn. 
//When the move is different then the one stored on your computer
//your board will update THEIR pieces (or just change the one piece that moved)

//SECURITY PROBLEMS (JAVASCRIPT INJECTION)
//javascript:document.getElementById("63").src="http://wartex8.com/chess/wsq/wqueen.png"


//Initial Variables
pieceselected='';
playercolor='w';
if(playercolor=='w'){opponentcolor="b";}
else{opponentcolor="w";}
parray=[];
altarray=[];
blockcolor=[];

///////////////////////////////////////
//SET BOARD (height and then width)
///////////////////////////////////////

function setpieces(){
//Set White
document.getElementById("71").src="bsq/wpawn.png";document.getElementById("71").alt="pawn";
document.getElementById("72").src="wsq/wpawn.png";document.getElementById("72").alt="pawn";
document.getElementById("73").src="bsq/wpawn.png";document.getElementById("73").alt="pawn";
document.getElementById("74").src="wsq/wpawn.png";document.getElementById("74").alt="pawn";
document.getElementById("75").src="bsq/wpawn.png";document.getElementById("75").alt="pawn";
document.getElementById("76").src="wsq/wpawn.png";document.getElementById("76").alt="pawn";
document.getElementById("77").src="bsq/wpawn.png";document.getElementById("77").alt="pawn";
document.getElementById("78").src="wsq/wpawn.png";document.getElementById("78").alt="pawn";

document.getElementById("81").src="wsq/wrook.png";  document.getElementById("81").alt="rook";
document.getElementById("82").src="bsq/wknight.png";document.getElementById("82").alt="knight";
document.getElementById("83").src="wsq/wbishop.png";document.getElementById("83").alt="bishop";
document.getElementById("84").src="bsq/wking.png";  document.getElementById("84").alt="king";
document.getElementById("85").src="wsq/wqueen.png"; document.getElementById("85").alt="queen";
document.getElementById("86").src="bsq/wbishop.png";document.getElementById("86").alt="bishop";
document.getElementById("87").src="wsq/wknight.png";document.getElementById("87").alt="knight";
document.getElementById("88").src="bsq/wrook.png";  document.getElementById("88").alt="rook";

//Set Black
document.getElementById("21").src="wsq/bpawn.png";document.getElementById("21").alt="pawn";
document.getElementById("22").src="bsq/bpawn.png";document.getElementById("22").alt="pawn";
document.getElementById("23").src="wsq/bpawn.png";document.getElementById("23").alt="pawn";
document.getElementById("24").src="bsq/bpawn.png";document.getElementById("24").alt="pawn";
document.getElementById("25").src="wsq/bpawn.png";document.getElementById("25").alt="pawn";
document.getElementById("26").src="bsq/bpawn.png";document.getElementById("26").alt="pawn";
document.getElementById("27").src="wsq/bpawn.png";document.getElementById("27").alt="pawn";
document.getElementById("28").src="bsq/bpawn.png";document.getElementById("28").alt="pawn";

document.getElementById("11").src="bsq/brook.png";  document.getElementById("11").alt="rook";
document.getElementById("12").src="wsq/bknight.png";document.getElementById("12").alt="knight";
document.getElementById("13").src="bsq/bbishop.png";document.getElementById("13").alt="bishop";
document.getElementById("14").src="wsq/bking.png";  document.getElementById("14").alt="king";
document.getElementById("15").src="bsq/bqueen.png"; document.getElementById("15").alt="queen";
document.getElementById("16").src="wsq/bbishop.png";document.getElementById("16").alt="bishop";
document.getElementById("17").src="bsq/bknight.png";document.getElementById("17").alt="knight";
document.getElementById("18").src="wsq/brook.png";  document.getElementById("18").alt="rook";
}

///////////////////////////////////////
//MOVE PIECE (Second Click)
///////////////////////////////////////

function movepiece(a){
//cannot move into check




///////////////
//REWRITE code so POSSIBLEMOVE.PNG is under the psq folder. Then run clickrules(a) function and compair to "psq"
///////////////

clickrules(a);

if(pieceselected !='' && !yourpiece(a) && (document.getElementById(a).src == "http://wartex8.com/chess/psq/possiblemove.png" || foldertest == "psq")){
resetpossiblemove();
piecetaken=a; ////////////////Make sure this is not reset
document.getElementById(a).src= boardcolor(a) + "sq/" + playercolor + document.getElementById(piece_to_move).alt + ".png";  document.getElementById(a).alt=document.getElementById(piece_to_move).alt;
document.getElementById(piece_to_move).src= boardcolor(piece_to_move) + ".png"; document.getElementById(piece_to_move).alt="nothing";

pieceselected='';
document.getElementById('info').innerHTML = "Second Click: " + a;
resetpossiblemove();
resetpossiblecapture();
}

///////////////////////////////////////
//SET PIECE (First Click-this is inside a function)
///////////////////////////////////////

else if(document.getElementById(a).src != "http://wartex8.com/chess/b.png" && document.getElementById(a).src != "http://wartex8.com/chess/w.png" && yourpiece(a)){

haspiecemoved(); //this will test rooks and king to determine if castling is an option

resetpossiblecapture();
piece_to_move=a;
pieceselected=document.getElementById(a).alt;

if(pieceselected != ''){resetpossiblemove();} //causing problems ---- maybe can remove if piece to move is selected

findpossiblemoves(a,pieceselected);
document.getElementById('info').innerHTML = "First Click: " + a;

}
}

///////////////////////////////////////
//Click Rules
///////////////////////////////////////
function clickrules(a){
colortest = document.getElementById(a).src.split("/");
foldertest = colortest[colortest.length-2];
//colortest = colortest[colortest.length-1];

//if(colortest=="b.png" || colortest=="w.png"){color='na';}
//else{color=colortest.substring(0, 1);}
}

///////////////////////////////////////
//Give piece information
///////////////////////////////////////
function findpossiblemoves(location,piece){
document.getElementById('pieceinfo').innerHTML = piece + " at " + location;

if(piece=='pawn'){pawn(location);}
else if(piece=='rook'){rook(location);}
else if(piece=='knight'){knight(location);}
else if(piece=='bishop'){bishop(location);}
else if(piece=='king'){king(location);}
else if(piece=='queen'){queen(location);}
}


///////////////////////////////////////
//Check if it's your piece
///////////////////////////////////////
function yourpiece(a){
colortest = document.getElementById(a).src.split("/");
foldertest = colortest[colortest.length-2];
colortest = colortest[colortest.length-1];

if(colortest.substring(0, 1) == playercolor && colortest != "w.png"){return true}
else{return false}
}

///////////////////////////////////////
//Get Piece Location
///////////////////////////////////////

function getlocation(x){
loc=x + '';
loc=loc.split('');
h=loc[0];
l=loc[1];
//altarray=[]; //clear array so you can re-add the new one
//parray=[]; //clear array so you can re-add the new one
//blockcolor=[]; //clear array so you can re-add the new one
}

///////////////////////////////////////
//On Game Board?
///////////////////////////////////////

function ongameboard(){
return !(l<1 || h<1 || l>8 || h>8)
}

///////////////////////////////////////
//What color is the board?
///////////////////////////////////////

function boardcolor(x){
loc=x + '';
loc=loc.split('');
result=loc[0]/1 + loc[1]/1;

if(iseven(result)){return "b"}
else if(!iseven(result)){return "w"}
}

///////////////////////////////////////
//Has a rook or king moved?
///////////////////////////////////////

function haspiecemoved(){

//test castling
//81 rook
//88 rook
//84 king

//11 rook
//18 rook
//14 king

}

///////////////////////////////////////
//Reset Possible Moves (purple squares)
///////////////////////////////////////

function resetpossiblemove(){
parraylength=parray.length;

while(parraylength>0){
deletethissrc=(parray[parraylength-1]);

if(altarray[parraylength-1] == "nothing"){
if(iseven(blockcolor[parraylength-1]) && piece_to_move!=deletethissrc){document.getElementById(deletethissrc).src="b.png";}
else if(!iseven(blockcolor[parraylength-1]) && piece_to_move!=deletethissrc){document.getElementById(deletethissrc).src="w.png";}
}

else{
//set the piece to the correct graphic
}
--parraylength;
}
parray=[];
altarray=[];
blockcolor=[];
}

///////////////////////////////////////
//Reset Possible Capture
///////////////////////////////////////
function resetpossiblecapture(){
firstvar=1;
secondvar=1;
markedarray=[];

while(firstvar<9){
piecelocation=firstvar+ '' + secondvar; //works

ulength = document.getElementById(piecelocation).src.split("/");
ulengthn = ulength.length;
ulength = ulength[ulengthn-2];

if(ulength == "psq"){
editu = document.getElementById(piecelocation).src.split("psq");
editu = editu[0] + boardcolor(piecelocation) + "sq" + editu[1];
document.getElementById(piecelocation).src = editu;

document.getElementById('debug').innerHTML = editu;

}
secondvar++;
if(secondvar == "9"){secondvar=1;firstvar++;}
}
}