function get_arrivals(d)  

 {  

	var dep_date = $('[name="abook[departure_date]"]');

	var ret_date = $('[name="abook[return_date]"]');

	var dep_time = $('[name="abook[departure_time]"]');

	var ret_time = $('[name="abook[return_time]"]');

	

	$(dep_date).val('');

	$(ret_date).val('');

	$(dep_time).val('');

	$(ret_time).val('');

	$('#persons_list .person_price').html('0.00');
	$('#persons_list input[name="person[][price]"]').val('0.00');
	$('#persons_list input[name="person[][price1]"]').val('0.00');
	$('#persons_list input[name="person[][price2]"]').val('0.00');
	$('#persons_list input[name="person[][seat_discount]"]').val('0.00');
	$('#persons_list input[name="person[][discount1]"]').val('0.00');
	$('#persons_list input[name="person[][discount2]"]').val('0.00');
	

	//alert('ajax/?action=get_arrivals&d='+d);
	apply_mask();
		 $.ajax({  

	         type: "GET",  

	         dataType: "json",  

	         cache: false,  

	         url: 'http://www.gobus.ie/ajax/index.php?action=get_arrivals&d='+d,  

			
  

	         error: function (XMLHttpRequest, textStatus, errorThrown) {

				  // typically only one of textStatus or errorThrown 

				  // will have info

				  alert(XMLHttpRequest+' '+textStatus + ' ' +errorThrown);
					hide_mask();
				  this; // the options for this ajax request

				},  

	         success: function(data) {   

	             // Clear  

				//alert(data);

				$("select[name*=arrival] option").remove();  

	              

				 $.each(data, function(i, j){  

					if (j.value!='undefined'){

		                var row = "<option value=\"" +  j.value +  "\">" +  j.text +  "</option>";  

						

		                $(row).appendTo("select[name*=arrival]");  

					}

	             });  

				hide_mask();

				 

	         }  

	     });  

	

 } 

 

 

 function change_route(el){

	$('.times table').removeClass('active');

	$('#times'+$(el).attr('rel')).addClass('active');

	$('h2').html($(el).html());

	$('#routes').hide();

	$('.times').css('background-position','0 27px');

 }



 

 function resize_iAuth(w,h){

	$('#fancybox-content').height(h);

	$('#fancybox-wrap').height(parseInt(h)+20);

	$.fancybox.center();

}















 

 // AGENTS

 

