// 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 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;
			document.getElementById('main').innerHTML = helloMessage;
		// a HTTP status different than 200 signals an error
		} else {
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
	}
}





function submitEditItem(editid){
// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		//alert(editid);
		//alert(document.getElementById('id_' + editid).value);
		var editcopyid = document.getElementById('id_' + editid).value;
		var edittitle = document.getElementById('title_'+editid).value;
		var editcontent = document.getElementById('content_'+editid).value;
		var editprojid = document.getElementById('projid_'+editid).value;
		parameters = 'edittitle=' + edittitle + '&editcontent=' + editcontent + '&editprojid=' + editprojid + '&editid=' + editcopyid;
		xmlHttp.open('POST', 'forms.php?type=processedit', true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      	xmlHttp.setRequestHeader("Content-length", parameters.length);
      	xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(parameters);
		document.getElementById("item_"+editid).innerHTML = 'Editing item...';
		restoreid = 'item_' + editid;
		xmlHttp.onreadystatechange = handleRestoreResponse;
		setTimeout('shrinkbox("editalert")', 4000)
		//addmessage = document.getElementById("message").focus();
	} else {
		// if the connection is busy, try again after one second
		setTimeout("submitnewitem()", 1000);
	}
// executed automatically when a message is received from the server
}

function submitnewitem(){
// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		addtitle = document.getElementById("title").value;
		addcontent = document.getElementById("content").value;
		parameters = 'title=' + addtitle + '&content=' + addcontent;
		xmlHttp.open('POST', 'forms.php?type=processadd', true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      	xmlHttp.setRequestHeader("Content-length", parameters.length);
      	xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(parameters);
		document.getElementById("adder").innerHTML = 'Adding new item...';
		xmlHttp.onreadystatechange = handleAddResponse;
		//addmessage = document.getElementById("message").focus();
	} else {
		// if the connection is busy, try again after one second
		setTimeout("submitnewitemchild()", 1000);
	}
// executed automatically when a message is received from the server
}

function submitnewitemchild(parentid){
// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		addtitle = document.getElementById("title").value;
		addcontent = document.getElementById("content").value;
		addrelid = document.getElementById("relid").value;
		parameters = 'title=' + addtitle + '&content=' + addcontent + '&relid='+addrelid;
		xmlHttp.open('POST', 'forms.php?type=processaddchild', true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      	xmlHttp.setRequestHeader("Content-length", parameters.length);
      	xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(parameters);
		document.getElementById("adder").innerHTML = 'Adding related item...';
		xmlHttp.onreadystatechange = handleAddChildResponse;
		//addmessage = document.getElementById("message").focus();
	} else {
		// if the connection is busy, try again after one second
		setTimeout("submitnewitemchild()", 1000);
	}
// executed automatically when a message is received from the server
}

function submitedititem(){
// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		addtitle = document.getElementById("title").value;
		addcontent = document.getElementById("content").value;
		parameters = 'title=' + addtitle + '&content=' + addcontent;
		xmlHttp.open('POST', 'forms.php?type=processadd', true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      	xmlHttp.setRequestHeader("Content-length", parameters.length);
      	xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(parameters);
		document.getElementById("adder").innerHTML = 'Adding item...';
		xmlHttp.onreadystatechange = handleAddResponse;
		//addmessage = document.getElementById("message").focus();
	} else {
		// if the connection is busy, try again after one second
		setTimeout("submitnewitem()", 1000);
	}
// executed automatically when a message is received from the server
}


function additems(){
// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		xmlHttp.open("GET", "forms.php?type=add", true);
		// define the method to handle server responses
		xmlHttp.onreadystatechange = handleAResponse;
		// make the server request
		xmlHttp.send(null);
	} else {
		// if the connection is busy, try again after one second
		setTimeout("additems()", 1000);
	}
// executed automatically when a message is received from the server
}

function addrelateditems(relid){
// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		xmlHttp.open("GET", "forms.php?type=addchild&relid="+relid, true);
		// define the method to handle server responses
		xmlHttp.onreadystatechange = handleAResponse;
		// make the server request
		xmlHttp.send(null);
	} else {
		// if the connection is busy, try again after one second
		setTimeout("addrelateditems('"+relid+"')", 1000);
	}
// executed automatically when a message is received from the server
}

function edititem(editid){
// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		xmlHttp.open("GET", "forms.php?type=edit&id="+editid, true);
		// define the method to handle server responses
		currenteditid = editid;
		xmlHttp.onreadystatechange = handleEResponse;
		// make the server request
		xmlHttp.send(null);
	} else {
		// if the connection is busy, try again after one second
		setTimeout("additems()", 1000);
	}
