var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-23559357-2']);
_gaq.push(['_trackPageview', pageNameVar]);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

$('a[rel*=facebox]').facebox();

$(document).bind('beforeReveal.facebox', function() {
  var height = $(window).height() - 100;
  $('#facebox .content').css('height', height + 'px');
  $('#facebox').css('top', ($(window).scrollTop() + 10) + 'px');
});

$('#facebox .close_image').live({ 
	  click: function() {
		  $(document).trigger('close.facebox')
	  }
});  

$(document).ready(function() { 
	$("select, input, input:text, input:radio").uniform();	
	$('.searchLink').click(function(){;
		var thisId = $(this).attr('id'); 
		if(thisId.substr(0,5) == 'puApo' && $('#puAdd').is(':visible')){
			$('#puAdd').css('display','none');			
			$('#puApo').css('display','block');
			$(this).removeClass('searchCategoryOff').addClass('searchCategoryOn');
			$('#puAddLink').removeClass('searchCategoryOn').addClass('searchCategoryOff');
			$('#pickupLocationTracker').val('airport');
		}else if(thisId.substr(0,5) == 'puAdd' && $('#puApo').is(':visible')){
			$('#puApo').css('display','none');			
			$('#puAdd').css('display','block'); 
			$(this).removeClass('searchCategoryOff').addClass('searchCategoryOn'); 
			$('#puApoLink').removeClass('searchCategoryOn').addClass('searchCategoryOff');
			$('#pickupLocationTracker').val('address');  
		}else if(thisId.substr(0,5) == 'doAdd' && $('#doApo').is(':visible')){
			$('#doApo').css('display','none');			
			$('#doAdd').css('display','block');
			$(this).removeClass('searchCategoryOff').addClass('searchCategoryOn');
			$('#doApoLink').removeClass('searchCategoryOn').addClass('searchCategoryOff');
			$('#dropoffLocationTracker').val('address');   
		}else if(thisId.substr(0,5) == 'doApo' && $('#doAdd').is(':visible')){
			$('#doAdd').css('display','none');			
			$('#doApo').css('display','block');
			$(this).removeClass('searchCategoryOff').addClass('searchCategoryOn'); 
			$('#doAddLink').removeClass('searchCategoryOn').addClass('searchCategoryOff'); 
			$('#dropoffLocationTracker').val('airport'); 
		}
	});
	$('#puAdd').hide();	
	$('#emailContactsYes').click(function() {
		$('.additionalEmail').css('display','block');  
	});	
	$('#emailContactsNo').click(function() {
		$('.additionalEmail').css('display','none');  
	});	
	$("#pickupAirportCode").autocomplete({
		minLength: 2,
		suggLimit: 10,
		source: availableTags,
		caller: 'pickupAirportCodeUIAC',
		close: function(event, ui) {
			  if($('.pickupAirportCodeUIAC li').size() == 1){
				$(this).val($('.pickupAirportCodeUIAC li').text());	
			  }
		},
		change: function(event, ui) {
				// provide must match checking if what is in the input
				// is in the list of results. HACK!
				var source = $(this).val();
				var found = $('.pickupAirportCodeUIAC li').text().search(source);
				//console.debug('found:' + found);
				if(found < 0) {
					$(this).val('');
				}
			}	   
	});	
	$("#dropoffAirportCode").autocomplete({
		minLength: 2,
		suggLimit: 10,
		source: availableTags,
		caller: 'dropoffAirportCodeUIAC',
		close: function(event, ui) {
			  if($('.dropoffAirportCodeUIAC li').size() == 1){
				$(this).val($('.dropoffAirportCodeUIAC li').text());	
			  }
		},
		change: function(event, ui) {
				// provide must match checking if what is in the input
				// is in the list of results. HACK!
				var source = $(this).val();
				var found = $('.dropoffAirportCodeUIAC li').text().search(source);
				//console.debug('found:' + found);
				if(found < 0) {
					$(this).val('');
				}
			}	   
	});
	$('#pickupDate').datepicker({
		numberOfMonths: [1, 2],
		minDate: '0d',
		maxDate: '1y',
		showButtonPanel: true,
		closeText: 'X'
	});
	
	$('#dropoffAddressCountry').change(function(){
		if($(this).val() != "US" && $(this).val() != "CA"){
			$('#dropoffStateRow').hide();	
		}else{
			$('#dropoffStateRow').show();
		}		  
	});
	
	$('#pickupAddressCountry').change(function(){	
		if($(this).val() != "US" && $(this).val() != "CA"){
			$('#pickupStateRow').hide();	
		}else{
			$('#pickupStateRow').show();
		}		  
	});	
	
	var pickupAddressAddress = $('#pickupAddressAddress').val(); 
	var pickupAddressCity = $('#pickupAddressCity').val();
	var pickupAddressZipCode = $('#pickupAddressZipCode').val();
	var dropoffAirportCode = $('#tagsB').val(); 
	//alert(pickupAddressZipCode)
   if(pickupAddressAddress != '' || pickupAddressCity != '' || pickupAddressZipCode != ''){
	   $('#pickupLocationTracker').val('address');
	   $("#puApo").hide();
	   $("#puAdd").show();
	   $('#puAddLink').removeClass('searchCategoryOff').addClass('searchCategoryOn');
	   $('#puApoLink').removeClass('searchCategoryOn').addClass('searchCategoryOff'); 
		if($('#pickupAddressCountry').val() != "US" && $('#pickupAddressCountry').val() != "CA"){
			$('#pickupStateRow').hide();	
		}else{
			$('#pickupStateRow').show();
		}		 
  }else{
	   $('#pickupLocationTracker').val('airport');
	   $("#puApo").show();
	   $("#puAdd").hide(); 
	   $('#puApoLink').removeClass('searchCategoryOff').addClass('searchCategoryOn');
	   $('#puAddLink').removeClass('searchCategoryOn').addClass('searchCategoryOff');   
   }
   if(dropoffAirportCode == ''){
	   $('#dropoffLocationTracker').val('address');
	   $("#doApo").hide();
	   $("#doAdd").show();  
	   $('#doAddLink').removeClass('searchCategoryOff').addClass('searchCategoryOn');
	   $('#doApoLink').removeClass('searchCategoryOn').addClass('searchCategoryOff');
		if($('#dropoffAddressCountry').val() != "US" && $('#dropoffAddressCountry').val() != "CA"){
			$('#dropoffStateRow').hide();	
		}else{
			$('#dropoffStateRow').show();
		}
   }else{
	   $('#dropoffLocationTracker').val('airport');
	   $("#doApo").show();
	   $("#doAdd").hide();   
	   $('#doApoLink').removeClass('searchCategoryOff').addClass('searchCategoryOn');
	   $('#doAddLink').removeClass('searchCategoryOn').addClass('searchCategoryOff');  
   }
   
   if($('#pickupDate').val() == '') $('#pickupDate').val(pickUpDateVal);  
   
	$('#cancelReservationSubmit').live({ 
		click: function() {
			   var cancelRE = new RegExp("\^\[a\-zA\-Z\]\{2\}\[0\-9\]\{7\}\-\[0\-9\]\$");
			   var lastNameRE = new RegExp("\^\[a\-zA\-Z\s\'\-\]\+\$");
			   var retrieveConfirmationNumber = $('#retrieveConfirmationNumber').val();
			   var lastName = $('#retrieveLastName').val();
				if(retrieveConfirmationNumber.length == 0 || lastName.length == 0){
					   $('div.errorContainer').show();
					   $('.errorMsg').html('Please provide a Last Name and Confirmation Number');   
					return false;
				}else if(!lastName.match(lastNameRE)){
					   $('div.errorContainer').show();
					   $('.errorMsg').html('Invalid Last Name');   
					return false;
				}else if(!retrieveConfirmationNumber.match(cancelRE)){
					   $('div.errorContainer').show();
					   $('.errorMsg').html('Invalid Confirmation Number');   
					return false;
				}else{
					$('div.errorContainer').hide();				
					$( "#dialog-confirm" ).dialog('open');
				}
		}
	});	  
   
						  
	
	$('#facebox .cancelKeep').live({ 
		click: function() {									
			$(document).trigger('close.facebox')
		}
	});	 
	$('#facebox .close_image').live({ 
		click: function() {
			$(document).trigger('close.facebox')
		}
	});	  
   
$( "#dialog-confirm" ).dialog({ 
			autoOpen: false,
			resizable: false,
			width: 500,
			modal: true,
			buttons: {  
				"images/cancelRes.png": function() {   
							$.ajax({
						   type: "POST",
						   url:  "/rp/",
						   data: 'retrieveConfirmationNumber='+$('#retrieveConfirmationNumber').val()+'&retrieveLastName=xx',
						   success: function(msg){ //console.log(msg);
								var obj = jQuery.parseJSON(msg);
								if(obj.tag === "error"){
									//location.href = obj.url;
									var errorMessage = '<div style="clear: both; padding: 15px 0pt; font-size: 1em;" class="errorContainer"><div style="height: 48px;" class="errorIcon"></div>There were errors with your request <br> '+obj.message+'<span style="color: rgb(255, 0, 0);" class="errorMsg"></span></div>'
									$('#cancelConfirm').html(errorMessage);
								}else if(obj.tag === "success"){
									var successMessage = "<span class=estimatedTotal>Your reservation has been cancelled</span><p>The reservation with the confirmation number: <b>"+obj.tripNumber[0]+"</b> has been cancelled.<p>Your cancellation number for reference is: <b>"+obj.cancellationNumber[0]+"</b><p>";
									$('#cancelConfirm').html(successMessage);
									$('.ui-dialog-buttonpane').hide();
								}

						   }});

				},
				"images/keepRes.png": function() {
					$( this ).dialog( "close" );
				}
			}
   });
 
					   
	$( "#dialog-confirm" ).bind( "dialogclose", function(event, ui) {
		$( "#cancelConfirm" ).html('<span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>Are you sure you wish to cancel this reservation?');
	});
});
