try{
	if(Node){
		Node.prototype.swapNode = function (node) {
			var nextSibling = this.nextSibling;
			var parentNode = this.parentNode;
			node.parentNode.replaceChild(this, node);
			parentNode.insertBefore(node, nextSibling);  
		}
	}
} catch(err) {
}

function getprev(source,tag){
	if(source && source.previousSibling){
		if(source.previousSibling.nodeName == tag){
			return source.previousSibling;
		} else if(source.previousSibling.previousSibling.nodeName == tag){
			return source.previousSibling.previousSibling;
		} else {
			return false;
		}
	}
}

function trimField(field,countfield,maxlimit){
	if(field.value.length > maxlimit){
		field.value = field.value.substring(0,maxlimit);
	} else {
		countfield.value = maxlimit - field.value.length;
	}
}

// Calender stuff
try{
	if(Node){
		Node.prototype.swapNode = function (node) {
			var nextSibling = this.nextSibling;
			var parentNode = this.parentNode;
			node.parentNode.replaceChild(this, node);
			parentNode.insertBefore(node, nextSibling);  
		}
	}
} catch(err){
}
function UCWord(str) {
	if (str != "") {
		var firstLetter = str.substring(0, 1).toUpperCase();
		var restOfWord = str.substring(1, str.length).toLowerCase();
		return firstLetter + restOfWord;
	} else {
		return "";
	}
}
function getnext(source,tag){
	if(source && source.nextSibling){
		if(source.nextSibling.nodeName == "TR"){
			return source.nextSibling;
		} else if(source.nextSibling.nextSibling && source.nextSibling.nextSibling.nodeName == "TR"){
			return source.nextSibling.nextSibling;
		} else {
			return false;
		}
	} else {
	}
}
function swap(sourceid,targetid){
	if(!sourceid || !targetid) return false;

	//alert("Swapping: " + sourceid + " / " + targetid);

	var source = document.getElementById(sourceid);
	var target = document.getElementById(targetid);

	try{
		var tmp = target.innerHTML;
		target.innerHTML = source.innerHTML;
		source.innerHTML = tmp;
		
		//can't have duped ids, even for a second, at least in safari
		var tgt_id = target.id;
		var src_id = source.id;

		target.id = null;
		source.id = null;

		target.id = src_id;
		source.id = tgt_id;
	} catch(err) {
		source.swapNode(target);
	}
}
function moveup(elementid){
	var source = document.getElementById(elementid);
	var target = getprev(source,"TR");

	if(!source || !target) return false;

	swap(elementid,target.id);
	return false;
}

function movedown(elementid){
	var source = document.getElementById(elementid);
	var target = getnext(source,"TR");
	if(!source || !target) return false;

	swap(elementid,target.id);
	return true;
}

function popup(mylink,windowname,ileft,itop){

	if(!window.focus) return true;
	var href = (typeof(mylink) == 'string' ? mylink : mylink.href);
	var left = (ileft ? ileft : 0);
	var top = (itop ? itop : 0);

//	if(typeof(mylink) == 'string') href=mylink;
//	else href=mylink.href;

//	if(!ileft) left = 0; else left = ileft;
//	if(!itop) top = 0; else top = itop;

	window.open(href, windowname, 'location=no,width=800,height=550,scrollbars=yes');
	return false;
}

//simple...pop the user with a confirm and redirect if they say ok
function confirmgo(question,url){
	if(confirm(question)){
		window.location = url;
	}
}

//wrapper function to create a xml http object for ajax
function xhrup(){
	var xhr;
	if(window.ActiveXObject){
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	} else if(window.XMLHttpRequest){
		xhr = new XMLHttpRequest();
	}
	return xhr;
}

function createCellWithText(text){
	var cell = document.createElement("td");
	var textNode = document.createTextNode(text);
	cell.appendChild(textNode);
	
	return cell;
}

function createInput(type,name,id,value,size){
	var inp = document.createElement("input");
	inp.type = type;
	inp.id = id;
	inp.name = name;
	if(value) inp.value = value;
	if(size) inp.size = size;
	return inp;
}

function activateOnChange(obj){
	var onChangeHandler = new Function(obj.onchange);

	if (obj.addEventListener) {
		obj.addEventListener('change', onChangeHandler, false );
	} else if (obj.attachEvent) {
		obj.attachEvent('onchange', onChangeHandler);
	}
}

