 /*
 * AjaxObject is a hypothetical object that encapsulates the transaction
 *     request and callback logic.
 *
 * handleSuccess( ) provides success case logic
 * handleFailure( ) provides failure case logic
 * processResult( ) displays the results of the response from both the
 * success and failure handlers
 * call( ) calling this member starts the transaction request.
 */

var myObj;

var AjaxObject = {

  handleSuccess:function(o){
    // This member handles the success response
    // and passes the response object o to AjaxObject's
    // processResult member.
    // this.callAlert(o);
    this.processResult(o);
  },

  handleFailure:function(o){
    // Failure handler
    // alert("whoops - xhr problem");
    document.forms[0].testselect.options[0] = new Option("Unknown postcode", "");
    
  },

  processResult:function(o){
    // This member is called by handleSuccess
    // alert(o.responseText);
    myObj = eval( '(' + o.responseText + ')' );
    
    if (myObj.result == null)
    {
      window.status = "empty result list";
      document.forms[0].testselect.options[0] = new Option("Unknown postcode", "");

    }
    else
    {
      window.status = "got " + myObj.result.length + " items";
      document.forms[0].testselect.options[0] = new Option('Please select', 0);
      for(var i=0; i<(myObj.result.length); i++)
      {
        tmp = myObj.result[i].address1;
        if (myObj.result[i].housename !="") { tmp = myObj.result[i].housename + " " + tmp;}
        document.forms[0].testselect.options[i+1] = new Option(tmp, i+1);
      }
    }

  },

  startRequest:function(code) {
     YAHOO.util.Connect.asyncRequest('GET', '/pafproxy.aspx?postcode='+code, callback, null);
  }

};

/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */
var callback =
{
  success:AjaxObject.handleSuccess,
  failure:AjaxObject.handleFailure,
  timeout: 5000, // 5 second timeout
  scope: AjaxObject
};


function doit()
{
  var tmp;
  tmp = document.forms[0].postcode4paf.value;
  // convert to lower case
  tmp = tmp.toLowerCase();
  // strip punctuaction etc as \W means any non word character ie. not a-z or 0-9
  tmp = tmp.replace( /\W/g, '');
  
  // clear form items
  document.forms[0].testselect.options.length = 0;
  document.forms[0].housename.value = "";
  document.forms[0].address1.value = "";
  document.forms[0].address2.value = "";
  document.forms[0].address3.value = "";
  document.forms[0].county.value = "";
  document.forms[0].postcode.value = "";  

  // make the call
  AjaxObject.startRequest(tmp);
  return false;
}
  
function showaddress()  
{
  var frm = document.forms[0];
  var n = frm.testselect.options.selectedIndex;
  // alert(n);
  var addr = myObj.result[n-1];
  frm.housename.value = addr.housename;
  frm.address1.value = addr.address1;
  frm.address2.value = addr.address2;
  frm.address3.value = addr.address3;
  frm.county.value = addr.county;
  frm.postcode.value = addr.postcode;  
}

