// Forum message / comment submission.
// This module requires the jquery library.

var aForumForm  = null;
var aCreateUrl  = null;
 
// extract fields from form         
 function parseForm() {
  var params = {};
  var formEle = document.getElementById(aForumForm);
  if (formEle == null) {
    alert("Form not found:"+aFormId);
    return;
  }

  var a = formEle.getElementsByTagName('textarea');
  if (a != null) {
    for(i=0; i<a.length; i++) {
      params[a[i].name] = a[i].value;
    }
  }
  a = formEle.getElementsByTagName('input');
  if (a != null) {
    for(i=0; i<a.length; i++) {
      if (a[i].type == "text" || a[i].type == "hidden") {
        params[a[i].name] = a[i].value;
      }
      else if (a[i].type == 'checkbox') {
        if (a[i].checked) {
          params[a[i].name] = a[i].value;
        } else {
          params[a[i].name] = "";
        }
      }
      else if (a[i].type == "radio") {
        if (a[i].checked) {
          params[a[i].name] = a[i].value;
        }
      }
    }
  }
  a = formEle.getElementsByTagName('select');
  if (a != null) {
    for(i=0; i<a.length; i++) {
      if (a[i].selectedIndex >= 0) {
        params[a[i].name] = a[i].options[a[i].selectedIndex].value;
      }
    }
  }
  return params;
}

// submit a comment / forum message.
function processForum() {
//  var aForumMessageUrl;

  try {
    
    var params = parseForm();

    $("body").ajaxError(function(event, request, settings, errorThrown){
       //$(this).append("<li>Error requesting page " + settings.url + "</li>");
       alert("Ajax error: "+request.statusText+" ("+request.status+")"+
         "\nRequesting page:"+settings.url);
       window.location.reload();
     });

    $.get(aCreateUrl, null,function(data){
        var aForumMessageUrl = data;

        $.post(aForumMessageUrl, params,   // params are key=value pairs
          function(data){
            var str = window.location.href;
            if (str.indexOf("frompost=1") == -1){
              window.location = window.location + "\&frompost=1";
            }else{
              window.location.reload();
            }  

          });
      });

  }
  catch(e) {
    alert("Error: "+e.message);
  }
}

