/**
 * VENTANA DE SELECCION DE FECHA
 * SIN DEPURAR Y SOLO FUNCIONA EN IE O NE VIEJO
 * Dic 12 - 2004 Depurados 2 errores ...
 * 		- Movimiento Limitado a la fecha actual y adelante
 			Se agrego un parametro, numero de años atras
			( El numero de elementos (meses) visibles esta dado por 
			la variable 'monthamount', por defecto 120 )
 * 		- Movimiento erroneo al llegar al ultimo elemento
 			Depuracion y cambio de codigo
 * 		- Codigo inecesario al graficar elementos
 			Depurado
 * El codigo se puede hacer legible para culaquier navegador .....
 * Pendiente de implementacion .... ( es que se demora mucho )
**/

/**
 * DEFINICION DE FIESTAS .... ( Dias no habiles o no seleccionables ) 
 * DiaMesAño =>     31012004, 01022004, .....
**/

var hollidays = "";
/*
hollidays += "02062003,24062003,26062003,06062003,07062003,08062003,09062003,10062003,02072003,02082003,02092003,02102003,02112003,03122003,02012003,02022003,02032003,02042003,02052003,";
hollidays += "02062004,24062004,26062004,06062004,07062004,08062004,09062004,10062004,02072004,02082004,02092004,02102004,02112004,04122004,02012004,02022004,02032004,02042004,02052004,";
hollidays += "02062005,24062005,26062005,06062005,07062005,08062005,09062005,10062005,02072005,02082005,02092005,02102005,02112005,02122005,02012005,02022005,02032005,02042005,02052005,";
hollidays += "02062006,24062006,26062006,06062006,07062006,08062006,09062006,10062006,02072006,02082006,02092006,02102006,02112006,02122006,02012006,02022006,02032006,02042006,02052006,";
*/

/* PopUp Calendar v2.1
© PCI, Inc.,2000 • Freeware
webmaster@personal-connections.com
+1 (925) 955 1624
Permission granted  for unlimited use so far
as the copyright notice above remains intact. */

//From http://www.eksperten.dk/spm/227160?Esession=fb0b4da0604d8883924516c136d74175
//Edited by Jaap van Boxtel (japievb@hotmail.com); script used on www.huisjesfrankrijk.nl

/* Settings. Please read readme.html file for instructions*/

/* PopUp Calendar v2.1
© PCI, Inc.,2000 • Freeware
webmaster@personal-connections.com
+1 (925) 955 1624
Permission granted  for unlimited use so far
as the copyright notice above remains intact. */

//From http://www.eksperten.dk/spm/227160?Esession=fb0b4da0604d8883924516c136d74175
//Edited by Jaap van Boxtel (japievb@hotmail.com); script used on www.huisjesfrankrijk.nl

/* Settings. Please read readme.html file for instructions*/
var ppcDF = "Y-m-d";
var ppcYB = 0;
var ppcMN = new Array("01","02","03","04","05","06","07","08","09","10","11","12");
var ppcWN = new Array("","","","","","","");
var ppcER = new Array(4);
ppcER[0] = "Required DHTML functions are not supported in this browser.";
ppcER[1] = "Target form field is not assigned or not accessible.";
ppcER[2] = "Sorry, the chosen date is not acceptable. Please read instructions on the page.";
ppcER[3] = "Unknown error occured while executing this script.";
var ppcUC = false;
var ppcUX = 4;
var ppcUY = 4;
var varcolor = "tabla over_blue"  // background color for the not selecteble data (in data.js)
						// NELSON: paila ahora es el estilo que usa
var varblock = "Y"  //block holidays (in data.js) or not (Y/N)
var varavailable = ""
var monthamount = "120" //amount of month in the selector

/* Do not edit below this line unless you are sure what are you doing! */

