//RUN INSTANT

	
//RUN ON PAGE LOAD COMPLETE
jQuery(document).ready(function(){
	
	//IE6 fixes
	if(jQuery.browser.msie && jQuery.browser.version < 7){
		//hover
		jQuery('.hover').hover( 
			function(){ jQuery(this).addClass('hovering');jQuery(this).hasClass('selected') ? jQuery(this).addClass('selectedhovering'):'';}, 
			function(){ jQuery(this).removeClass('hovering'); jQuery(this).hasClass('selectedhovering')? jQuery(this).removeClass('selectedhovering'):'';
		});

		//:first-child
		jQuery('#nav li:first-child').addClass('firstchild');
	}
	
	
	//handle search bar behavior
	jQuery('#search>li').click(function(){
		jQuery('#search .selected').removeClass('selected');
		jQuery(this).addClass('selected');
	});
	jQuery('.searchkeyword, .searchzip, .searchfirstname, .searchlastname').focus(function(){
		if( jQuery(this).val() == 'First Name' || jQuery(this).val() == 'Last Name' || jQuery(this).val() == 'Keywords' || jQuery(this).val() == 'Zip Code'	){ jQuery(this).val(''); }
	});
	jQuery('.searchkeyword, .searchzip, .searchfirstname, .searchlastname').blur(function(){
		if( jQuery(this).attr('class') === 'searchkeyword' && jQuery(this).val() === '' ){ jQuery(this).val('Keywords'); }
		if( jQuery(this).attr('class') === 'searchzip' && jQuery(this).val() === '' ){ jQuery(this).val('Zip Code'); }
		if( jQuery(this).attr('class') === 'searchfirstname' && jQuery(this).val() === '' ){ jQuery(this).val('First Name'); }
		if( jQuery(this).attr('class') === 'searchlastname' && jQuery(this).val() === '' ){ jQuery(this).val('Last Name'); }
	});
	jQuery('.searchsubmit').click(function(){
		jQuery(this).siblings().each(function(){
			a = {'searchfirstname':'First Name','searchlastname':'Last Name','searchkeyword':'Keywords','searchzip':'Zip Code'};
			if( jQuery(this).attr('class') in a && a[jQuery(this).attr('class')] == jQuery(this).val() ){ jQuery(this).val(''); }
		});
	});
});



	
		

// NON-JQUERY

	/*
	This is used by modal panels to prevent closure whenever validation fails.
	<a4j:commandButton value="Save" action="#{myController.save}" oncomplete="if (!ajaxRequestContainsErrors()) Richfaces.hideModalPanel('myModalPanelId');"/>
	*/
	function ajaxRequestContainsErrors() {
		return (document.getElementById("maximumSeverity").value == "2");
	}

function redirectFrame() {
top.location.href = document.getElementById("valveredir").value;
}


function redirectFrameTo(redirURL) {
top.location.href = redirURL;
}


/* #### THIS FUNCTION NEEDS DOCUMENTATION #### */
checkTimeZone = function() {
	var tzo = new Date().getTimezoneOffset();
	var tz = (tzo/60);
	tz>0 ? tz = 'GMT' + tz*(-1) : tz = 'GMT+' + tz*(-1);
	document.cookie = 'timezoneOffset=' + tz;
}

window.attachEvent ? window.attachEvent("onload", function(){ checkTimeZone() }) : window.addEventListener("load", function(){ checkTimeZone() }, false);




checkAll = function(FormName, FieldName, CheckValue) {
	if(!document.forms[FormName] || !document.forms[FormName].elements.length)
		return;
	for (var i=0;i<document.forms[FormName].elements.length;i++) {
		var e = document.forms[FormName].elements[i];
		if(e.type=='checkbox' && e.name.indexOf(FieldName)!==-1) {
			e.checked=CheckValue;
		}
	}
}

function ajaxRequestContainsErrors() {
    return document.getElementById('maximumSeverity').value == "2";
}

function closeModalPanel(panelId,hid) {
		if (!ajaxRequestContainsErrors(hid)) 
			Richfaces.hideModalPanel(panelId);
}

selectAll = function(FormName, FieldName1, FieldName2) {
	var objCheckBox = document.forms[FormName].elements[FieldName1].checked;
	if(!objCheckBox)
		checkAll(FormName, FieldName2, false);
	else
		checkAll(FormName, FieldName2, true);
}





