/*INISEAL:[6qgnG4MB3xMKyrLoStkS85O83qg%3D%0A]*/
var inputText = "icss:search:query";
var outputList = "keywords";
var autoLayer = "autoLayer";
var selectRowIndex = -1;
var pageurl = "";

function objById(id)
{
	var obj = document.getElementById(id);
    //if (obj == null)
       // alert("Fail to find the ["+id+"] Element!");
   // else
        return obj;
}

function connectAjax(url, id, query, page) {
		var async = this;
		var ajaxObj = new ajax(url);

		ajaxObj.addParam('forwardName', 'list') ;
		ajaxObj.addParam('autoId', id) ;
		ajaxObj.addParam('inverse', 'true');
		ajaxObj.addParam('count', 10);
		//ajaxObj.addParam('query', query) ;
		ajaxObj.addParam('query', encodeURIComponent(query)) ;
		pageurl = page;

		ajaxObj.execMethod = function(xmlRequest) {
			//var keywords = xmlRequest.responseXml;
			var keywords = xmlRequest.responseText;
			//alert(keywords1);
			if(keywords.length != 0){
				 displayKeywords(keywords, query);
				 WinResize();
			}
		}
		ajaxObj.connect() ;
}

function verify() 
{ 
 if (xmlDoc.readyState != 4) 
 { 
   return false; 
 } 
}


function displayKeywords(keywords, query)
{
    var word_row, word_col, word_text;
	
	var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
	 xmlDoc.async="false"; 
	 xmlDoc.loadXML(keywords); 
	 xmlObj=xmlDoc.documentElement; 
	
	while (objById(outputList).firstChild.childNodes.length > 0)
        objById(outputList).firstChild.removeChild(objById(outputList).firstChild.childNodes[0]);
	
	if(xmlObj == null){
		closewindow();
		return;
	}

    for (var i=0; i<xmlObj.childNodes[0].childNodes.length; i++)
    {
        word_row = document.createElement("tr");
        word_col = document.createElement("td");
        word_col.style.paddingTop = "0";
        word_col.style.paddingLeft = "2";

		word_col.onmouseover = moveMouse;
		word_col.onmouseout = moveMouse;
		word_col.onclick = selectRow;
		
		word_text = xmlObj.childNodes[0].childNodes[i].text;
        var word_reg = new RegExp('^'+query+'+');
		var word_reg_last = new RegExp(query+'$');

        if (word_reg.test(word_text))
       {
            word_color = document.createElement("font");
            word_color.color = "#0099FF";
            word_color.innerText = query;
            word_col.appendChild(word_color);
			var wordtmp = word_text;
			wordtmp = wordtmp.substring(query.length, wordtmp.length);
			word_text = document.createTextNode(wordtmp);
			word_col.appendChild(word_text);
			word_row.appendChild(word_col);
			
        }else if(word_reg_last.test(word_text)){
			
			var wordtmplast = word_text;
			wordtmplast = wordtmplast.substring(0, word_text.length-query.length);
			word_text = document.createTextNode(wordtmplast);
			
			word_col.appendChild(word_text);

			word_color = document.createElement("font");
            word_color.color = "#0099FF";
            word_color.innerText = query;
            word_col.appendChild(word_color);

            word_row.appendChild(word_col);
		}
        else{
            word_text = document.createTextNode(word_text);
			word_col.appendChild(word_text);
			word_row.appendChild(word_col);
		}

        
        objById(outputList).firstChild.appendChild(word_row);
    }

    objById(autoLayer).style.display = "block";
}

