var xmlHttp;
var xmlId;
var sessionId
var xmlAppend = false

/* Error Messages */
var speed = "Cool your jets!";
var empty = "Don't you have anything to say?";

function ajaxUpdate(url, id)
{
	new Ajax(url, {method: 'get',update: $(id)}).request();
} 

function voteUpdate(url, id)
{
	new Ajax(url, {method: 'get',update: $(id), onComplete: voteChanged(id)}).request();
} 

function stateChanged() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		if(xmlAppend) {
			append(xmlHttp.responseText);
		} else
		document.getElementById(xmlId).innerHTML=xmlHttp.responseText;
	}
}
function voteChanged(id) 
{ 
	var changeSpeed = 250;
	var marginChange = new Fx.Style(id, 'opacity',{duration:changeSpeed});
	marginChange.start(1);
}

function append(x) {
	var pp = document.getElementById('ajax_comment_do');
	pp.style.display = '';
	var px = document.getElementById('ajax_comment_hide');
	px.style.display = 'none';
		
	//String Replace
	pp.innerHTML = pp.innerHTML.replace(speed, '');
	pp.innerHTML = pp.innerHTML.replace(empty, '');
	
	if(x == '0') {
		//Timeout
		pp.innerHTML = speed + pp.innerHTML;	
	} else {	
		var r = document.getElementById(xmlId);
		r.innerHTML = x + r.innerHTML;	
	}
	
	xmlAppend = false;
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function setSession(s) {
	sessionId = s;
}
function changeCart(i, dir) {
	if(!(document.getElementById('quan'+i).value <= 0 && dir == 'down')) {
		if(dir == 'up' || dir == 'down') {
			cartSend('change_cart/'+i+'/'+dir, i);
		} else if(parseInt(dir.value) >= 0) {
			cartSend('change_cart/'+i+'/'+parseInt(dir.value), i);
		} else {
			dir.value = 0;
		}
	}
}
function cartSend(url, id)
{
	xmlId = id;
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}        
	
	xmlHttp.onreadystatechange=cartUpdate;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}  

function cartUpdate() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		if(xmlAppend) {
			append(xmlHttp.responseText);
		} else if(xmlHttp.responseText == 'cart empty') {
			window.location = './cart';
		} else {
			var response = xmlHttp.responseText.split('|');
			$('postageCost').setHTML(response[0]);
			$('quan'+xmlId).value= response[1];
			$('total'+xmlId).innerHTML= response[2]; 
			$('cartTotal').innerHTML= response[3];
			$('shopping_cart').innerHTML= response[4];
			$('voucherDiv').setHTML(response[5]);
		}
	}
}
function inviteFriends(method) {
	email = $("inviteEmail").value + $("inviteDomain").value;
	password = $("invitePassword").value;
	$('gathering').setStyle('display', '');
	new Ajax('./includes/inviteFriends.php', {data: 'email='+email+'&password='+password+'&method='+method, update: 'inviteFriends', onSuccess: function() {
		$('inviteEmail').value = '';
		$('invitePassword').value = '';
		$('gathering').setStyle('display', 'none');
		$('inviteEmail').setStyle('background-image', "url('./images/grabber_user.gif')");
		$('invitePassword').setStyle('background-image', "url('./images/grabber_pass.gif')");
	}}).request();//+'method='+method
}
function ajaxPOST(url, postValues, id)
{
	var myAjax = new Ajax(url, {method: 'post',update: $(id), data: postValues});
	myAjax.request();
} 
function signUpEmail() {
	email = document.getElementById("signUpEmail").value;
	ajaxPOST("includes/signUpEmail.php", "email="+email, 'signUpEmailDiv');
}