try { document.execCommand("BackgroundImageCache", false, true); } catch(err) {}

/* sitewide onload */
window.onload = function(){
	var unfield = document.getElementById("usrname");
	if(unfield) defaultTextbox(unfield,"Username");

	var pwfield = document.getElementById("pwd");
	if(pwfield) defaultTextbox(pwfield,"Password");

	var searchbutton = document.getElementById('bannerSearchButton');
	if(searchbutton) {
		searchbutton.onmouseover=function(){addClass(this,'searchbtnhover')};
		searchbutton.onmousedown=function(){addClass(this,'searchbtnclick')};
		searchbutton.onmouseup=function(){removeClass(this,'searchbtnclick')};
		searchbutton.onmouseout=function(){removeClass(this,'searchbtnhover')};
	}
	var loginsubmit = document.getElementById('loginsubmit');
	if(loginsubmit) {
		loginsubmit.onmouseover=function(){addClass(this,'loginbtnhover')};
		loginsubmit.onmousedown=function(){addClass(this,'loginbtnclick')};
		loginsubmit.onmouseup=function(){removeClass(this,'loginbtnclick')};
		loginsubmit.onmouseout=function(){removeClass(this,'loginbtnhover')};
	}
	facetmodal.init();
}

function defaultTextbox(elm,str){
	if(elm.nodeName && typeof(str)=='string'){
		elm.value=str;
		elm.onblur=function(){if(this.value=='')this.value=str;};
		elm.onfocus=function(){if(this.value==str)this.value='';};
	}
}
function validateMinMax(o,p,mn,mx){
	if(o[p]){
		var val = o[p].value;
		if(parseInt(val,10) >= mn && parseInt(val,10) <= mx)
			return true;
		alert("Please enter a number between "+mn+" and "+mx+".");
	}
	return false;
}

/* Home Page */
var carousel={
	current:0,
	base:'carouselImg',
	total:-1,
	sel:"carsel",
	timeoutId:0,
	init:function(){
		var img_base = "/static/images/";
		var car_prev = document.getElementById('car_prev');
		if(car_prev){
			car_prev.onmouseover=function(){this.src=img_base+"carousel_prev_on.gif";}
			car_prev.onmouseout=function(){this.src=img_base+"carousel_prev.gif";}
		}
		var car_next = document.getElementById('car_next');
		if(car_next){
			car_next.onmouseover=function(){this.src=img_base+"carousel_next_on.gif";}
			car_next.onmouseout=function(){this.src=img_base+"carousel_next.gif";}
		}
		carousel.show(0);
	},
	next:function(){
		if(carousel.total<1) return;
		return carousel.show((carousel.current==carousel.total-1)?0:carousel.current+1);
	},
	prev:function(){
		if(carousel.total<1) return;
		return carousel.show((carousel.current==0)?carousel.total-1:carousel.current-1);
	},
	show:function(n){
		var gid=function(id){return document.getElementById(id);};
		removeClass(gid("carouselbtn"+carousel.current),carousel.sel);
		carousel.current=n;
		if(carousel.total<0){carousel.total=gid("carouselholder").getElementsByTagName('img').length;}
		for(var i = 0; i < carousel.total; i++) {
			if(i==n) {
				gid(carousel.base+i).style.display='block';
				addClass(gid("carouselbtn"+n),carousel.sel);
			}
			else gid(carousel.base+i).style.display='none';
		}
		if(carousel.timeoutId==0){carousel.timeoutId = setTimeout("carousel.rotate()", 7000);}
		
		return false;
	},
	rotate:function(){
		carousel.next();
		carousel.timeoutId = setTimeout("carousel.rotate()", 7000);
	},
	stopRotate:function(){
		clearTimeout(carousel.timeoutId);
		carousel.timeoutId = 0;
	}
};

var hometabs={
	active:1,
	show:function(i){
		if(hometabs.active==i) return;
		var o = document.getElementById('hometabs');
		addClass(o,'showtab'+i);
		removeClass(o,'showtab'+hometabs.active);
		hometabs.active=i;
	}
}

