//<script language="JavaScript" type="text/javascript">
////////////////////////////////////////////////////////////////////////////////////
// global var
//

var colday = new Array('do','lu','ma','mi','ju','vi','sa');
var sday   = new Array('domingo','lunes','martes','miercoles','jueves','viernes','sabado');

var months = new Array('Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio',
                       'Agosto','Septiembre','Octubre','Noviembre','Diciembre');
                       
var monthStyleLeft = new Array('42','36','41','45','43','44','46','39','25','36','27','29');
var yearStyleLeft  = new Array('79','84','79','75','77','77','75','82','95','85','93','91')

var date    = new Date();
var now     = new Date();
var day     = now.getDate();
var month   = now.getMonth();
var year    = now.getFullYear();
var calendarRetVal  = new String();

var oPopupCalendar = window.createPopup();

////////////////////////////////////////////////////////////////////////////////////
// Begin
//

var divCal = document.createElement('div'); window.document.body.appendChild(divCal);
divCal.id  = 'Calendar';
divCal.style.display  = 'none';

var table =document.createElement('table'); divCal.appendChild(table);
with (table) {
	border       = '0px';
	width        = '150px';
	height       = '19px';
	cellSpacing  = '0px';
	cellPadding  = '0px';
	bgColor      = '#FFFFF0';
}

var tr = document.createElement('tr'); table.appendChild(tr);

var td = document.createElement('td');
tr.appendChild(td)
with (td) {
	id          = 'monthPrevious';
	innerHTML   = '&lt;&lt;';
	title       = 'ver ' + String(months == 0 ? months[11] : months[month - 1]);
	noWrap      = true;
	width       = '15%';
	
	onmouseover = "this.style.textDecoration = 'underline'";
	onmouseout  = "this.style.textDecoration = 'none'";
	
	style.fontFamily      = 'verdana';
	style.fontSize        = '8pt';
	style.fontWeight      = 'normal';
	style.color           = '#FFFFFF';
	style.backgroundColor = '#678EF5';
	style.textAlign       = 'center';
	style.verticalAlign   = 'middle';
	style.borderBottom    = '1px solid #FFFFFF';
	style.cursor          = 'hand';
}

var td = document.createElement('td'); tr.appendChild(td)
with (td) {
	id     = 'monthYear';
	noWrap = true;
	
	style.colSpan         = '5';
	style.fontFamily      = 'verdana';
	style.fontSize        = '8pt';
	style.fontWeight      = 'normal';
	style.color           = '#FFFFFF';
	style.backgroundColor = '#678EF5';
	style.height          = '19px';
	style.textAlign       = 'center';
	style.verticalAlign   = 'middle';
	style.borderBottom    = '1px solid #FFFFFF';
	style.cursor          = 'hand';
}

var span = document.createElement('span'); td.appendChild(span);
with (span) {
	id          = 'month';
	title       = 'seleccionar mes';
	onmouseover = "this.style.textDecoration = 'underline'";
	onmouseout  = "this.style.textDecoration = 'none'";
	innerText   = String(months[month]);
	
	insertAdjacentHTML('afterEnd','&nbsp;')
}

var span = document.createElement('span'); td.appendChild(span);
with (span) {
	id          = 'year';
	title       = 'seleccionar a' + String.fromCharCode(241) + 'o';
	onmouseover = "this.style.textDecoration = 'underline'";
	onmouseout  = "this.style.textDecoration = 'none'";
	innerText   = '';
}

var td = document.createElement('td'); tr.appendChild(td)
with (td) {
	id = 'monthNext';
	width       = '14%';
	title       = 'ver ' + String(months == 11 ? months[0] : months[month + 1]);
	onmouseover = "this.style.textDecoration = 'underline'";
	onmouseout  = "this.style.textDecoration = 'none'";
	noWrap      = true;
	innerHTML   = '&gt;&gt;';
	
	style.fontFamily      = 'verdana';
	style.fontSize        = '8pt';
	style.fontWeight      = 'normal';
	style.color           = '#FFFFFF';
	style.backgroundColor = '#678EF5';
	style.textAlign       = 'center';
	style.verticalAlign   = 'middle';
	style.borderBottom    = '1px solid #FFFFFF';
	style.cursor          = 'hand';
}

