function wd_ClearPalette() {
	var theF=document.forms["form_tplpalette"];
	if (theF) {
		var oIdEl=theF.elements["tpl_plt_code"];
		var oRepresCell=document.getElementById("tpl_plt_rgb");
		if (oIdEl&&oRepresCell) {
			for (iI=10; iI<28; iI++) {
				var sCellId="_%c"+iI+"%";
				oIdEl.value=sCellId;
				oRepresCell.bgColor="#F8F9FA";
				amgSetEditedColor();
			}
		}
	}
	//
	var arrPalette=new Array();
	wd_SetMixedPalette(arrPalette);
}
function wd_SetMixedPalette(arrMixedPalette) {
	try {
		if (typeof(arrMixedPalette)=="undefined") {
			var iDate=wd_TimeUTC();
			var arrPalette=amg_ModalDialog('mkplt.php?clrqstart='+iDate, 640, 400, 'wd_pltmixer', 0);
		} else {
			var arrPalette=arrMixedPalette;
		}
	} catch (theError) {
		//alert("wd_SetMixedPalette error: "+theError.description);
	}
	//
	if (arrPalette) {
		var iI=0;
		var theF=document.forms["form_tplpalette"];
		if (theF) {
			var oIdEl=theF.elements["tpl_plt_code"];
			var oRepresCell=document.getElementById("tpl_plt_rgb");
			if (oIdEl&&oRepresCell) {
				var iRowStarter=28;
				if (arrPalette['_0']&&arrPalette['_0'][0]&&arrPalette['_0'][0]!="#F8F9FA") {
					for (iI=0; iI<9; iI++) {
						var sCellId="_%c"+(iRowStarter+iI)+"%";
						oIdEl.value=sCellId;
						oRepresCell.bgColor=arrPalette['_0'][iI];
						amgSetEditedColor();
					}
				} else {
					for (iI=0; iI<9; iI++) {
						var sCellId="_%c"+(iRowStarter+iI)+"%";
						oIdEl.value=sCellId;
						oRepresCell.bgColor="#F8F9FA";
						amgSetEditedColor();
					}
				}
				if (arrPalette['_1']&&arrPalette['_1'][0]&&arrPalette['_1'][0]!="#F8F9FA") {
					iRowStarter=37;
					for (iI=0; iI<9; iI++) {
						var sCellId="_%c"+(iRowStarter+iI)+"%";
						oIdEl.value=sCellId;
						oRepresCell.bgColor=arrPalette['_1'][iI];
						amgSetEditedColor();
					}
					iRowStarter=46;
				} else {
					iRowStarter=55;
					for (iI=0; iI<9; iI++) {
						var sCellId="_%c"+(iRowStarter+iI)+"%";
						oIdEl.value=sCellId;
						oRepresCell.bgColor="#F8F9FA";
						amgSetEditedColor();
					}
					iRowStarter=37;
				}
				if (arrPalette['_2']&&arrPalette['_2'][0]&&arrPalette['_2'][0]!="#F8F9FA") {
					for (iI=0; iI<9; iI++) {
						var sCellId="_%c"+(iRowStarter+iI)+"%";
						oIdEl.value=sCellId;
						oRepresCell.bgColor=arrPalette['_2'][iI];
						amgSetEditedColor();
					}
				} else {
					for (iI=0; iI<9; iI++) {
						var sCellId="_%c"+(iRowStarter+iI)+"%";
						oIdEl.value=sCellId;
						oRepresCell.bgColor="#F8F9FA";
						amgSetEditedColor();
					}
				}
				iRowStarter+=9;
				if (arrPalette['_3']&&arrPalette['_3'][0]&&arrPalette['_3'][0]!="#F8F9FA") {
					for (iI=0; iI<9; iI++) {
						var sCellId="_%c"+(iRowStarter+iI)+"%";
						oIdEl.value=sCellId;
						oRepresCell.bgColor=arrPalette['_3'][iI];
						amgSetEditedColor();
					}
				} else {
					for (iI=0; iI<9; iI++) {
						var sCellId="_%c"+(iRowStarter+iI)+"%";
						oIdEl.value=sCellId;
						oRepresCell.bgColor="#F8F9FA";
						amgSetEditedColor();
					}
				}
			}
		}
	}
}

