var xmlhttp = createRequestObject();
var xmlhttp2 = createRequestObject();
var numberOfStores = 4;
var ddmenuitem = 0;

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($("#location-options").children().size() < numberOfStores)
	{
		if(xmlhttp.readyState == 0 || xmlhttp.readyState == 4)
		{
			var url = '/code/clearance-generator.php?type=buildSelect';
			url += '&rand=' + parseInt(Math.random() * 999999999);
			xmlhttp.open('get', url, true);
			xmlhttp.onreadystatechange = function()
			{
				if (xmlhttp.readyState == 4) 
				{
					if (xmlhttp.status == 200) 
					{
						var id = "store" + $("#location-options").children().size();
						$("#location-options").append("<div><select id='" + id + "' onchange='buildClearance(\"buildClearance\", this.value, this.id)' style='font-size: .8em; width: 300px;'><option value='0' selected='selected'>Select a location</option>" + xmlhttp.responseText + "</select> <input type='button' value='Add another location' onclick='buildSelect()' /></div>");
						$("#store-clearance").append("<div id='" + id + "clearance'></div>");
					}
				}			
			};
			xmlhttp.send(null);
		}
		else
		{
			setTimeout('buildSelect()', 30);
		}
	}
}

//For each store, make sure to change the location_id vvv
function buildClearance(type, location_id, div_id)
{
	if(location_id != 0)
	{
		if(xmlhttp2.readyState == 0 || xmlhttp2.readyState == 4)
		{
			var url = '/code/clearance-generator.php?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) 
					{
						var id = "#" + div_id + "clearance";
						$(id).html(xmlhttp2.responseText);
					}
				}
			};
			xmlhttp2.send(null);
		}
		else
		{
			setTimeout('buildClearance(' + location_id + ', ' + store_id + ')', 30);
		}
	}
}

function getContactInfo(location_id)
{
	if(location_id)
	{
		location_id = parseInt(location_id);
		
		if(xmlhttp.readyState == 0 || xmlhttp.readyState == 4)
		{
			var url = '/code/contact-info-generator.php?id=' + location_id;
			url += '&rand=' + parseInt(Math.random() * 999999999);
			xmlhttp.open('get', url, true);
			xmlhttp.onreadystatechange = function()
			{
				if(xmlhttp.readyState == 4) 
				{
					if(xmlhttp.status == 200) 
					{
						$("#contact-info").html(xmlhttp.responseText);
					}
				}
			};
			xmlhttp.send(null);
		}
		else
		{
			setTimeout('getContactInfo(' + location_id + ')', 30);
		}
	}
}

$(document).ready(function()
{
	try
	{
		$('#specials').cycle({fx: 'scrollDown', speed: '500', timeout: '3500', pause: '1'});
		
		$(".rollover img").hover(
		function()
		{
			this.src = this.src.replace("_Normal","_Rollover");
		},
		function()
		{
			this.src = this.src.replace("_Rollover","_Normal");
		});
	}
	catch(e)
	{}
});

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$.preloadImages("/graphics/standard-interface/mainnav-robg.jpg", "/graphics/standard-interface/Newsletter_Rollover.png", "/graphics/standard-interface/Trees_Rollover.jpg", "/graphics/standard-interface/Plants_Rollover.jpg", "/graphics/standard-interface/Products_Rollover.jpg", "/graphics/standard-interface/Locations_Rollover.jpg", "/graphics/standard-interface/Overstock_Rollover.jpg", "/graphics/standard-interface/Specials_Rollover.jpg");