/*
$Id: script.js,v 1.4 2006/11/29 22:23:56 keithm Exp $
vim: set ai expandtab ts=4 :
*/

function PopAnyPage(thisPage,width,height) {
        if (!width){width=600}
        if (!height){height=400}
        var goToURL = thisPage;
        window.open(goToURL,"windowName",'toolbar=no,width='+width+',height='+height+',status=no,scrollbars=yes,resizable=yes,menubar=no,location=no');
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function hideDemoNote() {
	MM_changeProp('demoNote','','style.display','none','DIV')
}

function hideDemoAlert() {
	MM_changeProp('demoAlert','','style.display','none','DIV')
}

function showDemoAlert() {
	MM_changeProp('demoAlert','','style.display','block','DIV')
}

////////////////////////////////////////////////////////////////////////////////////
// The following functions were added for the Glossary Quick Search functionality //
////////////////////////////////////////////////////////////////////////////////////

function getHTTPObject() { 
	var xmlhttp;
	/*@cc_on 
	@if (@_jscript_version >= 5) 
		try { 
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
		} catch (e) { 
			try { 
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
			} catch (E) { 
				xmlhttp = false; 
			} 
		} 
	@else 
	xmlhttp = false; 
	@end @*/ 
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { 
		try { 
			xmlhttp = new XMLHttpRequest(); 
		} catch (e) { 
			xmlhttp = false; 
		} 
	} 
	return xmlhttp; 
} 

var http = getHTTPObject(); // We create the HTTP Object
var dataarray = new Array() 
var genericprompt = 'Type the term you are looking for.';
var loopcontrol = 0;
var directoryDeep = "";

function getgloss(userentry) {
	if (!userentry) {
		userentry = document.getElementById("termfield").value;
	} else {
		userentry = escape(dataarray[userentry]); // Make string compatable for a URI.
		userentry = userentry + "&this=1";
	}
	if (userentry) {
		http.open("GET", directoryDeep + "getgloss.html?find=" + userentry, true); 
		http.onreadystatechange = function() { 
			if (http.readyState == 4) { 
				processdata(http.responseText); 
			} 
		} 
		http.send(null);
	} else {
		MM_changeProp('glosslookup','','innerHTML',genericprompt,'DIV');
	}
}

function processdata(data) {
	if (data == '') {
		// Someitmes Firefox calls this function before it has recieved any data.  So we try again.
		// loopcontrol is to keep this from getting caught in an infinte loop if something goes wrong.
		loopcontrol ++ ;
		if (loopcontrol < 10) {
			getgloss();
		}
	} else {
		// Things are OK, reset loopcontrol.
		loopcontrol = 0; 
		// Remove new line characters.
		var regEx = new RegExp ('\n', 'gi');
		data = data.replace(regEx, '');
		// Replace single quotes with HTML eqivelant.
		regEx = new RegExp ("'", 'gi');
		data = data.replace(regEx, '&#8217;');
		// Make an array of data sent.
		dataarray = data.split("~");
		// Decide what to display to the user.
		var display = '';
		if (dataarray[0] == 'single_term') {
			display = '<b>' + dataarray[1] + '</b><br>' + dataarray[2];
		} else if (dataarray[0] == 'no_match'||dataarray[0] == 'no_find') {
			display = 'No matches found.';
		} else {
			display = dataarray[0] + ' matches<br>';
			for (var i=1; i<dataarray.length; i++){
				display = display + '<a href="JavaScript:getgloss(' + i + ')">' + dataarray[i] + '</a><br>';
			}
		}
		//Replace the inards of the div below the text box.
		MM_changeProp('glosslookup','','innerHTML', display,'DIV');
	}
}

function clearprompt() {
	if (document.getElementById("termfield").value == 'View matches as you type') {
		document.getElementById("termfield").value = '';
	}
}

function restoreprompt() {
	document.getElementById("termfield").value = 'View matches as you type';
}