var table =document.createElement('table'); divCal.appendChild(table);
with (table) {
	border       = '0px';
	width        = '150px';
	height       = '19px';
	cellSpacing  = '0px';
	cellPadding  = '0px';
	bgColor      = '#FFFFF0';
}

var tr = document.createElement('tr'); table.appendChild(tr);
for (var i = 0 ; i < colday.length ; i++) {
	var td = document.createElement('td'); tr.appendChild(td);
	with (td) {
		id        = 'colDay_' + i;
		width     = '19px';
		title     = sday[i];
		innerText = colday[i];
		
		style.fontFamily      = 'verdana';
		style.fontSize        = '8pt';
		style.fontWeight      = 'normal';
		style.color           = '#FFFFFF';
		style.backgroundColor = '#678EF5';
		style.height          = '19px';
		style.textAlign       = 'center';
		style.verticalAlign   = 'middle';
		style.borderBottom    = '1px solid #FFFFFF';
		style.cursor          = 'default';
		
		if ( i == 0 ) {
			style.color = 'silver';
		}
	}
}

var table =document.createElement('table'); divCal.appendChild(table);
with (table) {
	id           = 'tblDays';
	border       = '0px';
	width        = '150';
	height       = '19px';
	cellSpacing  = '0px';
	cellPadding  = '0px';
	bgColor      = '#FFFFF0';
}

for (var i = 0 ; i < 6 ; i++) {
	var tr = document.createElement('tr'); table.appendChild(tr);
	for (var j = 0 ; j < 7 ; j++) {
		var td = document.createElement('td'); tr.appendChild(td);
		with (td) {
			id        = 'cell_' +  i + j;
			innerHTML = '&nbsp';
			
			onmouseover = "this.style.textDecoration = 'underline'";
			onmouseout  = "this.style.textDecoration = 'none'";
			
			style.fontFamily      = 'verdana';
			style.fontSize        = '7pt';
			style.fontWeight      = 'normal';
			style.color           = 'black';
			style.backgroundColor = '#FFFFF0';
			style.height          = '19px';
			style.textAlign       = 'center';
			style.verticalAlign   = 'middle';
			style.cursor          = 'default';
			
			if ( j == 0 ) {
				style.color = 'background';
			}
		}
	}
}

var table =document.createElement('table'); divCal.appendChild(table);
with (table) {
	border       = '0px';
	width        = '150';
	height       = '19px';
	cellSpacing  = '0px';
	cellPadding  = '0px';
	bgColor      = '#FFFFF0';
}

var tr = document.createElement('tr'); table.appendChild(tr);

var td = document.createElement('td'); tr.appendChild(td);
with (td) {
	id        = 'titleToday'
	width     = '50%';
	innerText = 'Hoy es:';
		
	style.fontFamily      = 'verdana';
	style.fontSize        = '8pt';
	style.fontWeight      = 'normal';
	style.color           = '#FFFFFF';
	style.backgroundColor = '#678EF5';
	style.height          = '19px';
	style.textAlign       = 'right';
	style.verticalAlign   = 'middle';
	style.borderTop       = '1px solid #FFFFFF';
	style.cursor          = 'default';
		
}