function set_travelpass(p_id){

	var total_discount = 0.00;
	var total_discount_return = 0.00;
	

	if ($('input[name="person['+p_id+'][travel_pass]"]').val().length == 10 || $('input[name="person['+p_id+'][travel_pass]"]').val().length == 11){

		$('#p'+p_id).find('span.price').html('Price: &euro; 0.00');

		$('#p'+p_id).find('input[name*=discount1]').val($('#p'+p_id).find('input[name*="price1"]').val());

		$('#p'+p_id).find('input[name*=discount2]').val($('#p'+p_id).find('input[name*="price2"]').val());

	}

	else {

		$('#p'+p_id).find('span.price').html('Price: &euro; '+$('#p'+p_id).find('input[name="price[]"]').val());

		$('#p'+p_id).find('input[name*=discount1]').val('0');
		$('#p'+p_id).find('input[name*=discount2]').val('0');

	}

	
	

	

	$('.ptype7').each(function(){

		$(this).find('span.price').html('Price: &euro; '+$(this).find('input[name="price[]"]').val());

		$(this).find('input[name*=discount1]').val('0');
		$(this).find('input[name*=discount2]').val('0');

	});

	

	$('.ptype4').each(function(){

		if ($(this).find('span.price').html().indexOf(' 0.00')>-1){

			var skip=0;

			

			$('.ptype7').each(function(){

				if (skip==0 && $(this).find('span.price').html().indexOf(' 0.00') == -1){

					$(this).find('span.price').html('Price: &euro; 0.00');

					$(this).find('input[name*=discount1]').val($(this).find('input[name*="price1"]').val());

					$(this).find('input[name*=discount2]').val($(this).find('input[name*="price2"]').val());

					
					skip=1;

				}

			});

		}

	});
	
	//TOTALS
		
		$('.p_details').each(function(){
			if (parseFloat($(this).find('input[name*=discount1]').val())>0){
				total_discount += parseFloat($(this).find('input[name*=discount1]').val());
			}
		});
	
	
		$('.p_details').each(function(){
			if (parseFloat($(this).find('input[name*=discount2]').val())>0){
				total_discount_return += parseFloat($(this).find('input[name*=discount2]').val());
			}
		});
	
	
	$('input[name="trip[discount]"]').val(total_discount);
	$('input[name="trip_return[discount]"]').val(total_discount_return);
	//TOTALS
	
	//alert($('#total').html());
	$('#total').html((parseFloat($('input[name="trip[price]"]').val()) + parseFloat($('input[name="trip_return[price]"]').val())) - (parseFloat(total_discount) + parseFloat(total_discount_return)));

}

 

 function agent_get_times(return_only)  

 {  

	if (!return_only)

		return_only = 0;

	

	

	var from = $('[name="abook[departure]"]');

	var to = $('[name="abook[arrival]"]');

	var dep_date = $('[name="abook[departure_date]"]');
	
	var ret_date = $('[name="abook[return_date]"]');

	var dep_time = $('[name="abook[departure_time]"]');

	var ret_time = $('[name="abook[return_time]"]');

	
	$('#persons_list .person_price').html('0.00');
	$('#persons_list input[name="person[][price]"]').val('0.00');
	$('#persons_list input[name="person[][price1]"]').val('0.00');
	$('#persons_list input[name="person[][price2]"]').val('0.00');
	$('#persons_list input[name="person[][seat_discount]"]').val('0.00');
	$('#persons_list input[name="person[][discount1]"]').val('0.00');
	$('#persons_list input[name="person[][discount2]"]').val('0.00');

	//alert(return_only);

	if (return_only==0)

		$(dep_time).val('');

		

	$(ret_time).val('');

	

	

	if ($(from).val()=="" || $(to).val()=="" || $(dep_date).val()==""){

		$(dep_time).val('');

		return false;

	}

	

	

	if (return_only==0)

		$('select[name="abook[departure_time]"] option').remove(); 

		

	$('select[name="abook[return_time]"] option').remove();

	

	if (return_only==0)
		$('#prices_info').html('');
	//alert('http://www.gobus.ie/ajax/index.php?action=get_times&departure='+$(from).val()+'&arrival='+$(to).val()+'&departure_date='+$(dep_date).val()+'&return_date='+$(ret_date).val()+'&departure_time='+$(dep_time).find('option:selected').html()+'&return_only='+return_only);
	apply_mask();
		 $.ajax({  

	         type: "GET",  

	         dataType: "json",  

	         cache: false,  

	         url: 'http://www.gobus.ie/ajax/index.php?action=get_times&departure='+$(from).val()+'&arrival='+$(to).val()+'&departure_date='+$(dep_date).val()+'&return_date='+$(ret_date).val()+'&departure_time='+$(dep_time).find('option:selected').html()+'&return_only='+return_only,  

			  

	         error: function (XMLHttpRequest, textStatus, errorThrown) {

				  // typically only one of textStatus or errorThrown 

				  // will have info

				  alert(XMLHttpRequest+' '+textStatus + ' ' +errorThrown);
					hide_mask();
				  this; // the options for this ajax request

				},  

	         success: function(data) {   

	             // Clear  

				//alert(data);

				

				 

				

				$.each(data, function(i, j){  

					if (j.value!='undefined'){

						var row = "<option value=\"" +  j.value +  "\">" +  j.text +  "</option>";  

						

						if (j.rel=="trip1" && return_only==0)

							$(row).appendTo('select[name="abook[departure_time]"]');  

						if (j.rel=="trip2")

							$(row).appendTo('select[name="abook[return_time]"]'); 

						
						//alert(j.prices);
						if (return_only==0)
							$('#prices_info').append(j.prices);
						
					}

				 });  

				hide_mask();
			 }
	 

	           

	     });  

	

 } 
 
 
 function ticket_get_times(fld)  

 {  

	
	var from = $('[name="departure"]');

	var to = $('[name="arrival"]');

	var dep_date = $('[name="departure_date"]');

	var ret_date = $('[name="return_date"]');

	var dep_time = $('[name="'+fld+'"]');


	$('select[name="'+fld+'"] option').remove();

	

	//alert('http://www.gobus.ie/ajax/index.php?action=get_times&departure='+$(from).val()+'&arrival='+$(to).val()+'&departure_date='+$(dep_date).val()+'&return_date='+$(ret_date).val());
	apply_mask();
		 $.ajax({  

	         type: "GET",  

	         dataType: "json",  

	         cache: false,  

	         url: 'http://www.gobus.ie/ajax/index.php?action=get_times&departure='+$(from).val()+'&arrival='+$(to).val()+'&departure_date='+$(dep_date).val()+'&return_date='+$(ret_date).val(),  

			  

	         error: function (XMLHttpRequest, textStatus, errorThrown) {

				  // typically only one of textStatus or errorThrown 

				  // will have info

				  alert(XMLHttpRequest+' '+textStatus + ' ' +errorThrown);
					hide_mask();
				  this; // the options for this ajax request

				},  

	         success: function(data) {   

	             // Clear  

				//alert(data);

				$.each(data, function(i, j){  
					
					if (j.value!='undefined'){

						var row = "<option value=\"" +  j.value +  "\">" +  j.text +  "</option>";  

						if (j.rel=="trip1" && fld=='departure_time')
							$(row).appendTo('select[name="'+fld+'"]');  

						if (j.rel=="trip2" && fld=='return_time')
							$(row).appendTo('select[name="'+fld+'"]');  

						
					}

				 });  

				hide_mask();
			 }
	 

	           

	     });  

	

 } 
 
 function get_opentimes(el)  

 {  
	var departure_date = $(el).val();
	var order_trip_id = $(el).parent().find('input[name=order_trip_id]').val();
	var hash = $(el).parent().find('input[name=hash]').val();
	
	
	$('select[name="departure_time"] option').remove();

	//alert('http://www.gobus.ie/ajax/index.php?action=get_opentimes&order_trip_id='+order_trip_id+'&departure_date='+departure_date+'&hash='+hash);
	apply_mask();
		 $.ajax({  

	         type: "GET",  

	         dataType: "json",  

	         cache: false,  

	         url: 'http://www.gobus.ie/ajax/index.php?action=get_opentimes&order_trip_id='+order_trip_id+'&departure_date='+departure_date+'&hash='+hash,  

			  

	         error: function (XMLHttpRequest, textStatus, errorThrown) {

				  // typically only one of textStatus or errorThrown 

				  // will have info

				  alert(XMLHttpRequest+' '+textStatus + ' ' +errorThrown);
					hide_mask();
				  this; // the options for this ajax request

				},  

	         success: function(data) {   

	             // Clear  

				//alert(data);

				

				$.each(data, function(i, j){  

					if (j.value!='undefined'){

						var row = "<option value=\"" +  j.value +  "\">" +  j.text +  "</option>";  

						$(row).appendTo('select[name="departure_time"]');  

					}

				 });  

				hide_mask();
			 }
	 

	           

	     });  

	

 } 

 function set_opentime(el)  

 {  
	
	var order_trip_id = $(el).parent().find('input[name=order_trip_id]').val();
	var trip_id = $(el).parent().find('select[name=departure_time]').val();
	var hash = $(el).parent().find('input[name=hash]').val();
	
	
	
	//alert('http://www.gobus.ie/ajax/index.php?action=set_opentime&order_trip_id='+order_trip_id+'&trip_id='+trip_id+'&hash='+hash);
	apply_mask();
		 $.ajax({  

	         type: "GET",  

	         dataType: "html",  

	         cache: false,  

	         url: 'http://www.gobus.ie/ajax/index.php?action=set_opentime&order_trip_id='+order_trip_id+'&trip_id='+trip_id+'&hash='+hash,  

			  

	         error: function (XMLHttpRequest, textStatus, errorThrown) {

				  // typically only one of textStatus or errorThrown 

				  // will have info

				  alert(XMLHttpRequest+' '+textStatus + ' ' +errorThrown);
					hide_mask();
				  this; // the options for this ajax request

				},  

	         success: function(data) {   

	             // Clear  

				//alert(data);
				result = data.split("|");
				if (result[0]=="done"){
					$(el).parent().parent().parent().find('td').css({'background-color':'#d7e5f7'});
					$(el).parent().parent().html(result[1]);
				}

				
				hide_mask();
			 }
	 

	           

	     });  

	

 } 

 

 

 

 function validate_agent_b1(){

	if ($('select[name="abook[departure]"]').val()==''){

		alert('Select Departing From!');

		return false;

	}

	if ($('select[name="abook[arrival]"]').val()==''){

		alert('Select Destination!');

		return false;

	}

	

	if ($('input[name="abook[departure_date]"]').val()==''){

		alert('Select your date!');

		return false;

	}

	if ($('select[name="abook[departure_time]"]').val()==''){

		alert('Select time!');

		return false;

	}

	if ($('input[name="abook[return]"]').is(':checked') && ($('input[name="abook[return_date]"]').val()=='' || $('select[name="abook[return_time]"]').val()=='')){

		alert('Select return date/time!');

		return false;

	}

	
	
	if ($('select[name="abook[quantity][6]"]').val()>0 && ($('select[name="abook[quantity][1]"]').val()==0 || $('select[name="abook[quantity][1]"]').val()=='')){
		alert("Infant allowed only with adults! One infant per adult.");

		return false;
	}
	
	if ($('select[name="abook[quantity][6]"]').val()>$('select[name="abook[quantity][1]"]').val()){
		
		alert("Only one infant allowed per adult!");

		return false;

	}
	
	
	
	var seats = 10000;
	$('.available_num').each(function(){
			if ($(this).html()=="") $(this).html('0');
			
			seats = Math.min(seats,parseInt($(this).html()));
		});
	if (seats==10000) seats=0;
	

	
	var selected_tickets = 0;
	var m = 1;
	$('select[name^="abook[quantity]"]').each(

		function(){
			m=1;
			var name = $(this).attr("name")
			//alert(name+' '+name.indexOf('[quantity][5]'));
			if ( name.indexOf('[quantity][5]')!=-1 ) m=4;
			if ( name.indexOf('[quantity][6]')!=-1 ) m=0;
			
			selected_tickets += m * parseInt($(this).find('option:selected').val());

		}

	);
	if (selected_tickets==0) {

		alert("Select some tickets!");

		return false;

	}
	
	
	
	if (selected_tickets>seats) {

		alert("Not enough available seats for your booking!");

		return false;

	}

	

	return true;

 }

 
