var magComing = false;
var parentDiv = document.getElementById("parentSearchDiv");
var parentIFrameDiv = document.createElement("Div");

parentIFrameDiv.innerHTML = "<iframe id='ifrmHideSearch' src='about:blank' scrolling='no' frameborder='0' style='position:absolute;width:150px;height:120px;top:100;left:-3000;border:none;display:block;z-index:99999'></iframe>";
parentDiv.appendChild(parentIFrameDiv);

	function showMagnify( event, query ) {
		getQuery( event, query ) ;

		//		return escapeSearch( event, getQuery( query ) );
	}


	function cancelMag() {

		magComing = false;
	}

  var onSearch = false;
  
  function escapeSearch( event, returnXml ) {

	  setTimeout("closeSearchDiv()",2000);
	  
   }

   function closeSearchDiv() {
	if ( !onSearch ) {
		var oDiv = document.getElementById("keyWord");
		if ( oDiv != null ) {
			parentDiv.removeChild( oDiv );
		}

		var ifrm1 = document.getElementById("ifrmHideSearch");
		if ( ifrm1 != null ) {
			ifrm1.style.top = 0 + "px";
			ifrm1.style.left = -1000;
		}
	} else {
		setTimeout("closeSearchDiv()",1000);
	}
   }


  function getMouseCoord(e) {

            var mousex,mousey;

            if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)

            if (e)
            {
                    if (e.pageX || e.pageY)
                    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
                    mousex = e.pageX;
                    mousey = e.pageY;
                    }
                    else if (e.clientX || e.clientY)
                    { // works on IE6,FF,Moz,Opera7
                    // Note: I am adding together both the "body" and "documentElement" scroll positions
                    //       this lets me cover for the quirks that happen based on the "doctype" of the html page.
                    //         (example: IE6 in compatibility mode or strict)
                    //       Based on the different ways that IE,FF,Moz,Opera use these ScrollValues for body and documentElement
                    //       it looks like they will fill EITHER ONE SCROLL VALUE OR THE OTHER, NOT BOTH
                    mousex = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
                    mousey = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
                    }
            }
            return mousex+","+mousey;
    }


    function cInclude(id, url) {
var req = false;
// For Safari, Firefox, and other non-MS browsers
if (window.XMLHttpRequest) {
        try {
        req = new XMLHttpRequest();
        } catch (e) {
        req = false;
        }
} else if (window.ActiveXObject) {
        // For Internet Explorer on Windows
        try {
        req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
        try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
                req = false;
        }
        }
}