var td = document.createElement('td'); tr.appendChild(td);
with (td) {
	id        = 'toDay';
	width     = '50%';
	innerText = (now.getDate() < 10 ? '0' + now.getDate() : now.getDate()) + '/' + 
	            ((now.getMonth() + 1) < 10 ? '0' + (now.getMonth() + 1) : (now.getMonth() + 1)) + '/' + 
	            now.getFullYear();
	
	title = sday[now.getDay()] + ' ' + now.getDate() + ' de ' + months[now.getMonth()] + ' de ' + now.getFullYear();
	
	onmouseover = "this.style.textDecoration = 'underline'";
	onmouseout  = "this.style.textDecoration = 'none'";
		
	style.fontFamily      = 'verdana';
	style.fontSize        = '8pt';
	style.fontWeight      = 'normal';
	style.color           = '#FFFFFF';
	style.backgroundColor = '#678EF5';
	style.height          = '19px';
	style.textAlign       = 'center';
	style.verticalAlign   = 'middle';
	style.borderTop       = '1px solid #FFFFFF';
	style.cursor          = 'hand';
		
}

var table =document.createElement('table'); divCal.appendChild(table);
with (table) {
	id           = 'tblMonths';
	border       = '0px';
	width        = '66px';
	height       = '150px';
	cellSpacing  = '0px';
	cellPadding  = '0px';
	bgColor      = '#FFFFF0';
	
	style.left       = '40px';
	style.top        = '15px';
	style.border     = '1px solid black';
	style.display    = 'none';
	style.position   = 'absolute'; 
    style.overflow   = 'hidden'; 
    style.fontFamily = 'verdana'; 
    style.fontSize   = '7pt';
    style.clip       = 'rect(0px, 66px, 150px, 0px)';
    style.cursor     = 'default';
}

for (var i = 0; i < 12; i++){
	var tr = document.createElement('tr'); table.appendChild(tr);
	var td = document.createElement('td'); tr.appendChild(td);
	with (td) {
		id          = 'month_' + i;
		innerHTML   = months[i];
		onmouseover = "this.style.textDecoration = 'underline'";
		onmouseout  = "this.style.textDecoration = 'none'";
		tabIndex    = i;
			
		style.paddingLeft = '3px';
		style.fontWeight  = 'normal';
		style.cursor      = 'hand';
	}
}

var divPageYears = document.createElement('div'); divCal.appendChild(divPageYears);
with ( divPageYears ) {
	id  = 'pageYears';
	
	style.left            = '60px';
	style.top             = '15px';
	style.display         = 'none';
	style.position        = 'absolute'; 
    style.overflow        = 'hidden'; 
    style.fontFamily      = 'verdana'; 
    style.fontSize        = '7pt'; 
    style.width           = '53px';
    style.height          = '147px'; 
    style.clip            = 'rect(0px, 53px, 147px, 0px)'; 
    style.backgroundColor = '#FFFFF0';
    style.border          = '1px solid black';
    style.cursor          = 'hand';
}

var table =document.createElement('table'); divPageYears.appendChild(table);
with (table) {
	id           = 'tblYears';
	border       = '0px';
	width        = '38px';
	height       = '150px';
	cellSpacing  = '0px';
	cellPadding  = '0px';
	bgColor      = '#FFFFF0';
	
	style.left       = '0px';
	style.top        = '0px';
	style.position   = 'absolute'; 
    style.overflow   = 'hidden'; 
    style.fontFamily = 'verdana'; 
    style.fontSize   = '7pt';
    style.clip       = 'rect(0px, 38px, 150px, 0px)';
    style.cursor     = 'default';
}

for (var i = 0; i < 13; i++){
	var tr = document.createElement('tr'); table.appendChild(tr);
	var td = document.createElement('td'); tr.appendChild(td);
	with (td) {
		id          = 'year_' + i;
		innerText   = i;
		onmouseover = "this.style.textDecoration = 'underline'";
		onmouseout  = "this.style.textDecoration = 'none'";
			
		style.paddingLeft = '3px';
		style.fontWeight  = 'normal';
		style.cursor      = 'hand';
	}
}