function agent_check_seats(trip_id,is_return){


if (!trip_id || trip_id==''){
	var msg = $('p.error').html().split('<br>');
	if (is_return==1){
		$('p.error').html(msg[0]+'<br>');
	}
	else
	{
		$('p.error').html('<br>'+msg[1]);
	}
	return;
}
	//alert('http://www.gobus.ie/ajax/index.php?action=agents_check_seats&trip_id'+trip_id);


	apply_mask();
	 $.ajax({  

		 type: "GET",  

		 dataType: "html",  

		 cache: false,  

		 url: 'http://www.gobus.ie/ajax/index.php?action=agents_check_seats&trip_id='+trip_id,  

		 

		 error: function (XMLHttpRequest, textStatus, errorThrown) {

			  // typically only one of textStatus or errorThrown 

			  // will have info

			  alert(XMLHttpRequest+' '+textStatus + ' ' +errorThrown);
				hide_mask();
			  this; // the options for this ajax request

			},  

		 success: function(data) {   

			 // Clear  

			//alert(data);
			data = data.split("||");
			

			if (data[0] == "done"){
				
				if ( $.browser.msie ) 
					var msg = $('p.error').html().split('<BR>');
				else
					var msg = $('p.error').html().split('<br>');
				
				if (is_return==1){
					$('p.error').html(msg[0]+'<br>' + data[2]).show();
				}
				else
					$('p.error').html(data[2]+'<br>'+((msg[1])?msg[1]:'')).show();
					
				if (data[1]==2)	{
					$('#persons_list').hide();
					if (is_return!=1){
						$('input[name="abook[return]"]').attr('checked',false).attr('disabled',true);
						$('#return').hide();$('#return :input').val('');
					}
				}
				if (data[1]==1)	{
					$('#persons_list').show();
					$('input[name="abook[return]"]').attr('disabled',false);
				}
				
				

			}
			else 
			{
				alert("There was an error. Please refresh and try again.");
				$('#persons_list').hide();
			}
			hide_mask();
		 }

			 

		   

	 });  

	
}


 

 function validate_agent_b2(){
	
	if ($('input[name="contact_name"]').val()==''){

		alert('Contact Name is required');

		return false;

	}

	if ($('input[name="contact_email"]').val()==''){

		alert('Contact Email Required!');

		return false;

	}
	if ($('input[name="contact_phone"]').val()==''){

		alert('Contact Phone Required!');

		return false;

	}

	
	
 return true;

 }
 
function get_prices_info(){
	var trip_id = $('select[name="abook[departure_time]"]').val();
	var trip_return_id = $('select[name="abook[return_time]"]').val();
	
	$('#prices_info table').hide();
	$('#t'+trip_id).show();
	
	
}
 
 
  function apply_mask(){
	$('#mask').css({'width':$(document).width(),'height':$(document).height()}).show();
 }
 function hide_mask(){
	$('#mask').hide();
 }
 
 function reload_page(url){
	if (url)
		window.location =url;
	else
		window.location.reload();
 }
 

