date		= new Date();
theMonth	= date.getMonth(); //0-11 or jan-dec
theYear		= date.getFullYear();

//Comment if used with other Zen in motion JS libraries
//or with prototype, mootools, etc...

/*
function $(id) {
  return document.getElementById(id);
}
*/

function getURLVars()
{
test = location.href;

test= test.split("?");
return test[1];
}


//=======Comment out if not using this feature=====
/*
function updateParentForm(whichDrop1,day,month,year)
{
	tempVar		= "monthval" + whichDrop1;
	tempVar2	= "yearval" + whichDrop1;
	tempVar3	= "dayval" + whichDrop1;
	
	day			= parseInt(day);
	
	if(day < 10)
	day			= "0" + day;
	
	switch(month)
	{
		case 0:
		month = "Jan";
		break;
		case 1:
		month = "Feb";
		break;
		case 2:
		month = "March";
		break;
		case 3:
		month = "Apr";
		break;
		case 4:
		month = "May";
		break;
		case 5:
		month = "June";
		break;		
		case 6:
		month = "July";
		break;		
		case 7:
		month = "Aug";
		break;		
		case 8:
		month = "Sept";
		break;		
		case 9:
		month = "Oct";
		break;		
		case 10:
		month = "Nov";
		break;		
		case 11:
		month = "Dec";
		break;						
	}

	
	window.opener.$(tempVar).value		= month;
	window.opener.$(tempVar2).value		= year;
	window.opener.$(tempVar3).value		= day;
	
}
*/
 function showEventDetails(theday1,themonth1,theyear1) {
	
	themonth1++;
	
	dateval = theyear1 + "-" + themonth1 + "-" + theday1;
	http_request = false;
	
	parameters = "dateval=" + encodeURI(dateval);
	
	http_request = createXMLHttpRequest();
      
	  if (http_request.overrideMimeType)
            http_request.overrideMimeType('text/html');
	  
	  //build body for Form Post
      http_request.onreadystatechange = function(){
	 if (http_request.readyState == 4) {
         if (http_request.status == 200) {
			// alert(http_request.responseText);
if(http_request.responseText == "0")
{
alert("Sorry no events on that date.");
}
else
{
	
	$('calEvents').style.display = 'block';
	
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(http_request.responseText);
  }  
catch(e)
  {
  try // Firefox, Mozilla, Opera, etc.
    {
    parser=new DOMParser();
    xmlDoc=parser.parseFromString(http_request.responseText,"text/xml");
    }
  catch(e)
    {
    alert(e.message);
    return;
    }
  }
  
 var x=xmlDoc.getElementsByTagName("eventdetails");
 
$('eventdetails').innerHTML	= ""; 
 
for (i=0;i<x.length;i++)
{ 
$('eventdetails').innerHTML += "<div class='calLeftColsm'>" + x[i].getElementsByTagName("eventdate")[0].childNodes[0].nodeValue + "</div>";
$('eventdetails').innerHTML += "<div class='calRightColsm'><b>Submitted by: " + x[i].getElementsByTagName("submitname")[0].childNodes[0].nodeValue + "</b><br/>" + x[i].getElementsByTagName("details")[0].childNodes[0].nodeValue + "</div>";
$('eventdetails').innerHTML += "<div align='center' style='border-top: 1px dashed #999999; margin:3px 0px 0px 0px;'></div>";
}	  

} //end else
			 
			 
			 
		 }
	 	}
	  }
      http_request.open('POST', "JS_Calendar/cal_details_process.php", true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

function closeCalWindow()
{
	$('calEvents').style.display = 'none';
}

//====-===-===Calendar Functions ====-===-===
function populateCalendar(changeMonth)
{
	
whichDrop =  getURLVars();	
pos = 30;	

//====Clear Calendar First====
for(c=1;c<=42;c++)
{
$('_Cal' + c).innerHTML = "&nbsp;";
$('_Cal' + c).className = "";
}

//==-==-==Determine which month ==-==-==
date = new Date();

if(changeMonth == "next")
{
	
	if(theMonth == 11)
	{
	theMonth = 0;
	theYear++;
	}
	else
	{
	theMonth++; //0-11 or jan-dec
	}

}
else if(changeMonth == "prev")
{
	if(theMonth == 0)
	{
	theMonth = 11;
	theYear--;
	}
	else
	{
	theMonth--; //0-11 or jan-dec
	}
}
else
{
theMonth	= date.getMonth(); //0-11 or jan-dec
theYear		= date.getFullYear();	
}


//else default global declaration kicks in
if(theMonth == 0)
{
lastday		= 31;
monthOut	= "January";
}
else if(theMonth == 1)
{
lastday		= 28;
monthOut	= "February";
}
else if(theMonth == 2)
{
lastday		= 31;
monthOut	= "March";
}
else if(theMonth == 3)
{
monthOut	= "April";	
lastday		= 30;
}
else if(theMonth == 4)
{
monthOut	= "May";	
lastday		= 31;
}
else if(theMonth == 5)
{
monthOut	= "June";		
lastday		= 30;
}
else if(theMonth == 6)
{
monthOut	= "July";			
lastday		= 31;
}
else if(theMonth == 7)
{
monthOut	= "August";			
lastday		= 31;
}
else if(theMonth == 8)
{
monthOut	= "September";			
lastday = 30;
}
else if(theMonth == 9)
{
monthOut	= "October";			
lastday		= 31;
}
else if(theMonth == 10)
{
monthOut	= "November";			
lastday		= 30;
}
else if(theMonth == 11)
{
monthOut	= "December";			
lastday		 = 31;
}

$('MonthHeader').innerHTML = monthOut + " " + theYear;

date.setFullYear(theYear,theMonth,1); //first day of selected month/year
dayofweek	= date.getDay() + 1;		//represents the day the first day of the month falls on (+1 to match my calendar IDs which are 1 based)
dayofweekout	= dayofweek;

date	= new Date();		//get the current date
today	= date.getDate();	//return the day of the current date (ex: jan 6, 1983 = 6)

for(c=1;c<=42;c++)
{		
if(c > lastday)
break;
	
	$('_Cal'+dayofweek).innerHTML = "<a href=\"javascript:showEventDetails("+c + "," + theMonth +"," + theYear +");\">"+c+"</a>";
	
	//if(c == today && changeMonth == null)
	//$('_Cal' + dayofweek).className = "todayDate";
	
pos--;
dayofweek++;
} //end for

month1		= theMonth +1;
tempdate	= month1 + "-1";

getMonthEvents(tempdate,theYear,dayofweekout);

//alert($('Calendar').innerHTML);
}
//==-===-==End populateCalendar==-===-==

//AJAX POPULATE FUNCTIOn


function getMonthEvents(dateval,yearval) {	
	
	http_request = false;

parameters = "dateval=" + encodeURI(dateval) + "&theYear=" + encodeURI(yearval);
	
	http_request = createXMLHttpRequest();
      
	  if (http_request.overrideMimeType)
            http_request.overrideMimeType('text/html');
	  
	  //build body for Form Post
      http_request.onreadystatechange = function(){
	 if (http_request.readyState == 4) {
         if (http_request.status == 200) {
		//alert(http_request.responseText);	 	 

if(http_request.responseText != "0")
{
		
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(http_request.responseText);
  }  
catch(e)
  {
  try // Firefox, Mozilla, Opera, etc.
    {
    parser=new DOMParser();
    xmlDoc=parser.parseFromString(http_request.responseText,"text/xml");
    }
  catch(e)
    {
    alert(e.message);
    return;
    }
  }
  
 var x=xmlDoc.getElementsByTagName("eventdetails");
 
 dayofweekout--;

for (i=0;i<x.length;i++)
{ 
dayofMonth = x[i].getElementsByTagName("eventday")[0].childNodes[0].nodeValue;

adjustedDate	= parseInt(dayofweekout) + parseInt(dayofMonth);

//alert(adjustedDate);

$('_Cal'+adjustedDate).className	= "todayDate";

}	  

} //endif

			}
	 	}
	  }
      http_request.open('POST', "JS_Calendar/cal_month_process.php", true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }
