﻿// JScript File

var req;
var effect_1 = null;
var term = null;
var term_matched = null;
var query_id = null;
var user_id = null;
var searchdebug = 0;

var smartSearchImageDomain = 'http://maps.pella.com/images/smartsearch/';

var searchurl="http://maps.pella.com/searchtermexp.aspx?q=";
//var searchurl="http://maps.pella.com/searchtermexp.aspx?q=";

var clickurl="/pages/searchredirect.aspx";
var allsearchurl="/pages/searchresults.aspx";
var currentCategory = "";

var searchnodes;
var searchxmlDoc;
var timerid;
var event_timeout;
var total_nodes_received;
var total_nodes_rendered;
var bottom_shadow_exists = 0;
var received_xml;

var arrow_key_counter = -1;
var arrow_key_selected = 0;

function screenClick(evt)
{
    //alert("screen event");
    var evt = evt || window.event; // event object
	var target = evt.target || window.event.srcElement; // event target
	var targetID = target.getAttribute("id"); // event target id
	
	//alert(targetID);
	
    if ( targetID == "autoContainer" )
    {   alert(" auto container click ");
        wordBlur();
    }
    
    if ( targetID == "mainContainer" )
    {   //alert(" main container click ");
        dropDownBlurOnly();
        //wordBlur();
    }
	
    if ( (targetID == "completeScreen" || targetID == null || targetID =="" || targetID != "completeScreen") && (targetID != "word"&& targetID != "closeButton" ) )
    {
    dropDownBlurOnly();
        //alert(" complete screen click "+ document.getElementById("autocomplete").offsetHeight);
        if( document.getElementById("autocomplete").offsetHeight > 100 )
        {
        }
        else
        {
            //dropDownBlurOnly();
        }
        if( document.getElementById("autocomplete").offsetHeight == 0 )
        {
           // wordBlur();
        }
    }
    

    if ( targetID == "autocomplete" )
    {
        alert("in auto complete");
    }
}


function WordFocus()
{
    HideDiv("autocomplete");
	HideDiv("mainContainer");

    //HideDiv("bottom");
    HideDiv("debug_window");
    if ( document.getElementById('word').value == "Search" )
    {
        document.getElementById("word").setAttribute("class", "search");
	    //document.getElementById("word").focus();
	    document.getElementById('word').value = "";
	    document.getElementById("closeButton").style.visibility="hidden";
	}
	
}


function dropDownBlurOnly()
{
    HideDiv("autocomplete");
    HideDiv("mainContainer");
    if ( document.getElementById('word').value != "Search" )
    {
        document.getElementById("closeButton").style.visibility="visible";
    }
       if ( document.getElementById('word').value == "" )
    {
        wordBlur();
    }
    

    //HideDiv("bottom");
    HideDiv("debug_window");
}
function wordBlur()
{
    //ShowDiv("searchEmpty");
    //if(document.getElementById('word').value == "")
   // {
    //alert("wb");
    document.getElementById("word").setAttribute("class", "searchBlur");
    document.getElementById("word").value = "Search";
    document.getElementById("closeButton").style.visibility="hidden";

    HideDiv("autocomplete");
    HideDiv("mainContainer");

    //HideDiv("bottom");
    HideDiv("debug_window");
  
  

    //}
    
}


function CloseButtonClick()
{
    //alert("closebuttonclick");
    //document.getElementById("word").focus();
    document.getElementById("word").value = "";
    document.getElementById("closeButton").style.visibility="hidden";
    document.getElementById("word").focus();
    //HideDiv("mainContainer");
    resetMainDiv();
    //HideDiv("mainContainer");
   

}
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 searchAndHighlightDown()
{
    if (searchnodes.length > 0)
    {
        
        
        if (arrow_key_counter == searchnodes.length-1) 
        { }
        else
        {
            arrow_key_counter++;
        }
        arrow_key_selected = 1;
        var page_name = searchnodes[arrow_key_counter].getAttribute("PageName");
        //alert("here..."+arrow_key_counter+page_name);
        var cell = document.getElementById("autocomplete");
        var i = 0;
    
	    if ( cell.hasChildNodes() )
	    {

            turnOffPages();
            
            
		    for(i=0;i < cell.childNodes.length ;i++)
		    {
		       var found_node_count = cell.childNodes[i].getAttribute("id").indexOf(page_name);
		      if ( found_node_count > 0  && ( cell.childNodes[i].getAttribute("id").length == (found_node_count + page_name.length) ) )
		        { 
		            //turnOnPage(cell.childNodes[i].getAttribute("id"));
		            //alert(cell.childNodes[i].getAttribute("id"));
		            turnOnPage(cell.childNodes[i].getAttribute("id"));
		        }
		    }

        }
    
    }
    else
    {
        arrow_key_counter = -1;
        arrow_key_selected = 0;
    }
    

}

