$(document).ready(function(){
         var state_tmp_html =  $('#state_div').html();
         var prov_tmp_html =  $('#prov_div').html();
         $('#prov_div').html(' '); 

	$("#reg_form").validate({
  		rules: {
			email: {
			    required: true,
			    email: true,
			    remote:{
                                    url: "/check_field.php",
                                    type: "post",
                                    data: {new_email: function() {return $("#email").val(); },field_type:'email_new'}
                             }
			},
                        zip: {
			    required: true,
			   //rangelength: [5, 10],
			    remote:{
                                    url: "/check_field.php",
                                    type: "post",
                                    data: {zip_code: function() {return $("#zip").val(); }, country: function() {return $("#country").val(); }, field_type:'zip_code'}
                             }
			},
                        phone: {
			    required: true,
			    minlength: 10,
			    remote:{
                                    url: "/check_field.php",
                                    type: "post",
                                    data: {phone_number: function() {return $("#phone").val(); }, field_type:'phone'}
                             }
			}
	},
			messages: {
				email: {
					required: "Please enter a valid email address",
					minlength: "Please enter a valid email address",
					remote: jQuery.format("{0} is already in use")
				},
				zip: {
					required: "Please enter a valid zip code",
					//rangelength: jQuery.format("Enter  {0} digits OR #####-####"),
					remote: jQuery.format("{0} is not a valid  zip code")
				},
				phone: {
					required: "Please enter a valid Phone number",
					minlength: jQuery.format("Enter  {0} digits"),
					remote: jQuery.format("{0} is not a valid  Phone number " )
				}
			}
               
	});

 	$("#Tos").rules("add", {
    	required: true,
        messages: {
        	required: "Must chose accept to continue with application"
    	}
   	});
  	$("#state").rules("add", {
    	         required: true,
                 messages: {
        	     required: "Please Make a valid Selection"
    	          }
   	 });
  	$("#create_form_submit").click(function(){
     	   // alert("form submit pressed");
     	    if ($("#reg_form").valid()){
                  $("#reg_form").submit();
     	    }
   	});
        $('#show_tos, #hide_tos').click(function(){
            $('#tos_div').slideToggle("slow");
        });

        $('#country option').click( function(){
            if($(this).val() == 'USA'){
               $('#prov_div').hide();
               $('#prov_div').html('');
               $('#state_div').html(state_tmp_html);
               $('#state_div').show();

            }else{
               $('#state_div').hide();
               $('#state_div').html(' ');
               $('#prov_div').html(prov_tmp_html);
               $('#prov_div').show();
            }
  	    $("#state").rules("add", {
    	            required: true,
                    messages: {
        	        required: "Please Make a valid Selection"
    	            }
   	   });

        });


});
