function alwaysTrue() { return true; }
function alwaysFalse() { return false; }

function trim(str) { return str.replace(/^\s+|\s+$/g, ''); }

var YUE = YAHOO.util.Event;
var YUD = YAHOO.util.Dom;
var YUA = YAHOO.util.Anim;
var YUC = YAHOO.util.Connect;
var YUM = YAHOO.util.Motion;

var enhance = {
	init: function() {
		if (YUD.hasClass(document.body,"welcome")) welcome.init();
		if (YUD.hasClass(document.body,"issue")) issue.init();
		if (YUD.hasClass(document.body,"article")) article.init();
		if (YUD.hasClass(document.body,"search")) search.init();
		if (YUD.hasClass(document.body,"email")) email.init();
		if (YUD.hasClass(document.body,"special_issues") || YUD.hasClass(document.body,"contribute")
			|| YUD.hasClass(document.body,"conferences")) special.init();
		
		enhance.head();
	},
	head: function() {
		var h = document.getElementById("headClick");
		YUE.addListener(h,"click",welcome.go,"index.php");
		var p = document.getElementById("page");
		YUE.addListener(p,"mouseover",enhance.toggle,p);
		YUE.addListener(p,"mouseout",enhance.toggle,p);
	},
	toggle: function(e,o) {
		if (e.type == "mouseover") {
			YUD.addClass(o,"active");
			if (YUD.hasClass(o,"first")) YUD.addClass(o,"firstActive");
			if (YUD.hasClass(o,"last")) YUD.addClass(o,"lastActive");
		} else {
			YUD.removeClass(o,"active");
			if (YUD.hasClass(o,"first")) YUD.removeClass(o,"firstActive");
			if (YUD.hasClass(o,"last")) YUD.removeClass(o,"lastActive");
		}
	}
}

var special = {
	init: function() {
		var st = YUD.getElementsByClassName("specialIssueTitle");
		for (var i=0; i<st.length; i++) {
			var s = st[i].parentNode;
			YUD.addClass(s,"closed");
			YUE.addListener(st[i],"click",special.toggle,st[i]);
			YUE.addListener(st[i],"mouseover",vis.toggle,st[i]);
			YUE.addListener(st[i],"mouseout",vis.toggle,st[i]);
		}
	},
	toggle: function(e,s) {
		// if (e.type == "mouseout") {
		if (YUD.hasClass(s.parentNode,"open")) {
			YUD.removeClass(s.parentNode,"open");
			YUD.addClass(s.parentNode,"closed");
		} else {
			YUD.addClass(s.parentNode,"open");
			YUD.removeClass(s.parentNode,"closed");
		}
		vis.maxHeight();
	}
}