function searchAndHighlightUp()
{
    if (searchnodes.length > 0  )
    {
        //alert(arrow_key_counter);
        if (arrow_key_counter == 0 || arrow_key_counter == -1)
        {
            arrow_key_counter=-1;
            arrow_key_selected = 0;
            turnOffPages();
            
        }
        else
        {
            arrow_key_counter--;
        }
        //if (arrow_key_counter == 0 ) arrow_key_counter--;
        arrow_key_selected = 1;
        if(arrow_key_counter != -1)
        {
            var page_name = searchnodes[arrow_key_counter].getAttribute("PageName");
            //alert("here..."+arrow_key_counter+page_name);
            var cell = document.getElementById("autocomplete");
            var i = 0;
        
	        if ( cell.hasChildNodes() )
	        {

                turnOffPages();
                
                
		        for(i=0;i < cell.childNodes.length ;i++)
		        {
		           var found_node_count = cell.childNodes[i].getAttribute("id").indexOf(page_name);
		          if ( found_node_count > 0 && ( cell.childNodes[i].getAttribute("id").length == (found_node_count + page_name.length) )  )
		            { 
		               // alert(cell.childNodes[i].getAttribute("id")+ "  " + cell.childNodes[i].getAttribute("id").length);
		                //turnOnPage(cell.childNodes[i].getAttribute("id"));
		                //alert(cell.childNodes[i].getAttribute("id"));
		                turnOnPage(cell.childNodes[i].getAttribute("id"));
		            }
		        }

            }
         }
         else
         {
            arrow_key_selected = 0;
         }
    
    }
    else
    {
        arrow_key_counter = -1;
        arrow_key_selected = 0;
    }
    

}

function turnOffPages()
{

    var cell = document.getElementById("autocomplete");
    var i = 0;

    if ( cell.hasChildNodes() )
    {
        for(i=0;i < cell.childNodes.length ;i++)
        {
        if (cell.childNodes[i].getAttribute("id").indexOf("ContainerDiv") > 0 )
            turnOffPage(cell.childNodes[i].getAttribute("id"));
        }
    }
        
}

function pageClick(inURL)
{
    //element = document.getElementById(inID); 
    var aLink;
    aLink=inURL;
    var destlink = clickurl+"?url="+aLink+"&user_id="+user_id+"&term="+term+"&term_matched="+term_matched+"&query_id="+query_id+"&from_page="+window.location.pathname;
    if (aLink.indexOf("target=_blank")>0 )
    {
        window.open(destlink);
        
    }
    else
    {
        window.location=destlink;
    }


}

function turnOffPage(inID)
{
    
    element = document.getElementById(inID); 
    
    allelems= element.getElementsByTagName("div");
    for (i = 0; i < allelems.length; i++)
    {
    allelems[i].style.background = "#FFF";

    }
    
   element.style.background = "#FFF";
   removeDestinationURL();

 }


function turnOnPage(inID)
{   
    //alert(this.pageurl);
    element = document.getElementById(inID); 
    var aLink;
    var capLength=38;
    //aLink=searchnodes[this.id.replace("ContainerDiv","")].getAttribute("PageURL");
    //aLink= this.offsetTop;
    aLink=searchnodes[inID.substring(0,1)].getAttribute("PageName") + "  " + searchnodes[inID.substring(0,1)].getAttribute("PageName").length;
    //aLink=searchnodes[this.id.replace("ContainerDiv","")].getAttribute("PageName").substring(0,capLength);
    allelems= element.getElementsByTagName("div");
    for (i = 0; i < allelems.length; i++)
    {

    allelems[i].style.background = "#CCC";

    }
    
    element.style.background = "#CCC";
    //var destlink = clickurl+"?url="+aLink+"&user_id="+user_id+"&term="+term+"&term_matched="+term_matched+"&query_id="+query_id
    showDestinationURL(aLink);
    
}

