function showMatchList(whichLink,theURL)
{
	myLI = whichLink.parentNode.parentNode; // a-->p-->li
	positionID = 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
	ajaxGetResumes(positionID,theURL); //Execute ajax
}

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

function ajaxGetResumes(positionID,theURL)
{
	/** THIS is the Input Field **/
	myInputField = this;
	req = null;
	myURL = theURL +'?positionID='+ positionID;
	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;
				buildResumeList(positionID,rispostaServer);
				manageSuccess(positionID);
			}
			else
			{
				//alert("The page generating XML has not been found!");
				document.getElementById(positionID).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 buildResumeList(positionID,rispServer)
{
	myRoot = (rispServer.firstChild.nodeName == "xml") ? rispServer.childNodes[1] : rispServer.childNodes[0];  //alert("myRoot= "+myRoot.nodeName);
	myTbody = document.getElementById("area"+positionID).getElementsByTagName("tbody")[0];
	atLeast1Resume = false;
	myCount = 0;

	for (a=0; a<myRoot.childNodes.length; a++)
	{
		level1item = myRoot.childNodes[a];

		if (level1item.tagName=="resume")
		{
			myCount++;
			resumeID=level1item.getAttribute("id"); //Resume' ID
			newTR = document.createElement("tr");
				newTR.id="tr_"+ positionID +"_"+ resumeID;
				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";}
					if (level2item.tagName=="person_name") {newTD.style.whiteSpace = 'nowrap';}

					tdValue = level2item.firstChild.data;
					is_hot = (level1item.getAttribute("is_hot")==1) ? ' <img src="../im/ico_hot.gif" alt="hot!" title="hot!"/> ' : '';
					has_photo = (level1item.getAttribute("has_photo")==1) ? ' <img src="../im/ico_hasPic.gif" alt="photo" title="photo"/> ' : '';
					has_video = (level1item.getAttribute("has_video")==1) ? ' <img src="../im/ico_hasVid.gif" alt="video" title="video"/> ' : '';
					mRankNum = level1item.getAttribute("mrank");
					mRankColor = (mRankNum<0.3) ? 'red' : ((mRankNum<0.6) ? 'orange' : 'green');
					mRank = '<b class="mRank"><img src="../im/ghost.gif" style="background:' +mRankColor+ '; width:' +(mRankNum*100)+ '%;"/></b>';
					newTD.innerHTML = (level2item.tagName=="person_name") ? has_photo+has_video+ "<a href=\"javascript:openPopup('selectResume.php?id=" +resumeID+ "');\">" +tdValue+ "</a>" +mRank : ( (level2item.tagName=="r_title") ? is_hot+tdValue : ( (level2item.tagName=="contact") ? "<img src=\"../im/ico_email.gif\" border=\"0\" alt=\"Contact job seeker\" title=\"Contact job seeker\"  style=\"padding:0 10px;\" onclick=\"showHideMsg(1," +tdValue+ ");\"/>" : tdValue ) );

					newTR.appendChild(newTD);
				}
			}

			atLeast1Resume=true;
		}
	}
	if (!atLeast1Resume)
	{
		emptyTR = document.createElement("tr");
		emptyTD = document.createElement("td");
		emptyTD.colSpan=10;
		myTbody.appendChild(emptyTR);
		emptyTR.appendChild(emptyTD);
		emptyTD.innerHTML = 'Sorry, no matching Resum&eacute;s. Please modify your matching criteria and check the skills you marked as <span class="red">required</span> in your job offer.';
	}
}

function manageSuccess(positionID)
{
	document.getElementById("area"+positionID).getElementsByTagName("a")[0].style.display = "none";
	document.getElementById("area"+positionID).getElementsByTagName("div")[0].style.display = "block";
	document.location.href='#' +positionID;
	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 defaultPosition(r_id,theURL)
{
	showMatchList(document.getElementById('area'+r_id).getElementsByTagName('a')[0],theURL);
}
