var zz, zv, d, fTSR;
var gBF=false;
var g_MINY = 1601;
var g_MAXY = 4500;
var g_month = 0;
var g_day = 0;
var g_year = 0;
var g_yLow = 1990;
var g_eC=null;
var g_eCV="";

var rgMC = Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
d = new Date();
fTSR=0;
zv = d.getTime();
zz = "&zz="+zv;

function GetDowStart() {return 1;}

function GetInputDate(t) {
	var l = t.length;
	if(0 == l) {
		return false;
	}
	var cSp = '\0';
	var sSp1 = "";
	var sSp2 = "";
	for(var i=0; i<t.length; i++){
		var c=t.charAt(i);
		if(c==' ' || isdigit(c)) continue;
		else if(cSp == '\0' && (c == '/' || c == '-' || c == '.')) {
			cSp = c;
			sSp1 = t.substring(i+1,l);
		} else if(c==cSp) sSp2 = t.substring(i+1,l);
		else if(c != cSp) return false;
	}
	if(0 == sSp1.length) return false;
	var m=atoi(sSp1);
	var d=atoi(t);
	var y; 
	if(0 != sSp2.length) y = atoi(sSp2);
	else y = DefYr(m,d);
	if(y < 100){
		y = 1900+y;
		while(y < g_yLow) y = y+100;
	}
	if(y < g_MINY || y > g_MAXY || m < 1 || m > 12) return false;
	if(d < 1 || d > GetMonthCount(m,y)) return false;
	g_month = m;
	g_day = d;
	g_year = y;
	return true;
}

function DefYr(m,d){
	var dt = new Date();
	var yCur = (dt.getYear() < 1000) ? 1900+dt.getYear() : dt.getYear();
	if(m-1 < dt.getMonth() || (m-1 == dt.getMonth() && d < dt.getDate())) return 1+yCur;
	else return yCur;
}

function atoi(s){
	var t=0;
	for(var i=0; i<s.length; i++){
		var c=s.charAt(i);
		if(!isdigit(c)) return t;
		else t = t*10 + (c-'0');
	}
	return t;
}

function isdigit(c) {
	return(c >= '0' && c <= '9');
}

function GetMonthCount(m,y){
	var c = rgMC[m-1];
	if((2 == m) && IsLeapYear(y)) c++;
	return c;
}
function IsLeapYear(y){
	if(0 == y % 4 && ((y % 100 != 0) || (y % 400 == 0))) return true;
	else return false;
}

function ocultarCalendar(dF, oculto) {
	if (oculto) {
//			dF.style.display="none";
		dF.style.width="0";
		dF.style.height="0";
		document.onclick=dF.documentonclick;
		document.onkeydown=dF.documentkeydown;
		g_eC=null;
	}	else {
		dF.style.width="144px";
		dF.style.height="160px";
		dF.documentonclick=document.onclick;
		document.onclick=function(event) {clickBodyCalendario(event)}
		dF.documentkeydown=document.onkeydown;
		document.onkeydown=function (event) {keyDownBodyCalendario(event)};
	}

	dF.esVisible=!oculto;
}

function clickBodyCalendario(e) {
	var elmto;
	if (e) {
		elmto=e.target;
	} else {
		e=window.event;
		elmto=e.srcElement;
	}
	if (elmto.tagName!="IMG") {
		ocultarCalendar(document.getElementById("CalFrame"), true);
		return;
	}
//habría que comprobar si efectivamente es un calendario
}

function keyDownBodyCalendario(e) {
	var elmto;
	if (e) {
		elmto=e.target;
	} else {
		e=window.event;
		elmto=e.srcElement;
	}
	var tecla=e.keyCode;
	if (tecla==27) { //Esc
		ocultarCalendar(document.getElementById("CalFrame"), true);
		return;
	}
}

function procesarDate(strFecha) {
	var dia , mes, anio;
	var aDatos=strFecha.split("/");
	if (aDatos.length!=3) return new Date();
		
	dia=aDatos[0];
	mes=aDatos[1];
	anio=aDatos[2];

	if (isNaN(dia) || isNaN(mes) || isNaN(anio))
		return (new Date());
	anio = parseInt (anio,10)
	if ((anio >=100)& (anio < 1800))
			return (false)
		
	dia = parseInt (dia, 10);
	mes = parseInt (mes, 10);
	
	if (dia<=0) return false;
	if ((mes<=0) || (mes>12)) return false;
		
	var year
	if (anio<100) {
		year= anio + ((anio<70) ? 2000 : 1900);
	} else {
		year=anio;
	}
	
	return new Date(year, mes-1, dia)
}

