var aFinMes = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

function CalGreg2CalJul(sFecha){ 	
	var dia = sFecha.substring(0,2);
	var mes = sFecha.substring(3,5);
	var ano = sFecha.substring(6,10);
	if (dia.charAt(0) == "0") dia = dia.charAt(1);
	if (mes.charAt(0) == "0") mes = mes.charAt(1);

	dia = parseInt(dia);
	mes = parseInt(mes);
	ano = parseInt(ano);
	
	var a = Math.floor((14 - mes) / 12);
	var y = ano + 4800 - a;
	var m = mes + 12 * a - 3;
	var k = dia + Math.floor((153 * m + 2) / 5);
	k = k + y * 365 + Math.floor(y / 4);
	k = k - 32083;
	
	return k;
}

function comprueba_dias_entre(fecha1,fecha2,destino){
	dias = dias_entre(fecha1.value,fecha2.value);
	if(dias>=0){
		if(destino) destino.value = dias;
	}else{
		alert("La fecha final no puede ser anterior a la fecha inicial");
		if(destino){
			destino.value = 1;
			fecha2.value = addToDate(fecha1.value,1);
		}
	}
}

function comprueba_fecha_inicio_fecha_fin(fecha1,fecha2,campo){
  miFechaInicio = document.getElementById(fecha1);
  miFechaFin = document.getElementById(fecha2);
  if ((miFechaInicio.value!='') && (miFechaFin.value!='')){
    dias = dias_entre(miFechaInicio.value,miFechaFin.value);
    if(dias>=0){
      return true;
    }else{
      eval ("alert('La fecha final no puede ser anterior a la fecha inicial en"+campo+"');");
      miFechaInicio.value='';
      miFechaFin.value='';
      return false;
    }
  }
}

function dias_entre(fecha1,fecha2,destino){

	var dias = CalGreg2CalJul(fecha2) - CalGreg2CalJul(fecha1);
	
	if(destino) destino.value = dias;
	return dias;

}

function comprueba_fecha_min(fecha,fecha_min_busqueda){
	if(dias_entre(fecha_min_busqueda,fecha.value)<0){
		alert('La fecha inicial no puede ser inferior a '+fecha_min_busqueda);
		fecha.value = fecha_min_busqueda;
		return false;
	}
	return true;
}

function comprueba_fecha_max(fecha,fecha_max_busqueda){
	if(dias_entre(fecha.value,fecha_max_busqueda)<0){
		alert('La fecha final no puede ser posterior a '+fecha_max_busqueda);
		fecha.value = fecha_max_busqueda;
		return false;
	}
	return true;
}

function finMes(nMes, nAno){
	return aFinMes[nMes - 1] + (((nMes == 2) && (nAno % 4) == 0)? 1: 0);
}

function padNmb(nStr, nLen, sChr){
	var sRes = String(nStr);
	for (var i = 0; i < nLen - String(nStr).length; i++)
	sRes = sChr + sRes;
	return sRes;
}

function makeDateFormat(nDay, nMonth, nYear){
	var sRes;
	sRes = padNmb(nDay, 2, "0") + "/" + padNmb(nMonth, 2, "0") + "/" + padNmb(nYear, 4, "0");
	return sRes;
}

function makeDateFormatMySQL(nDay, nMonth, nYear){
	var sRes;
	sRes = padNmb(nYear, 4, "0") + "-" + padNmb(nMonth, 2, "0") + "-" + padNmb(nDay, 2, "0");
	return sRes;
}

function incDate(sFec0){

	var nDia = parseInt(sFec0.substr(0, 2), 10);
	var nMes = parseInt(sFec0.substr(3, 2), 10);
	var nAno = parseInt(sFec0.substr(6, 4), 10);
	nDia += 1;
	if (nDia > finMes(nMes, nAno)){
		nDia = 1;
		nMes += 1;
		if (nMes == 13){
			nMes = 1;
			nAno += 1;
		}
	}
	return makeDateFormat(nDia, nMes, nAno);
}

function incDateMySQL(sFec0){

	var nAno = parseInt(sFec0.substr(0, 4), 10);
	var nMes = parseInt(sFec0.substr(5, 2), 10);
	var nDia = parseInt(sFec0.substr(8, 2), 10);
	nDia += 1;
	if (nDia > finMes(nMes, nAno)){
		nDia = 1;
		nMes += 1;
		if (nMes == 13){
			nMes = 1;
			nAno += 1;
		}
	}
	return makeDateFormatMySQL(nDia, nMes, nAno);
}


