// Body onload/onunload utility (supports multiple onload/onunload functions)
var gSafeOnload = new Array();
var gSafeOnunload = new Array();

// to add an onLoad event just call this function from your .js script
// i.e. if you have a function called myOnLoad() you would put SafeAddOnload(myOnLoad) at the bottom of your script.
function SafeAddOnload(f)
{
	// Browser Detection
	isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
	NS4 = (document.layers) ? true : false;
	IEmac = ((document.all)&&(isMac)) ? true : false;
	IE4plus = (document.all) ? true : false;
	IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
	IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
	ver4 = (NS4 || IE4plus) ? true : false;
	NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;

	if (IEmac && IE4)  // IE 4.5 blows out on testing window.onload
	{
		window.onload = SafeOnload;
		gSafeOnload[gSafeOnload.length] = f;
	}
	else if  (window.onload)
	{
		if (window.onload != SafeOnload)
		{
			gSafeOnload[0] = window.onload;
			window.onload = SafeOnload;
		}
		gSafeOnload[gSafeOnload.length] = f;
	}
	else
	{
		window.onload = f;
	}
}
function SafeAddOnunload(f)
{
	// Browser Detection
	isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
	NS4 = (document.layers) ? true : false;
	IEmac = ((document.all)&&(isMac)) ? true : false;
	IE4plus = (document.all) ? true : false;
	IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
	IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
	ver4 = (NS4 || IE4plus) ? true : false;
	NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;

	if (IEmac && IE4)  // IE 4.5 blows out on testing window.onload
	{
		window.onunload = SafeOnunload;
		gSafeOnunload[gSafeOnunload.length] = f;
	}
	else if  (window.onunload)
	{
		if (window.onunload != SafeOnunload)
		{
			gSafeOnunload[0] = window.onunload;
			window.onunload = SafeOnunload;
		}
		gSafeOnunload[gSafeOnunload.length] = f;
	}
	else
		window.onunload = f;
}
function SafeOnload()
{
	for (var i=0; i < gSafeOnload.length; i++)
		gSafeOnload[i]();
}
function SafeOnunload()
{
	for (var i=0;i<gSafeOnunload.length;i++)
		gSafeOnunload[i]();
}

function selectAll(form_obj, name)
{ 
	for(var i = 0; i < form_obj.elements.length; i++)
	{
		var elem = form_obj.elements[i];
		if(elem.type == 'checkbox' && elem.name == name)
		{
			elem.checked = true;
		}
	}
	return;
}
function selectNone(form_obj, name)
{
	for(var i = 0; i < form_obj.elements.length; i++)
	{
		var elem = form_obj.elements[i];
		if(elem.type == 'checkbox' && elem.name == name)
		{
			elem.checked = false;
		}
	}
	return;
}
function selectInverse(form_obj, name)
{
	for(var i = 0; i < form_obj.elements.length; i++)
	{
		var elem = form_obj.elements[i];
		if(elem.type == 'checkbox' && elem.name == name)
		{
			elem.checked = elem.checked ? false: true;
		}
	}
	return;
}
function toggleObjectVisibility(obj_id)
{
	var obj = getObject(obj_id);
	if(obj && document.getElementsByTagName)
	{
		if(obj.style.visibility == 'visible')
		{
			obj.style.display = 'none';
			obj.style.visibility = 'hidden';
		}
		else
		{
			obj.style.display = '';
			obj.style.visibility = 'visible';
		}
	}
}
function toggleObjectCurrent(obj_id)
{
	var obj = getObject(obj_id);
	if(obj && document.getElementsByTagName)
	{
		if(obj.className == 'command')
		{
			obj.className = 'command current';
		}
		else
		{
			obj.className = 'command';
		}
	}
}
function toggleShowHideMoreInfo(obj_id)
{
	var obj = getObject(obj_id);
	if(obj.src.lastIndexOf('/') != -1)
	{
		var image_dir = obj.src.substring(0,obj.src.lastIndexOf('/')+1);
		if(obj.src == image_dir + 'btn_show_more_info.gif')
			obj.src = image_dir + 'btn_hide_more_info.gif';
		else
			obj.src = image_dir + 'btn_show_more_info.gif';
	}
}
function getObject(objectId)
{
	// cross-browser function to get an object's style object given its id
	if(document.getElementById && document.getElementById(objectId)) {
		// W3C DOM
		return document.getElementById(objectId);
	} else if (document.all && document.all(objectId)) {
		// MSIE 4 DOM
		return document.all(objectId);
	} else if (document.layers && document.layers[objectId]) {
		// NN 4 DOM.. note: this won't find nested layers
		return document.layers[objectId];
	} else {
		return false;
	}
}