var anypage = {
	init: function() {
		anypage.activateSubmit();
		anypage.loginForm();
		anypage.signInOut(true);
		vis.maxHeight();
		// YUE.addListener(window,"resize",vis.maxHeight);
	},
	activateSubmit: function() {
		var s = YUD.getElementsByClassName("submit","input");
		for (i=0; i<s.length; i++) {
			YUE.addListener(s[i],"mouseover",vis.toggle,s[i]);
			YUE.addListener(s[i],"mouseout",vis.toggle,s[i]);
		}
	},
	loginForm: function() {
		var f = document.getElementById("form_login");
		if (f) {
			f.setAttribute("onsubmit","return false;");
			var s = f.getElementsByTagName("input");
			for (var i=0; i<s.length; i++) {
				if (s[i].getAttribute("type") == "submit") {
					if (YUD.hasClass(s[i].parentNode,"more")) 
						YUE.addListener(s[i],"click",forgot.fire);
					else	YUE.addListener(s[i],"click",anypage.loginSubmit,f);
				}
			}
		}
	},
	loginSubmit: function(e,f) { f.submit(); },
	
	signInOut: function(popopen) {
		var m = document.getElementById("menu");
		if (m) {
			var sa = m.getElementsByTagName("td");
			for (var i=0; i<sa.length; i++) {
				var cl = "";
				if (YUD.hasClass(sa[i],"log_in")) cl = "login";
				if (YUD.hasClass(sa[i],"account")) cl = "login";
				if (YUD.hasClass(sa[i],"search")) cl ="search";
				if (cl != "") {
					var a = sa[i].getElementsByTagName("a");
					if (a.length > 0) {
						a[0].setAttribute("href","javascript:void(0);");
						arr = Array(sa[i],cl);
						YUE.addListener(a[0],"click",anypage.loginToggle,arr);
						anypage.loginPositioning(cl);
					}
				}
			}
			anypage.loginMore();
		}
	},
	loginPositioning: function(cl) {
		var d = document.getElementById(cl);
		if (d) {
			YUD.addClass(d,"positioning");
			var x = vis.findPos(d); var y = x[1]; x = x[0];
			var w = vis.getSize(d); var h = w[1]; w = w[0];
			YUD.setStyle(d,"top",(y-h)+"px");
			setTimeout("anypage.loginPositioned('"+cl+"');",20);
			
			var b = document.getElementById("body");
			if (b) YUD.addClass(b,"positioned");
		}
	},
	loginPositioned: function(cl) {
		var d = document.getElementById(cl);
		if (d) {document.body.offsetHeight
			YUD.removeClass(d,"positioning");
			YUD.addClass(d,"positioned");
		}
	},
	loginToggle: function(e,o) {
		var m = o[0];
		var d = document.getElementById(o[1]);
		var ds = document.getElementById(o[1]+"Shell");
		if (d&&ds&&m) {
			var xy = vis.findPos(d); var y = xy[1]; x = xy[0];
			var wh = vis.getSize(d); var h = wh[1]; w = wh[0];
			if (YUD.hasClass(d,"open")) {
				YUD.removeClass(m,"open");
				YUD.removeClass(d,"open");
				vis.move(d,x,y-h,0.15);
				vis.fade(ds,"out",0.15,0);
				vis.fade(d,"out",1,0);
			} else {
				YUD.addClass(m,"open");
				YUD.addClass(d,"open");
				vis.move(d,x,y+h,0.5);
				vis.fade(d,"in",0.15,1);
				vis.fade(ds,"in",1,1);
			}
		}
	},
	loginPrompt: function(td) {
		var arr =Array();
		switch(td) {
			case "login":	arr.push("log_in"); break; // arr.push("sign_in"); arr.push("account"); break;
			case "search":	arr.push("search"); break;
		}
		var m = document.getElementById("menu");
		if (m) {
		console.info(arr);
			for (var i=0; i<arr.length; i++) {
				var tds = YUD.getElementsByClassName(arr[i],"td",m);
				if (tds.length > 0) {
					var tmp = Array(tds[0],td);
					anypage.loginToggle(null,tmp);
				}
			}
		}
	},
	
	loginMore: function() {
		var l = document.getElementById("login");
		var m = YUD.getElementsByClassName("more","a",l);
		for (var i=0; i<m.length; i++) {
			var h = m[i].getAttribute("href");
			m[i].setAttribute("href","javascript:void(0);");
			YUE.addListener(m[i],"click",anypage.loginToggleMore,m[i].parentNode);
			var d = YUD.getElementsByClassName("more","div",m[i].parentNode);
			for (var j=0; j<d.length; j++) {
				YUE.addListener(d[j],"mouseover",vis.toggle,d[j]);
				YUE.addListener(d[j],"mouseout",vis.toggle,d[j]);
				if (!YUD.hasClass(m[i].parentNode,"forgot")) YUE.addListener(d[j],"click",anypage.goUrl,h);
			}
		}
	},
	loginToggleMore: function(e,o) {
		var obj = o[0]; var href = o[1];
		if (YUD.hasClass(o,"open")) {
			YUD.removeClass(o,"open");
		} else {
			YUD.addClass(o,"open");
		}
	},
	
	goUrl: function(e,o) {
		window.location.href = o;
	}
}

