var xmlHttp = createRequestObject();
var xmlHttp2 = createRequestObject();
var processor = '/code/specials-generator.php';

function createRequestObject()
{
	var request;
	
	try
	{
		request = new XMLHttpRequest();
	}
	catch(e)
	{
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP");
		
		for(var i=0; i<XmlHttpVersions.length && !request; i++)
		{
			try
			{
				request = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch(e)
			{}
		}
	}
	
	if (!request)
	{
		alert("Error creating the XMLHttpRequest object.");
	}
	else
	{
		return request;
	}
}

function buildSelect()
{
	if(xmlhttp.readyState == 0 || xmlhttp.readyState == 4)
	{
		var url = processor + '?type=buildSelect';
		url += '&rand=' + parseInt(Math.random() * 999999999);
		xmlhttp.open('get', url, true);
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState == 4) 
			{
				if (xmlhttp.status == 200) 
				{
					$("#location-options").append("<div><select onchange='buildSpecials(\"buildSpecials\", this.value)' style='font-size: .8em; width: 300px;'>" + xmlhttp.responseText + "</select></div>");
					buildSpecials("buildSpecials", "0");
				}
			}			
		};
		xmlhttp.send(null);
	}
	else
	{
		setTimeout('buildSelect()', 30);
	}
}

function buildSpecials(type, location_id)
{
	if(xmlhttp2.readyState == 0 || xmlhttp2.readyState == 4)
	{
		var url = processor + '?type=' + type + '&location_id=' + location_id;
		url += '&rand=' + parseInt(Math.random() * 999999999);
		xmlhttp2.open('get', url, true);
		xmlhttp2.onreadystatechange = function()
		{
			if(xmlhttp2.readyState == 4) 
			{
				if(xmlhttp2.status == 200) 
				{
					$("#weekly-specials").html(xmlhttp2.responseText);
				}
			}
		};
		xmlhttp2.send(null);
	}
	else
	{
		setTimeout('buildClearance(' + location_id + ', ' + store_id + ')', 30);
	}
}