var COLOR = Array("#000000","#FF5555","#5555FF","#55FF55","#EEEE55","#FFCA25","#55FFFF","#FF55FF","#FF0000","#FFFFFF");
/*****************************************************************************/

function FIELD()
{
	this.width = 0;
	this.height = 0;
	this.left = 0;
	this.top = 0;
	this.right = 0;
	this.bottom = 0;
	this.block_width = 0;
	this.block_height = 0;
	this.block_pad = 0;
	this.frame_pad = 0;
	this.frame_col = '#FFFFFF';
	this.grid_col = '#000000'
	this.data = undefined;
	this.view = undefined;
	this.display = undefined;
	this.image = undefined;
	this.outerframe = undefined;
	this.innerframe = undefined;
	this.messageframeborder = undefined;
	this.messageframe = undefined;
	this.inputboxouterframe = undefined; 
	this.inputboxinnerframe = undefined;
}

/*****************************************************************************/

FIELD.prototype.plotframe = function plotframe(msgbox,sidebar)
{
	var mfh = this.block_height+3;
	var t = (this.top-this.frame_pad);
	var l = (this.left-this.frame_pad)-1;
	var w = (this.right+this.frame_pad)-l;
	var h = (this.bottom+this.frame_pad)-t;
	temp = document.createElement("DIV");
	this.outerframe = temp;
	temp.style.top = t;
	temp.style.left = l;
	temp.style.height = h;
	temp.style.width = w;
	temp.style.position = 'absolute';
	temp.style.background = this.frame_col;
	temp.style.overflow = 'hidden';
	PlaceHolder.appendChild(temp);
	temp = document.createElement("DIV");
	this.innerframe = temp;
	temp.style.top = (t+1)
	temp.style.left = (l+1);
	temp.style.height = (h-2);
	temp.style.width = (w-2);
	temp.style.position = 'absolute';
	temp.style.background = this.grid_col;
	temp.style.overflow = 'hidden';
	PlaceHolder.appendChild(temp);
	if(msgbox)
	{
		temp = document.createElement("DIV");
		this.messageframeborder = temp;
		temp.style.top = (t-msgbox);
		temp.style.left = (l+this.block+100);
		temp.style.height = (msgbox-1);
		temp.style.width = (w);
		temp.style.position = 'absolute';
		temp.style.background = this.frame_col;
		temp.style.overflow = 'hidden';
		PlaceHolder.appendChild(temp);
		temp = document.createElement("DIV");
		this.messageframe = temp;
		temp.style.top = ((t-msgbox+1))
		temp.style.left = (l+1);
		temp.style.height = (msgbox-3);
		temp.style.width = (w-2);
		temp.style.position = 'absolute';
		temp.style.background = '#000000';
		temp.style.overflow = 'hidden';
		PlaceHolder.appendChild(temp);
	}
	if(sidebar)
	{
		temp = document.createElement("DIV");
		this.messageframeborder = temp;
		temp.style.top = (this.top-1);
		temp.style.left = (this.right+2);
		temp.style.height = (h);
		temp.style.width = (sidebar);
		temp.style.position = 'absolute';
		temp.style.background = this.frame_col;
		temp.style.overflow = 'hidden';
		PlaceHolder.appendChild(temp);
		temp = document.createElement("DIV");
		this.messageframe = temp;
		temp.style.top = (this.top);
		temp.style.left = (this.right+3);
		temp.style.height = (h-2);
		temp.style.width = (sidebar-2);
		temp.style.position = 'absolute';
		temp.style.background = '#000000';
		temp.style.overflow = 'hidden';
		PlaceHolder.appendChild(temp);
	}
}

/*****************************************************************************/

FIELD.prototype.createfield = function createfield(FieldCode)
{
	var x,y,temp,timg;
	for(y=0;y<this.height;y++)
		for(x=0;x<this.width;x++)
		{
			temp = document.createElement("SPAN");
			temp.style.top = (((this.block_pad+this.block_height)*y)+this.top);
			temp.style.left = (((this.block_pad+this.block_width)*x)+this.left);
			temp.style.height = this.block_height;
			temp.style.width = this.block_width;
			temp.style.position = 'absolute';
			temp.style.background = '#000000';
			temp.style.overflow = 'hidden';
			if(this.editable)
			{
				if(is_ie)
	 				temp.attachEvent('onclick',OnBlockClick);
				else
		      temp.onclick = OnBlockClick; 
			}
			PlaceHolder.appendChild(this.display[x+(y*this.width)] = temp);
			timg = document.createElement("IMG");
			timg.src = PIC_USER.src;
			temp.appendChild(this.image[x+(y*this.width)] = timg);
		}
}

