var shortlistHandler=null;
var shortlistSrc=null;
var shortlistHighlight=0;
var shortlistData=null;
var highlightWhenSelected=false;
function shortlistConstruct(srcObj,replyHandler,highlight) {
    if (srcObj!=shortlistSrc || document.getElementById("shortlist")==null) {
        highlightWhenSelected=highlight;
        shortlistHandler=replyHandler;
        //if (shortlistSrc!=null) document.getElementById(shortlistSrc.id+"shortlist").style.display="none";
        closeShortlist();
        //removeTheNode("shortlist");
        shortlistSrc=srcObj;
        shortlistBox="<div id=\"shortlist\">Searching</div>";
        document.getElementById(srcObj.id+"shortlist").innerHTML=shortlistBox;
        //document.getElementById(srcObj.id+"shortlist").style.display="block";
        shortlistHighlight=0;
    }
}
function updateShortlist(reply) {
    if (highlightWhenSelected) setInputSelected(shortlistSrc,false);
    var op="";
	if (typeof(reply)=="object") {
		if (shortlistHighlight>=reply.length) shortlistHighlight=reply.length-1;
		shortlistData=[];
		i=0;
		for (code in reply) {
			if (code!="max" && code!="min" && code!="find" && code!="indexOf") {
				shortlistData.push(code);
				op+="<div class=\"";
				if (i==shortlistHighlight) {
					op+="highlight";
				}else{
					op+="item";
				}
				op+="\" onmouseover=\"shortlistHighlighter("+i+")\" onclick=\"selectShortlistOption("+i+")\" id=\"shortlist"+i+"\">"+reply[code]+"</div>";
				i++;
			}
		}
	}else{
		if (reply=="") {
			op="None found";
		}else{
			var options=reply.split("-optionsplit-");
			if (shortlistHighlight>=options.length) shortlistHighlight=options.length-1;
			shortlistData=[];
			for (var i=0;i<options.length;i++) {
				var theData=options[i].split("-datasplit-");
				shortlistData.push(theData[0]);
				op+="<div class=\"";
				if (i==shortlistHighlight) {
					op+="highlight";
				}else{
					op+="item";
				}
				op+="\" onmouseover=\"shortlistHighlighter("+i+")\" onclick=\"selectShortlistOption("+i+")\" id=\"shortlist"+i+"\">"+theData[1]+"</div>";
			}
		}
    }
	if (op=="") op="None found";
    document.getElementById("shortlist").innerHTML=op;
}
function shortlistSelect(step) {
    if (document.getElementById("shortlist"+shortlistHighlight)!=null) {
        document.getElementById("shortlist"+shortlistHighlight).className="item";
    }
    shortlistHighlight+=step;
    if (shortlistHighlight<0) shortlistHighlight=0;
    if (shortlistHighlight>=document.getElementById("shortlist").childNodes.length) shortlistHighlight=document.getElementById("shortlist").childNodes.length-1;
    if (document.getElementById("shortlist"+shortlistHighlight)!=null) {
        document.getElementById("shortlist"+shortlistHighlight).className="highlight";
    }
}
function shortlistHighlighter(newoption) {
    if (newoption<0) newoption=0;
    if (newoption>=document.getElementById("shortlist").childNodes.length) newoption=document.getElementById("shortlist").childNodes.length-1;
    if (newoption!=shortlistHighlight) {
        if (document.getElementById("shortlist"+shortlistHighlight)!=null) {
            document.getElementById("shortlist"+shortlistHighlight).className="item";
        }
        shortlistHighlight=newoption;
        if (document.getElementById("shortlist"+shortlistHighlight)!=null) {
            document.getElementById("shortlist"+shortlistHighlight).className="highlight";
        }
    }
}
function selectShortlistOption(newoption) {
    if (shortlistSrc!=null) {
        if (newoption==null) newoption=shortlistHighlight;
        //pasteOutput("*select*");
        if (highlightWhenSelected) {
            setInputSelected(shortlistSrc,true);
        }
        //alert(shortlistData[newoption]);
        shortlistHandler(shortlistData[newoption]);
        //document.getElementById(shortlistSrc.id+"shortlist").style.display="none";
        closeShortlist();
	//alert(newoption);
    }
}
function checkShortlistKeys(srcObj,e,handler,docUrl,highlight,content) {
        if (window.event) keycode=window.event.keyCode;
        else if (e) keycode=e.which;
    //pasteOutput("*"+keycode+"*");
        if (keycode==38 || keycode==40 || keycode==13 || keycode==27 || keycode==9 || keycode==11) {
            if (keycode==27) cancelShortlist();
            if (keycode==9 || keycode==11) closeShortlist();
            if (keycode==38) shortlistSelect(-1);
            if (keycode==40) shortlistSelect(+1);
            if (keycode==13) selectShortlistOption(shortlistHighlight);
        }else{
            shortlistConstruct(srcObj,handler,highlight);
            if (docUrl==null && content!=null) {
                updateShortlist(content);
            }else{
                sendRequest(docUrl,updateShortlist);
            }
        }
}
function cancelShortlist() {
    //pasteOutput("*cancel*");
    if (shortlistSrc!=null) {
        //document.body.innerHTML+="<br />Cancelling shortlist";
        if (highlightWhenSelected) {
            setInputSelected(shortlistSrc,false);
        }
        if (shortlistHandler!=null) shortlistHandler(null);
    }
    closeShortlist();
}
function closeShortlist() {
    if (shortlistSrc!=null) {
        pasteOutput("*close*");
        //if (document.getElementById(shortlistSrc.id+"shortlist")!=null) document.getElementById(shortlistSrc.id+"shortlist").style.display="none";
        shortlistHandler=null;
        shortlistSrc=null;
        shortlistHighlight=0;
        shortlistData=null;
        removeTheNode("shortlist");
    }
}

