var current = '';

function completeForm(ref, div1, div2, add_to_list) {
	
	ajaxFormFill(div1, div2, 'update', ref, '', specific, add_to_list);
	
	
}

function ajaxFormFill(div1, div2, type, ref, t, specific, add_to_list)
{

v = document.getElementById(div1).value;


var xmlHttp1;

try
  {
  // Firefox, Opera 8.0+, Safari
  
  xmlHttp1=new XMLHttpRequest();
 
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    
	xmlHttp1=new ActiveXObject("Msxml2.XMLHTTP");
	
    }
  catch (e)
    {
    try
      {
      
	  xmlHttp1=new ActiveXObject("Microsoft.XMLHTTP");
	 
      }
    catch (e)
      {
      alert("Your browser does not support AJAX");
      return false;
      }
    }
  }
  xmlHttp1.onreadystatechange=function()
    {
    if(xmlHttp1.readyState==4)
      {
		  
	 	 if (type == "update") {
     		document.getElementById(div1).value=xmlHttp1.responseText;
			
			
	  	} else {
	   		document.getElementById(div2).innerHTML=xmlHttp1.responseText;
	   		
		}
      }
    }
  if (type == "update") {
	 
	  xmlHttp1.open("GET",base+"dropdown.inc.php?div1="+div1+"&div2="+div2+"&r="+ref+"&current="+current+"&specific="+specific+"&add_to_list="+add_to_list,true);
	  
  } else {
 	   xmlHttp1.open("GET",base+"dropdown.inc.php?v="+v+"&div1="+div1+"&div2="+div2+"&t="+t+"&current="+current+"&specific="+specific+"&add_to_list="+add_to_list,true);
	   
  }
  xmlHttp1.send(null);
 
  }
  
  function leaveFormFill(div1) {
  	window.setTimeout("leaveForm('" + div1 + "')", 500);
  }
  
  function leaveForm(div1) {
  	
  	document.getElementById(div1).innerHTML='';
	current = 0;
}

var current = 0;
var currentVal = '';
var dropName = '';

document.onkeydown = stopEnter;

// check every keypress and if 'return', run the function
function stopEnter(e) {
	var KeyID = (window.event) ? event.keyCode : e.keyCode;
	//alert(KeyID);
	if (KeyID == 13 && current > 0) {
		
		document.getElementById('f_'+dropName).value = document.getElementById('drop_'+current).innerHTML;
		leaveForm('i_'+dropName);
		document.getElementById('drop_current_'+dropName).value = 0;
		current = 0;
		return false;
		
	}
	
}

// add from mouse click
function stopMouseClick(v) {

	document.getElementById('f_'+dropName).value = v;
	leaveForm('i_'+dropName);
	document.getElementById('drop_current_'+dropName).value = 0;
	current = 0;

}

function dropDown(n, specific, add_to_list) {
	
	dropName = n;
	document.onkeyup = KeyCheck;
	function KeyCheck(e) {
	
	   var KeyID = (window.event) ? event.keyCode : e.keyCode;
		
	   if (KeyID == 38) {
	   
		if (current > 0)
		current = current - 1;
			
	   } else if (KeyID == 40) {
		
		if (current < 11)
		current = current + 1;
		
	   } else if (KeyID == 13) {
		// return - the stopReturn will run
		
	   }
	   
	   if (KeyID != 13) {
	  	
			// continue with the ajax dropdown
			ajaxFormFill('f_'+dropName, 'i_'+dropName, 'search','', dropName, specific, add_to_list);
			if (KeyID == 38 || KeyID == 40) {
				document.getElementById('drop_current_'+dropName).value = current;
			} else {
				document.getElementById('drop_current_'+dropName).value = 0;
				current = 0;
			}
		
		//document.getElementById('f_interests').value = currentVal;
	   }
	   
	   //ajaxFormFill('f_interests', 'i_interests', 'search','', 5);
	  
	}
}
