
$(document).ready(function() {  
       
    // get their email from the cookie
  var emailVal = $.cookie("username");
  
  // change for the petition stuff
  var refer = $.url.param("link");
  if (refer != null && refer != "")
  {
     $.cookie("link", "yes", {expires:365});
  }
    
  // if their email is there
  if (emailVal != null && emailVal != "")
  {  
     $("#joinForm").hide();
  }     
  else
  {
     $("#warningMessage").hide();
  }
       
  $("#showJoin").click(function(){
     $("#joinForm").show();
  });
       
       
  var address = "";
  var city = "";
  var state = "";
  var phone = "";
  var lat = "";
  var long = "";

  // first lookup the information from White Pages    
  $("#register").click(function(){
  
      $(".error").text("");
  
	  var isError = false;
	  if ($("#Name").val()=="") 
	  {
	     $("#Name").next().text(" Name cannot be blank");
	     isError = true;
	  }     
	  if ($("#ZipCode").val().length!=5 || parseInt($("#ZipCode").val())==Number.NaN)
	  {
	     $("#ZipCode").next().text(" Zip Code must be 5 digits");
	     isError = true;
	  }
	  if ($("#ZipCode").val()=="")
	  {
	     $("#ZipCode").next().text(" Zip Code cannot be blank");
	     isError = true;
	  }
	  if ($("#Email").val()=="")
	  {
	     $("#Email").next().text(" Email cannot be blank");
	     isError = true;
	  }
	  if (isError)
	      return;  
  
      var name = $("#Name").val();
      while (name.indexOf(" ")>-1)
          name = name.replace(" ", "%20");
      var zip = $("#ZipCode").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];
                 if (result1.address.house != undefined)
 	                address = result1.address.house + " " + result1.address.street;
 	             else
 	                address = result1.address.street;
                 city = result1.address.city;
                 state = result1.address.state;
                 if (result1.phonenumbers && result1.phonenumbers.length > 0)
 	                phone = result1.phonenumbers[0].fullphone;  
                 //lat = result1.geodata.latitude;
                 //long =  result1.geodata.longitude;     
                 
             }    
                           
             // with this information, make an Ajax call to register the user
             $.post("../jsp/register_user.jsp", 
             {
                email: $("#Email").val(), 
                name: $("#Name").val(),
                zip: $("#ZipCode").val(),
                address: address,
                city: city,
                state: state,
                phone: phone,
                lat: lat,
                long: long
             }, function(data){
                    
                if (data.result == "SUCCESS")
                {
                   // if the registration is successful, write a cookie and direct them to welcome screen
                   $.cookie("username", $("#Email").val(), {expires:365});
                   // register the user in our mail manager
                   $("#registrationForm").submit();
                }
                else
                {
                   // if the registration is unsuccessful, write a message to them
                   $("#errorMessage").text(data.message);
                }
                 
             }, "json");

       });
     });

});
