// Definicion de Clase TDM
/////////////////////////////////////////////////////////
function TDM(id_dm,f_ini,f_fin,n_dias,tramo_abierto)
{
	this.id_dm=id_dm;
	this.f_ini=f_ini;
	this.f_fin=f_fin;
	this.n_dias=n_dias;
	this.tramo_abierto=tramo_abierto;  // 0=Cerrado 1=Abierto
}





//------------------------------------------------------------------------------------------------------------------------

function CompararFechas(fecha,fecha2)
// -1=(f1<f2)	0=(f1=f2)	1=(f1>f2)
// Formato de fechas=> mm/dd/YYYY
{
	var f1 = new Date(fecha);
	var f2 = new Date(fecha2);

	var diff = f1.getTime() - f2.getTime();
	resultado=-1;		// f1<f2
	if (diff > 0) 		// f1>f2
		resultado=1;
	else if (diff == 0)	// f1=f2
		resultado=0;	

	//alert(fecha+' - '+fecha2+' = '+resultado);

	return resultado;
}

function BuscarId_Tramo(dia,DatosTramo)
{
	var enc=false;
	var inde=0;
	var resultado=0;
	DatosTramo[0]=resultado;
	//alert('Nº Tramos: '+this.ListaDM.length);
	while ((!enc) && (inde<this.ListaDM.length))
	{
		cf_i=(this.CompararFechas(this.ListaDM[inde].f_ini,dia)<=0);
		cf_f=(this.CompararFechas(this.ListaDM[inde].f_fin,dia)>=0);
		//alert(this.ListaDM[inde].id_dm+' ==> '+this.ListaDM[inde].f_ini+' < '+dia+' < '+this.ListaDM[inde].f_fin+' - '+cf_i+' - '+cf_f)
		if((cf_i) && (cf_f))
		{
			enc=true;
			resultado=this.ListaDM[inde].id_dm;
			DatosTramo[0]=resultado;
			DatosTramo[1]=this.ListaDM[inde].n_dias;
			DatosTramo[2]=this.ListaDM[inde].tramo_abierto;  // 0=Cerrado 1=Abierto
		}
		inde++;
	}
	return resultado;
}

function ProcesaDia(Tramo,dia,total_dias_marcados)
{
	// 1.1 => Preparamos las variables
	var DatosTramo=new Array();
	DatosTramo[0]=0;
	DatosTramo[1]=0;
	DatosTramo[2]=0; // 0=Cerrado 1=Abierto
	if(this.BuscarId_Tramo(dia,DatosTramo)==0) // Si es 0 no pertenece a ningun tramo
	{
		//alert('No ha encontrado el tramo.');
		return 0;
	}
	
	// 1.2 => Buscamos si existe el id en Tramo
	// Tramo => [0]=id_dm  [1]=DiasMinimos  [2]=DiasSeñalados
	if(DatosTramo[0]!=0)
	{
		var enc=false;
		var revisar=false;
		var indt=0;
		//alert('Nº Tramos: '+Tramo.length);
		if(Tramo.length==0)
		{
			Tramo[0]=new Array();
			Tramo[0][0]=DatosTramo[0];
			Tramo[0][1]=DatosTramo[1];
			if(DatosTramo[2]==1)	// TRAMOS ABIERTOS
				Tramo[0][2]=total_dias_marcados;
			else					// TRAMOS CERRADOS
				Tramo[0][2]=1;
			enc=true;
			//alert('1º Carga: '+Tramo[0]);
		} // Si no hay tramos marcados
		else
		{
			while((!enc) && (indt<Tramo.length))
			{
				//alert('Busca Tramo (2): '+Tramo[indt][0]+' - '+DatosTramo[0]);
				if(Tramo[indt][0]==DatosTramo[0])
				{
					enc=true;
					revisar=true;
				}
				else
					indt++;
			}
		} // Si ya hay tramos rellenos
		
		if(enc)
		{
			//alert('Tramo abierto: '+DatosTramo[2]);
			if(DatosTramo[2]==1)	// TRAMOS ABIERTOS
				Tramo[indt][2]=total_dias_marcados;
			else if(revisar)					// TRAMOS CERRADOS
				Tramo[indt][2]=Tramo[indt][2]+1;
			//alert('Cargar 1: '+Tramo[indt]);
		}
		else
		{
			var longit=Tramo.length;
			Tramo[longit]=new Array();
			Tramo[longit][0]=DatosTramo[0];
			Tramo[longit][1]=DatosTramo[1];
			if(DatosTramo[2]==1)
				Tramo[longit][2]=total_dias_marcados;
			else
				Tramo[longit][2]=1;
			//alert('Cargar 2: '+Tramo[longit]);
		}
		
	} // Si está dentro de algún tramo
	
	//alert('Tramo:'+Tramo);
}