function setInputSelected(obj,selected) {
    cn="normalinput";
    if (selected) {
        cn="normalinput";
    }
    //pasteOutput(cn);
    //alert(cn);
    obj.className=cn;
}
function pasteOutput(ip) {
    if (document.getElementById("test")!=null) {
        document.getElementById("test").value+=ip+"\n";
    }
}
var shortlistInput=null;
function searchCountries(srcObj,e) {
	if (shortlistInput!=srcObj) cancelShortlist();
	shortlistInput=srcObj;
	scored=new Array();
	for(code in countries) {
		pattern=new RegExp(srcObj.value,"i");
		if (pattern.test(countries[code])) {
			scored.push(new Array(scoreSearch(srcObj.value,countries[code]),countries[code],code));
			//pRH(countries[code],document.body);
		}
	}
	scored.sort(sortScore);
	maxlist=10;
	if (scored.length<maxlist) maxlist=scored.length;
	sorted=new Array();
	for(l=0;l<maxlist;l++) sorted[scored[l][2]]=scored[l][1];
	//pRH(scored.length,document.body);
    checkShortlistKeys(srcObj,e,selectCountry,null,true,sorted);
}
function searchCounties(srcObj,e) {
	if (shortlistInput!=srcObj) cancelShortlist();
	shortlistInput=srcObj;
	scored=new Array();
	for(code in counties) {
		pattern=new RegExp(srcObj.value,"i");
		if (pattern.test(counties[code])) {
			scored.push(new Array(scoreSearch(srcObj.value,counties[code]),counties[code],code));
			//pRH(countries[code],document.body);
		}
	}
	scored.sort(sortScore);
	maxlist=10;
	if (scored.length<maxlist) maxlist=scored.length;
	sorted=new Array();
	for(l=0;l<maxlist;l++) sorted[scored[l][2]]=scored[l][1];
	//pRH(scored.length,document.body);
    checkShortlistKeys(srcObj,e,selectCounty,null,true,sorted);
}
function sortScore(a,b) {
	r=0;
	if (a[0]>b[0]) r=1;
	if (a[0]<b[0]) r=-1;
	return r;
}
function scoreSearch(searchstring,subject) {
	ss=searchstring.split(" ");
	subs=subject.split(" ");
	score=0;
	for(a=0;a<ss.length;a++) {
		for(b=0;b<subs.length;b++) {
			if (subs[b]==ss[a]) {
				score+20;
			}else{
				pattern=new RegExp("^"+ss[a],"i");
				if (pattern.test(subs[b])) {
					score+=10;
				}else{
					pattern=new RegExp(ss[a],"i");
					if (pattern.test(subs[b])) {
						score+=10*(subs[b].length/ss[a].length);
					}
				}
			}
		}
	}
	return score;				
}
function searchPlaces(srcObj,e)
{
	shortlistInput=srcObj;
    var searchField=srcObj.value;
    var docUrl="/find.php?place="+searchField;
    checkShortlistKeys(srcObj,e,placeSelect,docUrl,true);
}
function placeSelect(reply) {
	p=reply.split("^");
	//shortlistInput.value=p[0];
	//placeGeo=new Array(p[1],p[2]);
	setMapLocation(new Array(p[1],p[2]),2,p[3]);
	//alert(reply);
	document.getElementById("selectedPlace").innerHTML="<table style=\"display:inline\"><tr><td class=\"geoloc\" onclick=\"setMapLocation(new Array("+p[1]+","+p[2]+"),2)\" title=\"Reset the location to "+p[0]+"\">"+p[0]+"</td></tr></table>";
}
function searchpostcode(srcObj,e)
{
	shortlistInput=srcObj;
    var searchField=srcObj.value;
    var docUrl="/find.php?postcode="+searchField;
    checkShortlistKeys(srcObj,e,postcodeSelect,docUrl,true);
}
function postcodeSelect(reply) {
	p=reply.split("^");
	
	//shortlistInput.value=p[0];
	//placeGeo=new Array(p[1],p[2]);
	setMapLocation(new Array(p[1],p[2]),3,p[0]);
	document.getElementById("selectedpostcode").innerHTML="<table style=\"display:inline\"><tr><td class=\"geoloc\" onclick=\"setMapLocation(new Array("+p[1]+","+p[2]+"),3)\" title=\"Reset the location to "+p[0]+"\">"+p[0]+"</td></tr></table>";
}
function checkKey(e,alpha,numeric) {
    if (window.event) {
        e=window.event;
        keycode=window.event.keyCode;
    }else{
        keycode=e.which;
    }
	reply=false;
	if (alpha) {
		if (keycode<91 && keycode>64) reply=true;
	}
	if (numeric) {
		if (keycode<58 && keycode>47) reply=true;
		if (keycode<106 && keycode>95) reply=true;
	}
	if (keycode==190 || keycode==110) reply=true;
	if (keycode==8 || keycode==46 || keycode==36 || keycode==35 || keycode==37 || keycode==39) reply=true;
	//if (keycode==27) closeShortlist();
	if (keycode==27) cancelShortlist();
	//pRH(keycode,document.body);
	return reply;
}