function prev_month(username, month, year, type) {
  if(!month || !year) return false; 
  
  if(month == 1) {
    month = 12;
    year--;
  }else{
    month--;
  }
  
  var data = 'username='+username+'&month=' + month + '&year=' + year;
  
  new Ajax('/browsemonths', {
		method: 'post',
		update: $('calendar'),
		data: data,
	}).request();
}

function next_month(username, month, year, type) {
  if(!month || !year) return false; 
  
  if(month == 12) {
    month = 1;
    year++;
  }else{
    month++;
  }
  var data = 'username='+username+'&month=' + month + '&year=' + year;
  
  new Ajax('/browsemonths', {
		method: 'post',
		update: $('calendar'),
		data: data,
	}).request();
}

function join() {
  var email = $('email-letter').getValue().trim().clean();
  
  if(!email.contains('@')) {
    alert('Please enter a valid email address.');
    return false;
  }
  
  new Ajax('/join', {update: $('response'), evalScripts: true, data: $('letter'), onComplete: function() {
    $('letter').reset();
  }}).request();
}

function post_comment() {
  var comment = $('comment-body').getValue().trim().clean();
  
  if(comment.length < 2) {
    alert('Your comment is too short.');
    return false;
  }
  
  new Ajax('/postcomment', {update: $('user-comments'), evalScripts: true, data: $('comment-form'), onComplete: function() {
    $('comment-form').reset();
  }}).request();
}

function delete_comment(commentid, itemid, type) {
  $('comment-'+commentid).remove();
  new Ajax('/deletecomment', {update: $('user-comments'), evalScripts: true, data: 'commentid='+commentid+'&itemid='+itemid+'&type='+type, onComplete: function() {}}).request();
}
