﻿var locationList='';
var NeighBorhood=1;
var item =-1;
    var addEvent = function(elm, evType, fn, useCapture) {      		
        if (elm.addEventListener) {
            elm.addEventListener(evType, fn, useCapture);
            return true;
        } else if (elm.attachEvent) {
            var r = elm.attachEvent('on' + evType, fn);
            return r;
        } else {
            elm['on' + evType] = fn;
        }
    };

 
    var _$ = function(idObj) {
        return document.getElementById(idObj);
    };
	
	
	var createCookie = function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	};

	var readCookie = function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	};

	var eraseCookie = function(name) {
		createCookie(name,"",-1);
	};

		
		var showBooking = function(searchType){
		
	    var sBkDefault, sBkC, sBkF, sBkA, sBkAR;
		/*Is package or hotel*/
		sBkDefault = sBkC = sBkF = sBkA = sBkAR = 'none';
		
		if ((searchType == 'H') || (searchType == 'HC') || (searchType == 'HF') || (searchType == 'HCF')) {
			sBkDefault = '';			
		}else{

			if ((searchType == 'C')) {
				sBkC = '';
			}
			if ((searchType == 'F')) {
				sBkF = '';
			}
			if ((searchType == 'A')) {
				sBkA = '';
            }
            if ((searchType == 'AR')) {
                sBkAR = '';
            }
			
			
            
		}
		
		if ((searchType == 'H')) {
		                $('#dvdpckdestiny').hide();
				$('#neighborhood').show();
				$('#hotelbookinglabelArea').show();
				$('#hotelbookinglabelHotel').show();
				$('#hotel').show();
            }
		if ((searchType == 'HC') || (searchType == 'HF') || (searchType == 'HCF')) {
		$('#dvdpckdestiny').show();
		$('#neighborhood').hide();
		$('#hotelbookinglabelArea').hide();
		$('#hotelbookinglabelHotel').hide();
		
		$('#txtCitypk').hide();
		$('#hotel').hide();

		}
		
		_$('booking-package-search').style.display = sBkDefault;
		_$('booking-car-search').style.display = sBkC;
		_$('booking-flight-search').style.display = sBkF;
		_$('booking-activity-search').style.display = sBkA;
		_$('booking-restaurant-search').style.display = sBkAR;
		
		/*preview UI*/
		if(sBkDefault ==''){
			 if ((searchType == 'HF') || (searchType == 'HCF')) {
				
				_$("flightType").style.display = '';
				_$("booking-times").style.display = '';


				
			} else {
				_$("flightType").style.display = 'none';
				_$("booking-times").style.display = 'none';


	
			}
			if (searchType == 'H'){
			 _$("booking-promocode").style.display = '';

			}else{
			 _$("booking-promocode").style.display = 'none';
			 
			 }
		}else{
			/* Pendiente */
		}		
		
		if(null != readCookie(bkCookie)){
			eraseCookie(bkCookie);
		}
		createCookie(bkCookie, searchType, 7);		
	};


		
	/*Constants elements*/
    var searchType = 'H';
	var flightType = 'Rt';
	var bkCookie = 'uv-bk-search';	

	if(null != readCookie(bkCookie)){
		searchType = readCookie(bkCookie);
	}
	
 showBooking(searchType);

   
 addEvent(_$('btnBookingSearch'), 'click', function() {

	  var bk;
		var copyhtml=0;
		
		bk = document.forms["FrmBookingDNNTrick"];
		if (!bk)
		{bk = document.forms[0];}
		else
		{copyhtml =1;}
		


           // var bk = document.forms["booking-engine"];
			if(null != readCookie(bkCookie)){
				searchType = readCookie(bkCookie);			
			}else{
				searchType = 'H';
			}
					HTTP_RELATIVE_ACTION="http://www.hotelesenmexico.com.mx";
            
			
           // bk.submit();
           // return false;
          }, false);



        var optPk = document.getElementsByName("PackagesType");
		/* Set searchtypes radiobuttons events*/
        for (var i = 0; i < optPk.length; i++) {
            addEvent(optPk[i], 'click', function(e) {
				var srcElem = (window.event) ? window.event.srcElement : e.target;
				showBooking(srcElem.value);
            }, false);
            
            /*fire default selected*/
            if (optPk[i].value == searchType) optPk[i].click(); 
        }
		
        var optFm = document.getElementsByName("type_trip");
		/* Set flight modes radiobuttons events*/
        for (var i = 0; i < optFm.length; i++) {
            addEvent(optFm[i], 'click', function(e) {
				var srcElem = (window.event) ? window.event.srcElement : e.target;
				if(srcElem.value == 'OW' )
					_$('flight-return-dates').style.display = 'none';
				else
					_$('flight-return-dates').style.display = '';
            }, false);
            
            /*fire default selected*/
            if (optFm[i].value == flightType) optFm[i].click(); 
        }