function SendQuery(key, event)
{
    //alert(event.keyCode);
    
    if (event.keyCode == 40 )
    {
    // down arrow clicked
    searchAndHighlightDown();
    return;
    
    
    }
    
    if (event.keyCode == 38 )
    {
    // uparrow clicked
    searchAndHighlightUp();
    return;
    
    
    }

    if (event.keyCode == 13)
    {
        //alert(key);
        if (arrow_key_selected == 1)
        {
            //alert("HERE");
           // var cell = document.getElementById("autocomplete");
            pageClick(searchnodes[arrow_key_counter].getAttribute("PageURL"));
        }
        else
        {
            var destlink = allsearchurl+"?q="+key+"&from_page="+window.location.pathname;
            window.location=destlink;
        }
        //allsearchurl + "?user_id="+user_id+"&term="+term+"&term_matched="+term_matched+"&query_id="+query_id + "&q=" + term 
    }
    
    arrow_key_counter = -1;
    arrow_key_selected = 0;
    
	if(key=="")
	{   
	    HideDiv("autocomplete");
	    HideDiv("mainContainer");
	    bottom_shadow_exists = 0;
       // resetMainDiv();
       // HideDiv("bottom");
	    document.getElementById("closeButton").style.visibility="hidden";
	    if(searchdebug==1){
	                HideDiv("debug_window");
	                }
		return;
		
		}
	currentCategory = "";
	Initialize();
    //ShowDiv("autocomplete");
    //ShowDiv("mainContainer");

    
    
    if(searchdebug==1) ShowDiv("debug_window");
	                
	if(req!=null)
	{
		//req.onreadystatechange = Process;
		//req.open("GET", url+key, true);
        //	req.send(null); // removed for dynamic script request
        var jsreq  = searchurl+key+"&from_page="+window.location.pathname;; 
	    // Create a new request object
	    bObj = new JSONscriptRequest(jsreq); 
	    // Build the dynamic script tag
	    bObj.buildScriptTag(); 
	    // Add the script tag to the page
	    bObj.addScriptTag();
        //var progressElement=document.getElementById("progressBar");
        if (bObj != null)
        {
        document.getElementById("progressBar").style.visibility="visible";
        }
        document.getElementById("closeButton").style.visibility="hidden";
        

	}

}
function loadToXML(inXML)
{
    //alert(inXML);
    received_xml = inXML;
    //bObj.removeScriptTag(); 
    Process();
}
function Process()
{
          
                     var A;
                      if(window.ActiveXObject)
                       {
                         A=new ActiveXObject("Microsoft.XMLDOM");
                         A.async="false";
                         A.loadXML(received_xml);
                        }
                      else
                       {
                        var C=new DOMParser();
                        A=C.parseFromString(received_xml,"text/xml");
                        }

					searchxmlDoc=A;
					
					
					
					
					
					
					
                    
                    
                        ShowDiv("autocomplete");
                        ShowDiv("mainContainer");
                    
                    
                    try
                    {
                    term=searchxmlDoc.getElementsByTagName('term')[0].childNodes[0].nodeValue;
                    term_matched=searchxmlDoc.getElementsByTagName('term_matched')[0].childNodes[0].nodeValue;
                    query_id=searchxmlDoc.getElementsByTagName('query_id')[0].childNodes[0].nodeValue;
                    user_id=searchxmlDoc.getElementsByTagName('user_id')[0].childNodes[0].nodeValue;
                    }
                    catch(ex)
                    {
                    HideDiv("debug_window");
                    term_matched="";
                    
                    }
                   // termmatched=searchxmlDoc.getElementsByTagName('termmatched')[0].childNodes[0].nodeValue;
					//nodes=req.responseXML.documentElement.getElementsByTagName("match");
					if(searchdebug==1)
					{
					    showTermsMatched();
					}
					
					searchnodes=searchxmlDoc.getElementsByTagName('match');
					//alert(searchnodes.length);
					if (searchnodes.length == 0)
					{
					 //alert("HERE...");
					 resetMainDiv();
					 var ni = document.getElementById('autocomplete');
                     var newdiv = document.createElement('div');
                     newdiv.setAttribute("id","NothingFound");
                     newdiv.innerHTML="<div class=\"noresultsBox\"><div id=\"noresultsContainer\"><div class=\'nrimageContainer\'><img name=\'nrImg\'  onmouseover=\'nrImageOver()\' onmouseout=\'nrImageOut()\' src=\'/PublishingImages/arrow.jpg\'/></div><div class=\"noresults\"><a id=\'nref\' onmouseover=\'nrImageOver()\' onmouseout=\'nrImageOut()\' href=\""+ allsearchurl + "?user_id="+user_id+"&term="+term+"&term_matched="+term_matched+"&query_id="+query_id + "&q=" + term + "\">No Suggestions. View all Search Results</a></div></div></div>";
		             ni.appendChild(newdiv);

					}
					else
					{
					  total_nodes_received = computeTotalNodesReceived(searchnodes);
					  total_nodes_rendered = computeTotalNodesRendered(document.getElementById("autocomplete"));
					  //alert(total_nodes_received+ "  " + total_nodes_rendered);
					  //alert(total_nodes_rendered);
					  
					  
					
					            if( (total_nodes_rendered == 0 ) && (total_nodes_received > 0) )
					            {
					                    for (i = 0; i < searchnodes.length; i++) {
					                    //currentCategory = searchnodes[i].getAttribute("PageCategory");
					                    if (currentCategory != searchnodes[i].getAttribute("PageCategory"))
					                        {   
					                            currentCategory = searchnodes[i].getAttribute("PageCategory");
					                            addCategory(searchnodes[i].getAttribute("PageCategory") );
					                        }

					                    addElement(i,cleanHTML(searchnodes[i].getAttribute("PageName")), cleanHTML(searchnodes[i].getAttribute("Description")) , searchnodes[i].getAttribute("ImageURL"), searchnodes[i].getAttribute("PageURL"));

				                        }
                    				 
				                         if(searchnodes.length>0)
				                         {   
                                            //addFooter(term);
                                         }
                                 }
                                 else
                                 {
                                    // New logic to replace DIV's for smoother display.
                                    if ( total_nodes_rendered > total_nodes_received )
                                    {
                                        //remove some divs
                                        var diff = total_nodes_rendered - total_nodes_received;
                                        //alert(" decrease by : " + diff );
                                        removeDivs(diff, total_nodes_rendered, total_nodes_received);
                                    }
                                    else
                                    {
                                        //add some divs
                                        var diff = total_nodes_received - total_nodes_rendered;
                                        //alert(" increase by : " + diff );
                                        addDivs(diff, total_nodes_rendered, total_nodes_received);
                                    
                                    }
                                    
                                     currentCategory = "";
                                     //alert(nodes.length);
                                     var divCounter = 0;
                                     for (i = 0; i < searchnodes.length; i++) {
                                     
					                    //currentCategory = nodes[i].getAttribute("PageCategory");
					                // alert(document.getElementById("autocomplete").childNodes[divCounter].getAttribute("id") + "    " + nodes[i].getAttribute("PageName") + "    " + nodes[i].getAttribute("PageCategory") + currentCategory);
            					     
					                    if (currentCategory != searchnodes[i].getAttribute("PageCategory"))
					                        {   
					                            if (document.getElementById("autocomplete").childNodes[divCounter].getAttribute("id").indexOf("CategoryDiv") > 0 )
					                            {
					                            //alert("loop 1");
					                            CheckCategoryTypeAndValue(i, document.getElementById("autocomplete").childNodes[divCounter], searchnodes[i].getAttribute("PageCategory") );
            					                
					                            divCounter++;
					                            }
					                            else
					                            {
					                            //alert("loop 2");
					                              replaceCategory(document.getElementById("autocomplete").childNodes[divCounter], searchnodes[i].getAttribute("PageCategory"));
					                              divCounter++;
					                            }
					                            //addCategory(searchnodes[i].getAttribute("PageCategory") );
					                        }
                                            currentCategory = searchnodes[i].getAttribute("PageCategory");
                                            
					                        checkAndreplaceElement(i,document.getElementById("autocomplete").childNodes[divCounter], cleanHTML(searchnodes[i].getAttribute("PageName")), cleanHTML(searchnodes[i].getAttribute("Description")) , searchnodes[i].getAttribute("ImageURL"), searchnodes[i].getAttribute("PageURL"));
                                            divCounter++;
            				            
            				            
        				             }
            				             
                                 
                                 
                                 }
                     
                     if(searchnodes.length>0)
				             {   
                                addFooter(term);
                             }
                             
                  } 
                  

                      addBottomShadow();
            
    	

		
		setTimeout("clearProgress()",600);

			
}

