/* UTILITY.JS
   General JavaScript functions.

    Version Information:
     20020117a = Created.
	 20020731a = Added unconditional parameter to reload in killThis().
	 20060130a = Added querystring parsing functions.
	 20060418a = Modified picture window size in showPic.
	 20061128a = Added declaration of tbSiteRoot for more global usage.
	 20080207a = Added 'max' size options for popupWindow function.

Copyright © 2006, Land Information Access Association
*/

/* Initialize Variables */
var newWin;
var popupWin = new Array();
var picWin;
var tbSiteRoot;

/* Open New Window */
function popupWindow(winID, winWidth, winHeight, winURL) {
	if (popupWin[winID]) if (!popupWin[winID]) popupWin[winID].close();
	if (winWidth == 'max') {
		winWidth = screen.availWidth - 10;
		var winLeft = 0;
	} else {
		var winLeft = ((screen.availWidth - winWidth - 10) * .5);
	}
	if (winHeight == 'max') {
		winHeight = screen.availHeight - 122;
		var winTop = 0;
	} else {
		var winTop = ((screen.availHeight - winHeight - 30) * .5);
	}
	popupWin[winID] = window.open(winURL, 'PopWin' + winID, 'status=yes,menubar=no,toolbar=no,location=no,scrollbars=yes,top=' + winTop + ',left=' + winLeft + ',width=' + winWidth + ',height=' + winHeight + ',resizable=yes');
	popupWin[winID].focus();
}

/* Open Glossary Window */
function GlossaryWindow(winPathtoRoot, winGTerm) {
	popupWindow(10,550,350, winPathtoRoot + 'CC/SDB/SDBrecord.asp?SDBaction=show&mode=GV&term=' + winGTerm );
}

/* Define Picture Object */
function picObj(file, title, width, height) {
	this.file = file;
	this.width = width;
	this.height = height;
	this.title = title;
}

/* Show Picture in New Window */
function showPic(picThing) {
	if (picWin) { if (!picWin.closed) picWin.close(); }
	var docHTML = '<html><head><title>';
	if (picThing.title) {
		docHTML += picThing.title;
	} else {
		docHTML += 'Picture';
	}
	docHTML += '</title></head>';
	docHTML += '<body><div align="center"><center>';
	if (picThing.title) docHTML += '<h3>' + picThing.title + '</h3>';
	docHTML += '<a href="javascript:window.close();">';
	docHTML += '<img src="' + picThing.file + '"'
	if (picThing.height) docHTML += ' height="' + picThing.height + '"';
	if (picThing.width) docHTML += ' width="' + picThing.width + '"';
	docHTML += ' border="0">';
	docHTML += '<br><font face="Arial,Helvetica,sans-serif" size="1">Click to Close Window</font></a>';
	docHTML += '</center></div></body></html>';
	var winProperties = 'menubar=no,toolbar=no,location=no,scrollbars=yes,resizable=yes'
	if (picThing.height) winProperties += ',height=' + (picThing.height + 80);
	if (picThing.width) winProperties += ',width=' + (picThing.width + 40);
	mapPicWin = window.open('', 'PicWin', winProperties);
	mapPicWin.document.write(docHTML);
	mapPicWin.document.close();
}

/* Refresh Primary Window, Close Secondary Window */
function killThis() {
	opener.location.reload(true);
	window.close();
}

/* Show the names of all elements on all forms */
function showElements() {
	var formName, thisForm, numElem;
	var elemNames = "";
	for (x = 0; x < document.forms.length; x++) {
		formName = document.forms[x].name;
		elemNames += 'Form ' + formName + ':\n';
		thisForm = eval('document.' + formName);
		numElem = thisForm.elements.length;
		for (i = 0; i < numElem; i++) {
			elemNames += '  ' + thisForm.elements[i].name + '\n';
		}
	}
	alert(elemNames);
}

/* Object and queryString function to parse page querystring */
function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
			return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; } 
}
function queryString(key){
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}
function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}