// JavaScript Document
function setDate(str) {
		 var obj = str;
		 var dd ="";
		 str = str.innerHTML;
		 var today=new Date();
		 if (str == "&nbsp;"|| str == " ") {
		  return;
		 }
		 var td = today.getDate();
		 if(str == "Today")
		 {
			var dd = today.getDate();
			str = dd;
			document.getElementById('month').value=today.getMonth()-1;
			document.getElementById('year').value=today.getFullYear();
			nextMonth();
		 }
		 mnth1 = document.getElementById('month').value;
		 mnth = mnth1;
		 mnth++;
		 var year = document.getElementById('year').value;
		 dateStr = mnth+"/"+str+"/"+year;	
		 dateStr = trim(dateStr);		
		 document.getElementById('dateField').value = dateStr;
		 for(var i=1; i<42; i++) {
			
			if(document.getElementById('s'+i).innerHTML==td && mnth == today.getMonth()+1 && year == today.getFullYear())
			{
				document.getElementById('s'+i).className = 'calcellcurrent';
			}
			else if(dd!="" && document.getElementById('s'+i).innerHTML==dd)
			{
				document.getElementById('s'+i).className = 'calcellselect';
			}
			else if(obj.id == 's'+i && dd=="") { 
				document.getElementById('s'+i).className = 'calcellselect';	
			}else{
				document.getElementById('s'+i).className = 'calcell';	
			}
		  }
		  var datestr1 = year+"-"+mnth+"-"+str;
		 
			 var url = "various_dates.php?vtype=gdate&dt="+datestr1+"&aid=content";
			  //alert(url);
			  ajaxUpdater("content",url,{asynchronous:true});
			 // AJAX code will be here... 
				
			 //
}
	
/**
 * The function removes spaces from the selected date.
 */
	
function trim(str) {
	 res="";
	 for(var i=0; i< str.length; i++) {
	   if (str.charAt(i) != " ") {
		 res +=str.charAt(i);
	  }
	 }
	 return res;
}
	 
/**
 * The method to get the Month name given the Month number of the year.
 */
	
function getMonthName(mnth) {
	
	 if (mnth == 0) {
	  name = "January";
	 }else if(mnth==1) {
	  name = "February";
	 }else if(mnth==2) {
	  name = "March";
	 }else if(mnth==3) {
	  name = "April";
	 }else if(mnth==4) {
	  name = "May";
	 } else if(mnth==5) {
	  name = "June";
	 } else if(mnth==6) {
	  name = "July";
	 } else if(mnth==7) {
	  name = "August";
	 } else if(mnth==8) {
	  name = "September";
	 } else if(mnth==9) {
	  name = "October";
	 } else if(mnth==10) {
	  name = "November";
	 } else if(mnth==11) {
	  name = "December";
	 }
	 return name;
}


/**
 * Get the number of days in the month based on the year.
 */
	
function getNoOfDaysInMnth(mnth,yr) {
	 
	 rem = yr % 4;

	 if(rem ==0) {
	   leap = 1;
	 } else {
	  leap = 0;
	 }
	
	 noDays=0;
	
	 if ( (mnth == 1) || (mnth == 3) || (mnth == 5) ||
		  (mnth == 7) || (mnth == 8) || (mnth == 10) ||
		  (mnth == 12)) {
	  noDays=31;
	 } else if (mnth == 2) {
			   noDays=28+leap;
			} else {
			   noDays=30;
	 }
	 return noDays;
}
  
/**
 * The function to reset the date values in the buttons of the 
 * slots.
 */
	
function fillDates(dayOfWeek1,noOfDaysInmnth) {
	 for(var i=1; i<42; i++) {
	   str = "s"+i;	  
 	   document.getElementById(str).innerHTML="&nbsp;";
	 }	
	 startSlotIndx = dayOfWeek1;
	 slotIndx = startSlotIndx;
	
	 for(var i=1; i<(noOfDaysInmnth+1); i++) {
	  slotName = "s"+slotIndx;
	
	  val="";
	  if (i<10) {
		val = " "+i+" ";
	  } else {
		val = i;
	  }	
	  document.getElementById(slotName).innerHTML = val;
	  slotIndx++;
	 }
}

/**
 * The function that is called at the time of loading the page.
 * This function displays Today's date and also displays the 
 * the calendar of the current month.
 */
	
