function MM_openBrWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
}
jQuery.fn.reset = function () {
  $(this).each (function() { this.reset(); });
}

$('#frm_cotizador').ready(function(){
		$('#frm_cotizador').reset();
});
function reservar_hotel(id_habitacion,total,verificador){
	$('#id_habitacion_plan').val(id_habitacion);
	$('#total').val(total);
	$('#verificador').val(verificador);
	$('#frm_resarvar').attr('action','https://www.mexicohoteles.com.mx/Reservar/Hoteles/');
	//$('#frm_resarvar').attr('action','http://localhost/Reservar/Hoteles/');
	$('#frm_resarvar').submit();
}



$.datepicker.setDefaults($.datepicker.regional['es']);
$(function() {
	var dates = $('#llegada, #salida').datepicker({
		defaultDate: 0,
		altFormat: 'dd/mm/yyyy',
		minDate: 0,
		numberOfMonths: 2,
		showOn: 'both',
		duration:0,
		buttonImage: '/images/b_calendar.png',
		buttonImageOnly: true,
		onSelect: function(selectedDate) {
			var option = this.id == "llegada" ? "minDate" : "maxDate";
			var instance = $(this).data("datepicker");
			var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
			var date2=new Date(date);
			if(this.id == "llegada"){
				date2.setDate(date.getDate()+1);
				dates.not(this).datepicker("option", option, date2);
			}

		},
		beforeShow: function() {
			var option = this.id == "llegada" ? "maxDate" : "minDate";
			var fecha_valor = this.id == "llegada" ? $('#salida').val() : $('#llegada').val();
			var split_fecha=fecha_valor.split('/');
			var new_fecha=new Date(parseInt(split_fecha[2],10),parseInt(split_fecha[1],10)-1,parseInt(split_fecha[0],10));
			if(this.id != "llegada"){
				new_fecha.setDate(new_fecha.getDate()+1);
				$(this).datepicker("option", option, new_fecha);
			}
		}
	});
	$('#num_habitaciones').change(function(){
		var obj_select=this;
		var num_hab=$(obj_select).val();
		muestra_habitaciones(num_hab);
	});

	$('.num_menores').change(function(){
		var sel=this;
		var num_menores=$(this).val();
		var comp_id_hab=$(sel).attr('id').split('_');
		var id_hab=parseInt(comp_id_hab[2],10);
		if (num_menores != 0){
			muestra_menores(id_hab,num_menores);
		}
		else{
			esconde_menores(id_hab);
		}
	});
	$('#frm_cotizador').submit(function(event){
//		event.preventDefault();
		if($("#llegada").val()== '' || $("#salida").val()== ''){
			alert("Por favor seleccione su fecha de entrada y salida del hotel.");
			return false;
		}
		if(parseInt($("#num_habitaciones").val(),10) < 1){
			alert("No se puede reservar menos de una habitación.");
			return false;
		}
		if(!checa_adultos()){
			return false;
		}
		if(!checa_menores()){
			return false;
		}
		if(!checa_menores_edades()){
			return false;
		}
		return true;		
	});

	$('.ayuda_boton').click(function (){
		if ($('#ayuda').is (':visible')){
			$('#ayuda').hide('slow');
		}
		else{
			$('#ayuda').show('slow');
		}
		
	});

	$('#muesta_telefonos').hover(function (){
		$('#div_telefonos').show('slow');
	},function (){});

	$('#menu_telefonos').hover(function (){},function (){
		if ($('#div_telefonos').is (':visible')){
			$('#div_telefonos').hide('slow');
		}
	});
	$('#div_telefonos').hover( function(){} ,function (){
		$('#div_telefonos').hide('slow');
	});

	jQuery.fn.center = function () {
	    this.css("position","absolute");
	    this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
	    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
	    return this;
	}

	$(".detalles_tarjetas").click(function(event){
		event.preventDefault();
		$("#pagos_container").center().toggle();
	});

});
	
	function muestra_habitaciones(numero_habitaciones){
		var max_habitaciones=parseInt($('#numero_habitaciones').val(),10);
		for(i=2;i<=max_habitaciones;i++){
			if (!($('#hab_'+i).is(':visible')) && i <= numero_habitaciones){
				$('#adulto_hab_'+i+' option[value='+1+']').attr('selected', 'selected');
				$('#menor_hab_'+i+' option[value='+0+']').attr('selected', 'selected');
				$('#hab_'+i).show();
			}else{
				if(i > numero_habitaciones){
					$('#hab_'+i).hide();
					$('#div_menor_hab_'+i).hide();
				}
			}
		}
		esconde_titulo(numero_habitaciones);
	}

	function muestra_menores(id_habitacion,num_menores){
		var max_menores=parseInt($('#max_menores').val(),10);
		if(!$('#div_menor_hab_'+id_habitacion).is(':visible')){
			$('#div_menor_hab_'+id_habitacion).show();
		}
		if(!$('.menores_titulo').is(':visible')){
			$('.menores_titulo').show();
		}
		for(i=0;i<=max_menores;i++){
			if (!($('#divhabitacion-'+id_habitacion+'-menor-'+i).is(':visible')) && i <= num_menores){
				$('#habitacion-'+id_habitacion+'-menor-'+i+' option[value='+-1+']').attr('selected', 'selected');
				$('#divhabitacion-'+id_habitacion+'-menor-'+i).show();
			}
			else{
				if(i > num_menores){
					$('#divhabitacion-'+id_habitacion+'-menor-'+i).hide();
				}
			}
		}
	}
	
	function esconde_menores(id_habitacion){
		var max_menores=parseInt($('#max_menores').val(),10);
		var num_habitaciones=parseInt($('#num_habitaciones').val(),10);
		$('#div_menor_hab_'+id_habitacion).hide();
		for(i=1;i<=max_menores;i++){
			$('#habitacion-'+id_habitacion+'-menor-'+i+' option[value='+0+']').attr('selected', 'selected');
		}
		esconde_titulo(num_habitaciones);
	}
	
	function esconde_titulo(num_habitaciones){
		var retorno=true;
		for(i=1;i<=num_habitaciones;i++){
			var temp=parseInt($('#menor_hab_'+i).val(),10);
			if(temp!=0){
				retorno=false;
			}
		}
		if(retorno){
			$('.menores_titulo').hide();
		}
	}
	
	function checa_adultos(){
		retorno=true;
		var max_adultos=$("#max_adultos").val();
		$(".num_adultos_habitacion").each(function(){
			if($(this).is(':visible')){
				if(this.value < 1 && retorno){
					alert("No puede reservar una habitación sin adultos");
					this.focus();
					retorno=false;
				}
				if(this.value > max_adultos && retorno){
					alert("La habitación solo permite un máximo de "+max_adultos+" adultos");
					this.focus();
					retorno=false;
				}
			}
		});
		return retorno;
	}
	
	function checa_menores(){
		retorno=true;
		var max_menores=$("#max_menores").val();
		$(".num_menores").each(function(){
			if($(this).is(':visible')){
				if(this.value < 0 && retorno){
					alert("No puede reservar menos de 0 menores");
					this.focus();
					retorno=false;
				}
				if(this.value > max_menores && retorno){
					alert("La habitación solo permite un máximo de "+max_menores+" adultos");
					this.focus();
					retorno=false;
				}
			}
		});
		return retorno;
	}

	function checa_menores_edades(){
		retorno=true;
		var edad_max_menores=$("#edad_max_menores").val();
		$(".edades_menores").each(function(){
			if($(this).is(':visible')){
				if((this.value < 0 || this.value == "") && retorno){
					alert("Seleccione la edad de los menores.");
					this.focus();
					retorno=false;
				}
				if(parseInt(this.value,10) > edad_max_menores && retorno){
					alert("La edad máxima de los menores es de "+edad_max_menores+" años");
					this.focus();
					retorno=false;
				}
			}
		});
		return retorno;
	}
	

