//****************************************************************************
// PopCalendar 4.1, Emailware(please mail&commend me if u like it)
// Originally coded by Liming(Victor) Weng, email: victorwon@netease.com
// Release date: 2000.5.9
// Anyone may modify it to satify his needs, but please leave this comment ahead.
//****************************************************************************

var gdCtrl = new Object();
var gcGray = "#808080";
var gcToggle = "#DDDDDD";
var gcBG = "#EEEEEE";

var gdCurDate = new Date();
var giYear = gdCurDate.getFullYear();
var giMonth = gdCurDate.getMonth()+1;
var giDay = gdCurDate.getDate();
var VicPopCal = new Object();

//****************************************************************************
// Param: popCtrl is the widget beyond which you want this calendar to appear;
//        dateCtrl is the widget into which you want to put the selected date;
//        popCal is the widget to display the calendar;  
// i.e.: <input type="text" name="dc" style="text-align:center" readonly><INPUT type="button" value="V" onclick="fPopCalendar(dc,dc,popCal,event);return false">
//****************************************************************************
function fPopCalendar(popCtrl,dateCtrl,popCal,e){
  if(!e) e=window.event;
  e.cancelBubble=true;
  VicPopCal = popCal;
  gdCtrl = dateCtrl;
  //Si el objeto sobre el que ponemos la fecha ya tiene valor abrimos el calendario en el mes de ese valor...
  if(dateCtrl.value.length==0)
	fSetYearMon(giYear, giMonth);
  else {
    var aFecha=dateCtrl.value.split("/");
	fSetYearMon(parseInt(aFecha[2],10), parseInt(aFecha[1],10));
  }
  var point = fGetXY(popCtrl);
  with (VicPopCal.style) {
  	left = point.x;
	top  = point.y+popCtrl.offsetHeight+1;
	visibility = 'visible';
  }
  with (document.getElementById('DivShim').style) {
    width = VicPopCal.offsetWidth;
    height = VicPopCal.offsetHeight;
    top = VicPopCal.style.top;
    left = VicPopCal.style.left;
    zIndex = VicPopCal.style.zIndex - 1;
    display = "block";
  }
  VicPopCal.focus();
}

function fSetDate(iYear, iMonth, iDay){
  var sDay=new String(iDay);
  var sMonth=new String(iMonth);
  if(sDay.length==1)
	sDay='0' + sDay;
  if(sMonth.length==1)
	sMonth='0' + sMonth;
  gdCtrl.value = sDay+"/"+sMonth+"/"+iYear; //Here, you could modify the locale as you need !!!!
  document.getElementById('DivShim').style.display = "none";
  VicPopCal.style.visibility = "hidden";
}

function fSetSelected(aCell){
  var iOffset = 0;
  var iYear = parseInt(document.getElementById('tbSelYear').value);
  var iMonth = parseInt(document.getElementById('tbSelMonth').value);

  aCell.bgColor = gcBG;
  with (aCell){
  	var iDay = parseInt(innerHTML);
  	if (style.color==gcGray || style.color=='rgb(128, 128, 128)')
		iOffset = (iDay<15)?1:-1;
	iMonth += iOffset;
	if (iMonth<1) {
		iYear--;
		iMonth = 12;
	}else if (iMonth>12){
		iYear++;
		iMonth = 1;
	}
  }
  fSetDate(iYear, iMonth, iDay);
}

function Point(iX, iY){
	this.x = iX;
	this.y = iY;
}

function fBuildCal(iYear, iMonth) {
  var aMonth=new Array();
  for(i=1;i<7;i++)
  	aMonth[i]=new Array(i);

  var dCalDate=new Date(iYear, iMonth-1, 1);
  //Esta modificación de debe a q nuestra semana empieza el lunes...
  var iDayOfFirst=dCalDate.getDay()-1;
  if (iDayOfFirst<0)
	iDayOfFirst=6
  var iDaysInMonth=new Date(iYear, iMonth, 0).getDate();
  var iOffsetLast=new Date(iYear, iMonth-1, 0).getDate()-iDayOfFirst+1;
  var iDate = 1;
  var iNext = 1;

  for (d = 0; d < 7; d++)
	aMonth[1][d] = (d<iDayOfFirst)?-(iOffsetLast+d):iDate++;
  for (w = 2; w < 7; w++)
  	for (d = 0; d < 7; d++)
		aMonth[w][d] = (iDate<=iDaysInMonth)?iDate++:-(iNext++);
  return aMonth;
}

function fDrawCal(iYear, iMonth, iCellWidth, iDateTextSize) {
  var WeekDay = new Array("Lu","Ma","Mi","Ju","Vi","Sa","Do");
  var styleTD = " bgcolor='"+gcBG+"' width='"+iCellWidth+"' valign='middle' align='center' style='font:"+iDateTextSize+" Verdana;";

  with (document) {
	write("<tr>");
	for(i=0; i<7; i++)
		write("<td "+styleTD+"color:#FFFFFF;background-color:#808080;font:bold' >" + WeekDay[i] + "</td>");
	write("</tr>");

  	for (w = 1; w < 7; w++) {
		write("<tr>");
		for (d = 0; d < 7; d++) {
			write("<td id='calCell"+((7*w)+d)+"' "+styleTD+"cursor:hand;cursor:pointer;' onMouseOver='this.bgColor=gcToggle' onMouseOut='this.bgColor=gcBG' onclick='fSetSelected(this)'></td>");
		}
		write("</tr>");
	}
  }
}

