// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();
// retrieves the XMLHttpRequest object


function createXmlHttpRequestObject(){
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// if running Internet Explorer
	if(window.ActiveXObject){
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) {
			xmlHttp = false;
		}
	
	// if running Mozilla or other browsers
	} else {
		try {
			xmlHttp = new XMLHttpRequest();
		}
		catch (e) {
			xmlHttp = false;
		}
	}
	// return the created object or display an error message



	if (!xmlHttp){
		alert("Error creating the XMLHttpRequest object.");
	} else {
		return xmlHttp;
	}
	// make asynchronous HTTP request using the XMLHttpRequest object
	
}

function add_record(){
// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		
		name = encodeURIComponent(document.getElementById("name").value);
		amount = encodeURIComponent(document.getElementById("amount").value);
		// execute the quickstart.php page from the server
		//alert("record.php?name=" + name + "&focusvar=" + focusvar);
		//document.getElementById("divMessage").innerHTML = '<i>' + "Opening Connection..." + '</i>';
		xmlHttp.open("GET", "ajax_choice.php?name=" + name + "&amount=" + amount + "&action=newitem", true);
		// define the method to handle server responses
		xmlHttp.onreadystatechange = handleServerResponse;
		// make the server request
		xmlHttp.send(null);

	} else {
		// if the connection is busy, try again after one second
		setTimeout('add_record()', 1000);
	}
// executed automatically when a message is received from the server
}
function handleServerResponse(){
	// move forward only if the transaction has completed
	if (xmlHttp.readyState == 4){
	// status of 200 indicates the transaction completed successfully
		if (xmlHttp.status == 200){
			// extract the XML retrieved from the server
			//xmlResponse = xmlHttp.responseXML;
			// obtain the document element (the root element) of the XML structure
			
			//if (helloMessage){
				//alert('Transaction Added!');
			//}
			//document.getElementById("datatable").innerHTML = helloMessage;
			//appendRow("datatable", helloMessage, helloMessage2, helloMessage3, helloMessage4);
			RefreshTable('ajax_tableview.php');
			document.add_new_amount.name.value = '';
			document.add_new_amount.amount.value = '';
			document.add_new_amount.name.focus();
			// restart sequence
			//process();
			//setTimeout('process()', 1000);
		
		// a HTTP status different than 200 signals an error
		} else {
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
	}
}


function appendRow(tableid, data1, data2, data3, data4){
	var tbody = document.getElementById(tableid).getElementsByTagName("tbody")[0];
	var row = document.createElement("TR");
	var cell1 = document.createElement("TD");
	cell1.innerHTML = data1;
	var cell2 = document.createElement("TD");
	cell2.innerHTML = data2;
	var cell3 = document.createElement("TD");
	cell3.innerHTML = data3;
	var cell4 = document.createElement("TD");
	cell4.innerHTML = data4;
	cell4.align="right"
	row.appendChild(cell1);
	row.appendChild(cell2);
	row.appendChild(cell3);
	row.appendChild(cell4);
	tbody.appendChild(row);
}

function RefreshTable(pagepop){
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		//document.getElementById(filldiv).innerHTML = '<i>loading...</i>';
		xmlHttp.open("GET", pagepop, true);
		xmlHttp.onreadystatechange = handleStandard;
		xmlHttp.send(null);
	} else {
		setTimeout("RefreshTable('"+pagepop+"')", 1000);
	}
}
function handleStandard(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			helloMessage = xmlHttp.responseText;
			document.getElementById('balancetable').innerHTML = helloMessage;
			//document.getElementById('balancetable').style.visibility = 'visible';
		} else {
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
	}
}

function Core(pagepop){
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		//document.getElementById(filldiv).innerHTML = '<i>loading...</i>';
		xmlHttp.open("GET", pagepop, true);
		xmlHttp.onreadystatechange = handleCoreStandard;
		xmlHttp.send(null);
	} else {
		setTimeout("Core('"+pagepop+"')", 1000);
	}
}
function handleCoreStandard(){
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200){
			//helloMessage = xmlHttp.responseText;
			//document.getElementById('balancetable').innerHTML = helloMessage;
			//document.getElementById('balancetable').style.visibility = 'visible';
		} else {
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
	}
}