function addDivs(inCount, rendered, received)
{
    var cell = document.getElementById('autocomplete');
   
    var i = 0;
    
    if( received > rendered )
    {
    //alert(received+ "  " + rendered+ "   " + inCount);
        for(i=0;i<inCount;i++)
        {
            var newdivs = document.createElement('div');
            newdivs.setAttribute('id',"EmptyDiv"+i);
            cell.appendChild(newdivs);
        }
    
    }

}

function removeDivs(inCount, rendered, received)
{
	var cell = document.getElementById("autocomplete");
    var i = received;
    
    if ( rendered > received ) 
    {
	    if ( cell.hasChildNodes() )
	    {

		    for(i=0;i < inCount ;i++)
		    {   //alert(received+ "  " + rendered + "  " + "  " + inCount + "  " + i + "   " + cell.childNodes.length);
		        //if (cell.childNodes[i].getAttribute("id") != "BottomFooter" )
		       //{
		       //alert(cell.firstChild.getAttribute('id'));
		       //if(cell.firstChild.getAttribute('id') == 'bottom')
		       //{}
		       //else
		       //{ 
		       
			        cell.removeChild( cell.firstChild );    
			     //   }
			        
		      // }  
		    } 
	    }
	 }
    
}

function CheckCategoryTypeAndValue(pos, inRenderedDiv, inCategory )
{
    if ( inRenderedDiv.getAttribute("id").indexOf("CategoryDiv") > 0 )
    {
        // it's a category div, check name
        if ( inRenderedDiv.getAttribute("id").replace("CategoryDiv", "") == inCategory )
        {
        }
        else
        {
            replaceCategory(inRenderedDiv, inCategory);
        }
    }
    else
    {
        replaceCategory(inRenderedDiv, inCategory);
    }
    

}

