 
var xmlHttp;
 
function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}
    
function doSearch(userName,infoId,topic,userid,userIp) {
    createXMLHttpRequest();
    var url="saveLeaveMessage.do?userName="+userName+"&infoId="+infoId+"&topic="+topic+"&userid="+userid+"&userIp="+userIp;
    
    if(topic.length==0){    	
    	return false;
    }
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.open("post", url, true);
    xmlHttp.send(null);
    doAgain();
    document.form1.topic.value="";
}
    
function handleStateChange() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
            clearPreviousResults();
            parseResults();
        }
    }
}

function clearPreviousResults() {
    var header = document.getElementById("header");
    if(header.hasChildNodes()) {
        header.removeChild(header.childNodes[0]);
    }

    var tableBody = document.getElementById("resultsBody");
    while(tableBody.childNodes.length > 0) {
        tableBody.removeChild(tableBody.childNodes[0]);
    }
}

function parseResults() {
    var results = xmlHttp.responseXML;

    var property = null;
    var address = "";
    var price = "";
    var comments = "";

    var properties = results.getElementsByTagName("property");
    for(var i = 0; i < properties.length; i++) {
        property = properties[i];
         
        address = property.getElementsByTagName("userName")[0].firstChild.nodeValue;
        price = property.getElementsByTagName("topic")[0].firstChild.nodeValue;
        comments = property.getElementsByTagName("date")[0].firstChild.nodeValue;
        
        addTableRow(address, price, comments);
    }
    
  //  var header = document.createElement("h2");
    var headerText = document.createTextNode("Results:");
   // header.appendChild(headerText);
   // document.getElementById("header").appendChild(header);
    
  //  document.getElementById("resultsTable").setAttribute("border", "1");
}


function addTableRow(address, price, comments) {
    var row = document.createElement("tr");
    var cell = createCellWithTextleft(address);
    row.appendChild(cell);
    
    cell = createCellWithTextmid(price);
    row.appendChild(cell);
    
    cell = createCellWithTextright(comments);
    row.appendChild(cell);
    
    document.getElementById("resultsBody").appendChild(row);
}

function createCellWithTextleft(text) {
    var cell = document.createElement("td");
    cell.setAttribute("width","144");    
    cell.setAttribute("align","left");
    var textNode = document.createTextNode(text);    
    cell.appendChild(textNode);    
    return cell;
}
function createCellWithTextmid(text) {
    var cell = document.createElement("td");
    cell.setAttribute("width","300");
    cell.setAttribute("align","left");
    var textNode = document.createTextNode(text);
    
    cell.appendChild(textNode);    
    return cell;
}
function createCellWithTextright(text) {
    var cell = document.createElement("td");
    cell.setAttribute("width","100");
    var textNode = document.createTextNode(text);
    cell.appendChild(textNode);    
    return cell;
}
