function AjaxRequest(){var req =new Object(); req.timeout =null; req.generateUniqueUrl =true; req.url =window.location.href; req.method ="GET"; req.async =true; req.username =null; req.password =null; req.parameters =new Object(); req.requestIndex =AjaxRequest.numAjaxRequests++; req.responseReceived =false; req.groupName =null; req.queryString =""; req.responseText =null; req.responseXML =null; req.status =null; req.statusText =null; req.aborted =false; req.xmlHttpRequest =null; req.onTimeout=null; req.onLoading=null; req.onLoaded=null; req.onInteractive=null; req.onComplete=null; req.onSuccess=null; req.onError=null; req.onGroupBegin=null; req.onGroupEnd=null; req.xmlHttpRequest =AjaxRequest.getXmlHttpRequest(); if(req.xmlHttpRequest==null){return null;}req.xmlHttpRequest.onreadystatechange = function(){if(req==null || req.xmlHttpRequest==null){return;}if(req.xmlHttpRequest.readyState==1){req.onLoadingInternal(req);}if(req.xmlHttpRequest.readyState==2){req.onLoadedInternal(req);}if(req.xmlHttpRequest.readyState==3){req.onInteractiveInternal(req);}if(req.xmlHttpRequest.readyState==4){req.onCompleteInternal(req);}}; req.onLoadingInternalHandled=false; req.onLoadedInternalHandled=false; req.onInteractiveInternalHandled=false; req.onCompleteInternalHandled=false; req.onLoadingInternal= function(){if(req.onLoadingInternalHandled){return;}AjaxRequest.numActiveAjaxRequests++; if(AjaxRequest.numActiveAjaxRequests==1 && typeof(window['AjaxRequestBegin'])=="function"){AjaxRequestBegin();}if(req.groupName!=null){if(typeof(AjaxRequest.numActiveAjaxGroupRequests[req.groupName])=="undefined"){AjaxRequest.numActiveAjaxGroupRequests[req.groupName] =0;}AjaxRequest.numActiveAjaxGroupRequests[req.groupName]++; if(AjaxRequest.numActiveAjaxGroupRequests[req.groupName]==1 && typeof(req.onGroupBegin)=="function"){req.onGroupBegin(req.groupName);}}if(typeof(req.onLoading)=="function"){req.onLoading(req);}req.onLoadingInternalHandled=true;}; req.onLoadedInternal= function(){if(req.onLoadedInternalHandled){return;}if(typeof(req.onLoaded)=="function"){req.onLoaded(req);}req.onLoadedInternalHandled=true;}; req.onInteractiveInternal= function(){if(req.onInteractiveInternalHandled){return;}if(typeof(req.onInteractive)=="function"){req.onInteractive(req);}req.onInteractiveInternalHandled=true;}; req.onCompleteInternal= function(){if(req.onCompleteInternalHandled || req.aborted){return;}req.onCompleteInternalHandled=true; AjaxRequest.numActiveAjaxRequests--; if(AjaxRequest.numActiveAjaxRequests==0 && typeof(window['AjaxRequestEnd'])=="function"){AjaxRequestEnd(req.groupName);}if(req.groupName!=null){AjaxRequest.numActiveAjaxGroupRequests[req.groupName]--; if(AjaxRequest.numActiveAjaxGroupRequests[req.groupName]==0 && typeof(req.onGroupEnd)=="function"){req.onGroupEnd(req.groupName);}}req.responseReceived =true; req.status =req.xmlHttpRequest.status; req.statusText =req.xmlHttpRequest.statusText; req.responseText =req.xmlHttpRequest.responseText; req.responseXML =req.xmlHttpRequest.responseXML; if(typeof(req.onComplete)=="function"){req.onComplete(req);}if(req.xmlHttpRequest.status==200 && typeof(req.onSuccess)=="function"){req.onSuccess(req);}else if(typeof(req.onError)=="function"){req.onError(req);}delete req.xmlHttpRequest['onreadystatechange']; req.xmlHttpRequest =null;}; req.onTimeoutInternal= function(){if(req!=null && req.xmlHttpRequest!=null && !req.onCompleteInternalHandled){req.aborted =true; req.xmlHttpRequest.abort(); AjaxRequest.numActiveAjaxRequests--; if(AjaxRequest.numActiveAjaxRequests==0 && typeof(window['AjaxRequestEnd'])=="function"){AjaxRequestEnd(req.groupName);}if(req.groupName!=null){AjaxRequest.numActiveAjaxGroupRequests[req.groupName]--; if(AjaxRequest.numActiveAjaxGroupRequests[req.groupName]==0 && typeof(req.onGroupEnd)=="function"){req.onGroupEnd(req.groupName);}}if(typeof(req.onTimeout)=="function"){req.onTimeout(req);}delete req.xmlHttpRequest['onreadystatechange']; req.xmlHttpRequest =null;}}; req.process = function(){if(req.xmlHttpRequest!=null){if(req.generateUniqueUrl && req.method=="GET"){req.parameters["AjaxRequestUniqueId"] =new Date().getTime() + "" + req.requestIndex;}var content =null; for(var i in req.parameters){if(req.queryString.length>0){req.queryString +="&";}req.queryString +=encodeURIComponent(i) + "=" + encodeURIComponent(req.parameters[i]);}if(req.method=="GET"){if(req.queryString.length>0){req.url +=((req.url.indexOf("?")>-1)?"&":"?") + req.queryString;}}req.xmlHttpRequest.open(req.method,req.url,req.async,req.username,req.password); if(req.method=="POST"){if(typeof(req.xmlHttpRequest.setRequestHeader)!="undefined"){req.xmlHttpRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');}content =req.queryString;}if(req.timeout>0){setTimeout(req.onTimeoutInternal,req.timeout);}req.xmlHttpRequest.send(content);}}; req.handleArguments = function(args){for(var i in args){if(typeof(req[i])=="undefined"){req.parameters[i] =args[i];}else{req[i] =args[i];}}}; req.getAllResponseHeaders = function(){if(req.xmlHttpRequest!=null){if(req.responseReceived){return req.xmlHttpRequest.getAllResponseHeaders();}alert("Cannot getAllResponseHeaders because a response has not yet been received");}}; req.getResponseHeader = function(headerName){if(req.xmlHttpRequest!=null){if(req.responseReceived){return req.xmlHttpRequest.getResponseHeader(headerName);}alert("Cannot getResponseHeader because a response has not yet been received");}}; return req;}AjaxRequest.getXmlHttpRequest =function(){if(window.XMLHttpRequest){return new XMLHttpRequest();}else if(window.ActiveXObject){/*@cc_on @*/ /*@if(@_jscript_version >=5) try{return new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{return new ActiveXObject("Microsoft.XMLHTTP");}catch(E){return null;}}@end @*/}else{return null;}}; AjaxRequest.isActive =function(){return(AjaxRequest.numActiveAjaxRequests>0);}; AjaxRequest.get =function(args){AjaxRequest.doRequest("GET",args);}; AjaxRequest.post =function(args){AjaxRequest.doRequest("POST",args);}; AjaxRequest.doRequest =function(method,args){if(typeof(args)!="undefined" && args!=null){var myRequest =new AjaxRequest(); myRequest.method =method; myRequest.handleArguments(args); myRequest.process();}}; AjaxRequest.submit =function(theform, args){var myRequest =new AjaxRequest(); if(myRequest==null){return false;}var serializedForm =AjaxRequest.serializeForm(theform); myRequest.method =theform.method.toUpperCase(); myRequest.url =theform.action; myRequest.handleArguments(args); myRequest.queryString =serializedForm; myRequest.process(); return true;}; AjaxRequest.serializeForm =function(theform){var els =theform.elements; var len =els.length; var queryString =""; this.addField = function(name,value){if(queryString.length>0){queryString +="&";}queryString +=encodeURIComponent(name) + "=" + encodeURIComponent(value);}; for(var i=0;i<len;i++){var el =els[i]; if(!el.disabled){switch(el.type){case 'text': case 'password': case 'hidden': case 'textarea': this.addField(el.name,el.value); break; case 'select-one': if(el.selectedIndex>=0){this.addField(el.name,el.options[el.selectedIndex].value);}break; case 'select-multiple': for(var j=0;j<el.options.length;j++){if(el.options[j].selected){this.addField(el.name,el.options[j].value);}}break; case 'checkbox': case 'radio': if(el.checked){this.addField(el.name,el.value);}break;}}}return queryString;}; AjaxRequest.numActiveAjaxRequests =0; AjaxRequest.numActiveAjaxGroupRequests =new Object(); AjaxRequest.numAjaxRequests =0; 

function Browser()
{ 
	this.version	= navigator.appVersion;
	this.agent		= navigator.userAgent;
	this.dom		= document.getElementById?1:0;
	this.opera5		= this.agent.indexOf("Opera 5")>-1;
	this.ie4		= (document.all && !this.dom && !this.opera5)?1:0;
	this.ie5		= (this.version.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
	this.ie6		= (this.version.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie7		= (this.version.indexOf("MSIE 7")>-1 && this.dom && !this.opera5)?1:0;
	this.ie			= this.ie4||this.ie5||this.ie6||this.ie7;
	this.mac		= this.agent.indexOf("Mac")>-1;
	this.ns6		= (this.dom && parseInt(this.version) >= 5) ?1:0; 
	this.ns4		= (document.layers && !this.dom)?1:0;
	this.bw			= (this.ie7 || this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5);
	return this;
}

browser = new Browser();

var pageLoaded = false;
var cleared = false;
function clearText()
{
	if (!cleared)
	{
		document.forms[0].message.value = "";
		cleared = true;
	}
}

function showForm(what)
{
	var email = document.getElementById('email');
	var phone = document.getElementById('phone');

	if (what == 'email')
	{
		if (email)
			email.style.display = 'block';
	
		if (phone)
			phone.style.display = 'none';
	}
	else
	{
		if (phone)
			phone.style.display = 'block';
	
		if (email)
			email.style.display = 'none';
	}
}

function checkForm(form)
{
	var error = false;

	var msg = "You must correct the following errors to send this message.\n" ;

//	var form = document.forms[0];

	if (!form.name.value || form.name.value == ' ')
	{
		msg += "    + Please provide your name\n";
		error = true;
	}

	if (form.contact_type[0].checked)
	{
		if (	!form.phone1.value || form.phone1.value == ' ' ||
			!form.phone2.value || form.phone2.value == ' ' ||
			!form.phone2.value || form.phone2.value == ' '
		)
		{
			msg += "    + Please provide a complete phone number\n";
			error = true;
		}
	}
	if (form.contact_type[1].checked)
	{
		if (!form.nospam.value || form.nospam.value == ' ' )
		{
			msg += "    + Please provide your email address\n";
			error = true;
		}
		if (!echeck(form.nospam.value))
		{
			msg += "    + Please provide a valid email address\n";
			error = true;
		}
	}
	if (!form.message.value || form.message.value == ' ' || form.message.value == 'How can we help you?')
	{
		msg += "    + Please provide a brief message\n";
		error = true;
	}

	if (error)
	{
		alert(msg);
		return false;
	}
	
	return true;
}

function sendForm(theform, evt, name)
{
	if (!checkForm(theform))
	{
		return;
	}
	
	AjaxRequest.submit(
		theform,
		{
			'onSuccess':function(req){ alert( req.responseText );},
			'onError':function(req){ alert( 'Error: ' + req.responseText );}
		}
	);
	
	theform.innerHTML = "testing";
}


function echeck(str)
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}
	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    return false
	 }

	 return true					
}


// menu stuff

var ie4=document.all;
var ns6=document.getElementById&&!document.all;
var disappeardelay = 250;


function clearhidemenu()
{
	if (typeof delayhide!="undefined")
		clearTimeout(delayhide);
}

function contains_ns6(a, b)
{
	while (b.parentNode)
		if ((b = b.parentNode) == a)
			return true;
	return false;
}

function dynamichide(e)
{
	if (ie4&&!dropmenuobj.contains(e.toElement))
		menuoff();
	else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
		menuoff();
}

function populatemenu(what)
{
	var currentmenu = document.getElementById ? document.getElementById(what) : what;
	if (ie4||ns6)
	{
		if (currentmenu != null)
		{
			dropmenuobj.innerHTML = currentmenu.innerHTML;
			return true;
		}
		else
		{
			//dropmenuobj.innerHTML = "Error Loading Menu ... Sorry!";
			return false;
		}
	}
	return true;
}

function showhide(obj, e, visible, hidden, menuwidth)
{
	if (ie4||ns6)
		dropmenuobj.style.left=dropmenuobj.style.top="-500px";
	if (menuwidth!="")
	{
		dropmenuobj.widthobj=dropmenuobj.style;
		dropmenuobj.widthobj.width=menuwidth +"px";
	}
	if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
		obj.visibility=visible;
	else if (e.type=="click")
		obj.visibility=hidden;
}

function getposOffset(what, offsettype)
{
	var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
	var parentEl=what.offsetParent;
	while (parentEl!=null)
	{
		totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	return totaloffset;
}

function iecompattest()
{
	return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement: document.body;
}

function clearbrowseredge(obj, whichedge)
{
	var edgeoffset = 0;
	if (whichedge=="rightedge")
	{
		var windowedge=ie4 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15;
		dropmenuobj.contentmeasure=dropmenuobj.offsetWidth;
		if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
			edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth
	}
	else
	{
		var topedge=ie4 && !window.opera? iecompattest().scrollTop : window.pageYOffset
		var windowedge=ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
		dropmenuobj.contentmeasure=dropmenuobj.offsetHeight;
		if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
		{ //move up?
			edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight
			if ((dropmenuobj.y-topedge)<dropmenuobj.contentmeasure) //up no good either?
				edgeoffset=dropmenuobj.y+obj.offsetHeight-topedge
		}
	}
	return edgeoffset;
}

function clickreturnvalue()
{
	if (ie4||ns6) return false
	else return true
}

function menuon(obj, e, menuname, mwidth)
{
	if (!pageLoaded)
		return;
	if (typeof mwidth == "undefined")
	{
		mwidth=150;
	}
	if (window.event)
		event.cancelBubble=true;
	else if (e.stopPropagation)
		e.stopPropagation();

	clearhidemenu();
	dropmenuobj = document.getElementById ? document.getElementById("dropmenudiv") : dropmenudiv;
	if (populatemenu(menuname) == false)
	{
		hidemenu(e);
		return true;
	}
	if (ie4||ns6)
	{
		showhide(dropmenuobj.style, e, "visible", "hidden",mwidth);
		dropmenuobj.x = getposOffset(obj, "left");
		//dropmenuobj.y = getposOffset(obj, "top");
		dropmenuobj.y = 115;
		dropmenuobj.style.left = dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px";
		//dropmenuobj.style.top = dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px";
		dropmenuobj.style.top = "115px";
	}
	return clickreturnvalue();
}

function hidemenu(e)
{
	if (typeof dropmenuobj!="undefined")
	{
		if (ie4||ns6)
			dropmenuobj.style.visibility="hidden"
	}
}

function menuoff()
{
	if (ie4||ns6)
		delayhide=setTimeout("hidemenu()",disappeardelay)
}


function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// lightbox stuff

function showExternalLightbox(link,e,file)
{
	var mask = document.getElementById('lightbox-mask');
	var lightbox = document.getElementById('lightbox');

        var arrayPageSize = getPageSize();
        var arrayPageScroll = getPageScroll();

        mask.style.height = (arrayPageSize[1] + 'px');
        mask.style.display = 'block';

        lightbox.innerHTML = 'loading';
        lightbox.style.display = 'block';
	centerLightbox();
	lightbox.style.visibility = 'visible';


	AjaxRequest.get(
		{
			'url': file,
			'onSuccess':function(req){ lightbox.innerHTML = req.responseText; centerLightbox() },
			'onError':function(req){ lightbox.innerHTML = "We encountered an unexpected server error.  Please try again!"}
		}
	);
	
	centerLightbox();

}

function centerLightbox()
{
	var lightbox = document.getElementById('lightbox');
	var mask = document.getElementById('lightbox-mask');

	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - lightbox.clientHeight) / 2);
        var lightboxLeft = ((arrayPageSize[0] - lightbox.clientWidth) / 2);

        lightbox.style.top = (lightboxTop < 0) ? "0" : lightboxTop + "px";
        lightbox.style.left = (lightboxLeft < 0) ? "0" : lightboxLeft + "px";

        // Hide select boxes as they will 'peek' through the image in IE
        selects = document.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++) {
                selects[i].style.visibility = "hidden";
        }

	lightbox.style.visibility = 'visible';

        arrayPageSize = getPageSize();
        mask.style.height = (arrayPageSize[1] + 'px');
}

function showLightbox(link,e,name)
{
        var mask = document.getElementById('lightbox-mask');
        var lightbox = document.getElementById('lightbox');
        var content = document.getElementById(name);

        if (!content) return;

        var arrayPageSize = getPageSize();
        var arrayPageScroll = getPageScroll();

        mask.style.height = (arrayPageSize[1] + 'px');
        mask.style.display = 'block';

        lightbox.innerHTML = content.innerHTML;
        lightbox.style.display = 'block';

        var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - lightbox.clientHeight) / 2);
        var lightboxLeft = ((arrayPageSize[0] - lightbox.clientWidth) / 2);

        lightbox.style.top = (lightboxTop < 0) ? "0" : lightboxTop + "px";
        lightbox.style.left = (lightboxLeft < 0) ? "0" : lightboxLeft + "px";

        // Hide select boxes as they will 'peek' through the image in IE
        selects = document.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++) {
                selects[i].style.visibility = "hidden";
        }

        lightbox.style.visibility = 'visible';

        arrayPageSize = getPageSize();
        mask.style.height = (arrayPageSize[1] + 'px');
}

