
var image1 = new Image();
image1.src = "/images/nav_button_selected.gif";
var image2 = new Image();
image2.src = "/images/button_back.gif";
var image3 = new Image();
image3.src = "/images/button_large_back.gif";

var registerFuncs = [];
function register ( func ) {
	registerFuncs[ registerFuncs.length ] = func;
}
function pageLoad () {
	var navTable = document.getElementById("navTable");
	if (navTable) registerNavTable(navTable);
	for ( var i=0; i < registerFuncs.length; i++ )
		registerFuncs[ i ]();
	setTimeout("hideSaved('saveButton')", 1500);
}

function siteSearch () {
	document.siteSearchForm.submit();
}

function hideSaved (name) {
	var buttons = document.getElementsByName(name);
	for (var i=0; i < buttons.length; i++)
		buttons[i].value = "Save";
}

function set ( name, value ) {
	document.cookie = name + "=" + escape( value ) + "; expires=Thu, 01-Jan-2100 00:00:01 GMT; path=/";
}
function get ( name ) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf( "; " + prefix );
	if ( begin == -1 ) {
		begin = dc.indexOf( prefix );
		if ( begin != 0 ) return "";
	} else
		begin += 2;
	var end = document.cookie.indexOf( ";", begin );
	if ( end == -1 ) end = dc.length;
	return unescape( dc.substring(begin + prefix.length, end) );
}

function trim ( text ) {
	if ( !text ) return "";
	text = text + "";
	while ( text.substring(0, 1) == " " ) 
		text = text.substring( 1, text.length );
	while ( text.substring(text.length - 1, text.length) == " " )
		text = text.substring( 0, text.length - 1 );
	return text;
} 

function formatPrice ( price ) {
	if ( price == null ) price = 0;
	price = price.toFixed(2);
	if (price == "-0.00") price = "0.00";
	return "$" + price;
}

function addCommas ( price ) {
	price = price.replace(/\$/g, "");
	var newPrice = price.slice(-3);
	for (var i=price.length - 3, ii = 0; i > 0; i--, ii++) {
		var ch = price.substring(i, i - 1);
		if (ii % 3 == 0 && ii != 0 && ch != "-") newPrice = "," + newPrice;
		newPrice = ch + newPrice;
	}
	return "$" + newPrice;
}

function rand ( from, to ) {
	return parseInt( Math.random() * (to - from) + from );
}

function registerListing ( id, func, attribute ) {
	var table = document.getElementById( id );
	table.onmouseover = function () {
		var row = getListingRow(0);
		if ( row ) row.className = "resultHighlightedRow";
	};
	table.onmouseout = function () {
		var row = getListingRow(0);
		if ( row ) row.className = "";
	};
	table.onclick = function () {
		var row = getListingRow(0);
		if ( row && row.attributes.getNamedItem(attribute) ) func( row.attributes.getNamedItem(attribute).value, row );
	};
}
function getListingRow (index) {
	var cell = window.event.srcElement;
	while ( cell && cell.cellIndex == null )
		cell = cell.parentNode;
	if ( !cell ) return;
	if ( cell.attributes.getNamedItem("noSelect") ) return null;
	var row = cell.parentElement;
	if (index != null)
		return row.rowIndex > index ? row : null;
	else
		return row;
}
function checkAll ( id, bool ) {
	var rows = document.getElementById( id ).rows;
	for ( var i=1; i < rows.length; i++ )
		rows[ i ].cells[ 0 ].children [ 0 ].checked = bool;
}

function registerNavTable (table) {
	table.onmouseover = function () {
		var row = getListingRow();
		if ( row ) {
			if ( row.parentElement.parentElement != table ) return;
			var cell = row.cells[0];
			if (cell.className == "navButton")
				row.cells[0].style.backgroundImage = "url('/images/nav_button_selected.gif')";
		}
	};
	table.onmouseout = function () {
		var row = getListingRow();
		if ( row ) {
			if ( row.parentElement.parentElement != table ) return;
			var cell = row.cells[0];
			if (cell.className == "navButton")
				row.cells[0].style.backgroundImage = "url('/images/nav_button.gif')";
		}
	};
	table.onclick = function () {
		if ( window.event.srcElement.tagName.toLowerCase() == "a" ) return;
		var row = getListingRow();
		if ( row ) {
			if ( row.parentElement.parentElement != table ) return;
			var cell = row.cells[0];
			if (cell.className == "navButton") {
				var a = cell.getElementsByTagName("a")[0];
				if (a.href.indexOf("http://") == 0)
					document.location.href = a.href;
				else
					a.onclick();
			}
		}
	};
}

function reload (url) {
	if (url) document.reloadForm.action = url;
	document.reloadForm.submit();
}

function showImage (url) {
	window.open("/show.php?image=" + url, "url", "toolbar=0,scrollbars=yes,location=0,statusbar=0,menubar=0,resizable=1,width=640,height=480,left=0,top=0");
}

function textToHTML ( html ) {
	html += "";
	html = html.replace( /\&/g, "&amp;" ).replace( /</g, "&lt;" ).replace( />/g, "&gt;" ).replace( /\n/g, "<BR>" );
	while ( /\s\s/.test(html) )
		html = html.replace( /\s\s/, "&nbsp; " );
	return html.replace( /\s/g, " " );
}

function toggleSection ( sectionID ) {
	var sectionTR = document.getElementById( "section" + sectionID );
	if (!sectionTR) return;
	sectionTR.style.display = sectionTR.style.display == "none" ? (ie ? "inline" : "table-row") : "none";
}