var forgot = {
	email: "",
	div: null,
	
	connect: function() {
		var sUrl = '?view=forgot&email=' + forgot.email;
		var request = YUC.asyncRequest('GET', sUrl, { success:forgot.success, failure:forgot.failure });
	},
	success: function(o) { if (parseInt(o.responseText) != "FAILED") forgot.respond("An reminder email has been sent."); else forgot.failure(); },
	failure: function(o) { forgot.respond("There was a problem. Please try again."); },
	respond: function(val) {
		var p = forgot.div.parentNode;
		var fp = YUD.getElementsByClassName("response","div",p);
		if (fp.length > 0) fp[0].parentNode.removeChild(fp[0]);
		
		var r = document.createElement("div");
		YUD.addClass(r,"response");
		r.innerHTML = val;
		p.appendChild(r);
		vis.fade(r,'in',1,1);
	},
	
	fire: function() {
		var f = document.getElementById("form_login");
		if (f) {
			var inp = f.getElementsByTagName("input");
			for (var i=0; i<inp.length; i++) {
				if (inp[i].getAttribute("name") == "forgotten") { forgot.div = inp[i]; forgot.email = inp[i].value; }
			}
		}

		if (forgot.check()) { forgot.respond("Looking up your account..."); forgot.connect(); }
	},
	check: function() {
		var okay = true;
		var emv = forgot.email;
		if (emv == "" || emv.indexOf("@") == -1 || emv.indexOf("@") == 0 || emv.indexOf("@") == emv.length-1) { forgot.error("email"); okay = false; }
		return okay;
	},
	error: function(type) {
		switch(type) {
			case "email":		alert("Please enter a valid email address."); break;
		}
	}
}

var email = {
	init: function() {
		var f = document.getElementsByTagName("form");
		if (f.length > 0) YUE.addListener(f[0],"submit",email.validate, f[0]);
	},
	validate: function(e,o) {
		var varReturn = true;
		var to = YUD.getElementsByClassName("to","textarea",o);
		if (to.length > 0) {
			if (to[0].value == "" || to[0].value.indexOf("@") == -1) {
				alert("You must supply at least one valid e-mail address.");
				to[0].focus();
				varReturn = false;
			}
		}
		if (varReturn) o.submit();
	}
}

var search = {
	init: function() {
		var d = YUD.getElementsByClassName("button","div");
		var r = YUD.getElementsByClassName("result","li");
		for (var i=0; i<r.length; i++) d.push(r[i]);

		for (i=0; i<d.length; i++) {
			YUE.addListener(d[i],"mouseover",vis.toggle,d[i]);
			YUE.addListener(d[i],"mouseout",vis.toggle,d[i]);
		}
	}
}

var article = {
	init: function() {
		article.resizeURLs();
		article.activatePost();
	},
	resizeURLs: function() {
		var b = document.getElementById("body");
		if (b) {
			var a = b.getElementsByTagName("a");
			var bw = vis.getSize(b)[0] - 50;
			for (var i=0; i<a.length; i++) {
				var w = vis.getSize(a[i])[0];
				if (w > bw) {
					YUD.setStyle(a[i],"white-space","nowrap");
					w = vis.getSize(a[i])[0];
					
					var at = a[i].childNodes[0].nodeValue; var newAT = "";
					if (at) {
                        var len = at.length;
                        var avg = Math.round(w / len);
                        var line = Math.round(bw / avg);
                        var lines = Math.ceil(w / bw);
                        for (var j=0; j<lines; j++) {
                            newAT += at.substr(0,line) + " ";
                            at = at.substr(line);
                        }
                        newAT += at;
                    }
					a[i].childNodes[0].nodeValue = trim(newAT);

					YUD.setStyle(a[i],"white-space","normal");
				}
			}
		}
	},
	activatePost: function() {
		var d = YUD.getElementsByClassName("discussion");
		if (d.length > 0) {
			var f = YUD.getElementsByClassName("title","div",d[0]);
			if (f.length > 0) {
				YUE.addListener(f[0],"click",article.togglePost, f[0]);
				YUE.addListener(f[0],"mouseover",vis.toggle,f[0]);
				YUE.addListener(f[0],"mouseout",vis.toggle,f[0]);
			}
		}
	},
	togglePost: function(e,o) {
		var p = o.parentNode;
		if (YUD.hasClass(p,"expanded")) {
			YUD.removeClass(p,"expanded");
		} else {
			YUD.addClass(p,"expanded");
		}
	}
}