var informz = {
	id:"NewsletterPop",
	errorid:"NewsletterPopErrors",
	show:function(){
		var o = document.getElementById(informz.id);
		informz.showerror(); // to clear any errors
		if(o) o.style.display = 'block';
	},
	hide:function(){
		var o = document.getElementById(informz.id);
		if(o) o.style.display = 'none';
	},
	onsubmit:function(f){
		if(f && f.email) {
			var error=null;
			if(f.personal_171.value == '') error="Please enter a First Name.";
			else if(f.personal_172.value == '') error="Please enter a Last Name.";
			else if(f.personal_173.value == '') error="Please enter an Organization.";
			else if(f.personal_174.value == '') error="Please enter a Title.";
			else if(f.email.value == '') error="Please enter an e-mail address.";
			else if(!f.email.value.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) error="The e-mail address is invalid.  Please try again.";
			if(!error) {
				informz.showerror();
				return true;
			}
			informz.showerror(error);
		}
		return false;
	},
	showerror:function(s){
		var p = document.getElementById(informz.errorid);
		if(p) p.innerHTML=(s)?"<p>"+s+"</p>":'';
	}
};

/* search pages */
var facetmodal = {
	modals:{},
	init:function(){
		var elms = document.getElementsByTagName("*");
		for(var i = 0; i<elms.length; i++) {
			if(hasClass(elms[i],"facetmodal")) {
				var classes = elms[i].className.split(' ');
				for(var j = 0; j<classes.length; j++) {
					if(classes[j] && classes[j]!='facetmodal' && !hasClass(elms[i],'modal')) {
						facetmodal.modals[classes[j]] = elms[i];
						facetmodal.modalfy(elms[i],classes[j]);
					}
				}
			}
		}
	},
	modalfy:function(o,key){
		var header = o.getElementsByTagName('div')[0];
		var closefunc = function(){facetmodal.hide(key);}

		if(header){
			var div = document.createElement('div');
			div.className = "close";
			var a = document.createElement('a');
			a.href = "javascript:void(0);";
			a.onclick = closefunc;
			a.className = "closeButton";
			var img = document.createElement('img');
			img.src = "/static/images/facet_model_close_btn.gif";
			img.alt = "X";
			img.className = "Xbtn";
			a.appendChild(img);
			div.appendChild(a);
			header.appendChild(div)
		};
        var close = document.createElement('div');
        close.className = "closebtn";
		var closebutton = document.createElement('a');
		closebutton.href = "javascript:void(0);";
		closebutton.onclick = closefunc;
		closebutton.innerHTML = "Close";
		closebutton.className = "closebutton";
		close.appendChild(closebutton);
        o.appendChild(close);

		/* get it where nothing will float on top of it */
		o.style.visibility = "hidden";
		o.style.display = "block";
		var toppx = o.offsetTop;
		document.getElementById("backgroundWrap").appendChild(o);
		o.style.display = "none";
		o.style.visibility = "visible";
		o.style.top = toppx+'px';
		addClass(o,"modal");
	},
	hide:function(key){
		var m = facetmodal.modals[key];
		if(m) m.style.display='none';
	},
	show:function(key){
		var m = facetmodal.modals[key];
		if(m) m.style.display='block';
	},
	show2:function(key, top, left){
		var m = facetmodal.modals[key];
		if(m){
			m.style.display='block';
			m.style.top=top+'px';
			m.style.left=left+'px';
		}
	}
}

/* Forums */
function quoteOriginal(){
	var ta = document.getElementById("postmessage");
	if(ta) { ta.value += "\n" + originalmessage.author + " writes: " + originalmessage.messagebody.replace(/&#013;|&#13;/g,"\r") }
};

function onSubmitSiteSearch(f) {
	if(f.searchtype && f.searchtype[0].checked){ f.action ="/search"; }
	else if(f.searchtype && f.searchtype[1].checked){ f.action ="/networking/people"; }
	return true;
}

/* Utility */
function addClass(elem, class_name){
    if(!hasClass(elem, class_name)) {
        elem.className += " " + class_name;
    }
}

function removeClass(elem, class_name){
    if(hasClass(elem, class_name)) {
        var regEx = new RegExp("\\b"+class_name+"\\b","g");
        elem.className = elem.className.replace(regEx, "");
    }
}
function hasClass(elem, class_name) {
    var regEx = new RegExp("\\b"+class_name+"\\b");
    var m = elem.className.match(regEx);
    return (m && m.length>0)?true:false;
}

function changepic(elem_id, img_src) {document[elem_id].src = img_src;}

function openInFrame(url, name){
	frames[name].location.href = url;
}
