/*the user should define ajaxform_callback function to deal with ajax success responses*/
var http_request = false;
function makeRequest(url, parameters) {
   http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
      	// set type accordingly to anticipated content type
         //http_request.overrideMimeType('text/xml');
         http_request.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }
   http_request.onreadystatechange = alertContents;
   http_request.open('GET', url + parameters, true);
   http_request.send(null);
}

function alertContents() {
   if (http_request.readyState == 4) {
      if (http_request.status == 200) {
         //alert(http_request.responseText);
         var result = http_request.responseText;          
         ajaxform_callback(result);
      } else {
         alert('There was a problem with the request.');
      }
   }
}

/*formid is the id of form
  actionURL is the url of form action
*/
function ajaxform(formid, actionURL) {
   var obj = document.getElementById(formid);
   var getstr = "?";
   var formChilds = obj.elements;
   for (i=0; i<formChilds.length; i++) {
      if (formChilds[i].tagName == "INPUT") {
         if (formChilds[i].type == "text") {
            getstr += formChilds[i].name + "=" + formChilds[i].value + "&";
         }
         if (formChilds[i].type == "hidden") {
            getstr += formChilds[i].name + "=" + formChilds[i].value + "&";
         }
         if (formChilds[i].type == "checkbox") {
            if (formChilds[i].checked) {
               getstr += formChilds[i].name + "=" + formChilds[i].value + "&";
            } else {
               getstr += formChilds[i].name + "=&";
            }
         }
         if (formChilds[i].type == "radio") {
            if (formChilds[i].checked) {
               getstr += formChilds[i].name + "=" + formChilds[i].value + "&";
            }
         }
      }   
      if (formChilds[i].tagName == "SELECT") {
         var sel = formChilds[i];
         getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
      }
      
   }

   alert(getstr);
   makeRequest(actionURL, getstr);
}