function hideLightbox()
{
        var mask = document.getElementById('lightbox-mask');
        var lightbox = document.getElementById('lightbox');

        // hide lightbox and overlay
        mask.style.display = 'none';
        lightbox.style.display = 'none';
        lightbox.style.visibility = 'hidden';

        // make select boxes visible
        selects = document.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++) {
                selects[i].style.visibility = "visible";
        }
}


// generic stuff

function fixSinglePNG(element) 
{
	if(browser.ie && !browser.ie7)
	{
		if(element)
		{
			var width = element.width;
			var height = element.height;
			var source = element.src;
			
			element.src = '/media/spacer.gif';
			element.width = width;
			element.height = height;
			element.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + source + "', sizingMethod='image', enabled='true')";
			element.style.visibility = 'visible';
		}
	}
}

function fixBackgroundPNG(element) 
{
	if(browser.ie && !browser.ie7)
	{
		if(element)
		{
			var bg = element.currentStyle.backgroundImage;
			var img = bg.substring(5,bg.length-2);
			var position = element.style.backgroundPosition;
			
			element.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"', sizingMethod='scale')";
			element.style.backgroundImage = "url('/media/spacer.gif')";
			element.style.backgroundPosition = position; // reapply
			element.style.visibility = 'visible';
		}
	}
}

function addLoadEvent(_function)
{
        var _onload = window.onload;
        if ( typeof window.onload != 'function' )
        {
                if ( window.onload )
                {
                        window.onload = _function;
                }
                else
                {
                        var _addEventListener = window.addEventListener || document.addEventListener;
                        var _attachEvent = window.attachEvent || document.attachEvent;
                        if ( _addEventListener )
                        {
                                _addEventListener('load', _function, true);
                                return true;
                        }
                        else if ( _attachEvent )
                        {
                                var _result = _attachEvent('onload', _function);
                                return _result;
                        }
                        else
                        {
                                //todo: preloading fix for ie5.2 on mac os
                                return false;
                        }
                }
        }
        else
        {
                window.onload = function() {
                        _onload();
                        _function();
                }
        }
}

function initPage()
{
	pageLoaded = true;
	fixBackgroundPNG(document.getElementById('lightbox-mask'));
}


addLoadEvent(initPage);
