// DatePicker.js
// Version en formato YYYY/MM/DD

var esNN = (document.layers) ? 1 : 0 ;
var esIE = (document.all)    ? 1 : 0 ;
var fec, anio, mes, dia, ctrFec;
var winDP = -1;

function datePicker(ctrFecha)
{
	ctrFec = ctrFecha;
	var meses = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	var hoy_txt = "Hoy es " + formatDDMMAAAA(new Date());

	fecTmp = new Date();
	anioActual = fecTmp.getFullYear();
	
	if (checkDateTxt(ctrFecha.value)) {
		var auxF, auxD, auxM, auxA;
		auxF = trimWord(ctrFecha.value);
		auxD = auxF.substring (0, auxF.indexOf('/'));
		auxF = auxF.substring(auxF.indexOf('/')+1, auxF.length);
		auxM = auxF.substring (0, auxF.indexOf('/')); 
		auxF = auxF.substring(auxF.indexOf('/')+1, auxF.length);
		auxA = auxF;
		fec = new Date(auxM+"/"+auxD+"/"+auxA);
	} else {
		fec = new Date();
	}
	anio = fec.getFullYear();
	if ( anio < anioActual ) {
		var anioMax = anioActual;
	} else {
		var anioMax = anio;
	}
	var anioMin = 2000;
	mes = fec.getMonth();
	dia = fec.getDate();

	var wleft, wtop;
	if (esNN) {
		wleft=200;
		wtop=200;
	}
	else {
		wleft=event.screenX;
		wtop=event.screenY;
	}
  
	winDP = open("",null,"height=190,width=190,status=no,toolbar=no,menubar=no,location=no,resizable=no,dependent=yes,alwaysRaised=yes,top=" + wtop + ",left=" + wleft);
	winDP.focus();
	winDP.document.open();
	winDP.document.write("<HTML><HEAD><TITLE>Calendar</TITLE>");
	winDP.document.write("<STYLE type='text/css'><!--");
	winDP.document.write("A:link,");
	winDP.document.write("A:visited {COLOR:#073771; TEXT-DECORATION:none; FONT-FAMILY:Arial,Verdana,'Comic Sans MS'; FONT-SIZE:8pt}");
	winDP.document.write("A:active {COLOR:MediumVioletRed;TEXT-DECORATION:underline; FONT-FAMILY:Arial,Verdana,'Comic Sans MS'; FONT-SIZE:10pt; FONT-WEIGHT:bold}");
	winDP.document.write("A:hover {COLOR:blue;TEXT-DECORATION:none; FONT-FAMILY:Arial,Verdana,'Comic Sans MS'; FONT-SIZE:10pt; FONT-WEIGHT:bold}");
	winDP.document.write("A P {COLOR:MediumVioletRed;TEXT-DECORATION:underline; FONT-FAMILY:Arial,Verdana,'Comic Sans MS'; FONT-SIZE:10pt; FONT-WEIGHT:bold}");
	winDP.document.write(".dayWeek {BACKGROUND-COLOR: #cfd4e3; COLOR: #073771; FONT-FAMILY: Arial, Verdana, 'Comic Sans MS'; FONT-SIZE: 8pt}");
	winDP.document.write(".texto {COLOR: #073771; FONT-FAMILY: Arial, Verdana, 'Comic Sans MS'; FONT-SIZE: 8pt}");
	winDP.document.write(".textoHoy {COLOR: #cfd4e3; TEXT-DECORATION:none; FONT-FAMILY: Arial, Verdana, 'Comic Sans MS'; FONT-SIZE: 8pt; FONT-WEIGHT:bold}");
	winDP.document.write("SELECT {COLOR: #073771; FONT-FAMILY: Arial, Verdana, 'Comic Sans MS'; FONT-SIZE: 8pt}");
	winDP.document.write("--></STYLE></HEAD>");
	winDP.document.write("<BODY bgcolor=#073771 onUnload='JavaScript: winDP=-1;'>");
	winDP.document.write("<FORM name=formDatePicker>");
	winDP.document.write("<TABLE bgcolor=#073771 cellspacing=0 border=0>");
	winDP.document.write("<TR><TD class=texto align=center valign=top height=26 noWrap>");
	winDP.document.write("<A href='#' onClick='JavaScript: opener.anterior(self.document.formDatePicker.cbMonth, self.document.formDatePicker.cbYear); return false;'><SPAN class=textoHoy><B>&lt;&lt;&nbsp;</B></SPAN></A>");
	winDP.document.write("<SELECT name=cbMonth onChange='JavaScript: opener.loadDias(this.selectedIndex, self.document.formDatePicker.cbYear.options[self.document.formDatePicker.cbYear.selectedIndex].value);'>");

	for(i=0; i<12; i++){
		var mesi=i+1;
		if (mesi<10) mesi = "0" + mesi;
		if (i==mes) mesi += " selected";
		winDP.document.write("<OPTION value=" + mesi + ">" + meses[i] + "</OPTION>");
	}
	winDP.document.write("</SELECT>&nbsp;");
	winDP.document.write("<SELECT name=cbYear onChange='JavaScript: opener.loadDias(self.document.formDatePicker.cbMonth.selectedIndex, this.options[this.selectedIndex].value);'>");
	for(a=anioMax; a>=anioMin; a--) {
		if (a==anio) winDP.document.write("<OPTION value=" + a + " selected>" + a + "</OPTION>");
		else winDP.document.write("<OPTION value=" + a + ">" + a + "</OPTION>");
	}
	winDP.document.write("</SELECT>");
	winDP.document.write("<A href='#' onClick='JavaScript: opener.siguiente(self.document.formDatePicker.cbMonth, self.document.formDatePicker.cbYear); return false;'><SPAN class=textoHoy><B>&nbsp;&gt;&gt;</B></SPAN></A>");
	winDP.document.write("</TD></TR>");
	if (esNN) winDP.document.write("<TR><TD align=center height=135><LAYER name=divDias left=30 top=35></LAYER></TD></TR>");
	else winDP.document.write("<TR><TD align=center><DIV id=divDias></DIV></TD></TR>");
	winDP.document.write("<TR><TD class=textoHoy align=center valign=bottom height=16>" + hoy_txt + "</TD></TR>");
	winDP.document.write("</TABLE></FORM>");
	winDP.document.write("</BODY></HTML>");
	winDP.document.close();

	loadDias(mes,anio);
}
      