var ppcIE=(navigator.appName == "Microsoft Internet Explorer");
var ppcNN=((navigator.appName == "Netscape")&&(document.layers));
var ppcTT="<table width=\"200\" cellspacing=\"1\" cellpadding=\"2\" border=\"0\" class=\"tabla\">\n";
var ppcCD=ppcTT;
var ppcFT="<div class=\"textos\">";
var ppcFC=true;
var ppcTI=false;
var ppcSV=null;
var ppcRL=null;
var ppcXC=null;
var ppcYC=null;
var ppcML=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var ppcNow=new Date();var ppcPtr=new Date();
var ppcWE = ppcWN

var x; 
var hollidayArr  = new Array();
var blockHollidays = true;

if (ppcNN) {
 window.captureEvents(Event.RESIZE);
 window.onresize = restoreLayers;
 document.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);
 document.onmousedown = recordXY;
 document.onmouseup = confirmXY;}

function restoreLayers(e) {
 if (ppcNN) {
  with (window.document) {
   open("text/html");
   write("<html><head><title>Restoring the layer structure...</title></head>");
   write("<body bgcolor=\"#FFFFFF\" onLoad=\"history.go(-1)\">");
   write("</body></html>");
   close();}}}

function recordXY(e) {
 if (ppcNN) {
  ppcXC = e.x;
  ppcYC = e.y;
  document.routeEvent(e);}}

function confirmXY(e) {
 if (ppcNN) {
  ppcXC = (ppcXC == e.x) ? e.x : null;
  ppcYC = (ppcYC == e.y) ? e.y : null;
  document.routeEvent(e);}}

function getCalendarFor(target,bck) {
		//	"bck" NUMERO DE AÑOS ATRAS
	if ( target.toString ( ) != "[object]")
		target = document.getElementById ( target );
	
	ppcSV = target;

	ppcRL = null;
	ppcYB = ( bck ) ? Math.abs ( bck ) : 0;
	if ( ppcFC )	{
		setCalendar ( );
		//ppcFC = false;	//  NELSON LO BORRE PA'QUE SIEMPRE LO HAGA
	}

 if ((ppcSV != null)&&(ppcSV)) {
  if (ppcIE) {
   var obj = document.all['PopUpCalendar'];
   obj.style.left = document.body.scrollLeft+event.clientX;
   obj.style.top  = document.body.scrollTop+event.clientY;
   obj.style.visibility = "visible";}
  else if (ppcNN) {
   var obj = document.layers['PopUpCalendar'];
   obj.left = ppcXC
   obj.top  = ppcYC
   obj.visibility = "show";}
  else {showError(ppcER[0]);}}
 else {showError(ppcER[1]);}}

function switchMonth(param) {
 var tmp = param.split("|");
 setCalendar(tmp[0],tmp[1]);}

function moveMonth(dir) {
 var obj = null;
 var limit = false;
 var tmp,dptrYear,dptrMonth;
 if (ppcIE) {obj = document.ppcMonthList.sItem;}
 else if (ppcNN) {obj = document.layers['PopUpCalendar'].document.layers['monthSelector'].document.ppcMonthList.sItem;}
 else {showError(ppcER[0]);}
 if (obj != null) {
  if ((dir.toLowerCase() == "back")&&(obj.selectedIndex > 0)) {obj.selectedIndex--;}
  else if ((dir.toLowerCase() == "forward")&&(obj.selectedIndex < obj.length - 1 )) {obj.selectedIndex++;}
  else {limit = true;}}
 if (!limit) {
  tmp = obj.options[obj.selectedIndex].value.split("|");
  dptrYear  = tmp[0];
  dptrMonth = tmp[1];
  setCalendar(dptrYear,dptrMonth);}
 else {
  if (ppcIE) {
   obj.style.backgroundColor = "#5F7BE0";
   window.setTimeout("document.ppcMonthList.sItem.style.backgroundColor = '#FFFFFF'",50);}}}

function selectDate(param) {
 var arr   = param.split("|");
 var year  = arr[0];
 var month = arr[1];
 var date  = arr[2];
 var ptr = parseInt(date);
 ppcPtr.setDate(ptr);
 if ((ppcSV != null)&&(ppcSV)) {
  if (validDate(date)) {ppcSV.value = dateFormat(year,month,date);hideCalendar();}
  else {showError(ppcER[2]);if (ppcTI) {clearTimeout(ppcTI);ppcTI = false;}}}
 else {
  showError(ppcER[1]);
  hideCalendar();}}