function replaceElement(num, inRenderedDiv, title, copy, image, pageurl) 
{
  var divIdName = num+'ContainerDiv'+title;
  var divIdNameOver = 'my'+title+'DivOver';
  var copyAlt ="";
  
  inRenderedDiv.setAttribute('id',divIdName);

  inRenderedDiv.onmouseover = containerOverIE;
  inRenderedDiv.onmouseout = containerOutIE;
  inRenderedDiv.onclick = containerClickIE;

  if (copy.length>=110)
  {
  copyAlt = '<img id=\"transparency\" src=\"/PublishingImages/spacer.gif\" title=\"'+cleanHTML(copy)+'\" Alt=\"'+cleanHTML(copy)+'\" />'
  }
  inRenderedDiv.innerHTML = ' <div id=\"container\">  <div id=\'image\'><img src=\'' + smartSearchImageDomain + image + '\' padding="0" width="69" alt=\"'+title+'\" title=\"'+title+'\" height="57"/></div>' + copyAlt + '<div id=\'dbox\' class=\'descriptionbox\'>' + '<div id=\"tbox\" class=\'title\'>' + buildEllipsis(title, 45) + '</div>' + '<div class=\'clearbox\'/><div class=\'copy\' >' + buildEllipsis(copy, 110) + '</div>' + '<input type=hidden name=\"hbox\" value=\"'+pageurl+'\"/>'  +'</div></div>';
  

}

function checkAndreplaceElement(num, inRenderedDiv, title, copy, image, pageurl) {
 
     if ( inRenderedDiv.getAttribute("id").indexOf("ContainerDiv") > 0 )
    {
        // it's a category div, check name
        if ( inRenderedDiv.getAttribute("id").replace(num+"ContainerDiv", "") == title)
        {
        }
        else
        {
            replaceElement(num, inRenderedDiv, title, copy, image, pageurl) ;
        }
    }
    else
    {
        replaceElement(num, inRenderedDiv, title, copy, image, pageurl) ;
    }
 
 
}

function computeTotalNodesRendered(inNodes)
{   // remove one for the footer
   // return inNodes.childNodes.length-1;
      return inNodes.childNodes.length;
}

function computeTotalNodesReceived(inNodes)
{ 
    var tempCategory="";
    var counter = 0;
    var i =0
    for (i = 0; i < inNodes.length; i++) {
    if (tempCategory != inNodes[i].getAttribute("PageCategory"))
					    {   
					        tempCategory = inNodes[i].getAttribute("PageCategory");
					        counter++;
					    }
	}
    
    counter = counter + inNodes.length;
    
    return counter;

}

function clearProgress()
{
    //clearInterval(timerid);
    document.getElementById("progressBar").style.visibility="hidden";
    document.getElementById("closeButton").style.visibility="visible";
    if ( document.getElementById('word').value == "" )
    {
     document.getElementById("closeButton").style.visibility="hidden";
    }
    
    
    
}

function cleanHTML(inString)
{
    var retString;
    retString = inString.replace(/&apos;/i, "\u0027");
    retString = retString.replace('\"', "&quot;");
    return retString;
}