function fUpdateCal(iYear, iMonth) {
  myMonth = fBuildCal(iYear, iMonth);
  var i = 0;
  for (w = 0; w < 6; w++)
	for (d = 0; d < 7; d++) {
		with (document.getElementById("calCell"+((7*(w+1))+d))) {
			if (myMonth[w+1][d]<0) {
				style.color = gcGray;
				innerHTML = -myMonth[w+1][d];
			}else{
				style.color = ((d==6))?"red":"black";
				innerHTML = myMonth[w+1][d];
			}
		}
	}
}

//Establecemos un mes y un año determinado a mostrar en el calendario...
//se utiliza al abrirlo por primera vez...
function fSetYearMon(iYear, iMon){
  document.getElementById('tbSelMonth').options[iMon-1].selected = true;
  for (i = 0; i < document.getElementById('tbSelYear').length; i++)
	//Seleccionamos el año que le hemos pasado en el combo...
	if (document.getElementById('tbSelYear').options[i].value == iYear)
		document.getElementById('tbSelYear').options[i].selected = true;
  fUpdateCal(iYear, iMon);
}

//Si el decremento es menor de 1 entonces estamos en el año previo... de ahí ese if...
function fPrevMonth(){
  var iMon = document.getElementById('tbSelMonth').value;
  var iYear = document.getElementById('tbSelYear').value;

  if (--iMon<1) {
	  iMon = 12;
	  iYear--;
  }

  fSetYearMon(iYear, iMon);
}

//Si el incremento es mayor de 12 entonces estamos en un nuevo año... de ahí ese if...
function fNextMonth(){
  var iMon = document.getElementById('tbSelMonth').value;
  var iYear = document.getElementById('tbSelYear').value;

  if (++iMon>12) {
	  iMon = 1;
	  iYear++;
  }

  fSetYearMon(iYear, iMon);
}

function fGetXY(aTag){
  var oTmp = aTag;
  var pt = new Point(0,0);
  do {
  	pt.x += oTmp.offsetLeft;
  	pt.y += oTmp.offsetTop;
  	oTmp = oTmp.offsetParent;
  } while(oTmp.tagName!="BODY");
  return pt;
}

var gMonths = new Array("Ene","Feb","Mar","Abr","May","Jun","Jul","Ago","Sep","Oct","Nov","Dic");

with (document) {
write("<style>");
write(".caja { BORDER: 1px solid black; FONT-SIZE: 10px; COLOR: black; FONT-FAMILY: Verdana }");
write(".botonCalendar { BORDER: 1px solid black; FONT-WEIGHT: bolder; COLOR: white; FONT-FAMILY: Verdana; BACKGROUND-COLOR: gray; font-size:7pt; width:18px; text-align: center }");
write("</style>");
write("<table id='popTable' border='0' bgcolor='#FFFFFF'>");
write("<TR>");
write("<td valign='middle' align='center' nowrap><input type='button' id='PrevMonth' name='PrevMonth' value='-' class='botonCalendar' onClick='fPrevMonth()'>");
write("&nbsp;<select id='tbSelMonth' name='tbSelMonth' onChange='fUpdateCal(tbSelYear.value, tbSelMonth.value)' class='caja'>");
for (i=0; i<12; i++)
	write("<option value='"+(i+1)+"'>"+gMonths[i]+"</option>");
write("</SELECT>");
write("&nbsp;<SELECT id='tbSelYear' name='tbSelYear' onChange='fUpdateCal(tbSelYear.value, tbSelMonth.value)' class='caja'>");
for(i=1900;i<2050;i++)
	write("<OPTION value='"+i+"'>"+i+"</OPTION>");
write("</SELECT>");
write("&nbsp;<input type='button' id='PrevMonth' name='PrevMonth' value='+' class='botonCalendar' onclick='fNextMonth()'>");
write("</td>");
write("</TR><TR>");
write("<td align='center'>");
write("<table width='100%' border='0' cellpadding='1' bgcolor='#FFFFFF'>");
fDrawCal(giYear, giMonth, 19, 10);
write("</table>");
write("</td>");
write("</TR><TR><TD align='center'>");
write("<font style='cursor: hand; cursor: pointer; font-size: 10px; font-family: Verdana,Arial' onclick='fSetDate(giYear,giMonth,giDay)'><b>Hoy</b>:&nbsp;&nbsp;"+giDay+"&nbsp;"+gMonths[giMonth-1]+",&nbsp;"+giYear+"</font>");
write("</TD></TR>");write("</TD></TR>");
write("</TABLE>");
}