
$(document).ready(function() {

  // get their email from the cookie
  var emailVal = $.cookie("username");
  
  var foundCookie = false;
  var foundDB = 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);
     
     // 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")
        {
          foundDB = true;
        
          $("#name").val(data.name);
          $("#zip").val(data.zip);
          $("#address").val(data.address);
          $("#city").val(data.city);
          $("#state").val(data.state);
          $("#phone").val(data.phone);  
     
          // put the focus on the text area
          $("#messageMiddle").focus();
        }   
     }, "json");  
  
  }
  else
  {
     // if their email isn't there, put the focus on the email field
     $("#email").focus();
  }
  
  $("#email").blur(function(){
     if (!foundCookie)
     {
	      // 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")
	        {
	          foundDB = true;
	          
	          $("#name").val(data.name);
	          $("#zip").val(data.zip);
	          $("#address").val(data.address);
	          $("#city").val(data.city);
	          $("#state").val(data.state);
	          $("#phone").val(data.phone);  
	     
	          // put the focus on the text area
	          $("#messageMiddle").focus();  
	        } 
	     }, "json"); 
     }
  });
  
  $("#zip").blur(function(){
     if (!foundCookie && !foundDB)
     {     
         var name = $("#name").val();
         while (name.indexOf(" ")>-1)
             name = name.replace(" ", "%20");
         var zip = $("#zip").val();
         $.getJSON("http://api.whitepages.com/find_person/1.0/?name="+name+";zip="+zip+";api_key=79c75d038aff3236e24e219e80d012bd;outputtype=JSONP;callback=?", function(status){

             if (status.result.type == "success" && status.listings.length > 0)
             {
                  var result1 = status.listings[0];
                  var address = result1.address.house + " " + result1.address.street;
                  var city = result1.address.city;
                  var state = result1.address.state;
                  var phone = "";
                  if (result1.phonenumbers && result1.phonenumbers.length > 0)
                    phone = result1.phonenumbers[0].fullphone;        
                 
		          $("#address").val(address);
		          $("#city").val(city);
		          $("#state").val(state);
		          $("#phone").val(phone);  
             }  
          });
     }
  });
  
  $("#emailSubmit").click(function(){
       // if they didn't have a cookie, we'll write it here
       if (!foundCookie)
       {
          $.cookie("username", $("#email").val(), {expires:365});
       }
       // write it to the DB as well, de facto registering them
       if (!foundDB)
       {
          $.post("jsp/register_user.jsp", 
             {
                email: $("#email").val(), 
                name: $("#name").val(),
                zip: $("#zip").val(),
                address: $("#address").val(),
                city: $("#city").val(),
                state: $("#state").val(),
                phone: $("#phone").val()
             });
       }
       
       // validate that all the information is filled in here?
       
       // create the rep string
       var rep = "";
       if ($("#sen1").attr("checked"))
           rep += $("#sen1").val() + ",";
       if ($("#sen2").attr("checked"))
           rep += $("#sen2").val() + ",";
       if ($("#cong1").attr("checked"))
           rep += $("#cong1").val() + ",";
       if ($("#cong2").val() == "checked")
           rep += "cong2,";
       if ($("#cong3").val() == "checked")
           rep += "cong3,";
       if ($("#cong4").val() == "checked")
           rep += "cong4,";
       rep = rep.substring(0, rep.length-1);
                   
              
       // submit the information to the server, which will send the email
       $.post("jsp/send_mail.jsp", 
          {
             email: $("#email").val(),
             name: $("#name").val(),
             reps: rep,
             subject: $("#subject").val(),
             message: $("#messageTop").val() + $("#messageMiddle").val() + "\n\n" + $("#messageEnd").val(),
             zip: $("#zip").val(),
             address: $("#address").val(),
             city: $("#city").val(),
             state: $("#state").val(),
             phone: $("#phone").val(),
             emailType: $("#emailType").val()
          },
          function(data){
             
            //$("#errorMessage").text(data.message);
            alert("Your message has been sent");
             
          }, "json"); 
       
       
  });

});
