/** THIS FILE IS USED FOR DELETING COMMENTS/POSITIONS/RESUMES **/


/* this function can be used to delete ANY item (by providing appropriate URL) */
function deleteItem(whichLink,whichURL)
{
	myTR = whichLink.parentNode.parentNode; // td-->tr
	if(confirm('Sure to delete this item?'))
	{
		ajaxSubmit("delete",myTR,whichURL);
	}
}

function manageResponseForDelete(myTR,XMLresponse)
{
	myRoot = (XMLresponse.firstChild.nodeName == "xml") ? XMLresponse.childNodes[1] : XMLresponse.childNodes[0];  //alert("myRoot= "+myRoot.nodeName);
	if (myRoot.getAttribute("success")==1) // If DB query was successful...
	{
		myTR.parentNode.removeChild(myTR);
	}
	else {alert("An error occurred. Please refresh this page.");}
	showHideLoading(2); //hide loading div
}


function ajaxSubmit(mod,myObj,myUrl)
{
	/** Arguments: 
	* mod		= 'insert' or 'edit' or 'delete'
	* myObj	= the HTML object to be altered upon server response
	* myUrl	= the URL of the server script
	*/

	showHideLoading(1);
	
	/** THIS e' l'Input Field **/
	/*** Questa funzione si limita a fare la POST dei dati inseriti dall'utente, e a ricevere la risposta del server. Poi chiama lo script che gestisce la risposta del server. ***/
	myInputField = this;
	req = null;
	myURL = myUrl;
	myString = (myObj.id) ? myObj.id+"="+myObj.value : ""; // This function is called BOTH to submit textarea changes AND to delete an item. In the 2nd case, there is no input field.

	//alert("Submit effettuato. Attendo feedback dal server...");

	if (window.XMLHttpRequest) // Mozilla, Safari, ...
	{
		req = new XMLHttpRequest();
		if (req.overrideMimeType) { req.overrideMimeType('text/xml'); }
	}
	else if (window.ActiveXObject) // IE
	{
		try { req = new ActiveXObject("Msxml2.XMLHTTP"); } // Vers. 5.5 o inferiore
		catch (e)
		{
			try { req = new ActiveXObject("Microsoft.XMLHTTP"); } // Vers. 5.5 o superiore
			catch (e) {}
		}
	}

	if (!req) {
	   alert('Giving up :( Cannot create an XMLHTTP instance');
	   return false;
	}

	req.open("POST", myURL, true); // 1) POST or GET;  2) URL of the script to execute;  3) true for asynchronous (false for synchronous).
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	req.send(myString); // POSTs data to the server.
	
	result = false;

	req.onreadystatechange = function()
	{
		/** Ora THIS e' questa funzione! **/
		if (req.readyState == 4) // The 4 state means for the response is ready and sent by the server.
		{
			if (req.status == 200) // This status means "OK", otherwise some error code is returned, 404 for example.
			{
				//alert("Pagina trovata... \nSubmit effettuata!");

				/*** Following are the actions to be performed with the server response ***/
				rispostaServer = req.responseXML;
				if (mod=="delete") {manageResponseForDelete(myObj,rispostaServer);}
			}
			else
			{
				//alert("The page generating XML has not been found!");
				alert("An error occurred while submitting data to the server!\nCode: " + req.status + " " + req.statusText); // statusText è una proprietà dell'oggetto XMLHttpRequest che contiene il messaggio di errore.
				showHideLoading(2); //hide loading div
			}
		}
	}

}
