function flagLetter(permaKey) {
  flagTextID = 'abuse_comment_'+permaKey;

  if ($(flagTextID).value.length > 255) {
    $(flagTextID).value = $(flagTextID).value.substr(0,255);
  }
  flagText = $(flagTextID).serialize();
  new Ajax.Request('/flag.php?pk='+permaKey+'&'+flagText, {
          method: 'get',
	  onSuccess: function(transport, json){
  	    var response = transport.responseText || "no response text";
	    if (json.success == true) {
	      hideAds();
	      Dialog.alert($('flag_success').innerHTML,
			   {width:300, height:100, okLabel: "Close",
			       onOk: function(win) { showAds(); return true;}});
		$('report_abuse_div_'+permaKey).toggle();
		$(flagTextID).value = '';
	    } else {
	      alertContent = '<div class="letters_alert"><div class="letters_text">'+json.messages[0]+'</div></div>';
	      hideAds();
	      Dialog.alert(alertContent,
			   {width:300, height:100, okLabel: "Close",
			       onOk: function(win) { showAds(); return true;}});
	    }
	  },
	  onFailure: function(){ alert('Something went wrong...') }
      });
  return false;
}

function checkLogin(permaKey) {
  //The logged_id variable is initialized in the head of the doc
  //by translating a Smarty variable to javascript in the global scope
  if (logged_in) {
    $('report_abuse_div_'+permaKey).toggle();
  } else {
    hideAds();
    Dialog.alert($('flag_login').innerHTML,
		 {width:300, height:100, okLabel: "Close", 
		     onOk: function(win) { showAds(); return true;}});
  }
    return false;
}

function hideAds() {
  var ad_divs = $$('.ad_content');
  for (i = 0; i<ad_divs.length; i++) {
    ad_divs[i].style.visibility = 'hidden';
  }
}

function showAds() {
  var ad_divs = $$('.ad_content');
  for (i = 0; i<ad_divs.length; i++) {
    ad_divs[i].style.visibility = 'visible';
  }
}