function amgOnColorCompScroll(sLetter) {
	var iAnswer=1;
	//
	if (!sLetter) sLetter="r";
	sLetter=sLetter.toLowerCase();
	if (!(sLetter=="r"||sLetter=="g"||sLetter=="b")) sLetter="r";
	//
	var theF=document.forms['form_tplpalette'];
	var theDv=document.getElementById('tpl_plt_'+sLetter+'_dv');
	//
	var theDvHl=document.getElementById('tpl_plt_'+sLetter+'_hl');
	if (theF&&theDv) {
		var theDecEl=theF.elements['tpl_plt_'+sLetter+'_dec'];
		var theHexEl=theF.elements['tpl_plt_'+sLetter+'_hex'];
		//
		var sDvVal=theDv.scrollLeft;
		var iDvVal=parseInt(parseInt(sDvVal)/10);
		//
		if (iDvVal>255) iDvVal=255;
		if (iDvVal<0) iDvVal=0;
		var iRevVal=255-iDvVal;
		var hDvVal=iDvVal.toString(16).toUpperCase();
		while (hDvVal.length<2) hDvVal='0'+hDvVal;
		var hRevVal=iRevVal.toString(16).toUpperCase();
		while (hRevVal.length<2) hRevVal='0'+hRevVal;
		if (theDecEl) theDecEl.value=iDvVal;
		if (theHexEl) theHexEl.value=hDvVal;
		var sRevValue="";
		if (theDvHl) {
			switch (sLetter) {
				case "r":
					sRevValue+='#'+'FF'+hRevVal+hRevVal;
					break;
				case "g":
					sRevValue+='#'+hRevVal+'FF'+hRevVal;
					break;
				case "b":
					sRevValue+='#'+hRevVal+hRevVal+'FF';
					break;
				default:
					break;
			}
			theDvHl.style.backgroundColor=sRevValue;
		}
		amgSetEditingColor();
	}
	return true;
}

function amgSetColorToEdit(sCellId) {
	if (document.getElementById("wd_plt_optionstbl")) {
		document.getElementById("wd_plt_optionstbl").disabled=false;
		document.getElementById("wd_plt_optionstbl").style.display='block';
	}
	var oCell=document.getElementById(sCellId);
	var oTable=document.getElementById("tpl_plt_table");
	if (oCell && oTable) {
		var iI=0;
		for (iI=0; iI<oTable.cells.length; iI++) {
			var theCell=oTable.cells.item(iI);
			if (theCell.id&&theCell.id==sCellId) {
				theCell.style.borderColor="#000000";
			} else if (theCell.id && theCell.id.substr(0, 2)=="_%") {
				theCell.style.borderColor="#F0F0F0";
			}
		}
		var sColor=oCell.bgColor;
		sColor=sColor.toUpperCase();
		if (sColor.substr(0,1)=="#") sColor=sColor.substr(1);
		if (sColor.length==6) {
			if (sColor=="F8F9FA") {
				var sR=""; var sG=""; var sB="";
			} else {
				var sR=sColor.substr(0,2);
				var sG=sColor.substr(2,2);
				var sB=sColor.substr(4,2);
			}
			var oRepresCell=document.getElementById("tpl_plt_rgb");
			if (oRepresCell) {
				oRepresCell.bgColor=sColor;
				var oNumEl=document.getElementById("tpl_plt_currnumber");
				if (oNumEl) {
					var sNum=sCellId.replace(/[^\d]/ig, "");
					oNumEl.innerText=" #"+sNum;
				}
				var theF=document.forms["form_tplpalette"];
				if (theF) {
					var oIdEl=theF.elements["tpl_plt_code"];
					if (oIdEl) oIdEl.value=sCellId;
					//
					var oSelRGB=theF.elements["tpl_plt_rgb_hex"];
					oSelRGB.value=sR+sG+sB;
					oSelRGB.click();
				}
			}
		}
	}
}

function amgOnPltDecClick(theObj, chrColor) {
	var sVal=theObj.value;
	sVal=sVal.replace(/[^0-9]/ig, '');
	theObj.value=sVal;
	if (sVal.length) {
		var iVal=parseInt(sVal);
		if (iVal>255) iVal=255;
		if (iVal<0) iVal=0;
		theObj.value=iVal;
		iVal=iVal*10;
		var theDv=document.getElementById('tpl_plt_'+chrColor+'_dv');
		if (theDv) theDv.scrollLeft=iVal;
	} else {
		var theHl=document.getElementById('tpl_plt_'+chrColor+'_hl');
		if (theHl) theHl.style.backgroundColor='#F8F9FA';
		amgSetEditingColor();
	}
}
function amgOnPltHexClick(theObj, chrColor) {
	//
}
function amgOnPltPrcClick(theObj, chrColor) {
	var sVal=theObj.value;
	sVal=sVal.replace(/[^0-9]/ig, '');
	var iVal=parseInt(sVal, 10);
	if (!iVal) iVal=0;
	if (iVal>100) iVal=100;
	if (iVal<0) iVal=0;
	if (theObj.value!=iVal.toString()) theObj.value=iVal.toString();
	iVal=iVal*25.5;
	var theDv=document.getElementById('tpl_plt_'+chrColor+'_dv');
	if (theDv) theDv.scrollLeft=iVal;
}

