
/******************************************************************************
    H I T  C O U N T E R  S C R I P T
*******************************************************************************/
 /*
  * Hit counter main path url
  */
//hitMgtPath="http://localhost/hitmanagement/";
hitMgtPath="http://www.1upbanners.com/hitmanagement/";

 /*
  * Register this page
  * If this page doesn't exists this page will be register
  * otherwise it will not consider
  */
hitMgtDynamicScript("in/register_page.php", "registerJson");
hitMgtDeleteScript("registerJson");

 /*
  * By this function we can meke the dynamic script tag
  * By the dynamic script we can call any server side page instantly and execute that page
  */

function hitMgtDynamicScript(src, id)
{
	//Add the dynamic script into the head of the requested page.
	var registerScript=document.createElement("script");
	registerScript.src=hitMgtPath+src;
	if(navigator.appName!="Microsoft Internet Explorer")
		registerScript.id=id;
	registerScript.type="text/javascript";
	registerScript.charset="utf-8";
	var head=document.getElementsByTagName("head")[0];
	head.appendChild(registerScript);
}

 /*
  * After successfull request sending, delete the script tag, 
  * not only the tag, all properties of the script tag will be
  * delete from the memory to avoid the memory leak.
  * in IE this removeChild doesn't work, fortunately an IE is instead of add a new script node,
  * that replace the src property of the script.
  */

function hitMgtDeleteScript(id)
{
	//After successfull request sending, delete the script tag, 
	//not only the tag, all properties of the script tag will be
	//delete from the memory to avoid the memory leak.
	
	//in IE this removeChild doesn't work, fortunately an IE is instead of add a new script node,
	//that replace the src property of the script.
	var script;
	while(script=document.getElementById(id))
	{
		script.parentNode.removeChild(script);
		for(var prop in script)
		{
			delete script[prop];
		}
	}
//	while(document.body && document.body.hasChildNodes())
//	{
//		var node=document.body.firstChild;
//		document.body.removeChild(node);
//	}
}

 /*
  * Make and place the element
  * This element is using to display (hide) the response from the server
  * If it is append with body, it will append with body at last element
  */

function hitMgtSetContainer(id)
{
	// Add the container to display the response
//	var span=document.createElement("span");
//	span.id=id;
//	span.innerHTML='<div style="text-align:center"><img src="'+hitMgtPath+'loader.gif"/></div>';
//	span.style.textAlign="center";
//	var body=document.getElementsByTagName("body")[0];
//	body.appendChild(span);
	
	var span='<span id="'+id+'" >';
	span+='<div style="text-align:center"><img src="'+hitMgtPath+'loader.gif"/></div>'+'</span>';
	document.writeln(span);
}
 
 /*
  * Callback function of Hit counter
  * When the server side page will execute by the dynamic script tag
  * that will produce the responce in JSON object
  */

function hitCounterCallback(result)
{
	var hitCounterObj=eval('('+result+')');
	document.getElementById(hitCounterObj.element).innerHTML=hitCounterObj.counts;
}

 /*
  * Callback function of IP address
  * When the server side page will execute by the dynamic script tag
  * that will produce the responce in JSON object
  */

function ipAddressCallback(result)
{
	var ipAddressObj=eval('('+result+')');
	document.getElementById(ipAddressObj.element).innerHTML=ipAddressObj.ip;
}

 /*
  * Callback function of who is online
  * When the server side page will execute by the dynamic script tag
  * that will produce the responce in JSON object
  */

function usersOnlineCallback(result)
{
	var usersOnlineObj=eval('('+result+')');
	document.getElementById(usersOnlineObj.element).innerHTML=usersOnlineObj.users;
}

 /*
  * Get the hit counting value of a page
  */

function hitCounter()
{
//	if(readCookie('hit_counter_c')==null)
//	{
//		createCookie('hit_counter_c', 'hit_counter', 7);
		
		var postId=Math.ceil(Math.random()*100000);
		var element="hitCounter"+postId;
		hitMgtSetContainer(element);
		hitMgtDynamicScript("in/hit_counter.php?jsonp=hitCounterCallback&ele="+element, "hitCounterJson"); 
		if(navigator.appName!="Microsoft Internet Explorer")
			hitMgtDeleteScript("hitCounterJson");
	//}
}

 /*
  * Get a IP address of the page
  */

function IPAddress()
{
	var postId=Math.ceil(Math.random()*100000);
	var element="ipAddress"+postId;
	hitMgtSetContainer(element);
	hitMgtDynamicScript("in/hit_counter_ip_add.php?jsonp=ipAddressCallback&ele="+element, "ipAddressJson");
	if(navigator.appName!="Microsoft Internet Explorer")
		hitMgtDeleteScript("ipAddressJson");
}

 /*
  * Get the no of online users of the page
  */

function onlineUsers(container)
{
	if(container)
	{
		var postId=Math.ceil(Math.random()*100000);
		onlineUsers.element="usersOnline"+postId;
		hitMgtSetContainer(onlineUsers.element);
	}
	hitMgtDynamicScript("in/hit_counter_online_users.php?jsonp=usersOnlineCallback&ele="+onlineUsers.element, "usersOnlineJson");
	if(navigator.appName!="Microsoft Internet Explorer")
		hitMgtDeleteScript("usersOnlineJson");
	
	var t=setTimeout("onlineUsers(false)", 12000);

}

 /*
  * Create the cookie with expirity in days
  */

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=/";
}

 /*
  * Read the cookie by name
  */

function readCookie(name)
{
	var ret=null;
	var nameEq=name+"=";
	var cookieArr=document.cookie.split(";");
	var i;
	
	for(i=0; i<cookieArr.length; i++)
	{
		var cookie=cookieArr[i];
		
		var c=cookie.substring(1, cookie.length);
		
		if(c.indexOf(nameEq)==0)
		{
			ret=c.substring(nameEq.length, c.length);
			break;
		}
	}
	return ret;
}

 /*
  * Erase the cookie
  */

function eraseCookie(name)
{
	createCookie(name, "", -1);
}
