/*
Dynamic Calendar II (By Jason Moon at http://www.jasonmoon.net)
Permission granted to Dynamicdrive.com to include script in archive
For this and 100's more DHTML scripts, visit http://dynamicdrive.com
*/

var ns6=document.getElementById&&!document.all
var ie4=document.all


var Selected_Month;
var Selected_Year;
var Current_Date = new Date();
var Current_Month = Current_Date.getMonth();

var Days_in_Month = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var Month_Label = new Array('Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre');

var Current_Year = Current_Date.getYear();
if (Current_Year < 1000)
Current_Year+=1900


var Today = Current_Date.getDate();

function Header(Year, Month) {

   if (Month == 1) {
   Days_in_Month[1] = ((Year % 400 == 0) || ((Year % 4 == 0) && (Year % 100 !=0))) ? 29 : 28;
   }
   var Header_String = Month_Label[Month] + ' ' + Year;
   return Header_String;
}

function Make_Calendar(Year, Month) {
   var First_Date = new Date(Year, Month, 1);
   var Heading = Header(Year, Month);
   var First_Day = First_Date.getDay() + 1;
   if (((Days_in_Month[Month] == 31) && (First_Day >= 6)) ||
       ((Days_in_Month[Month] == 30) && (First_Day == 7))) {
      var Rows = 6;
   }
   else if ((Days_in_Month[Month] == 28) && (First_Day == 1)) {
      var Rows = 4;
   }
   else {
      var Rows = 5;
   }

   var HTML_String = '<table border=0 cellspacing=1 cellpadding=0 frame="box" class="ColorFondoCal" align="center">';
   HTML_String += '<tr><td colspan=7 align="center" width="100%"><font class="LabelMes">' + Heading + '</font></td></tr>';
   HTML_String += '<tr>';
   HTML_String += '	<td colspan=7 align="center" width="100%">';
   HTML_String += '		<table align="center" border=0 cellspacing=1 cellpadding=1 bgcolor="#ffffff" width="100%">';
   HTML_String += '		<tr class="ColorFondoDia">';
   HTML_String += '			<td align="center"><font class="LabelDia">Dom</font></td>';
   HTML_String += '			<td align="center"><font class="LabelDia">Lun</font></td>';
   HTML_String += '			<td align="center"><font class="LabelDia">Mar</font></td>';
   HTML_String += '			<td align="center"><font class="LabelDia">Mie</font></td>';
   HTML_String += '			<td align="center"><font class="LabelDia">Jue</font></td>';
   HTML_String += '			<td align="center"><font class="LabelDia">Vie</font></td>';
   HTML_String += '			<td align="center"><font class="LabelDia">Sab</font></td>';
   HTML_String += '		</tr>';
      
   var Day_Counter = 1;
   var Loop_Counter = 1;
   for (var j = 1; j <= Rows; j++) {
      HTML_String += '			<tr align="center" valign="top" bgcolor="#ffffff">';
      for (var i = 1; i < 8; i++) {
         if ((Loop_Counter >= First_Day) && (Day_Counter <= Days_in_Month[Month])) {
            if ((Day_Counter == Today) && (Year == Current_Year) && (Month == Current_Month)) {
               HTML_String += '		<td bgcolor="#ffffff" align="center"><a href=javascript:selday(' + Year + ',' + Month + ',' + Day_Counter + ') style=text-decoration:none;color:red;><font class="LabelDiaFeriado">' + Day_Counter + '</font></a></td>';
            }
            else {
               HTML_String += '		<td bgcolor="#ffffff" align="center"><a href=javascript:selday(' + Year + ',' + Month + ',' + Day_Counter + ') style=text-decoration:none;color:#000000><font class="LabelDiaNumero">' + Day_Counter + '</font></a></td>';
            }
            Day_Counter++;    
         }
         else {
            HTML_String += '		<td bgcolor="#ffffff" align="center"></td>';
         }
         Loop_Counter++;
      }
      HTML_String += '			</tr>';
   }
   HTML_String += '			</table>';
   HTML_String += '		<td>';
   HTML_String += '</tr>';
   HTML_String += '</table>';
   cross_el=ns6? document.getElementById("Calendar") : document.all.Calendar
   cross_el.innerHTML = HTML_String;
}


function Check_Nums() {
   if ((event.keyCode < 48) || (event.keyCode > 57)) {
      return false;
   }
}

function On_Year() {
   var Year = document.forms[0].year.value;
   if (Year.length == 4) {
      Selected_Month = document.forms[0].month.selectedIndex;
      Selected_Year = Year;
      Make_Calendar(Selected_Year, Selected_Month);
   }
}

function On_Month() {
   var Year = document.forms[0].year.value;
   if (Year.length == 4) {
      Selected_Month = document.forms[0].month.selectedIndex;
      Selected_Year = Year;
      Make_Calendar(Selected_Year, Selected_Month);
   }
   else {
      alert('Please enter a valid year.');
      document.forms[0].year.focus();
   }
}

