/*Specify the id of the div you want to change content of. The links should be the clas 'ajaxlink'*/

var divid= "bodymain";
var pg_loaded = "";


function CheckChange()
{
	if(window.location.href.indexOf("#")>0)
	{
		var a = window.location.href.substr(window.location.href.indexOf("#")+1);
		if((a != "") && (a.substr(0,2) != "TB"))
		{
	    	if(window.location.href.substr(window.location.href.indexOf("#")+1)!=pg_loaded)
			{
				pg_loaded = window.location.href.substr(window.location.href.indexOf("#")+1);
				getContent(pg_loaded, divid);
			}
		}
	}
	setTimeout("CheckChange()", 100);
}


function setAjaxContent(divId, html)
{ 
	document.getElementById(divId).innerHTML=html; 
  	var temp = html;
  	while(true)
	{
    	var sindex = temp.indexOf("<script");
		if(sindex < 0) break;
		sindex = temp.indexOf(">", sindex+1);
		var eindex = temp.indexOf("</script>",sindex);
		var js = temp.substring(sindex+1,eindex);           
    	eval(js);
		temp = temp.substring(eindex+9);
  	}     
}


function makeXmlHttp()
{
	var xmlHttp;
	try
	{
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}


function getContent(pagelink, divid)
{
	document.getElementById(divid).innerHTML="<center><br><br><br><br><br><br><br><br><font face='arial'>Loading please wait...</font><br><img src='Images/loadingAnimation.gif' /></center>";
	xmlHttp=makeXmlHttp();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			var content = xmlHttp.responseText;
			pageTracker._trackPageview(pagelink);
			setAjaxContent(divid, content);
			//document.getElementById(divid).innerHTML=content;
		 }
     }
	 xmlHttp.open("GET",pagelink,true);
     xmlHttp.send(null);
}
	

function submitContent(pagelink, post, divid, btn)
{
	xmlHttp=makeXmlHttp();
	btn.disabled=true;
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			var content = xmlHttp.responseText;
			//alert(content.substr(0,9));
			if(content.substr(0,9)=="<true  />")
				setAjaxContent(divid, content.substr(9));
			else
			{
				alert(content.substr(9));
			}
			btn.disabled=false;
		}
    }
    xmlHttp.open("POST",pagelink,true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.send(post);
}


/*function submitContent2(pagelink, post, divid, btn)
{
	xmlHttp=makeXmlHttp();
	btn.disabled=true;
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			var content = xmlHttp.responseText;
			alert(content.substr(1));
			if(content.substr(0,1)!="Y")
			    btn.disabled=false;
		}
	}
    xmlHttp.open("POST",pagelink,true);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.send(post);
}*/
	
	
function tb_submitContent(pagelink, post, divid, btn)
{
	xmlHttp=makeXmlHttp();
	btn.disabled=true;
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			var content = xmlHttp.responseText;
			alert(content.substr(9));
			if(content.substr(0,9)=="<true  />")
			{
				tb_remove();
			}
			btn.disabled=false;
		}
    }
    xmlHttp.open("POST",pagelink,true);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.send(post);
}

	
function ajaxLink(e)
{
	if (!e) var e = window.event;
	var targ;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	if(targ.nodeName=='A' && targ.className=='ajaxlink')
	{
		var a;
		if(targ.href.substr(0, 7) == "http://")
	    	a = targ.href.substr(targ.href.indexOf("/", 7));
		else
	   		a = targ.href;
		if(a==pg_loaded)
			getContent(targ, divid);
		else
	    	window.location.href = window.location.href.substr(0, window.location.href.indexOf("#"))+"#"+a;
		return false;
	}
	else
	{
		return true;
	}
}


function ajaxClick(src)
{
	getContent(src, divid);
}


function ajaxSubmit(src, post, btn)
{
	submitContent(src, post, divid, btn);
}


/*function ajaxSubmit2(src, post, btn)
{
	submitContent2(src, post, divid, btn);
}*/


function tb_ajaxSubmit(src, post, btn)
{
	tb_submitContent(src, post, divid, btn);
}


window.onload=function()
{
	document.onclick=ajaxLink;
    document.body.style.display = "block";
	var a = "bodymain.php";
	if(window.location.href.indexOf("#")>0)
	{
		var b = window.location.href.substr(window.location.href.indexOf("#")+1);
		if((b == "") || (b.substr(0,2) == "TB"))
		{
			window.location.href = window.location.href+"#/bodymain.php";
		}
	}
	else
	{
		window.location.href = window.location.href+"#/bodymain.php";
	}
	setTimeout("CheckChange()", 100);
	addScrollers();
}

function Login(frm)
{
	xmlHttp=makeXmlHttp();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			var content = xmlHttp.responseText;
			if(content.substr(0,9)=="<true  />")
			{
				document.getElementById("left_login").style.display = "block";
				document.getElementById("left_logout").style.display = "none";
				document.getElementById("registerbutton").style.display="none";
				document.location.href="#/profile.php";
			}
			else
			{
				document.getElementById("left_login").style.display = "none";
				document.getElementById("left_logout").style.display = "block";
				document.getElementById("registerbutton").style.display="block";
				alert(content.substr(9));
			}
			//alert(content.substr(9));
		}
    }
    xmlHttp.open("POST","scripts/login_do.php",true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.send("email=" + frm.email.value + "&pass=" + frm.pass.value);
}
