/*
File:		 js_liveCal.js.php
Author:		 Christopher Bennell christopher@emergencyinhaler.com
Date:		 March 2007
Description: This function handles the ajax request to create new calendars on the fly, using the next and prev month buttons.

*/

function newMonth(year, month, type, placeholder){	
	/*This function uses innerHTML to delete the current calendar, then generate a new one using AJAX methods.
	  The prototype library is used to handle AJAX requests.
	  The type argument defines which calendar, (start or end) is being replaced. 
	*/	
	year  			= year  +'';							//year to generate
	monthMinus1 	= month - 1;							//used for array references that start at zero
	month 			= padDigits(month +'', 2);				//pads with zeros
	datestring 		= year+padDigits(month +'', 2)+"01";	//string to pass to liveCal.php
	if(document.getElementById)
	{
		var calHolder 		= document.getElementById(placeholder);
	}
	else if (document.all)
	{
		calHolder = document.all[placeholder];
	}
	if (type != "mini"){
		selectorID 		= "event"+type+"Month";				//id of the month <select> tag. Used to update the current month
		monthSelector 	= document.getElementById(selectorID);
	}	
	
	new Ajax.Request(	'php/liveCal.php',{					//Make request for new calendar
							method:'get',				//Can be either get or post, but must match liveCal.php
							parameters: {date:datestring, calType:type},
							onSuccess: function(transport){
								var response = transport.responseText || "no response text";
								calHolder.innerHTML = "";
								calHolder.innerHTML = response;
								
								if(type != "mini"){							
									monthSelector.selectedIndex=monthMinus1;						//Updates current month
									eval("document.addEventForm.event"+type+"Day.value = ''"); 		//erase 'day' field
									eval("document.addEventForm.event"+type+"Year.value = year");	//update year
								}
							},
							onFailure: function(){
								
							}
						}
					);	
}

function padDigits(n, totalDigits){
	//Pads numbers with leading zeros. Used to create datestrings
	//alert(typeof(n));
	//n = n.toString(); 
	var pd = ''; 
	if (totalDigits > n.length){ 
		for (i=0; i < (totalDigits-n.length); i++){ 
			pd += '0'; 
		} 
	} 
	return pd + n;//.toString();
}