function Defaults(sFechaActual) {
   if (!ie4&&!ns6)
   return
   var Mid_Screen = Math.round(document.body.clientWidth / 2);
   //document.forms[0].month.selectedIndex = Current_Month;
   document.forms[0].year.value = document.forms[0].glb_date_sel.value.substr(0,4);
   Selected_Month = Current_Month;
   Selected_Year = Current_Year;
   //a = Make_Calendar(Current_Year, Current_Month);
   a = Make_Calendar(document.forms[0].glb_date_sel.value.substr(0,4), document.forms[0].month.selectedIndex);
   document.forms[0].glb_date_sel.value = sFechaActual
   //parent.frames[1].location = "agenda.asp?glb_date_sel=" + sFechaActual + '&glb_cod_nodo=' + document.forms[0].glb_cod_nodo.value;
   //document.forms[0].location = "agenda.asp?glb_date_sel=" + sFechaActual + '&glb_cod_nodo=' + document.forms[0].glb_cod_nodo.value;
}

function Skip(Direction) {
   if (Direction == '+') {
      if (Selected_Month == 11) {
         Selected_Month = 0;
         Selected_Year++;
      }
      else {
         Selected_Month++;
      }
   }
   else {
      if (Selected_Month == 0) {
         Selected_Month = 11;
         Selected_Year--;
      }
      else {
         Selected_Month--;
      }
   }
   Make_Calendar(Selected_Year, Selected_Month);
   document.forms[0].month.selectedIndex = Selected_Month;
   document.forms[0].year.value = Selected_Year;
}

function selday(nano,nmes,ndia){
	var ndate = new Date(nano,nmes,ndia);
	var pndia = 0
	var pudia = 0
	var pdsem = ""
	var udsem = ""
	
	if ((parseInt(ndate.getDay()) == 0)||(parseInt(ndate.getDay()) == 6)){
		if (parseInt(ndate.getDay()) == 0){
			pndia = ndate.getDate()
			undia = pndia + 6
		}
		if (parseInt(ndate.getDay()) == 6){
			undia = ndate.getDate()
			pndia = undia - 6
		}
	}else{
		pndia = ndate.getDate()
		pndia = pndia - parseInt(ndate.getDay())
		undia = pndia + 6		
	}
		
	pdsem = new Date(nano,nmes,pndia);
	udsem = new Date(nano,nmes,undia);
	
	npAno = pdsem.getYear() 
	npMes = pdsem.getMonth() + 1
	npDia = pdsem.getDate()
	
	if (parseInt(npMes) <= 9){
		strpMes = "0"+npMes
	}else{
		strpMes = npMes.toString()
	}
	
	if (parseInt(npDia) <= 9){
		strpDia = "0"+npDia
	}else{
		strpDia = npDia.toString()
	}
	
	strInicio = npAno + strpMes + strpDia

	nuAno = udsem.getYear() 
	nuMes = udsem.getMonth() + 1
	nuDia = udsem.getDate()
	
	if (parseInt(nuMes) <= 9){
		struMes = "0"+nuMes
	}else{
		struMes = nuMes.toString()
	}
	
	if (parseInt(nuDia) <= 9){
		struDia = "0"+nuDia
	}else{
		struDia = nuDia.toString()
	}
	
	strFinal = nuAno + struMes + struDia
	
	nmes = nmes + 1
	if (parseInt(nmes) <= 9){
		nmes = "0"+nmes
	}else{
		nmes = nmes.toString()
	}
	
	if (parseInt(ndia) <= 9){
		ndia = "0"+ndia
	}else{
		ndia = ndia.toString()
	}
	
	strSel = nano + nmes + ndia
	
	//parent.frame_agenda.location = "agenda.asp?glb_date_ini=" + strInicio + "&glb_date_fin=" + strFinal + "&glb_date_sel=" + strSel + '&glb_cod_nodo=' + document.forms[0].glb_cod_nodo.value;
	//document.forms[0].location = "agenda.asp?glb_date_ini=" + strInicio + "&glb_date_fin=" + strFinal + "&glb_date_sel=" + strSel + '&glb_cod_nodo=' + document.forms[0].glb_cod_nodo.value;
	document.forms[0].glb_date_sel.value = strSel;
	//document.forms[0].glb_date_ini.value = strInicio;
	//document.forms[0].glb_date_fin.value = strFinal;
	var cLink = '' + top.location;
	if (cLink.substr(cLink.length - 1) == "/") {
		document.forms[0].action = 'default.aspx';
	}
	document.forms[0].submit();
}

function WriteMonth() {
	if (ie4||ns6){
		var strFecMes = eval(document.forms[0].glb_date_sel.value.substr(4,2))-1;
	for (j=0;j<Month_Label.length;j++) {
		if(j == strFecMes) {
			document.writeln('<option value="' + j + '" selected>' + Month_Label[j] + '</option>');
		}
		else {
	      		document.writeln('<option value="' + j + '">' + Month_Label[j] + '</option>');
		}
	}
	}
}