var ie;
if ( document.all ) ie = true; else { ie = false; Event.prototype.__defineSetter__("returnValue",function ( b ) {if ( !b ) this.preventDefault();});Event.prototype.__defineSetter__("cancelBubble",function ( b ) {if ( b ) this.stopPropagation();});Event.prototype.__defineGetter__("srcElement",function () {var node = this.target;while ( node.nodeType != 1 )node = node.parentNode;return node;});Event.prototype.__defineGetter__("fromElement",function () {var node;if ( this.type == "mouseover" )node = this.relatedTarget;else if ( this.type == "mouseout" )node = this.target;if ( !node ) return;while ( node.nodeType != 1 )node = node.parentNode;return node;});Event.prototype.__defineGetter__("toElement",function () {var node;if (this.type == "mouseout")node = this.relatedTarget;else if (this.type == "mouseover")node = this.target;if (!node) return;while (node.nodeType != 1) node = node.parentNode;return node;});Event.prototype.__defineGetter__("offsetX",function () {return this.layerX;});Event.prototype.__defineGetter__("offsetY",function () {return this.layerY;});HTMLDocument.prototype.attachEvent = HTMLElement.prototype.attachEvent = function ( sType, fHandler ) {var shortTypeName = sType.replace( /on/, "" );fHandler._ieEmuEventHandler = function ( e ) {window.event = e;return fHandler();};this.addEventListener( shortTypeName, fHandler._ieEmuEventHandler, false );};HTMLDocument.prototype.detachEvent = HTMLElement.prototype.detachEvent = function ( sType, fHandler ) {var shortTypeName = sType.replace( /on/, "" );if ( typeof(fHandler._ieEmuEventHandler) == "function" )this.removeEventListener( shortTypeName, fHandler._ieEmuEventHandler, false );elsethis.removeEventListener( shortTypeName, fHandler, true );};var _eventNames = [ "click", "dblclick", "mouseover", "mouseout", "mousedown", "mouseup", "mousemove", "keydown", "keypress", "keyup" ];for (var i = 0; i < _eventNames.length; i++) {document.addEventListener(_eventNames[i],function ( e ) { window.event = e; },true);}HTMLElement.prototype.__defineGetter__("parentElement",function () {if ( this.parentNode == this.ownerDocument ) return null;return this.parentNode;});HTMLElement.prototype.__defineGetter__("children",function () {var tmp = [];var j = 0;var n;for ( var i = 0; i < this.childNodes.length; i++ ) {n = this.childNodes[ i ];if ( n.nodeType == 1 ) {tmp[ j++ ] = n;if ( n.name ) {if ( !tmp[n.name] )tmp[n.name] = [];tmp[ n.name ][ tmp[n.name].length ] = n;}if ( n.id ) tmp[ n.id ] = n;}}return tmp;});HTMLElement.prototype.contains = function ( oEl ) {if ( oEl == this ) return true;if ( oEl == null ) return false;return this.contains( oEl.parentNode );};HTMLElement.prototype.insertAdjacentHTML = function ( sWhere, sHTML ) {var df;var r = this.ownerDocument.createRange();switch ( String(sWhere).toLowerCase() ) {case "beforebegin":r.setStartBefore( this );df = r.createContextualFragment( sHTML );this.parentNode.insertBefore( df, this );break;case "afterbegin":r.selectNodeContents( this );r.collapse( true );df = r.createContextualFragment( sHTML );this.insertBefore( df, this.firstChild );break;case "beforeend":r.selectNodeContents( this );r.collapse( false );df = r.createContextualFragment( sHTML );this.appendChild( df );break;case "afterend":r.setStartAfter( this );df = r.createContextualFragment( sHTML );this.parentNode.insertBefore( df, this.nextSibling );break;}};HTMLElement.prototype.__defineGetter__("canHaveChildren",function () {switch (this.tagName) {case "AREA":case "BASE":case "BASEFONT":case "COL":case "FRAME":case "HR":case "IMG":case "BR":case "INPUT":case "ISINDEX":case "LINK":case "META":case "PARAM":return false;}return true;});HTMLElement.prototype.__defineSetter__("outerHTML",function ( sHTML ) {var r = this.ownerDocument.createRange();r.setStartBefore( this );var df = r.createContextualFragment( sHTML );this.parentNode.replaceChild( df, this );return sHTML;});HTMLElement.prototype.__defineGetter__("outerHTML",function () {var attr;var attrs = this.attributes;var str = "<" + this.tagName;for ( var i = 0; i < attrs.length; i++ ) {attr = attrs[ i ];if ( attr.specified )str += " " + attr.name + '="' + attr.value + '"';}if ( !this.canHaveChildren ) return str + ">";return str + ">" + this.innerHTML + "</" + this.tagName + ">";});HTMLElement.prototype.__defineSetter__("innerText",function ( sText ) {this.innerHTML = textToHTML( sText );return sText;});HTMLElement.prototype.__defineGetter__("innerText",function () {var r = this.ownerDocument.createRange();r.selectNodeContents( this );return r.toString();});HTMLElement.prototype.__defineSetter__("outerText",function ( sText ) {this.outerHTML = textToHTML( sText );return sText;});HTMLElement.prototype.__defineGetter__("outerText",function () {var r = this.ownerDocument.createRange();r.selectNodeContents( this );return r.toString();});HTMLElement.prototype.insertAdjacentText = function ( sWhere, sText ) {this.insertAdjacentHTML( sWhere, textToHTML(sText) );};Node.prototype.removeNode = function ( bRemoveChildren ) {var parent = this.parentNode;if ( parent && !bRemoveChildren ) {var df = document.createDocumentFragment();for( var i=0; i < this.childNodes.length; i++ )df.appendChild( this.childNodes[i] );parent.insertBefore( df, this )}return parent ? parent.removeChild( this ) : this;} }