var issue = {
	init: function() {
		issue.balance();
		issue.description();
		// issue.foot();
	},
	balance: function() {
		ic = document.getElementById("issueContents");
		if (ic) {
			if (YUD.hasClass(ic,"horizontal")) {
				uls = ic.getElementsByTagName("ul");
				var max = 0;
				for (var i=0; i<uls.length; i++) {
					if (vis.getSize(uls[i])[1] > max) max = vis.getSize(uls[i])[1];
				}
				for (var i=0; i<uls.length; i++) {
					YUD.setStyle(uls[i],"height",max+"px");
				}
			}
		}
	},
	description: function() {
		ic = document.getElementById("issueContents");
		if (ic) {
			var uls = ic.getElementsByTagName("ul");
			for (var i=0; i < uls.length; i++) {
				var d = YUD.getElementsByClassName("description","div",uls[i]);
				if (d.length > 0) {
					YUE.addListener(d[0],"mouseover",vis.hover,d[0]);
					YUE.addListener(d[0],"mouseout",vis.hover,d[0]);
					YUE.addListener(d[0],"click",issue.toggle,d[0]);
				}
			}
		}
	},
	toggle: function(e,p) {
		p = p.parentNode;
		if (YUD.hasClass(p,"active")) {
			YUD.removeClass(p,"active");
		} else {
			YUD.addClass(p,"active");
		}
	},
	foot: function() {
		var f = document.getElementById("foot");
		var ic = document.getElementById("issueContents");
		var b = document.getElementById("body");
		if (f&&ic&&b) {
			if (YUD.hasClass(ic,"horizontal")) {
				var fc = f.cloneNode(true);
				b.appendChild(fc);
				f.parentNode.removeChild(f);
				
				var icc = ic.cloneNode(true);
				ic.parentNode.appendChild(icc);
				ic.parentNode.removeChild(ic);
			}
		}
	}
}

var welcome = {
	init: function() {
		welcome.activate();
		welcome.fadeIn();
	},
	fadeIn: function() {
		vis.fade('sun','in',5,1);
		setTimeout("vis.fade('innovate','in',4,1);",1000);
		setTimeout("vis.fade('innovate-subtitle','in',1.5,1);",3000);
		setTimeout("vis.fade('fischler','in',1.5,1);",3100);
		setTimeout("vis.fade('link','in',4,1);",6000);
	},
	activate: function() {
		var l = document.getElementById("link");
		var a = l.getElementsByTagName("a")[0];
		var href = a.getAttribute("href");

		var w = document.getElementById("welcome");
		YUD.addClass(w,"clickable");
		YUE.addListener(w,"click",welcome.go,href);
	},
	go: function(e,p) {
		window.location.href = p;
	}
}

var menuOptions = {
	init: function() {
		var t = Array();
		t.push(document.getElementById("menu"));
		t.push(document.getElementById("options"));
		for (var k=0; k<t.length; k++) {
			if (t[k]) {
				var a = t[k].getElementsByTagName("a");
				for (var i=0; i<a.length; i++) {
					YUE.addListener(a[i],"mouseover",menuOptions.toggle,a[i]);
					YUE.addListener(a[i],"mouseout",menuOptions.toggle,a[i]);
				}
			}
		}
		
		var s = YUD.getElementsByClassName("shadowed");
		for (k=0; k<s.length; k++) {
			vis.addShadow(s[k]);
		}
	},
	toggle: function(e,a) {
		var td = a.parentNode;
		if (e.type == "mouseout") {
			YUD.removeClass(td,"active");
			YUD.removeClass(td,"firstActive");
			YUD.removeClass(td,"lastActive");
		} else {
			YUD.addClass(td,"active");
			if (YUD.hasClass(td,"first")) YUD.addClass(td,"firstActive");
			if (YUD.hasClass(td,"last")) YUD.addClass(td,"lastActive");
		}
	}
}

