
var xhttp      = createRequestObject();
var xAction    = 'save';
var xListingid = 0;
var xStatus    = 0;

function sndResToggle() {
  var d = document.getElementById(xListingid);

  if (document.getElementById(xListingid)) {
    if (xAction == 'save') {
      d.innerHTML = '<img title="Delete from My Listings" border=0 src=http://www.homefind.com/images/saved.gif>';
      //alert(d.onclick);
      d.onclick   = new Function('sndResState(\'delete\', \''+xListingid+'\'); return false;');
      sndClass(xListingid+'_color','saved');
    } else {
      d.innerHTML = '<img title="Save to My Listings" border=0 src=http://www.homefind.com/images/notSaved.gif>';
      //alert(d.onclick);
      d.onclick   = new Function('sndResState(\'save\' , \''+xListingid+'\'); return false;');
      sndClass(xListingid+'_color','norm');
    }
  }
}

function sndResState(action,listingid) {
  if (!xhttp) {
//    return;
    alert(1);
  } else {
    xAction    = action;
    xListingid = listingid;

    xhttp.open('get', '/ajax/rpcResState.bg?action='+action+'&listingid='+listingid);
    xhttp.onreadystatechange = xhandleResponse;
    if (xhttp.readyState == 1) { xhttp.send(null); }
  }
}

function xhandleResponse() {
  if (xhttp.readyState == 4){
    var response = xhttp.responseText;
    var update = new Array();

    if(response.indexOf('|||' != -1)) {
      update = response.split('|||');
      document.getElementById(update[0]).innerHTML = update[1];
    }
  }
  sndResToggle();
}
