function showMatchList(whichLink,theURL)
{
	myLI = whichLink.parentNode.parentNode; // a-->p-->li
	resumeID = myLI.id.substring(4); // The area's ID is the 5th character of the <LI>'s ID (which is "area" + area's ID).

	// Call Ajax script
	showHideLoading(1); //Show loading div
	ajaxGetPositions(resumeID,theURL); //Execute ajax
}

function getString4(fieldID)
{
	fieldValue = (document.getElementById(fieldID).checked) ? 1 : 0;
	return fieldID +"="+ fieldValue;
}

function ajaxGetPositions(resumeID,theURL)
{
	/** THIS is the Input Field **/
	myInputField = this;
	req = null;
	myURL = theURL +'?resumeID='+ resumeID;
	myString = getString4('location') +"&"+ getString4('workage') +"&"+ getString4('contract') +"&"+ getString4('degree') +"&"+ getString4('motherlanguage') + "&submit=1";

	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.

	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("Page found... \nSubmit executed!");

				/*** Following are the actions to be performed with the server response ***/
				rispostaServer = req.responseXML;
				buildPositionList(resumeID,rispostaServer);
				manageSuccess(resumeID);
			}
			else
			{
				//alert("The page generating XML has not been found!");
				document.getElementById(resumeID).innerHTML+="<br/><font color='red'>An error occurred while submitting data to the server!</font><br/>Code: " + req.status + " " + req.statusText; // statusText ¨¨ una propriet¨¤ dell'oggetto XMLHttpRequest che contiene il messaggio di errore.
				showHideLoading(2); //hide loading div
			}
		}
	}
}

function buildPositionList(resumeID,rispServer)
{
	myRoot = (rispServer.firstChild.nodeName == "xml") ? rispServer.childNodes[1] : rispServer.childNodes[0];  //alert("myRoot= "+myRoot.nodeName);
	myTbody = document.getElementById("area"+resumeID).getElementsByTagName("tbody")[0];
	atLeast1Position = false;
	myCount = 0;

	for (a=0; a<myRoot.childNodes.length; a++)
	{
		level1item = myRoot.childNodes[a];
		if (level1item.tagName=="position")
		{
			myCount++;
			positionID=level1item.getAttribute("id"); //Position ID
			newTR = document.createElement("tr");
				newTR.id="tr_" + resumeID +"_"+ positionID;
				newTR.className = (myCount%2==0) ? 'row1' : 'row2';
				newTR.originalClass = newTR.className;
				addEvent(newTR,'mouseover',function(){this.className='trHover';});
				addEvent(newTR,'mouseout',function(){this.className=this.originalClass;});
			myTbody.appendChild(newTR);

			 //First of all, the counter TD
			fstTD = document.createElement('td');
			fstTD.className = 'count';
			newTR.appendChild(fstTD);
			fstTD.innerHTML = myCount;

			for (k=0; k<level1item.childNodes.length; k++)
			{
				level2item = level1item.childNodes[k];
				if (level2item.nodeName!="#text")
				{
					newTD = document.createElement("td");
					if (level2item.tagName=="matching") {newTD.className = "matchColumn";}
					if (level2item.tagName=="contact") {newTD.className = "contactColumn";}
					
					tdValue = level2item.firstChild.data;
					is_hot = (level1item.getAttribute("is_hot")==1) ? ' <img src="../im/ico_hot.gif"/> ' : '';
					
					switch (level2item.tagName) {
						case 'p_name':
							newTD.innerHTML = is_hot+ '<a href="javascript:openPopup(\'selectPos.php?id=' +positionID+ '\');">' +tdValue+ '</a>';
							break;
						case 'span':
							newTD.innerHTML = '<span style="' +level2item.getAttribute('style')+ '">' +tdValue+ '</span>';
							break;
						case 'contact':
							newTD.innerHTML = '<img src="../im/ico_email.gif" border="0" alt="Contact employer" title="Contact employer" style="padding:0 10px;" onclick="showHideMsg(1,' +tdValue+ ');"/>';
							break;
						case 'location':
							newTD.noWrap = 'nowrap';
							newTD.innerHTML = tdValue;
							break;
						default:
							newTD.innerHTML = tdValue;
					}

					newTR.appendChild(newTD);
				}
			}
			atLeast1Position=true;
		}
	}
	if (!atLeast1Position)
	{
		emptyTR = document.createElement("tr");
		emptyTD = document.createElement("td");
		emptyTD.colSpan=10;
		myTbody.appendChild(emptyTR);
		emptyTR.appendChild(emptyTD);
		emptyTD.innerHTML = "Sorry, no matching Job Offers. Please modify your matching criteria and verify that you have input your real skills.";
	}
}

function manageSuccess(resumeID)
{
	document.getElementById("area"+resumeID).getElementsByTagName("a")[0].style.display = "none";
	document.getElementById("area"+resumeID).getElementsByTagName("div")[0].style.display = "block";
	document.location.href='#' +resumeID;
	showHideLoading(2);
}

function closeAllTables()
{
	for (b=0; b<document.getElementById("matchingUL").getElementsByTagName("li").length; b++)
	{
		myLi = document.getElementById("matchingUL").getElementsByTagName("li")[b];
		myLi.getElementsByTagName("a")[0].style.display = "inline";
		myLi.getElementsByTagName("div")[0].style.display = "none";
		// Delete the TBODY:
		myLi.getElementsByTagName("table")[0].removeChild(myLi.getElementsByTagName("tbody")[0]);
		// Restore an empty TBODY:
		myTbody = document.createElement("tbody");
		myLi.getElementsByTagName("table")[0].appendChild(myTbody);
	}
}

function defaultResume(p_id,theURL)
{
	showMatchList(document.getElementById('area'+p_id).getElementsByTagName('a')[0],theURL);
}
