///////////////////////
// Contact Form AJAX //
///////////////////////

 $(document).ready(function(){				// When Document Ready
	$("#sendmail").click(function(){		// When Element with ID #sendmail is clicked
		
		// Set Variables
		var valid = '';		// Set validation variable to empty
		var isr = ' is required.';		// Set 'is required' profix for errors
		var name = $("#name").val();	// Set 'name' variable as '#name' value
		var mail = $("#mail").val();	// Set 'mail' variable as '#mail' value
		var subject = $("#subject").val();		// Set 'subject' variable as '#subject' value
		var company = $("#company").val();		// Set 'company' variable as '#company' value
		var phone = $("#phone").val();		// Set 'phone' variable as '#phone' value
		var text = $("#text").val();		// Set 'text' variable as '#text' value
		var antispam = $("#enquiry").val();		// Set 'antispam' as '#enquiry' value (should be empty if spambot)
		
		
		// Validation
		if (antispam.length>0)		// Check antispam (#enquiry) is empty
			{	
			valid += '<br /><b>WE DO NOT ACCEPT SPAM!</b>';		// Create error for field
			}
		if (name.length<1)	// If name length is less than 1, create error and highlight field in red
			{
			valid += '<br /><em><b>Full Name</b></em>'+isr;
			$("#name").css("border", "1px dotted red");
			}
		if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i))	// If email format incorrect, create error and highlight field in red
			{
			valid += '<br />A valid <em><b>Email Address</b></em>'+isr;
			$("#mail").css("border", "1px dotted red");
			}
		if (document.getElementById('formail').subject.selectedIndex == 0) 	// If subject not selected, create error and highlight field in red
			{
			valid += '<br /><em><b>Subject</b></em>'+isr;
			$("#subject").css("border", "1px dotted red");
			}
		if (text.length<1)	// If text length is less than 1, create error and highlight field in red
			{
			valid += '<br />A <em><b>Message</b></em>'+isr;
			$("#text").css("border", "1px dotted red");
			}
		if (valid!='')	// If validation variable not equal to blank, produce error status box
			{
			$("#response").fadeIn("slow");
			$("#response").html("<div style=\"float: left;\"><h3 style=\"color:#fff;\">ERROR:</h3></div></"+valid);
			$("#response").css("background", "url('/images/red.png')");
			}
		else {	// Else restore field border color to black
			$("#name").css("border", "1px dotted #000");
			$("#mail").css("border", "1px dotted #000");
			$("#subject").css("border", "1px dotted #000");
			$("#text").css("border", "1px dotted #000");
			
			
			var datastr ='&name='+name+'&company='+company+'&phone='+phone+'&mail='+mail+'&subject='+subject+'&text='+text; // Create 'datastr' string to send in POST
			$("#response").css("display", "block");		// Make status display !important!
			$("#response").css("background", "url('/images/blue.png')");
			$("#response").css("text-align", "center");
			$("#response").html("Sending your enquiry...&nbsp;&nbsp;<img src=\"http://www.xclamation.co.uk/images/email.gif\" height=\"12px\" />");
			$("#response").fadeIn("slow");
			$.ajax	(
				{
				type: "POST",
				url: 'http://www.xclamationdesign.co.uk/includes/mail.php',		// Full explicit location of mail.php to prevent any problems
				data: datastr,
				cache: false,
				success: function(html)		// On success, perform function on html
					{
					$("#response").fadeIn("slow");
					$("#response").html(html);
					setTimeout('$("#response").fadeOut("slow")',5000);
					setTimeout('$("#response").css("text-align", "right")',5500);
					document.getElementById('formail').reset();
					}
				}
			)
		}
		return false;
	});
});

function resetFields()
	{
		$(document).formail.reset();
	}

// Send data to 'mail.php' using AJAX
 function send(datastr)
	{
	$.ajax	(
				{
				type: "POST",
				url: 'http://www.xclamationdesign.co.uk/includes/mail.php',		// Full explicit location of mail.php to prevent any problems
				data: datastr,
				cache: false,
				success: function(html)		// On success, perform function on html
					{
					$("#response").fadeIn("slow");
					$("#response").html(html);
					setTimeout('$("#response").fadeOut("slow")',5000);
					setTimeout('$("#response").css("text-align", "right")',5500);
					document.getElementById('formail').reset();
					}
				}
			)
	}