var xmlHttp;

function chooseHint(primary_hint,secondary_hint,searchfield) {
	document.getElementById(searchfield).value = stripslashes(primary_hint);
	document.getElementById(searchfield + "_hints").style.display="none";
	document.getElementById(searchfield + "_hints").innerHTML="";
	if (secondary_hint.length > 0) {
		if (searchfield == "latin") {
			document.getElementById("common").value = stripslashes(secondary_hint);
		}
		else if (searchfield == "common") {
			document.getElementById("latin").value = stripslashes(secondary_hint);
		}
	}
}

function noHint(searchfield) {
	document.getElementById(searchfield + "_hints").style.display="none";
	document.getElementById(searchfield + "_hints").innerHTML="";
}

function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}

function addslashes(str) {
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\0/g,'\\0');
	return str;
}

var noResultsCharNum = 255; // The number of characters at which the search starts to return no results

function showResult(str,searchfield) {
	if (str.length<3) {
		document.getElementById(searchfield + "_hints").style.display="none";
		document.getElementById(searchfield + "_hints").innerHTML="";
		document.getElementById(searchfield + "_hints").style.border="0px";
		return;
	}
	else if(str.length<noResultsCharNum) {
		noResultsCharNum = 255; //  string has been deleted back to the point where it might start returning results again, so remove old barrier to searching
		
		xmlHttp=GetXmlHttpObject()
	
		if (xmlHttp==null) {
			alert ("Browser does not support HTTP Request");
			return;
		}
	
		var url="autocomplete/livesearch.php";
		url=url+"?q="+str;
		url=url+"&field="+searchfield;
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

function stateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		var p = eval("(" + xmlHttp.responseText + ")");
		field = p.searchfield;
		searchString = p.searchString;
		var hintlist = "";
		if (p.primary_hints != null && p.primary_hints.length > 0) {
			for (i=0; i<p.primary_hints.length; i++) {
			hintlist = hintlist + '<div class="livesearch_item" onClick="chooseHint(\''+ addslashes(p.primary_hints[i]) + '\',\''+ addslashes(p.secondary_hints[i]) + '\',\'' + field +'\')" onMouseOver="this.style.backgroundColor=\'#CC0000\';" onMouseOut="this.style.backgroundColor=\'#FFFFFF\';">' + p.primary_hints[i] + '<div class=\"livesearch_subname\">' + p.secondary_hints[i] + '</div></div>';
			}
		}
		else {
			noResultsCharNum = searchString.length;
			hintlist = '<div class="livesearch_item" onClick="noHint(\''+ field +'\');" onMouseOver="this.style.backgroundColor=\'#CC0000\';" onMouseOut="this.style.backgroundColor=\'#FFFFFF\';">no suggestions</div>';
		}
		
		document.getElementById(field + "_hints").innerHTML=hintlist;
		document.getElementById(field + "_hints").style.display="";
		document.getElementById(field + "_hints").style.border="1px solid #A5ACB2";
	} 
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function chainedSelect(mainWin,selection,unum) {
	document = mainWin.document;
	if (selection == "mygroup") {
		document.getElementById('groupname').style.display='';
		xmlHttp=GetXmlHttpObject()
	
		if (xmlHttp==null) {
			alert ("Browser does not support HTTP Request");
			return;
		}
	
		var url="autocomplete/groupsearch.php";
		url=url+"?unum="+unum;
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=groupStateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
	else {
		document.getElementById('groupname').style.display='none';
		document.getElementById('group').options[2].text = "one of my groups";
		if (selection == "newgroup") {
			createGroupPopup();
		}
	}
}

function createGroupPopup() {
	var newWin = window.open('creategroup.php?popup=1','','width=300,height=350,location=no,menubar=no,toolbar=no');
	if (newWin && newWin.top) {
		// popup has opened
	} else {
		// popup has been blocked
		alert("Sorry, we couldn't open a window to create a new group.  Perhaps your browser is blocking pop-ups?\n\n You can manually create a group at :\n\nhttp://forage.rs/creategroup.php");
	} 
}

function groupStateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		var groupQuery = eval("(" + xmlHttp.responseText + ")");
		var groups = groupQuery.groups;
		var group_ids = groupQuery.group_ids;
		if (groups != null && groups.length > 0) {
			listLength=document.getElementById('groupname').length;
			for (j=listLength-1; j>=0; j--) {
				document.getElementById('groupname').remove(j);
			}
			for (i=0; i<groups.length; i++) {
				var groupoption=document.createElement('option');
				groupoption.text=groups[i];
				groupoption.value=group_ids[i];
				try {
					document.getElementById('groupname').add(groupoption,null); // standards compliant
				}
				catch(ex) {
					document.getElementById('groupname').add(groupoption); // IE only
				}
			}
			document.getElementById('group').options[document.getElementById('group').selectedIndex].text = "my group";
		}
		else {
			alert("you are not a member of any groups.  To create a new group choose the \"new group\" option.");
			document.getElementById('groupname').style.display='none';
		}
	}
}