//When document loads, apply the prepareImageSwap function to various images with our desired settings
function setImageSwaps() {
	//Mousedown, restore - for images in container with ID=example2
	//prepareImageSwap('client_login',true,true,true,true);
	//Hover with restore, most basic usage - for any image in document.body that are not yet processed (function accepts elements,too)
	prepareImageSwap(document.body);
	//Note that once an image is processed, it won't be processed again, so you should set more specific images first, e.g. document.body, as it is the grand
	//container, has to be processed last.
}

//The following is the function that do the actual job
function prepareImageSwap(elem,mouseOver,mouseOutRestore,mouseDown,mouseUpRestore,mouseOut,mouseUp) {
	//Do not delete these comments.
	//Non-Obtrusive Image Swap Script by Hesido.com
	//V1.1
	//Attribution required on all accounts
	if (typeof(elem) == 'string') elem = document.getElementById(elem);
	if (elem == null) return;
	var regg = /(.*)(_nm\.)([^\.]{3,4})$/
	var prel = new Array(), img, imgList, imgsrc, mtchd;
	imgList = elem.getElementsByTagName('img');
	for (var i=0; img = imgList[i]; i++) {
		if (!img.rolloverSet && img.src.match(regg)) {
			mtchd = img.src.match(regg);
			img.hoverSRC = mtchd[1]+'_hv.'+ mtchd[3];
			img.outSRC = img.src;
			if (typeof(mouseOver) != 'undefined') {
				img.hoverSRC = (mouseOver) ? mtchd[1]+'_hv.'+ mtchd[3] : false;
				img.outSRC = (mouseOut) ? mtchd[1]+'_ou.'+ mtchd[3] : (mouseOver && mouseOutRestore) ? img.src : false;
				img.mdownSRC = (mouseDown) ? mtchd[1]+'_md.' + mtchd[3] : false;
				img.mupSRC = (mouseUp) ? mtchd[1]+'_mu.' + mtchd[3] : (mouseOver && mouseDown && mouseUpRestore) ? img.hoverSRC : (mouseDown && mouseUpRestore) ? img.src : false;
			}
			if (img.hoverSRC) {preLoadImg(img.hoverSRC); img.onmouseover = imgHoverSwap;}
			if (img.outSRC) {preLoadImg(img.outSRC); img.onmouseout = imgOutSwap;}
			if (img.mdownSRC) {preLoadImg(img.mdownSRC); img.onmousedown = imgMouseDownSwap;}
			if (img.mupSRC) {preLoadImg(img.mupSRC); img.onmouseup = imgMouseUpSwap;}
			img.rolloverSet = true;
		}
	}
	function preLoadImg(imgSrc) {
		prel[prel.length] = new Image(); prel[prel.length-1].src = imgSrc;
	}
}
function imgHoverSwap() {this.src = this.hoverSRC;}
function imgOutSwap() {this.src = this.outSRC;}
function imgMouseDownSwap() {this.src = this.mdownSRC;}
function imgMouseUpSwap() {this.src = this.mupSRC;}
SafeAddOnload(setImageSwaps);

