var agt=navigator.userAgent.toLowerCase();

var listlength = 17;
var inputinit = new Array();
var respage = 0;
var mark = 0;
var hit = false;
var hittexts = new Array();
var hitvalues = new Array();
var hitinputs = new Array();
var cityhit = false;
var aitahit = false;
var countryhit = false;
var airporthit = false;
var inputstr = "";
var shiftmark = false;

function showlist(divid){
	document.getElementById(divid).style.visibility = "visible";
/*
	if (agt.indexOf("msie 5.")!=-1 || agt.indexOf("msie 6.")!=-1){
		document.getElementById("minie7hide1").style.visibility = "hidden";
		document.getElementById("minie7hide2").style.visibility = "hidden";
		document.getElementById("minie7hide3").style.visibility = "hidden";
		document.getElementById("minie7hide4").style.visibility = "hidden";
		document.getElementById("minie7hide5").style.visibility = "hidden";
		document.getElementById("minie7hide6").style.visibility = "hidden";
	}
*/
}

function hidelist(divid){
	document.getElementById(divid).style.visibility = "hidden";
/*
	if (agt.indexOf("msie 5.")!=-1 || agt.indexOf("msie 6.")!=-1){
		document.getElementById("minie7hide1").style.visibility = "visible";
		document.getElementById("minie7hide2").style.visibility = "visible";
		document.getElementById("minie7hide3").style.visibility = "visible";
		document.getElementById("minie7hide4").style.visibility = "visible";
		document.getElementById("minie7hide5").style.visibility = "visible";
		document.getElementById("minie7hide6").style.visibility = "visible";
	}
*/
}

function noenter(e){
	if (e.which == 13 || e.keyCode == 13){
  	return false;
	}
}

function firstupper(str){
	if (str=="usa"){
		return "USA";
	} else {
		return str.substr(0,1).toUpperCase()+str.substr(1,str.length).toLowerCase();
	}
}

function testhit(compvalue,testvalue){
	if(testvalue.substring(0,compvalue.length).toLowerCase() == compvalue.toLowerCase()){
		hit = true;
		testvalue = firstupper(testvalue);
		return "<b>"+testvalue.substring(0,compvalue.length)+"</b>"+testvalue.substring(compvalue.length,testvalue.length);
	} else {
		return false;
	}
}

function drawdiv(divid,inputname,submitinput){
	selectedstr = "";
	var startitem = respage*listlength;
	if (respage){
		selectedstr += '<div onmousedown="respage--; drawdiv(\''+divid+'\',\''+inputname+'\',\''+submitinput+'\');" onmouseover="pageshift(\'prev\');" id="prevpage"><i><b><--- Forrige side ---</b></i></div>';
	}
	for (j=startitem; j<hittexts.length && j<startitem+listlength; j++){
		selectedstr += '<div onmousedown="mark='+j+'; selecter(\''+divid+'\',\''+inputname+'\',\''+submitinput+'\');" onmouseover="domark('+j+');" id="'+hitvalues[j]+'">'+hittexts[j]+'</div>';
	}
	if (startitem+listlength <= hittexts.length){
		selectedstr += '<div onmousedown="respage++; drawdiv(\''+divid+'\',\''+inputname+'\',\''+submitinput+'\');" onmouseover="pageshift(\'next\');" id="nextpage"><i><b>--- Næste side ---></b></i></div>';
	}
	document.getElementById(divid).innerHTML = selectedstr;
	shiftmark = false;
	mark = startitem;
	domark(startitem);
	if (hittexts){
		showlist(divid);
	} else {
		hidelist(divid);
	}
}

function domark(newmark){
	if (shiftmark=="next"){
		document.getElementById("nextpage").style.backgroundColor = "#FFFFFF";
		mark = newmark;
	} else if(shiftmark=="prev"){
		document.getElementById("prevpage").style.backgroundColor = "#FFFFFF";
		mark = newmark;
	} else {
		document.getElementById(hitvalues[mark]).style.backgroundColor = "#FFFFFF";
		mark = newmark;
	}
	shiftmark = false;
	document.getElementById(hitvalues[mark]).style.backgroundColor = "#E8F199";
}

