/*	Javascript code to allow double-clicking a link to initiate a search of that site
	Copyright 2001 by Mabry Tyson (Tyson@AI.SRI.COM)
	You may use this code in your site if you include the above copyright notice for this code.
	You may not sell this code except by written permission.
	Tested on PC: NS 4.7x and IE 5.5, on Mac: NS 4.77 and IE 5.0; SunOS5.6 NS 4.73; LinuxELF2.2 NS 4.75
	To use this, links in the document should be like:
		<a href="http://cssvirginia.org/" onClick="return Single(this)" onDblClick="return SearchLink(this)" target="x">
	The target is there to bring these pages up in a different window
	The HREF should either be a file (e.g., http://cssvirginia.org/vacsn/index.htm) or a directory or site that ends in a slash
	 (e.g. http://cssvirginia.org/vacsn/)
	The body should have something like:
		<p>By double clicking on the above links, you can search that link's site for<br>
		<form name="SearchData" method="get">
		  <input type="text" name="q" size="24" value=""> via&nbsp;&nbsp;
		  <input type="radio" value="Google" name="SearchEngine">
			<a href="http://www.google.com/" target="Google">Google</a>
	 	  <input type="radio" value="AltaVista" name="SearchEngine" checked>
			<a href="http://www.altavista.com/" target="AltaVista">AltaVista</a></p>
		  <p><font size="-1">AltaVista searches will cover the specified site. Google searches will cover entire domains. 
		  For a link whose URL is <tt>http://cssvirginia.org/vacsn/</tt> the Google search will search all of 
			<tt>http://cssvirginia.org/</tt>. For a link like <tt>http://cssvirginia.org/</tt>,
			 both searches cover the same pages.  Javascript is required</font></p>
		</form>
		<form name="GoogleSearch" action="http://www.google.com/search" method="get" target="Google">
			<input type="hidden" value="" name="q">
		</form>
		<form name="AVSearch" action="http://www.altavista.com/sites/search/web" method="get" target="AltaVista">
			<input type="hidden" value="" name="q"><input type="hidden" value="off" name="sc">
		</form>
*/
function showProps(obj,objName) {
	var result = ""	
	var count = 0
	for (var i in obj) {
		result += objName+"."+i+" = "+obj[i]+"\n"
		count++
		if (count == 25) {
			alert(result)
			result = ""
			count = 0
		}
	}
	alert(result)
}
var isDouble=false
var timeoutID=-1
var pendinglink
// This gets called for each click, including for the first and/or the second click of a double click
function Single(link) {
	if (timeoutID != -1) {
		clearTimeout(timeoutID)
		timeoutID = -1
		// Unix X-windows systems don't have double-clicks, so if we get here before the timeoout has expired
		// then we treat it as a double click
		var OS=navigator.platform
		if (OS.match(/^SunOS/) || OS.match(/^Linux/)) {
			SearchLink(link)
		}
	}
	if (!isDouble) {
		pendinglink=link
		timeoutID = setTimeout("GotoLink(pendinglink)",300)
	}
	isDouble = false
	return false
}
// Wouldn't it be nice if I could just create a window that is the same as the current one
function GotoLink(link) {
	timeoutID=-1
	if (link.target == "") {
		window.location=link.href
		} else {
		var params = "menubar=yes,directories=yes,location=yes,scrollbars,status=yes,toolbar=yes"
		if (navigator.appName == "Netscape") {
			if (navigator.appVersion.charAt(0) >=3) {
				params=""
				if (window.menubar.visible) {params = params + ",menubar=yes";}
				if (window.personalbar.visible) {params +=",directories=yes";}
				if (window.locationbar.visible) {params += ",location=yes"}
				if (window.scrollbars.visible) {params += ",scrollbars=yes";}
				if ( window.statusbar.visible) {params += ",status=yes";}
				if (window.toolbar.visible) { params += ",toolbar=yes";}
				params = params.substring(1,params.length)
				}
			}
		if (parent.frames.length > 0) {
			for (var i= 0; i<parent.frames.length; i++) {
				if (parent.frames[i].name == link.target) {
					 parent.frames[link.target].location=link.href
					 return false
				 }
			 }
			 window.open(link.href,link.target,params)
			 return false
		 } else {
		 window.open(link.href,"",params)
		 return false
		 }		
		}
	}
//  There's probably a part of the location (link) that has this, but...
//   I probalby should worry about "http://cssvirginia.org"  vs  "http://cssvirginia.org/"
function getDirPath(URL) {
	return unescape(URL.substring(0,(URL.lastIndexOf("/"))+1))
	}
// Here's the guts of this.  Rather simple!
function SearchLink(link){
	isDouble = true
	clearTimeout(timeoutID)
	timeoutID=-1
       var form
	var SearchEngineRadio = document.SearchData.SearchEngine
	var urlToSearch = ""
	for (var i= 0; i<SearchEngineRadio.length; i++) {
 	      if(SearchEngineRadio[i].checked) {
			if (SearchEngineRadio[i].value=="Google") {
				form=document.GoogleSearch
				 urlToSearch=" site:"+link.hostname
 		    } else {
			if (SearchEngineRadio[i].value=="AltaVista") {
				form=document.AVSearch
				urlToSearch=" url:"+getDirPath(link.href)
				}
			}
		}
	}
	form.elements[0].value=document.SearchData.elements[0].value + urlToSearch
	form.submit()
	return false
}