function AutoComplete(obj, type, hiddenObj, div, addsign)
{
	this.input = obj;
	if(hiddenObj)
	{
		this.hInput = hiddenObj;
	}else
	{
		this.hInput = null;
	}
	
	if(div)
	{
		this.resultDiv = div;
	}else
	{
		this.resultDiv = this.input.parentNode.parentNode;
	}
		
	this.onChangeFunction = null;
	
	this.type = type;
	this.input.onkeyup = myAutoCompleteTrampoline(this, 0);
	this.input.onkeydown = myAutoCompleteTrampoline(this, -1)
	this.input.onfocus = myAutoCompleteTrampoline(this, 1);
	this.input.onblur = myAutoCompleteTrampoline(this, 2);
	this.result = document.createElement("div");
	this.oldvalue = "";
	this.serial = 0;
	this.color = "#D4FFFF";
	this.mouse = false;
	this.done = false;
	if(this.hInput)
	{
		if(this.hInput.value != -1)
		{
			this.input.style.backgroundColor = this.color;
			this.done = true;
		}
	}
	//this.result.setAttribute("id", "itemLeft");
	this.result.style.position = "absolute";
	//this.result.style.display = "block";
	this.r = new Array();
	this.ajax = null;
	this.selected = -1;
	this.input.setAttribute("autocomplete", "off");
	//this.input.setAttribute("autosubmit", "off");
	//autocomplete="off"
	version=0
	if (navigator.appVersion.indexOf("MSIE")!=-1){
	temp=navigator.appVersion.split("MSIE");
	version=parseFloat(temp[1]);
	}

	if (version>=5.5) //NON IE browser will return 0
	{
		this.result.style.width = "210px";
	}else
	{
		this.result.style.width = "208px";
	}
	
	//this.result.style.height = "150px";
	//this.result.style.overflow = "auto";
	
	if(version < 7 && version > 0)
	{
		this.iframe = true;
	}else
	{
		this.iframe = false;
	}
	//alert(version);
	//this.result.style.width = this.input.style.width;
	this.result.style.backgroundColor = "#F7F7F7";
	//this.input.style.backgroundColor = "#F7F7F7";
	this.result.style.border = "solid 1px #C7C7C7";
	this.result.style.overflow = "hidden";
	
	//this.result.style.top = 0;
	//this.result.style.left = 0;
	
	//position:absolute;visibility:hidden
	this.result.style.zIndex = 2;
	this.removing = false;

	//alert(addsign);
	if(addsign != null)
	{
		this.showAddOption = addsign;
		//alert(this.showAddOption);
	}else
	{
		this.showAddOption = true;
	}
	
	//alert(this.showAddOption);
	if((this.showAddOption == true) && (this.type == 'city' || this.type == 'university' || this.type == 'school'))
	{
		var add = document.createElement("label");
		add.innerHTML = "<label id='itemSmallLeft'><b>Can't find your "+this.type+"?&nbsp;<a href='#' onclick='return false;' style='color:#0288BB'>add here</a></b></label>";
	//add.id = "itemSmallLeft"
		add.onclick = myAutoCompleteTrampoline(this, 7);
		this.input.parentNode.appendChild(add);
	}
	
	
	this.saveCurrentSelection();
	
	
	//alert(this.input.style.width);
}
AutoComplete.prototype.setCountry = function(country)
{
	this.hCountry = country;
}
AutoComplete.prototype.SetOnChange = function(func)
{
	this.onChangeFunction = func;
}

AutoComplete.prototype.clear = function(func)
{
	this.input.value="";
	this.hInput.value = 1;
	this.input.style.backgroundColor = "";
}