function pageshift(dir){
	if (dir == "prev"){
		document.getElementById(hitvalues[mark]).style.backgroundColor = "#FFFFFF";
		document.getElementById("prevpage").style.backgroundColor = "#E8F199";
		mark--;
		shiftmark="prev";
	} else if(dir == "next"){
		document.getElementById(hitvalues[mark]).style.backgroundColor = "#FFFFFF";
		document.getElementById("nextpage").style.backgroundColor = "#E8F199";
		mark++;
		shiftmark="next";
	}
}

function keymark(e){
	if (e.which == 38 || e.keyCode == 38){
		if (mark > respage*listlength && mark!=0){
			domark(mark-1);
		} else if (mark==respage*listlength && mark!=0){
			pageshift("prev");
		}
  } else if (e.which == 40 || e.keyCode == 40){
		if (mark < respage*listlength+listlength-1 && mark!=hittexts.length-1){
			domark(mark+1);
		} else if (mark == (respage+1)*listlength-1 && mark!=hittexts.length-1){
			pageshift("next");
		}
	}
}

function selecter(divid,inputname,submitinput){
	if (hitinputs[mark]){
		document.getElementById(inputname).value = firstupper(hitinputs[mark]);
		document.getElementById(submitinput).value = hitvalues[mark].toUpperCase();
	} else {
		//document.getElementById(inputname).value = "";
		//document.getElementById(submitinput).value = "";
		document.getElementById(submitinput).value = document.getElementById(inputname).value;
	}
	hidelist(divid);
}

function clearnewfocus(compvalue,inputname){
	var newfocus = true;
	for (x in inputinit){
		if (inputinit[x] == inputname){
			newfocus = false;
		}
	}
	if (newfocus){
		inputinit[inputinit.length] = inputname;
		document.getElementById(inputname).value = "";
		compvalue = "";		
	}
	return compvalue;
}

function autocompleter(e,compvalue,divid,inputname,submitinput){
	compvalue = clearnewfocus(compvalue,inputname);
	if (compvalue){
		if (e.which == 38 || e.keyCode == 38 || e.which == 40 || e.keyCode == 40){
			keymark(e);
  	} else if (e.which == 13 || e.keyCode == 13){
			if (shiftmark=="next"){
				respage++;
				drawdiv(divid,inputname,submitinput);
			} else if(shiftmark=="prev"){
				respage--;
				drawdiv(divid,inputname,submitinput);
			} else {
				selecter(divid,inputname,submitinput);
			}
		} else {
			respage = 0;
			mark = 0;
			hittexts = new Array();
			hitvalues = new Array();
			hitinputs = new Array();
			for (i=0; i<cityarray.length; i++){
				hit = false;
				inputstr = "";
				cityhit = testhit(compvalue,cityarray[i]);
				countryhit = testhit(compvalue,countryarray[i]);
				airporthit = testhit(compvalue,airportarray[i]);
				aitahit = testhit(compvalue,aitaarray[i]);
				if (hit){
					inputstr += cityhit ? cityhit : firstupper(cityarray[i]);
					inputstr += ", ";
					inputstr += countryhit ? countryhit : firstupper(countryarray[i]);
					inputstr += " - ";
					inputstr += airporthit ? airporthit : firstupper(airportarray[i]);
					inputstr += " (";
					inputstr += aitahit ? aitahit.toUpperCase() : aitaarray[i].toUpperCase();
					inputstr += ")";
					hittexts[hittexts.length] = inputstr;
					hitvalues[hitvalues.length] = aitaarray[i].toLowerCase();
					hitinputs[hitinputs.length] = firstupper(cityarray[i]);
				}
			}
			if (hittexts.length){
				drawdiv(divid,inputname,submitinput);
			} else {
				hidelist(divid);
			}
		}
	} else {
		hidelist(divid);
	}
}


