var color = new Object();

color.intoclor = function (fieldName) {
	if (fieldName) this.fieldName = fieldName;
	this.ColorHex=new Array('00','33','66','99','CC','FF');
	this.SpColorHex=new Array('FF0000','00FF00','0000FF','FFFF00','00FFFF','FF00FF');
	this.current=null;

	var colorTable='';
	for (i=0;i<2;i++) {
		for (j=0;j<6;j++) {
			colorTable=colorTable+'<tr height=12>';
			colorTable=colorTable+'<td width=11 style="background-color:#000000">';
			if (i==0) {
				colorTable=colorTable+'<td width=11 style="background-color:#'+this.ColorHex[j]+this.ColorHex[j]+this.ColorHex[j]+'">';
			} else {
				colorTable=colorTable+'<td width=11 style="background-color:#'+this.SpColorHex[j]+'">';
			}
			colorTable=colorTable+'<td width=11 style="background-color:#000000">';
			for (k=0;k<3;k++) {
				for (l=0;l<6;l++) {
					colorTable=colorTable+'<td width=11 style="background-color:#'+this.ColorHex[k+i*3]+this.ColorHex[l]+this.ColorHex[j]+'">';
				}
			}
		}
	}
	colorTable='<table width=253 border="0" cellspacing="0" cellpadding="0" style="border:1px #000000 solid;border-bottom:none;border-collapse: collapse" bordercolor="000000">'
	+'<tr height=30><td colspan=21 bgcolor=#cccccc>'
	+'<table cellpadding="0" cellspacing="1" border="0" style="border-collapse: collapse">'
	+'<tr><td width="3"><td><input type="text" id="DisColor" name="DisColor" size="6" disabled style="border:solid 1px #000000;background-color:#ffff00"></td>'
	+'<td width="3"><td><input type="text" id="HexColor" name="HexColor" size="7" style="border:inset 1px;font-family:Arial;" value="#000000"></td></tr></table></td></table>'
	+'<table width=253 border="1" cellspacing="0" cellpadding="0" style="border-collapse: collapse" bordercolor="000000" onmouseover="return color.doOver(event);" onmouseout="return color.doOut(event);" onclick="return color.doclick(event);" style="cursor:hand;">'
	+colorTable+'</table>';
	this.fieldName.innerHTML=colorTable;
};

color.doOver = function (e) {
	try {
		this.elem = window.event.srcElement;
	}
	catch(E) {
		this.elem=e.target
	}

	if ((this.elem.tagName=="TD") && (this.current!=this.elem)) {
		//if (this.current!=null) {
		//	this.current.style.backgroundColor = this.current._background;
		//}
		this.elem._background = this.elem.style.backgroundColor;
		if (this.elem._background.indexOf("rgb") >= 0) {
			tmpBGC = this.elem._background;
			tmpBGC = tmpBGC.replace(/rgb\(/g, '');
			tmpBGC = tmpBGC.replace(/\)/g, '');
			tmpBGCAr = tmpBGC.split(',');
			this.elem._background = "#"+color.RGBtoHex(tmpBGCAr[0], tmpBGCAr[1], tmpBGCAr[2]);
		}
		DisColor.style.backgroundColor = this.elem.style.backgroundColor;
		HexColor.value = this.elem._background;
		//event.srcElement.style.backgroundColor = "white";
		this.current = this.elem;
	}
};

color.doOut = function () {
	//if (this.current!=null) this.current.style.backgroundColor = this.current._background;
};

color.doclick = function (e) {
	try {
		this.elem = window.event.srcElement;
	}
	catch(E) {
		this.elem=e.target
	}

	if (this.elem.tagName=="TD"){
		if (this.timer) clearTimeout(this.timer);
		this.highPanelSet();
		this.inputField.value = this.elem._background;
	}
};

color.showPanel = function (curr, inputField, offset) {
	if (inputField) this.inputField = inputField;
	if (this.timer) clearTimeout(this.timer);
	
	var pos = XMLHttp.getObjectPos(curr);
	this.fieldName.style.left = parseInt(pos[0]+offset[0])+"px";
	this.fieldName.style.top = parseInt(pos[1]+offset[1])+"px";
	this.fieldName.style.display = "block";
};

color.highPanel = function () {
	if (this.timer) clearTimeout(this.timer);
	this.timer = setTimeout("color.highPanelSet()", 500);
};

color.highPanelSet = function () {
	this.fieldName.style.left = 0+"px";
	this.fieldName.style.top = 0+"px";
	this.fieldName.style.display = "none";
};

color.highPanelClear = function () {
	if (this.timer) clearTimeout(this.timer);
};

color.RGBtoHex = function (R,G,B) {
	return color.toHex(R)+color.toHex(G)+color.toHex(B);
}

color.toHex = function (N) {
 if (N==null) return "00";
 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
 return "0123456789ABCDEF".charAt((N-N%16)/16)
      + "0123456789ABCDEF".charAt(N%16);
}