// executed automatically when a message is received from the server
}

function showMainItems(){
// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		xmlHttp.open("GET", "forms.php?type=showhome", true);
		// define the method to handle server responses
		xmlHttp.onreadystatechange = handleShowResponse;
		// make the server request
		xmlHttp.send(null);
	} else {
		// if the connection is busy, try again after one second
		setTimeout("showMainItems()", 1000);
	}
// executed automatically when a message is received from the server
}

function showSpecificItems(itemid){
// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		xmlHttp.open("GET", "forms.php?type=showitem&id="+itemid, true);
		// define the method to handle server responses
		xmlHttp.onreadystatechange = handleShowResponse;
		// make the server request
		xmlHttp.send(null);
	} else {
		// if the connection is busy, try again after one second
		setTimeout("showSpecificItems('"+itemid+"')", 1000);
	}
// executed automatically when a message is received from the server
}

function restoreEditBox(itemid){
// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		xmlHttp.open("GET", "forms.php?type=singleitem&id="+itemid, true);
		// define the method to handle server responses
		restoreid = 'item_' + itemid;
		xmlHttp.onreadystatechange = handleRestoreResponse;
		// make the server request
		xmlHttp.send(null);
	} else {
		// if the connection is busy, try again after one second
		setTimeout("showSpecificItems('"+itemid+"')", 1000);
	}
// executed automatically when a message is received from the server
}
////////////////////////////
//////// RESPONSES /////////
////////////////////////////

function handleRestoreResponse(){
	// 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;
			document.getElementById(restoreid).innerHTML = helloMessage;
		// a HTTP status different than 200 signals an error
		} else {
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
	}
}

function handleEResponse(){
	// 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;
			document.getElementById("item_"+currenteditid).innerHTML = helloMessage;
		// a HTTP status different than 200 signals an error
		} else {
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
	}
}

function handleAResponse(){
	// 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;
			document.getElementById('adder').innerHTML = helloMessage;
		// a HTTP status different than 200 signals an error
		} else {
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
	}
}

function handleAddResponse(){
	// 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;
			document.getElementById('adder').innerHTML = helloMessage;
			showMainItems();
		// a HTTP status different than 200 signals an error
		} else {
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
	}
}

function handleAddChildResponse(){
	// 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;
			document.getElementById('adder').innerHTML = helloMessage;
			currentid = document.getElementById('curid').value;
			showSpecificItems(currentid); //Need to pass this id somehow.
		// a HTTP status different than 200 signals an error
		} else {
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
	}
}


function handleShowResponse(){
	// 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;
			document.getElementById('showme').innerHTML = helloMessage;
		// a HTTP status different than 200 signals an error
		} else {
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
	}
}

function clearaddbox(){
	document.getElementById('adder').innerHTML = '';
}




function deleteTopItem(itemid){
// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		xmlHttp.open("GET", "forms.php?type=processdelete&delid="+itemid, true);
		// define the method to handle server responses
		xmlHttp.onreadystatechange = handleAddResponse;
		// make the server request
		xmlHttp.send(null);
		moveBoxLeft('item_'+itemid);
		setTimeout("shrinkbox('item_"+itemid+"')", 3000);
	} else {
		// if the connection is busy, try again after one second
		setTimeout("deleteTopItem('"+itemid+"')", 1000);
	}
// executed automatically when a message is received from the server
}


function deleteChildItem(itemid){
// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		xmlHttp.open("GET", "forms.php?type=processdelete&delid="+itemid, true);
		// define the method to handle server responses
		xmlHttp.onreadystatechange = handleAddChildResponse;
		// make the server request
		xmlHttp.send(null);
		moveBoxLeft('item_'+itemid);
		setTimeout("shrinkbox('item_"+itemid+"')", 3000);
	} else {
		// if the connection is busy, try again after one second
		setTimeout("deleteTopItem('"+itemid+"')", 1000);
	}
// executed automatically when a message is received from the server
}



function moveBoxLeft(divid){
	curpos = document.getElementById(divid).style.left;
	curpos = curpos.replace(/px/,"");
	newpos = curpos - 50;
	document.getElementById(divid).style.left = newpos+'px';
	if(newpos > -450){
		setTimeout("moveBoxLeft('"+divid+"');", 10);
	}
}

function shrinkbox(divid){
	document.getElementById(divid).style.display = 'none';
}

