var submit_clicked = false;

function check_flights(checked)
{
	if(checked) $("#airport_tr").show();
	else $("#airport_tr").hide();
}

function get_photo(photo_id)
{
	//get next photo
	$.get("/ajax/photos.php", { action: "get_photo", photo: photo_id }, photo_callback, "xml");
}

function photo_callback(data, status)
{
	var id = jQuery('id', data).text();
	var photo = jQuery('photo', data).text();
	var caption = jQuery('caption', data).text();
	var prev = jQuery('prev', data).text();
	var next = jQuery('next', data).text();
	var preload = jQuery('preload', data).text();
	
	var prev_link = (prev != "") ? "javascript:get_photo('" + prev + "')" : '#';
	var next_link = (next != "") ? "javascript:get_photo('" + next + "')" : '#';
	
	//photo id
	$("input#photo_id").val(id);
	
	//preload next image
	var preload_img = new Image();
	preload_img.src = '/images/content/' + preload;
	
	//image
	$("div#image").html('<a href="' + next_link + '"><img src="/images/content/' + photo + '" ' + ' title="' + caption + '" alt="' + caption + '" border="0" /></a>');
	
	//previous link
	if(prev != "") $("div#prev_arrow").html("<a class=\"carousel_previous\" href=\"" + prev_link + "\" title=\"previous\">previous</a>");
	else $("div#prev_arrow").html("<a class=\"carousel_previous_disabled\" title=\"previous\">previous</a>");
	
	//next link
	if(next != "") $("div#next_arrow").html("<a class=\"carousel_next\" href=\"" + next_link + "\" title=\"next\">next</a>");
	else $("div#next_arrow").html("<a class=\"carousel_next_disabled\" title=\"next\">next</a>");
}

function quote_callback(data, status)
{
	if(data.substr(0,4) == 'FAIL')
	{
		$("span#fail_reason").html(data.substr(5));
		quote_step(2,4);
		submit_clicked = false;
	}
	else
	{
		$("span#refcode").html(data);
		quote_step(2,3);
	}
}

function quote_step(step_out, step_in)
{
	$("#quote_step"+step_out).fadeOut('normal', function() { $("#quote_step"+step_in).fadeIn(); } );
}

function remove_content(id)
{
	if(confirm('Are you sure you want to remove this content?\nAll information and photos will be permanently deleted.'))
	{
		document.location = '/content_update.php?action=remove&id=' + id;
	}
}

function remove_photo()
{
	var id = $("input#photo_id").val();
	
	if(confirm('Are you sure you want to remove this photo?'))
	{
		document.location = '/photo_delete.php?id=' + id;
	}
}

function submit_quote()
{
	if(submit_clicked) return false;
	
	var from_date = $("#from_date").val();
	var to_date = $("#to_date").val();
	var contact_name = $("#contact_name").val();
	var contact_number = $("#contact_number").val();
	var email = $("#email").val();
	var confirm_email = $("#confirm_email").val();
	
	if(from_date == '' || from_date == 'dd/mm/yyyy')
	{
		alert('Please select a from date');
		return false;
	}
	else if(to_date == '' || to_date == 'dd/mm/yyyy')
	{
		alert('Please select a to date');
		return false;
	}
	else if(contact_name == '')
	{
		alert('Please enter a contact name');
		return false;
	}
	else if(contact_number == '')
	{
		alert('Please enter a contact number');
		return false;
	}
	else if(email == '')
	{
		alert('Please enter an email address');
		return false;
	}
	else if(confirm_email == '')
	{
		alert('Please confirm your email address');
		return false;
	}
	else if(confirm_email != email)
	{
		alert('Email addresses do not match');
		return false;
	}
	
	submit_clicked = true;
	return true;
}

function submit_quickquote()
{
	if(submit_clicked) return;
	
	var from_date = $("#from_date").val();
	var to_date = $("#to_date").val();
	var contact_name = $("#contact_name").val();
	var contact_number = $("#contact_number").val();
	var email = $("#email").val();
	var confirm_email = $("#confirm_email").val();
	
	if(from_date == '' || from_date == 'dd/mm/yyyy')
	{
		alert('Please select a from date');
		return;
	}
	else if(to_date == '' || to_date == 'dd/mm/yyyy')
	{
		alert('Please select a to date');
		return;
	}
	else if(contact_name == '')
	{
		alert('Please enter a contact name');
		return;
	}
	else if(contact_number == '')
	{
		alert('Please enter a contact number');
		return;
	}
	else if(email == '')
	{
		alert('Please enter an email address');
		return;
	}
	else if(confirm_email == '')
	{
		alert('Please confirm your email address');
		return false;
	}
	else if(confirm_email != email)
	{
		alert('Email addresses do not match');
		return false;
	}
	
	//submit form
	submit_clicked = true;
	var data = [];
	
	$('#quickquoteform :input').each(function() {
		if((this.type == 'checkbox' && this.checked) || (this.type != 'checkbox' && this.name != ''))
			data.push(this.name + '=' + escape(this.value));
	});
	
	$.post('/quote.php', data.join('&'), quote_callback);
}

function toggle_panel(panel)
{
	$("#"+panel+"_panel").slideToggle("normal");
	$("h3#"+panel+"_drawer").toggleClass("open");
}