var checkTimeOut = -1;
var getFunctionsUrl = "./ajax/suggest/suggest.php?keyword=";
var phpHelpUrl="";
var suggestionMaxLength = 50;
var debugMode = true;
var isKeyUpDownPressed = false;
var xmlHttpGetSuggestions = createXmlHttpRequestObject();
var inputNameAnterior = '';

function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

function handleGettingSuggestions() 
{
  //if the process is completed, decide what to do with the returned data
  if (xmlHttpGetSuggestions.readyState == 4) 
  {
    // only if HTTP status is "OK"
    if (xmlHttpGetSuggestions.status == 200) 
    { 
      try
      {
        // process the server's response
        updateSuggestions();
      }
      catch(e)
      {
        // display the error message
        displayError(e.toString()); 
      }  
    } 
    else
    {
      displayError("There was a problem retrieving the data:\n" + 
                   xmlHttpGetSuggestions.statusText);
    }       
  }
}

function getSuggestions(keyword, option) 
{  
  /* continue if keyword isn't null and the last pressed key wasn't up or 
     down */
  if(keyword != "" && !isKeyUpDownPressed)
  {          
     if(xmlHttpGetSuggestions)
     { 
       try
       {
         /* if the XMLHttpRequest object isn't busy with a previous
            request... */
         if (xmlHttpGetSuggestions.readyState == 4 || 
             xmlHttpGetSuggestions.readyState == 0) 
         {    
           var inputName = document.getElementById("inputName").value;
		   
           httpRequestKeyword = keyword;
           userKeyword = keyword;
           xmlHttpGetSuggestions.open("GET", 
                               getFunctionsUrl + encode(keyword) + '&opt=' + option, true);
                               

           xmlHttpGetSuggestions.onreadystatechange = 
                                               handleGettingSuggestions; 
           xmlHttpGetSuggestions.send(null);
         }
       }
       catch(e)

       {
         displayError("Can't connect to server:\n" + e.toString());
       }
     }    
  }
}

function updateSuggestions()
{
  // retrieve the server's response 
  var response = xmlHttpGetSuggestions.responseText;
//  alert (response); 
  // server error?
  if (response.indexOf("ERRNO") >= 0 
      || response.indexOf("error:") >= 0
      || response.length == 0)
    throw(response.length == 0 ? "Void server response." : response);
  // retrieve the document element
  response = xmlHttpGetSuggestions.responseXML.documentElement;
  // initialize the new array of functions' names
  nameArray = new Array();
  idArray = new Array();
  // check to see if we have any results for the searched keyword
 
  if(response.childNodes.length)
  {
    /* we retrieve the new functions' names from the document element as 
       an array */
    nameArray= xmlToArray(response.getElementsByTagName("name"));      
    idArray = xmlToArray(response.getElementsByTagName("id"));
  }
  // check to see if other keywords are already being searched for
  if(httpRequestKeyword == userKeyword)    
  {
    // display the results array
    displayResults(httpRequestKeyword, nameArray,idArray);
  }
}

function displayResults(keyword, results_array,idArray) 
{  
  var inputName = document.getElementById("inputName").value;
  
  // get the number of results from the cache
  suggestions = results_array.length;
  
  // display the results
  if(suggestions > 0)
  {
  		
	var oSelect = document.getElementById("keyword"+inputName);
	oSelect.options.length=0;
	
    // loop through all the results and generate the HTML list of results
    for (var i=0; i<suggestions; i++) 
    {  
      // retrieve the current function
      crtFunction = results_array[i];
      // set the string link for the for the current function 
      // to the name of the function
      crtFunctionLink = crtFunction;
      // replace the _ with - in the string link
      while(crtFunctionLink.indexOf("_") !=-1)
        crtFunctionLink = crtFunctionLink.replace("_","-");
		
		// Se agregan las opciones al combo correspondiente
		var optn = document.createElement("OPTION");
		optn.text = crtFunction;
		optn.value = idArray[i];
		oSelect.options.add(optn);
    }
  }
  if(inputName=='opcion1'){
	  selTec();
  }
}

function xmlToArray(resultsXml)
{
  // initiate the resultsArray
  var resultsArray= new Array();  
  // loop through all the xml nodes retrieving the content  
  for(i=0;i<resultsXml.length;i++)
    resultsArray[i]=resultsXml.item(i).firstChild.data;
  // return the node's content as an array
  return resultsArray;
}

function displayError(message)
{
  // display error message, with more technical details if debugMode is true
  alert("Error accessing the server! "+
        (debugMode ? "\n" + message : ""));
}

function encode(uri) 
{
  if (encodeURIComponent) 
  {
    return encodeURIComponent(uri);
  }
  if (escape) 
  {
    return escape(uri);
  }
}

function getPos(theElement)
{
  var positionX = 0;
  var positionY = 0;
  while (theElement != null)
  {
    positionX += theElement.offsetLeft;
    positionY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }
  return [positionX, positionY];
}