
var req;

/**
 * Open a connection to the specified URL, which is
 * intended to provide an XML message.  The specified data
 * is sent to the server as parameters.  This is the same as
 * calling xmlOpen("POST", url, toSend, responseHandler).
 *
 * @param string url    The URL to connect to.
 * @param string toSend The data to send to the server; must be URL encoded.
 * @param function responseHandler The Javascript function handling server response.
 */
function xmlPost(url, toSend, responseHandler)
{
    xmlOpen("POST", url, toSend, responseHandler);
}

/**
 * Open a connection to the specified URL, which is
 * intended to provide an XML message.  No other data is
 * sent to the server.  This is the same as calling
 * xmlOpen("GET", url, null, responseHandler).
 *
 * @param string url    The URL to connect to.
 * @param function responseHandler The Javascript function handling server response.
 */
function xmlGet(url, responseHandler)
{
    xmlOpen("GET", url, null, responseHandler);
}

/**
 * Open a connection to the specified URL, which is
 * intended to respond with an XML message.
 * 
 * @param string method The connection method; either "GET" or "POST".
 * @param string url    The URL to connect to.
 * @param string toSend The data to send to the server; must be URL encoded.
 * @param function responseHandler The Javascript function handling server response.
 */
 

function xmlOpen(method, url, toSend, responseHandler)
{
    if (window.XMLHttpRequest)
    {
        // browser has native support for XMLHttpRequest object
        req = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        // try XMLHTTP ActiveX (Internet Explorer) version
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    if(req)
    {
        req.onreadystatechange = responseHandler;
        req.open(method, url, true);
        req.setRequestHeader("content-type","application/x-www-form-urlencoded");
        req.send(toSend);
    }
    else
    {
        alert('This web browser does not support XMLHttpRequest, which is required by this page.');
    }
}

/**
 * Gets the first child node of <code>parent</code> with the
 * specified tag name.
 *
 * @param parent the parent XML DOM node to search
 * @param tagName the tag name of the child node to search for
 */
function getNode(parent, tagName)
{
    var i;
    var max = parent.childNodes.length;
    
    // Check each child node
    for(i = 0; i < max; i++)
    {
        if(parent.childNodes[i].tagName)
        {
            if(parent.childNodes[i].tagName.toUpperCase() == tagName.toUpperCase())
            {
                // We found a matching child node; return it.
                return parent.childNodes[i];
            }
        }
    }
    // One was not found; return null
    return null;
}

/**
 * Gets the first child node of <code>parent</code> with the
 * specified tag name and whose value of the 'key' attribute
 * is <code>key</code>.
 *
 * @param parent the parent XML DOM node to search
 * @param tagName the tag name of the child nodes to search in
 * @param key the value of the 'key' attribute to search on
 */
function getNodesWithKey(parent, tagName, key)
{
    var i;
    var cellNodes = parent.getElementsByTagName(tagName);
    var max = cellNodes.length;
    
    // Check each cell node for the specified value for
    // the 'key' attribute
    for(i = 0; i < max; i++)
    {
        if(cellNodes[i].getAttribute('key') == key)
        {
            // We found a matching cell node; return it.
            return cellNodes[i];
        }
    }
    // One was not found; return null
    return null;
}

/* Cross-browser means of getting node contents as text */
function getInnerText (node) {
	if (typeof node.textContent != 'undefined') {
		return node.textContent;
	}
	else if (typeof node.innerText != 'undefined') {
		return node.innerText;
	}
	else if (typeof node.text != 'undefined') {
		return node.text;
	}
	else {
		switch (node.nodeType) {
		case 3:
		case 4:
			return node.nodeValue;
			break;
		case 1:
		case 11:
			var innerText = '';
			for (var i = 0; i < node.childNodes.length; i++) {
				innerText += getInnerText(node.childNodes[i]);
			}
			return innerText;
			break;
		default:
			return '';
		}
	}
}




/* Functions for ajax rss and site search */

function rsshandler()
{
	if (req.readyState == 4)
	{
		// Make sure the status is "OK"
		if (req.status == 200)
		{
			var swappableSection = document.getElementById('searchResults');
			var notes = req.responseXML.getElementsByTagName('item');
			var str = '';
			for(i=0; i < notes.length; i++)
			{
				var noteNode = notes.item(i);
				if(noteNode != null && noteNode.hasChildNodes())
				{
					str += '<p><strong>' + noteNode.getElementsByTagName('title').item(0).text + '</strong><br />';
					str += noteNode.getElementsByTagName('category').item(0).text + '<br />';
					str += noteNode.getElementsByTagName('description').item(0).text + '</p>';
				}
			}
			swappableSection.innerHTML = str;
		  }
		else
		{
			alert("There was a problem retrieving the XML data:\n" +
				req.statusText);
		}
	}
}




function searchHandler()
{
	var swappableSection = document.getElementById('searchResults');
	//document.getElementById('rightCol').style.visibility = "visible";

	if (req.readyState == 1)
		swappableSection.innerHTML = rsltTable(rsltCell('Receiving results...\n'));
	if (req.readyState == 2)
		swappableSection.innerHTML = rsltTable(rsltCell('Data is loaded...\n'));
	if (req.readyState == 3)
		swappableSection.innerHTML = rsltTable(rsltCell('Interactive...\n'));
	if (req.readyState == 4)
	{
	   if (req.status == 200)
		{
			var results = req.responseXML.getElementsByTagName('result');
			var str = '';
			var sectionStr = '';
			var lineStr = '';
			for(i=0; i < results.length; i++)
			{
				var resultNode = results.item(i);
				if(resultNode != null && resultNode.hasChildNodes())
				{
					lineStr = '';
					if (resultNode.getAttribute("section") != sectionStr)
						{
						sectionStr = resultNode.getAttribute("section");
						lineStr += '<i><span class="highlight">' + sectionStr + '</span></i><br />';
						}
					lineStr += '<a href="' + resultNode.getAttribute("url") + '">'
					//lineStr += resultNode.firstChild.textContent;
					lineStr += getInnerText(resultNode.firstChild);
					lineStr += '</a>';
					str += rsltCell(lineStr);
				}
			}
			if (results.length == 0)
				str = rsltCell('No results found');
				//document.getElementById('rightCol').style.visibility = "visible";
			else
				str = rsltCell(results.length + ' results found:') + str;
				//document.getElementById('rightCol').style.visibility = "hidden";
				// str = '<div class="smallFont">' + str + '</div>';
			swappableSection.innerHTML = rsltTable(str);
		  }
		else
		{
			swappableSection.innerHTML = rsltTable(rsltCell("Error retrieving search results:\n" + req.statusText));
		}
	}
}



function timedSearch(searchurl)
{
		document.getElementById('searchResults').innerHTML = rsltTable(rsltCell('Searching...'));
		xmlGet(searchurl, searchHandler);
}

var tid = 0;

function dosearch(searchfor)
{
	var srchRslt = '';

	if (document.getElementById('rightCol')) {
		if (searchfor.length == 0)
			document.getElementById('rightCol').style.visibility = "visible";
		else
			document.getElementById('rightCol').style.visibility = "hidden";
		}

	if (searchfor.length > 2)
		{
		if (tid != 0) 
			clearTimeout(tid);
		srchRslt = 'Waiting...';
		searchfor = '/jkcm/default.aspx?pg=1443&searchfor=' + searchfor;
		tid = setTimeout('timedSearch("' + searchfor + '");', 1000);
		}else{
		if (searchfor.length >0)
			srchRslt = 'Please enter 3 or more characters...';
		}
	if (searchfor.length == 0)
	{
	 document.getElementById('searchResults').innerHTML = '';
	 document.getElementById('rightCol').style.visibility = "visible";
	 //document.getElementById('searchResults').style.visibility = "hidden";
	 } else	{
	 document.getElementById('searchResults').innerHTML = rsltTable(rsltCell(srchRslt));
	}
}

function rsltCell(forStr)
{
	return '<tr><td class="smallFont">' + forStr + '</td></tr>';
}

function rsltTable(forStr)
{
	var rslt = '';
	rslt = '<div id="ajaxsearchresults"><table id="ajaxsearchresultstable" width="100%" border="0" cellpadding="2" cellspacing="0" summary="Search results">\n';
	rslt = rslt + '<tr><td class="underline"><strong>Search Results</strong></td></tr>';
	rslt = rslt + forStr + '</table></div>\n';
	//alert(forStr);
	return rslt;
}







