var ventanaCalendario=false
function trimAll(sString) 
{
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}


function finMes(mes,ano){
	var nMes = parseInt(mes);
	var nRes = 0;
	switch (nMes){
	case 1: nRes = 31; break;
	case 2: (ano%4==0)?nRes = 29:nRes=28; break;
	case 3: nRes = 31; break;
	case 4: nRes = 30; break;
	case 5: nRes = 31; break;
	case 6: nRes = 30; break;
	case 7: nRes = 31; break;
	case 8: nRes = 31; break;
	case 9: nRes = 30; break;
	case 10: nRes = 31; break;
	case 11: nRes = 30; break;
	case 12: nRes = 31; break;
	}
	return nRes;
}

function esDigito(sChr){
	var sCod = sChr.charCodeAt(0);
	return ((sCod > 47) && (sCod < 58));
}

function valDia(dia, ano, mes){
	var bOk = false;
	bOk = bOk || ((dia >= 1) && (dia <= finMes(mes,ano)));
	return bOk;
}

function valMes(mes){
	var bOk = false;
	bOk = bOk || ((mes >= 1) && (mes <= 12));
	return bOk;
}

function valAno(ano){
	var bOk = true;
	bOk = bOk && ((ano >1900) && (ano<2101));
	return bOk;
}
// formato   aaaa/mm/aa
function valFecha(fecha){
	var bOk = true;
	aux= (fecha.replace("-","/")).split("/");
	if (parseInt(aux[1]) ==0 || aux[1].substr(0,1) =="0"){aux[1]=aux[1].substr(1,1);}
	if (parseInt(aux[2]) ==0|| aux[2].substr(0,1) =="0"){aux[2]=aux[2].substr(1,1);}
	//alert("año "+(aux[0])+"      mes "+(aux[1])+"      dias "+(aux[2])+"      ")
	bOk = bOk && (valAno(parseInt(aux[0])));
	bOk = bOk && (valMes(parseInt(aux[1])));
	bOk = bOk && (valDia(parseInt(aux[2]),parseInt(aux[0]),parseInt(aux[1])));
	if (!bOk){
		alert("Fecha inválida. Se utilizará la fecha Inicial(Actual en su defecto).");
		return false;
	}
	return true;
}


function muestraCalendario(raiz,formulario_destino,campo_destino,fecha_inicial, valor_SID){
	//funcion para abrir una ventana con un calendario.
	//Se deben indicar los datos del formulario y campos que se desean editar con el calendario, es decir, los campos donde va la fecha.
	if (typeof ventanaCalendario.document == "object") {
		ventanaCalendario.close()
	}
	valor =eval("document.getElementById('"+campo_destino +"').value")
	//valor=document.getElementById("'"+campo_destino+"'" ).value;
	if (trimAll(valor)=="" || !valFecha(valor)){
		ventanaCalendario = window.open(raiz + "calendario/index.php?"+valor_SID+"&formulario=" + formulario_destino + "&nomcampo=" + campo_destino + "&ini=" +fecha_inicial ,"calendario","width=300,height=330,left=100,top=100,scrollbars=no,menubars=no,statusbar=NO,status=NO,resizable=YES,location=NO")
	} else {
		aux=valor.split("/");
		if (parseInt(aux[1]) ==0 || aux[1].substr(0,1) =="0"){aux[1]=aux[1].substr(1,1);}
		if (parseInt(aux[2]) ==0|| aux[2].substr(0,1) =="0"){aux[2]=aux[2].substr(1,1);}
		ventanaCalendario = window.open(raiz + "calendario/index.php?formulario=" + formulario_destino + "&nomcampo=" + campo_destino + "&nuevo_ano=" + aux[0] + "&nuevo_mes=" + aux[1] + "&dia=" + aux[2] +"&fecha_selec="+ aux[0] + "/"+ aux[1] + "/"+ aux[2]+ "&ini=" +fecha_inicial ,"calendario","width=300,height=330,left=100,top=100,scrollbars=no,menubars=no,statusbar=NO,status=NO,resizable=YES,location=NO")
	}
}