AutoComplete.prototype.onKey = function(e)
{
	var characterCode;
	if(e && e.which)
	{ 
		e = e;
		characterCode = e.which;
	}
	else
	{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}
			//alert(characterCode);
			if(characterCode == 8)
			{
				//this.removing = true;
			}else
			{
				this.removing = false;
			}
	if(characterCode == 13 || characterCode == 44 || characterCode == 59 || characterCode == 188 || characterCode == 186 || characterCode == 9)
	{ 
		if(this.r.length != 0)
		{
			this.selectResult(this.selected);
		}
		return false;
	}else
	{
		return true;
	}
}
AutoComplete.prototype.onChange = function(e)
{
	//alert(this.input.value);
	//alert(e);
	if(this.r.length == 0)
	{
		//this.result.innerHTML = "Loading..."
	}else
	{
		var characterCode;
			if(e && e.which)
			{ 
				e = e;
				characterCode = e.which;
			}
			else
			{
				e = event;
				characterCode = e.keyCode; //character code is contained in IE's keyCode property
			}
			//alert(characterCode);
			if(characterCode == 13 || characterCode == 44 || characterCode == 59 || characterCode == 188 || characterCode == 186)
			{ 
				this.selectResult(this.selected);
				//alert("d");
				return true;
			}
			if(characterCode == 40)
			{ 
				
				if(this.selected < this.r.length - 1)
				{
					this.selected++;
				}else
				{
					this.selected = 0;
				}
				this.updateResult(this.selected);
				return;
			}
			
			if(characterCode == 38)
			{ 
				
				if(this.selected > 0)
				{
					this.selected--;
				}else
				{
					if(this.r.length > 0)
					{
						this.selected = this.r.length - 1;
					}
				}
				this.updateResult(this.selected);
				return;
			}
			
	}
	
	if(this.oldvalue == this.input.value)
	{
		return;
	}else
	{
		this.oldvalue = this.input.value;
		this.edited = true;
	}
	
	if(this.ajax)
	{
		this.ajax.cancel();
	}
	
	//var aj = this.ajax;
	this.serial++;
	url = "ajax//autocompleteresult.php?word="+this.input.value+"&serial="+this.serial+"&type="+this.type;
	if(this.hCountry)
	{
		url = url + "&country="+this.hCountry.value;
	}
	
	if(this.input.value.length == 0)
	{
		this.r = new Array();
		this.selected = -1;
		if(this.iframe)
		{
			this.result.innerHTML = '<div id="itemLeft">Start typing your '+this.type+' name</div><iframe style="display:block;position:absolute;top:0;z-index:-1;filter:mask()" width="1000px" height="5000px"></iframe>';
		}else
		{
			this.result.innerHTML = '<div id="itemLeft">Start typing your '+this.type+' name</div>';
		}
		return;
	}
	if(this.done == false)
	{
		this.ajax = new MyAjax(url,"",myAutoCompleteTrampoline(this, 3));
	}
}

