var timer;
var lastTab 											= "";

function showChunk(chunkName) {
	if(lastTab != "" && $("divlayer_"+lastTab) != null) {
		$("divlayer_"+lastTab).hide();
	}
	lastTab 											= chunkName;
	if($("divlayer_"+chunkName) != null) {
		$("divlayer_"+chunkName).show();
	}
}

function resetTabs() {
	clearTimeout(timer);
	var buttonSecArray = new Array("overview", "higher", "independent", "healthcare", "religious", "professional", "environmental", "cultural", "community");
	for(var x = 0; x<buttonSecArray.length; x++) { 
		if($(buttonSecArray[x]) != undefined) {
			$(buttonSecArray[x]).src = '/images/btn_bt_'+buttonSecArray[x]+'.gif'
		}
	} 
}

function swapImage(btnName) {
	resetTabs();
	$(btnName).src 										= "/images/btn_bt_"+btnName+"_over.gif";
}

function swapBack() {
	swapImage(lastTab);
}

function swapImageBack() {
	clearTimeout(timer);
	if(lastTab!="") {
		timer											= setTimeout("swapBack()", 120);
	} else {
		resetTabs();	
	}
}

function callDefault(btnName) {
	swapImage(btnName);
	showChunk(btnName);
}

function turnontab(tab) {
	var tabArray = new Array("byclient", "bypractice", "byregion", "profile", "newstab", "speeches");
	var tabname = 'div_'+tab;
	for(var x = 0; x<tabArray.length; x++) { 
		if($(tabArray[x]) != undefined) {
			$(tabArray[x]).removeClassName('inlinetabactive');
		}
	} 
	$(tab).addClassName('inlinetabactive');
	showChunk(tab);
}

function clearSearchField() {
	if (document.search.search.value == 'Search this site...')
		document.search.search.value = '';
}

function clearEmailField() {
	if (document.ccoptin.ea.value == 'Enter e-mail address')
		document.ccoptin.ea.value = '';
}

//Change font size by swaping out css
function addStyle(href, idname) 
{ 
  var style 				= document.createElement("link"); 
  style.rel 				= "stylesheet"; 
  style.type 				= "text/css"; 
  style.href 				= href; 
  style.media 				= "screen"; 
  style.id 					= idname; 
  document.getElementsByTagName("head")[0].appendChild(style); 
} 

var lastStyleID				= "";
var changeFontSize			= function(style, idname, id) {
	if(lastStyleID!="") {
		 document.getElementsByTagName("head")[0].removeChild(document.getElementById(lastStyleID));
		 //$(lastStyleID).remove();
	}
	addStyle(style, idname);
	lastStyleID 			= idname;
	
	if($('misc_t1'))
		$('misc_t1').src 	= "/images/misc_t1.gif";
	if($('misc_t2'))
		$('misc_t2').src 	= "/images/misc_t2.gif";
	if($('misc_t3'))
		$('misc_t3').src 	= "/images/misc_t3.gif";
	if($(id))
		$(id).src 			= "/images/"+id+"_over.gif";
	
	createCookie('grahampeltonfontsize',id,7)
}

function emailpage(emailURL) {
	var temploc 			= emailURL + "?myURL=";
	temploc 				+= window.location.href.gsub(/\&/, '%26').escapeHTML().gsub(/\?/, '%3F');
	temploc 				+= "&pathURL=" + emailURL;
	window.location.href 	= temploc;
}

//COOKIE for fontsize
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

var checkUserEmail = function() {
	var theForm = document.ccoptin;
	
	var temp = theForm.ea.value;
	if ((temp == '') || (temp.charAt(0) == ' ')){
		alert("Please enter email.");
		theForm.ea.focus();
		return false;
	}
	var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
	var check=/@[\w\-]+\./;
	var checkend=/\.[a-zA-Z]{2,3}$/;

	if(((temp.search(exclude) != -1)||(temp.search(check)) == -1)||(temp.search(checkend) == -1)) {
		alert("Invalid email address.");
		theForm.ea.focus();
		return false;
	}
	
	var w = 800, h = 600;
	
	if (document.all || document.layers) {
	   w = screen.availWidth;
	   h = screen.availHeight;
	}
	
	var popW = 475, popH = 500;
	
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;
	
	//window.open('http://visitor.constantcontact.com/d.jsp?m=1102271207531&p=oi&ea='+temp,'popup','width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos);
	var ccStr = "http://visitor.constantcontact.com/d.jsp?m=1102271207531&p=oi&ea="+temp;
	pageTracker._trackPageview(ccStr); 
	window.open(ccStr,'popup','width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos);
	return false;
}
