var xmlHttp = createXmlHttpRequestObject(); 

function createXmlHttpRequestObject()
{	
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;

  // if running Internet Explorer
  if(window.ActiveXObject) {
    try {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) {
      xmlHttp = false;
    }
  } else {
    try {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp) {
    alert("Error creating the XMLHttpRequest object.");
    return NULL;
  } else {
    return xmlHttp;
  }
}

function makeRequest(url, handler) {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = handler;

    xmlHttp.send(null);
  }
}


function trim(str) {
  return str.replace(/^\s*|\s*$/g,"");
}

function CheckEmail(field) {
  var filter = /^[-A-Za-z0-9_\+\.]+@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,4}$/;

  var is_ok = filter.test(field.value);

  if (!is_ok) {
    field.focus();
  }
  return is_ok;
}

function CheckTextField(inTextField) {
  var is_ok = (trim(inTextField.value) != "") && (inTextField.value != "undefined");

  if (!is_ok) {
    inTextField.focus();
  }
  return is_ok;
}

function CheckUploadForm(form) {
  if (!CheckTextField(form.name)) {
    alert("Name is required.");
    return false;
  }

  if (!CheckEmail(form.email)) {
    alert("Please enter a valid e-mail address.");
    return false;
  }

  if (!CheckTextField(form.comment_text)) {
    alert("Comment is required.");
    return false;
  }

  if (!CheckTextField(form.code)) {
    alert("Verification code is required.");
    return false;
  }

  return true;
}

function GetTimeStamp() {
  var date = new Date();
  return date.getTime();
}

function RequestModeration(commentid) {
  var comment_div = document.getElementById("comment" + commentid);
  var report_form = document.getElementById("report" + commentid);
  var report_link = document.getElementById("reportlink" + commentid);
  if (comment_div.className == "comment-text") {
    report_link.innerHTML = "Report (-)";
    comment_div.className = "comment-text-report";
    var timestamp = GetTimeStamp();

    report_form.innerHTML = "<b>Report Comment</b> <br />Verify: <img class='verification-image'width='100' height='25' src='/include/captcha.img.php?id=" + commentid + "&amp;width=100&amp;height=25&amp;tm=" + timestamp + "' alt='Verification Code' /> <input type='text' id='verify" + commentid + "' name='code' maxlength='4' style='width: 60px;' />  <input id='reportbutton" + commentid + "' type='button' style='width:55px;' value='Report' onclick='ReportComment(" + commentid + ");' class='submit-button' />";
    report_form.className = "report-comment-form";
    var field = document.getElementById("verify" + commentid);
    field.focus(); 
   
  } else {
    report_link.innerHTML = "Report";
    comment_div.className = "comment-text";
    report_form.innerHTML = "";
    report_form.className = "";
  }
}


function FinishReportComment(commentid, response) {
  var report_form = document.getElementById("report" + commentid);

  if (response == "ok") {
    report_form.innerHTML = "<span style='color:green;'>The comment has been reported.</span>";
    comment_div = document.getElementById("comment-" + commentid);
    if (comment_div) {
      comment_div.innerHTML = "<span style='color:red'>*</span>" + comment_div.innerHTML;
    }
  } else if (response == "wrongcode") {
    wrong_message = document.getElementById("wrongmessage" + commentid);
    if (!wrong_message) {
      report_form.innerHTML = report_form.innerHTML + " <span id='wrongmessage" + commentid + "' style='color:red;'>Wrong verification code.</span>";
    }
    var report_button = document.getElementById("reportbutton" + commentid);
    report_button.disabled = false;
  }
}

var currentCommentID = 0;

function ReportcCommentHandler() {
  if (xmlHttp.readyState == 4) {
    FinishReportComment(currentCommentID, xmlHttp.responseText);
  }
}


function ReportComment(commentid) {
  currentCommentID = commentid;

  var verify_field = document.getElementById("verify" + commentid);
  var report_button = document.getElementById("reportbutton" + commentid);
  var requestURL = "/report.comment.php?id=" + commentid + "&vc=" + verify_field.value;
  makeRequest(requestURL, ReportcCommentHandler);
  report_button.disabled = true;
}