var divScrollYears = document.createElement('div'); divPageYears.appendChild(divScrollYears);
with ( divScrollYears ) {
	id = 'scrollYears';
	
	style.position         = 'absolute';
    style.overflow         = 'hidden'; 
    style.fontFamily       = 'verdana';
    style.fontSize         = '8pt'; 
    style.left             = '37px';
    style.top              = '0px'; 
    style.width            = '16px'; 
    style.height           = '147px'; 
    style.clip             = 'rect(0px, 16px, 147px, 0px)'; 
	style.borderLeft       = '1px solid black';
    style.backgroundColor  = '#FFFFF0';
}

var divScrollYearsUp = document.createElement('div'); divScrollYears.appendChild(divScrollYearsUp);
with ( divScrollYearsUp ) {
	id = 'scrollYearsUp';
	
	style.paddingTop      = '2px';
	style.paddingLeft     = '2px';
	style.position        = 'absolute';
    style.overflow        = 'hidden'; 
    style.fontFamily      = 'verdana';
    style.fontSize        = '8pt'; 
    style.left            = '0px';
    style.top             = '0px'; 
    style.width           = '16px'; 
    style.height          = '16px'; 
    style.clip            = 'rect(0px, 16px, 16px, 0px)'; 
	style.borderBottom    = '1px solid black';
    style.backgroundColor = '#FFFFF0';
}

var imgUp = document.createElement('img'); divScrollYearsUp.appendChild(imgUp);
with ( imgUp ) {
	id     = 'imgUp';
	border = 0;
	src    = '';
}

var divScrollYearsDown = document.createElement('div'); divScrollYears.appendChild(divScrollYearsDown);
with ( divScrollYearsDown ) {
	id = 'scrollYearsDown';
	
	style.paddingTop      = '3px';
	style.paddingLeft     = '2px';
	style.position        = 'absolute';
    style.overflow        = 'hidden'; 
    style.fontFamily      = 'verdana';
    style.fontSize        = '8pt'; 
    style.left            = '0px';
    style.top             = '130px'; 
    style.width           = '16px'; 
    style.height          = '16px'; 
    style.clip            = 'rect(0px, 16px, 16px, 0px)'; 
	style.borderTop       = '1px solid black';
    style.backgroundColor = '#FFFFF0';
}

var imgDown = document.createElement('img'); scrollYearsDown.appendChild(imgDown);
with ( imgDown ) {
	id     = 'imgDown';
	border = 0;
	src    ='';
}

oPopupCalendar.document.body.innerHTML    = divCal.innerHTML;
oPopupCalendar.document.body.bgColor      = '#FFFFF0';
oPopupCalendar.document.body.style.border = '1px solid black';
    
oPopupCalendar.document.all('monthPrevious').attachEvent('onclick',monthPrevious);
oPopupCalendar.document.all('monthNext').attachEvent('onclick',monthNext);
oPopupCalendar.document.all('month').attachEvent('onclick',viewMonths);
oPopupCalendar.document.all('year').attachEvent('onclick',viewYears);
oPopupCalendar.document.all('toDay').attachEvent('onclick',setToDay);

oPopupCalendar.document.all('imgUp').attachEvent('onclick',scrollYearUp);
oPopupCalendar.document.all('imgDown').attachEvent('onclick',scrollYearDown);

for ( var i = 0 ; i < oPopupCalendar.document.all('tblMonths').cells.length; i++ ) {
	oPopupCalendar.document.all('tblMonths').cells[i].attachEvent('onclick',setMonthSelected);
}

for ( var i = 0 ; i < oPopupCalendar.document.all('tblYears').cells.length; i++ ) {
	oPopupCalendar.document.all('tblYears').cells[i].attachEvent('onclick',setYearSelected);
}

for (var i = 0 ; i < oPopupCalendar.document.all('tblDays').cells.length; i++){
	oPopupCalendar.document.all('tblDays').cells[i].attachEvent('onclick',returnValue);
}

////////////////////////////////////////////////////////////////////////////////////
// Functions
//

function returnValue() {
	var cell = oPopupCalendar.document.parentWindow.event.srcElement;
	var obj  = document.all(popupActive);
	obj.value = cell.title;
	cell.style.textDecoration = 'none';
	oPopupCalendar.hide();
}

