var xmlHttpSearch;

var mDivMOver = false;

function showResult(e, pathType, strPath, txtSrc) {

	var unicode = e.keyCode? e.keyCode : e.charCode;

	if(unicode == 13) {

		selSpan = document.getElementById("selSpanId").value;

		if(document.getElementById('hidSpnProdId' + selSpan)) {

			prodId = document.getElementById('hidSpnProdId' + selSpan).value;
			document.getElementById('searchProdId').value = prodId;

			spanVal = document.getElementById('hidSpnSearch' + selSpan).value;
			document.getElementById('dispSearchVal').value = spanVal;

			document.getElementById('search_popup').innerHTML = '';
			document.getElementById('search_popup').style.display = 'none';
		}
		submitFrmTopSearch('SP');
	}
	else if(unicode == 38 || unicode == 40) {

		var minVal = 1;
		var maxVal = document.getElementById('cntSpanId').value;

		// up key pressed
		if(unicode == 38) {

			selSpan = document.getElementById("selSpanId").value;
			selSpan--;
			document.getElementById("selSpanId").value = selSpan;

			if(selSpan < minVal) {
				selSpan = minVal;
				document.getElementById("selSpanId").value = selSpan;
			}

			if(selSpan > maxVal) {
				selSpan = maxVal;
				document.getElementById("selSpanId").value = selSpan;
			}

			var flDisp = true;
			var cnt = 1;
			while(flDisp) {

				spanSrc = document.getElementById('liveSearch' + cnt);
				if(spanSrc) {

					if(cnt == selSpan) {
						spanSrc.className='li_active';
					}
					else {
						spanSrc.className='';
					}
				}
				else {
					flDisp = false;
				}

				cnt++;
			}
		}
		// down key pressed
		else if(unicode == 40) {

			selSpan = document.getElementById("selSpanId").value;
			selSpan++;
			document.getElementById("selSpanId").value = selSpan;

			if(selSpan < minVal) {
				selSpan = minVal;
				document.getElementById("selSpanId").value = selSpan;
			}

			if(selSpan > maxVal) {
				selSpan = maxVal;
				document.getElementById("selSpanId").value = selSpan;
			}

			var flDisp = true;
			var cnt = 1;
			while(flDisp) {

				spanSrc = document.getElementById('liveSearch' + cnt);
				if(spanSrc) {

					if(cnt == selSpan) {
						spanSrc.className='li_active';
					}
					else {
						spanSrc.className='';
					}
				}
				else {
					flDisp = false;
				}

				cnt++;
			}
		}
	}
	else {

		document.getElementById("selSpanId").value = 0;

		if (txtSrc.length==0) {
			document.getElementById("search_popup").style.display = "none";
			document.getElementById("search_popup").innerHTML = "";
			return;
		}
		xmlHttpSearch=GetXmlHttpObjectSearch()
		if (xmlHttpSearch==null) {
			alert ("Your browser does not support XML HTTP Request");
			return;
		}

		var url = strPath + "livesearchajax.php?doAction=GetSearchResult";
		url = url + "&txtSrc=" + encodeURIComponent(txtSrc) + "&sitePath=" + pathType;
		xmlHttpSearch.onreadystatechange=stateChangedSearch ;
		xmlHttpSearch.open("GET",url,true);
		xmlHttpSearch.send(null);
	}
}

function changeColor(selSpan) {
	var flDisp = true;
	var cnt = 1;
	while(flDisp) {

		spanSrc = document.getElementById('liveSearch' + cnt);
		if(spanSrc) {

			if(cnt == selSpan) {
				spanSrc.className='li_active';
				document.getElementById('selSpanId').value = cnt;
			}
			else {
				spanSrc.className='';
			}
		}
		else {
			flDisp = false;
		}

		cnt++;
	}
}

function submitSearchForm(intCnt, prodId) {
	if(intCnt == 0) {
		 if(!mDivMOver) {
			document.getElementById('search_popup').innerHTML = '';
			document.getElementById('search_popup').style.display = 'none';
		 }
	}
	else {
		if(document.getElementById('hidSpnSearch' + intCnt)) {

			document.getElementById('searchProdId').value = prodId;

			spanVal = document.getElementById('hidSpnSearch' + intCnt).value;
			document.getElementById('dispSearchVal').value = spanVal;

			document.getElementById('search_popup').innerHTML = '';
			document.getElementById('search_popup').style.display = 'none';
		}
		submitFrmTopSearch('SP');
	}
}

function stateChangedSearch() {
	if (xmlHttpSearch.readyState==4) {

		strResponseText = xmlHttpSearch.responseText;
		arrResponseText = strResponseText.split("~@");

		if(arrResponseText[0] != "") {
			document.getElementById("search_popup").style.display = "";
			document.getElementById("search_popup").innerHTML = arrResponseText[0];
		}
		else {
			document.getElementById("search_popup").style.display = "none";
			document.getElementById("search_popup").innerHTML = "";
		}

		document.getElementById("cntSpanId").value = arrResponseText[1];
	}
}

function GetXmlHttpObjectSearch() {
	/*if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject) {
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;*/

	try	{
		// Firefox, Opera 8.0+, Safari
		return new XMLHttpRequest();
	}
	catch(e) {
		// Internet Explorer
		try	{
			return  new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e) {
				alert("Your browser does not support AJAX!");
				return null;
			}
		}
	}
}