$(document).ready(function () {
	
	getPage();
	
	//Check if url hash value exists (for bookmark)
	$.history.init(pageload);	
	    
	//highlight the selected link
	$('a[href=' + document.location.hash + ']').addClass('selected');
	
	//Seearch for link with REL set to ajax
	$('a[rel=ajax]').click(function () {
		
		//grab the full url
		var hash = this.href;
		
		//remove the # value
		hash = hash.replace(/^.*#/, '');
		
		//for back button
	 	$.history.load(hash);	
	 	
	 	//clear the selected class and add the class class to the selected link
	 	$('a[rel=ajax]').removeClass('selected');
	 	$(this).addClass('selected');
	 	
	 	//hide the content and show the progress bar
	 	$('#content').hide();
	 	$('#loading').show();
	 	
	 	//run the ajax
		getPage();
	
		//cancel the anchor tag behaviour
		return false;
	});	
});
	

function pageload(hash) {
	//if hash value exists, run the ajax
	if (hash) getPage();    
}
		
function getPage() {
	
	//generate the parameter for the php script
	if (document.location.hash=='') var data = 'page=' + encodeURIComponent('#page1');
	else var data = 'page=' + encodeURIComponent(document.location.hash);
	
	
	$.ajax({
		url: "loader.php",	
		type: "GET",		
		data: data,		
		cache: false,
		success: function (html) {	
		
			//hide the progress bar
			$('#loading').hide();	
			
			//add the content retrieved from ajax and put it in the #content div
			$('#content').html(html);
			
			//display the body with fadeIn transition
			$('#content').fadeIn('slow');		
		}		
	});
}

var xmlhttp,xdivid,url,met,parameters;
function loadurl(divid,url,met)
{
	//alert(url);
	var end = new Date();
now= end.getTime() ;
if (url.indexOf('?')==-1)
{
url=(url+"?arndxy="+now);
}
else
{
	url=(url+"&arndxy="+now);
}

	xdivid=divid;
	xmlhttp=null;
	
	if (window.XMLHttpRequest)  
		xmlhttp=new XMLHttpRequest();
	else if (window.ActiveXObject) 
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	
	if (xmlhttp!=null)
	{
		xmlhttp.onreadystatechange=state_Change; 
		xmlhttp.open(met,url,true);  
		if(met=='POST')
		{
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.setRequestHeader("Content-length", parameters.length);
			xmlhttp.setRequestHeader("Connection", "close");
			xmlhttp.send(parameters);
		}
		else
		{
			xmlhttp.send(null);
		}
	}
	else
		alert("Your browser does not support AJAX.");
}

function state_Change()
{
	  //document.getElementById(xdivid).innerHTML="<center><img src='images/load.gif'/></center>";  
	 if(met=='POST')
	 	document.getElementById('DivStatus').innerHTML="<img src='images/load.gif' width='31' align='absmiddle' />Processing...</Div>";  
	 else
	document.getElementById('DivStatus').innerHTML="<img src='images/load.gif' width='31' align='absmiddle' />Loading...</Div>";  
	
if (xmlhttp.readyState==4)
  {

	  
  if (xmlhttp.status==200)
    {// "OK"
if(xdivid!=''){
  var curdiv=document.getElementById(xdivid);
  curdiv.innerHTML=xmlhttp.responseText;
}
document.getElementById('DivStatus').innerHTML="";  

    }
	
  else alert("Problem retrieving data:" + xmlhttp.statusText);

  }
}



function postToUrl(divid,url,usrform)
{
	//alert(divid+"  "+url+"  "+usrform);
	parameters='';
	
	for (i=0;i<document.forms[usrform].elements.length;i++)
	{
		if ( document.forms[usrform].elements[i].type=="radio"|| document.forms[usrform].elements[i].type=="checkbox") 
		{
			if(document.forms[usrform].elements[i].checked==false) 
				parameters+= document.forms[usrform].elements[i].name + "=0&";
			else
				parameters+= document.forms[usrform].elements[i].name + "=" + encodeURI( document.forms[usrform].elements[i].value )+"&";
		}
		else
			parameters+= document.forms[usrform].elements[i].name + "=" + encodeURI( document.forms[usrform].elements[i].value )+"&";
	}
	
	parameters=parameters.substring(0,parameters.length-1);
	//alert(parameters);
	loadurl(divid,url,'POST');
	return false;
}


