function create_request() { var request = undefined; try { request = new XMLHttpRequest(); } catch (trymicrosoft) { try { request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (othermicrosoft) { try { request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (failed) { request = false; }; }; }; return request; };
function ajax_post(url_str, post_str, success_fnc, error_fnc, async) { var request = create_request(); if((async == undefined) || (async == '')) var async = true; if((post_str == undefined) || (post_str == '')) var post_str = true; request.onreadystatechange = function() { if(request.readyState == 4) { if(request.status == 200) { if((success_fnc != undefined) && (success_fnc != '')) eval(success_fnc+'(request);'); else if((error_fnc != undefined) && (error_fnc != '')) eval(error_fnc+'(request)'); }; }; }; request.open('POST', url_str, true); request.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); request.send(post_str); };
function ajax_get(url_str, success_fnc, error_fnc, async) { var request = create_request(); if((async == undefined) || (async == '')) var async = true; request.onreadystatechange = function() { if(request.readyState == 4) { if(request.status == 200) { if((success_fnc != undefined) && (success_fnc != '')) eval(success_fnc+'(request);'); else if((error_fnc != undefined) && (error_fnc != '')) eval(error_fnc+'(request)'); } else if((error_fnc != undefined) && (error_fnc != '')) eval(error_fnc+'(request)'); }; }; request.open('GET', decodeURI(url_str), async); request.send(null); return true; };
var mouse_x = 0; var mouse_y = 0;
function password_focus(elm) { if((elm.style.color == "") || (str_rgb2hex(elm.style.color) == "#999999")) { elm.value = ""; elm.style.color = "#333333"; elm.style.fontStyle = "normal"; }; };
function password_blur(elm) { if((elm.value == "") && (str_rgb2hex(elm.style.color) == "#333333")) { elm.style.color = "#999999"; elm.style.fontStyle = "oblique"; elm.value = "__notused__"; }; };

function getElm(elementId) { return document.getElementById(elementId); }
function isset(checkObject) { return ( ((typeof checkObject != 'undefined') && (checkObject != null))?true:false); }
function issetElm(elementId) { return isset(getElm(elementId)); }

function ajax_facebook_login() {
  ajax_get('ajax_facebook_login.php', 'facebook_login_done');  
  return false;
}

function facebook_login_done(request) {
  eval('var ajax_response = ' + request.responseText);
  if(ajax_response.success == 0) {
    alert('Sorry! An error occured... Please try again later');
  } else if(ajax_response.success == 1) {
    document.location = 'http://slapnotes.com/?dummy=1328497311';
  } else if(ajax_response.success == 2) {
    document.location = '/settings.php?_pop_it_=1';
  }
  //document.location = 'http://slapnotes.com/?dummy=1328497311';
  return false;
}

function ajax_facebook_connect() {
  ajax_get('ajax_facebook_connect.php', 'facebook_connect_done');  
  return false;
}

function facebook_connect_done(request) {
  document.location = 'http://slapnotes.com/?dummy=1328497311';
}

function do_login() {
  var username = document.getElementById('username').value;
  var userpass = document.getElementById('userpass').value;
  var login_but = document.getElementById('login_but');
  var username_ok = false;
  var userpass_ok = false;
  if((typeof username != 'undefined') && (username != "") && (username != "username")) {
    username_ok = true;
  } else {
    alert('please provide a username');
  };
  if((typeof userpass != 'undefined') && (userpass != "") && (userpass != "__notused__")) {
    userpass_ok = true;
  } else {
    alert('please provide a password');
  };
  if(username_ok && userpass_ok) {
    login_but.disabled = true;
    login_but.value = "wait...";
    var post_str = 'username=' + encodeURIComponent( username );
    post_str = post_str + '&userpass=' + encodeURIComponent( userpass );
    ajax_post('ajax_login.php', post_str, 'login_done');
  };
  return false;
};

function login_done(request) {
  eval('var ajax_response = ' + request.responseText);
  if(ajax_response.success == 0) {
    var login_but = document.getElementById('login_but');
    login_but.value = "log in";
    login_but.enabled = true;
    alert('wrong username and/or password');
  } else {
    var cur_location = new String(document.location);
    cur_location = cur_location.replace(/(\?|\&)dummy=\d\.\d+/g, "");
    if(cur_location.indexOf("?") == -1) document.location = cur_location + "?dummy="+Math.random();
    else document.location = cur_location + "&dummy="+Math.random();
  };
};

function logout() { document.cookie="users_id=none; domain=slapnotes.com; path=/; expires=Monday, 19-Aug-1996 05:00:00 GMT"; var cur_location = new String(document.location); cur_location = cur_location.replace(/(\?|\&)dummy=\d\.\d+/g, ""); if(cur_location.indexOf("?") == -1) document.location = cur_location + "?dummy="+Math.random(); else document.location = cur_location + "&dummy="+Math.random(); return false;};

function forgot_password() {
  remove_helper(this, 'errusername');
  var forgotlink = document.getElementById('forgotlink');
  forgotlink.innerHTML = 'Checking username...';
  var username = document.getElementById('username').value;
  if((typeof username == 'undefined') || (username == "username")) username = "";
  if(username == "") {
    var username_pos = get_topleft(document.getElementById('username'));
    show_helper(this, 'tl', 'errusername', 'please provide a username to check', (parseInt(username_pos.top+6)), (parseInt(username_pos.left+60)));
    document.getElementById('errusernamehelper').onmouseout = function() { remove_helper(this, 'errusername'); };
    forgotlink.innerHTML = 'I forgot my password';
  } else {
    var post_str = 'username=' + encodeURIComponent( username );
    ajax_post('ajax_forgot_password.php', post_str, 'forgot_password_done');
  }; return false;
};

function forgot_password_done(request) {
  var forgotlink = document.getElementById('forgotlink');
  forgotlink.innerHTML = 'I forgot my password';
  eval('var ajax_response = ' + request.responseText);
  if(ajax_response.success == 0) {
    var username_pos = get_topleft(document.getElementById('username'));
    if(ajax_response.errors.no_username == 1) {
      show_helper(this, 'tl', 'errusername', 'please provide a username to check', (parseInt(username_pos.top+6)), (parseInt(username_pos.left+60)));
    } else if(ajax_response.errors.wrong_username == 1) {
      show_helper(this, 'tl', 'errusername', 'the username could not be found', (parseInt(username_pos.top+6)), (parseInt(username_pos.left+60)));
    } else if(ajax_response.errors.sending_failed == 1) {
      show_helper(this, 'tl', 'errusername', 'username found, but the e-mail failed...', (parseInt(username_pos.top+6)), (parseInt(username_pos.left+60)));
    };
    document.getElementById('errusernamehelper').onmouseout = function() { remove_helper(this, 'errusername'); };
  } else {
    var username_pos = get_topleft(document.getElementById('username'));
    show_helper(this, 'tl', 'emailsend', 'e-mail sent - check your inbox (and spam)', (parseInt(username_pos.top+6)), (parseInt(username_pos.left+60)));
    document.getElementById('emailsendhelper').onmouseout = function() { remove_helper(this, 'emailsend'); };
  };
};

function shorten(str, str_length, str_append) {
  if((typeof str_length == 'undefined') || (str_length == '')) str_length = 200;
  if((typeof str_append == 'undefined') || (str_append == '')) str_append = '...';
  if(str.length > str_length) {
    str = str.substr(0, (str_length - str_append.length));
    if(str.lastIndexOf(' ') > -1) str = str.substr(0, str.lastIndexOf(' '));
    str = str + str_append;
  }
  return str;
}