function addElement(num, title, copy, image, pageurl) {
  var ni = document.getElementById('autocomplete');

  var newdiv = document.createElement('div');
  //var newdivover = newdiv.createElement('div');
  var divIdName = num+'ContainerDiv'+title;
  var divIdNameOver = 'my'+title+'DivOver';
  var copyAlt ="";
  
  newdiv.setAttribute('id',divIdName);
  //newdiv.setAttribute('pageurl',pageurl);
  newdiv.onmouseover = containerOverIE;
  newdiv.onmouseout = containerOutIE;
  newdiv.onclick = containerClickIE;
  //newdiv.setAttribute("onmouseover", "test()");
  //newdiv.setAttribute('onmouseover', 'containerOver(this.id, \"'+pageurl+'\")');
  //newdiv.setAttribute('onclick', 'containerClick(this.id, \''+pageurl+'\')');
  //newdiv.setAttribute('onmouseout', 'containerOut(this.id)');
  if (copy.length>=110)
  {
  copyAlt = '<img id=\"transparency\" src=\"/PublishingImages/spacer.gif\" title=\"'+cleanHTML(copy)+'\" Alt=\"'+cleanHTML(copy)+'\" />'
  }
  newdiv.innerHTML = '<div id=\"container\">  <div id=\'image\'><img src=\'' + image + '\' padding="0" width="69" alt=\"'+title+'\" title=\"'+title+'\" height="57"/></div>' + copyAlt + '<div id=\'dbox\' class=\'descriptionbox\'>' + '<div id=\"tbox\" class=\'title\'>' + buildEllipsis(title, 45) + '</div>' + '<div class=\'clearbox\'/><div class=\'copy\' >' + buildEllipsis(copy, 110) + '</div>' + '<input type=hidden name=\"hbox\" value=\"'+pageurl+'\"/>'  +'</div></div>';
  //newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'#\' onclick=\'removeElement('+divIdName+')\'>Remove the div "'+divIdName+'"</a>';
  ni.appendChild(newdiv);
}
function test()
{
alert("test");
}

function DoSmartSearchTag()
{
	// get search term obj
	objSearchTerm = document.getElementById('word');
	if(objSearchTerm != null)
		pageTracker._trackPageview('/smartsearch/' + objSearchTerm.value);
}

function KeyHandlerSearchBox(evt) 
{
	// run search only for Enter key
	evt = (evt) ? evt : ((window.event) ? window.event : "")
	if (evt)
		if (evt.keyCode == 13)
            return false;
}


function containerClickIE()
{
    DoSmartSearchTag();

    element = document.getElementById(this.id); 
    var aLink;
    aLink=searchnodes[this.id.substring(0,1)].getAttribute("PageURL");
    var destlink = clickurl+"?url="+aLink+"&user_id="+user_id+"&term="+term+"&term_matched="+term_matched+"&query_id="+query_id+"&from_page="+window.location.pathname;
    if (aLink.indexOf("target=_blank")>0 )
    {
        window.open(destlink);
    }
    else
    {
        window.location=destlink;
    }
}
function containerClick(sentID, aLink)
{
    
    var destlink = clickurl+"?url="+aLink+"&user_id="+user_id+"&term="+term+"&term_matched="+term_matched+"&query_id="+query_id;
    //alert(destlink);
    window.location=destlink;
}

function containerOverIE()
{   
    //alert(this.pageurl);
    turnOffPages();
    arrow_key_counter=-1;
    arrow_key_selected=0;
    element = document.getElementById(this.id); 
    var aLink;
    var capLength=38;
    //aLink=searchnodes[this.id.replace("ContainerDiv","")].getAttribute("PageURL");
    //aLink= this.offsetTop;
    aLink=searchnodes[this.id.substring(0,1)].getAttribute("PageName") + "  " + searchnodes[this.id.substring(0,1)].getAttribute("PageName").length;
    //aLink=searchnodes[this.id.replace("ContainerDiv","")].getAttribute("PageName").substring(0,capLength);
    allelems= element.getElementsByTagName("div");
    for (i = 0; i < allelems.length; i++)
    {

    allelems[i].style.background = "#CCC";

    }
    
    element.style.background = "#CCC";
    //var destlink = clickurl+"?url="+aLink+"&user_id="+user_id+"&term="+term+"&term_matched="+term_matched+"&query_id="+query_id
    showDestinationURL(aLink);
    //th_rolloff(1);
    //event_timeout=true;
    if(searchnodes[this.id.substring(0,1)].getAttribute("Description").length > 80 || searchnodes[this.id.substring(0,1)].getAttribute("PageName").length > 37)
    {
        event_timeout=false;
    }
}

function rollIn(offsetY, display)
{
   //th_rolloff(1);
   //setTimeout("th_roll("+offsetY+",1,\'"+display+"\')",1000);
   //th_roll(offsetY, 1,display);

}

