var temp;
var geocoder = new google.maps.Geocoder();
function getState(zipcode) {
    geocoder.geocode( { 'address': zipcode}, function (result, status) {
        var state = "N/A";
        error='';
        if(status=='ZERO_RESULTS'){
          error='invalid zip code';
        }else{
          for (var component in result[0]['address_components']) {
            for (var i in result[0]['address_components'][component]['types']) {
		if (result[0]['address_components'][component]['types'][i] == "administrative_area_level_1") {
			state = result[0]['address_components'][component]['short_name'];
			// do stuff with the state here!

  if ($('#shipping_ship_to_billing').attr('checked')==true){
			Statecode=$('#billing_state option:selected');
	}else{
Statecode=$('#shipping_state option:selected');
	}
 			
$.each(Statecode, function(key, value){
 			 Statecode=this.value;



 			});
			if (error!=''){
			   alert(error);
			}else{
				if(state==Statecode){
				  continueCheckout();
				}else{
				  alert('Invalid Zip Code/State combination');
				}
			}
			return;

		}
            }
          }
       }
    });
}

function checkZip(){
if ($('#shipping_ship_to_billing').attr('checked')==true){
	ZipCode=$('#billing_zippostcode');
  	
}else{
	ZipCode=$('#shipping_zippostcode');
}
$.each(ZipCode, function(key, value){
	  ZipCode=this.value;
});
if (ZipCode.length != 5){alert('Please enter a 5 digit zip code');}else{
getState(ZipCode);
}
}

$('#btn-make-payment').attr('href','javascript:checkZip()');
$('#checkout-section-checkoutplugin').bind("after_refresh.brendan", function() { $('#btn-make-payment').attr('href','javascript:checkZip()'); });
