$(document).ready(function(){
	var so = new SWFObject("/designs/ascot/flash/flash.swf", "flash", "699", "267", "8", "");
	so.addParam("wmode","transparent");
	so.write("flash");
	
	rollovers();
	
	loadMap();
});

function rollovers()
{
	$(".rollover").hover(
		function () {
			imgSrc = $(this).attr("src");
			newImgSrc = imgSrc.replace("_0","_1");
			
			$(this).attr("src",newImgSrc);
		}, 
		function () {
			imgSrc = $(this).attr("src");
			imgAlt = $(this).attr("alt");
			
			if(imgAlt != "on")
			{
				newImgSrc = imgSrc.replace("_1","_0");
			
				$(this).attr("src",newImgSrc);
			}
		}
	);
}

function submitContactForm()
{
	yourname = $("input[name='Name']");
	number = $("input[name='Contact_Number']");
	email = $("input[name='Email']");
	message = $("textarea[name='Message']");
	
	if(yourname.val() == "")
	{
		alert("Please enter your name.");
		yourname.focus();
	}
	else if(email.val() == "")
	{
		alert("Please enter your email address.");
		email.focus();
	}
	else if(!isEmail(email.val()))
	{
		alert("Please enter in a valid email address.");
		email.focus();
	}
	else if(message.val() == "")
	{
		alert("Please enter in a message.");
		message.focus();
	}
	else
	{
		$('div#register').html("<p><b>Thank you</b> for contacting us.<br /><br />We will be in contact shortly.</p>");

		$.post("/mod/cms/public/contact", { Name: yourname.val(), Email: email.val(), Phone_Number: number.val(), Message: message.val()},
		  function(data){
		  	
		  });
	}
	
	return false;
}

function submitBoatForm()
{
	yourname = $("input[name='Name']");
	number = $("input[name='Contact_Number']");
	email = $("input[name='Email']");
	own_a_boat = $("input[name='Own_a_boat']:checked");
	buying_a_boat = $("input[name='Buying_a_boat']:checked");
	boat_size = $("input[name='Size_of_boat']");
	new_boat_size = $("input[name='Size_boat_will_be']");
	boat_moored = $("input[name='Where_boat_is_moored']");
	boat_delivered = $("input[name='When_boat_will_be_delivered']");
	
	if(yourname.val() == "")
	{
		alert("Please enter your name.");
		yourname.focus();
	}
	else if(email.val() == "")
	{
		alert("Please enter your email address.");
		email.focus();
	}
	else if(!isEmail(email.val()))
	{
		alert("Please enter in a valid email address.");
		email.focus();
	}
	else
	{
		$('div#register').html("<p><b>Thank you</b> for registering your interest in securing a boat pen at Ascot Waters Marina.<br /><br />We will be in contact shortly.</p>");
		$('div#register').css('height','10px');

		$.post("/mod/cms/public/boatpens", { Name: yourname.val(), Email: email.val(), Phone_Number: number.val(), Own_a_boat: own_a_boat.val(), Buying_boat: buying_a_boat.val(), Boat_size: boat_size.val(), Boat_size_will_be: new_boat_size.val(), Currently_moored_at: boat_moored.val(), New_boat_delivered_on:boat_delivered.val()},
		  function(data){
		  	//alert(data);
		  });
	}
	
	return false;
}

function submitRegisterForm()
{
	firstname = $("input[name='First_Name']");
	surname = $("input[name='Surname']");
	email = $("input[name='Email']");
	address = $("input[name='Address']");
	suburb = $("input[name='Suburb']");
	state = $("input[name='State']");
	postcode = $("input[name='Postcode']");
	phone = $("input[name='Phone']");
	fax = $("input[name='Fax']");
	how = $("select[name='How_did_you_hear_about_us']");
	area = $("input[name='area']");
	
	if(firstname.val() == "")
	{
		alert("Please enter your first name.");
		firstname.focus();
	}
	if(surname.val() == "")
	{
		alert("Please enter your surname.");
		surname.focus();
	}
	else if(email.val() == "")
	{
		alert("Please enter your email address.");
		email.focus();
	}
	else if(address.val() == "")
	{
		alert("Please enter your address.");
		address.focus();
	}
	else
	{
		$('div#copy').html("<b>Thank you</b> for registering your interest with us.<br /><br />We will be in contact shortly.");

		$.post("/mod/cms/public/register", { Firstname: firstname.val(), Surname: surname.val(), Email: email.val(), Address: address.val(), Suburb: suburb.val(), State: state.val(), Postcode: postcode.val(), Phone: phone.val(), Fax: fax.val(), How : how.val(), Area : area.val() },
		  function(data){
		  	
		  });
	}
	
	return false;
}

function submitCafeForm()
{
	firstname = $("input[name='First_Name']");
	surname = $("input[name='Surname']");
	email = $("input[name='Email']");
	phone = $("input[name='Phone']");
	message = $("textarea[name='message']");
	
	if(firstname.val() == "")
	{
		alert("Please enter your first name.");
		firstname.focus();
	}
	if(surname.val() == "")
	{
		alert("Please enter your surname.");
		surname.focus();
	}
	else if(email.val() == "")
	{
		alert("Please enter your email address.");
		email.focus();
	}
	else
	{
		$('div#register').html("<b>Thank you</b> for registering your interest with us.<br /><br />We will be in contact shortly.<br /><br /><br /><br /><br /><br />");

		$.post("/mod/cms/public/cafe", { Firstname: firstname.val(), Surname: surname.val(), Email: email.val(), Phone: phone.val(), Message : message.val() },
		  function(data){
		  	
		  });
	}
	
	return false;
}


function loadMap()
{
	if($('div#gmap').length)
	{
		if (GBrowserIsCompatible())
		{
			var map = new GMap2(document.getElementById("gmap"));
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng(-31.938573, 115.917814), 16);
			map.setMapType(G_NORMAL_MAP);
			map.setZoom(16);
			var center = new GLatLng(-31.938573, 115.917814);
			var marker = new GMarker(center);
			map.addOverlay(marker);
			
			//http://maps.google.com/maps?f=q&hl=en&geocode=&q=196+Abernethy+Rd,+Belmont,+WA+6104&ie=UTF8&ll=-31.958179,115.930427&spn=0.007828,0.016522&t=h&z=17
		}
		
		//$('div#gmap').hide();
	}
}

function showGMap()
{
	hideEl = $('img#nmap');

	hideEl.css("border","1px solid blue");
	hideEl.fadeOut('medium',function() { $('div#gmap').fadeIn();});
}

function isEmail(str)
{
	if(str == "") return false;

	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i

	return re.test(str);
}