/* addEvent: simplified event attachment */
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}
	
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);
 
/* window 'load' attachment */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function toggle(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

// End lib functions

function stopEvent(ev){
	if (ev.stopPropagation) {
		ev.stopPropagation();
	} else {
		ev.cancelBubble = true;
	}
	
	if (ev.preventDefault) {
		ev.preventDefault();
	} else {
		ev.returnValue = false;
	}
}


/* This is the ajax functionality that drives the "join the family" form submission */
function ajaxRequest() 
{
	var xmlHttp;
	if (window.XMLHttpRequest) {
		try {
			xmlHttp = new XMLHttpRequest(); 
		} catch (e) {
			xmlHttp = false; 
		} // end try 
	} // end if
	else {
		if (window.ActiveXObject) {
			try { 
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
			} catch (e) { 
				xmlHttp = false; 
			} // end try
		}  // end if
	} // end if
	return xmlHttp;
} // end ajaxRequest

var rq;
var input;

var joinTheFamily = function(email) {
	
	document.getElementById('joinForm').style.display = 'none';
	
	document.getElementById('joinSuccess').innerHTML = 'Sending...';
	
	document.getElementById('joinSuccess').style.display = 'block';
	
//	input.disabled = true;
//	input.value = "Sending...";
	rq = new ajaxRequest();
	if (rq.readyState == 4 || rq.readyState == 0)	{
		rq.open("GET", "/join/" + email, true);
		rq.onreadystatechange = getResponse;
		rq.send(null);
	} // end if
	return false;
} // end joinTheFamily

function getResponse() {
	if (rq.readyState == 4) {
		//input.disabled = false;
		
		if (rq.status == 200) {
			alert(rq.responseText);
			
			if (rq.responseText.indexOf('welcome') == -1){
			
				document.getElementById('joinForm').style.display = 'block';
				document.getElementById('joinSuccess').style.display = 'none';
			
			}else{
				document.getElementById('joinSuccess').innerHTML = 'You were added.';
				document.getElementById('joinSuccess').style.display = 'block';
			}
		} else {
			document.getElementById('joinForm').style.display = 'block';
			alert("We could not subscribe you. Please try again, or contact us directly in order to subscribe.");
		}
	}
}

//on page load

addLoadEvent(function(){
	
	var search, searchResources;
	
	if (search = document.getElementById('keyword')){
	
		addEvent(search, 'focus', function(){
			
			if (search.value == 'Search'){
				search.value = '';	
			}
			
		});
		
		addEvent(search, 'blur', function(){
			
			if (search.value == ''){
				search.value = 'Search';	
			}
			
		});
		
	}
	
	if (searchResources = document.getElementById('term')){
	
		addEvent(searchResources, 'focus', function(){
			
			if (searchResources.value == 'Search tools & resources'){
				searchResources.value = '';	
			}
			
		});
		
		addEvent(searchResources, 'blur', function(){
			
			if (searchResources.value == ''){
				searchResources.value = 'Search tools & resources';	
			}
			
		});
		
	}
	
	//make print link visible
	var print = document.getElementById('print');
	
	//remove hidden class
	//print.className = '';
	
	if (print && window.print){
	
		addEvent(print, 'click', function(e){
										  stopEvent(e);
										  window.print();
										  });
	
	}
	
	//make bookmark link visible
	if (window.sidebar || window.external){
		
		var bookmark = document.getElementById('bookmark');
		
		//remove hidden class
		//bookmark.className = '';
		
		addEvent(bookmark, 'click', function(e){
											 
			  var title=document.getElementsByTagName("title")[0].text;
			  if(window.sidebar) window.sidebar.addPanel(title,location.href,"");
			  else if(window.external) window.external.AddFavorite(location.href,title);
		
				
				stopEvent(e);
		   }
		   
		);
	
	}
	
	var frm, resFrm;
	
	if (frm = document.getElementById('searchForm')){
	
		addEvent(frm, 'submit', function(e){
			
			stopEvent(e);			
			
			window.location = 'http://' + window.location.host + '/search/' + search.value;
		});
	
	}
	
	if (frm = document.getElementById('searchMain')){
	
		addEvent(frm, 'submit', function(e){
			
			stopEvent(e);
			
			
			window.location = 'http://' + window.location.host + '/search/' + document.getElementById('search-keyword').value;
		});
	
	}
	
	if (resFrm = document.getElementById('resourceSearch')){
	
		addEvent(resFrm, 'submit', function(e){
			
			stopEvent(e);
			
			window.location = resFrm.action + '/' + encodeURIComponent(document.getElementById('term').value);
		});
	
	}
	
	
	
	//email page: catch and open in new window
	var email = document.getElementById('email-page');
	addEvent(email, 'click', function(ev){
		if (ev.stopPropagation) {
			ev.stopPropagation();
		} else {
			ev.cancelBubble = true;
		}
		
		if (ev.preventDefault) {
			ev.preventDefault();
		} else {
			ev.returnValue = false;
		}
		
		window.open(ev.target || ev.srcElement,"email_window","width=350,height=500,resizable=1");
	});
	
	;
	
	if (frm = document.getElementById('joinForm')){			
		
		var emailInput = document.getElementById('email');
		
		addEvent(emailInput, 'focus', function(){
			
			if (emailInput.value == 'Enter your email address'){
				emailInput.value = '';	
			}
			
		});
		
		addEvent(emailInput, 'blur', function(){
			
			if (emailInput.value == ''){
				emailInput.value = 'Enter your email address';	
			}
			
		});
		
		addEvent(frm, 'submit', function(e){
			
			stopEvent(e);
			
			joinTheFamily(emailInput.value);
		});
	}
	
});
