
var Tb="div";
var Kb="span";
var theForm;
var theField;
var theDiv;
var theDummyIFDiv;

var ea=1;
var Ma=1;

var usernamew; // will be filled from hidden iframe...

function iTS(frm,fld_username)
{
	theForm=frm;
	theField=fld_username;

	theField.autocomplete="off";

	theDiv=document.createElement("DIV");
	theDiv.id="completeDiv";
	theDiv.style.borderRight="black "+ea+"px solid";
	theDiv.style.borderLeft="black "+ea+"px solid";
	theDiv.style.borderTop="black "+Ma+"px solid";
	theDiv.style.borderBottom="black "+Ma+"px solid";
	theDiv.style.zIndex="10";
	theDiv.style.paddingRight="2";
	theDiv.style.paddingLeft="2";
	theDiv.style.paddingTop="2";
	theDiv.style.paddingBottom="2";
	da();
	theDiv.style.visibility="hidden";
	theDiv.style.position="absolute";
	theDiv.style.backgroundColor="white";
	theDiv.style.fontFamily="Geneva, Verdana, sans-serif";
	theDiv.style.fontSize="10px";
	theDiv.style.textAlign="justify";
	document.body.appendChild(theDiv);

	theField.onblur=hideTheDiv;
	theField.onfocus=fiOF;
	theField.onkeyup=suggestUsernames;

	//dummy for IE zIndex problem
	theDummyIFDiv=document.createElement("IFRAME");
	theDummyIFDiv.id="dummy";
	theDummyIFDiv.src="usernamesuggest_ifr_hidden.php";
	theDummyIFDiv.scrolling="no";
	theDummyIFDiv.frameborder="0";
	theDummyIFDiv.style.position="absolute";
	theDummyIFDiv.style.left="0";
	theDummyIFDiv.style.top=="0";
	theDummyIFDiv.style.visibility="hidden";
	document.body.appendChild(theDummyIFDiv);

	window.onresize=da;
}
function fiOF()
{
	theField.select();
	updateUsernamesDiv();
	//showTheDiv();
}
function hideTheDiv(){
	theDummyIFDiv.style.visibility="hidden"
	theDiv.style.visibility="hidden"
}
function showTheDiv(){
	var wi;
	var he;
	da();

	if(navigator&&navigator.userAgent.toLowerCase().indexOf("msie")==-1){
		 wi=theDiv.offsetWidth-ea*2;
		 he=theDiv.offsetHeight-ea*2;
	}else{
		wi=theDiv.offsetWidth;
		he=theDiv.offsetHeight;
	}
	theDummyIFDiv.style.width = wi;
    theDummyIFDiv.style.height = he;
    theDummyIFDiv.style.top = theDiv.style.top;
    theDummyIFDiv.style.left = theDiv.style.left;
    theDummyIFDiv.style.zIndex = theDiv.style.zIndex - 1;
	theDummyIFDiv.style.visibility="visible";

	theDiv.style.visibility="visible";
}
function zb(s){
	return kb(s,"offsetLeft")
}
function kb(s,na){
	var wb=0;
	while(s){
		wb+=s[na];
		s=s.offsetParent
	}
	return wb
}
function Yb(s){
	return kb(s,"offsetTop")
}
function da(){
	if(theDiv){
		theDiv.style.left=zb(theField)+"px";
		theDiv.style.top=Yb(theField)+theField.offsetHeight-1+"px";
		theDiv.style.width=Ta()+"px";
		theDiv.style.zIndex=10;
	}
}
function Ta(){
	if(navigator&&navigator.userAgent.toLowerCase().indexOf("msie")==-1){
		return theField.offsetWidth-ea*2
	}else{
		return theField.offsetWidth
	}
}

///////////////////

var currUsernameSel=-1;


function createUsernamesList()
{
	currUsernameSel=-1;
	//alert("createUsernameList: count="+usernames.length);
}

function updateUsernamesDiv()
{
	var sugg=usernames;
	var tmp;
	var at='';
	at='<table cellspacing="0" cellpadding="0" width="100%" class="info">';
	for(var i=0;i<sugg.length;i++){
		if(i==currUsernameSel){
			tmp="<tr span style='background-color: #999999;'><td style='color: #ffffff;'>"+sugg[i]+"</td></tr>";
		}else{
			tmp="<tr onMouseOver='currUsernameSel="+i+"; selectFromUsernameSuggestionlist(); updateUsernamesDiv();'><td>" + sugg[i]+"</td></tr>";
		}
		at+=tmp;
	}
	at+='</table>';

	document.getElementById('completeDiv').innerHTML=at;
	if(sugg.length){
		da();
		showTheDiv();
	}else{
		hideTheDiv();
	}
}
function suggestUsernames(ev)
{
	//
	if(!ev){
		ev=window.event;
	}
	var key=ev.keyCode;
	if(key==38){
		if(currUsernameSel>0){
			currUsernameSel-=1;
		}else{
			currUsernameSel=usernames.length -1;
		}
		updateUsernamesDiv();
		selectFromUsernameSuggestionlist();
		return;
	}else if(key==40){
		if(currUsernameSel<(usernames.length-1)){
			currUsernameSel+=1;
		}else{
			currUsernameSel=0;
		}
		updateUsernamesDiv();
		selectFromUsernameSuggestionlist();
		return;
	}
	var val=theField.value;
	//new
	tellEggFrame('dummy', 'usernamesuggest_ifr_hidden.php?like='+val+"&update=1");
}
function selectFromUsernameSuggestionlist()
{
	if(isdefined(usernames[currUsernameSel])){
		theField.value=usernames[currUsernameSel];
	}
}
function isdefined(v)
{
    return (typeof(v) == "undefined")?  false: true;
}