function setCalendar(year,month) {
 if (year  == null) {year = getFullYear(ppcNow);}
 if (month == null) {month = ppcNow.getMonth();setSelectList(year - ppcYB,month);}
 if (month == 1) {ppcML[1]  = (isLeap(year)) ? 29 : 28;}
 ppcPtr.setYear(year);
 ppcPtr.setMonth(month);
 ppcPtr.setDate(1);
 updateContent ( );}

function updateContent() {
 generateContent();
 if (ppcIE) {document.all['monthDays'].innerHTML = ppcCD;}
 else if (ppcNN) {
  with (document.layers['PopUpCalendar'].document.layers['monthDays'].document) {
   open("text/html");
   write("<html>\n<head>\n<title>DynDoc</title>\n</head>\n<body bgcolor=\"#FFFFFF\">\n");
   write(ppcCD);
   write("</body>\n</html>");
   close();}}
 else {showError(ppcER[0]);}
 ppcCD = ppcTT;}

function generateContent() {
 var year  = getFullYear(ppcPtr);
 var month = ppcPtr.getMonth();
 var date  = 1;
 var day   = ppcPtr.getDay();
 
if (day == 0) {day = 6}
else (day = day-1);

 var len   = ppcML[month];
 var bgr,cnt,tmp = "";
 var j,i = 0;
 for (j = 0; j < 7; ++j) {
  if (date > len) {break;}
  for (i = 0; i < 7; ++i) {
  bgr = (holliday(date,month,year)) ? varcolor : "tabla out";
   if (((j == 0)&&(i < day))||(date > len)) {tmp  += makeCell(bgr,year,month,0);}
   else {tmp  += makeCell(bgr,year,month,date);++date;}}
  ppcCD += "<tr align=\"center\">\n" + tmp + "</tr>\n";tmp = "";}
 ppcCD += "</table>\n";}

function makeCell(bgr,year,month,date) {
/**
 * NELSON: Moldifique esta funcion para aplicar los estilos  ....
 * El parametro bgr que era el color en el evento over ( #FFFFFF ), ahora es el estilo a usar
 * Meti los estilos tabla, out y over
**/
 var param = "\'"+year+"|"+month+"|"+date+"\'";
 var td1 = "<td width=\"20\" class='" + bgr + "' ";
 var td2 = (ppcIE) ? "</div></span></td>\n" : "</div></a></td>\n";
 var evt = "onMouseOver=\"this.className='tabla over'\" onMouseOut=\"this.className='" + bgr + "'\" onMouseUp=\"selectDate ( "+param+" )\" ";
 var ext = "<span Style=\"cursor: hand\">";
 var lck = "<span Style=\"cursor: default\">";
 var lnk = "<a href=\"javascript:selectDate("+param+")\" onMouseOver=\"window.status=\' \';return true;\">";
 var cellValue = (date != 0) ? date+"" : "";
 if ((ppcNow.getDate() == date)&&(ppcNow.getMonth() == month)&&(getFullYear(ppcNow) == year)) {
  cellValue = "<b>"+cellValue+"</b>";}
 var cellCode = "";
 if ((date == 0) || (bgr== varcolor && blockHollidays))
{
  if (ppcIE) {cellCode = td1+"Style=\"cursor: default\">"+lck+ppcFT+cellValue+td2;}
  else {cellCode = td1+">"+ppcFT+cellValue+td2;}}
 else {
  if (ppcIE) {cellCode = td1+evt+"Style=\"cursor: hand\">"+ext+ppcFT+cellValue+td2;}
  else {
   if (date < 10) {cellValue = "&nbsp;" + cellValue + "&nbsp;";}
   cellCode = td1+">"+lnk+ppcFT+cellValue+td2;}}
 return cellCode;}

