/*******************
  Cual elemento
********************/
var f = 1;


/*******************
  Utility Functions
********************/
// day of week of month's first day
function getFirstDay(theYear, theMonth){
    var firstDate = new Date(theYear,theMonth,1);
    return firstDate.getDay( );
}
   
// number of days in the month
function getMonthLen(theYear, theMonth) {
    var nextMonth = new Date(theYear, theMonth + 1, 1);
    nextMonth.setHours(nextMonth.getHours( ) - 3);
    return nextMonth.getDate( );
}
   
// read position of an element in regular document flow
function getElementPosition(elemID) {
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}
   
// position and show calendar
function showCalendar(evt) {
    evt = (evt) ? evt : (event)? event: window.event;
    if (evt) {
        if (document.getElementById("calendar").style.visibility != "visible") {
			var elem = (evt.target) ? evt.target : evt.srcElement;
            var position = getElementPosition(elem.id);
            shiftTo("calendar", position.left + elem.offsetWidth, position.top);
            show("calendar");
        } else {
            hide("calendar");
        }
    }
}
   

function numeroMes(str){
	switch(str){
		case "Enero":		return 0;
		case "Febrero":		return 1;
		case "Marzo":		return 2;
		case "Abril":		return 3;
		case "Mayo":    	return 4;
		case "Junio":		return 5;
		case "Julio":		return 6;
		case "Agosto":		return 7;
		case "Septiembre":	return 8;
		case "Octubre":		return 9;
		case "Noviembre":	return 10;
		case "Diciembre":	return 11;
	}

}
/************************
  Draw Calendar Contents
*************************/
// clear and re-populate table based on form's selections
function populateTable() {
	// Get all elements from their id
    // pick up date form choices
	var form = document.getElementById("dateChooser");
	var chooseMonth = document.getElementById("chooseMonth");
	var chooseYear  = document.getElementById("chooseYear");
	var tableHeader = document.getElementById("tableHeader");
	var TBody = document.getElementById("tableBody");
	var theMonth = chooseMonth.value;
	theMonth = numeroMes(theMonth);
    var theYear = parseInt(chooseYear.options[chooseYear.selectedIndex].text);
    // initialize date-dependent variables
    var firstDay = getFirstDay(theYear, theMonth);
    var howMany = getMonthLen(theYear, theMonth);
    var today = new Date( );
    // fill in month/year in table header
    tableHeader.innerHTML = chooseMonth.options[chooseMonth.selectedIndex].text + " " + theYear;
    // initialize vars for table creation
    var dayCounter = 1;
    // clear any existing rows
    while (TBody.rows.length > 0) {
        TBody.deleteRow(0);
    }
    var newR, newC, dateNum;
    var done=false;

	while (!done) {
		// create new row at end
        newR = TBody.insertRow(TBody.rows.length);
        if (newR) {
            for (var i = 0; i < 7; i++) {
                // create new cell at end of row
                newC = newR.insertCell(newR.cells.length);
                if (TBody.rows.length == 1 && i < firstDay) {
                    // empty boxes before first day
                    newC.innerHTML = "&nbsp;";
                    continue;
                }
                if (dayCounter == howMany) {
                    // no more rows after this one
                    done = true;
                }
                // plug in link/date (or empty for boxes after last day)
                if (dayCounter <= howMany) {
                    if (today.getFullYear() == theYear && 
							today.getMonth() == theMonth && 
							today.getDate() == dayCounter){
                        newC.id = "today";
                    }
                    newC.innerHTML = "<a href='#'onclick='chooseDate(" +
                        dayCounter + "," + theMonth + "," + theYear + 
                        "); return false;'>" + dayCounter + "</a>";
                     dayCounter++;
               } else {
                    newC.innerHTML = "&nbsp;";
                }
            }
        } else {
            done = true;
        }
    }
}

/*******************
   Process Choice
********************/
function chooseDate(date, month, year) {
	month = month+1;
    document.getElementById("kdia"+f).value = (date < 10)? ("0"+date):date;
    document.getElementById("kmes"+f).value = (month < 10)? ("0"+month):month;
    document.getElementById("kano"+f).value = year;
    hide("calendar");
}

var cuantas=0;

function calendario(evt,i){
	if(cuantas < 1){
		initDHTMLAPI( );
		fillYears( );
		populateTable( );
		cuantas++;
	}
	f=i;
	showCalendar(evt);
}
   
