// JavaScript Document
function loadData(URL)
{
// Create the XML request
    xmlReq = null;
    if(window.XMLHttpRequest) {
		xmlReq = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(xmlReq==null) return; // Failed to create the request

	// Anonymous function to handle changed request states
    xmlReq.onreadystatechange = function()
    {
		switch(xmlReq.readyState)
			{
			case 0: // Uninitialized
				break;
			case 1: // Loading
				break;
			case 2: // Loaded
				break;
			case 3: // Interactive
				break;
			case 4: // Done!
				// Retrieve data
				ticketcode = getNodeValue(xmlReq.responseXML,'code');//xmlReq.responseXML.getElementsByTagName('code')[0].firstChild.data;
				ticketamount = getNodeValue(xmlReq.responseXML,'amount');//xmlReq.responseXML.getElementsByTagName('amount')[0].firstChild.data;
				ticketdiscount = getNodeValue(xmlReq.responseXML,'discount');//xmlReq.responseXML.getElementsByTagName('discount')[0].firstChild.data;
				ticketname = getNodeValue(xmlReq.responseXML,'name');//xmlReq.responseXML.getElementsByTagName('name')[0].firstChild.data;
				
				if (ticketcode == "ERROR") {
					//console.log("Ticket was not valid!");
					ticketamount = 0;
					ticketdiscount = 0;
					ticketname = "";
					//console.log("recalc empty");
					calcTotal();
				} else {
					//console.log("recalc with:"+ticketname);
					calcTotal();
				}
				break;
			default:
				break;
			}
    }

// Make the request
    xmlReq.open ('GET', URL, true);
    xmlReq.send (null);
}

function retrieveCode()
{
	var ticketvalue = document.regform.elements["fieldFeeTicket"].value;
	if (ticketvalue != "") {
		loadData("http://register.winconference.com/templates/registration/ticketload.php&code="+ticketvalue);	
	}
}

function getNodeValue(node, tagname, default_val) {
	var elems = node.getElementsByTagName(tagname);

	//test to see if tags aren't blank and have some value
	if (elems && elems[0] && elems[0].firstChild &&
		elems[0].firstChild.nodeValue) {
		return elems[0].firstChild.nodeValue;
	}
	return default_val;
}
