/*

JavaScript Document

2007 M S Electrical

web design Brown Eye Software http://www.brown-eye-software.co.uk/
	
*/

var output_string = 'not set!';
var url,basket_total,basket_empty;

// ajax stuff ...

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject()
{
	var xmlHttp;
	if(window.ActiveXObject) // if IE
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp = false;
		}
	}
	else // if mozilla or other
	{
		try
		{
			xmlHttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlHttp = false;
		}
	}
	if(!xmlHttp)
	{
		alert("Error creating the XMLHttpRequest object!");
	}
	else
	{
		return xmlHttp;
	}
} // end of function

function basketAJAX(id)
{
	var tempString;
	tempString = 'basket' + id;
	url = 'ajax_update_basket2.php?i='+id+'&qty='+document.getElementById(tempString).value;
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0) // only proceed if the object isn't busy
	{
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange = handleServerResponseBasket;
		xmlHttp.send(null);
	}
	else
	{
		setTimeout("basketAJAX(id)",500);
	}
}

function handleServerResponseBasket()
{
	// update cost
	
	// update delivery
	
	// up[date totalcost
	
	if(xmlHttp.readyState == 4)
	{
		if(xmlHttp.status == 200)
		{
			xmlResponse = xmlHttp.responseXML;
			xmlDocumentElement = xmlResponse.documentElement;
			responseText = xmlDocumentElement.firstChild.data;
			// update screen
			document.getElementById('costOfItems').innerHTML = (responseText/100);
			if((responseText*1) > 4499)
			{
				document.getElementById('costOfDelivery').innerHTML = '0.00 - Free delivery on all orders over £45!';
				document.getElementById('totalCost').innerHTML = ((responseText*1)/100);
			}
			else
			{
				document.getElementById('costOfDelivery').innerHTML = '4.50';
				document.getElementById('totalCost').innerHTML = (   ((responseText*1)+450)/100      );
			}
		}
		else
		{
			alert("Error accessing server!");
		}
	}
	
}

function processAJAX(id,qty)
{
	url = 'ajax_update_basket.php?id='+id+'&qty='+qty;
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0) // only proceed if the object isn't busy
	{
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange = handleServerResponse;
		xmlHttp.send(null);
	}
	else
	{
		setTimeout("process()",500);
	}
} // end of function

function handleServerResponse()
{
	if(xmlHttp.readyState == 4)
	{
		if(xmlHttp.status == 200)
		{
			xmlResponse = xmlHttp.responseXML;
			xmlDocumentElement = xmlResponse.documentElement;
			basket_total = xmlDocumentElement.firstChild.data;
			// update screen
			if(basket_empty == true)
			{
				var temp_html;
				temp_html = '<span id="basket_container">' + basket_total + '</span> items(s) in basket<br/><br/><a href="basket.php">view basket</a><br/>or ...<br/><a href="checkout.php">go to checkout</a><span id="temp_warning"><br/><br/><span class="red"><strong>Basket Updated</strong></span></span>';				
				document.getElementById('basket_container_wide').innerHTML = temp_html;
				basket_empty = false;	
				setTimeout("clearTempWarning()",3000);
			}
			else
			{
				document.getElementById('basket_container').innerHTML = basket_total;
				document.getElementById('temp_warning').innerHTML = '<br/><br/><span class="red"><strong>Basket Updated</strong></span>';
				setTimeout("clearTempWarning()",3000);
			}
			alert('your item(s) have been added to your basket!');
		}
		else
		{
			alert("Error accessing server!");
		}
	}
} // end of function

function removeAJAX(i)
{
	url = 'ajax_remove_basket.php?i='+i;
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0) // only proceed if the object isn't busy
	{
		xmlHttp.open("GET",url,true);
		xmlHttp.onreadystatechange = handleServerResponseRemove;
		xmlHttp.send(null);
	}
	else
	{
		setTimeout("process()",500);
	}
} // end of function

function handleServerResponseRemove()
{
	if(xmlHttp.readyState == 4)
	{
		if(xmlHttp.status == 200)
		{
			xmlResponse = xmlHttp.responseXML;
			xmlDocumentElement = xmlResponse.documentElement;
			gafx = xmlDocumentElement.firstChild.data;
			// update screen
			//alert(gafx);
			window.location = 'basket.php';
		}
		else
		{
			alert("Error accessing server!");
		}
	}
} // end of function


function addToBasket(id,count)
{
	// use AJAX to update basket ...
	processAJAX(id,document.getElementById('qty_'+count).innerHTML)
	
	// update screen ??? only if needed
}

function increaseQty(count)
{
	temp = document.getElementById('qty_' + count).innerHTML;
	if(temp < max_addtocart)
	{
		var temp
		temp++;
		document.getElementById('qty_' + count).innerHTML = temp;
	}
}

function decreaseQty(count)
{
	var temp
	temp = document.getElementById('qty_' + count).innerHTML;
	if(temp > 1)
	{
		temp--;
		document.getElementById('qty_' + count).innerHTML = temp;
	}
}

function clearTempWarning()
{
	document.getElementById('temp_warning').innerHTML = '';
}