AutoComplete.prototype.updateResult = function(index)
{
	if(this.done == true)
	{
		return;
	}
	if(index)
	{
		this.selected = index;
	}else
	{
		this.selected = 0;
	}
	var msg = "";
	for(var i = 0; i < this.r.length; i++)
	{
		if(this.type == "city")
		{
			var city = this.r[i].getAttribute("city");
			var state = this.r[i].getAttribute("state");
			var country = this.r[i].getAttribute("country");
			var location = "";
			if(state != "" && state != "N/A")
			{
				location = state+", "+country;
			}else
			{
				location = country;
			}
			if(i == this.selected)
			{
				msg += '<div style="background-color:#000000; border-bottom:1px solid #000000; width:100%;"><div id="itemLeft" style="color:#FFFFFF;"><b>'+city+'</b></div><div id="itemLeft" style="font-size:9px; color:#C7C7C7;"><b>'+location+'</b></div></div>'; 
			}else
			{
				msg += '<div style="border-bottom:1px solid #000000; width:100%;"><div id="itemLeft"><b>'+city+'</b></div><div id="itemLeft" style="font-size:9px; color:#505050;"><b>'+location+'</b></div></div>'; 
			}
		}else 
		if(this.type == "country")
		{
			
			var country = this.r[i].getAttribute("country");
			
			if(i == this.selected)
			{
				msg += '<div style="background-color:#000000; border-bottom:1px solid #000000; width:100%;"><div id="itemLeft" style="color:#FFFFFF;"><b>'+country+'</b></div></div>'; 
			}else
			{
				msg += '<div style="border-bottom:1px solid #000000; width:100%;"><div id="itemLeft"><b>'+country+'</b></div></div>'; 
			}
		}else 
		if(this.type == "language")
		{
			
			var show = this.r[i].getAttribute("language");
			
			if(i == this.selected)
			{
				msg += '<div style="background-color:#000000; border-bottom:1px solid #000000; width:100%;"><div id="itemLeft" style="color:#FFFFFF;"><b>'+show+'</b></div></div>'; 
			}else
			{
				msg += '<div style="border-bottom:1px solid #000000; width:100%;"><div id="itemLeft"><b>'+show+'</b></div></div>'; 
			}
		}else 
		if(this.type == "concentration")
		{
			
			var show = this.r[i].getAttribute("concentration");
			
			if(i == this.selected)
			{
				msg += '<div style="background-color:#000000; border-bottom:1px solid #000000; width:100%;"><div id="itemLeft" style="color:#FFFFFF;"><b>'+show+'</b></div></div>'; 
			}else
			{
				msg += '<div style="border-bottom:1px solid #000000; width:100%;"><div id="itemLeft"><b>'+show+'</b></div></div>'; 
			}
		}
		else 
		if(this.type == "university")
		{
			
			var show = this.r[i].getAttribute("university");
			var location = this.r[i].getAttribute("location");
			if(i == this.selected)
			{
				msg += '<div style="background-color:#000000; border-bottom:1px solid #000000; width:100%;"><div id="itemLeft" style="color:#FFFFFF;"><b>'+show+'</b></div><div id="itemLeft" style="font-size:9px; color:#C7C7C7;"><b>'+location+'</b></div></div>'; 
			}else
			{
				msg += '<div style="border-bottom:1px solid #000000; width:100%;"><div id="itemLeft"><b>'+show+'</b></div><div id="itemLeft" style="font-size:9px; color:#505050;"><b>'+location+'</b></div></div>'; 
			}
		}else 
		if(this.type == "school")
		{
			
			var show = this.r[i].getAttribute("school");
			var location = this.r[i].getAttribute("location");
			if(i == this.selected)
			{
				msg += '<div style="background-color:#000000; border-bottom:1px solid #000000; width:100%;"><div id="itemLeft" style="color:#FFFFFF;"><b>'+show+'</b></div><div id="itemLeft" style="font-size:9px; color:#C7C7C7;"><b>'+location+'</b></div></div>'; 
			}else
			{
				msg += '<div style="border-bottom:1px solid #000000; width:100%;"><div id="itemLeft"><b>'+show+'</b></div><div id="itemLeft" style="font-size:9px; color:#505050;"><b>'+location+'</b></div></div>'; 
			}
		}
		else 
		if(this.type == "buddy")
		{
			
			var show = this.r[i].getAttribute("name");
			
			if(i == this.selected)
			{
				msg += '<div style="background-color:#000000; border-bottom:1px solid #000000; width:100%;"><div id="itemLeft" style="color:#FFFFFF;"><b>'+show+'</b></div></div>'; 
			}else
			{
				msg += '<div style="border-bottom:1px solid #000000; width:100%;"><div id="itemLeft"><b>'+show+'</b></div></div>'; 
			}
		}
	}
	if(this.r.length > 0)
	{
		if(this.iframe)
		{
			this.result.innerHTML = msg+'<iframe style="display:block;position:absolute;top:0;z-index:-1;filter:mask()" width="1000px" height="5000px"></iframe>';
		}else
		{
			this.result.innerHTML = msg;
		}
		this.result.onclick = null;
	}else
	{
		
		if(this.type == "city" && this.showAddOption)
		{
			this.result.innerHTML = '<div id="itemLeft">No results found.</div><div id="itemLeft">Can\'t find your location?</div><div id="itemHeadLeft" style="color:#FFFFFF; background-color:#000000; cursor:pointer;">Add Here</div>';
			//var add = document.createElement("div");
			//add.innerHTML = '<div id="itemLeft"><a href="#">Add Here</a>';
			//add.onclick = myAutoCompleteTrampoline(this, 7);
			//this.result.appendChild(add);
			this.result.onclick = myAutoCompleteTrampoline(this, 7);
			//alert(add.onclick);
		}else
		if(this.type == "university" && this.showAddOption)
		{
			this.result.innerHTML = '<div id="itemLeft">No results found.</div><div id="itemLeft">Can\'t find your university?</div><div id="itemHeadLeft" style="color:#FFFFFF; background-color:#000000; cursor:pointer;">Add Here</div>';
			this.result.onclick = myAutoCompleteTrampoline(this, 7);
		}else
		if(this.type == "school" && this.showAddOption)
		{
			this.result.innerHTML = '<div id="itemLeft">No results found.</div><div id="itemLeft">Can\'t find your highschool?</div><div id="itemHeadLeft" style="color:#FFFFFF; background-color:#000000; cursor:pointer;">Add Here</div>';
			this.result.onclick = myAutoCompleteTrampoline(this, 7);
		}else
		{
			this.result.innerHTML = '<div id="itemLeft">No results found.</div>';
			this.result.onclick = null;
		}
		if(this.iframe)
		{
			this.result.innerHTML = this.result.innerHTML+'<iframe style="display:block;position:absolute;top:0;z-index:-1;filter:mask()" width="1000px" height="5000px"></iframe>';
		}
		
		//return;
		
	}
	
	
	var counter = 0;
	for(var i = 0; i < this.result.childNodes.length; i++)
	{
		//var this = this;
		this.result.childNodes[i].onmouseout = myAutoCompleteTrampoline(this, 5, i);
		this.result.childNodes[i].onmouseover = myAutoCompleteTrampoline(this, 4, i);
		this.result.childNodes[i].onclick = myAutoCompleteTrampoline(this, 6, i);
		counter++;
	}
	
	//if(counter == 1)
	//{
	//	if(this.removing == false)
	//	{
	//		this.selectResult(0);
	//	}
	//}
	//alert(counter);
}
AutoComplete.prototype.showResult = function()
{
	//alert(this.ajax.getResponseText());
	var data = this.ajax.getResponseText();
	//alert(this.type);
	if(data.length <= 0)
	{
		this.r = new Array();
		this.selected = -1;
		if(this.iframe)
		{
		this.result.innerHTML = '<div id="itemLeft">Start typing your '+this.type+' name</div><iframe style="display:block;position:absolute;top:0;z-index:-1;filter:mask()" width="1000px" height="5000px"></iframe>';
		}else
		{
			this.result.innerHTML = '<div id="itemLeft">Start typing your '+this.type+' name</div>';
		}
		return;
	}
	
	if (window.ActiveXObject)
  	{
  		var doc=new ActiveXObject("Microsoft.XMLDOM");
 	 	doc.async="false";
 	 	doc.loadXML(data);
  	}else
 	 {
 		 var parser=new DOMParser();
  		var doc=parser.parseFromString(data,"text/xml");
  	}// documentElement always represents the root node
	var x=doc.documentElement;
	
	if(!x)
	{
		return;
	}
	
	if(x.getAttribute("serial") == this.serial)
	{
		this.r = x.getElementsByTagName("result");
		this.updateResult();
		//alert(this.resultDiv.style.height);
	}
}
AutoComplete.prototype.onFocus = function()
{
	//alert(this.input.value);
	//if(this.r.length <= 0)
	//{
		//alert(this.type);
		this.done = false;
		var str = this.input.value;
		str = str.split(",");
		this.input.value = str[0];
		setCaretTo(this.input, str[0].length);
		if(this.hInput)
		{
			this.hInput.value = -1;
		}
		this.input.style.backgroundColor = "";
		if(this.iframe)
		{
			this.result.innerHTML = '<div id="itemLeft">Start typing your '+this.type+' name</div><iframe style="display:block;position:absolute;top:0;z-index:-1;filter:mask()" width="1000px" height="5000px"></iframe>';
		}else
		{
			this.result.innerHTML = '<div id="itemLeft">Start typing your '+this.type+' name</div>';
		}
		if(this.resultDiv)
		{
			this.resultDiv.appendChild(this.result);
		}
		/*else
		{
			this.input.parentNode.parentNode.appendChild(this.result);
		}
		*/
		this.r = new Array();
	//}
}

