// JavaScript Document
// JQUERY Plugin
// (c)2009 CMD/Creative Media Disciples/Mark White.
// Original Reference Source: http://be.twixt.us/jquery/formSubmission.php, modified and changed by Mark White

$.fn.ajaxSubmit = function(e) { 

	this.submit(function(){ 
																				
		var params ={}; // create a parameters array based upon the inputs in the form
		var myform=$(this);
		var preloader='<div id="imagePreLoader" >Sending Message<br/><br /><img src="/images/ajax-loader.gif"/></div>';
		var myMessage="<p><strong>Thank you for your message</strong></p><p><strong>Did you know you can also contact us by any of the following methods</strong></p><p>Give us a ring: <strong>+44 (0) 844 884 2500</strong></p><p>E-mail us:<strong> info@cmdonline.co.uk</strong></p><p>pop in and see us in the Oxford Studio at:</p><p><strong>Cherwell Business Village, Southam Road, Banbury, Oxfordshire, OX16 2SP</strong></p>";
		
		
		myform.animate({"opacity":"0"},500);
		myform.before(preloader);
		
		//go through each element in the form
		myform.find("input,textarea").each(function(e){
			params[this.name] = this.value; // we add inputs to the array the array
		});
	
		// add /1 to show the form it's coming from ajax request
		$.post(this.getAttribute("action")+"/1",params,function(data){
			// just return a "sent" to confirm
			if(data=="sent"){
				myform.hide();
				$("#imagePreLoader").remove();
				myform.after(myMessage);
								
				// it was sent properly
			}else{
		
				$("#imagePreLoader").remove();
				myform.animate({"opacity":"1"},500);
				$("#"+$(myform).attr("id")+"Error").html("<p><strong>Error: please check your message</strong></p>");
				var dataCheck=data.split(",");
				for(i=0;i<dataCheck.length-1;i++)
				{
					if(dataCheck[i]!="form")
						$("#"+dataCheck[i]).css({"color":"#EE008A"});
				}
			}
								
	 });
		
		return false; 									
	});


}