function getXmlHttpRequestObj()
{
	var xmlHttpObj = null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttpObj=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttpObj=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
      	{
      		xmlHttpObj=new ActiveXObject("Microsoft.XMLHTTP");
      	}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttpObj;
}


function getRequest(url, element_id)
{
	xmlHttp = getXmlHttpRequestObj();

	if(url.indexOf("?") == -1)
		url = url + "?random="+Math.random();	
	else
		url = url + "&random="+Math.random();
	
	xmlHttp.open("GET",url,true);
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
			document.getElementById(element_id).innerHTML = xmlHttp.responseText;
	}
	
	xmlHttp.send(null);
}


function postRequest(url, params, element_id)
{
	// params looks like lorem=ipsum&name=binny
	
	xmlHttp = getXmlHttpRequestObj();

	if(url.indexOf("?") == -1)
		url = url + "?random="+Math.random();	
	else
		url = url + "&random="+Math.random();
	
	xmlHttp.open("POST",url,true);
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
			document.getElementById(element_id).innerHTML = xmlHttp.responseText;
	}
	
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}


function showWidget(sid,w_shape,w_size)
{
	var url = "http://www.ecrater.com/widget.php";
	url = url+"?sid=" + sid;
	url = url+"&shape=" + w_shape;
	url = url+"&size=" + w_size;
	
	getRequest(url, "ecrater_widget");
}


function updateCart(sessid,cart_sid)
{
	if(cart_sid == '')
		var url = "http://www.ecrater.com/updateqty-aj.php";
	else
		var url = "http://www.ecrater.com/updateqty-aj.php?cart_sid=" + cart_sid;
	var do_post;
	str_params = '';
	
	num_elements = document.update.pqty.length;
	if(num_elements)
	{
		for(i=0;i<num_elements;i++)
		{
			str_params = str_params + document.update.pqty[i].name + '=' + document.update.pqty[i].value + '&';
			if(document.update.pqty[i].value != pqty_original[i] && document.update.pqty[i].value != '' && document.update.pqty[i].value != ' ')
			{
				do_post = 1;
				pqty_original[i] = document.update.pqty[i].value
			}
		}
	}
	else
	{
		// there is just one element.
		if(document.update.pqty.value != pqty_original && document.update.pqty.value != '' && document.update.pqty.value != ' ')
		{
			str_params = document.update.pqty.name + '=' + document.update.pqty.value + '&';
			do_post = 1;
			pqty_original = document.update.pqty.value
		}
	}
	
	params = str_params + sessid;
	
	if(do_post)
	{
		postRequest(url, params, "ecrater_cart");
	}
}

function showSubCats(cid,sessid)
{
	xmlHttp = getXmlHttpRequestObj();

	var url = "getsubcats.php";
	url = url+"?cid=" + cid;
	url = url + "&" + sessid;
	url = url + "&random=" + Math.random();
	
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
		{
			// outerHTML was is developed by Microsoft and is not supported in Firefox
			// setting the .innerHTML on a select element does not work in IE. IE strips out the first option element, thus breaking the select box and making it appear blank
			// The workaround is to check if the .outerHTML property is defined and if the browser is IE. If so use outerHTML. Otherwise we use innerHTML
			if(document.getElementById("global_sub_cats").outerHTML && navigator.appName == 'Microsoft Internet Explorer')
			{
				document.getElementById("global_sub_cats").outerHTML = xmlHttp.responseText;
			}
			else
			{
				entire_select = xmlHttp.responseText;
				pattern = /<select.*?>(.*?)<\/select>/;
				matches = entire_select.match(pattern);
				pure_options = matches[1];
				document.getElementById("global_sub_cats").innerHTML = pure_options;
			}
		}
	}
	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