function ComprobarDiasMinimos(ver_alert)
{
	var resultado=true;
	var Tramo = new Array();
	
	if (this.ListaDM.length>0) // Si tiene restricciones
	{
		total_dias_marcados=Globales.Lista_Seleccion.Lista.length;
		
		// 1. Para los días marcados, vamos contando los días por tramos de días mínimos
		for (i=0;i<total_dias_marcados;i++) 
			if(Globales.Lista_Seleccion.Lista[i]!=null)
			{
				mes1=Globales.Lista_Seleccion.Lista[i].Mes;
				dia1=Globales.Lista_Seleccion.Lista[i].Dia;
				ano1=Globales.Lista_Seleccion.Lista[i].Ano;
				if(mes1<=9) mes1='0'+mes1;
				if(dia1<=9) dia1='0'+dia1;
				if(ano1<=9) ano1='0'+ano1;
				fech=mes1+'/'+dia1+'/'+ano1;
				//alert(Tramo +' - '+fech+' - '+Globales.Lista_Seleccion.TReserva_NDias());
				this.ProcesaDia(Tramo,fech,Globales.Lista_Seleccion.TReserva_NDias());
			}

		// 2. Comprobamos que todos los tramos de los dias tengas días mínimos
		var enc_fallo=false;
		var indt=0;
		var id_fallo=0;
		while((!enc_fallo) && (indt<Tramo.length))
		{
			//alert('Comprobar: '+Tramo[indt][1]+' > '+Tramo[indt][2]);
			if(Tramo[indt][1]>Tramo[indt][2])
			{
				enc_fallo=true;
				id_fallo=Tramo[indt][0];
			}
			indt++;
		}
		resultado=!enc_fallo;
	
		if (!resultado)
		{
			var enc_fallo=false;
			var indt=0;
			var sms_fallo='';
			while((!enc_fallo) && (indt<this.ListaDM.length))
			{
				if(this.ListaDM[indt].id_dm==id_fallo)
				{
					enc_fallo=true;
					fechaI=this.ListaDM[indt].f_ini;
					fechaF=this.ListaDM[indt].f_fin;
					fechaI = new Date(fechaI);
					fechaF = new Date(fechaF);
					var fechaI2=fechaI.getDate()+'/'+(fechaI.getMonth()+1)+'/'+fechaI.getFullYear();
					var fechaF2=fechaF.getDate()+'/'+(fechaF.getMonth()+1)+'/'+fechaF.getFullYear();
					/*sms_fallo='Estimado cliente \n\nEntre las fechas '+fechaI2+' - '+fechaF2+' las noches mínimas de contratación son '+this.ListaDM[indt].n_dias+'. \nDisculpe las molestias.';*/
					sms_fallo=plantilla_sms_dias_minimos; // Esta variable es global en calendario.php
					sms_fallo=sms_fallo.replace(/\<br \/\>/g,'\n');
					sms_fallo=sms_fallo.replace('[fecha_1]',fechaI2);
					sms_fallo=sms_fallo.replace('[fecha_2]',fechaF2);
					sms_fallo=sms_fallo.replace('[noches_min]',this.ListaDM[indt].n_dias);
					Fallo_Dias_Minimos=sms_fallo;
				}
				indt++;
			}
			
			if(ver_alert)
				alert(sms_fallo);
		}
	}
	
	return resultado;
}

function OrdenaFechas()
{
	return true;
}

function AddDia(id_dm,f_ini,f_fin,n_dias,tramo_abierto)
{
	  this.ListaDM[this.ListaDM.length]=new TDM(id_dm,f_ini,f_fin,n_dias,tramo_abierto);
	  this.OrdenaFechas();
}

function Mostrar()
{
	var salida='';
	for(i=0;i<this.ListaDM.length;i++)
		salida+='id='+this.ListaDM[i].id_dm+' - f_ini='+this.ListaDM[i].f_ini+' - f_fin='+this.ListaDM[i].f_fin+' - n_dias='+this.ListaDM[i].n_dias+'   /   ';
	alert(salida);
}

// Definicion de Clase TDiasMinimos
/////////////////////////////////////////////////////////
function TDiasMinimos()
{
	this.ListaDM=new Array();
	this.AddDia=AddDia;
	this.ComprobarDiasMinimos=ComprobarDiasMinimos;
	this.Mostrar=Mostrar;
	this.ProcesaDia=ProcesaDia;
	this.CompararFechas=CompararFechas;
	this.BuscarId_Tramo=BuscarId_Tramo;
	this.OrdenaFechas=OrdenaFechas;
}
