var mtimer;
var col="#555566";
var old="";
var next="#444455";
var draw=0;
var ptimer;
var index2=0;

var pixSize2 = 183;
//var pix2 = new Array(183);
var width2 = 127;
var height2 = 5;
var pix2=[129,256,383,510,3,257,4,258,5,259,133,260,387,514,10,137,264,
	391,518,11,265,12,266,13,267,141,395,522,18,145,272,399,526,19,
	273,527,20,274,528,21,529,22,530,29,157,285,412,539,159,33,164,
	291,418,38,546,39,547,40,548,168,295,422,45,172,299,426,554,555,
	556,49,176,303,430,183,310,437,564,57,311,58,312,59,313,187,314,
	441,568,64,191,318,445,572,65,319,66,320,67,321,195,72,199,326,
	453,580,73,327,74,328,75,329,203,80,207,334,461,588,81,335,82,
	336,83,337,211,465,592,215,342,469,89,597,90,598,91,599,219,346,
	473,96,223,351,478,606,353,480,100,227,104,231,358,485,612,105,
	359,613,106,360,614,107,615,108,616,112,239,366,493,620,113,621,
	114,622,115,623,243,370,497,247,121,122,376,630,123,377,251,];

var pImage = [];
var salted;
//			var iImage = [];
var sharedStyleSheet;

var ua = navigator.userAgent;
var IE = /msie/i.test( ua );

function create() {
	
	document.getElementById("uname").focus();
	
	j = 0;
	table = document.getElementById("pPicture"); 
	table.style.tableLayout = "fixed";
	tbody = document.createElement("TBODY");
	table.insertBefore(tbody, null);
	for (y=0; y<height2; y++) {
		tr = document.createElement("TR");
		tbody.insertBefore(tr, null);
		for (x=0; x<width2; x++) {
			pImage[j++] = td = document.createElement("TD");
			td.pixel = true;
			td.className = "areyouapproved";
			tr.insertBefore(td, null);
		}
	}
	ptimer = setInterval('pixelUpdate2()',15);
	mtimer = setInterval('messageUpdate()', 3000);
}

function pixelUpdate2() {

	for (a=0; a<5; a++) {
		if(IE){
			pImage[pix2[index2]].runtimeStyle.backgroundColor=col;
		} else
		{
			pImage[pix2[index2]].style.backgroundColor=col;
		}
		index2++;
		if (index2>=pixSize2) {
			if (old=="#00ff00") {
				clearInterval(ptimer);
				
				//document.forms[0].password.value = hex_md5(salted + hex_md5(document.forms[0].password.value));
				//document.forms[0].submit();
				
				var address = document.location.href;
				if (address.search("enabler=logout")!=-1) {
					document.location.href = "index.php";
				} else {
					document.location.reload();
				}
			}
			index2 = 0;
			col = next;

			old = next;
			if (next == "#555566") {
				next = "#444455";
			} else {
				next = "#555566"
			}
		}
	}
}

function setColor(col) {

	next = col;
}			

var mess_index=0;
var temp_delay=0;
function messageUpdate() {
	
	clearInterval(mtimer);
	mtimer=setInterval('messageUpdate()', 50+Math.random()*100+temp_delay);
	temp_delay=0;
	
	parted = message.substring(0, mess_index+1);
	
	if (parted.charAt(mess_index)=="%") {
		mess_index++;
	}
	
	// Skip tags
	if (parted.charAt(mess_index)=="<") {
		while (parted.charAt(mess_index)!=">") {
		
			mess_index++;
			parted = message.substring(0, mess_index+1);
		}
	}

	var total=parted;
	if (mess_index==message.length) {
		
		clearInterval(mtimer);
		total = message;
	}

	t2 = "";
	for (a=0; a<total.length; a++) {
	
		if (total.charAt(a)=='%') {
		
			t2 += "<br>";
		} else {
			t2 += total.charAt(a);
		}
	}

	document.getElementById('message').innerHTML = t2; 
	
	mess_index++;
	if (message.charAt(mess_index)=="%") {
		temp_delay=500;
	}
}

function tryLogin(salt) {
	
	salted = salt;
	
	var uname = document.getElementById('uname').value;
	
	// md5( salt + md5(pw));	
	var pword = hex_md5(salt + hex_md5(document.getElementById('pword').value));
	
	callServer("elfs/login_try.php?username="+uname+"&password=" + pword);
	
	return false;
}