function setToDay() {
	var d = new Date();
	setMonth(d);
}

function monthPrevious() {
	var d = new Date(date);
	d.setMonth(d.getMonth() - 1);
	setMonth(d);
}

function monthNext() {
	var d = new Date(date);
	d.setMonth(d.getMonth() + 1);
	setMonth(d);
}

function viewMonths() {
	for (var i = 0; i < 12; i++){
		oPopupCalendar.document.all('month_' + i).style.backgroundColor = '#FFFFF0';
		oPopupCalendar.document.all('month_' + i).style.color           = 'black';
		
		if (i == date.getMonth()) {
			oPopupCalendar.document.all('month_' + i).style.backgroundColor = '#678EF5';
			oPopupCalendar.document.all('month_' + i).style.color           = 'white';
		}
	}
	
	oPopupCalendar.document.all('pageYears').style.display = 'none';
	oPopupCalendar.document.all('tblMonths').style.left = monthStyleLeft[date.getMonth()] + 'px';
	oPopupCalendar.document.all('tblMonths').style.display    = '';
}

function setMonthSelected() {
	var d     = new Date(date);
	var month = oPopupCalendar.document.parentWindow.event.srcElement;
	d.setMonth(month.tabIndex);
	setMonth(d);
}

function viewYears() {
   setScrollYears(date);
	
   oPopupCalendar.document.all('tblMonths').style.display = 'none';
   oPopupCalendar.document.all('pageYears').style.left    = yearStyleLeft[date.getMonth()] + 'px';
   oPopupCalendar.document.all('pageYears').style.display = '';
}

function setYearSelected() {
	var d    = new Date(date);
	var year = oPopupCalendar.document.parentWindow.event.srcElement;
	d.setFullYear(year.innerText);
	setMonth(d);
}

function scrollYearUp(){
	var d = new Date();
	var i = oPopupCalendar.document.all('tblYears').cells[0].innerText  - 1;
	d.setFullYear(i);
	setScrollYears(d);
}

function scrollYearDown(){
	var d = new Date();
	var i = oPopupCalendar.document.all('tblYears').cells[12].innerText;
	d.setFullYear(i);
	setScrollYears(d);
}

function setScrollYears(d) {
	var j = (parseInt(d.getFullYear() / 10) * 10);

	for ( var i = 0 ; i < 13; i++ ) {
		oPopupCalendar.document.all('tblYears').cells[i].innerText = j;
		
		oPopupCalendar.document.all('tblYears').cells[i].style.backgroundColor = '#FFFFF0';
		oPopupCalendar.document.all('tblYears').cells[i].style.color           = 'black';
		
		if ( j == date.getFullYear()) {
			oPopupCalendar.document.all('tblYears').cells[i].style.backgroundColor = '#678EF5';
			oPopupCalendar.document.all('tblYears').cells[i].style.color           = 'white';
		}
		
		j++;
	}
}