var vis = {
	getSize: function(i) {
		h = i.offsetHeight; if (!h) h = i.style.pixelHeight; 
		w = i.offsetWidth; if (!w) w = i.style.pixelWidth;
		
		return Array(w,h);
	},
	addShadow: function(d) {
		var wh = vis.getSize(d);
		var s = document.createElement("div");
		YUD.addClass(s,"shadow");
		YUD.setStyle(s,"width",wh[0]+"px");
		YUD.setStyle(s,"height",wh[1]+"px");

		var os = YUD.getElementsByClassName("shadow","div",d);
		for (var i=0; i<os.length; i++) d.removeChild(os[i]);
		d.appendChild(s);
	},
	fade: function(e,inout,dur,max) {
		if (!dur) dur = 1; if (!max) max = 1;
		
		var myAnim = new YUA(e);
		myAnim.duration = dur;

		myAnim.attributes.opacity = { from: 0, to: max };
		if (inout == "out") myAnim.attributes.opacity = { from: max, to: 0 };
		
		myAnim.animate();
	},
	toggle: function(e,p) {
		if (e.type == "mouseover") {
			YUD.addClass(p,"active");
		} else {
			YUD.removeClass(p,"active");
		}
	},
	hover: function(e,p) {
		if (YUD.hasClass(p,"hover")) {
			YUD.removeClass(p,"hover");
		} else {
			YUD.addClass(p,"hover");
		}
	},
	move: function(obj,x,y,dur) {
		if (browser == "MSIE") { x += 3; y += 3; }
		var attributes = { 
			points: { to: [x, y] } 
		};
		var anim = new YUM(obj, attributes, dur, YAHOO.util.Easing.easeOut);
		anim.animate();
	},
	maxHeight: function() {
		var m = document.getElementById("main");
		if (m) {
			YUD.setStyle(m,"height","auto");
			var wh = window.innerHeight; if (!wh) wh = document.body.offsetHeight;
			var mh = vis.getSize(m)[1];
			if (mh < wh) YUD.setStyle(m,"height",(wh-16)+"px");
			// else YUD.setStyle(m,"height",(mh)+"px");
		}
	},
	findPos: function (obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curleft,curtop];
	}
}

var initialize = {
	page: function() {
		enhance.init();
		menuOptions.init();
		anypage.init();
	}
}
YUE.addListener(window,"load",initialize.page);

function checkPosition(box, other) {
	if (box.value == "Other") {
		document.getElementById(other).style.display = 'inline';
		document.getElementById(other).style.visibility = 'visible';
	} else {
		document.getElementById(other).style.display = 'none';
		document.getElementById(other).style.visibility = 'hidden';
	}
}

function checkFields(myform, formname) {
	problems = ""; myreturn = true;
	if (trim(myform.first_name.value) == "") { problems += "a first name, "; }
	if (trim(myform.last_name.value) == "") { problems += "a last name, "; }
	if (trim(myform.email.value) == "") { problems += "an e-mail address, "; }
	if (myform.email.value.indexOf("@") == -1) { problems += "a valid e-mail address, "; }
	if (trim(myform.title.value) == "") { problems += "a title, "; }
	if (trim(myform.position[myform.position.selectedIndex].value) == "") { problems += "a position, "; }
	if (trim(myform.organization.value) == "") { problems += "an organization, "; }
	if (trim(myform.orgtype[myform.orgtype.selectedIndex].value) == "") { problems += "an organization type, "; }
	if (trim(myform.location[myform.location.selectedIndex].value) == "") { problems += "a location, "; }
	// if (trim(myform.username.value) == "") { problems += "a user name, "; }
				
	if ((formname != 'subscribe') && (trim(myform.pass_original.value) == "")) { problems += "your current password, "; }						
	if ((formname == 'subscribe') || (formname != 'subscribe' && (myform.pass1.value != "" || myform.pass2.value != ""))) {
		if (trim(myform.pass1.value) == "") { problems += "a password, "; }						
		if (trim(myform.pass1.value).length < 6) { problems += "a password of at least 6 characters, "; }
	}
				
	if (problems != "") {
		problems = problems.substr(0,problems.length-2);
		alert("Please fill out the following fields before submitting your account request: " +problems+ ".");
		myreturn = false;
	}
				
	if (myform.pass1.value != myform.pass2.value) {
		alert("Your passwords do not match. Please try again.");
		myreturn = false;
	}
				
	return myreturn;
}