function setSelectList(year,month) {
 var i = 0;
 var obj = null;
 if (ppcIE) {obj = document.ppcMonthList.sItem;}
 else if (ppcNN) {obj = document.layers['PopUpCalendar'].document.layers['monthSelector'].document.ppcMonthList.sItem;}
 else {/* NOP */}

  while (i < + monthamount) {
  obj.options[i].value = ( year ) + "|" + month;
  obj.options[i].text  = ppcMN[month] + " - " + ( year );
  if ( year == getFullYear(ppcPtr) && month == ppcPtr.getMonth ( ) )
	  obj.options[i].selected = true;
  
  i++;
  month++;
  if (month == 12) {year++;month = 0;}}}

function hideCalendar() {
 if (ppcIE) {document.all['PopUpCalendar'].style.visibility = "hidden";}
 else if (ppcNN) {document.layers['PopUpCalendar'].visibility = "hide";window.status = " ";}
 else {/* NOP */}
 ppcTI = false;
 setCalendar();
 ppcSV = null;
 if (ppcIE) {var obj = document.ppcMonthList.sItem;}
 else if (ppcNN) {var obj = document.layers['PopUpCalendar'].document.layers['monthSelector'].document.ppcMonthList.sItem;}
 else {/* NOP */}
 obj.selectedIndex = 0;}

function showError(message) {
 window.alert("[ PopUp Calendar ]\n\n" + message);}

function isLeap(year) {
 if ((year%400==0)||((year%4==0)&&(year%100!=0))) {return true;}
 else {return false;}}

function getFullYear(obj) {
 if (ppcNN) {return obj.getYear() + 1900;}
 else {return obj.getYear();}}

function validDate(date) {
 var reply = true;
 if (ppcRL == null) {/* NOP */}
 else {
  var arr = ppcRL.split(":");
  var mode = arr[0];
  var arg  = arr[1];
  var key  = arr[2].charAt(0).toLowerCase();
  if (key != "d") {
   var day = ppcPtr.getDay();
   var orn = isEvenOrOdd(date);
   reply = (mode == "[^]") ? !((day == arg)&&((orn == key)||(key == "a"))) : ((day == arg)&&((orn == key)||(key == "a")));}
  else {reply = (mode == "[^]") ? (date != arg) : (date == arg);}}
 return reply;}

function isEvenOrOdd(date) {
 if (date - 21 > 0) {return "e";}
 else if (date - 14 > 0) {return "o";}
 else if (date - 7 > 0) {return "e";}
 else {return "o";}}

function dateFormat(year,month,date) {
 if (ppcDF == null) {ppcDF = "m/d/Y";}
 var day = ppcPtr.getDay();
 var crt = "";
 var str = "";
 var chars = ppcDF.length;
 for (var i = 0; i < chars; ++i) {
  crt = ppcDF.charAt(i);
  switch (crt) {
   case "M": str += ppcMN[month]; break;
   case "m": str += (month<9) ? ("0"+(++month)) : ++month; break;
   case "Y": str += year; break;
   case "y": str += year.substring(2); break;
   case "d": str += ( ( ppcDF.indexOf ( "m" ) != -1 ) && ( date < 10 ) ) ? ( "0" + date ) : date; break;
   case "W": str += ppcWN[day]; break;
    default: str += crt;}}
 return unescape(str);}

function setMonths(parm) { 
 ppcMN = parm;
}

function setHollidays(arr) { 
 hollidayArr  = arr.split(",");
 for ( x = 0; hollidayArr[x]!=""; ++x){}
} 

function setBlockHolliday(parm) {
if (parm=="N")
   {blockHollidays = false;}
else
   {blockHollidays = true;}
}

function holliday(date,month,year){  
 month=month+1;
 var dateStr= "";
 if (date<10) {dateStr += "0"+date;}
 else {dateStr += date;}
 if (month<10) {dateStr += "0"+month;}
 else {dateStr += month;}
 dateStr += year;
 var holliday=false;
 for (var i=0; i < x ; ++i){ 
   if (dateStr==hollidayArr[i]) {holliday=true;}}
 return holliday;}

/**
 * SET DE DIAS FESTIVOS
**/
setBlockHolliday ( varblock );
setHollidays ( hollidays );
