// jQuery.fn.ForceNumericOnly =
// function()
// {
//     return this.each(function()
//     {
//         $(this).keydown(function(e)
//         {
//             var key = e.charCode || e.keyCode || 0;
//             // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
//             return (
//                 key == 8 || 
//                 key == 9 ||
//                 key == 46 ||
//                 (key >= 37 && key <= 40) ||
//                 (key >= 48 && key <= 57) ||
//                 (key >= 96 && key <= 105));
//         })
//     })
// };

$(function() {  
	
	$('input.credit-input').css({backgroundColor:"#FFFFFF"});
  	$('input.credit-input').focus(function(){
   		$(this).css({backgroundColor:"#ffed00"});
 	});
  	$('input.credit-input').blur(function(){
    	$(this).css({backgroundColor:"#FFFFFF"});
  	});
  	$("input#postcode").ForceNumericOnly();
  	$("input#contactphone").ForceNumericOnly();
	
	$(".billingbtn").click(function() {
		
		$("label#companynamelabel").removeClass('error');
		$("label#streetaddress1label").removeClass('error');
		$("label#suburblabel").removeClass('error');
		$("label#statelabel").removeClass('error');
		$("label#postcodelabel").removeClass('error');
		$("label#contactnamelabel").removeClass('error');
		$("label#contactphonelabel").removeClass('error');
		
		var CustomerName = $('input#companyname').val();
		if (CustomerName == "") {
	      $("label#companynamelabel").addClass('error');
	      $("input#companyname").focus();
	      return false;
	    } 
		
		var StreetAddress1 = $('input#streetaddress1').val();
		if (StreetAddress1 == "") {
	      $("label#streetaddress1label").addClass('error');
	      $("input#streetaddress1").focus();
	      return false;
	    }
	    
	    var StreetAddress2 = $('input#streetaddress2').val();
		
		var Suburb = $('input#suburb').val();
		if (Suburb == "") {
	      $("label#suburblabel").addClass('error');
	      $("input#suburb").focus();
	      return false;
	    }
	    
	    var State = $("#state option:selected").text();
	    if (State == "Select State") {
  	      $("label#statelabel").addClass('error');
  	      $("#state").focus();
  	      return false;
  	    }
  	    
  	    var PostCode = $('input#postcode').val();
	    if (PostCode == "") {
  	      $("label#postcodelabel").addClass('error');
  	      $("#postcode").focus();
  	      return false;
  	    }
  	    
  	    var ContactName1 = $('input#contactname1').val();
	    if (ContactName1 == "") {
  	      $("label#contactname1label").addClass('error');
  	      $("#contactname1").focus();
  	      return false;
  	    }
		
		var ContactPhone = $('input#contactphone').val();
	    if (ContactPhone == "") {
  	      $("label#contactphonelabel").addClass('error');
  	      $("#contactphone").focus();
  	      return false;
  	    }
  	    
  	    var CustomerCode = $('input#customercode').val();
		
		var dataString = "CustomerCode="+CustomerCode+"&CustomerName="+CustomerName+"&StreetAddress1="+StreetAddress1+"&StreetAddress2="+StreetAddress2+"&Suburb="+Suburb+"&PostCode="+PostCode+"&State="+State+"&ContactName="+ContactName1+"&ContactPhone="+ContactPhone;
		
	  	$.ajax({
		      type: "POST",
		      url: "bin/process_billing.php",
		      data: dataString,
		      success: function(data) {
		      	window.location.href ="delivery.php";
		      	
		      	}
     	});
     return false;
 	});
});