function setMonth() {
	oPopupCalendar.document.all('tblMonths').style.display = 'none';
	oPopupCalendar.document.all('pageYears').style.display = 'none';

	if ( setMonth.arguments.length != 0 ) { var d = new Date(setMonth.arguments[0]); }
	else { var d = new Date(); }
	
	if (d.getMonth() == date.getMonth() && d.getDate() == date.getDate() && d.getFullYear() == date.getFullYear() && oPopupCalendar.document.all('cell_22').title != '') {
		return false;
	}
	
	if (d.getMonth() == 0) {
		oPopupCalendar.document.all('monthPrevious').title = 'ver ' + months[11] + ' de ' + (d.getFullYear() - 1);
	}
	else {
		oPopupCalendar.document.all('monthPrevious').title = 'ver ' + months[d.getMonth() - 1] + ' de ' + d.getFullYear();
	}
	
	if (d.getMonth() == 11) {
		oPopupCalendar.document.all('monthNext').title = 'ver ' + months[0] + ' de ' + (d.getFullYear() + 1);
	}
	else {
		oPopupCalendar.document.all('monthNext').title = 'ver ' + months[d.getMonth() + 1] + ' de ' + d.getFullYear();
	}

	oPopupCalendar.document.all('month').innerText = String(months[d.getMonth()]);
	oPopupCalendar.document.all('year').innerText  = String(d.getFullYear());
	
	for (var i = 0 ; i < oPopupCalendar.document.all('tblDays').rows[0].cells.length; i++){
		var cell = oPopupCalendar.document.all('tblDays').rows[0].cells[i];
		cell.innerText = '';
		cell.title     = '';
		cell.style.backgroundColor = '#FFFFF0';
		cell.style.cursor          = 'default';
		cell.style.color           = 'black';
		cell.style.border          = '';
	}
	
	for (var i = 0 ; i < oPopupCalendar.document.all('tblDays').rows[4].cells.length; i++){
		var cell = oPopupCalendar.document.all('tblDays').rows[4].cells[i];
		cell.innerText = '';
		cell.title     = '';
		cell.style.backgroundColor = '#FFFFF0';
		cell.style.cursor          = 'default';
		cell.style.color           = 'black';
		cell.style.border          = '';
	}
	
	for (var i = 0 ; i < oPopupCalendar.document.all('tblDays').rows[5].cells.length; i++){
		var cell = oPopupCalendar.document.all('tblDays').rows[5].cells[i];
		cell.innerText = '';
		cell.title     = '';
		cell.style.backgroundColor = '#FFFFF0';
		cell.style.cursor          = 'default';
		cell.style.color           = 'black';
		cell.style.border          = '';
	}
	
	var j=0;
	var i=1;
	
	for (var i=1; i<=31; i++){
		var s = new String();
		var x = new Date(d.getMonth()+1+'/'+i+'/'+d.getFullYear());
		if (d.getMonth() == x.getMonth()){
			
			var cell=oPopupCalendar.document.all('cell_'+j+x.getDay());
			cell.innerText = i;
			
			if ( i < 10 ) { s += '0' + i + '/';	}
			else { s += i + '/'; }
			
			if ( (x.getMonth() + 1) < 10 ) { s += '0' + (x.getMonth() + 1) + '/' }
			else { s += (x.getMonth() + 1) + '/' }
			
			s += x.getFullYear();
			
			cell.title = s;
			
			if ( x.getDay() == 0 ) { cell.style.color = 'background'; }
			
			cell.style.backgroundColor = '#FFFFF0';
			cell.style.cursor          = 'default';
			cell.style.color           = 'black';
			cell.style.border          = '';
			cell.style.cursor          = 'hand';
			
			if (x.getMonth() == now.getMonth() && x.getDate() == now.getDate() && x.getFullYear() == now.getFullYear()) {
				cell.style.backgroundColor = '#678EF5';
				cell.style.color           = 'white';
			}
			if (x.getDate() == d.getDate()) { cell.style.border = '1px solid background'; }
			if (x.getDay() == 0) { cell.style.color = 'gray'; }
		}
		if (x.getDay()==6) {j++;}
	}
	
	date = d;
}

/* AGREGADO */
var popupActive = new String();

function setDate() {
	var d   = new Date();
	var obj = window.document.all(popupActive);
	var val = new String( '' );
	var x   = 0;
	var y   = obj.offsetHeight + 1;
	
	val = obj.value;
	
	if ( val != '' ) {
		d = new Date(val.substring(3,5)+'/'+val.substring(0,2)+'/'+val.substring(6,10));
		if ( isNaN(d) ) {
			alert('Ingrese una fecha válida.');
			obj.select();
			return;
		}
	}
	
	setMonth(d);
	oPopupCalendar.document.all('imgDown').src = '../../images/calendardown.ico';
	oPopupCalendar.document.all('imgUp').src   = '../../images/calendarup.ico';
	oPopupCalendar.show(x, y, 152, 173, obj );
	
}