if (req) {
        // Synchronous request, wait till we have it all
        req.open('GET', url, false);
        req.send(null);
	if ( req.responseText.indexOf("cannot") != -1 ) {
		return "";
	} else {
       		 return req.responseText;
	}

} else {
        element.innerHTML =
"Sorry, your browser does not support " +
        "XMLHTTPRequest objects. This page requires " +
        "Internet Explorer 5 or better for Windows, " +
        "or Firefox for any system, or Safari. Other " +
        "compatible browsers may also exist.";
}

}


   function getQuery( event, params ) {
   
		  closeSearchDiv();


          var divContents = "<div class=searchpop>";
          divContents += "<div class=isp>";
          divContents += "<table border='0' cellpadding='0' cellspacing='0' width='220'><tr><td><table border='0' cellpadding='3' cellspacing='1' width='100%'><tr><td style='text-align:left;padding:3px;' align='left'>";
          divContents += "<H4>Loading...</H4>";
          divContents += "</td></tr></table></td></tr></table>";
          divContents += "</div></div>";

          oDiv=document.createElement("DIV");
          oDiv.id = "keyWord";
          oDiv.style.position = "absolute";

          var coord = getMouseCoord(event).split(",");
          var xLoc = parseInt(coord[0]);
          var yLoc = parseInt(coord[1]);
          oDiv.style.left = xLoc;
          oDiv.style.top = yLoc;
          oDiv.style.background = '#f6fcff';
		  oDiv.style.zIndex = 100000;
          oDiv.style.cursor="hand";
          oDiv.style.position = "absolute";
          oDiv.style.margin = 5;
          oDiv.style.border = "solid 1px #cccccc";

          oDiv.onmouseleave = closeSearchDiv;
          oDiv.innerHTML = divContents;
          parentDiv.appendChild(oDiv);
          oDiv.style.width = oDiv.offsetWidth + 12;
          oDiv.style.height = oDiv.offsetHeight;
          oDiv.style.border = "solid 1px #cccccc";

			var ifrm1 = document.getElementById("ifrmHideSearch"); 
			if ( ifrm1 != null ) {
				ifrm1.style.top = yLoc + 10;
				ifrm1.style.left = xLoc;
				ifrm1.style.width = oDiv.offsetWidth + 12;
				ifrm1.style.height = oDiv.offsetHeight + 12; 
			}

          event.returnValue = false;
          
          var scrollTop = document.body.scrollTop;
		var windowHeight = document.body.clientHeight;
		
		
		var boxPosition = 350 + (yLoc-scrollTop);
		
		boxPosition = windowHeight - boxPosition;
		          
        if(boxPosition > 0){
			boxPosition = 0;
        }
         
         
         setTimeout("showResults('"+params+"', "+boxPosition+", "+yLoc+")",1);
         
         


    // return null;
  }
  
  function showResults( params, boxPosition, yLoc) {
		
		
		 
        var innerDiv = document.getElementById("keyWord");
          
		var thisArticle = getQueryVariable( "theArt" );
		var randomnumber = Math.floor(Math.random()*1000001);
	
		var results = cInclude(null, "/Articles/SearchContents.aspx?target=" + params + '&theArt=' + thisArticle + '&' + randomnumber );
		
		if ( results.indexOf("</Record>") != -1 ) {
			var output = "";
		var resultsArray = results.split("</Record>");
		for ( var t=0; t<resultsArray.length-1; t++ ) {
				output += ( "<p><strong>" + getElement( resultsArray[t], "Rank" ) + ".  " );
				var title = getElement( resultsArray[t], "Title" );
			if ( title == "" ) {
			title = "untitled";
			}
				output += ( title );
			var summary = getElement( resultsArray[t], "Summary" );
				if ( summary.length > 150 ) {
			summary = summary.substring(0,150);
				}
				output += ( "</strong><br />" + summary + "..." );
				output += ( "</p>" );
		}


          var divContents = "<div class=searchpop>";
          divContents += "<div class=isp>";
          divContents += output;
        
          divContents += "</div></div>";

          innerDiv.innerHTML = divContents;
			
          oDiv = document.getElementById("keyWord");
         
          oDiv.style.top = yLoc + boxPosition;
		  oDiv.style.width = innerDiv.offsetWidth + 12;
          oDiv.style.height = innerDiv.offsetHeight;
          oDiv.style.border = "solid 1px #cccccc";
         
		}

  }

   function replacestring(str_normal,str_find,str_replace,int_case_insensitive)
    {
            if (arguments.length<3 || str_find=="" || str_normal=="" || typeof("".split)!="function")
                    return(str_normal);

            //no parm means default, "case SENSITIVE"...
            if(!(int_case_insensitive))
                    return(str_normal.split(str_find)).join(str_replace);

            str_find=str_find.toLowerCase();

            var rv="";
            var ix=str_normal.toLowerCase().indexOf(str_find);
            while(ix>-1)
            {
                    rv+=str_normal.substring(0,ix)+str_replace;
                    str_normal=str_normal.substring(ix+str_find.length);
                    ix=str_normal.toLowerCase().indexOf(str_find);
            }
            return(rv+str_normal);
    }
    
    function openTheWindow( href ){
 
		window.open( href );
		
    }


      function getElement( events, name ) {
	if ( events.indexOf("<"+name+">") != -1 ) {
           return events.substring( events.indexOf( "<"+name+">" ) + ( "<"+name+">" ).length, events.indexOf( "</"+name+">" ) );
	} else {
        return null;
	}
    }


    function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                    var pair = vars[i].split("=");
                    if (pair[0] == variable) {
                    return pair[1];
                    }
            }
            return null;
    }

