var g_from;
var g_to;
var g_from_x;
var g_from_y;
var g_to_x;
var g_to_y;
var g_from_old_src;
var g_from_new_src;
var g_alt;
var g_old_alt;
var g_to_old_src;
var g_is_clicked = false;
var g_can_move = true;
var g_is_castling = false;
var g_rook_from;
var g_rook_to;

function changeCursor(square)
{
	if( document.all == true){square.style.cursor = "hand";}
	else{square.style.cursor = "pointer";}
}

function can_move()
{
	return g_can_move;
}
function is_allowed_to()
{
	var index = g_map[g_from_x*8+g_from_y];
	if( index>=0 )
	{
		for( i=0; i<g_allowed_to[index].length; i+=2)
		{
			if( g_allowed_to[index][i]==g_to_x && g_allowed_to[index][i+1]==g_to_y)
				return true;
		}
	}
	return false;
}
function get_bold_img(img)
{
	slen = img.length;
	img2 = img.substring(0,slen-4) + "2.gif";
	return img2;
}
function board_from_change()
{
	var from,row,col;
	from = document.getElementById("from_txt_id").value;
	document.getElementById("from_txt_id").value=from.toUpperCase();
	col = ltn(from.charAt(0).toUpperCase());
	row = from.charAt(1);
	if( col == -1 || (row<1||row>8))
	{
		document.getElementById("from_txt_id").value = "";
	
	}
	else
	{
		row = 8-row;
		Clicked(row,col);
	}
}
function board_to_change()
{
	var to,row,col;
	to = document.getElementById("to_txt_id").value;
	document.getElementById("from_txt_id").value=to.toUpperCase();
	col = ltn(to.charAt(0).toUpperCase());
	row = to.charAt(1);
	if( col == -1 || (row<1||row>8))
		document.getElementById("to_txt_id").value = "";
	else
	{
		
		row = 8-row;
		Clicked(row,col);
	}
}

function Clicked(x,y)
{ 
	if( !g_can_move )
	{
		alert("you already made a move, if you want to cancel your move you can click \"Cancel\" button.");
		return;
	}
	if( !g_is_clicked)
	{
		var index = g_map[x*8+y]
		if( index>=0 )
		{
			g_from = "s" + x + y;
			g_from_x = x;
			g_from_y = y;
			g_is_clicked = true;
			g_from_old_src = document.images[g_from].src;
			g_from_new_src = get_bold_img(g_from_old_src);
			document.images[g_from].src = g_from_new_src;
			document.getElementById("from_txt_id").value =  ntl(g_from_y)+(8-g_from_x);
		}
	}
	else
	{
		g_to = "s" + x + y;
		g_to_x = x;
		g_to_y = y;
		//if clicked the same piece, reset the img
		if( g_to == g_from)
		{
			g_is_clicked = false;
			g_is_castling = false;
			document.images[g_from].src = g_from_old_src;
			document.images[g_from].alt = g_alt;
			document.move_form.submit.disabled = true;
			document.getElementById("from_txt_id").value =  "";
			return;
		}
		if( is_allowed_to() )
		{
			g_to_old_src = document.images[g_to].src;
			
			document.images[g_to].src = g_from_new_src;
			document.images[g_from].src = "pics/"+pics_sub_dir+"empty.gif";
			g_alt = document.images[g_from].alt;
			g_old_alt = document.images[g_to].alt;
			document.images[g_to].alt = g_alt;
			document.images[g_from].alt = "";
			document.getElementById("to_txt_id").value =  ntl(g_to_y)+(8-g_to_x);

			if( g_from_x == kx && g_from_y == ky)
			{
				var rook_from_y;
				var rook_to_y;
				if( g_to_y == ky+2)
				{
					g_is_castling = true;
					rook_from_y = 7;
					rook_to_y = 5;
				}
				if( g_to_y == ky-2)
				{
					g_is_castling = true;
					rook_from_y = 0;
					rook_to_y = 3;
				}
				if( g_is_castling )
				{
					g_rook_from = "s" + kx + rook_from_y;
					g_rook_to = "s" + kx + rook_to_y;
					document.images[g_rook_to].src = document.images[g_rook_from].src;
					document.images[g_rook_from].src = "pics/"+pics_sub_dir+"empty.gif";
				}
			}
			document.move_form.move.value = g_from_x + "," + g_from_y + "," + g_to_x + "," + g_to_y;
			document.move_form.submit.disabled = false;
			g_can_move = false;
		}
	}	
}
function Reset()
{

	if( !g_can_move )
	{
		document.images[g_from].src = g_from_old_src;
		document.images[g_to].src = g_to_old_src;
		document.images[g_from].alt = g_alt;
		document.images[g_to].alt = g_old_alt;
		document.getElementById("from_txt_id").value =  "";
		document.getElementById("to_txt_id").value =  "";
		if( g_is_castling )
		{
			document.images[g_rook_from].src = document.images[g_rook_to].src;
			document.images[g_rook_to].src = "pics/"+pics_sub_dir+"empty.gif";
			g_is_castling = false;
		}
		g_can_move = true;
		g_is_clicked = false;
	}
	else if( g_is_clicked )
	{
		document.images[g_from].src = g_from_old_src;
		g_can_move = true;
		g_is_clicked = false;
	}
	document.move_form.submit.disabled = true;
}

function ntl(n)
{
	switch(n){case 0: return 'A'; case 1: return 'B'; case 2: return 'C'; case 3: return 'D'; case 4: return 'E'; case 5: return 'F'; case 6: return 'G'; case 7: return 'H';}
}
function ltn(n)
{
	switch(n){ case 'A': return 0; case 'B': return 1; case 'C': return 2;	case 'D': return 3;	case 'E': return 4;	case 'F': return 5;	case 'G': return 6;	case 'H': return 7; default: return -1;}
}


