var xmlHttp

function browseProfiles()
{ 
	var firstName = document.getElementById("bpFirstName").value;
	var lastName = document.getElementById("bpLastName").value;
	var url="http://www.uta.edu/engineering/ajax/coe-browseprofiles-rsp.php?";
	
	if (firstName != "" || lastName != "") { 
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request");
			return;
		} 
		if (firstName != "")
			url=url+"fname="+firstName;
		if (lastName != "")
			url=url+"&lname="+lastName;
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
		//hide people table
		document.getElementById("pplGenInfoTable").style.display = "none";
		//hide title
		document.getElementById("title").style.display = "none";
	}
	else {
		document.getElementById("bpResults").innerHTML = "";
		//show people table
		document.getElementById("pplGenInfoTable").style.display = "inline";
		//show title
		document.getElementById("title").style.display = "inline";
	}
}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		if (xmlHttp.responseText != "Empty Set") {
			//Parse xmlHttp.responseText and put all records in an array
			var splitRows = xmlHttp.responseText.split("\n");
			var allRecords = new Array();
			for (var rowCounter = 0; rowCounter < splitRows.length; rowCounter++) {
				var splitColumns = splitRows[rowCounter].split(",,");
				for (var columnCounter = 0; columnCounter < splitColumns.length; columnCounter++) {
					allRecords[rowCounter] = splitColumns;
				}
			}
			
			//Print results
			var rsp_base_url = "http://www.uta.edu/ra/real/";
			var rsp_image_path = "images/128/";
			var rsp_profile_path = "editprofile.php?pid=";
			var coe_base_url = "http://www.uta.edu/engineering/";
			var resultsTable = document.getElementById("bpResults");
			var table;
			table = "<table width=\"90%\" border=\"0\" cellspacing=\"2\" cellpadding=\"2\">";
			for (var i = 0; i < allRecords.length; i++) {
				table += "<tr>";
				/////////////////////////
				//Print image cell
				/////////////////////////
				table += "<td td width=\"10%\" height=\"130\" valign=\"middle\" align=\"center\">";
				var full_image_path;
				if (allRecords[i][5] != "" && allRecords[i][5] != 0)
					full_image_path = rsp_base_url + rsp_image_path + allRecords[i][0] + "_0_" + allRecords[i][5];
				else 
					full_image_path = "http://www.uta.edu/ra/real/images/icons/silhouette.jpg";
				table += ("<a href=\"" + rsp_base_url + rsp_profile_path + allRecords[i][0] + "\">");
				table += ("<img src=\"" + full_image_path + "\" border=\"0\"/>");
				table += "</a>";
				table += "</td>";
				/////////////////////////
				//Print information cell
				/////////////////////////
				table += "<td width=\"90%\" height=\"130\" valign=\"middle\" align=\"left\" style=\"border-top:1px solid #CCCCCC;\">";
				//Print name
				table += ("<a href=\"" + rsp_base_url + rsp_profile_path + allRecords[i][0] + "\">");
				table += (allRecords[i][1] + " " + allRecords[i][2] + " " + allRecords[i][3] + " " + allRecords[i][4]);
				table += "</a>";
				table += "<br />";
				//Print designation and department
				var designation = allRecords[i][6].split("-");
				if (designation[0])
					table += (designation[0] + "<br />");
				if (designation[1]) {
					table += ("<a href=\"" + coe_base_url + "people.php?hid=" + allRecords[i][12] + "\">");
					table += designation[1];
					table += "</a>";
					table += "<br />";
				}
				table += "<br />";
				//Print email
				if (allRecords[i][7] != "") {
					table += ("<a href=\"mailto:" + allRecords[i][7] + "\">");
					table += allRecords[i][7];
					table += "</a><br />";
				}
				//Print phone number(s)
				if (allRecords[i][9] != "")
					table += (allRecords[i][8] + " | " + allRecords[i][9]);
				else if (allRecords[i][8] != "")
					table += allRecords[i][8];	
				table += "</td>";
				table += "</tr>";
			}
			table += "</table>";
			resultsTable.innerHTML = table;
		}
		else
			document.getElementById("bpResults").innerHTML = "<h4>No matches found.</h4>";
	}
} 

function GetXmlHttpObject()
{ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}
