
var offensive_url  = 'http://www.epocrates.com/insights/wp-content/plugins/offensive/offensive.rpc.php';
var offensive_ajax = new sack(offensive_url);

//  Send user's response to serve via AJAX
function offensive_comment(comment_id, post_id){
  // if (!confirm('Do you want to report this to Epocrates?')) return;
  hide('mark_as_offensive-'+comment_id+'-comment-area');
  update('mark_as_offensive-'+comment_id, 'Sending...');  
  comment  = document.getElementById("mark_as_offensive-"+comment_id+"-comment");
  offensive_ajax.reset();
  offensive_ajax.setVar("comment_id", comment_id);
  offensive_ajax.setVar("post_id", post_id);
  offensive_ajax.setVar("offensive_comment", comment.value);
  offensive_ajax.method = 'GET';
  offensive_ajax.element = 'element';
  offensive_ajax.onCompletion = function () {completionCallback(comment_id)};
  // offensive_ajax.onCompletion = function () {alert(offensive_ajax.response);};
  offensive_ajax.onError = function () {errorCallback(0)};
  offensive_ajax.runAJAX();
}
//  Completion Callback
function completionCallback(comment_id){
  if (offensive_ajax.response == 'success'){
    update('mark_as_offensive-'+comment_id, 'Thank you for your feedback. Your comment has been recorded & will be reviewed by Epocrates staff.');
    // Set Cookie for this user & comment (to prevent multiple complaints on 1 commment by 1 user)
    offensive_createCookie('complaint_'+comment_id, true, 365)
  } else {errorCallback(offensive_ajax.response);} 
}
//  Error Callback
function errorCallback(s){
  msg = ""
  if (s!=0) msg += 'Slow Down...  It appears that you just sent feedback '+s+' seconds ago\n.'
            msg += 'Comment was not recorded - please try again later.'
  alert(msg);
}

// REMOVED -- now accomplizhed via scriptaculous Effect.
//function toggle(e){
//	var x = document.getElementById(e);
//	if (x.style.display == 'none') {show(e)}
//    else {hide(e)}
//}

function show(e){ var x = document.getElementById(e); x.style.display = '';}
function hide(e){ var x = document.getElementById(e); x.style.display = 'none';}
function update(e,v){
  var x = document.getElementById(e); 
  x.innerHTML = v
}

function offensive_createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}