$(document).ready(function() {

  // get their email from the cookie
  var emailVal = $.cookie("username");
  
  var foundCookie = false;
  
  // if their email is there
  if (emailVal != null && emailVal != "")
  {
     // denote that they have a cookie
     foundCookie = true;
  
     // put it in the email field
     $("#email").val(emailVal);
     $("#custom").val(emailVal);
     
     
     // then use the email to look up their DB information and fill in the fields
     $.post("../jsp/lookup_user.jsp", {email:emailVal }, function(data){
     
        if (data.result == "SUCCESS")
        {
           $("#address1").val(data.address);
           $("#city").val(data.city);
           $("#personName").val(data.name);
           var names = data.name.split(" ");
           if (names.length > 0)
              $("#first_name").val(names[0]);
           if (names.length > 1)
              $("#last_name").val(names[1]);
           $("#night_phone_a").val(data.phone);
           $("#state").val(data.state);
           $("#zip").val(data.zip);
        }   
     }, "json"); 
      
  }
  else
  {
     // if their email isn't there, put the focus on the email field
     $("#email").focus();
  }
  
  $("#email").blur(function(){
     if (!foundCookie)
     {
         $("#custom").val($("#email").val());
      
	      // then use the email to look up their DB information and fill in the fields
	     $.post("../jsp/lookup_user.jsp", {email:$("#email").val() }, function(data){
	     
	        if (data.result == "SUCCESS")
	        {
	           $("#address1").val(data.address);
	           $("#city").val(data.city);
               $("#personName").val(data.name);
	           var names = data.name.split(" ");
	           if (names.length > 0)
	              $("#first_name").val(names[0]);
	           if (names.length > 1)
	              $("#last_name").val(names[1]);
	           $("#night_phone_a").val(data.phone);
	           $("#state").val(data.state);
	           $("#zip").val(data.zip);
	        } 
	     }, "json"); 
     }
  });
  
  $("input:radio").change(function(){
      $("#amount").val($(this).val());
  });
  
  
});