function decDate(sFec0){
	var nDia = Number(sFec0.substr(0, 2));
	var nMes = Number(sFec0.substr(3, 2));
	var nAno = Number(sFec0.substr(6, 4));
	nDia -= 1;
	if (nDia == 0){
		nMes -= 1;
		if (nMes == 0){
			nMes = 12;
			nAno -= 1;
		}
		nDia = finMes(nMes, nAno);
	}
	return makeDateFormat(nDia, nMes, nAno);
}
function restarFechas(fechaInicio,fechaFin){
/*
	fechaI = new Date(Number(fechaInicio.substr(6, 4)),Number(fechaInicio.substr(3, 2)),Number(fechaInicio.substr(0, 2)));
	fechaF = new Date(Number(fechaFin.substr(6, 4)),Number(fechaFin.substr(3, 2)),Number(fechaFin.substr(0, 2)));
	var diferencia = fechaF.getTime() - fechaI.getTime();
	var dias = Math.round(diferencia / (1000 * 60 * 60 * 24))
*/
    dias = dias_entre(fechaInicio,fechaFin);

	return dias;
}

function decDateMySQL(sFec0){
	var nAno = parseInt(sFec0.substr(0, 4), 10);
	var nMes = parseInt(sFec0.substr(5, 2), 10);
	var nDia = parseInt(sFec0.substr(8, 2), 10);
	nDia -= 1;
	if (nDia == 0){
		nMes -= 1;
		if (nMes == 0){
			nMes = 12;
			nAno -= 1;
		}
		nDia = finMes(nMes, nAno);
	}
	return makeDateFormatMySQL(nDia, nMes, nAno);
}

function addToDate(sFec0, sInc){
	var nInc = Math.abs(parseInt(sInc));
	var sRes = sFec0;
	if (parseInt(sInc) >= 0)
	for (var i = 0; i < nInc; i++) sRes = incDate(sRes);
	else
	for (var i = 0; i < nInc; i++) sRes = decDate(sRes);
	return sRes;
}

function addToDateMySQL(sFec0, sInc){
	var nInc = Math.abs(parseInt(sInc));
	var sRes = sFec0;
	if (parseInt(sInc) >= 0)
	for (var i = 0; i < nInc; i++) sRes = incDateMySQL(sRes);
	else
	for (var i = 0; i < nInc; i++) sRes = decDateMySQL(sRes);
	return sRes;
}

function recalcF1(){
	with (document.formulario){
		fecha1.value = addToDate(fecha0.value, increm.value);
	}
}

function onKeyPressNumberHora(campo)
{
	if ((event.keyCode > 31 && event.keyCode < 48 ) || event.keyCode > 57)
	{
		event.returnValue=false;
	}
	else
	{
		valor = String(campo.value);
		tam = valor.length;
		if(tam==2)
		{
			campo.value = valor+':';
		}
		else if(tam==5)
		{
			campo.value = valor+':';
		}
	}
}
function validarNoches(noches,defecto){
	if(noches.value>=defecto){
		return true;
	}else{
		alert('El mínimo de noches es '+defecto);
		noches.value = defecto;
		return false;
	}	
}

function comparaFechas(fecha_inicial,fecha_final){
	var Data1_arr,Data2_arr,aString1,aString2,iString1,iString;

	Data1_arr = fecha_inicial.split('/');
	Data2_arr = fecha_final.split('/');

	aString1 = Data1_arr[2] + Data1_arr[1] + Data1_arr[0];
	aString2 = Data2_arr[2] + Data2_arr[1] + Data2_arr[0];
	iString1 = parseInt(aString1);
	iString2 = parseInt(aString2);

	sMascara = /^\d{2}(\/)\d{2}(\/)\d{4}$/
	if (!sMascara.test(fecha_inicial) || !sMascara.test(fecha_final))
	{
		alert("Las fechas deben estar en formato dd/mm/aaaa");
		return fecha_inicial;
	}

	if (iString1 <= iString2) {
		return fecha_final;
	}
	else
	{
		alert('La fecha final ('+fecha_final+') no puede ser anterior a la fecha inicial ('+fecha_inicial+')');
		return fecha_inicial;
	}
}

function validarFecha(id_campo){
	var fecha = $('#'+id_campo).val();
	var fallo = 0;
	re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
	if(fecha != '') {
		if(regs = fecha.match(re)) {
			var dia = fecha.substring(0,2);
			var mes = fecha.substring(3,5);
			var anio = fecha.substring(6,10);
			if (mes == 2) {
				if (dia == 29) {
					if (anio % 4 != 0 || anio % 100 == 0 && anio % 400 != 0) {
						fallo = 1;
					}
				}
				else if (dia > 28) {
					fallo = 1;
				}
			}else if (mes == 4 || mes == 6 || mes  == 9 || mes == 11) {
				if (dia > 30) {
					fallo = 1;
				}
			}else {
				if (mes > 12) {
					fallo = 1;
				}else if (dia > 31) {
					fallo = 1;
				}

			}
			if(fallo){
				alert("Fecha incorrecta");
				$('#'+id_campo).val('');
			}
		}
	}
}