$(document).ready(function(){
   
    jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
	return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");

    jQuery.validator.addMethod("zipcode", function(zipdigits, element) {
    zipdigits = zipdigits.replace(/\s+/g, ""); 
	return this.optional(element) || zipdigits.match(/^[0-9]{5}(-[0-9]{4})?$/);
}, "Please specify a valid zip code");

  
$("#leadForm").validate({
  rules: {
    phone: {
      required: true,
      phoneUS: true
    },
    zip: {
      required: true,
      zipcode: true
    },
	
	creditcards: {
	  required: true
	}
  },
  
  messages: {
  	creditcards: "Please specify whether you currently accept credit cards"
  },
  errorPlacement: function(error, element) {
			if ( element.is(":radio") )
				error.appendTo( element.parent() );
			else if ( element.is(":checkbox") )
				error.appendTo ( element.next() );
			else
				error.appendTo( element.parent() );
	},
	submitHandler: function(form) {
		$('form#leadForm').hide();
		$('div.success').fadeIn();	
		//form.submit();
		AjaxPostData();
	}


});
 function AjaxPostData()
 {
 		var fname     	= $('#fname').attr('value');  
		var lname     	= $('#lname').attr('value'); 
		var bname     	= $('#bname').attr('value');
		var city     	= $('#city').attr('value');				 
		var state     	= $('#state').attr('value');
   		var zip     	= $('#zip').attr('value');  
		var phone     	= $('#phone').attr('value'); 
		var cemail     	= $('#cemail').attr('value');
		var creditcards = $('#leadForm input:radio:checked').attr('value');
		var dataString = "http://centertrk.com/d.ashx?ckm_campaign_id=51&ckm_key=3drOWGG6LBo&ckm_subid=http://www.creditcardprocessing.net" + "&first_name="+ fname + "&last_name=" + lname + "&business_name="+ bname + "&city=" + city + "&state="+ state + "&zip_code=" + zip + "&email_address="+ cemail + "&contact_phone=" + phone + "&accept_cards=" + creditcards;
		
		/*$.ajax({
		type: "POST",
		url: "http://centertrk.com/d.ashx",
		dataType: 'json',
		data: "dataString",
		success:  function(data){
	   }
		});	
		*/
		$('div.pixel').html('<img src="' + dataString  + '" style="height:1px; width:1px;" />');
		return false;

 }

});   
