/* AJAX STUFF =========================================================== */

/* Function to get a new XMLHttpRequest object.
 * Based on an example from w3schools.com
 */
function getXmlHttpObject() {
  var xmlHttp = null;

  try {
    // Presumably everything that's not IE < 7 (or, say, Lynx)
    xmlHttp = new XMLHttpRequest();
  } catch (e) {
    try {
      // Internet Explorer 6+
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        // Internet Explorer 5.5+
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        alert("Your browser doesn't appeart to support AJAX. :(");
        return false;
      }
    }
  }

  return xmlHttp;
}


/* COOKIE STUFF ========================================================= */

/* Get the value of a cookie.
 */
function get_cookie(name) {
  if (document.cookie.length > 0) {
    start = document.cookie.indexOf(name + "=");

    if (start != -1) {

      start = start + name.length + 1;
      end = document.cookie.indexOf(";", start);
      if (end == -1) end = document.cookie.length;

      return unescape(document.cookie.substring(start,end));
    }
  }

  return '';
}


/* Set the value of a cookie.
 */
function set_cookie(name, value) {
  var expires = new Date();
  expires.setDate(expires.getDate() + 14);

  document.cookie = name + "=" + escape(value) + 
    "; expires=" + expires.toGMTString() +
    "; domain=notadouche.com" +
    "; path=/";
}


/* Check the cookie for a value.
 *
 * Returns true if the value is set, false otherwise.
 */
function check_cookie(name) {
  var val = get_cookie(name);
  return ((val == null || val == '') ? false : true);
}


/* "Erase" a cookie by giving it a negative expiration.
 */
function erase_cookie(name) {
  var value = '';
  var expires = new Date();
  expires.setDate(expires.getDate() - 1);

  document.cookie = name + "=" + escape(value) + 
    "; expires=" + expires.toGMTString() +
    "; domain=notadouche.com" +
    "; path=/";
}



/* NOTADOUCHE STUFF ===================================================== */

var xmlHttp;


/* Returns the base notadouche url.
 */
function get_base_url() {
  return 'http://www.notadouche.com';
}


/* Gets a user's id.
 */
function get_uid() {
  if (check_cookie('ruid')) {
    return get_cookie('ruid');
  } else if (check_cookie('uid')) {
    return get_cookie('uid');
  } else {
    return -1;
  }
}


/* Clears all notadouche cookies.
 */
function clear_cookies() {
  erase_cookie('uid');
  erase_cookie('ruid');
  erase_cookie('profile_name');
}


/* Parses a uid from a PHP response.
 */
function get_uid_from_response(response) {
  uid_re = /^(?:\s*)(\d+)\s/;
  uid_re.test(response);
  return RegExp.lastParen;
}


/* Parses a profile_name from a PHP response.
 */
function get_profile_name_from_response(response) {
  pn_re = /^(?:\s*)(?:\d+)\s(.*)(?:\s*)$/;
  pn_re.test(response);
  return RegExp.lastParen;
}


/* Gets a profile_name based on a uid.
 */
function set_profile_name_cookie(uid) {
  var script_url = "userinfo.php";
  script_url += "?uid=" + uid;
  script_url += "&want=profile_name";

  xmlHttp = getXmlHttpObject();

  xmlHttp.onreadystatechange = profile_state_changed;
  xmlHttp.open("GET", script_url, true);
  xmlHttp.send(null);
}

function profile_state_changed() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
    // return value is profile_name
    var profile_name = xmlHttp.responseText;

    set_cookie("profile_name", profile_name);
  }
}
