// AJAX funcs
baseurl = ''

function add_item (formid, id) {

	x = 0;
	while ((x < document.forms[formid].elements.length)) {

		nm = document.forms[formid].elements[x].name;
		if (nm == 'quantity') {
			quantity = document.forms[formid].elements[x].value;
			break;
		}
		x++;

	}

	new Ajax.Updater('basketinfo', baseurl + 'xmlhttp.php?action=updatebasket&do=add&item='+id+'&quantity='+quantity, {asynchronous:true});
	new Effect.Highlight('basketinfo', {startcolor:'#ffff99', endcolor:'#ffffff'});
	document.getElementById('addlink'+id).innerHTML = 'Added ' + quantity + ' to Basket';
	document.getElementById('addlink'+id).className = 'ajaxupdated';

	return false;

}

function checkUserName (id) {

	name = document.getElementById(id).value;
	if (name != '') {

		// set loading message
		document.getElementById(id+'_info').innerHTML = '<b>Checking name...</b>';
		document.getElementById(id+'_info').className = 'xmlcheckprogress';

		url = 'xmlhttp.php?action=xmlcheck&check=username&value='+name;

		loadXMLDoc(url);
	}

	return false;

}

// loadXMLDoc()
//	-	create connection and send the data
function loadXMLDoc(url)
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

// processReqChange()
//	-	wait for status to be OK, then handle the result
function processReqChange() {
	// only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// ...processing statements go here...
			response = req.responseXML.documentElement;

			method = response.getElementsByTagName('method')[0].firstChild.data;

			eval(method + '(response)');
		} else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}

function updatefield (response) {

	var result = response.getElementsByTagName('result')[0].firstChild.data;
	var message = response.getElementsByTagName('message')[0].firstChild.data;
	var field = response.getElementsByTagName('edited')[0].firstChild.data;

	// update the text div
	document.getElementById(field+'_info').innerHTML = '<b>'+message+'</b>';

	// update styling
	if (result > 0) {
		document.getElementById(field+'_info').className = 'xmlcheckok';
	}
	else {
		document.getElementById(field+'_info').className = 'xmlcheckbad';
	}

}