function numeroDeDias(mth,yr)
{ if (mth==3 || mth==5 || mth==8 || mth==10) return 30;
  else if(mth==1 && bisiesto(yr)) return 29;
  else if (mth==1) return 28;
  else return 31;
}
  
function bisiesto(yr)
{ if (((yr%4==0) && (yr%100!=0)) || (yr%400==0)) return true;
  else return false;
}
  
function formatDDMMAAAA(f)
{ var d, m, a;
  var txt = "";
  d = f.getDate();
  if (d<10) txt += "0" + d + "/";
  else txt += d + "/";
  m = f.getMonth() + 1;
  if (m<10) txt += "0" + m + "/";
  else txt += m + "/";
  a = f.getFullYear();
  txt += a;
  return txt;
}
 
function loadDias(m,a)
{ var firstDay = new Date((m+1)+"/01/"+a);
  firstDayW = firstDay.getDay();
  lastDay = numeroDeDias(m,a);
  var di = 0;
  var dia_txt;
  var txt = "<TABLE bgcolor=White cellspacing=1px border=0>";
  txt += "<TR>";
  txt += "<TD width=16 align=center class=dayWeek>S</TD>";
  txt += "<TD width=16 align=center class=dayWeek>M</TD>";
  txt += "<TD width=16 align=center class=dayWeek>T</TD>";
  txt += "<TD width=16 align=center class=dayWeek>W</TD>";
  txt += "<TD width=16 align=center class=dayWeek>T</TD>";
  txt += "<TD width=16 align=center class=dayWeek>F</TD>";
  txt += "<TD width=16 align=center class=dayWeek>S</TD>";
  txt += "</TR>";
       
  txt += "<TR>";
  for (i=0; i<7; i++)
  { if (i<firstDayW)
    { txt += "<TD height=18 align=center class=texto>&nbsp;</TD>"
    }
    else
    { di++;
      if (di<10) dia_txt = "0" + di;
      else dia_txt = di;
      if (dia==di && mes==m && anio==a) txt += "<TD height=18 align=center><A name='dia" + di + "' href='JavaScript: opener.selectFecha(" + dia_txt + "," + (m+1) + "," + a + ");'><P>" + dia_txt + "</P></A></TD>";
      else txt += "<TD height=18 align=center><A name='dia" + di + "' href='JavaScript: opener.selectFecha(" + dia_txt + "," + (m+1) + "," + a + ");'>" + dia_txt + "</A></TD>";
    }
  }
  txt += "</TR>";
    
  while (di<lastDay)
  { txt += "<TR>";
    for (i=0; i<7; i++)
    { di++;
      if (di<=lastDay)
      { if (di<10) dia_txt = "0" + di;
        else dia_txt = di;
        if (dia==di && mes==m && anio==a) txt += "<TD height=18 align=center><A name='dia" + di + "' href='JavaScript: opener.selectFecha(" + dia_txt + "," + (m+1) + "," + a + ");'><P>" + dia_txt + "</P></A></TD>";
        else txt += "<TD height=18 align=center><A name='dia" + di + "' href='JavaScript: opener.selectFecha(" + dia_txt + "," + (m+1) + "," + a + ");'>" + dia_txt + "</A></TD>";
      }
      else txt += "<TD height=18 align=center class=texto>&nbsp;</TD>";
    }      
    txt += "</TR>";
  }
  txt += "</TABLE>";   
  
  if (esNN)
  { with(winDP.document.divDias.document)
    { open();
      write(txt);
      close();
    }
  }
  else winDP.divDias.innerHTML = txt;
}

