function signUpWithFacebook() {
  var username = document.facebookconnectform.username.value;
  
  var xmlhttp;
  
  if (window.XMLHttpRequest) {    
    xmlhttp = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // code for IE6, IE5      
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    alert("Your browser does not support XMLHTTP! This page will not work");
  }
  
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 1) {
    
      document.getElementById('connectmsg').innerHTML = "Please wait while we create your diPoll account...";  
      
    } else if (xmlhttp.readyState == 4) {
    
      var msg = "";
      
      // Get data from the server's response      
      var lines = xmlhttp.responseText.split("\n");      
      if (lines.length == 0) {
        // error this
        return;
      }
      
      var tokens = lines[0].split("|");
      if (tokens.length == 0) {
        // error this
        return;
      }
      if (tokens[0] == "error") {
              
        if (tokens[1] == "db_error") {
          msg = "<div class=\"formerror\">Something's wrong with the database. Please try again!</div>";
        } else if (tokens[1] == "unknown_error") {
          msg = "<div class=\"formerror\">Something's wrong. Please try again!</div>";
        } else if (tokens[1] == "username_bad_symbols") {
          msg = "<div class=\"formerror\">Your username consists of invalid symbols. Only letters &amp; underscores are allowed.</div>";
        } else if (tokens[1] == "username_too_short") {
          msg = "<div class=\"formerror\">Your username must be at least 6 characters long.</div>";
        } else if (tokens[1] == "username_too_long") {
          msg = "<div class=\"formerror\">Your username must be less than 20 characters long.</div>";
        } else if (tokens[1] == "username_taken") {
          msg = "<div class=\"formerror\">The username you selected has already been taken. Please try another one.</div>";
        } else if (tokens[1] == "facebook_linked") {
          msg = "<div class=\"formerror\">Your Facebook account is already connected to diPoll (?)</div>";
        } else {
          msg = "<div class=\"formerror\">Something's not right. Please try again!</div>";
        }
        
      } else if (tokens[0] == "success") {
        
        msg = "";
        document.getElementById('mainmsg').innerHTML = "<div id=\"connect_container\">You have successfully connected with Facebook! <br/><br/><a href=\"javascript:;\" onClick=\"window.location.href=window.location.href;\">Click here</a> to continue.</div>";        
        
      }            
      document.getElementById('connectmsg').innerHTML = msg;
    }
  }  
  xmlhttp.open("GET", "signupwithfacebook.php?username="+username, true);
  xmlhttp.send(null);
}

function loginAndLinkFacebook() {

  var username = document.dipollloginform.username.value;
  var password = document.dipollloginform.password.value;
  var poststr = "username="+encodeURI(username)+"&password="+encodeURI(password);  
  
  var xmlhttp;
  
  if (window.XMLHttpRequest) {    
    xmlhttp = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // code for IE6, IE5      
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    alert("Your browser does not support XMLHTTP! This page will not work");
  }
  
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 1) {
    
      document.getElementById('connectmsg').innerHTML = "Please wait while we connect to your diPoll account...";  
      
    } else if (xmlhttp.readyState == 4) {
    
      var msg = "";
      
      // Get data from the server's response       
      var lines = xmlhttp.responseText.split("\n");      
      if (lines.length == 0) {
        // error this
        return;
      }
      
      var tokens = lines[0].split("|");
      if (tokens.length == 0) {
        // error this
        return;
      }
      if (tokens[0] == "error") {
              
        if (tokens[1] == "error_linking") {
          msg = "<div class=\"formerror\">You have logged in, but there was a problem connecting Facebook with your diPoll account. Please try again!</div>";
        } else if (tokens[1] == "not_activated") {
          msg = "<div class=\"formerror\">Your diPoll account is not activated yet! Please follow the instructions in your activation email and try again!</div>";
        } else if (tokens[1] == "invalid_login") {
          msg = "<div class=\"formerror\">Your username/password was not correct. Please try again!</div>";
        } else {
          msg = "<div class=\"formerror\">Something's not right. Please try again!</div>";
        }
        
      } else if (tokens[0] == "success") {
        msg = ""
        var redirect = window.location.href.replace("facebooksignin","facebooklinked");
        window.location.href = redirect;
        document.getElementById('mainmsg').innerHTML = "<div id=\"connect_container\">You have successfully connected with Facebook! <br/><br/><a href=\"javascript:;\" onClick=\"window.location.href=window.location.href;\">Click here</a> to continue.</div>";        
        
      }            
      document.getElementById('connectmsg').innerHTML = msg;
    }
  }
  xmlhttp.open("POST", "loginlinkfacebook.php", true);
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.setRequestHeader("Content-length", poststr.length);
  xmlhttp.setRequestHeader("Connection", "close");
  xmlhttp.send(poststr);
}

function FBConnectThenVote(poll_id, choice) {
  
  var xmlhttp;
  
  if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    alert("Your browser does not support XMLHTTP! This page will not work");
  }
    
  xmlhttp.open('GET', 'setfacebookcallback.php?poll_id='+poll_id+'&choice='+choice, false);
  xmlhttp.send(null);
  
  // Get data from the server's response
  var lines = xmlhttp.responseText.split("\n");
  if (lines.length == 0) {
    // error this
    return;
  }
        
  var tokens = lines[0].split("|");
  if (tokens.length == 0) {
    // error this
    return;
  }
        
  if (tokens[0] == "error") {
  } else if (tokens[0] == "success") {
    FB.Connect.requireSession(null, true);
  }
  
}

function FBPublishVoteAuto(poll_id, choice, fbid) {
  var xmlhttp;
  var poststr = "poll_id="+encodeURI(poll_id)+"&choice="+encodeURI(choice)+"&fbuid="+encodeURI(fbid);
  if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // code for IE6, IE5      
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } else {              
  }
  xmlhttp.onreadystatechange = function() {}
  xmlhttp.open("POST", "fbpublishvote.php", true);
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.setRequestHeader("Content-length", poststr.length);
  xmlhttp.setRequestHeader("Connection", "close");
  xmlhttp.send(poststr);
}
