// JavaScript Document
$(document).ready(function () {
	$('div.panel-contents').fadeOut(1);
	// click to draw contact form
	$('.openForm').click(function () {
		$("div.formpanel").animate({
			height: "230px"
		}).animate({
			height: "205px"
		}, 500, function () {
			$("div.panel-contents").fadeIn(500);
		});
		$('.openForm').fadeOut(500);
		$('.close').delay(500).fadeIn(500);
	});
	function closePanels() {
		$("div.formpanel").animate({
			height: "230px"
		}).animate({
			height: "0px",
			top: "0px"
		}, "slow", function () {
			$(this).hide();
		});
	}
	$('.close').click(function () {
		$('div.panel-contents').fadeOut("slow", closePanels());
		$('.close').fadeOut(500);
		$('.openForm').delay(500).fadeIn(500);
	});
	$('#contactForm #submit').click(function () {
		// Fade in the progress bar
		$('#contactForm #formProgress').hide();
		$('#contactForm #formProgress').html('Sending&hellip;');
		$('#contactForm #formProgress').fadeIn();
		// Disable the submit button
		$('#contactForm #submit').attr("disabled", "disabled");
		// Clear and hide any error messages
		$('#contactForm .formError').html('');
		// Set temaprary variables for the script
		var isFocus = 0;
		var isError = 0;
		// Get the data from the form
		var name = $('#contactForm #name').val();
		var email = $('#contactForm #email').val();
		var country = $('#contactForm #country').val();
		var message = $('#contactForm #message').val();
		// Validate the data
		if (name == '') {
			$('#contactForm #errorName').html('This is a required field.');
			$('#contactForm #name').focus();
			isFocus = 1;
			isError = 1;
		}
		if (email == '') {
			$('#contactForm #errorEmail').html('This is a required field.');
			if (isFocus == 0) {
				$('#contactForm #email').focus();
				isFocus = 1;
			}
			isError = 1;
		} else {
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if (reg.test(email) == false) {
				$('#contactForm #errorEmail').html('Invalid email address.');
				if (isFocus == 0) {
					$('#contactForm #email').focus();
					isFocus = 1;
				}
				isError = 1;
			}
		}
		if (country == '') {
			$('#contactForm #errorCountry').html('This is a required field.');
			$('#contactForm #country').focus();
			isFocus = 1;
			isError = 1;
		}
		if (message == '') {
			$('#contactForm #errorMessage').html('This is a required field.');
			if (isFocus == 0) {
				$('#contactForm #message').focus();
				isFocus = 1;
			}
			isError = 1;
		}
		// Terminate the script if an error is found
		if (isError == 1) {
			$('#contactForm #formProgress').html('');
			$('#contactForm #formProgress').hide();
			// Activate the submit button
			$('#contactForm #submit').attr("disabled", "");
			return false;
		}
		$.ajaxSetup({
			cache: false
		});
		var dataString = 'name=' + name + '&email=' + email + '&country=' + country + '&message=' + message;
		$.ajax({
			type: "POST",
			url: "http://www.rycote.com/scripts/contactSubmit.php",
			data: dataString,
			success: function (msg) {
				//alert(msg);
				// Check to see if the mail was successfully sent
				if (msg == 'Mail sent') {
					// Update the progress bar
					$('#contactForm #formProgress').html('<h2 style="padding-left:30px;">Message submitted</h2>');
					// Clear the country field and message textbox
					$('#contactForm #name').val('');
					$('#contactForm #email').val('');
					$('#contactForm #country').val('');
					$('#contactForm #message').val('');
					$('div.panel-contents').fadeOut("slow", closePanels());
				} else {
					$('#contactForm #formProgress').html('');
					alert('There was an error sending your email. Please try again.');
				}
				// Activate the submit button
				$('#contactForm #submit').attr("disabled", "");
			},
			error: function (ob, errStr) {
				$('#contactForm #formProgress').html('');
				alert('There was an error sending your email. Please try again.');
				// Activate the submit button
				$('#contactForm #submit').attr("disabled", "");
			}
		});
		return false;
	});
});