function getKeywords(url, id, keyword, pageurl,event)
{
	var frm =document.getElementById('searchfrm');


    if (event.keyCode == 8 || (event.keyCode > 45 && event.keyCode < 91) || (event.keyCode > 95 && event.keyCode < 112))
    {

        if (keyword.length > 0)
        {

			connectAjax(url, id, keyword, pageurl);
        }
        else
        {
            while (objById(outputList).firstChild.childNodes.length > 0)
                objById(outputList).firstChild.removeChild(objById(outputList).firstChild.childNodes[0]);
        }
        selectRowIndex = -1;
		
    }
    else if (event.keyCode > 32 && event.keyCode < 41 && objById(outputList).firstChild.childNodes.length > 0)
    {
		//  objById('autolist').focus();
        switch(event.keyCode)
        {
            case 33:   //pageup
                if (objById(autoLayer).style.display != "none")
                {
                    clearHighlight();
	                selectRowIndex = 0;
                    highlight(objById(outputList).firstChild.childNodes[selectRowIndex], "D9E4F2");
					frm.elements[inputText].value = objById(outputList).firstChild.childNodes[selectRowIndex].innerText;
    			}
                break;
            case 34:   //pagedn
                if (objById(autoLayer).style.display != "none")
                {
                    clearHighlight();
                    selectRowIndex = objById(outputList).firstChild.childNodes.length - 1;
                    highlight(objById(outputList).firstChild.childNodes[selectRowIndex], "D9E4F2");
					frm.elements[inputText].value = objById(outputList).firstChild.childNodes[selectRowIndex].innerText;
                   // objById(inputText).value = objById(outputList).firstChild.childNodes[selectRowIndex].innerText;
                }
                break;
            case 38:      // arrow up
                if (selectRowIndex == 0)
                {
                    objById(autoLayer).style.display = "none";
                    selectRowIndex--;
                }
                if (selectRowIndex > 0)
                {
					objById("autolist").scrollTop -= 22;
                    clearHighlight();
                    highlight(objById(outputList).firstChild.childNodes[--selectRowIndex], "D9E4F2");
					frm.elements[inputText].value = objById(outputList).firstChild.childNodes[selectRowIndex].innerText;
                    //objById(inputText).value = objById(outputList).firstChild.childNodes[selectRowIndex].innerText;
                }
                break;
            case 40:  // arrow down
                if (selectRowIndex < objById(outputList).firstChild.childNodes.length - 1)
                {
					objById("autolist").scrollTop += 22;
					clearHighlight();
                    highlight(objById(outputList).firstChild.childNodes[++selectRowIndex], "D9E4F2");
					frm.elements[inputText].value = objById(outputList).firstChild.childNodes[selectRowIndex].innerText;
                }
                if (selectRowIndex == 0)
                {
                    objById(autoLayer).style.display = "block";
                }
                break;
        }
    }
   // else if (event.keyCode == 13)   //enter
    //    searchSubmit();
	
}

function clearHighlight()
{
    for (i=0; i<objById(outputList).firstChild.childNodes.length; i++)
        objById(outputList).firstChild.childNodes[i].style.background = "";
}

function moveMouse()
{
    var obj = window.event.srcElement.parentNode;
    while (obj.tagName != "TR")
        obj = obj.parentNode;

    clearHighlight();
    highlight(obj, "D9E4F2");
}

function highlight(obj, color)
{
    if (typeof(color) == "undefined")
        color = "#D9E4F2";
    if (color.indexOf('#') < 0)
        color = "#" + color;

    if (obj.style.background == color.toLowerCase())
        obj.style.background = "";
    else
        obj.style.background = color;
}

function selectRow()
{
    var obj = window.event.srcElement.parentNode;

    while (obj.tagName != "TR")
        obj = obj.parentNode;
	
	var param = "";
	if(objById("icss:search:query:field") != null){
		param = param + "&icss:search:query:field=" + objById("icss:search:query:field").value;
	}
	if(objById("icss:search:query:op") != null){
		param = param + "&icss:search:query:op=" + objById("icss:search:query:op").value;
	}
    top.location.href = pageurl + "?&icss:search:query=" + encodeURI(obj.innerText) + param;
}

function closewindow()
{
	objById(autoLayer).style.display="none";
}

function hideORshow(obj)
{
    if (obj.style.display == "none")
        obj.style.display = "block";
    else
        obj.style.display = "none";
}


function searchSubmit()
{
	alert('searchSubmit');
    return false;
}

function WinResize()
{
	 var count = objById(outputList).firstChild.childNodes.length;
	 objById(autoLayer).style.height = 90;
	 objById("autolist").style.height = 80;
}

function OnFocus()
{
	alert("focus");
}