function GetXmlHttpObject()
{
	var xmlHttp = null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
		//Internet Explorer
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
function stateChanged(obj_id)
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		var obj = getObject(obj_id);
		var xmlDoc = xmlHttp.responseXML;
		obj.innerHTML = xmlDoc
	}
}
var current_page ='';
var current_tier ='';
var current_plus = 0;
months     = new Array();
months[1]  = 31;
months[2]  = 28;
months[3]  = 31;
months[4]  = 30;
months[5]  = 31;
months[6]  = 30;
months[7]  = 31;
months[8]  = 31;
months[9]  = 30;
months[10] = 31;
months[11] = 30;
months[12] = 31;
month_names     = new Array();
month_names[0]  = 'Jan';
month_names[1]  = 'Feb';
month_names[2]  = 'Mar';
month_names[3]  = 'Apr';
month_names[4]  = 'May';
month_names[5]  = 'Jun';
month_names[6]  = 'Jul';
month_names[7]  = 'Aug';
month_names[8]  = 'Sep';
month_names[9]  = 'Oct';
month_names[10] = 'Nov';
month_names[11] = 'Dec';
function openstats(site_id,tier)
{
	xmlHttp = GetXmlHttpObject()
	if (xmlHttp == null)
	{
		alert ("Browser does not support AJAX")
		return
	}
	current_page = 'summary';
	toggleObjectVisibility('s_'+tier);
	toggleObjectCurrent('b_'+tier);
	change_page(current_page,site_id,tier);
}
function change_page(page,site_id,start,end)
{
	xmlHttp = GetXmlHttpObject()
	if (xmlHttp == null)
	{
		alert ("Browser does not support AJAX")
		return
	}
	var stats_obj = getObject('nyroModalContent');
	current_page = page;
	if (page == 'summary')
	{
		stats_obj.innerHTML = 'Loading Summary';
		var url = 'ajax.php?cmd=summary&id='+site_id;
 		xmlHttp.open("GET", url, false);
		xmlHttp.send(null);
		if (xmlHttp.readyState == 4)
		{
			stats_obj.innerHTML = xmlHttp.responseText;
		}
	}
	else if (page == 'summary2')
	{
		stats_obj.innerHTML = 'Loading Summary';
		var url = 'ajax.php?cmd=summary&id='+site_id+'&start_stamp='+start+'&end_stamp='+end;
 		xmlHttp.open("GET", url, false);
		xmlHttp.send(null);
		if (xmlHttp.readyState == 4)
		{
			stats_obj.innerHTML = xmlHttp.responseText;
		}
	}
	else if (page == 'referrers')
	{
		stats_obj.innerHTML = 'Loading Referer List';
		var url = 'ajax.php?cmd=referers&id='+site_id;
 		xmlHttp.open("GET", url, false);
		xmlHttp.send(null);
		if (xmlHttp.readyState == 4)
		{
			stats_obj.innerHTML = xmlHttp.responseText;
		}
	}
	else if (page == 'domains')
	{
		stats_obj.innerHTML = 'Loading Domain Referer List';
		var url = 'ajax.php?cmd=domains&id='+site_id;
 		xmlHttp.open("GET", url, false);
		xmlHttp.send(null);
		if (xmlHttp.readyState == 4)
		{
			stats_obj.innerHTML = xmlHttp.responseText;
		}
	}
	else if (page == 'search')
	{
		stats_obj.innerHTML = 'Loading Search Keywords';
		var url = 'ajax.php?cmd=search&id='+site_id;
 		xmlHttp.open("GET", url, false);
		xmlHttp.send(null);
		if (xmlHttp.readyState == 4)
		{
			stats_obj.innerHTML = xmlHttp.responseText;
		}
	}
	else if (page == 'visits')
	{
		stats_obj.innerHTML = 'Loading Recent Visitors';
		var url = 'ajax.php?cmd=visits&id='+site_id;
 		xmlHttp.open("GET", url, false);
		xmlHttp.send(null);
		if (xmlHttp.readyState == 4)
		{
			stats_obj.innerHTML = xmlHttp.responseText;
		}
	}
	else if (page == 'browsers')
	{
		stats_obj.innerHTML = 'Loading Browsers';
		var url = 'ajax.php?cmd=browsers&id='+site_id;
 		xmlHttp.open("GET", url, false);
		xmlHttp.send(null);
		if (xmlHttp.readyState == 4)
		{
			stats_obj.innerHTML = xmlHttp.responseText;
		}
	}
}

function movegraphleft(site_id,page,start_stamp,end_stamp)
{
	current_plus = (current_plus-3);
	stamp_start = addDays(current_plus,start_stamp);
	stamp_end = addDays(current_plus,end_stamp);
	document.getElementById('startdate').innerHTML = formdate(stamp_start);
	document.getElementById('enddate').innerHTML = formdate(stamp_end);
	document.getElementById('graph').src = 'graphs.php?id='+site_id+'&rand='+Math.random()+'&time='+time+'&start_stamp='+stamp_start+'&end_stamp='+stamp_end;
}
function movegraphright(site_id,page,start_stamp,end_stamp)
{
	current_plus = (current_plus+3);
	stamp_start = addDays(current_plus,start_stamp);
	stamp_end = addDays(current_plus,end_stamp);
	document.getElementById('startdate').innerHTML = formdate(stamp_start);
	document.getElementById('enddate').innerHTML = formdate(stamp_end);
	document.getElementById('graph').src = 'graphs.php?id='+site_id+'&rand='+Math.random()+'&start_stamp='+stamp_start+'&end_stamp='+stamp_end;
}
function addDays(days,date) 
{
	return (date + (days*86400));
}
function formdate(stamp)
{
	var curdate = new Date((stamp*1000));
	var mday = curdate.getDate();
	var month = curdate.getMonth();
	return mday+' '+month_names[month];
}



startList = function() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("nav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}