function trim(mystring) { return mystring.replace(/^\s*|\s*\$/g,""); }




/*
windowopen.js
V 1.0
copyright Andrew Holt 
This script is free to use provided this notice remains.
Full instructions can be found @ http://www.webdevtips.com/webdevtips/js/openwindow.shtml
*/

function open_win(what_link,the_x,the_y,toolbar,addressbar,directories,statusbar,menubar,scrollbar,resize,history,pos,wname){ 
var the_url = what_link;
// the_x -= 0;
// the_y -= 0;

// added 9/17/04 to correct all incoming requests with a position at the top left-hand corner of the page.
pos = 1;

var how_wide = screen.availWidth;
var how_high = screen.availHeight;
if(toolbar == "0"){var the_toolbar = "no";}else{var the_toolbar = "yes";}
if(addressbar == "0"){var the_addressbar = "no";}else{var the_addressbar = "yes";}
if(directories == "0"){var the_directories = "no";}else{var the_directories = "yes";}
if(statusbar == "0"){var the_statusbar = "no";}else{var the_statusbar = "yes";}
if(menubar == "0"){var the_menubar = "no";}else{var the_menubar = "yes";}
if(scrollbar == "0"){var the_scrollbars = "no";}else{var the_scrollbars = "yes";}
if(resize == "0"){var the_do_resize =  "no";}else{var the_do_resize = "yes";}
if(history == "0"){var the_copy_history = "no";}else{var the_copy_history = "yes";}
if(pos == 1){top_pos=0;left_pos=0;}
if(pos == 2){top_pos = 0;left_pos = (how_wide/2) -  (the_x/2);}
if(pos == 3){top_pos = 0;left_pos = how_wide - the_x;}
if(pos == 4){top_pos = (how_high/2) -  (the_y/2);left_pos = 0;}
if(pos == 5){top_pos = (how_high/2) -  (the_y/2);left_pos = (how_wide/2) -  (the_x/2);}
if(pos == 6){top_pos = (how_high/2) -  (the_y/2);left_pos = how_wide - the_x;}
if(pos == 7){top_pos = how_high - the_y;left_pos = 0;}
if(pos == 8){top_pos = how_high - the_y;left_pos = (how_wide/2) -  (the_x/2);}
if(pos == 9){top_pos = how_high - the_y;left_pos = how_wide - the_x;}
if (window.outerWidth ){
var option = "toolbar="+the_toolbar+",location="+the_addressbar+",directories="+the_directories+",status="+the_statusbar+",menubar="+the_menubar+",scrollbars="+the_scrollbars+",resizable="+the_do_resize+",outerWidth="+the_x+",outerHeight="+the_y+",copyhistory="+the_copy_history+",left="+left_pos+",top="+top_pos;
wname=window.open(the_url, wname, option);
wname.focus();
}
else
{
var option = "toolbar="+the_toolbar+",location="+the_addressbar+",directories="+the_directories+",status="+the_statusbar+",menubar="+the_menubar+",scrollbars="+the_scrollbars+",resizable="+the_do_resize+",Width="+the_x+",Height="+the_y+",copyhistory="+the_copy_history+",left="+left_pos+",top="+top_pos;
if (!wname.closed && wname.location){
wname.location.href=the_url;
}
else
{
wname=window.open(the_url, wname, option);
//wname.resizeTo(the_x,the_y);
wname.focus();
wname.location.href=the_url;
}
}
} 