function anterior(cbM,cbY)
{ if (cbM.selectedIndex==0 && cbY.selectedIndex==(cbY.options.length-1))
  { alert("El mes y el aņo no pueden ser anteriores a los seleccionados.");
    winDP.focus();
  }
  else
  { if (cbM.selectedIndex==0)
    { cbM.selectedIndex=11;
      cbY.selectedIndex++;
    }
    else cbM.selectedIndex--;
    loadDias(cbM.selectedIndex,cbY.options[cbY.selectedIndex].value);
  }
}

function siguiente(cbM,cbY)
{ if (cbM.selectedIndex==11 && cbY.selectedIndex==0)
  { alert("El mes y el aņo no pueden ser posteriores a los seleccionados.");
    winDP.focus();
  }
  else
  { if (cbM.selectedIndex==11)
    { cbM.selectedIndex=0;
      cbY.selectedIndex--;
    }
    else cbM.selectedIndex++;
    loadDias(cbM.selectedIndex,cbY.options[cbY.selectedIndex].value);
  }
}
  
function selectFecha(d,m,a)
{ var nuevaFecha = "";
//  if (m<10) nuevaFecha += "0" + m + "/"; else nuevaFecha += m + "/";
//  if (d<10) nuevaFecha += "0" + d + "/"; else nuevaFecha += d + "/"; 
//  nuevaFecha += a;
  if (d<10) nuevaFecha += "0" + d + "/"; else nuevaFecha += d + "/"; 
  if (m<10) nuevaFecha += "0" + m + "/"; else nuevaFecha += m + "/";
  nuevaFecha += a;
  ctrFec.value = nuevaFecha;
  winDP.close();
}

function checkDateTxt (fecha)
{ var nuevaFecha = new String(fecha);
  var dia, anio;
  var diai, mesi, anioi;
  nuevaFecha  = trimWord(nuevaFecha);
  if (nuevaFecha=="") return false;
  dia = nuevaFecha.substring (0, nuevaFecha.indexOf('/'));
  if (!justNumber(dia)) return false;
  diai = parseFloat(dia);
  if (isNaN(diai)) return false;
  if (diai>31 || diai<1) return false;
  nuevaFecha = nuevaFecha.substring(nuevaFecha.indexOf('/')+1, nuevaFecha.length);
  mes = nuevaFecha.substring (0, nuevaFecha.indexOf('/')); 
  if (!justNumber(mes)) return false;
  mesi = parseFloat(mes);
  if (isNaN(mesi)) return false; 
  if (mesi>12 || mesi<1) return false;
  nuevaFecha = nuevaFecha.substring(nuevaFecha.indexOf('/')+1, nuevaFecha.length);
  anio = nuevaFecha;
  if (anio.length!=4) return false;
  if (!justNumber(anio)) return false;
  anioi = parseFloat(anio);
  if (isNaN(anioi)) return false;
  if (anioi>9999 || anioi<0) return false;
  switch (mesi)
  { case 1 :
    case 3 :
    case 5 :
    case 7 :
    case 8 :
    case 10 :
    case 12 :
      if (diai<1 || diai>31) return false;
      break;
    case 2 :
      if (((anioi%4==0) && (anioi%100!=0)) || (anioi%400==0))
      { if (diai<1 || diai>29) return false; }
      else
      { if (diai<1 || diai>28) return false; }
      break;
    case 4 : 
    case 6 :
    case 9 :
    case 11 :
      if (diai<1 || diai>30) return false;
      break;
  }
  return true;                 
}

function justNumber(numero) 
{ var nuevoNumero = new String(numero);
  for (i=0; i<nuevoNumero.length; i++)
    if (isNaN(parseInt(nuevoNumero.substring(i, i+1)))) return false;
  return true;
}

function trimWord(palabra)
{ var nuevaPalabra = new String(palabra);
  var ant, pos;
  while(nuevaPalabra.indexOf(' ')!=-1)
  { ant = nuevaPalabra.substring(0, nuevaPalabra.indexOf(' '));
    pos = nuevaPalabra.substring(nuevaPalabra.indexOf(' ')+1, nuevaPalabra.length);
    nuevaPalabra =  new String (ant + pos);
  }
  return (nuevaPalabra);
}
