// Contact Us dropdown

$(document).ready(function() 
{
	$("a#enquiry_anchor")
	.click(function () 
	{	
		$("div#enquiry_slide_down").slideToggle(500);
		$("#enquiry_message_list").html("");
		return false;		
	})
	.mouseover(function ()
	{
		$(this).css('cursor', 'pointer');		
	});
});

///////////////

function add_subscriber_ajax(email)
{
	$.fn.clearForm = function() {
	  return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
		  return $(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
		  this.value = '';
		else if (type == 'checkbox' || type == 'radio')
		  this.checked = false;
		else if (tag == 'select')
		  this.selectedIndex = -1;
	  });
	};

	$.post(
		"/async_call.php",
		{action: "add_subscriber", email: email},
		function(data, textStatus)
		{
			clear_messages();
			$("message",data).each(function(id) {
				   message = $("message",data).get(id);
				   add_message($("title",message).text(), $("description",message).text());
				 }); 

			if ($("title", $("message", data).eq(0)).text() == "Subscribed")
			{
				$("form").filter("#subscriber_form").clearForm();
			}
		},
		"xml"
	);

	return false;
}

function send_enquiry_ajax(first_name, last_name, email, phone, message)
{
	$.fn.clearForm = function() {
	  return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
		  return $(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
		  this.value = '';
		else if (type == 'checkbox' || type == 'radio')
		  this.checked = false;
		else if (tag == 'select')
		  this.selectedIndex = -1;
	  });
	};

	$.post(
		"/async_call.php",
		{action: "send_enquiry", first_name: first_name, last_name: last_name, email: email, phone: phone, message: message},
		function(data, textStatus)
		{
			clear_messages();
			$("message",data).each(function(id) {
				   message = $("message",data).get(id);
				   add_message($("title",message).text(), $("description",message).text());
				 }); 

			if ($("title", $("message", data).eq(0)).text() == "Enquiry Sent")
			{
				try
				{
					_s_action('03');
					pageTracker._trackPageview('/booking-tracking/enquiry_success');
				}
				catch (err)
				{
					//Do nothing
				}
				$("form").filter("#enquiry_html_form").clearForm();
				$("a#enquiry_anchor").click();
			}
		}, 
		"xml"
	);

	return false;
}

// Messages Javascript //

function add_message(title, message)
{
	message_list= get_message_list();
	message_list.prepend("<h6>" + title + "</h6>" + message);
}

function clear_messages()
{
	message_list= get_message_list();
    	message_list.html("");
}

function get_message_list()
{
	if ($("#static_message_list").length > 0)
	{
		message_list= $("#static_message_list");
	}
	else
	{
		message_list= $("#dynamic_message_list");
		$("#dynamic_message_container").show();
	}
	
	return message_list;
}

