/****************************************************************************************************** JOEL/JUSTIN **/

function hidePages() {
	document.getElementById('gpages').className="tabs";
}

function toggleVisibility(target) {
	(document.getElementById(target))?(target=document.getElementById(target)):(target=target);
	(target.className=='tabbox show')?(target.className='tabbox hide'):(target.className='tabbox show');
}

function togglePage(target) {
	var old_select = document.getElementsByClassName('select')[0];
	old_select.className = old_select.className.replace('select','');
	document.getElementsByClassName(target)[0].className = target + ' select';
	document.getElementsByClassName('tabbox').each(function(tabbox){
		if (tabbox.tagName.toLowerCase() == 'dl') { 
			tabbox.className = (tabbox.id == target) ? 'tabbox show' : 'tabbox hide'; 
		}
	});
}

function enableSwitcheroo(target) {
	var allPlans = document.getElementsByClassName('plan').each(function(plan){ plan.className = plan.className.replace('highlight',''); });
	if (target.checked == true) { target.parentNode.parentNode.className = target.parentNode.parentNode.className + ' highlighted'; }
}


/****************************************************************************************************** KATY **/



var last_el = '';
var last_ev = '';
function track (root,rid,sid,cat,el,ev) {
	
	if (cat=='assessments' && el==last_el && ev==last_ev) return; 
		
	last_el = el;
	last_ev = ev;
	
	var qs = "responder_id="+rid+"&sid="+sid+"&category="+cat+"&element="+el+"&event="+ev;
	sendRequest(root+'/handlers/post_track.php',doNothing,qs);
}

ask_about_overlay();
var overlay_timer = window.setInterval("ask_about_overlay()",30000);

function hide_overlay () { // this is only called when user clicks continue button
	sendRequest(webroot+'/handlers/ajax_overlay.php',doNothing,'sid='+sess_id+'&q=continue');
	hide_standard_overlay();
}
function hide_standard_overlay () { // this is called when user clicks continue button, or when force restart is triggered
	var ol = document.getElementById('standard_overlay');
	ol.style.display = 'none';
}
function show_overlay (which) {
	if (which=='force') {
		hide_standard_overlay();
		window.clearInterval(overlay_timer);
	}
	var ol = document.getElementById(which+'_overlay');
	ol.style.display = 'block';
}
function ask_about_overlay () {
	sendRequest(webroot+'/handlers/ajax_overlay.php',require_overlay,'sid='+sess_id+'&q=overlay')
}
function require_overlay (req) {
	if (req.responseText=='standard') show_overlay('standard');
	else if (req.responseText=='force') {
		if (page_id=='ftaf') window.close();
		else show_overlay('force');
	}
}



/*** track the viewport size ***/

function getViewportSize (xory) {
	// the guts of this function come from ppk
	// ... not that there are many other ways to accomplish this
	
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	
	if 		(xory=='x') return x;
	else if (xory=='y') return y;
	else 	return x + ' x ' + y;
}


/*** pop up window ***/

function popWindow (url,w,h,name) {
	
	if (!url) return false;
	if (!w) w=800;
	if (!h) h=600;
	if (!name) name='_blank';
	
	var w = window.open(url,name,"width="+w+",height="+h+",scrollbars");
}



/***  A J A X    F U N C S   (ppk) ***/

function sendRequest(url,callback,postData) {
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
//			alert('HTTP error ' + req.status);
			return;
		}
		callback(req);
	}
	if (req.readyState == 4) return;
	req.send(postData);
}

function XMLHttpFactories() {
	return [
		function () {return new XMLHttpRequest()},
		function () {return new ActiveXObject("Msxml2.XMLHTTP")},
		function () {return new ActiveXObject("Msxml3.XMLHTTP")},
		function () {return new ActiveXObject("Microsoft.XMLHTTP")}
	];
}

function createXMLHTTPObject() {
	var xmlhttp = false;
	var factories = XMLHttpFactories();
	for (var i=0;i<factories.length;i++) {
		try {
			xmlhttp = factories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}

// not from ppk
function doNothing (req) {}
