var req;
var completeField;
var completeTable;
var count = 0;
var div2;
var div3;
var resultList;
var categoryList;
var para;
var anchor;
var img;
var loadingElement;

function initRequest() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
}

function clearTable() {
    if (completeTable.getElementsByTagName("dl").length > 0) {
        for (loop = completeTable.childNodes.length -1; loop >= 0 ; loop--) {
            completeTable.removeChild(completeTable.childNodes[loop]);
        }
    }
}

function showFullResult() {
	para = document.createElement("p");
	anchor = document.createElement("a");
        anchor.setAttribute("href", "javascript:document.getElementById('pp-search').submit();");
        //anchor.setAttribute("href", "#");
	//anchor.setAttribute("onclick", "document.searchForm.submit();return false;");
	div3.appendChild(para);
	para.appendChild(anchor);
	anchor.appendChild(document.createTextNode(fullResultText));
}

function addDivs() {
	div2 = document.createElement("div");
	div2.className = "pp-predictive-search";
	div3 = document.createElement("div");
	div3.className = "pp-predictive-search-content";
	div2.appendChild(div3);
	completeTable.appendChild(div2);
}	
	
function showEmptyMessage() {
	addDivs();
	resultList = document.createElement("dl");
	div3.appendChild(resultList);
    
    var emptyResponse = document.createElement("dt");
    emptyResponse.appendChild(document.createTextNode(noMatchingSuggestionsmessage));
    resultList.appendChild(emptyResponse);
    showFullResult();
}

function parseMessages(responseXML) {
	clearTable();
	
	if (responseXML == null) {
		return false;
    } else {
	
		var options = responseXML.getElementsByTagName("options")[0]; 
		
		if (options.childNodes.length > 0) {       	
			div2 = document.createElement("div");
			div2.className = "pp-predictive-search";
			div3 = document.createElement("div");
			div3.className = "pp-predictive-search-content";

			div2.appendChild(div3);
			completeTable.appendChild(div2);
			resultList = document.createElement("dl");
			div3.appendChild(resultList);
			
			var items = options.getElementsByTagName("category"); 
			
			for (var loop = 0; loop < items.length; loop++) {
				var category = items[loop];
				var categoryName = category.getElementsByTagName("categoryName")[0].firstChild.nodeValue;;
				categoryList = document.createElement("dt");
				resultList.appendChild(categoryList);
				categoryList.appendChild(document.createTextNode(categoryName));
				var option = category.getElementsByTagName("option");
				
				for (var x = 0; x < option.length; x++) {
					var child = option[x];
					var dd = document.createElement("dd");
					var displayText = child.getElementsByTagName("displayText")[0].firstChild.nodeValue;				
					var link = child.getElementsByTagName("link")[0].firstChild.nodeValue;
					
					linkElement = document.createElement("a");
	    			linkElement.setAttribute("href", link);
	    			linkElement.appendChild(document.createTextNode(displayText));
	    			dd.appendChild(linkElement);
	    			resultList.appendChild(dd);
    			}
	       }
	       showFullResult();
       }
    }
}

function showLoading() {
	addDivs();
    var loadingDiv = document.createElement("div");
    loadingDiv.setAttribute("class", "pp-loading");
    div3.appendChild(loadingDiv);
    loadingElement = document.createElement("p");
    loadingElement.appendChild(document.createTextNode(loadingText));
    loadingDiv.appendChild(loadingElement);
}

function callback() {
	clearTable();
    if (req.readyState == 4) {    	
        if (req.status == 200) {
            parseMessages(req.responseXML);
        }
        else {
        	showEmptyMessage();
        }
    }
}

function isEmpty(mytext) {
	var re = /^\s*$/; //Match any white space including space, tab, form-feed, etc.
	if (re.test(mytext)) {
		return true;
	} 
	else {
		return false;
	} 
}

function doCompletion() {
	completeField = document.getElementById("pp-search-terms");
    	completeTable = document.getElementById("pp-predictive-search-wrap");
	var inputValue = completeField.value;	
	if (isEmpty(inputValue)) {
		clearTable();
	}
	else {
		var url = "/PpPortalSupport/PredictiveSearch?searchInput=" + inputValue;
		req = initRequest();
		showLoading();
		req.open("GET", url, true);
		req.onreadystatechange = callback;
		req.send(null);
	}
}	
