$(document).ready(function(){
	$('#main-container').jqTransform();

	$("button").click(function(){

		$(".formError").hide();

	});

	var use_ajax=true;
	$.validationEngine.settings={};

	$("#contact-form").validationEngine({
		inlineValidation: false,
		promptPosition: "centerRight",
		success :  function(){use_ajax=true},
		failure : function(){use_ajax=false;}
	 })

	$("#contact-form").submit(function(e){

			if(!$('#subject').val().length)
			{
				$.validationEngine.buildPrompt(".jqTransformSelectWrapper","* This field is required","error")
				return false;
			}
			
			if(use_ajax)
			{
				$('#loading').css('visibility','visible');
				$.post('submit.php',$(this).serialize()+'&ajax=1',
				
					function(data){
						if(parseInt(data)==-1)
							$.validationEngine.buildPrompt("#captcha","* Wrong verification number!","error");
							
						else
						{
							$("#contact-form").hide('slow').after('<h1>Thank you!</h1>');
						}
						
						$('#loading').css('visibility','hidden');
					}
				
				);
			}
			e.preventDefault();
	})

});


function form_submit() {
	$(document).ready( function() {
									 
		var email = $( "#yourEmail" ).val();
		var title = $( "#msgTitle" ).val();
		var content = $( "#msgContent" ).val();

		$( ".error" ).hide();
		$( ".confirm" ).hide();
									 
		if( email.length < 1 ) {
			
			$( ".error" ).show( "slow" );
			$( ".error" ).html( "You have not supplied a valid email address" );
			
		} else if( title.length < 1 ) {
			
			$( ".error" ).show( "slow" );
			$( ".error" ).html( "You have not supplied a valid title" );
			
		} else if( content.length < 1 ) {
			
			$( ".error" ).show( "slow" );
			$( ".error" ).html( "You have not supplied a valid message" );
			
		} else {
			/* AJAX */
			$.ajax({
			   type: "POST",
			   url: "send-email.php",
			   data: "email="+escape( email )+"&title="+escape( title )+"&content="+escape( content ),
			   success: function(msg){
				 if( msg == "Thank you for your message, it has been sent." ) {
					 
					 $( ".confirm" ).show( "slow" );
					 $( ".confirm" ).html( "Thank you for your message, it has been sent." );
					 
			     } else {
				   
				    $( ".error" ).show( "slow" );
					$( ".error" ).html( "Your message could not be sent, try again." );
					
				 }
				   
			     }
			 });
			
		}
									 
	} );
	
}
