﻿var mes;
var anio;
var dia;
var controlact;
var antiguoly;
function cargarcalendar(control,origen,obj){
//document.body.onclick=hidecal;
controlact=control;
var fecha=document.getElementById(control).value;
fecha=fecha.split("/");
if (fecha[2]<60 && fecha[2]>=0){
    fecha[2]="20" + fecha[2];
}
if (origen==1){
var loading = document.getElementById(antiguoly);
if (loading){
WCH.Discard(antiguoly)
document.body.removeChild(loading);
}
}
if (fecha!="" && origen==1){
var date = new Date(fecha[2],Number(fecha[1]-1),fecha[0]);
mes=date.getMonth();
dia=date.getDay();
anio=date.getFullYear();
}
else if (origen==1){
var fechaactual=new Date();
mes=fechaactual.getMonth();
anio=fechaactual.getFullYear();
dia=fechaactual.getDate();
fecha=dia + "/" + mes + "/" + anio;
fecha=fecha.split("/");
}
var ar = new Array(12);
  ar[0] = "Enero"
  ar[1] = "Febrero"
  ar[2] = "Marzo"
  ar[3] = "Abril"
  ar[4] = "Mayo"
  ar[5] = "Junio"
  ar[6] = "Julio"
  ar[7] = "Agosto"
  ar[8] = "Septiembre"
  ar[9] = "Octubre"
  ar[10] = "Noviembre"
  ar[11] = "Diciembre"
var date = new Date(anio,mes,1);
var diasemana=date.getDay();
if (diasemana==0){
diasemana=6
}else{
diasemana--;
}
var diasmes=getDays(mes,anio);
//alert(diasemana);
var semanas=(diasmes+diasemana)/7;
/*if (diasmes==28 && diasemana>1){
var semanas=(diasmes/7)+1;
}else{
var semanas=diasmes/7;
}*/

semanas=Math.ceil(semanas);

var mnitembefore=document.getElementById(controlact + "cal");
if (mnitembefore && origen==1 ){
document.body.removeChild(mnitembefore);
}

var vb =document.createElement('table');
vb.cellSpacing=0;
vb.cellPadding=1;
vb.id="nuevocalendario";
vb.className="tablacalendario";
var griditem = document.createElement('tr');
griditem.className="cabeceracalendario";


var vbo=document.createElement('td');
vbo.height="20";
var lblitem=document.createElement('a');
lblitem.setAttribute('class','b10');

vbo.setAttribute('align','left');
lblitem.innerHTML="&lt;";
lblitem.setAttribute('onclick',"antmes('" + control + "')");
lblitem.href="javascript:;";
vbo.appendChild(lblitem);
griditem.appendChild(vbo);

var vbo=document.createElement('td');
var lblitem=document.createElement('span');

vbo.setAttribute('colspan','5');
vbo.setAttribute('align','center');
lblitem.innerHTML=ar[mes] + " " + anio;

vbo.appendChild(lblitem);
griditem.appendChild(vbo);

var vbo=document.createElement('td');
var lblitem=document.createElement('a');
lblitem.className="b10";
vbo.setAttribute('align','right');
lblitem.innerHTML="&gt;";
//lblitem.setAttribute('style',"text-decoration:none;color:#000000");
lblitem.setAttribute('onclick',"sigmes('" + control + "')");
lblitem.href="javascript:;";
vbo.appendChild(lblitem);
griditem.appendChild(vbo);
vb.appendChild(griditem)

var griditem = document.createElement('tr');

var diasdesc = new Array(7);
diasdesc[0]="L";
diasdesc[1]="M";
diasdesc[2]="X";
diasdesc[3]="J";
diasdesc[4]="V";
diasdesc[5]="S";
diasdesc[6]="D";
for (var z = 0; z <7; z++) {
var vbo=document.createElement('td');
vbo.height="20";
var lblitem=document.createElement('span');
lblitem.setAttribute('class','n10');

vbo.setAttribute('align','center');
lblitem.innerHTML=diasdesc[z];
vbo.appendChild(lblitem);
griditem.appendChild(vbo);
}
vb.appendChild(griditem)

var griditem = document.createElement('tr');
var vbo=document.createElement('td');
vbo.height="1";
vbo.colSpan=7;
vbo.style.backgroundColor="#cccccc";
griditem.appendChild(vbo);
vb.appendChild(griditem)

var dia=0;

for (var z = 0; z <semanas; z++) {
if (z==0){
var griditem = document.createElement('tr');


for (var i = 0; i <7; i++) {
dia++;
if (diasemana>i){
var vbo=document.createElement('td');
vbo.width="20";
vbo.height="20";
griditem.appendChild(vbo);
dia=0;
}else{
var vbo=document.createElement('td');
var lblitem=document.createElement('a');
//rowitem.appendChild(lblitem);
//rowsitem.appendChild(rowitem);

vbo.width="20";
vbo.height="20";
vbo.setAttribute('align','center');
if (fecha[0] + "/" + Number(fecha[1]) + "/" + fecha[2]==dia + "/" + Number(mes+1) + "/" + anio){
vbo.setAttribute('class','sel10');
lblitem.setAttribute('class','b10');
}
else{
lblitem.setAttribute('class','n10');
}
lblitem.href="javascript:;";
lblitem.setAttribute('onclick','setfecha(' + dia +')');
lblitem.innerHTML=dia;
vbo.appendChild(lblitem);
griditem.appendChild(vbo);
}
}
vb.appendChild(griditem)
//menuprincipal.appendChild(vb);
}
else{
var griditem = document.createElement('tr');

for (var i = 1; i <8; i++) {
dia++;
if (dia>diasmes){
var vbo=document.createElement('td');
griditem.appendChild(vbo);
vbo.width="20";
vbo.height="20";
}
else{
var vbo=document.createElement('td');
var lblitem=document.createElement('a');
//rowitem.appendChild(lblitem);
//rowsitem.appendChild(rowitem);

vbo.width="20";
vbo.height="20";
vbo.setAttribute('align','center');
if (fecha[0] + "/" + Number(fecha[1]) + "/" + fecha[2]==dia + "/" + Number(mes+1) + "/" + anio){
vbo.setAttribute('class','sel10');
lblitem.setAttribute('class','b10');
}
else{
lblitem.setAttribute('class','n10');
}
lblitem.href="javascript:;";
lblitem.setAttribute('onclick','setfecha(' + dia +')');
lblitem.innerHTML=dia;

vbo.appendChild(lblitem);
griditem.appendChild(vbo);
}
}
vb.appendChild(griditem)
//menuprincipal.appendChild(vb);
}

}
var griditem = document.createElement('tr');
var vbo=document.createElement('td');
vbo.height="1";
vbo.colSpan=7;
vbo.style.backgroundColor="#cccccc";
griditem.appendChild(vbo);
vb.appendChild(griditem)
var griditem = document.createElement('tr');
var vbo=document.createElement('td');
vbo.height="1";
vbo.colSpan=3;
var lblitem=document.createElement('a');
lblitem.innerHTML="Hoy";
lblitem.href="javascript:;";
lblitem.setAttribute('onclick','sethoy()');
lblitem.setAttribute('class','n10');
vbo.appendChild(lblitem);
vbo.setAttribute('align','center');
griditem.appendChild(vbo);
var vbo=document.createElement('td');
vbo.height="1";
vbo.colSpan=4;
var lblitem=document.createElement('a');
lblitem.innerHTML="Cancelar";
lblitem.href="javascript:;";
lblitem.setAttribute('onclick','hidecal()');
lblitem.setAttribute('class','n10');
vbo.appendChild(lblitem);
vbo.setAttribute('align','center');
griditem.appendChild(vbo);
vb.appendChild(griditem)
//menuprincipal.appendChild(vb);

if (obj){
var loading = document.createElement("div");
loading.id = controlact + "cal";
loading.style.position = "absolute";
loading.style.left = findPosX(document.getElementById(obj))  + "px";
loading.style.top = findPosY(document.getElementById(obj)) + "px";
loading.style.zIndex = "9999";
antiguoly=controlact + "cal";
//WCH.Apply('nuevocalendario')
}
else{
var loading = document.getElementById(controlact + "cal");
}

loading.innerHTML = "<table cellSpacing=0 cellPadding=1  class='tablacalendario'>" + vb.innerHTML + "</table>";

document.body.appendChild(loading);

WCH.Apply(controlact + "cal");
//findPosX
//menuprincipal.innerHTML="<table cellSpacing=0 cellPadding=1 id='nuevocalendario' class='tablacalendario'>" + vb.innerHTML + "</table>"

}

