var Chat = {
	input_id:		"input",
	submit_id:		"submit",
	display_id:		"chat",
	status_id:		"status",
	rep_id:			"rep",
	method:			"post",
	script:			"response.php",
	header_prefix:	"X-Chat-",
	waiting:		0,
	stall_delay:	180,
	stall_timer:	0,
	stall_possible:	0,
	header_key:		0,
	header_value:	0,
	idling:			0,

	delay:			0,

	make:			"",
	model:			"",
	year:			0,

	setup: function() 
	{
		try			{ this.pipe = new XMLHttpRequest(); }
		catch(e)	{ this.pipe = new ActiveXObject("Microsoft.XMLHTTP"); }

		document.getElementById(this.input_id).onkeypress = function(e) 
		{
			if(!e) var e = window.event;
			if(e.keyCode == 13) Chat.submit(document.getElementById(Chat.input_id).value);

			Chat.stall_reset();
			return(0);
		}		
		Chat.submit(' ');
	},

	submit:	function(s) 
	{
		if (s == '')
		{
			return(false);
		}
		
		if(this.waiting && s != '') 
		{
			return(false);
		}

		this.script	+= "?";
		if(this.make)	this.script += "make="	+ escape(this.make)		+ "&";
		if(this.model)	this.script += "model="	+ escape(this.model)	+ "&";
		if(this.year)	this.script += "year="	+ escape(this.year)		+ "&";

		if(s) this.change_status("Sending message...");
		this.pipe.open(this.method, this.script, true);
		if(this.header_key) 
		{
			this.pipe.setRequestHeader(this.header_key, this.header_value);
			this.header_key		= 0;
			this.header_value	= 0;
		}
		this.pipe.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		this.pipe.onreadystatechange = this.response;
		this.pipe.send("input=" + escape(s));
		this.waiting = 0;
		this.idling	= 1;
		Chat.idle();
		if(s != false) document.getElementById(this.input_id).value = "";
		return(true);
	},

	response: function() 
	{
		var state 			= Chat.pipe.readyState;

		Chat.delay = 0;

		if(state == 4) 
		{
			var d = new Date();

			Chat.stall_possible	= 0;
			Chat.change_status("Last message received: " + d.getHours() + ":" + Chat.pad(d.getMinutes(), 2) + ":" + Chat.pad(d.getSeconds(), 2));

			try		
			{ 
				Chat.delay = Chat.pipe.getResponseHeader(Chat.header_prefix + "Delay");
			}
			catch(e)
			{}

			try		
			{
				Chat.reroute = Chat.pipe.getResponseHeader(Chat.header_prefix + "Reroute");
				if(Chat.reroute) document.location = Chat.reroute;
			}
			catch(e)
			{}

			try		
			{
				Chat.rep = Chat.pipe.getResponseHeader(Chat.header_prefix + "Rep");
				if(Chat.rep) document.getElementById(Chat.rep_id).innerHTML = Chat.rep;
			}
			catch(e)
			{}

			try		
			{
				Chat.stall_possible = Chat.pipe.getResponseHeader(Chat.header_prefix + "Stall");
			}
			catch(e)
			{}

			document.getElementById(Chat.display_id).innerHTML	= Chat.pipe.responseText;
			document.getElementById(Chat.input_id).focus();

			if(Chat.delay > 0)	Chat.wait(Chat.delay);		

			Chat.stall_reset();
		}
	},

	change_status: function(s) 
	{
		document.getElementById(Chat.status_id).innerHTML = s;
	},

	wait: function(delay) 
	{
		setTimeout("Chat.submit(''); Chat.idling=0;", delay * 1000);
		Chat.waiting = 1;
		Chat.change_status("Waiting for response...");
	},

	stall: function() 
	{
		Chat.send_header(Chat.header_prefix + 'Stall', 1);
		Chat.submit(' ');
	},

	stall_reset: function() 
	{
		if(Chat.stall_timer) clearTimeout(Chat.stall_timer);
		if(Chat.stall_possible) 
		{
			Chat.stall_timer = setTimeout("Chat.stall();", Chat.stall_delay * 1000);
		}
	},

	send_header: function(key, value) 
	{
		Chat.header_key		= key;
		Chat.header_value	= value;
		return(true);
	},

	idle: function() 
	{
		return(false);
	},

	pad: function(d, len) 
	{
		d = d.toString();
		while(d.length < len) 
		{
			d = '0' + d;
		}
		return(d);
	}
	
}

