

///////////////////////////////////////////////////////////////////////////////
// function to handle an ajax request and pass the response onto a url
// url:  web page to call eg proc.php
// form: name of the form, all fields will be POST'd to the web page
// rqst: Used to identify what item generated the ajax request
///////////////////////////////////////////////////////////////////////////////
function ajaxRequest(url,form,rqst)
  {
  var data=$(form).serialize();
            //add a random number to prevent the server from using a cached file
  data="form="+form+"&rndm="+Math.random()+"&rqst="+rqst+"&"+data;

//alert(url);
//alert(data);

  var aj=new Ajax.Request(url, {method: 'post', parameters: data, onComplete: getResponse});
  }


////////////////////////////////////////////////////////////////////////////////////
// getResponse expects transport to contain fieldname,value pairs formatted as json
// The fieldname from the pair will be set to display the value from the pair
////////////////////////////////////////////////////////////////////////////////////
function getResponse(transport,jsn)
  {
  var data = eval(transport.responseText);
//alert(transport.responseText);
  for (var i=0; i<data.length; i++)
    {
    var item = data[i];
    for(p in item)
      {
      var type=p.substr(0,2);
      if(type=="V:")      //fields that should have the value set are preceded by V:
        {
        var fn=p.substr(2);      //get rid of the V:
        $(fn).value=item[p];     //set the value, not innerHTML
        }
      else if(type=="S:")        //fields that should have the src set are preceded by S:
        {
        var fn=p.substr(2);      //get rid of the S:
        $(fn).src=item[p];       //set the src, not innerHTML
        }

      else
        $(p).innerHTML=item[p];
      }
    }
  }


