/*
 Events DatePicker functions collection
 ======================================
*/

//Make the first letter of a word upper
function initialCap(field) {
  	field = field.substr(0, 1).toUpperCase() + field.substr(1);
   	return field;
}

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    
    for(var i = 0; i < hashes.length; i++)
    {
    	hash = hashes[i].split('=');
    	vars.push(hash[0]);
    	vars[hash[0]] = hash[1];
   	}
   	
   	return vars;
}

//Querry the events of a given month through ajax into global_events
function refreshMonth(year, month)
{
	new Ajax.Request("?page=events&func=ajax&subfunc=month_events&year="+year+"&month="+month,
	{
		method: "get",
		asynchronous: false,
		onSuccess: function(transport)
		{
			//If we get the right events number on the day 
			if(transport.responseText != "false")
			{	
				//the events array index starts FROM ZERO (0)
				global_events = transport.responseText.split('\n');
			}
			else
			{
				//happen nothing
			}
				
	 	},
		onFailure: function()
		{
	 		//happen nothing
		}
	});
}

//If the two digit day begins with zero we cut it.
function dayIndexCreate(day)
{
	var temp;
	if (day[0] == "0")
	{
		temp = day["1"];
	}
	else
	{
		temp = day;
	}
	return temp;
}

//get the number of days of a given month
function daysInMonth(iMonth, iYear)
{
	return 32 - new Date(iYear, iMonth, 32).getDate();
}

//Querry the events of a given month through ajax into global_month_events
function refreshHeaderMonth(year, month)
{
	new Ajax.Request("?page=events&func=ajax&subfunc=month_events&year="+year+"&month="+month,
	{
		method: "get",
		asynchronous: false,
		onSuccess: function(transport)
		{
			//If we get the right events number on the day 
			if(transport.responseText != "false")
			{	
				//the events array index starts FROM ZERO (0)
				global_month_events = transport.responseText.split('\n');
			}
			else
			{
				for(var i = 1; i < 32; i++)
					global_month_events[i] = -1;
			}
				
	 	},
		onFailure: function()
		{
	 		for(var i = 1; i < 32; i++)
					global_month_events[i] = -1;
		}
	});
}

//Querry the events of a given month through ajax into global_week_events
function refreshHeaderWeek(minweek, maxweek)
{
	new Ajax.Request("?page=events&func=ajax&subfunc=week_events&minweek="+minweek+"&maxweek="+maxweek,
	{
		method: "get",
		asynchronous: false,
		onSuccess: function(transport)
		{
			//If we get the right events number on the day 
			if(transport.responseText != "false")
			{	
				//the events array index starts FROM ZERO (0)
				global_week_events = transport.responseText.split(',');
				for (var i = 0; i < global_week_events.length; i++)
				{
					global_week_events[i] = global_week_events[i].split('\n');
				}
			}
			else
			{
				//happen nothing
			}
				
	 	},
		onFailure: function()
		{
	 		//happen nothing
		}
	});
}

function refreshHeaderYear(year)
{
	new Ajax.Request("?page=events&func=ajax&subfunc=year_events&year="+year,
	{
		method: "get",
		asynchronous: false,
		onSuccess: function(transport)
		{
			//If we get the right events number on the day 
			if(transport.responseText != "false")
			{	
				//the events array index starts FROM ZERO (0)
				global_year_events = transport.responseText.split(',');
				for (var i = 0; i < global_year_events.length; i++)
				{
					global_year_events[i] = global_year_events[i].split('\n');
				}
			}
			else
			{
				//happen nothing
			}
				
	 	},
		onFailure: function()
		{
	 		//happen nothing
		}
	});
}
