//
// Form AJAX toolkit
//

var http_request = false;
var current_action=-1;

var form_processor_url= new Array();
var form_processor_return= new Array();

function PrepareFormProcessing(ix,post_url,retfunc)
{
    form_processor_url   [ix]=post_url;
    form_processor_return[ix]=retfunc;
}

function makePOSTRequest(url, parameters) {

  http_request = false;

//  alert(parameters);

  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');
     current_action=-1;
     return false;
  }

  http_request.onreadystatechange = alertContents;
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}


function alertContents() {

    if (http_request.readyState == 4) {
        if (http_request.status == 200) {

            result = http_request.responseText;

            form_processor_return[current_action](result);
            current_action=-1;

//            document.getElementById('myspan').innerHTML = result;
//            alert("Thank you for rating this pic!");
        }
        else {
             alert('AJAX request problem');
             current_action=-1;
        }
    }
}

function get(ix,obj) {

  var getstr = '';
  if (current_action!=-1) {
//      alert("Action Conflict");
      return;
  }
  current_action=ix;

  getstr=get_fields(obj);

  // makePOSTRequest('/vote.asp', getstr);
  makePOSTRequest(form_processor_url[ix], getstr,ix);
}

// recursive DOM search

function get_fields(obj)
{
var str='';

    if (obj.childNodes.length > 0) {
        for (var i=0;i<obj.childNodes.length;i++) {
            str += get_fields(obj.childNodes[i]);
        }
     }

     if (obj.tagName == "INPUT") {
        if (obj.type == "hidden") {
           str += obj.id + "=" + escape(obj.value) + "&";
        }
        if (obj.type == "text") {
           str += obj.id + "=" + escape(obj.value) + "&";
        }
        if (obj.type == "checkbox") {
           if (obj.checked) {
              str += obj.id + "=" + obj.value + "&";
           }
           else {
              str += obj.id + "=&";
           }
        }
        if (obj.type == "radio") {
           if (obj.checked) {
              str += obj.name + "=" + obj.value + "&";
           }
        }
     }
     if (obj.tagName == "select") {
        var sel = obj;
        str += sel.id + "=" + sel.options[sel.selectedIndex].value + "&";
     }
     if (obj.tagName == "TEXTAREA") {
        var ta = obj;
        str += ta.id + "=" + escape(ta.value) + "&";
     }

    return str;
}