/*****************************************************************************/

FIELD.prototype.plotfield = function plotfield()
{
	var x,y;
	for(y=0;y<this.height;y++)
		for(x=0;x<this.width;x++)
			if(this.view[x+y*this.width] != this.data[x+y*this.width])
				this.putsquare(x, y, this.view[x+y*this.width]=this.data[x+y*this.width]);
}

/*****************************************************************************/

FIELD.prototype.pluckfield = function pluckfield()
{
	var x,y;
	for(y=0;y<this.height;y++)
		for(x=0;x<this.width;x++)
			if((!!this.getsquare(x,y)) == (!this.view[x+y*this.width]))
				this.data[x+y*this.width] = 8*this.getsquare(x,y);
}

/*****************************************************************************/

FIELD.prototype.putsquare = function putsquare(x, y, color)
{
	this.display[x+(y*this.width)].style.background = COLOR[color];
}

/*****************************************************************************/

FIELD.prototype.getsquare = function getsquare(x, y)
{
	return this.display[x+(y*this.width)].style.background!='#000000';
}


/*****************************************************************************/

FIELD.prototype.freefield = function freefield()
{
	this.data = undefined;
	this.view = undefined;
	this.display = undefined;
	this.width=NULL;
	this.height=NULL;
}

/*****************************************************************************/

FIELD.prototype.makefield = function makefield(width,height)
{
	this.data = new Array(height*width);
	this.view = new Array(height*width);
	this.display = new Array(height*width);
	this.image = new Array(height*width);
	this.width = width;
	this.height = height;
}

/*****************************************************************************/

FIELD.prototype.initfield = function initfield(left,top,block_width,block_height,block_pad,frame_pad)
{
	var i;
	this.left  = left;
	this.top   = top;
	this.right = left+(block_width+block_pad)*this.width-block_pad;
	this.bottom= top+(block_height+block_pad)*this.height-block_pad;
	this.block_width  = block_width;
	this.block_height = block_height;
	this.frame_pad = frame_pad;
	this.block_pad = block_pad;
	this.frame_col = "#AAAAAA";
	this.grid_col  = "#303030";
	for(i=0;i<this.height*this.width;i++) {
		this.data[i]=0;
		this.view[i]=0;
	}
}

/*****************************************************************************/

FIELD.prototype.wipefield = function wipefield()
{
	var i;
	for(i=0;i<this.height*this.width;i++) {
		this.data[i]=0;
	}
}

/*****************************************************************************/

function gametimer()
{
	return GeneralTimer;
}

/*****************************************************************************/

FIELD.prototype.createinputbox = function createinputbox(boxwidth,boxheight)
{
	var mfh = this.block_height+3;
	var t = (this.top-this.frame_pad);
	var l = (this.left-this.frame_pad)-1;
	var w = (this.right+this.frame_pad)-l;
	var h = (this.bottom+this.frame_pad)-t;
	var borderwidth = w - boxwidth;
	var borderheight = h - boxheight;
	var it = t + borderheight/2;
	var ih = h - borderheight;
	var il = l + borderwidth/2;
	var iw = w - borderwidth;
	temp = document.createElement("DIV");
	this.inputboxouterframe = temp;
	temp.style.top = it;
	temp.style.left = il;
	temp.style.height = ih;
	temp.style.width = iw;
	temp.style.position = 'absolute';
	temp.style.background = this.frame_col;
	temp.style.overflow = 'hidden';
	this.inputboxouterframe = PlaceHolder.appendChild(temp);
	temp = document.createElement("DIV");
	this.inputboxinnerframe = temp;
	temp.style.top = 1;
	temp.style.left = 1;
	temp.style.height = (ih-2);
	temp.style.width = (iw-2);
	temp.style.position = 'relative';
	temp.style.background = this.grid_col;
	temp.style.overflow = 'hidden';
	this.inputboxinnerframe = this.inputboxouterframe.appendChild(temp);
}

/*****************************************************************************/