function amgSetEditingColor() {
	var oRepresCell=document.getElementById("tpl_plt_rgb");
	var oRepresTxt=document.getElementById("tpl_plt_rgb_hex");
	var theF=document.forms["form_tplpalette"];
	if (theF) {
		var oSelR=theF.elements["tpl_plt_r_hex"];
		var oSelG=theF.elements["tpl_plt_g_hex"];
		var oSelB=theF.elements["tpl_plt_b_hex"];
		var oSelRD=theF.elements["tpl_plt_r_dec"];
		var oSelGD=theF.elements["tpl_plt_g_dec"];
		var oSelBD=theF.elements["tpl_plt_b_dec"];
		var oSelRP=theF.elements["tpl_plt_r_prc"];
		var oSelGP=theF.elements["tpl_plt_g_prc"];
		var oSelBP=theF.elements["tpl_plt_b_prc"];
		if (oSelR && oSelG && oSelB && oSelRD && oSelGD && oSelBD) {
			var sR=oSelR.value; if (!sR.length) sR="00";
			var sRD=oSelRD.value; if (!sRD.length) sRD="0";
			var sG=oSelG.value; if (!sG.length) sG="00";
			var sGD=oSelGD.value; if (!sGD.length) sGD="0";
			var sB=oSelB.value; if (!sB.length) sB="00";
			var sBD=oSelBD.value; if (!sBD.length) sBD="0";
			if (oRepresTxt) oRepresTxt.value=sR+sG+sB;
			oSelR.value=sR; oSelG.value=sG; oSelB.value=sB;
			if (sR.length) {
				oSelRD.value=parseInt(sR, 16);
			} else {
				oSelRD.value="";
			}
			if (sG.length) {
				oSelGD.value=parseInt(sG, 16);
			} else {
				oSelGD.value="";
			}
			if (sB.length) {
				oSelBD.value=parseInt(sB, 16);
			} else {
				oSelBD.value="";
			}
			if (!(oSelRD.value.length && oSelGD.value.length && oSelBD.value.length)) {
				if (oRepresCell) oRepresCell.bgColor="#F8F9FA";
			} else {
				if (oRepresCell) oRepresCell.bgColor="#"+sR+sG+sB;
			}
			if (oSelRP) oSelRP.value=Math.round(parseInt(oSelRD.value, 10)/2.55);
			if (oSelGP) oSelGP.value=Math.round(parseInt(oSelGD.value, 10)/2.55);
			if (oSelBP) oSelBP.value=Math.round(parseInt(oSelBD.value, 10)/2.55);
		}
	}
}

function amgSetEditedColor() {
	var theF=document.forms["form_tplpalette"];
	if (theF) {
		var oIdEl=theF.elements["tpl_plt_code"];
		if (oIdEl) {
			var sCellId=oIdEl.value;
			if (sCellId.length&&sCellId.substr(0,2)=="_%") {
				var oRepresCell=document.getElementById("tpl_plt_rgb");
				if (oRepresCell) {
					var oCell=document.getElementById(sCellId);
					if (oCell) oCell.bgColor=oRepresCell.bgColor;
				}
			}
		}
	}
}

function amgSetEditedPalette() {
	var theF=document.forms["form_tplpalette"];
	if (theF) {
		var oPalette=theF.elements["tpls_newpalette"];
		var oTable=document.getElementById("tpl_plt_table");
		if (oPalette&&oTable) {
			oPalette.value="";
			var iI=0;
			for (iI=0; iI<oTable.cells.length; iI++) {
				var theCell=oTable.cells.item(iI);
				if (theCell.id&&theCell.id.substr(0, 2)=="_%") {
					oPalette.value+="\n"+theCell.id+"="+theCell.bgColor;
				}
			}
		}
	}
	return true;
}