var arrColors;

function convert_to_hex(num) {
	var digit = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
	var n1 = Math.floor(num / 16);
	var n2 = num - n1 * 16;
	var retstr = digit[n1];
	retstr += digit[n2];
	return (retstr);
}

function validateColor(valHex) {
	var reg1=/^(\#[0-9A-F]{6}|\#[0-9A-F]{3})$/i;
	if(reg1.test(valHex)==false){
		alert ("You must enter a valid color code."); 
		return false;
	}
	return true;
}

function GetColor() {
	var hi = document.getElementById("hex-input");
	hi.value = hi.value.toUpperCase();
	var HexValue = hi.value;

	if (validateColor(HexValue) == false) {
		return;
	}
	
	r.setValue(parseInt(HexValue.substring(1,3),16));
	g.setValue(parseInt(HexValue.substring(3,5),16));
	b.setValue(parseInt(HexValue.substring(5,7),16));
}

function SetColor(valClrArea) {
	var valColor = document.getElementById("color-picker-out").firstChild.data;
	var objCell = document.getElementById(valClrArea);
	objCell.style.backgroundColor = valColor;
}

function AddCombination() {
	var valClrBack = document.getElementById("clrBack").style.backgroundColor;
	var valClrNoise = document.getElementById("clrNoise").style.backgroundColor;
	var valClrText = document.getElementById("clrText").style.backgroundColor;
	var objTable = document.getElementById("combination-table");
	
	//kombinasyon yapmış mı
	if ((valClrBack == '') || (valClrNoise == '') || (valClrText == '')) {
		alert("You must set background, noise and text color.");
		return;
	}

	//kombinasyon varmı kontrol et
	if (objTable.rows.length > 1) {
		for (var i=1; i<objTable.rows.length; i++) {
			if ((valClrBack == objTable.rows.item(i).cells.item(0).style.backgroundColor) && (valClrNoise == objTable.rows.item(i).cells.item(1).style.backgroundColor) && (valClrText == objTable.rows.item(i).cells.item(2).style.backgroundColor)) {
				alert("This combination already exists.");
				return;
			}
		}
	}

	//kombinasyon ekle
	var objRow = objTable.insertRow(objTable.rows.length);
	var objCell = objRow.insertCell(0);
	objCell.style.backgroundColor = valClrBack;
	var objCell = objRow.insertCell(1);
	objCell.style.backgroundColor = valClrNoise;	
	var objCell = objRow.insertCell(2);
	objCell.style.backgroundColor = valClrText;
	var objCell = objRow.insertCell(3);
	objCell.align = "left";
	objCell.id = (objTable.rows.length-1);
	objCell.innerHTML = '<input name="BtnDelete" type="button" class="style2" id="BtnDelete" onclick="RemoveCombination(' + (objTable.rows.length-1) + ')" value="Delete" />';	
}

function RemoveCombination(valIndex) {
	var objTable = document.getElementById("combination-table");
	for (var i=1; i<objTable.rows.length; i++) {
		if (objTable.rows.item(i).cells.item(3).id == valIndex) {
			objTable.deleteRow(i);
			break;
		}
	}
}

function AddColor(valColor) {
	for (var i in arrColors) {
		if (arrColors[i] == valColor) {
			return convert_to_hex(i);
		} 
	}
	
	if (arrColors.length > 256) {
		alert("256 different colors limit exceeded. Please delete some combination.");
		return "XX";
	} else {
		return convert_to_hex(arrColors.push(valColor)-1);
	}
}

function GenerateColorMap() {
	var objTable = document.getElementById("combination-table");
	if (objTable.rows.length > 1) {
		var i, mapRow, tmpHex, colorMap;
		arrColors = new Array();
		var arrColorMap = new Array();
		 //liste temizleniyor
		for (i=1; i<objTable.rows.length; i++) {
			mapRow = "";
			var valClrBack = objTable.rows.item(i).cells.item(0).style.backgroundColor;
			tmpHex = AddColor(valClrBack);
			if (tmpHex != "XX") {
				mapRow = tmpHex;
			} else {
				continue;
			}		

			var valClrNoise = objTable.rows.item(i).cells.item(1).style.backgroundColor;
			tmpHex = AddColor(valClrNoise);
			if (tmpHex != "XX") {
				mapRow = mapRow + "," + tmpHex;
			} else {
				continue;
			}
			
			var valClrText = objTable.rows.item(i).cells.item(2).style.backgroundColor;
			tmpHex = AddColor(valClrText);
			if (tmpHex != "XX") {
				mapRow = mapRow + "," + tmpHex;
			} else {
				continue;
			}
			
			arrColorMap.push(mapRow);
		}
		
		//renk varsa harita yapılıyor
		if (arrColorMap.length > 0) {
			var tmpColor = "";
			for (i in arrColorMap) {
				arrColorMap[i] = 'split("' + arrColorMap[i] + '",",") ';
				
				if (i < (arrColorMap.length-1)) {
					arrColorMap[i] += ",_";
				} else {
					arrColorMap[i] += "_";
				}
			}
			
			for (i in arrColors) {
				tmpColor = arrColors[i];
				if (tmpColor.charAt(0) == "#") {
					//IE
					tmpColor = tmpColor.substring(5,7) + tmpColor.substring(3,5) + tmpColor.substring(1,3);
				} else {
					//rgb(128, 128, 128)
					//firefox
					tmpColor = tmpColor.substring(4,tmpColor.length-1);
					tmpColor = tmpColor.split(',');
					tmpColor = convert_to_hex(tmpColor[2]) + convert_to_hex(tmpColor[1]) + convert_to_hex(tmpColor[0]);
				}
				arrColors[i] = tmpColor + "00";
			}
			
			var objMemo = document.getElementById("const-out");
			objMemo.value = "'#Begin ColorMap\nconst BmpColorMap = \"" + arrColors.join("") + "\"\n\nColorMap = Array(_\n" + arrColorMap.join("\n") + "\n)'End ColorMap";
		} 
	} else {
		alert("Color combination could not find.");
		return;	
	}
}