/***********************************************************************
zjax, Luis Breda, 2006 (Please, keep this header)
***********************************************************************/
window.zjax=function(m,u,d,t){
  var t=(t?'responseXML':'responseText');
  if (window.XMLHttpRequest){
    var r=new XMLHttpRequest();
  }else if(window.ActiveXObject){
    var r=new ActiveXObject('Microsoft.XMLHTTP');
  }    
  if (r){
    if (m==0){
      r.open('GET',u+'?'+d+'&r='+Math.random(),false);
      r.send(null);
    }else{
      r.open('POST',u,false);
      r.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      r.send(d);
    }
  }
  return r[t];
}
/***********************************************************************
::Instructions
zjax is a lightweight !aJAX function for working with XMLHttpRequest in
synchronous mode only. zjax() and functions below, echo() and serf(),
are the hard core of my 'web_no_refresh_page' applications :)
Luis Breda

:Parameters
 m = method (0=GET, other=POST)
 u = url (ie: deepthought.php)
 d = data (in form "ultimate='lu'&question='e')
 t = type (if defined='responseXML', if omitted='responseText')

:Usage examples (Thanks to DA)
  Getting from 'deepthought.php?ultimate=lu&question=e' 
  and show the response in element with 'id=luae'
     echo('luae',zjax(0,'deepthought.php','ultimate=lu&question=e'));

  Set variable 'luae' with the response using 'responseXML'
     var luae=zjax(0,'deepthought.php','ultimate=lu&question=e',1);

  Posting for 'scrabblemath.php' the variables
  'operation' 'six' 'nine' and 'result'.
     zjax(1,'scrabblemath.php','operation=multiply&six=6&nine=9&result=42');
  
  Posting for 'scrabblemath.php' the form with 'id=thgttg'.
     zjax(1,'scrabblemath.php',serf('thgttg'));

***********************************************************************/

window.echo=function(where,what){
 document.getElementById(where).style.display='block';
 document.getElementById(where).innerHTML=what;
}

window.serf=function(f){
  var f=document.getElementById(f);
  var l=f.length;
  var r='';
  for(var i=0;i<l;i++){
    r+='&'
    var e=f.elements[i];
    var t=e.type;
    if(t=='checkbox'){
      r+=e.name+'='+e.checked;
    }else if(t=='radio'&&e.checked){
      r+=e.name+'='+e.value;    
    }else{
      r+=e.name+'='+e.value;
    }
  }
  return r.substring(1);
}
//***********************************************************************


