$(document).ready(function(){

  
  //set up the year away expiration date
  var expiryDate=new Date();
  expiryDate.setTime(expiryDate.getTime()+60*60*24*365*1000);
  
  //put the cookies on the users computer
  
  //this sets the individual WID home page
  if(!getCookie('whoIsHome')){
  
    //parse the url
    var url = window.location.pathname;
    url = url.split('/',2);
    
    //get the affiliate directory
    var whoIsName=url[1];

    document.cookie="whoIsHome=" + whoIsName + "; expires= " + expiryDate.toGMTString() + ";path=/;domain=.whoisdirectory.com"; 
  }
  
  //this tracks the userId, the grapevine of the referring person for new sign ups
  if(!getCookie('whoIsGv')){
    document.cookie="whoIsGv=" + whoIsGv + "; expires= " + expiryDate.toGMTString() + ";path=/;domain=.whoisdirectory.com";
  }


/**
 * This will record a visit to this homepage for this IP address, matching the gv and the IP, it will then set a cookie to speed up subsequent page loads
 */
  if (!getCookie('whoIsTracking') && whoIsGv){
    $.post('http://www.whoisdirectory.com/app/ajax/whois.php', {a : 'track', gv : whoIsGv}, function (data){
      if (data == 'success'){
        document.cookie="whoIsTracking=success; expires= " + expiryDate.toGMTString() + ";path=/;domain=.whoisdirectory.com";
      }
    });
    
  }
  
  /**
   * This will capture their name
   */
/*
  $(":input[name=name]").blur(function(){
    var name = $(this).val();
    if (!name){
      return false;
    }
    $.post('/app/ajax/whois.php', {a : "track", name : name, gv : whoIsGv});
  });
*/
  
  /**
   * This will capture their email
   */
/*
  $(":input[name=from]").blur(function(){
    var email = $(this).val();
    if (!email){
      return false;
    }
    $.post('/app/ajax/whois.php', {a : "track", email : email, gv : whoIsGv});
  });
*/

  // this gives an id to the form
  // this gets around the problem that we face having an embedded form with no id
  // because the form comes from another place
  $('#AboutCopy [method=post]:first').attr('id','capture-tracking-form');
  var captureTrackingButton = $("#capture-tracking-form :submit");
  

  /**
   * This will take the submit button and submit to ajax first then trigger itself to send onward
   */
  captureTrackingButton.click(function(){trackFormData()});  

  var captureTrackingFormReady = false

  function trackFormData(){
  
    //on the second pass don't save data
    if(captureTrackingFormReady){
      return true;
    }
    
    var form = $('#capture-tracking-form');
  
    var name = $("#capture-tracking-form :input[name=name]").val();
    var email = $("#capture-tracking-form :input[name=from]").val();
    
    if (!name && !email){
      return true;
    }
    
    $.post('/app/ajax/whois.php', {a : "track", name : name, email : email, gv : whoIsGv}, function(){
      captureTrackingFormReady = true;
      captureTrackingButton.trigger('click');
    });
    return false;
  }


});