function delete_suggestion_notice(id, recipientID){
   var httpObject = getHTTPObject();
  if(httpObject != null)
    {
      var link =  "remove_suggestion.php?id="+id+"&recipientID=" + recipientID;
      httpObject.open("GET", link, true);
      httpObject.send(null);
      httpObject.onreadystatechange = function(){
	if(httpObject.readyState == 4)
	  {
	    if(	    httpObject.responseText == 'true' )
	      {
		$('#notification'+id).hide('slow');
	      }
	    else
	      alert(httpObject.responseText);
	  }
      }
    }
}

// RECOVER PASSWORD

function recover_password(){
  popupAjax( "recover_password.php");

}

function recover_password_go(){
  var form = document.getElementById('recoverEmail');
  var email = form.value;

  var httpObject = getHTTPObject();
  if(httpObject != null)
    {
      var link =  "recover_password_go.php?email=" + email;
      httpObject.open("GET", link, true);
      httpObject.send(null);
      httpObject.onreadystatechange = function(){
	if(httpObject.readyState == 4)
	  {
	    var response = httpObject.responseText;
	    var div = document.getElementById('recoverForm');
	    div.innerHTML = response;
	  }
      }
    }
}


// SIGN UP

function sign_up(facebook){
  if(facebook)
    popupAjax('signup_form.php?fb=true');
  else
    popupAjax('signup_form.php');

  $('#popup').keydown(function(event){
      switch (event.keyCode) {
      case 13:
	signup_go();
      }
    });

}

function country_change(){
  var country = document.getElementById('signupCountry').selectedIndex;
  if( country == 0 )
    $('.usaZip').show('slow');
  else 
    $('.usaZip').hide('slow').find('input').attr('value', '');

  if( country == 6 )
    $('.otherCountry').show('slow');
  else 
    $('.otherCountry').hide('slow').find('input').attr('value', '');

}

function member_type_change(){
  var name = $('#signupMemberType').val();
  if( name == 'Individual')
    {
      $('#voweln').show();
      $('#profsAuths').slideDown('slow');
      $('#signupLastName').show('slow');
      $('#groupWaiver').slideUp('slow');
      $('#profsAuths').slideDown('slow');
      set_form_default('signupFirstName', 'First');
      set_form_default('signupLastName', 'Last');
    }
  else 
    {
      if(name == 'Book club or other group')
	name = 'Group';
      name = name.toLowerCase();
      $('#voweln').hide();
      $('#profsAuths').slideUp('slow');
      $('#signupLastName').hide('slow').val('');
      $('#groupName').html(name);
      $('#groupWaiver').slideDown('slow');
      $('#profsAuths').slideUp('slow');
      set_form_default('signupFirstName', name + ' name');
    }
}


function signup_go(facebook){
  $('#signupSubmit').attr('disabled', true);
  var firstName = encodeURIComponent(document.getElementById('signupFirstName').value);
  var lastName = encodeURIComponent(document.getElementById('signupLastName').value);
  var country = document.getElementById('signupCountry').value;
  if( country == 'other')
    country = document.getElementById('otherCountry').value;
  var email = encodeURIComponent(document.getElementById('signupEmail').value);
  var password = encodeURIComponent(document.getElementById('signupPassword').value);
  var memberType = $('#signupMemberType').val();

  memberType = memberType.toLowerCase();
  if(memberType == 'book club or other group')
    memberType = 'group';

  var expert = document.getElementById('signupExpert').checked;

  var httpObject = getHTTPObject();
  if(httpObject != null)
    {
      var link =  "signup_go.php?firstName=" + firstName + "&lastName=" + lastName + "&email=" + email + "&country=" + country + '&password=' + password + '&expert=' + expert + '&memberType=' + memberType;
      if(facebook)
	link += '&fb=true';
      httpObject.open("GET", link, true);
      httpObject.send(null);
      httpObject.onreadystatechange = function(){
	if(httpObject.readyState == 4)
	  {
	    var response = httpObject.responseText;
	    if(response == 'true')
	      {
		if(facebook)
		  {
		    login_go(email, password, false);
		  }
		else
		  {
		    var loginMessage = '<p>Thank you.  A <b>verification email</b> has been sent to your  address.  Please follow the instructions in that message to complete the sign-up proccess.</p><p class="smallText">If you do not receive this message within a few minutes, please check your email\'s spam folder.</p>';
		    $('#popup').jqmHide();
		    popup(loginMessage);
		  }
	      }
	    else
	      {
		var div = document.getElementById('signupFormMessages');
		div.innerHTML = response;
		div.style.display = 'block';
		$('#signupSubmit').attr('disabled', false);
		document.getElementById('popup').scrollTop;
	      }
	  }
      }
    }

}

// login
function loginkeys(e){
  alert('here and');
  alert(e.keyCode);
}

function display_login( email, errorMessage ){
  var form = "<span id='loginErrorMessage' class='errorMessage'></span><span id='loginLoadingMessage'></span><table  border=0>        <tr >        <td><b>Email</b></td>	<td><p><input type='text'  name='email' id='loginEmail' size='24' value='";
  
  if(email)
    form += email;

  form += "'></p></td>        </tr>	<tr>        <td><b>Password</b></td>	<td><p><input onclick='loginkeys;' type='password' name='password' id='loginPassword' size='24' ></p></td>        </tr>        </table><p><input type='checkbox' id='loginRememberMe'>Remember me  <a class='space jqmClose' onclick=\"recover_password();\">Forgot your password?</a> <b><a onclick=\"login();\">Sign In</a></b></p>  </div>\n";

  popup(form);
  $('#popup').keydown(function(event){
      switch (event.keyCode) {
      case 13:
	login();
      }
    });

  if(errorMessage)
    login_message(errorMessage);

  if(email)
    $('#loginPassword').focus();
  
}

