// JavaScript Document
var p_AjaxID
var CurrentSel = 0;
var LastSel = 0;
var MinLenght = 3;

function ShowPerson(inFN,inEN,inID,inType){
	var box = document.getElementById(inID);
	var fornavn = document.getElementById(inFN);
	//var eftirnavn = document.getElementById(inEN);
	

		box.style.left = findPosX(fornavn);
		box.style.top = findPosY(fornavn) + 17;
		box.style.visibility = 'visible';
		box.style.height = '1px';
		
		//var theURL = 'includes/Personar.asp?t=' + inType + '&f=' + fornavn.value + '&e=' + eftirnavn.value + '&inFN=' + inFN + '&inEN=' + inEN + '&inID=' + inID
		var theURL = 'includes/Personar.asp?t=' + inType + '&f=' + fornavn.value + '&inFN=' + inFN + '&inID=' + inID
		
		//alert(theURL);
		p_AjaxGet(theURL,inID,'leitar...');
	
}
function HidePerson(inFN,inEN,inID,inX){
	if(inFN!==0){
		var box = document.getElementById(inID);
		var fornavn = document.getElementById(inFN);
		var eftirnavn = document.getElementById(inFN + '_sel');
		box.style.visibility = 'hidden';
		box.style.height = '1px';
	}
	else
	{
		var box = document.getElementById(inID);
		box.style.visibility = 'hidden';
		box.style.height = '1px';
	}
	if (inX!=0){
		fornavn.value = document.getElementById('tempPerson_' + inX).FNval;
		fornavn.style.fontStyle = 'normal';
		fornavn.style.fontWeight = 'bold';
		eftirnavn.value = document.getElementById('tempPerson_' + inX).IDval;
		CurrentSel=0;
	}	
}
var p_AjaxID;

function p_AjaxGet(theurl,theid,tempText)
	{
	p_AjaxID = theid
	document.getElementById(theid).innerHTML = tempText;
	if (theurl.length > 0)
	{ 
		if (Right(theurl,4) == ".asp")
			{theurl=theurl + "?sid=" + Math.random()}
		else
			{theurl=theurl + "&sid=" + Math.random()}
		var url=theurl
		
		xmlHttp=GetXmlHttpObject(p_AjaxStateChanged)
		xmlHttp.open("GET", url , true)
		xmlHttp.send(null)
	} 
	else
	{ 
		document.getElementById(theid).innerHTML="ein feilur er hendur...";
	} 
}
function p_AjaxStateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		var temp = xmlHttp.responseText;
		document.getElementById(p_AjaxID).innerHTML = temp;
		if (InStr(temp,'Onki') > -1)
		{
			document.getElementById(p_AjaxID).style.visibility = 'hidden';
			CurrentSel=0;
		}
		else
		{
			document.getElementById(p_AjaxID).style.height = '1px';
			SelectClass('tempPerson_' + LastSel,'tempPerson_' + CurrentSel,'row0b');
		}
	} 
}

function setBoxHeight(theString){
	var z;
	for (z=0; z < 5; z++)
	{
	    if (InStr(theString, '<!--[' + z + ']-->') > -1){
			return z;
		}
	}
	return z;
}

function testKey(fornavn,eftirnavn,person_box,theType,e){
	if(theType=='3')
		{theLenght = 1}
	else
		{theLenght = MinLenght}
	
	var theKey = -1;
	var inBox = document.getElementById(person_box);
	var inFN = document.getElementById(fornavn);
	//var inEN = document.getElementById(eftirnavn);
	
	if(Len(inFN.value)<theLenght){
		inBox.style.visibility = 'hidden';
		inBox.style.height = '1px';
		CurrentSel=0;
		return false;
	}
	
	if (window.event)
		theKey = window.event.keyCode;
	else if (e)
		theKey = e.which;
	
	//alert(theKey);
	
	if(theKey==37){return false;}
	else if (theKey==39){return false;}
	else if (theKey==16){return false;}
	else if (theKey==17){return false;}
	else if (theKey==9){return false;}
	else if (theKey==38){
		//alert('up');
		LastSel = CurrentSel;
		CurrentSel--;
		if(elementExists('tempPerson_' + CurrentSel) == false){CurrentSel++;}
		SelectClass('tempPerson_' + LastSel,'tempPerson_' + CurrentSel,'row0b');
		return false;
	}
	else if (theKey==40){
		//alert('down');
		LastSel = CurrentSel;
		CurrentSel++;
		if(elementExists('tempPerson_' + CurrentSel) == false){CurrentSel--;}
		SelectClass('tempPerson_' + LastSel,'tempPerson_' + CurrentSel,'row0b');
		return false;
	}
	else if (theKey==13){
		if(CurrentSel>0){
			//alert(CurrentSel + ' selected');
			HidePerson(fornavn,eftirnavn,person_box,CurrentSel)
		}
		return false;
	}
	document.getElementById(fornavn + '_sel').value = 0;
	inFN.style.fontStyle = 'italic';
	inFN.style.fontWeight = 'normal';
	//alert(fornavn + ',' + eftirnavn);
	CurrentSel=1;
	ShowPerson(fornavn,eftirnavn,person_box,theType);
	
}

function SelectClass(preID,theID,theClass){
	if(elementExists(preID) != false){document.getElementById(preID).className='';}
	if(elementExists(theID) != false){document.getElementById(theID).className=theClass;}
}