AutoComplete.prototype.onBlur = function()
{
	//alert(this.mouse);
	
	if(this.mouse == false && this.done == false)
	{
		try
		{
			//this.input.parentNode.parentNode.removeChild(this.result);
			this.resultDiv.removeChild(this.result);
		}catch(ex)
		{
			alert("ex1");
		}
		this.input.value = "";
		if(this.hInput)
		{
			this.hInput.value = -1;
		}
		
		if(this.edited == false)
		{
			if(this.prevId != -1)
			{
				this.input.value = this.prevName;
				this.hInput.value = this.prevId;
				this.input.style.backgroundColor = this.color;
			}
		}
	}
	
}

function myAutoCompleteTrampoline(obj, value, index, e)
{
	if(value == -1)
	{
  		return function (e) { return obj.onKey(e);};
	}else if(value == 0)
	{
  		return function (e) { obj.onChange(e); return true;};
	}else
	if(value == 1)
	{
  		return function () { obj.onFocus(); };
	}else
	if(value == 2)
	{
  		return function () { obj.onBlur(); };
	}else
	if(value == 3)
	{
  		return function () { obj.showResult(); };
	}else
	if(value == 4)
	{
  		return function () 
		{ 
			obj.mouse = true; 
			if(index != obj.selected)
			{
				//alert ("over"); 
				
				obj.updateResult(index); 
			}
		};
	}else
	if(value == 5)
	{
  		return function () 
			{ 
				//alert("out"); 
				obj.mouse = false;
			};
	}else
	if(value == 6)
	{
  		return function () 
			{ 
				obj.selectResult(index);
			
				/*
				try
				{
					obj.done = true;
					obj.mouse = false;
					//obj.r = new Array();
					//this.input.blur();
					var city = obj.r[index].getAttribute("city");
					var state = obj.r[index].getAttribute("state");
					var country = obj.r[index].getAttribute("country");
					var id = obj.r[index].getAttribute("id");
					
					
					if(state == "" || state == "N/A")
					{
						obj.input.value = city+", "+country;
					}else
					{
						obj.input.value = city+", "+state+", "+country;
					}
					if(obj.hInput)
					{
						obj.hInput.value = id;
					}
					obj.input.style.backgroundColor = obj.color;
					//obj.input.value = city;
					
					obj.input.parentNode.parentNode.removeChild(obj.result);
					obj.input.blur();
				}catch(e)
				{
					//obj.mouse = true;
					//obj.done = false;
					//obj.r = new Array();
					//alert("ex");
				}
				*/
				
			};
	}else if(value == 7)
	{
		//alert("a");
		return function()
		{
			try
				{
			//this.input.parentNode.parentNode.removeChild(this.result);
				obj.resultDiv.removeChild(obj.result);
				obj.input.value = "";
			}catch(ex)
			{
				//alert("ex1");
			}
			if(obj.type == "city")
			{
				newDBLocation(null, null, null, obj);
			}else
			if(obj.type == "university")
			{	
				newDBUniversity(null, null, null, obj);
			}else
			if(obj.type == "school")
			{
				newDBHighSchool(null, null, null, obj);
			}
		};
	}
}


