
function ajax_wall_post(formEl, wallEl) {
  var ajaxUrl = '/ajax/wallpost_ajax.php';
  var ajax = new Ajax();
  var wallStatus = ge('wall_status');
  var wallError = ge('wall_error');
  var AOK = true;

  hide(wallError);
  wallStatus.innerHTML = '<h2>Posting...</h2>';
  show(wallStatus);

  ajax.onDone = function (ajaxObj, responseText) {
    var responseObj = eval("("+responseText+")");
    // errors
    if (responseObj['err']) {
      AOK = false;
      var wallErrString = '<h2>'+responseObj['errTitle']+'</h2>';
      if (responseObj['errBody']) {
        wallErrString = wallErrString + '<p>'+responseObj['errBody']+'</p>';
      }
      wallError.innerHTML = wallErrString;
      show(wallError);
      hide(wallStatus);
    }
    // status
    if (responseObj['st']) {
      AOK = false;
      var wallStString = '<h2>'+responseObj['stTitle']+'</h2>';
      if (responseObj['stBody']) {
        wallStString = wallStString + '<p>'+responseObj['stBody']+'</p>';
      }
      wallStatus.innerHTML = wallStString;
    }
    // new wall post
    if (responseObj['wp']) {
      var postHack = document.createElement('div');
      postHack.innerHTML = responseObj['wp'];
      var post = postHack.firstChild;
      wallEl.insertBefore(post, wallEl.firstChild);
      // 10 or more - last guy falls off
      if (formEl.wall_count.value >= 10) {
        hide(wallEl.lastChild);
      } else {
        formEl.wall_count.value++;
      }
      var wall_subtitle = ge('wall_subtitle');
      formEl.wall_total.value++;
      wall_subtitle.innerHTML = get_wall_subtitle(formEl.wall_count.value, formEl.wall_total.value, formEl.id.value);
      // make spamming hard since the wall post was definitely added

      var txt = formEl.text;
      txt.is_focused = 0;
      txt.value = formEl.text.getAttribute("placeholder");
      txt.style.color = '#777';

      if (AOK) {
        wallStatus.innerHTML = '<h2>Post successful!</h2>';
        setTimeout("hide(ge('wall_status'))", 2000);
      }
    }
  }

  ajax.onFail = function (ajaxObj) {
    wallError.innerHTML = '<h2>Wall is currently unavailable.</h2><p>Please try again later.</p>';
    show(wallError);
    hide(wallStatus);
  }

  params = 'to='+formEl.id.value+'&from='+formEl.user.value+'&text='+encodeURIComponent(formEl.text.value)+'&post_form_id='+formEl.post_form_id.value;
  ajax.post(ajaxUrl, params);
}

/*
 * Transforms an integer string such as "1234567"
 * into a string like "1,234,567"
 */
function formatInt(numstr, sep) {
  var mod = numstr.length%3;
  var loop_pos = mod == 0 ? 3 : mod;
  var res = new Array( numstr.substring(0,loop_pos) );
  while( loop_pos < numstr.length ) {
    var next_pos = loop_pos + 3;
    res.push( numstr.substring( loop_pos, next_pos ) );
    loop_pos = next_pos;
  }
  return res.join( sep ? sep : ',' );
}

function get_wall_subtitle(wallCount, wallTotal, wallId) {
  var wallSubtitle = '';
  var wallLink = '/wall.php?id='+wallId;

  if (wallCount < wallTotal) {
    wallSubtitle = 'Displaying '+wallCount+' of <a href="'+wallLink+'">'+formatInt(wallTotal.toString())+' wall posts</a>.';
  } else if (wallCount == 2) {
    wallSubtitle = 'Displaying the only 2 wall posts.';
  } else if (wallCount > 1) {
    wallSubtitle = 'Displaying all '+wallCount+' wall posts.';
  } else if (wallCount == 1) {
    wallSubtitle = 'Displaying the only wall post.';
  } else {
    wallSubtitle = 'No wall posts.';
  }
  return wallSubtitle;
}