function containerOutIE()
{
    element = document.getElementById(this.id); 
    
    allelems= element.getElementsByTagName("div");
    for (i = 0; i < allelems.length; i++)
    {
    allelems[i].style.background = "#FFF";

    }
    
   element.style.background = "#FFF";
   removeDestinationURL();
   //th_rolloff(1);
   if(searchnodes[this.id.substring(0,1)].getAttribute("Description").length > 80|| searchnodes[this.id.substring(0,1)].getAttribute("PageName").length > 37)
    {
    //th_rolloff(1);
    }
 }

function containerOver(sentID, aLink)
{   
    //alert(sentID);
    element = document.getElementById(sentID); 
    
    allelems= element.getElementsByTagName("div");
    for (i = 0; i < allelems.length; i++)
    {
    allelems[i].style.background = "#CCC";

    }
    
    element.style.background = "#CCC";
    //var destlink = clickurl+"?url="+aLink+"&user_id="+user_id+"&term="+term+"&term_matched="+term_matched+"&query_id="+query_id
    showDestinationURL(aLink);
    //element.style.cursor="default;";
 
    //element.style.visibility="visible";
    //element.style.border="2px solid";
    //dbox.style.background ="#CCC";
}


function containerOut(sentID)
{
    element = document.getElementById(sentID); 
    
    allelems= element.getElementsByTagName("div");
    for (i = 0; i < allelems.length; i++)
    {
    allelems[i].style.background = "#FFF";

    }
    
   element.style.background = "#FFF";
   removeDestinationURL()
   //element.style.cursor="pointer;";
   // element.style.visibility="hidden";
   //  element.style.border="0px solid";
}

function replaceCategory(inRenderedDiv, categoryName) 
{
    //alert(categoryName);
  var divIdName = categoryName+'CategoryDiv';
  inRenderedDiv.setAttribute('id',divIdName);
   inRenderedDiv.onmouseover = null;
  inRenderedDiv.onmouseout = null;
  inRenderedDiv.onclick = null;
  inRenderedDiv.innerHTML = "<div id=\'containerCategory\'>" +  "<div id=\'titleCategory\' >"+categoryName+"</div>"+ "</div>";

}

function addCategory(categoryName) {
  var ni = document.getElementById('autocomplete');
  //var numi = document.getElementById('theValue');
  //var num = (document.getElementById('theValue').value -1)+ 2;
  //numi.value = num;
  var newdiv = document.createElement('div');
  var divIdName = categoryName+'CategoryDiv';
  newdiv.setAttribute('id',divIdName); 
  newdiv.innerHTML = "<div id=\'containerCategory\'>" +  "<div id=\'titleCategory\' >"+categoryName+"</div>"+ "</div>";
  //newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'#\' onclick=\'removeElement('+divIdName+')\'>Remove the div "'+divIdName+'"</a>';
  ni.appendChild(newdiv);
}

function addFooter(Term) {
  var ni = document.getElementById('autocomplete');
  var newdiv = document.createElement('div');
  var divIdName = 'BottomFooter';
  newdiv.setAttribute('id',divIdName);
  newdiv.onclick = footerClickIE;
  newdiv.onmouseover=rollImageOver;
  newdiv.onmouseout=rollImageOut;
  newdiv.innerHTML = "<div id=\'footer\'>" +  "<div id=\'footerContainer\'><div id=\'imageContainer\'><img name=\'footerImg\' onmouseover=\'rollImageOver()\' onmouseout=\'rollImageOut()\' src=\'/PublishingImages/arrow.jpg\'/></div><div class=\'textFooter\' >"+"<a href=\'"+ allsearchurl + "?user_id="+user_id+"&term="+term+"&term_matched="+term_matched+"&query_id="+query_id+"&q=" + Term + "\' id=\'ref\'  onmouseover=\'rollImageOver()\' onmouseout=\'rollImageOut()\' >"+"View All Results"+"</a></div></div>"+ "</div>";
  ni.appendChild(newdiv);
}

function footerClickIE()
{
    var destlink = allsearchurl+"?q="+term+"&user_id="+user_id+"&term="+term+"&term_matched="+term_matched+"&query_id="+query_id+"&from_page="+window.location.pathname;

    window.location=destlink;

}

function addBottomShadow()
{
  var finaldiv = document.getElementById('mainContainer');
 
  var exists = 0;
  //alert("here...");
  if ( finaldiv.hasChildNodes() )
	{
	 for(var i=0;i<finaldiv.childNodes.length;i++)
	 {
	// alert("here..."+ finaldiv.childNodes.length);
	   try {
	    //alert(finaldiv.childNodes[i].getAttribute('id'));
	    if ( finaldiv.childNodes[i].getAttribute('id') == 'bottom' )
	    {
	    //alert("exists");
	     exists=1;
	    }
	       }
	    catch(e)
	    {
	    //alert(i);
	    }
	    
	 }
	 }

  if(exists==0){
  var finalnewdiv = document.createElement('div');
  divIdName = 'bottom';
  finalnewdiv.setAttribute('id',divIdName);
  var maindiv = document.getElementById('mainContainer');
  maindiv.appendChild(finalnewdiv);
  }
}