addEvent(_$('btnBookingSearch'), 'click', function() {

	  var bk;
		var copyhtml=0;
		
		bk = document.forms["FrmBookingDNNTrick"];
		if (!bk)
		{bk = document.forms[0];}
		else
		{copyhtml =1;}
	
			if(null != readCookie(bkCookie)){
				searchType = readCookie(bkCookie);			
			}else{
				searchType = 'H';
			}
	
			try
			{
	
			if ($('#opt_H').attr('checked'))
				searchType =$('#opt_H').val();
			if ($('#opt_F').attr('checked'))
				searchType =$('#opt_F').val();
			if ($('#opt_C').attr('checked'))
				searchType =$('#opt_C').val();
			if ($('#opt_A').attr('checked'))
				searchType =$('#opt_A').val();
			if ($('#opt_HC').attr('checked'))
				searchType =$('#opt_HC').val();
			if ($('#opt_HCF').attr('checked'))
				searchType =$('#opt_HCF').val();
			if ($('#opt_HF').attr('checked'))
				searchType =$('#opt_HF').val();
			}
			catch(e)
			{
				if(null != readCookie(bkCookie)){
				searchType = readCookie(bkCookie);			
				}else{
					searchType = 'H';
				}
			}
			
			
			HTTP_RELATIVE_ACTION="http://www.hotelesenmexico.com.mx";
            
			switch(searchType) {
			case "H":
			if (NeighBorhood==0)
			{
				if(_$('hotel').value.split(',').length>0)
				 {
						for (i=0; i<dnnUrlHotels.length; i++)
							{
								if (dnnUrlHotels[i].Id==_$('hotel').value.split(',')[0])
									{item =i;break;}
							}
				}
				if (item>=0)
				{
					_$('iProperty').value = _$('hotel').value.split(',')[0];
					_$('iProvider').value = "0";	
					_$('iChainCode').value = "UV";
					_$('iAccessCode').value = _$('promo').value;
					_$('iCurrency').value = _$('currency').value;
					_$('iTab').value = "Rates";
					
					_$('iStartDate').value = _$('checkinpk').value;
					_$('iEndDate').value = _$('checkoutpk').value;
					_$('iAges').value = "";
					_$('iChildren').value = _$('childrenHotel').value;
					
					_$('iAdults').value = _$('adultsHotel').value;
					
					_$('iRooms').value = _$('roomsHotel').value;
				}
				
				
				if(copyhtml==1 && _$('dvBookingData') && _$('bookingGrid'))
					{
					
						if(document.all)
						{
						
						_$('dvBookingData').innerHTML =  _$('bookingGrid').innerHTML ;
						}
						else
							bk.appendChild(_$('bookingGrid'));
					}
				
					if (item>=0)
					{
					
						bk.action =dnnUrlHotels[i].Url + RatesPage;		    
						bk.method ='Post';	
						bk.submit();
					}	
			}
			else
			{
					 var strStart = _$('checkinpk').value;
		    	var strEnd = _$('checkoutpk').value;
		    	if (strStart.split("/").length>2){strStart = _$('checkinpk').value.split("/")[2] + _$('checkinpk').value.split("/")[0] + _$('checkinpk').value.split("/")[1]}
		    	if (strEnd.split("/").length>2){strEnd = _$('checkoutpk').value.split("/")[2] + _$('checkoutpk').value.split("/")[0] + _$('checkoutpk').value.split("/")[1]}
		
		    //alert(window.location.href);
		    	var urlhotel = locationList + '?Checkin=' + strStart + '&CheckOut=' + strEnd + "&Rooms=" +  _$('roomsHotel').value + "&Adults=" + _$('adultsHotel').value +  "&Children=" +   _$('childrenHotel').value + "&AccessCode=" + _$('promo').value + "&Currency=" + _$('currency').value;
				//alert(urlhotel);
				window.location.href = urlhotel;
			}
			
return;					
				break;
			case "C":
				var urlcar="";
				urlcar =HTTP_RELATIVE_ACTION+"/Car/pgnCarIncompleteSearch.aspx?";
				urlcar+="Aeropuerto="
				if ($('#rbAirport').attr('checked'))
					urlcar+="1";
				else
					urlcar+="0";
				urlcar+="&CityIn="+$('#txtCity').val();	
				urlcar+="&Horasalida="+$('#ddTime1').val();	
				urlcar+="&Size="+$('#cmbClases').val();	
				urlcar+="&Type="+$('#cmbTipos').val();	
				urlcar+="&CityDroft="+$('#txtCiudadDrop').val();	
				urlcar+="&CurrencyType="+$('#currency').val();	
				
				//alert($('#currency').val());
				
				var strStart = _$('checkincar').value;
		    	var strEnd = _$('checkoutcar').value;
		    	if (strStart.split("/").length>2){strStart = _$('checkincar').value.split("/")[2] + _$('checkincar').value.split("/")[0] + _$('checkincar').value.split("/")[1]}
		    	if (strEnd.split("/").length>2){strEnd = _$('checkoutcar').value.split("/")[2] + _$('checkoutcar').value.split("/")[0] + _$('checkoutcar').value.split("/")[1]}
				urlcar+="&CheckIn="+strStart;
				urlcar+="&CheckOut="+strEnd;
				window.location.href=urlcar;
				return;
				break;				
			case "F":	
				 var urlflight = "";

				urlflight =HTTP_RELATIVE_ACTION+"/flight/pgnAirIncompleteSearch.aspx?";
					urlflight += "chkFlightsSeparately=no";

					if ($('#chkFlightRedondo').attr('checked'))
					urlflight += "&type_trip=RT";
				else
					urlflight += "&type_trip=OW";
					/*
					if (document.getElementById("idtype_trip"))
						urlflight += "&type_trip=" + document.getElementById("idtype_trip").value;
					else
					urlflight += "&type_trip=RT";
					*/
					if (document.getElementById("ddlClase"))
						urlflight += "&ddlClase=" + document.getElementById("ddlClase").value;
					
					var strStart = _$('checkinflt').value;
					var strEnd = _$('checkoutflt').value;
					if (strStart.split("/").length>2){strStart = _$('checkinflt').value.split("/")[2] + _$('checkinflt').value.split("/")[0] + _$('checkinflt').value.split("/")[1]}
					if (strEnd.split("/").length>2){strEnd = _$('checkoutflt').value.split("/")[2] + _$('checkoutflt').value.split("/")[0] + _$('checkoutflt').value.split("/")[1]}
					urlflight+="&CheckIn="+strStart;
					urlflight+="&CheckOut="+strEnd;
					
					if (document.getElementById("ddlAerolinea"))
						urlflight += "&ddlAerolinea=" + document.getElementById("ddlAerolinea").value;
					if (document.getElementById("txtCity1"))
						urlflight += "&RefCityO=" + document.getElementById("txtCity1").value;
					if (document.getElementById("txtCity"))
						urlflight += "&RefCityD=" + document.getElementById("txtCity").value;
					if (document.getElementById("ddTime1"))
						urlflight += "&ddHorasalida=" + document.getElementById("ddTime1").value;
					if (document.getElementById("ddTime2"))
						urlflight += "&ddhoraregreso=" + document.getElementById("ddTime2").value;
					if (document.getElementById("adults"))
						urlflight += "&Adultos=" + document.getElementById("adults").value;
					if (document.getElementById("children"))
						urlflight += "&Ninios=" + document.getElementById("children").value;
					if (document.getElementById("Seniors"))
						urlflight += "&Seniors=" + document.getElementById("Seniors").value;
					if (document.getElementById("currency"))
						urlflight += "&tipomoneda=" + document.getElementById("currency").value;


           window.location.href=urlflight;
				return;
				break;
			case "A":
			var urlactivitie="";
				urlactivitie =HTTP_RELATIVE_ACTION+"/activity/activitiessearch.aspx?";
			
				urlactivitie+="&txtCity="+$('#txtCity').val();	
				
				var strStart = _$('checkinact').value;
		    	var strEnd = _$('checkoutact').value;
		    	//if (strStart.split("/").length>2){strStart = _$('checkinact').value.split("/")[2] + _$('checkinact').value.split("/")[0] + _$('checkinact').value.split("/")[1]}
		    	//if (strEnd.split("/").length>2){strEnd = _$('checkoutact').value.split("/")[2] + _$('checkoutact').value.split("/")[0] + _$('checkoutact').value.split("/")[1]}
				urlactivitie+="&txtActF1="+strStart;
				urlactivitie+="&txtActF2="+strEnd;
				urlactivitie+="&currency="+$('#currency').val();
				urlactivitie+="&booking=1";
				//alert(urlactivitie);
				window.location.href=urlactivitie;
				return;

				break;
			case "AR":				 
				 var date1 = _$('txtActF11').value;
				 var date2 = _$('txtActF21').value;		
				if (date1 != "mm/dd/aaaa")
					date1 = date1.substring(6,10) + date1.substring(0,2) + date1.substring(3,5);
				if (date2 != "mm/dd/aaaa")
					date2 = date2.substring(6,10) + date2.substring(0,2) + date2.substring(3,5);				 
					if($('iClassificationCode')){_$('iClassificationCode').value="REST";}
					if(_$('iClassificationMode')){_$('iClassificationMode').value="0";}				
			     
			     
				 _$('CurrencyType').value = _$('txtCurrency').value;
				 _$('itmpqrrest').value = "?&Code=LAP&Checkin=" + date1 + "&Checkout=" + date2 + " &loc=La+Paz%2c+Baja+California+Sur%2c+MÉXICO&RefPoint=La+Paz&ClassificationMode=0&ClassificationCode=REST";
				 bk.action = HTTP_RELATIVE_ACTION + "/activity/activitiessearch.aspx";
				break;				
			default:
                

				var urlpk="";
				urlpk =HTTP_RELATIVE_ACTION+"/packages/PackageSearch.aspx?";
				urlpk+="Packages="+searchType;
				urlpk+="&txtDeparture="+$('#Departure').val();
				if(_$('txtCitypk'))
				urlpk+="&txtArrival="+ _$('txtCitypk').value 
                
				urlpk+="&ddHorasalida="+$('#cmbStartTime').val();	
			    urlpk+="&ddhoraregreso="+$('#cmbEndTime').val();	
				urlpk+="&CmbAdultos="+$('#adultsHotel').val();	
				urlpk+="&CmbChildren="+$('#childrenHotel').val();	
				urlpk+="&cmbRooms="+$('#roomsHotel').val();	
				urlpk+="&CurrencyType="+$('#currency').val();	
			  
				var strStart = _$('checkinpk').value;
		    	var strEnd = _$('checkoutpk').value;
		    	//if (strStart.split("/").length>2){strStart = _$('checkin').value.split("/")[2] + _$('checkin').value.split("/")[0] + _$('checkin').value.split("/")[1]}
		    	//if (strEnd.split("/").length>2){strEnd = _$('checkout').value.split("/")[2] + _$('checkout').value.split("/")[0] + _$('checkout').value.split("/")[1]}
				urlpk+="&txtHotF1="+strStart;
				urlpk+="&txtHotF2="+strEnd;
				urlpk+="&booking=1";
				  urlpk += "&idArea=" + _$('txtAreapk').value;
				window.location.href=urlpk;
				return;
				
				break;
            }
			
            bk.submit();
	

        }, false);
		
		
		

    function isMatch(e1,e2)
       {
        try{
            if (e1.toLowerCase().trim()==e2.toLowerCase().trim())
                { return 1;}
        }catch(err){
         if (e1.trim()==e2.trim())
                { return 1;}
        }
        return 0;
      }
        
    
	var locations = "" ;
	$(document).ready(function() {
		
	    $("select#neighborhood").change(function() {
	        //$("select#hotel").hide();
	        var optionsCities = '';
	        var currentCountry = this.value;
	        $.each(locations.area, function(i, itemCountry) {	
	            
	            if (itemCountry.value == currentCountry)	  
	                {optionsCities ='';              
	                locationList = itemCountry.UrlArea.replace('.aspx',listPage);	                
	               /* $.each(itemCountry.city, function(j, itemCity) {	                    
	                    optionsCities += '<option value="' + itemCity.value + '">' + itemCity.text + '</option>';
	                });*/
	             
	               }
	            
	        });
	         //$("select#hotel").html(optionsCities).fadeIn("fast"); 
	           
	    });

		//Show loading.. msg
		$("select#neighborhood").html("<option>Loading...</option>");
		$("select#hotel").html("<option>Loading...</option>");
		$("select#txtAreapk").html("<option>Loading...</option>");
	    //Fill Combo Cities 
		$("#uvselectby").attr("checked", true); 
		
		try{setVarcmd(1);}catch(e){}
		
	    $.getJSON(VarUrlSite+"GetHotels.aspx", function(data) {
	        locations = data;
	        var optionsCountries = '';
	        var optionsHotels = '';
	        $.each(locations.area, function(i, itemCountry) {
	        if(locationList=='')
	        {locationList = itemCountry.UrlArea.replace('.aspx',listPage);	}
	        
	            optionsCountries += '<option value="' + itemCountry.value
	             optionsHotels += '<optgroup label="' + itemCountry.text  + '">;'
	            
	            if (itemCountry.value == "MX")
	                optionsCountries += '" SELECTED >' + itemCountry.text + '</option>';
	            else
	                optionsCountries += '">' + itemCountry.text + '</option>';
	                $.each(itemCountry.city, function(j, itemCity) {
	               
	                        optionsHotels += '<option value="' + itemCity.propertyNumber + '">' + itemCity.text + '</option>';
	                }); 
	                    optionsHotels + '</optgroup>' 
	        });
	        $("select#neighborhood").html(optionsCountries).trigger("change");
	        
	         $("select#hotel").html(optionsHotels);
		 $("select#txtAreapk").html(optionsCountries);
	    });
	});