AutoComplete.prototype.saveCurrentSelection = function()
{
	this.prevId = this.hInput.value;
	this.prevName = this.input.value;
	this.edited = false;
}
AutoComplete.prototype.setResult = function(id, value)
{
	this.hInput.value = id;
	this.input.value = value;
	if(this.id != -1)
	{
		this.input.style.backgroundColor = this.color;
	}else
	{
		this.input.style.backgroundColor = "";
	}
	this.saveCurrentSelection();
}

AutoComplete.prototype.selectResult = function(index)

{
	try
	{
		var obj = this;
		obj.done = true;
		obj.mouse = false;
					//obj.r = new Array();
					//this.input.blur();
		var result = "";
		if(this.type == "city")
		{
			var city = obj.r[index].getAttribute("city");
			var state = obj.r[index].getAttribute("state");
			var country = obj.r[index].getAttribute("country");
			var id = obj.r[index].getAttribute("id");
			
			
			if(state == "" || state == "N/A")
			{
				result = city+", "+country;
			}else
			{
				result = city+", "+state+", "+country;
			}
		}else
		if(this.type == "country")
		{
			
			var country = obj.r[index].getAttribute("country");
			var id = obj.r[index].getAttribute("id");
			result = country;
		}else
		if(this.type == "language")
		{
			
			var lang = obj.r[index].getAttribute("language");
			var id = obj.r[index].getAttribute("id");
			result = lang;
		}else
		if(this.type == "concentration")
		{
			
			var lang = obj.r[index].getAttribute("concentration");
			var id = obj.r[index].getAttribute("id");
			result = lang;
		}
		else
		if(this.type == "university")
		{
			
			var lang = obj.r[index].getAttribute("university");
			var id = obj.r[index].getAttribute("id");
			result = lang;
		}else
		if(this.type == "school")
		{
			
			var lang = obj.r[index].getAttribute("school");
			var id = obj.r[index].getAttribute("id");
			result = lang;
		}else
		if(this.type == "buddy")
		{
			
			var lang = obj.r[index].getAttribute("name");
			var id = obj.r[index].getAttribute("id");
			result = lang;
		}
		
		
		obj.input.value = result;
		if(obj.hInput)
		{
			obj.hInput.value = id;
		}
		
		obj.input.style.backgroundColor = obj.color;
		obj.saveCurrentSelection();
		//obj.input.parentNode.parentNode.removeChild(obj.result);
		obj.resultDiv.removeChild(this.result);
		obj.input.blur();
		
		if(obj.onChangeFunction != null)
		{
			//alert("onchange");
			obj.onChangeFunction();
		}
	}catch(e)
	{
		//obj.mouse = true;
		//obj.done = false;
		//obj.r = new Array();
		//alert("ex"+e.toString());
	}
}