function ShowCalendar(eP,eD,dmin,dmax){
	var dF=document.getElementById("CalFrame");
	var wF=window.frames["CalFrame"];
	if(null==wF.g_fCalLoaded || false==wF.g_fCalLoaded){
		alert("No se puede mostrar el calendario.\nPor favor, actualiza la pagina.");
		return;
	}

	wF.SetMinMax(procesarDate(dmin), procesarDate(dmax));

	if(eD==g_eC && dF.esVisible){
		if(g_eCV != eD.value && GetInputDate(eD.value)){
			wF.SetInputDate(g_day,g_month,g_year);
			wF.SetDate(g_day,g_month,g_year);
			g_eCV=eD.value;
		} else
			ocultarCalendar(dF, true);
	} else {
		if(GetInputDate(eD.value)){
			wF.SetInputDate(g_day,g_month,g_year);
			wF.SetDate(g_day,g_month,g_year);
		} else{
			var dt=new Date();
			wF.SetInputDate(dt.getDate(),dt.getMonth()+1,dt.getFullYear());
			wF.SetDate(dt.getDate(),dt.getMonth()+1,dt.getFullYear());
		}
		/*
		var eL=0;var eT=0;
		for(var p=eP; p&&p.tagName!='BODY'; p=p.offsetParent){
			eL+=p.offsetLeft;
			eT+=p.offsetTop;
		}
		*/
		var eL=getPageOffsetLeft(eP)-120;
		var eT=getPageOffsetTop(eP)+22;
		var altura=160;
		var anchura=144;
		var maxAlto=alturaInterna() + elementoBody().scrollTop
		if (eT+altura>maxAlto) eT=maxAlto-altura;
		if (eL<0) eL=0;
		dF.style.left=eL+ "px";
		dF.style.top=eT+ "px";
		if(!dF.esVisible)
			ocultarCalendar(dF, false);

		g_eC=eD;
		g_eCV=eD.value;
	}
}

function SetDate(d,m,y){
var tmp;
var texto;
//if (y>2000) y-=2000;

texto = CeroPadding(d) + "/" + CeroPadding(m) + "/" + CeroPadding(y);

	g_eC.focus();
	g_eC.value=texto;
	g_eCV=g_eC.value;


	if (window.Calendario_OnCambioFecha) {
		Calendario_OnCambioFecha(texto, g_eC);
	}
}

function CeroPadding(number) {
	var tmp;
	tmp = number.toString();
	if (tmp.length==1) {tmp="0" + tmp};
	return(tmp);
}


// funciones para simplificar la creación de las páginas.
// posiblemente las 2 primeras se podrían desviar a ASP mejor.

	function IniciarCalendario() {	
		document.write("<IFRAME frameBorder='0' id='CalFrame' marginHeight='0' marginWidth='0' noResize " +
			" scrolling=no src='/widgets/calendario/cal.htm' " +
		  " style='DISPLAY: none; HEIGHT: 157px; POSITION: absolute; WIDTH: 144px; Z-INDEX: 100'></IFRAME> ");
	}

	function PonerBotonCalendario(CampoControl) {
		document.write("<img align='absMiddle' border='0' valign='absmiddle' " +
			" src='/widgets/calendario/calendario.gif' style='cursor:hand' " + 
			" onclick='MostrarCalendario(\"" + CampoControl + "\", this)' >");
	}

	function MostrarCalendario(CampoControl, BotonReferencia) {
		var Control=document.getElementById(CampoControl);
		var fechaInicial='01/01/2001';
		if (typeof calFechaInicio =="string") fechaInicial=calFechaInicio;
		var fechaFinal='31/12/2010';
		if (typeof calFechaFin =="string") fechaFinal=calFechaFin;

		ShowCalendar(BotonReferencia, Control, fechaInicial, fechaFinal);
	}




function MostrarCalendario3(CampoControl, BotonReferencia) {
	if (!document.getElementById("CalFrame"))	
		MostrarCalendario2(CampoControl, BotonReferencia);
	else {
		var wF=window.frames["CalFrame"];
		if(null==wF.g_fCalLoaded || false==wF.g_fCalLoaded)
			MostrarCalendario2(CampoControl, BotonReferencia);
		else 
			MostrarCalendario(CampoControl, BotonReferencia);

	}
}



/*iran en uritecDOM.js*/

function getPageOffsetLeft(el) {
  var x;

  // Return the x coordinate of an element relative to the page.
	if (el.style.position=="absolute") {
		return getComputedStyleX(el, "left");
	}
  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);

  return x;
}

function getPageOffsetTop(el) {
  var y;

  // Return the x coordinate of an element relative to the page.
	if (el.style.position=="absolute") {
		return getComputedStyleX(el, "top");
	}
  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);

  return y;
}

function alturaInterna() {
	//innerHeight suma las barras (Moz)
//	return (window.innerHeight ? window.innerHeight : elementoBody().clientHeight);
	return (elementoBody().clientHeight);
}

function anchuraInterna() {
//	return (window.innerWidth ? window.innerWidth : elementoBody().clientWidth);
	return (elementoBody().clientWidth);
}

function CalcularX(e) {
	if (e.pageX)
		return e.pageX;
	else {
		return elementoBody().scrollLeft + e.clientX;
	}
}

function CalcularY(e) {
	if (e.pageY)
		return e.pageY;
	else {
		return elementoBody().scrollTop + e.clientY;
	}
}
//las paginas funcionan distinto si estan en modo CSS1Compat (Standard) o BackCompat
function elementoBody() {
	if ((document.compatMode) && (document.compatMode=="CSS1Compat")) 
		return document.documentElement;
	else
		return document.body;
}

function getComputedStyleX(el, sProp) {
	if (!document.defaultView)
		return parseInt(el.currentStyle[sProp]);
	else
		return parseInt(document.defaultView.getComputedStyle(el, null).getPropertyValue(sProp));
}