function login_message( msg ){
  $('#loginErrorMessage').html( msg ).css({display: 'block'});
}


function login(){
  var email = encodeURIComponent(document.getElementById('loginEmail').value);
  var password = encodeURIComponent(document.getElementById('loginPassword').value);
  var remember = document.getElementById('loginRememberMe');
  if(remember)
    remember = remember.checked;
  $('#loginErrorMessage').hide();		
  $('#loginLoadingMessage').html("<p><img src='images/loadingAnimation.gif' alt='Logging in...'></p>").show();

  login_go(email, password, remember);
}

function login_fb(){
  var httpObject = getHTTPObject();
  if(httpObject != null)
    {
      var link =  "login_fb.php";

      httpObject.open("GET", link, true);
      httpObject.send(null);
      httpObject.onreadystatechange = function(){
	if(httpObject.readyState == 4)
	  {
	    if(httpObject.responseText == 'true')
	      window.location.reload();
	    else
	      //	      alert("Error logging in. Please try again.");
	      alert(httpObject.responseText)
	  }
      }
    }
}

function login_go(email, password, remember){
  var httpObject = getHTTPObject();
  if(httpObject != null)
    {
      var link =  "login.php?email=" + email + "&password=" + password;

      if(remember)
	link += "&remember=true";

      httpObject.open("GET", link, true);
      httpObject.send(null);
      httpObject.onreadystatechange = function(){
	if(httpObject.readyState == 4)
	  {
	    var response = httpObject.responseText;
	    if(response == 'true')
	      {
		window.location.reload();
	      }
	    else
	      {
		$('#loginLoadingMessage').hide();		
		var error = document.getElementById('loginErrorMessage');
		error.style.display = 'block';
		error.innerHTML = response;
	      }
	  }
      }
    }
}

function logout(fb){
  var httpObject = getHTTPObject();
  var local_logout = function(){
    if(httpObject != null)
      {
	var link =  "logout.php";
	httpObject.open("GET", link, true);
	httpObject.send(null);
	httpObject.onreadystatechange = function(){
	  if(httpObject.readyState == 4)
	    {
	      window.location = "index.php";
	    }
	}
      }
  }
  if(fb)
    FB.Connect.logout(local_logout);
  else
    local_logout();
}

function search_focus(){
  $('#searchInput').focus().addClass('focusedInput').blur(function(){$(this).removeClass('focusedInput');});
}


function edu_intersections(){
  popupAjax("explain_intersections.php");
}

function edu_tags(){
  popupAjax("explain_tags.php");
}


function edu_trails(){
  popupAjax("explain_trails.php");
}

function edu_featured(){
  popupAjax("explain_featured.php");
}

function edu_share(){
  popupAjax("explain_share.php");
}

function edu_public(trailID){
  link = "explain_public.php";
  if(trailID)
    link += '?id=' + trailID;

  popupAjax(link);
}

function edu_institution(type){
  link = 'explain_institution.php?type=' + type;
  popupAjax(link);
}

function browse_tags(){
  load_tab('browseTrails', 'browse_by_tag.php', true);
  tabs_unselect();
  $('#browseTagsLabel').addClass('tabSelected');
}
function browse_authors(){
  load_tab('browseTrails', 'browse_by_author.php', true);
  tabs_unselect();
  $('#browseAuthorsLabel').addClass('tabSelected');
}

function tabs_unselect(){
  $('#allLabel').removeClass('tabSelected');
  $('#friendsLabel').removeClass('tabSelected');
  $('#youLabel').removeClass('tabSelected');
  $('#browseTagsLabel').removeClass('tabSelected');
  $('#browseAuthorsLabel').removeClass('tabSelected');
}

function tabs_hide(){
      $('#browseTrails').hide('slow');
      $('#friendsTrails').hide('slow');
      $('#allTrails').hide('slow');
      $('#youTrails').hide('slow');
}

function tab_new( name, friendsid ){
  var tab = $('#'+name+'Trails');
  if(tab.html() == '')
    get_trail_list( name+'Trails', true, friendsid);
  else
    {
      tabs_hide();
      tab.show('slow');
    }
  tabs_unselect();
  $('#'+name+'Label').addClass('tabSelected');
}

function get_trail_list( receptacle, display, friendsid, author, tag, youid ){
      var link =  "index_trails_list.php?";
      if(friendsid)
	link += "friendsid=" + friendsid;
      if(author)
	link += 'author=' + author;
      if(tag)
	link += 'tag=' + tag;
      if(receptacle == 'youTrails')
	{
	  if(youid)
	    link += 'who='+youid;
	  else
	    link += 'who=you';
	}
      load_tab(receptacle, link, display);
}
 
function load_tab( receptacle, link, display ){
  if(display)
    {
      tabs_hide();
      $('#'+receptacle).html("<img src='images/loadingAnimation.gif' alt='Loading...'>").show('slow');
    }
  $('#'+receptacle).load(link);
}
