// utf-8 编码
function HttpClient()
{
this.host = getHost();
this.responseText = null;
this.messenger = null;
this.handler = null;
this.isBusy = false;
this.action = null;
this.UNINITIALIZED=0;
this.LOADING=1;
this.LOADED=2;
this.INTERACTIVE=3;
this.COMPLETE=4;

function getHost()
{
var u='http://'+window.location.host+'/';
return u;
}

function $(id) {return document.getElementById(id);}

this.setHandler = function()
{
if (this.handler != null && this.handler.readyState != 0 && this.handler.readyState != 4)
this.handler.abort();
try
{
this.handler = new XMLHttpRequest();
}
catch (error)
{
try
{
this.handler = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (error)
{
return false;
}
}
return true;
}

this.get = function(url,action)
{
if (this.setHandler())
{
var self = this;
this.action = action == null ? this.defaultAction : action;
this.handler.onreadystatechange = function()
{
self.executeAction();

return true;
};
this.handler.open("GET", this.host+url, true);
this.handler.send(null);
}
else return false;
}
this.post = function(url,arg,action)
{
if (this.setHandler())
{
var self = this;
this.action = action == null ? this.defaultAction : action;
this.handler.onreadystatechange = function()
{
self.executeAction();

return true;
};
this.handler.open('POST', this.host+url, true);
this.handler.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
this.handler.send(arg);
}
else return false;
}
this.executeAction = function()
{
if (this.handler.readyState == this.COMPLETE)
{
try
{
if (this.handler.status == '200')
{
this.responseText = this.handler.responseText;

this.action();
}
else
{
alert("There was an error while retrieving the URL:\n\n " + this.handler.statusText);
}
}
catch (error)
{
}
}

return true;
}

this.setAction = function(action)
{
if (this.isAvailable() && !isBusy)
{
this.action = action;
return true;
}
else
return false;
}

this.defaultAction = function()
{
var lines=this.responseText.split("\n");
var lineNum = lines.length;
for (var i=0; i<lineNum; i++)
{
var actions=lines[i].split('<!--XxX-->');

switch (actions[0])
{
case 'append':
this.append(actions[1],actions[2]);
break;
case 'replace':
this.replace(actions[1],actions[2]);
break;
case 'show':
$(actions[1]).style.display = 'block';
break;
case 'hide':
$(actions[1]).style.display = 'none';
break;
case 'alert':
alert(decodeURIComponent(actions[1]));
break;
case 'vlert':
vlert(decodeURIComponent(actions[1]));
break;
case 'remove':
var par = $(actions[1]);
var toDel = $(actions[2]);
if (par != null && toDel != null)
{
try
{
	par.removeChild(toDel);
}
catch (err)
{
alert(err);
}
};
break;
case 'showAfter':
setTimeout('$("'+actions[1]+'").style.display="block";',actions[2]*1000);
break;
case 'hideAfter':
setTimeout('$("'+actions[1]+'").style.display="none";',actions[2]*1000);
break;
case 'setSrc':
$(actions[1]).src = actions[2];
break;
case 'setText':
	if ($(actions[1]))$(actions[1]).innerText = actions[2];
	break;
case 'clear':
	if ($(actions[1]))$(actions[1]).value = '';
	break;
case 'setValue':
$(actions[1]).value = actions[2];
break;
case 'eval':
eval(actions[1]);
break;
}
}
}

this.append = function(id,value)
{
$(id).innerHTML += value;
}

this.replace = function(id,value)
{
$(id).innerHTML = value;
}

}
function vlert(m)
{
hideMessenger();
messenger=new jsPopupMessenger(280,150);
messenger.show(m);
hideTimer=setTimeout('hideMessenger()',1000*hideAfter);
}