if(typeof Richfaces == 'function'){

	// modify/override default Richfaces show modal function to remove automatic vertical centering.
	modalshow = function(event, opts){

	    if (!this.shown && this.invokeEvent("beforeshow", event, null, element)) {

		var element = this.id;
		this.preventFocus();
		if (!this.floatedToBody) {
		    this.parent = element.parentNode;
		    document.body.insertBefore(this.parent.removeChild(element), null);
		    this.floatedToBody = true;
		}

		var eCdiv = $(this.cdiv);
		var forms = eCdiv.getElementsByTagName("form");
		if (this.options.keepVisualState && forms) {
		    this.formOnsubmit = this.setStateInput.bindAsEventListener(this);
		    for (var i = 0; i < forms.length; i++) {
			Event.observe(forms[i], "submit", this.formOnsubmit);
		    }
		}

		var eIframe;
		if (ModalPanel.disableSelects && !this.iframe) {
		    this.iframe = this.id.id + "IFrame";
		    new (Insertion.Top)(eCdiv, "<iframe src=\"javascript:''\" frameborder=\"0\" scrolling=\"no\" id=\"" + this.iframe + "\" " + "class=\"dr-mpnl-iframe\" style=\"width: 1px; height: 1px;\">" + "</iframe>");
		    eIframe = $(this.iframe);
		    Event.observe(eIframe, "load", this.initIframe.bindAsEventListener(eIframe));
		}

		var options = {};
		this.userOptions = {};
		if (!eCdiv.mpSet) {
		    Object.extend(options, this.options);
		}

		if (opts) {
		    Object.extend(options, opts);
		    Object.extend(this.userOptions, opts);
		}

		var eContentElt = this.getContentElement();
		if (!this.options.autosized) {
		    if (options.width && options.width == -1) {
			options.width = 300;
		    }
		    if (options.height && options.height == -1) {
			options.height = 200;
		    }
		}

		if (options.width && options.width != -1) {
		    if (this.minWidth > options.width) {
			options.width = this.minWidth;
		    }
		    eContentElt.style.width = options.width + (/px/.test(options.width) ? "" : "px");
		}

		if (options.height && options.height != -1) {
		    if (this.minHeight > options.height) {
			options.height = this.minHeight;
		    }
		    eContentElt.style.height = options.height + (/px/.test(options.height) ? "" : "px");
		}

		eCdiv.mpSet = true;
		this.disableOuterSelects();
		ModalPanel.activePanels = ModalPanel.activePanels.without(this);
		ModalPanel.activePanels.push(this);
		var eDiv = $(this.div);

		if (eDiv.style.position == "absolute") {
		    var we = "getSizeElement().clientWidth + \"px\"";
		    var he = "getSizeElement().clientHeight + \"px\"";

		    eDiv.style.setExpression("width", we);
		    eDiv.style.setExpression("height", he);

		var eCursorDiv = $(this.cursorDiv);

		    eCursorDiv.style.setExpression("width", we);
		    eCursorDiv.style.setExpression("height", he);

		    var le = "-Position.cumulativeOffset(this.parentNode)[0] + getSizeElement().scrollLeft + \"px\"";
		    var te = "-Position.cumulativeOffset(this.parentNode)[1] + getSizeElement().scrollTop + \"px\"";

		    eDiv.style.setExpression("left", le);
		    eDiv.style.setExpression("top", te);
		    eCursorDiv.style.setExpression("left", le);
		    eCursorDiv.style.setExpression("top", te);

		    var leftExpr = "(this.mpLeft || 0) + -Position.cumulativeOffset(this.parentNode)[0] + getSizeElement().scrollLeft + \"px\"";
		    var topExpr = "(this.mpTop || 0) + -Position.cumulativeOffset(this.parentNode)[1] + getSizeElement().scrollTop + \"px\"";

		    eCdiv.style.setExpression("left", leftExpr);

		    //disabled so that modal panels are not automatically positioned vertically in IE
		    //eCdiv.style.setExpression("top", topExpr);

		}

		element.style.visibility = "hidden";

		Element.show(element);

		this.correctShadowSizeEx();
		if (options.left) {
		    var _left;
		    if (options.left != "auto") {
			_left = parseInt(options.left, 10);
		    } else {
			var cw = getSizeElement().clientWidth;
			var _width = this.width();
			if (cw >= _width) {
			    _left = (cw - _width) / 2;
			} else {
			    _left = 0;
			}
		    }
		    this.setLeft(Math.round(_left));
		}
		if (options.top) {
		    var _top;
		    if (options.top != "auto") {
			_top = parseInt(options.top, 10);
		    } else {
			var cw = getSizeElement().clientHeight;
			var _height = this.height();
			if (cw >= _height) {
			    _top = (cw - _height) / 2;
			} else {
			    _top = 0;
			}
		    }

		    //disabled to stop auto-setting top position
		    //this.setTop(Math.round(_top));

		}
		if (this.options.autosized) {
		    this.observerSize = window.setInterval(this.correctShadowSize.bindAsEventListener(this), 500);
		}
		this.doResizeOrMove(ModalPanel.Sizer.Diff.EMPTY);
		for (var k = 0; k < this.borders.length; k++) {
		    this.borders[k].doPosition();
		}
		if (this.header) {
		    this.header.doPosition();
		}
		Element.hide(eCdiv);
		element.style.visibility = "";
		this.lastOnfocus();

		Element.show(eCdiv);

		var event = {};
		event.parameters = opts || {};
		this.shown = true;

		this.invokeEvent("show", event, null, element);
	    }


	}



	Richfaces.showModalPanel=function(id,opts,event){
		var invoke=(RichFaces.MSIE==RichFaces.navigatorType())?function(f){

		if(document.readyState!="complete"){
			var args=arguments;
			var dis=this;
			window.setTimeout(function(){
				args.callee.apply(dis,args);},50);
			}else{ f(); }

	}:function(f){ f(); };

		var panel=$(id);
		if(!panel){
			panel=Richfaces.findModalPanel(id);
		}

		invoke(function(){
			//replace default Richfaces modal show function
			panel.component.show = modalshow;
			panel.component.show(event,opts);
		});

	};

}