// 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 paybillz(ACTion, ID, LATE){
// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		LATE01 = LATE;
		if(LATE01 == 0){
			document.getElementById("div_" + ID).innerHTML = '<i>updating...</i>';
		} else {
			document.getElementById("latediv_" + ID).innerHTML = '<i>updating...</i>';	
		}
		billzID = ID;
		xmlHttp.open("GET", "ajax_pay.php?action=" + ACTion + "&billid=" + ID + "&late=" + LATE01, true);
		// define the method to handle server responses
		billzRESPONSE = 'paybillz';
		xmlHttp.onreadystatechange = handleSuperResponse;
		// make the server request
		xmlHttp.send(null);
	} else {
		// if the connection is busy, try again after one second
		setTimeout('paybillz()', 1000);
	}
// executed automatically when a message is received from the server
}

function addbillz(ACTion, ID){
// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		addbillz_title = document.getElementById("title").value;
		addbillz_amt = document.getElementById("amt").value;
		addbillz_type = document.getElementById("type").value;
		addbillz_notes = document.getElementById("notes").value;
		addbillz_day = document.getElementById("day").value;
		addbillz_month = document.getElementById("month").value;
		addbillz_year = document.getElementById("year").value;
		document.getElementById("addarea_div").innerHTML = '<i>adding record...</i>';
		billzID = ID;
		xmlHttp.open("GET", "ajax_pay.php?action=" + ACTion + "&title=" + addbillz_title + "&amt=" + addbillz_amt + "&type=" + addbillz_type + "&notes=" + addbillz_notes + "&day=" + addbillz_day + "&month=" + addbillz_month + "&year=" + addbillz_year, true);
		// define the method to handle server responses
		billzRESPONSE = 'addbillz';
		xmlHttp.onreadystatechange = handleSuperResponse;
		// make the server request
		xmlHttp.send(null);
	} else {
		// if the connection is busy, try again after one second
		setTimeout('addbillz()', 1000);
	}
// executed automatically when a message is received from the server
}
function handleSuperResponse(){
	// move forward only if the transaction has completed
	if (xmlHttp.readyState == 4){
	// status of 200 indicates the transaction completed successfully
		if (xmlHttp.status == 200){
			helloMessage = xmlHttp.responseText;
			
			if(billzRESPONSE == 'paybillz'){
				if(LATE01 == 0){
					document.getElementById("div_" + billzID).innerHTML = helloMessage;
				} else {
					document.getElementById("latediv_" + billzID).innerHTML = helloMessage;
				}
			}
			if(billzRESPONSE == 'addbillz'){
				document.getElementById("addarea_div").innerHTML = helloMessage;
				document.getElementById("title").value = '';
				document.getElementById("amt").value = '';
				document.getElementById("type").selectedIndex = '0';
				document.getElementById("notes").value = '';
			}
			
		
		// a HTTP status different than 200 signals an error
		} else {
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
	}
}
function hidebillz(DIVID){
	if(DIVID == 'latebillz'){
		document.getElementById('latebillz').style.display = 'none';
		document.getElementById('latebillznotify').style.display = 'block';
	}
	if(DIVID == 'latebillznotify'){
		document.getElementById('latebillz').style.display = 'block';
		document.getElementById('latebillznotify').style.display = 'none';
	}
	if(DIVID == 'latebillzall'){
		document.getElementById('latebillz').style.display = 'none';
		document.getElementById('latebillznotify').style.display = 'none';
	}
}