function thisMonth() {
	  dt = new Date();
	  mnth  = dt.getMonth(); 
	  dayOfMnth = dt.getDate(); 
	  dayOfWeek = dt.getDay(); 
	  yr = dt.getFullYear(); 
	  mnthName = getMonthName(mnth)+ " ";
	  document.getElementById('month').value = mnth;
	  document.getElementById('year').value = yr;
	  document.getElementById('currMonth').value = mnth;
	  document.getElementById('currYear').value = yr;
	  document.getElementById('monthYear').innerHTML = "&nbsp;"+mnthName+yr+"&nbsp;";
	  document.getElementById('dateField').value = (mnth+1)+"/"+dayOfMnth+"/"+yr;
	  startStr = (mnth+1)+"/1/"+yr;
	  dt1 = new Date(startStr);
	  dayOfWeek1 = dt1.getDay(); /*0-6*/
	  noOfDaysInMnth = getNoOfDaysInMnth(mnth+1,yr);
	  fillDates(dayOfWeek1+1,noOfDaysInMnth);
	  for(var i=1; i<42; i++) {
		if(parseInt(document.getElementById('s'+i).innerHTML) == dayOfMnth && parseInt(document.getElementById('s'+i).innerHTML)!='') {
			document.getElementById('s'+i).className = 'calcellcurrent';	
			break;
		}
	  }	
}
	
/**
 * The function that will be used to display the calendar of the next month.
 */

function nextMonth() {
	
	 var currMnth = document.getElementById('month').value;
	 currYr = document.getElementById('year').value;
	  
	 if (currMnth == "11") {
		nextMnth = 0;
		nextYr = currYr;
		nextYr++;
	 } else {
	   nextMnth=currMnth;
	   nextMnth++;
	   nextYr = currYr;
	 }
	
	 mnthName = getMonthName(nextMnth);
	 document.getElementById('month').value=nextMnth;
	 document.getElementById('year').value=nextYr;
	 document.getElementById('monthYear').innerHTML= "&nbsp;"+mnthName+" "+nextYr+"&nbsp;";


	 
	 str = (nextMnth+1)+"/1/"+nextYr;
	 dt = new Date(str);
	 dayOfWeek = dt.getDay();
	 ndayOfMnth = dt.getDate(); 
	 nmnth  = dt.getMonth();  
	 ndayOfWeek = dt.getDay(); 
	 nyr = dt.getFullYear(); 
	 noOfDays = getNoOfDaysInMnth(nextMnth+1,nextYr);
	 fillDates(dayOfWeek+1,noOfDays);

	//code
	  cdt = new Date();
	  cmnth  = cdt.getMonth(); 
	  cdayOfMnth = cdt.getDate(); 
	  cdayOfWeek = cdt.getDay(); 
	  cyr = cdt.getFullYear(); 
	  cmnthName = getMonthName(cmnth)+ " ";
	  
	//end code
	 
	 for(var i=1; i<42; i++) {
		document.getElementById('s'+i).className = 'calcell';	
	 }
 	 
	  if(cdayOfMnth && nmnth == cmnth && nyr == cyr) {
		  for(var i=1; i<42; i++) {
			if(document.getElementById('s'+i).innerHTML == cdayOfMnth){
				document.getElementById('s'+i).className = 'calcellcurrent';			
			}

		  }		 
	 } 

}

/**
 * The method to display the calendar of the previous month.
 */
function prevMonth() {
	 var currMnth = document.getElementById('month').value;
	 currYr = document.getElementById('year').value;
	 if (currMnth == "0") {
		prevMnth = 11;
		prevYr = currYr;
		prevYr--;
	 } else {
	   prevMnth=currMnth;
	   prevMnth--;
	   prevYr = currYr;
	 }
	
	 str = (prevMnth+1)+"/1/"+prevYr;
	 dt = new Date(str);
	 dayOfWeek = dt.getDay();
	 ndayOfMnth = dt.getDate(); 
	 nmnth  = dt.getMonth();  
	 ndayOfWeek = dt.getDay(); 
	 nyr = dt.getFullYear(); 
	 
	 mnthName = getMonthName(prevMnth);
	 document.getElementById('month').value=prevMnth;
	 document.getElementById('year').value=prevYr;
	 document.getElementById('monthYear').innerHTML= "&nbsp;"+mnthName+" "+prevYr+"&nbsp;";	
	 noOfDays = getNoOfDaysInMnth(prevMnth+1,prevYr);
	 fillDates(dayOfWeek+1,noOfDays);
	 
 	//code
	  cdt = new Date();
	  cmnth  = cdt.getMonth(); 
	  cdayOfMnth = cdt.getDate(); 
	  cdayOfWeek = cdt.getDay(); 
	  cyr = cdt.getFullYear(); 
	  cmnthName = getMonthName(cmnth)+ " ";
	  
	//end code
	  for(var i=1; i<42; i++) {
		document.getElementById('s'+i).className = 'calcell';	
	  }

	 if(cdayOfMnth && nmnth == cmnth && nyr == cyr) {
		  for(var i=1; i<42; i++) {
			if(document.getElementById('s'+i).innerHTML == cdayOfMnth){
				document.getElementById('s'+i).className = 'calcellcurrent';			
			}
		  }
		 
	 }
}