// (c)2010 Michael Holtstrom

function Xhr()
{dbg('Xhr constructor');try{this.obj=new XMLHttpRequest();}
catch(e){try{this.obj=new ActiveXObject('Msxml2.XMLHTTP');}
catch(e2){this.obj=new ActiveXObject('Microsoft.XMLHTTP');}}
this.hook=null;this.pending=false;};Xhr.prototype.listen=function()
{dbg('Xhr.listen '+this.obj.readyState);if(this.obj.readyState==4)
{var result=this.obj.responseText;if(this.obj.status!=200)
{result='0';}
this.pending=false;this.hook(result);this.hook=null;}};Xhr.prototype.cleanup=function()
{dbg('Xhr.cleanup');this.hook=null;this.obj.onreadystatechange=null;this.obj=null;};Xhr.prototype.join=function(name,email,password)
{dbg('Xhr.join '+this.pending);if(this.pending){return;}
this.pending=true;this.hook=function(res){main.loginjoin(res,false);}
dbg("cmd_join.php?a="+util.encode(name)+"&b="+util.encode(email)+"&c="+util.hide(password));var bound=this;this.obj.onreadystatechange=function(){bound.listen();}
this.obj.open("GET","cmd_join.php?a="+util.encode(name)+"&b="+util.encode(email)+"&c="+util.hide(password),true);this.obj.send(null);};Xhr.prototype.login=function(name,password)
{dbg('Xhr.login '+this.pending);if(this.pending){return;}
this.pending=true;this.hook=function(res){main.loginjoin(res,true);}
dbg("cmd_login.php?a="+util.encode(name)+"&b="+util.hide(password));var bound=this;this.obj.onreadystatechange=function(){bound.listen();}
this.obj.open("GET","cmd_login.php?a="+util.encode(name)+"&b="+util.hide(password),true);this.obj.send(null);};Xhr.prototype.save=function(sheet)
{dbg('Xhr.save '+this.pending);if(this.pending)
{err.msg('Sorry, but I\'m still working on your previous save request.');return;}
this.pending=true;this.hook=function(res){sheet.DOicoSaved(res);}
dbg("cmd_save.php?a="+sheet.dbid+"&b="+main.user_id+"&c="+sheet.stamp+"&d="+util.encode(localStorage['s'+sheet.aryid]));var bound=this;this.obj.onreadystatechange=function(){bound.listen();}
this.obj.open("GET","cmd_save.php?a="+sheet.dbid+"&b="+main.user_id+"&c="+sheet.stamp+"&d="+util.encode(localStorage['s'+sheet.aryid]),true);this.obj.send(null);};