(function(B){
	B(document).ready(function(){
		B('form[class^="cform"]').submit(function(e){
			e.preventDefault();
			//in case there is already an error message
			B('#B_error').slideUp();
			B('#B_error').remove();
			var form = B(this);
			var error = false;
			var required = B('.fldrequired');
			required.each(function(){
				if(B(this).val() == ''){
					error = true;
				}
			});
			
			if(error){
				displayError(form);
				B('#B_error').show();
				document.location.href = '#B_error';
				B('#B_error').hide();
				B('#B_error').slideDown();
			}else{
//sajax_debug_mode = 1;
				//The form is never submitted, so the url in the backend doesn't matter anymore.
				//The way it works, is we simulate a request to leads 360
				//and let cforms perform it's regular tasks via cforms_validate
				//This way we post to leads 360 and let cforms send emails and do its regular job.
				B('#URL').val(B("#URL").val() + "?p=" + B("#Phone").val());
				var query = form.serialize();
				//alert(query);
				//This image will simulate the GET request to leads 360. It passes the parameters as a query string.
				var img = document.createElement('img');
				img.src = B_postUrl + '?' + query;
				cforms_validate(B_no, false)
				
				//B.post(
				//	B_ajaxUrl,
				//	query,
				//	function(data){
				//		document.location.href = B_postUrl + '?' + query;
				//	}
				//);
				//alert(B_postUrl + '?' + query);
				//document.location.href = B_postUrl + '?' + query;
			}
			
		});
	});
	
	//@param jQueryObj form[req]: A reference to the jquery object of the form being submitted.
	function displayError(form){
		var msg = "You left out your phone number. If you prefer to be contacted via email, specify that below and we will not call you. Your number is safe with us.";
		form.before('<div id="B_error" style="display: none; color: #F00; background-color: #FFFF99; padding: 6px; border: 2px solid #FFFF66;">' + msg + '</div>');
	}
})(jQuery);

