var xmlHttp= GetXmlHttpObject();
var stat = false;

function checkState()
{
	
/*	
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="status.php";
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
return stat;
*/
return true;
} 


function showEvent(thedate)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="cal.php?date="+thedate;
xmlHttp.onreadystatechange=calChange;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
return stat;
} 

function nextCal(month,year)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="cal.php?year="+year+"&month="+month+"&direction=next";
xmlHttp.onreadystatechange=calRefresh;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
return stat;
} 

function prevCal(month,year)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="cal.php?year="+year+"&month="+month+"&direction=prev";
xmlHttp.onreadystatechange=calRefresh;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
return stat;
} 


function dispatch(rep,t)
{
	if(rep=='FALSE')
	{

		  var txt = 'Session Expired!<br/>Please enter your login info:<br />Username: <input type="text" id="usr" name="username" value="" />Password: &nbsp;<input type="password" id="pwd" name="pwd" value="" />';
		  
		  if(t=='I')
		  txt+="<br>Invalid Login! Try Again.";


		$.prompt(txt,{callback: mycallbackform,buttons: { Login: 'Hello', Exit: 'Good Bye' }});
		stat=false;
	}
	else
	stat=true;
    
}




function mycallbackform(v,m)
{
	if(v=='Good Bye')
	return false;
	if(v=='Hello')
	{
	  //check login info
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		  {
		  alert ("Your browser does not support AJAX!");
		  return;
		  } 
		var url="status.php?update=1";
		var params="username="+m.children('#usr').val()+"&password="+m.children('#pwd').val();
		xmlHttp.onreadystatechange=stateChanged2;
		xmlHttp.open("POST",url,true);
		
		//Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);
	  	return true;
	 
	}
}


function stateChanged() 
{ 
	if (xmlHttp.readyState==4 && xmlHttp.status == 200)
	{ 
		dispatch(xmlHttp.responseText,'F');
	}
}

function calChange()
{
	if (xmlHttp.readyState==4 && xmlHttp.status == 200)
	{ 
		document.getElementById('eventinfo').innerHTML=xmlHttp.responseText;
	}
}

function calRefresh()
{
	if (xmlHttp.readyState==4 && xmlHttp.status == 200)
	{ 
		document.getElementById('thecal').innerHTML=xmlHttp.responseText;
	}
}



function stateChanged2()
{
	if (xmlHttp.readyState==4 && xmlHttp.status == 200)
	{ 
		dispatch(xmlHttp.responseText,'I');
	}
}






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;
}