function paginate(field,rpp,numres,p){
	rpp = Number(rpp);
	p = Number(p);
	numres = Number(numres);

	if(!numres){
		var ret = document.createTextNode("No results found.");
		field.innerHTML = "";
		field.appendChild(ret);
	} else {
		var dispEnd = false;
		var start = rpp * p;
		var end = ((start + rpp) > numres ? numres : start + rpp);
		var maxpgs = Math.floor(numres / rpp);
		if((numres % rpp) == 0) maxpgs--;
		start++;

		if(start > numres){
			var ret = document.createTextNode("Invalid page number.");
			field.appendChild(ret);
		} else {
			var ret = "Displaying results " + start + " - " + end + " of " + numres + "<br/>\n";
		}

		if(p > 0) ret += "<a href='javascript: turnpage(0);'>&lt;&lt;</a>&nbsp;<a href='javascript: turnpage(" + (p - 1) + ");'>&lt;</a>&nbsp;";

		if(maxpgs > 0){
			if(p < 3){
				for(var i = 0 ; (i < 5) && (i <= maxpgs) ; i++){
					if(i == p){
						ret += "[" + (i + 1) + "]&nbsp;";
					} else {
						ret += "<a href='javascript: turnpage(" + i + ");'>" + (i + 1) + "</a>&nbsp;";
					}
					if(i == maxpgs) dispEnd = true;
				}
			} else if (p > (maxpgs - 3)) {
				for(var i = (maxpgs - 4) ; i <= maxpgs ; i++) {
					if(i == p){
						ret += "[" + (i + 1) + "]&nbsp;"
					} else {
						ret += "<a href='javascript: turnpage(" + i + ");'>" + (i + 1) + "</a>&nbsp;";
					}
					if(i == maxpgs) dispEnd = true;
				}
			} else {
				for(var i = (p - 2) ; i <= (p + 3) ; i++) {
					if(i == p){
						ret += "[" + (i + 1) + "]&nbsp;"
					} else {
						ret += "<a href='javascript: turnpage(" + i + ");'>" + (i + 1) + "</a>&nbsp;";
					}
					if(i == maxpgs) dispEnd = true;
				}
			}
		}

		if(p < maxpgs) ret += "<a href='javascript: turnpage(" + (p + 1) + ");'>&gt;</a>&nbsp;<a href='javascript: turnpage(" + maxpgs + ");'>&gt;&gt;</a><br/>";

		field.innerHTML = ret;
	}
}

function in_array(needle, haystack){
	for (h in haystack) {
		if (haystack[h] == needle) return true;
	}
	return false;
	// or if you prefer to get the key of the first found match use
	// return h;
}

function togglevis(whichLayer){
	if (document.getElementById){
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display ? "" : "block";
	} else if (document.all){
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display ? "" : "block";
	} else if (document.layers) {
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display ? "" : "block";
	}
}

function expand_chklst(){
	var chklst = document.getElementsByTagName("div");
	for (var i = 0 ; i < chklst.length ; i++) {
		if(chklst[i].className == 'sub_chklst')
			chklst[i].style.display = "";
	}
}

function collapse_chklst(){
	var chklst = document.getElementsByTagName("div");
	for (var i = 0 ; i < chklst.length ; i++) {
		if(chklst[i].className == 'sub_chklst')
			chklst[i].style.display = "none";
	}
}

function turnpage(page) {
	document.getElementById("page").value = page;
	query();
}

function activateOnClick(obj){
	var onClickHandler = new Function(obj.onclick);

	if (obj.addEventListener) {
		obj.addEventListener('click', onClickHandler, false );
	} else if (obj.attachEvent) {
		obj.attachEvent('onclick', onClickHandler);
	}
}

/*
var states = new Array(<?php
$dbl->query("select name from states");
$first = true;
while($myrow = $dbl->fetch_array()){
	if(!$first) echo ",";
	echo "\"" . $myrow['name'] . "\"";
	$first = false;
}
?>);

var sids = new Array(<?php
$dbl->query("select sid from states");
$first = true;
while($myrow = $dbl->fetch_array()){
	if(!$first) echo ",";
	echo "\"" . $myrow['sid'] . "\"";
	$first = false;
}
?>);
*/

function deleteChildren(obj){
	if(!obj) return;
	if(!obj.hasChildNodes()) return;

	while(obj.childNodes.length > 0){
		obj.removeChild(obj.childNodes[0]);
	}
}

