$(document).ready(function(){
	
	$('.message').one('focus', function() {
		clearText(document.getElementById('message'));
	});

});

function submitContactForm(baseUrl)
{
	var validation = validateContactForm();
	
	if (validation)
	{	
		var name 	= document.getElementById('name').value;
		var email 	= document.getElementById('email').value;
		var message = document.getElementById('message').value;
		var subject = document.getElementById('subject').value;
		
		var subjectTitle = window.langs.EmailComponents.EmailSubject;
		var nameTitle = window.langs.EmailComponents.Name;
		var messageTitle = window.langs.EmailComponents.Message;
		
		var submitted = document.getElementById('submitted').value;
		
		var formString = "subjectTitle="+ subjectTitle +"&nameTitle="+ nameTitle +
		"&messageTitle="+ messageTitle +"&name="+ name +"&email="+
			email +"&message="+ message +"&submitted="+ submitted +"&subject="+ subject +"";
		
		$.ajax({
			type: "POST",
			cache: false,
			data: formString,
			success: function(msg){
				if (msg == 'EmailSent')
				{
					var msgBack = window.langs.EmailNotifications.EmailSent;
				}
				else
				{
					var msgBack = window.langs.EmailNotifications.EmailNotSent;
				}
				
				$('#contact_form').html('<div id="notifications">'+ msgBack +'</div>');
				$('#notifications').hide();
				$('#notifications').fadeIn(1500);
			},
			error: function (jqXHR, textStatus, errorThrown) {
				$('#contact_form').html('<div id="notifications">'+window.langs.EmailNotifications.EmailNotSent+'</div>');
				$('#notifications').hide();
				$('#notifications').fadeIn(1500);
			}
		});
	}
	else
	{
		return false;
	}
}

function validateContactForm()
{
	var name 		= document.getElementById('name').value;
	var email 		= document.getElementById('email').value;
	var message 	= document.getElementById('message').value;
	
	if (name.length == 0)
	{
		var emptyName = window.langs.EmailNotifications.EmptyName;
		return showErrorMessage(emptyName);
	}
	
	if (email.length == 0)
	{
		var emptyEmail = window.langs.EmailNotifications.EmptyEmail;
		return showErrorMessage(emptyEmail);
	}
	
	var defaultMessage = window.langs.EmailNotifications.DefaultMessage;
	if ( (message.length == 0) || (message == defaultMessage) )
	{
		var emptyMessage = window.langs.EmailNotifications.EmptyMessage;
		return showErrorMessage(emptyMessage);
	}
	
	if ( !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email)) )
	{
		var invalidEmail = window.langs.EmailNotifications.InvalidEmail;
		return showErrorMessage(invalidEmail);
	}
	
	return true;
}

function showErrorMessage(msg)
{
	// alert(msg);
    jAlert(msg, '----------');
	return false;
}
