/*
Copyright (c) 2005-2008, Gurú Sistemas and/or Gustavo Adolfo Arcila Trujillo
All rights reserved.
www.gurusistemas.com

phpMyDataGrid Professional IS NOT FREE and may not be re-sold as a single library.

If you want to use phpMyDataGrid Professional on any of your projects, you Must purchase a license.

You can buy the full source code or encoded version at http://www.gurusistemas.com/phpmydatagrid/
also can try the donationware version, which can be downloaded from http://www.gurusistemas.com/indexdatagrid.php

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  "AS IS"  AND ANY EXPRESS  OR  IMPLIED WARRANTIES, INCLUDING, 
BUT NOT LIMITED TO,  THE IMPLIED WARRANTIES  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  INDIRECT,  INCIDENTAL, SPECIAL, EXEMPLARY,  OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF  USE, DATA, OR PROFITS;  OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 

For more info, samples, tips, screenshots, help, contact, support, please visit phpMyDataGrid site  
http://www.gurusistemas.com/phpmydatagrid/
*/
cal_old_mousedown = function(){};

	function daysInMonth(month, year) {
		switch (month){
			case 2: return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 29 : 28; break;
			case 4: case 6: case 9: case 11: return 30; break;
			default: return 31;	break;
		};
	};
	
	function setMyDate(retDay, id, format){
		retMonth= parseInt(DG_gvv('calMonth')); if (retMonth<10) retMonth='0'+retMonth;
		retYear= parseInt(DG_gvv('calYear')); if (parseInt(retDay)<10) retDay='0'+parseInt(retDay);
		format = format.replace('dd',retDay); format = format.replace('mm',retMonth); format = format.replace('yyyy',retYear); DG_svv(id,format); closeCalendar();
	};

	function closeCalendar(){ document.body.onmousedown = cal_old_mousedown; MM_ocultar(); };
	function getMonthName(myMonth) { var mName=DG_gvv('DG_months'+ac()).split(","); return mName[myMonth-1]; };
	function viewCalendar(id, format, e ){ cal_old_mousedown = document.body.onmousedown; document.body.onmousedown = ""; MM_nombreDiv="DGdivCalendar"+ac(); MM_mostrar(MM_nombreDiv, e); setCalData(id, format); };
		
	function setCalData(id, format, MonthToShow, YearToShow ) {
		var myID=id.split("_AjaxDhtml");
		if (DG_isdefined(myID[1])){
			arrFldData = myID[0].split('.-.');
			var idLen = ac().length; var fldLen = arrFldData[0].length; var fldData = DG_left(arrFldData[0], fldLen - idLen); var origidField = fldData+".-."+arrFldData[1];
			fecha=DG_gvv("i"+ac()+origidField);
		}else{
			fecha=DG_gvv("i"+myID[0]);
		};
		var monthPos = format.indexOf('mm'); month = parseInt(fecha.substr(monthPos,2));	
		var yearPos = format.indexOf('yyyy');year = parseInt(fecha.substr(yearPos,4));
		if(DG_isdefined(MonthToShow) && MonthToShow!="") month=MonthToShow;
		if(DG_isdefined(YearToShow) && YearToShow!="") year=YearToShow;
		year = parseInt(year); month= parseInt(month);
		var now   = new Date();
		if (year==0 || isNaN(year)) year=now.getYear();
		if (month==0 || isNaN(month)) month=now.getMonth()+1;
		year = parseInt(year); month= parseInt(month);
		if (year < 1000) year+=1900;
		month--; if (month > 11) { month-=12; year++;} if (month < 0) { month+=12; year--;};
		var myDate= now.getDate();
		var StartMonth = new Date(year, month, 1);
		var StartMonth = StartMonth.getDay();
		var days = daysInMonth( month + 1, year);
		drawCal(StartMonth + 1, days, myDate, month + 1, year, id, format);
	};

	function drawCal(firstDay, lastDate, aDate, myMonth, myYear, id, format) {
		var now = new Date();
		lm = myMonth-1; na=la=myYear; if (lm<0) {lm=12; la=myYear-1;};
		nm = myMonth+1; if (nm>12) {nm=1; na=myYear+1;};
		ay = now.getYear(); if (ay < 1000) ay+=1900;
		var strOutput = '<table id="tableCalendar"><tr>';
		strOutput += '<td class="calHeader" colspan="7" align="center" style="cursor:pointer"><a href="javascript:setCalData(\''+id+'\', \''+format+'\', '+lm+','+la+')">&laquo;<\/a>&nbsp;';
		strOutput += '<select id="calMonth" name="calMonth" onchange="setCalData(\''+id+'\', \''+format+'\', this.value,'+myYear+')">';
		for (nMonth=1;nMonth<=12;nMonth++){
		  	strOutput += '<option value="'+nMonth+'" ';
			if (nMonth==myMonth) strOutput += ' selected="selected" ';
			strOutput += '>'+getMonthName(nMonth)+'<\/option>';
		};
		strOutput += '<\/select>';
		strOutput += '<select id="calYear" name="calYear" onchange="setCalData(\''+id+'\', \''+format+'\', '+myMonth+',this.value)">';
		for (nYear=myYear-25;nYear<=myYear+25;nYear++){
		  	strOutput += '<option value="'+nYear+'" ';
			if (nYear==myYear) strOutput += ' selected="selected" ';
			strOutput += '>'+nYear+'<\/option>';
		};
		strOutput += '<\/select>';
		strOutput += '&nbsp;<a href="javascript:setCalData(\''+id+'\', \''+format+'\', '+nm+','+na+')">&raquo;<\/a><\/td>';
		strOutput += '<\/tr><tr>';

		var dNames=DG_gvv('DG_days'+ac()).split(",");
		var dName = new Array(7);
		dName[0] = "<abbr title='"+dNames[0]+"'>"+dNames[0].substr(0,1)+"<\/abbr>";
		dName[1] = "<abbr title='"+dNames[1]+"'>"+dNames[1].substr(0,1)+"<\/abbr>";
		dName[2] = "<abbr title='"+dNames[2]+"'>"+dNames[2].substr(0,1)+"<\/abbr>";
		dName[3] = "<abbr title='"+dNames[3]+"'>"+dNames[3].substr(0,1)+"<\/abbr>";
		dName[4] = "<abbr title='"+dNames[4]+"'>"+dNames[4].substr(0,1)+"<\/abbr>";
		dName[5] = "<abbr title='"+dNames[5]+"'>"+dNames[5].substr(0,1)+"<\/abbr>";
		dName[6] = "<abbr title='"+dNames[6]+"'>"+dNames[6].substr(0,1)+"<\/abbr>";
	
		for (var dayNum = 0; dayNum < 7; ++dayNum) strOutput += '<td class="dayTitles">' + dName[dayNum] + '<\/td>';
		strOutput += '<\/tr>';
		var cDay = curCell = 1;

		endLastMonth=daysInMonth(lm, la);
		SF = endLastMonth - (firstDay - 2);
		
		for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
			strOutput += '<tr>';
			for (var col = 1; col <= 7; ++col) {
				if (cDay > lastDate) break;
				if (curCell < firstDay) {
					strOutput += '<td class="nodayCell">'+SF+'<\/td>';SF++;
					curCell++;
				} else {
					var TD_text = '<td class="dayCell" ';
					if (cDay == aDate && myMonth == now.getMonth()+1 && myYear == ay) TD_text = '<td class="todayCell"';
					strOutput += TD_text + ' onclick="setMyDate('+cDay+', \''+id+'\', \''+format+'\')" style="cursor:pointer" >' + cDay + '<\/td>';
					cDay++;
				};
			};
			SF=1; for (colf = col; colf <= 7; ++colf){ strOutput += '<td class="nodayCell">'+SF+'<\/td>'; SF++;};
			strOutput += '<\/tr>';
		};
		strOutput += '<tr><td colspan="7" class="closeCell" align="center"><a href="javascript:closeCalendar()">'+DG_gvv('DGtxtCancel'+ac())+'<\/a><\/td><\/tr><\/table>';
		document.getElementById("DGdivCalendar"+ac()).innerHTML=strOutput;
	};