var states = new Array("Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","District of Columbia","Florida","Georgia","Hawaii","Idaho","Indiana","Iowa","Illinois","Kansas","Kentucky","Louisiana","Maine","Massachusetts","Mississippi","Missouri","Maryland","Michigan","Minnesota","Montana","Nebraska","New Hampshire","New Mexico","North Carolina","North Dakota","Nevada","New Jersey","New York","Oregon","Ohio","Oklahoma","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","West Virginia","Wyoming","Utah","Vermont","Virginia","Washington","Wisconsin");
var sids = new Array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51");	

	function populate_states(theid){
		var select = document.getElementById(theid);

		for(i = 0; i < states.length; i++){
			var opt = document.createElement("option");
			opt.appendChild(document.createTextNode(states[i]));
			select.appendChild(opt);
		}
	}
	
	// ====================================================================
	//		URLEncode and URLDecode functions
	//
	// Copyright Albion Research Ltd. 2002
	// http://www.albionresearch.com/
	//
	// You may copy these functions providing that 
	// (a) you leave this copyright notice intact, and 
	// (b) if you use these functions on a publicly accessible
	// web site you include a credit somewhere on the web site 
	// with a link back to http://www.albionresarch.com/
	//
	// If you find or fix any bugs, please let us know at albionresearch.com
	// ====================================================================
	function URLEncode(plaintext)
	{
		// The Javascript escape and unescape functions do not correspond
		// with what browsers actually do...
		var SAFECHARS = "0123456789" +					// Numeric
						"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
						"abcdefghijklmnopqrstuvwxyz" +
						"-_.!~*'()";					// RFC2396 Mark characters
		var HEX = "0123456789ABCDEF";

		var encoded = "";
		for (var i = 0; i < plaintext.length; i++ ) {
			var ch = plaintext.charAt(i);
			if (ch == " ") {
				encoded += "+";				// x-www-urlencoded, rather than %20
			} else if (SAFECHARS.indexOf(ch) != -1) {
				encoded += ch;
			} else {
				var charCode = ch.charCodeAt(0);
				if (charCode > 255) {
					alert( "Unicode Character '" 
							+ ch 
							+ "' cannot be encoded using standard URL encoding.\n" +
							  "(URL encoding only supports 8-bit characters.)\n" +
							  "A space (+) will be substituted." );
					encoded += "+";
				} else {
					encoded += "%";
					encoded += HEX.charAt((charCode >> 4) & 0xF);
					encoded += HEX.charAt(charCode & 0xF);
				}
			}
		} // for

		return encoded;
	};

	function URLDecode(encoded){
		// Replace + with ' '
		// Replace %xx with equivalent character
		// Put [ERROR] in output if %xx is invalid.
		var HEXCHARS = "0123456789ABCDEFabcdef"; 
		var plaintext = "";
		var i = 0;
		while (i < encoded.length) {
			var ch = encoded.charAt(i);
			if (ch == "+") {
				plaintext += " ";
				i++;
			} else if (ch == "%") {
				if (i < (encoded.length-2) 
						&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
						&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
					plaintext += unescape( encoded.substr(i,3) );
					i += 3;
				} else {
					alert( 'Bad escape combination near ...' + encoded.substr(i) );
					plaintext += "%[ERROR]";
					i++;
				}
			} else {
			   plaintext += ch;
			   i++;
			}
		} // while

		return plaintext;
	};

	/**
	The JavaScript functions below will gradually enlarge or shrink an image
	on the current page. I use this for mouseover effects, but there might be
	some other interesting applications of it as well.

	You can use these scripts in any way you'd like, just don't pretend like
	you wrote them yourself.

	version 1.0
	March 17, 2005
	Julian Robichaux, http://www.nsftools.com
	*/

	/**** adjust these two parameters to control how fast or slow
	 **** the images grow/shrink ****/
	// how many milliseconds we should wait between resizing events
	var resizeDelay = 10;
	// how many pixels we should grow or shrink by each time we resize
	var resizeIncrement = 5;

	// this will hold information about the images we're dealing with
	var imgCache = new Object();

	/**
	The getCacheTag function just creates a (hopefully) unique identifier for
	each <img> that we resize.
	*/
	function getCacheTag (imgElement) {
		return imgElement.src + "~" + imgElement.offsetLeft + "~" + imgElement.offsetTop;
	}

	/**
	We're using this as a class to hold information about the <img> elements
	that we're manipulating.
	*/
	function cachedImg (imgElement, increment) {
		this.img = imgElement;
		this.cacheTag = getCacheTag(imgElement);
		this.originalSrc = imgElement.src;

		var h = imgElement.height;
		var w = imgElement.width;
		this.originalHeight = h;
		this.originalWidth = w;

		increment = (!increment) ? resizeIncrement : increment;
		this.heightIncrement = Math.ceil(Math.min(1, (h / w)) * increment);
		this.widthIncrement = Math.ceil(Math.min(1, (w / h)) * increment);
	}

	/**
	This is the function that should be called in the onMouseOver and onMouseOut
	events of an <img> element. For example:

	<img src='onesmaller.gif' onMouseOver='resizeImg(this, 150, "onebigger.gif")' onMouseOut='resizeImg(this)'>

	The only required parameter is the first one (imgElement), which is a
	reference to the <img> element itself. If you're calling from onMousexxx, 
	you can just use "this" as the value.

	The second parameter specifies how much larger or smaller we should resize
	the image to, as a percentage of the original size. In the example above,
	we want to resize it to be 150% larger. If this parameter is omitted, we'll
	assume you want to resize the image to its original size (100%).

	The third parameter can specify another image that should be used as the
	image is being resized (it's common for "rollover images" to be similar but
	slightly different or more colorful than the base images). If this parameter
	is omitted, we'll just resize the existing image.
	*/
	function resizeImg (imgElement, percentChange, newImageURL) {
		// convert the percentage (like 150) to an percentage value we can use
		// for calculations (like 1.5)
		var pct = (percentChange) ? percentChange / 100 : 1;

		// if we've already resized this image, it will have a "cacheTag" attribute
		// that should uniquely identify it. If the attribute is missing, create a
		// cacheTag and add the attribute
		var cacheTag = imgElement.getAttribute("cacheTag");
		if (!cacheTag) {
			cacheTag = getCacheTag(imgElement);
			imgElement.setAttribute("cacheTag", cacheTag);
		}

		// look for this image in our image cache. If it's not there, create it.
		// If it is there, update the percentage value.
		var cacheVal = imgCache[cacheTag];
		if (!cacheVal) {
			imgCache[cacheTag] = new Array(new cachedImg(imgElement), pct);
		} else {
			cacheVal[1] = pct;
		}

		// if we're supposed to be using a rollover image, use it
		if (newImageURL)
			imgElement.src = newImageURL;

		// start the resizing loop. It will continue to call itself over and over
		// until the image has been resized to the proper value.
		resizeImgLoop(cacheTag);
		return true;
	}

	/**
	This is the function that actually does all the resizing. It calls itself
	repeatedly with setTimeout until the image is the right size.
	*/
	function resizeImgLoop (cacheTag) {
		// get information about the image element from the image cache
		var cacheVal = imgCache[cacheTag];
		if (!cacheVal)
			return false;

		var cachedImageObj = cacheVal[0];
		var imgElement = cachedImageObj.img;
		var pct = cacheVal[1];
		var plusMinus = (pct > 1) ? 1 : -1;
		var hinc = plusMinus * cachedImageObj.heightIncrement;
		var vinc = plusMinus * cachedImageObj.widthIncrement;
		var startHeight = cachedImageObj.originalHeight;
		var startWidth = cachedImageObj.originalWidth;
		var currentHeight = imgElement.height;
		var currentWidth = imgElement.width;
		var endHeight = Math.round(startHeight * pct);
		var endWidth = Math.round(startWidth * pct);

		// if the image is already the right size, we can exit
		if ( (currentHeight == endHeight) || (currentWidth == endWidth) )
			return true;

		// increase or decrease the height and width, making sure we don't get
		// larger or smaller than the final size we're supposed to be
		var newHeight = currentHeight + hinc;
		var newWidth = currentWidth + vinc;
		if (pct > 1) {
			if ((newHeight >= endHeight) || (newWidth >= endWidth)) {
				newHeight = endHeight;
				newWidth = endWidth;
			}
		} else {
			if ((newHeight <= endHeight) || (newWidth <= endWidth)) {
				newHeight = endHeight;
				newWidth = endWidth;
			}
		}

		// set the image element to the new height and width
		imgElement.height = newHeight;
		imgElement.width = newWidth;

		// if we've returned to the original image size, we can restore the
		// original image as well (because we may have been using a rollover
		// image in the original call to resizeImg)
		if ((newHeight == cachedImageObj.originalHeight) || (newWidth == cachedImageObj.originalwidth)) {
			imgElement.src = cachedImageObj.originalSrc;
		}

		// shrink or grow again in a few milliseconds
		setTimeout("resizeImgLoop('" + cacheTag + "')", resizeDelay);
	}
	function togglevis(whichLayer){
		if (document.getElementById){
			// this is the way the standards work
			var style2 = document.getElementById(whichLayer).style;
			style2.display = ((style2.display == "block") ? "none" : "block");
		} else if (document.all){
			// this is the way old msie versions work
			var style2 = document.all[whichLayer].style;
			style2.display = ((style2.display == "block") ? "none" : "block");
		} else if (document.layers) {
			// this is the way nn4 works
			var style2 = document.layers[whichLayer].style;
			style2.display = ((style2.display == "block") ? "none" : "block");
		}
	}

