// Copyright 2006 WildStorms Digital.
// All Rights Reserved
//
// Ajax prototype
//
// Author: Ng Teng Yong
//


var req;			//xmlhttprequest object

var overcolor = "suggestdivon";
var outcolor = "suggestdivout";

//initialize cross browser xmlhttprequest object
function Initialize()
{
	try
	{
		req=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			req=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			req=null;
		}
	}

	if(!req && typeof XMLHttpRequest!="undefined")
	{
		req=new XMLHttpRequest();
	}
}

function ShowDiv(divid)
{
	if (document.layers) 
		document.layers[divid].visibility="show";
	else
	{ 
		document.getElementById(divid).style.visibility="visible";
		document.getElementById(divid).style.borderStyle="solid";
		document.getElementById(divid).style.borderWidth="1px";
	}
}

function HideDiv(divid)
{
	if (document.layers) 
		document.layers[divid].visibility="hide";
	else 
	{
		document.getElementById(divid).style.visibility="hidden";
		document.getElementById(divid).style.borderStyle="none";
	}
}

function movein(which)
{
  which.className = overcolor;
}

function moveout(which)
{
  which.className = outcolor;
}

//tbid - Textbox id
function press(link)
{
window.location=link; 
  
}

getObject =function(obj) {
  var theObj;
  if(document.all) {
    if(typeof obj=="string") {
      return document.all(obj);
    } else {
      return obj.style;
    }
  }
  if(document.getElementById) {
    if(typeof obj=="string") {
      return document.getElementById(obj);
    } else {
      return obj.style;
    }
  }
  return null;
}

getNodes = function(divName){
   //find selected
  var parent = getObject(divName);
  return parent.childNodes;
}

getSelectedNode = function(nodes){
   var selected = -1;
    
    for (var i=0;i<nodes.length;i++){
      if (nodes[i].className == overcolor){
        //this is the one
        selected = i;
        break;
       }
  }
  
  return selected;
}

moveSelection = function(direction,thatid){

  
  var nodes = getNodes(thatid);
  
 
  var selected = getSelectedNode(nodes);
  
  switch (direction){
    
    case "down":
     
      
      if (nodes.length>selected +1){

         
        for (var i=0;i<nodes.length;i++){
          
          if (i == selected +1){
              
              nodes[i].className = overcolor;

          }else{
          
            nodes[i].className =outcolor;
          
          }
        }
      
      }
      
     break;
    case "up":
      for (var i=0;i<nodes.length;i++){
        if (i == selected -1){
          nodes[i].className = overcolor;
        }else{
          nodes[i].className =outcolor;
        }
      }
      
  } 
  

  
}


setCarratToEnd = function(that){


  var pos = that.value.length;
      
  if (that.createTextRange) {

         var oRange = that.createTextRange(); 
         oRange.moveStart("character", pos); 
         oRange.moveEnd("character", pos);      
         oRange.select();

     //use setSelectionRange() for Mozilla
     } else if (that.setSelectionRange) {
         that.setSelectionRange(pos, pos+1);
  }     

}

checkForReturn = function (e,dd_,that){

  if (e.keyCode ==13){
         var nodes = getNodes(dd_);
          
         
        var selected = getSelectedNode(nodes);
        
        if (selected > -1){

          var val = nodes[selected];
		  window.location= val.attributes["target"].value;
          
        }
        return;
    }
}


//SendQuery function sends request to server asyncrhonously.
function SendQuery(arg,that,dd_,baseurl,e)
{
    key = that.value;
    
    if (e.keyCode ==40){
      moveSelection('down',dd_);
      setCarratToEnd(that);
  
      return;
    }else if (e.keyCode ==38){
      moveSelection('up',dd_);
            setCarratToEnd(that);
      return;
    }else if (e.keyCode ==13){
      return;
    }
    
    if (key.lastIndexOf(",") >0){
      key = key.substr(key.lastIndexOf(",")+1,key.length - key.lastIndexOf(","));
    }
    

	Initialize();
 //get the url of the current web page
	
	//check if other HTTP GET argument exist,if yes use "?" else use "&" to concatenate http request string
		var url=baseurl+key; //the get argument must be unique for each control.arg is a unique variable of a web page

	   
	$.getJSON(url, function(data) {

		   var myHtml = "";
		   $.each(data, function(key, val) {

				
				myHtml += "<div class='suggestdivout'  onmouseover='movein(this)' target='"+val+"' onmouseout='moveout(this)' onclick=\"press('"+val+"')\">"+key+"</div>";
                
        
				var xsl_tags="<xsl:stylesheet><xsl:template match=\"/\"> ";
				xsl_tags+="     <xsl:for-each select=\"response/suggestion\">";
				xsl_tags+="       <div class='suggestdivout'  onmouseover='movein(this)' onmouseout='moveout(this)' onclick=\"press('"+val+"')\"><xsl:value-of select=\"word\" /></div>";
				 xsl_tags+="      </xsl:for-each>";
				xsl_tags+=" </xsl:template>";
				xsl_tags+="</xsl:stylesheet>";
		  
		  });
			getObject(dd_).innerHTML=myHtml;
			ShowDiv(dd_);//show drop down area
		  
		  
		  
	});
}

function Process(dd,that,words)
{

	if (req.readyState == 4)
	{
		if (req.status == 200)// only if "OK"
		{
			if(req.responseText=="")
				HideDiv(dd);//hide drop down area
			else
			{
        
        var response = req.responseXML.documentElement;
        var words = response.getElementsByTagName('word');
        
        var myHtml = "";
        
       
//				var xml = xmlParse(req.responseText);
//        var xslt = xmlParse(xsl_tags);
//				var html = xsltProcess(xml, xslt);
        if (myHtml=="")
					HideDiv(dd);
				else	
          getObject(dd).innerHTML=myHtml;
					
			}
		}
		else
		{
			getObject(dd).innerHTML="There was a problem retrieving data:<br>"+ req.statusText;
		}
	}
}


