var xmlhttp;
var ndate, ndate2;

var m_names = new Array("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC");
var d_names = new Array("SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT");
var mname = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

function showPreviousDate(str,projid,teamid)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	  }
	var url="/tools/ajax/getevent.php";
	url=url+"?q="+str;
	url=url+"&sid="+Math.random();
	url=url+"&projid="+projid;
	url=url+"&teamid="+teamid;
	xmlhttp.onreadystatechange=stateChangedPreviousDate;
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange=stateChangedPreviousDate;
	xmlhttp.send(null);
}


function showNextDate(str,projid,teamid)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	  }
	var url="/tools/ajax/getevent.php";
	url=url+"?q="+str;
	url=url+"&sid="+Math.random();
	url=url+"&projid="+projid;
	url=url+"&teamid="+teamid;
	xmlhttp.onreadystatechange=stateChangedNextDate;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}


function showPreviousMonth(str,projid,teamid)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	  }
	var url="/tools/ajax/getevent.php";
	url=url+"?q="+str;
	url=url+"&sid="+Math.random();
	url=url+"&projid="+projid;
	url=url+"&teamid="+teamid;
	xmlhttp.onreadystatechange=stateChangedPreviousMonth;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function showNextMonth(str,projid,teamid)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	  }
	var url="/tools/ajax/getevent.php";
	url=url+"?q="+str;
	url=url+"&sid="+Math.random();
	url=url+"&projid="+projid;
	url=url+"&teamid="+teamid;
	xmlhttp.onreadystatechange=stateChangedNextMonth;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

 
function stateChangedPreviousDate()
{
	if (xmlhttp.readyState==4)
	{
		document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
		document.getElementById("today_date").value = document.getElementById("previous_date").value;
		ndate = document.getElementById("today_date").value;
		ndate2 = ndate.split("-");
		var a = mname[(parseInt(ndate2[1],10)-1)]+" "+ndate2[2]+", "+ndate2[0];
		ndate2 = new Date(a);
		document.getElementById("display_date").value = d_names[(ndate2.getDay())]+" "+(ndate2.getDate())+" "+m_names[(ndate2.getMonth())]+" "+ndate2.getFullYear();
		ndate2.setDate(ndate2.getDate() - 1);
		document.getElementById("previous_date").value = ndate2.getFullYear()+"-"+(ndate2.getMonth()+1)+"-"+ndate2.getDate();
		document.getElementById("txtHint2").innerHTML=document.getElementById("display_date").value;
		ndate2.setDate(ndate2.getDate() + 2);
		document.getElementById("next_date").value = ndate2.getFullYear()+"-"+(ndate2.getMonth()+1)+"-"+ndate2.getDate();
	}
}

function stateChangedPreviousMonth()
{
	if (xmlhttp.readyState==4)
	{
		document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
		document.getElementById("today_date").value = document.getElementById("previous_month").value;
		ndate = document.getElementById("today_date").value;
		ndate2 = ndate.split("-");
		var a = mname[(parseInt(ndate2[1],10)-1)]+" "+ndate2[2]+", "+ndate2[0];
		ndate2 = new Date(a);
		document.getElementById("display_date").value = d_names[(ndate2.getDay())]+" "+(ndate2.getDate())+" "+m_names[(ndate2.getMonth())]+" "+ndate2.getFullYear();
		document.getElementById("next_date").value = ndate2.getFullYear()+"-"+(ndate2.getMonth()+1)+"-"+(ndate2.getDate() + 1);
		document.getElementById("previous_date").value = ndate2.getFullYear()+"-"+(ndate2.getMonth()+1)+"-"+(ndate2.getDate() - 1);
		//ndate2.setYear(ndate2.getFullYear() - 1);
		if(ndate2.getMonth()<1)
		{
			document.getElementById("previous_month").value = (ndate2.getFullYear()-1)+"-12-"+ndate2.getDate();
		}
		else
		{
			document.getElementById("previous_month").value = ndate2.getFullYear()+"-"+(ndate2.getMonth())+"-"+ndate2.getDate();
		}
		document.getElementById("txtHint2").innerHTML=document.getElementById("display_date").value;
		//ndate2.setYear(ndate2.getFullYear() + 2);
		document.getElementById("next_month").value = ndate2.getFullYear()+"-"+(ndate2.getMonth()+2)+"-"+ndate2.getDate();
	}
}

function stateChangedNextDate()
{
	if (xmlhttp.readyState==4)
	{
		if(!xmlhttp && typeof XMLHttpRequest!="undefined") {
		    xmlhttp=new XMLHttpRequest();}
		document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
		document.getElementById("today_date").value = document.getElementById("next_date").value;
		ndate = document.getElementById("today_date").value;
		ndate2 = ndate.split("-");
		var a = mname[(parseInt(ndate2[1],10)-1)]+" "+ndate2[2]+", "+ndate2[0];
		ndate2 = new Date(a);
		document.getElementById("display_date").value = d_names[(ndate2.getDay())]+" "+(ndate2.getDate())+" "+m_names[(ndate2.getMonth())]+" "+ndate2.getFullYear();
		ndate2.setDate(ndate2.getDate() + 1);
		document.getElementById("next_date").value = ndate2.getFullYear()+"-"+(ndate2.getMonth()+1)+"-"+ndate2.getDate();
		document.getElementById("txtHint2").innerHTML=document.getElementById("display_date").value;
		ndate2.setDate(ndate2.getDate() - 2);
		document.getElementById("previous_date").value = ndate2.getFullYear()+"-"+(ndate2.getMonth()+1)+"-"+ndate2.getDate();
	}
}

function stateChangedNextMonth()
{
	if (xmlhttp.readyState==4)
	{
		document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
		document.getElementById("today_date").value = document.getElementById("next_month").value;
		ndate = document.getElementById("today_date").value;
		ndate2 = ndate.split("-");
		var a = mname[(parseInt(ndate2[1],10)-1)]+" "+ndate2[2]+", "+ndate2[0];
		ndate2 = new Date(a);
		document.getElementById("display_date").value = d_names[(ndate2.getDay())]+" "+(ndate2.getDate())+" "+m_names[(ndate2.getMonth())]+" "+ndate2.getFullYear();
		document.getElementById("next_date").value = ndate2.getFullYear()+"-"+(ndate2.getMonth()+1)+"-"+(ndate2.getDate() + 1);
		document.getElementById("previous_date").value = ndate2.getFullYear()+"-"+(ndate2.getMonth()+1)+"-"+(ndate2.getDate() - 1);
		//ndate2.setMonth(ndate2.getMonth() - 1);
		if((ndate2.getMonth())<1) {
		document.getElementById("previous_month").value = (ndate2.getFullYear()-1)+"-12-"+ndate2.getDate();
		} else {
			document.getElementById("previous_month").value = ndate2.getFullYear()+"-"+(ndate2.getMonth())+"-"+ndate2.getDate();
		}
		//ndate2.setYear(ndate2.getFullYear() + 2);
		if((ndate2.getMonth()+2)>12)
		{
			document.getElementById("next_month").value = (ndate2.getFullYear()+1)+"-01-"+ndate2.getDate();
		}
		else
		{
			document.getElementById("next_month").value = ndate2.getFullYear()+"-"+(ndate2.getMonth()+2)+"-"+ndate2.getDate();
		}
		document.getElementById("txtHint2").innerHTML=document.getElementById("display_date").value;
	}
}

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");
    }
  }
   if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
    xmlhttp=new XMLHttpRequest();}
return xmlhttp;
}
