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,
	address:		"",
	zip:			"",
	gui:			"conversation03.php",
	catalog:		"catalog",
	phonenum:		"800-248-8720",
	zipDesc:		"zip code",
	modal_wait:		null,
	
	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(' ');

		Chat.modal_wait = new YAHOO.widget.Panel
		("wait", 
			{ 
				width:"240px", 
				fixedcenter:true, 
				underlay:"shadow", 
				close:false, 
				draggable:false, 
				modal:true, 
				effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
			} 
		);

		Chat.modal_wait.setHeader("<span style='text-align:center'>Please wait while a rep <br /> gathers your information...</span>");
		Chat.modal_wait.setBody("<img src=\"images/loading.gif\"/>");
		Chat.modal_wait.render(document.body);
		Chat.modal_wait.show();
	},

	submit:	function(s) 
	{
		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(this.gui)	this.script += "gui="	+ escape(this.gui)		+ "&";
		if(this.catalog)	this.script += "catalog="	+ escape(this.catalog)		+ "&";
		if(this.phonenum)	this.script += "phonenum="	+ escape(this.phonenum)		+ "&";

		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.zip = Chat.pipe.getResponseHeader(Chat.header_prefix + "Zip");
				if(Chat.zip) 
				{
					Chat.getshops(Chat.zip);
				}
			}
			catch(e)
			{}

			try		
			{
				Chat.address = Chat.pipe.getResponseHeader(Chat.header_prefix + "Address");
				if(Chat.address) Chat.loadmap(Chat.address);
			}
			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)
			{}
			
			if (Chat.pipe.responseText.length > 50)
			{
				Chat.modal_wait.hide();
			}
			res = Chat.pipe.responseText;
			res = res.replace(/zip code/g, Chat.zipDesc);
			document.getElementById(Chat.display_id).innerHTML	= res;

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

			Chat.stall_reset();
			document.getElementById(Chat.input_id).focus();
		}
	},

	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);
	},
	loadmap: function(address) 
	{
		if (GBrowserIsCompatible())
		{
			// var map = new GMap2(document.getElementById("map"));
			// map.setCenter(new GLatLng(37.4419, -122.1419), 13);

			document.getElementById("map").style.display = 'block'; 
			var map = new GMap2(document.getElementById("map"));
			var geocoder = new GClientGeocoder();
		  
			geocoder.getLatLng(
				address,
				function(point) 
				{
					if (!point) 
					{
						alert(address + " not found");
					} 
					else 
					{
						map.addControl(new GSmallMapControl());
						map.addControl(new GMapTypeControl());
						map.setCenter(point, 13);
						var marker = new GMarker(point);
						map.addOverlay(marker);
						marker.openInfoWindowHtml(address);
					}
				}
			);
		}
	},
	getshops: function()
	{
		return (false);
	}
	
}

function loadpage(client, server, make, model, catalog, phonenumber, zipdesc)
{
	var loader = new YAHOO.util.YUILoader({
		base: "",
		require: ["container","yahoo"],
		loadOptional: true,
		combine: true,
		filter: "MIN",
		allowRollup: true,
		onFailure: function() {alert('loading failure');}, 
		onSuccess: function() 
		{
			initChat(client, server, make, model, catalog, phonenumber, zipdesc);
			load(); // google maps
		}
	});

	// Load the files using the insert() method.
	loader.insert();	
}

function initChat(client, server, make, model, catalog, phonenumber, zipdesc)
{
	Chat.script	= server;
	Chat.make	= make;
	Chat.model  = model;
	Chat.gui	= client;
	Chat.getshops = getshops;
	Chat.catalog = catalog;
	Chat.phonenum = phonenumber;
	Chat.zipDesc = zipdesc;
	
	Chat.setup();
}