function rollImageOver()
{
    document.footerImg.src ="/PublishingImages/arrowH.jpg";
    document.getElementById('ref').style.color = "#333333";
   
}
function rollImageOut()
{
    document.footerImg.src ="/PublishingImages/arrow.jpg";
    document.getElementById('ref').style.color = "#993300";
  
}

function nrImageOver()
{
    document.nrImg.src ="/PublishingImages/arrowH.jpg";
    document.getElementById('nref').style.color = "#333333";
   
}
function nrImageOut()
{
    document.nrImg.src ="/PublishingImages/arrow.jpg";
    document.getElementById('nref').style.color = "#993300";
  
}

function showTermsMatched(){
if(searchdebug==1){
    var dwindow = document.getElementById("debug_window");
    dwindow.innerHTML="";
    dwindow.innerHTML+= "" + "<b> Term Entered : " + term + "</b>";
    dwindow.innerHTML+= "<br>" + "<b> Term Matched : " + term_matched + "</b>";
    dwindow.innerHTML+= "<br>" + "<b> Destination URL : " + "" + "</b>";
    dwindow.innerHTML+= "<br>" + "<b> User ID : " + user_id + "</b>";
    dwindow.innerHTML+= "<br>" + "<b> Query ID : " + query_id + "</b>";
    ShowDiv("debug_window");
    }
    //alert(term_matched);
}

function showDestinationURL(aLink){
if (searchdebug==1){
    var dwindow = document.getElementById("debug_window");
    dwindow.innerHTML="";
    dwindow.innerHTML+= "" + "<b> Term Entered : " + term + "</b>";
    dwindow.innerHTML+= "<br>" + "<b> Term Matched : " + term_matched + "</b>";
    dwindow.innerHTML+= "<br>" + "<b> Destination URL : " + aLink + "</b>";
    dwindow.innerHTML+= "<br>" + "<b> User ID : " + user_id + "</b>";
    dwindow.innerHTML+= "<br>" + "<b> Query ID : " + query_id + "</b>";
    ShowDiv("debug_window");
    }
    //alert(term_matched);
}

function removeDestinationURL(){
    showTermsMatched();
}

function offTermsMatched(){
    var dwindow = document.getElementById("debug_window");

    HideDiv("debug_window");
}

function buildEllipsis(titleLink, capLength){
    var retStr = "";
    //var capLength=37;
    var capLenString;
    //retStr = "<a onMouseOver='showDestinationURL(\""+aLink+"\")' onMouseOut='removeDestinationURL()' href=\""+clickurl+"?url="+aLink+"&user_id="+user_id+"&term="+term+"&term_matched="+term_matched+"&query_id="+query_id+"\" alt=\"" + "term matched : " + term_matched + "\">"+titleLink+"</a>";
    if(titleLink.length>=capLength)
    {
        capLenString = titleLink.substring(0,capLength);
        retStr = capLenString.substring(0,capLenString.lastIndexOf(" "));
        
        
        
        if(retStr.charAt(retStr.length-1)== ",")
            {
                retStr = retStr.substring(0,retStr.length-1)+"...";
            }
            
            else 
            {
                retStr = retStr+"...";
            }
            
            
    }
    else
    {
        retStr = titleLink;
    }
    return retStr;
}

function removeElement(divNum) {
  var d = document.getElementById("autocomplete");
  var olddiv = document.getElementById(divNum);
  d.removeChild(olddiv);
}

function removeBottomElement(divNum) 
{
  var d = document.getElementById("mainContainer");
  var olddiv = document.getElementById(divNum);
  try
  {
    d.removeChild(olddiv);
  }
  catch(err)
  {
  }
  
}


function resetMainDiv()
{
	var cell = document.getElementById("autocomplete");

	if ( cell.hasChildNodes() )
	{
		while ( cell.childNodes.length >= 1 )
		{
			cell.removeChild( cell.firstChild );       
		} 
	}

}

function ShowDiv(divid)
{
   if (document.layers) document.layers[divid].visibility="show";
   else document.getElementById(divid).style.visibility="visible";
}

function HideDiv(divid)
{
   if (document.layers) document.layers[divid].visibility="hide";
   else document.getElementById(divid).style.visibility="hidden";
}
