// TR 2009 collected data for sending.
$(function() {
		   
// This will hold our timer for TR
		var myTimer = {};
		   
function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}
$.fn.clearForm = function() {
   
        return this.each(function() {
   
          var type = this.type, tag = this.tagName.toLowerCase();
   
          if (tag == 'form')
   
            return $(':input',this).clearForm();
   
          if (type == 'text' || type == 'password' || tag == 'textarea')
   
            this.value = '';
          else if (type == 'checkbox' || type == 'radio')

            this.checked = false;

          else if (tag == 'select')
            this.selectedIndex = -1;
        });

      };


		$('form').submit(function() {
		//setup and get firstname of customer
        var customer_name = $("input#name").val();
		
		if ($.trim(customer_name) == "" ){
			alert("Please make sure your Name has been filled in");
			return false;
		}
		//setup lastname of customer
		var customer_email = $("input#email").val();
		if ($.trim(customer_email) == ""){
			alert("Please make sure your Email has been filled in");
			return false;
		}
		//setup customer email of customer
		var customer_tel = $("input#tel").val();
		if ($.trim(customer_tel) == ""){
			alert("Please make sure your Telephone Number has been filled in");
			return false;
		}
		
		//setup customer mobile
		var customer_mobile = $("input#mobile").val();
		
		//setup customer postcode of customer
		var customer_postc = $("input#postc").val();
		if ($.trim(customer_postc) == ""){
			alert("Please make sure you fill in your postcode");
			return false;
		}
		
		//checkboxes read in and then sent on the form
		var customer_byphone = $("input#phone").val();
		var customer_bymeeting = $("input#meeting").val();
		var customer_byinternet = $("input#internet").val();
		
		//get the where did you hear from us data
		
		var customer_media = $("#media").val();
		//end the here from us data
		

		
		$('#success').fadeOut('2000');

        $.ajax({
            type: 'post',
            url: 'scripts/email-tr.php',
            data: 'customername=' + customer_name + '&customeremail=' + customer_email + '&customertel=' + customer_tel  + '&customermob=' + customer_mobile   + '&customerpost=' + customer_postc  + '&customerbymeeting=' + customer_bymeeting + '&customerbyphone=' + customer_byphone + '&customerbyinternet=' + customer_byinternet + '&customermedia=' + customer_media,

            success: function(response) {
                $('#success').fadeIn('1000').empty().append(response);
	
				
				
            }
			   
	

        	});
		
		return false;
		});

$("#loading").bind("ajaxSend", function(){
   $(this).show();
 }).bind("ajaxComplete", function(){
   $(this).hide();
   document.location.href="process-thankyou.html";
   myTimer = $.timer(6000,function(){
	$('#success').fadeOut('2000');
	$('form').clearForm();
	// redirect to the thankyou page
	
	
	
								   
	});
   			

 });
			
	
});
