function getHex(dec){
    var hexArray = new Array( "0", "1", "2", "3", 
                              "4", "5", "6", "7",
                              "8", "9", "A", "B", 
                              "C", "D", "E", "F" );
    return hexArray[dec - (Math.floor(dec / 16)) * 16];
} 

function dec2hex(dec){
    var hexCode = new Array(), i = 0, hex = "";
    while(dec > 15){
		hexCode[i] = getHex(dec);
		dec = Math.floor(dec / 16);
		i += 1;
    }
    hexCode[i] = getHex(dec);
    i = hexCode.length;
    while(i--){
        hex += hexCode[i];
    }
    return hex;
}

function hex2dec(hex){
	return parseInt(hex,16);
}

function doDec2hex(){
	document.getElementById("dec2hexhex").value = "0x"+ dec2hex(document.getElementById("dec2hexdec").value);
}

function doHex2dec(){
	document.getElementById("dec2hexdec").value = hex2dec(document.getElementById("dec2hexhex").value);
}
function make(){
	var i,j,h = '<table cellpadding="0" cellspacing="0">'
	for(i=0; i<8; i++){
		h += '<tr>'
		for(j=0; j<5; j++){
			h += '<td id="i'+ i +'j'+ j +'" onmouseover="this.style.cursor=\'pointer\'" onmouseout="this.style.cursor=\'\'" onclick="a('+ i +', '+ j +');upd();">&nbsp;</td>'
		}
		h += '</tr>'
	}
	h += '</table>'
	$id('do').innerHTML = h;
	upd();
}
function a(i, j){
	var id = $id('i'+ i +'j'+ j)
	if(id.style.backgroundColor == "gray") id.style.backgroundColor = "white"
	else id.style.backgroundColor = "gray"	
}
function clr(){
	for(var i=0; i<8; i++)
		for(var j=0; j<5; j++)
			$id('i'+ i +'j'+ j).style.backgroundColor = "white"
	upd();
}
function set(){
	for(var i=0; i<8; i++)
		for(var j=0; j<5; j++)
			$id('i'+ i +'j'+ j).style.backgroundColor = "gray"
	upd();
}
function inv(){
	for(var i=0; i<8; i++)
		for(var j=0; j<5; j++)
			a(i, j)
	upd();
}
function getValBox(i, j){
	return ($id('i'+ i +'j'+ j).style.backgroundColor == "gray")
}
function getVal(i){
	var j,k,r=0;
	for(j=0, k=4; j<5; j++, k--)
		r += (getValBox(i, j)) ? (1<<k) : 0;	
	return r
}
function upd(){
	var t,h = "unsigned char __attribute__ ((progmem)) lcdCC[] = {\r\n\t"
	for(var i=0; i<8; i++){
		t=getVal(i)
		h += '0x'+ ((t<16) ? '0' : '') + dec2hex(t)
		if(i<7) h += ", "
		else h += "\r\n"
	}
	h += "};\r\n"
	$id('tt').innerHTML = h
}
function sel(){
	$id('tt').select();
}