function getDays(month, year) {
  // create array to hold number of days in each month
  var ar = new Array(12);
  ar[0] = 31; // January
  if (Bisiesto(year)) {
  ar[1]=29;
  	}		 // solo para Febrero
else
{
  ar[1]=28;
  }
 // ar[1] = (year) ? 29 : 28; // February
  ar[2] = 31; // March
  ar[3] = 30; // April
  ar[4] = 31; // May
  ar[5] = 30; // June
  ar[6] = 31; // July
  ar[7] = 31; // August
  ar[8] = 30; // September
  ar[9] = 31; // October
  ar[10] = 30; // November
  ar[11] = 31; // December

  // return number of days in the specified month (parameter)
  return ar[month];
}
function sigmes(control){
//alert("kjhj");
mes=mes+1;
if (mes>11){
mes=0;
anio++;
}
cargarcalendar(control,0);
}
function antmes(control){
//alert("kjhj");
mes=mes-1;
if (mes==-1){
mes=11;
anio--;
}
cargarcalendar(control,0);
}
function Bisiesto(year) 
{
if ((year % 4 == 0) && (( year % 100 != 0) || (year % 400 ==0)))
  return true;
else
  return false;
}
function setfecha(dia){
WCH.Discard(controlact + "cal")
if(dia<10 & Number(mes+1)<10){
    document.getElementById(controlact).value="" + dia + "/0" + Number(mes+1) + "/" + anio;
}else {
    if (dia<10){
        document.getElementById(controlact).value="" + dia + "/" + Number(mes+1) + "/" + anio;
    }else{
        if (Number(mes+1)<10){
            document.getElementById(controlact).value=dia + "/0" + Number(mes+1) + "/" + anio;
        }else{
            document.getElementById(controlact).value=dia + "/" + Number(mes+1) + "/" + anio;         
        }
    }
} 
  
var loading = document.getElementById(controlact + "cal");
document.body.removeChild(loading);
}
function sethoy(){
WCH.Discard(controlact + "cal")
var fechaactual=new Date();
var mesactual=fechaactual.getMonth();
var anioactual=fechaactual.getFullYear();
var diaactual=fechaactual.getDate();
if(diaactual<10 & Number(mesactual+1)<10){
    document.getElementById(controlact).value="" + diaactual + "/0" + Number(mesactual+1) + "/" + anioactual;
}else {
    if (diaactual<10){
        document.getElementById(controlact).value="" + diaactual + "/" + Number(mesactual+1) + "/" + anioactual;
    }else{
        if (Number(mesactual+1)<10){
            document.getElementById(controlact).value=diaactual + "/0" + Number(mesactual+1) + "/" + anioactual;
        }else{
             document.getElementById(controlact).value=diaactual + "/" + Number(mesactual+1) + "/" + anioactual;         
        }
    }
}

var loading = document.getElementById(controlact + "cal");
document.body.removeChild(loading);
}
function hidecal(){
WCH.Discard(controlact + "cal")
var loading = document.getElementById(controlact + "cal");
if (loading){
document.body.removeChild(loading);
}

}

    function findPosX(obj)
    {
	    var curleft = -160;
	    if (obj.offsetParent)
	    {
		    while (obj.offsetParent)
		    {
			    curleft += obj.offsetLeft
			    obj = obj.offsetParent;
		    }
	    }
	    else if (obj.x)
		    curleft += obj.x;
	    return curleft;
    }

    function findPosY(obj)
    {
	    var curtop = -170;
	    var printstring = '';
	    if (obj.offsetParent)
	    {
		    while (obj.offsetParent)
		    {
    			
			    curtop += obj.offsetTop
			    obj = obj.offsetParent;
		    }
	    }
	    else if (obj.y)
		    curtop += obj.y;
	    window.status = printstring;
	    return curtop;
    }

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	WCH.js - Windowed Controls Hider v3.10
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	(c) Copyright 2003, Aleksandar Vacic, aleck@sezampro.yu, www.aplus.co.yu
	## This work is licensed under the Creative Commons Attribution-ShareAlike License.
	## To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/1.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Credits: Mike Foster for x functions (cross-browser.com)
	Credits: Tim Connor for short and sweet way of dealing with IE5.0 - dynamic creation of style rule (www.infosauce.com)
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Based on idea presented by Joe King. Works with IE5.0+/Win
	IE 5.5+: place iFrame below the layer to hide windowed controls
	IE 5.0 : hide/show all elements that have "WCHhider" class
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
var WCH_Constructor = function() {
	//	exit point for anything but IE5.0+/Win
	if ( !(document.all && document.getElementById && !window.opera && navigator.userAgent.toLowerCase().indexOf("mac") == -1) ) {
		this.Apply = function() {};
		this.Discard = function() {};
		return;
	}

	//	private properties
	var _bIE55 = false;
	var _bIE6 = false;
	var _oRule = null;
	var _bSetup = true;
	var _oSelf = this;

	//	public: hides windowed controls
	this.Apply = function(vLayer, vContainer, bResize) {
		if (_bSetup) _Setup();

		if ( _bIE55 && (oIframe = _Hider(vLayer, vContainer, bResize)) ) {
			oIframe.style.visibility = "visible";
		} else if(_oRule != null) {
			_oRule.style.visibility = "hidden";
		}

	};

	//	public: shows windowed controls
	this.Discard = function(vLayer, vContainer) {
		if ( _bIE55 && (oIframe = _Hider(vLayer, vContainer, false)) ) {
			oIframe.style.visibility = "hidden";
		} else if(_oRule != null) {
			_oRule.style.visibility = "visible";
		}
	};

	//	private: returns iFrame reference for IE5.5+
	function _Hider(vLayer, vContainer, bResize) {
		var oLayer = _GetObj(vLayer);
		var oContainer = ( (oTmp = _GetObj(vContainer)) ? oTmp : document.getElementsByTagName("body")[0] );
		if (!oLayer || !oContainer) return;
		//	is it there already?
		var oIframe = document.getElementById("WCHhider" + oLayer.id);
		
		//	if not, create it
		if ( !oIframe ) {
			//	IE 6 has this property, IE 5 not. IE 5.5(even SP2) crashes when filter is applied, hence the check
			var sFilter = (_bIE6) ? "filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0);" : "";
			//	get z-index of the object
			var zIndex = oLayer.style.zIndex;
			if ( zIndex == "" ) zIndex = oLayer.currentStyle.zIndex;
			zIndex = parseInt(zIndex);
			//	if no z-index, do nothing
			if ( isNaN(zIndex) ) return null;
			//	if z-index is below 2, do nothing (no room for Hider)
			if (zIndex < 2) return null;
			//	go one step below for Hider
			zIndex--;
			var sHiderID = "WCHhider" + oLayer.id;
			oContainer.insertAdjacentHTML("afterBegin", '<iframe class="WCHiframe" src="javascript:false;" id="' + sHiderID + '" scroll="no" frameborder="0" style="position:absolute;visibility:hidden;' + sFilter + 'border:0;top:0;left;0;width:0;height:0;background-color:#ccc;z-index:' + zIndex + ';"></iframe>');
			oIframe = document.getElementById(sHiderID);
			//	then do calculation
			_SetPos(oIframe, oLayer);
		} else if (bResize) {
			//	resize the iFrame if asked
			_SetPos(oIframe, oLayer);
		}
		return oIframe;
	};

	//	private: set size and position of the Hider
	function _SetPos(oIframe, oLayer) {
		//	fetch and set size
		oIframe.style.width = oLayer.offsetWidth + "px";
		oIframe.style.height = oLayer.offsetHeight + "px";
		//	move to specified position
		oIframe.style.left = oLayer.offsetLeft + "px";
		oIframe.style.top = oLayer.offsetTop + "px";
	};

	//	private: returns object reference
	function _GetObj(vObj) {
		var oObj = null;
		switch( typeof(vObj) ) {
			case "object":
				oObj = vObj;
				break;
			case "string":
				oObj = document.getElementById(vObj);
				break;
		}
		return oObj;
	};

	//	private: setup properties on first call to Apply
	function _Setup() {
		_bIE55 = (typeof(document.body.contentEditable) != "undefined");
		_bIE6 = (typeof(document.compatMode) != "undefined");

		if (!_bIE55) {
			if (document.styleSheets.length == 0)
				document.createStyleSheet();
			var oSheet = document.styleSheets[0];
			oSheet.addRule(".WCHhider", "visibility:visible");
			_oRule = oSheet.rules(oSheet.rules.length-1);
		}

		_bSetup = false;
	};
};
var WCH = new WCH_Constructor();


