/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var pageType;
function AutoComplete(oText, oDiv, nMaxSize,page)
{
    // initialize member variables
    this.oText = oText;
    this.oDiv = oDiv;
    this.nMaxSize = nMaxSize;
    this.page = page;
	pageType=page;
	

    // attach handlers to the text-box
    oText.AutoComplete = this;
    oText.onkeyup = AutoComplete.prototype.onTextChange;
    oText.onChange = AutoComplete.prototype.onTextChange;
    oText.onblur = AutoComplete.prototype.onTextBlur;

}


AutoComplete.prototype.onTextBlur = function()
{
    this.AutoComplete.onblur();
}

AutoComplete.prototype.onblur = function()
{
    this.oDiv.style.visibility = "hidden";
}

AutoComplete.prototype.onTextChange = function()
{
    this.AutoComplete.onchange(arguments[0]);
}

AutoComplete.prototype.onDivMouseDown = function()
{
    this.AutoComplete.oText.value = this.innerHTML;
}

AutoComplete.prototype.onArrowkeypress = function()
{
    this.AutoComplete.onArrowkey();
}

AutoComplete.prototype.onDivMouseOver = function()
{
    this.className = "AutoCompleteHighlight";
}

AutoComplete.prototype.onDivMouseOut = function()
{
    this.className = "AutoCompleteBackground";
}

AutoComplete.prototype.onchange = function(event)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (event) keycode = event.which;
    if(keycode == 40){
        this.oDiv.style.visibility = "visible";
        if(this.oDiv.hasChildNodes()){
            var children = this.oDiv.childNodes;
            var selecteddiv=-1;
            for(var i=0;i<children.length;i++){
                if(children[i].className == "AutoCompleteHighlight"){
                    selecteddiv = i;
                }
            }
            if(selecteddiv == -1){
                children[0].className = "AutoCompleteHighlight";
                this.oText.value=children[0].innerHTML;
            }else{
                for(var j=0;j<children.length;j++){
                    if(j == selecteddiv+1){
                        children[j].className = "AutoCompleteHighlight";
                        this.oText.value=children[j].innerHTML;
                    }else{
                        children[j].className = "";
                    }
                }
            }
        }
    }else if(keycode == 38){
        this.oDiv.style.visibility = "visible";
        if(this.oDiv.hasChildNodes()){
            children = this.oDiv.childNodes;
            selecteddiv=-1;
            for(i=0;i<children.length;i++){
                if(children[i].className == "AutoCompleteHighlight"){
                    selecteddiv = i;
                }
            }
            if(selecteddiv == -1){
            }else{
                for(j=children.length-1;j>=0;j--){
                    if(j == selecteddiv-1){
                        children[j].className = "AutoCompleteHighlight";
                        this.oText.value=children[j].innerHTML;
                    }else{
                        children[j].className = "";
                    }
                }
            }
        }
    }else if(keycode != 13){
        var txt = this.oText.value;
        if(txt!=null && txt != ""){
            //var categoryindex=document.getElementById("category").selectedIndex;
           // var arrcategory=document.getElementById("category").options;
            //var valcategory=arrcategory[categoryindex].value;

            //var languageindex=document.getElementById("language").selectedIndex;
            //var arrlanguage=document.getElementById("language").options;
            //var vallanguage=arrlanguage[languageindex].value;
            //url = "loginlanding/getdata.jsp?searchfor="+txt+"&maxvalues="+this.nMaxSize+"&category="+valcategory+"&language="+vallanguage;
            //url = "loginlanding/getdata.jsp?searchfor="+txt+"&maxvalues="+this.nMaxSize+"&category="+categoryindex+"&language="+vallanguage;
    var formName;
    var searchTextBox;

    if(pageType=='main'){
        formName = 'searchbysongname';
        searchTextBox = 'searchKeyWord';
    }
    if(pageType=='popup'){
        formName = 'searchbysongname1';
        searchTextBox = 'searchKey';
    }
    

	var valarr=document.getElementById(formName).radio;

	var length=valarr.length;

    var radioval="";
    for(var i=0;i<length;i++)
    {
        if(valarr[i].checked)
        {

            radioval=valarr[i].value;

        }
    }
	
			//var radioVal = document.getElementById('radio').value;

            var text = document.getElementById(searchTextBox).value;
            url = "autoComplete/getdata.jsp?searchKeyWord="+text+"&maxvalues="+this.nMaxSize+'&radio='+radioval;
            callUrlAutoComplete(url,this);
        }else // hide the popup-div
        {
            this.oDiv.innerHTML = "";
            this.oDiv.style.visibility = "hidden";
        }
    }
}

function populateDiv(titles,objThis)
{
    var aStr = new Array();
    if(titles!=null || titles !=""){
        if(titles.indexOf(',')!=-1){
            aStr = titles.split(',');
        }else{
            aStr[0] = titles;
        }

        // clear the popup-div.
        while ( objThis.oDiv.hasChildNodes() )
            objThis.oDiv.removeChild(objThis.oDiv.firstChild);


        // add each string to the popup-div
        var i, n = aStr.length;
        for ( i = 0; i < n; i++ )
        {
            var oDiv = document.createElement('div');
            objThis.oDiv.appendChild(oDiv);
            oDiv.innerHTML = aStr[i];
            oDiv.onmousedown = AutoComplete.prototype.onDivMouseDown;
            oDiv.onmouseover = AutoComplete.prototype.onDivMouseOver;
            oDiv.onmouseout = AutoComplete.prototype.onDivMouseOut;
            oDiv.id = "Div_"+i;
            oDiv.AutoComplete = objThis;
            oDiv.style.textAlign="left";
            oDiv.style.cursor = "default";
        }
        objThis.oDiv.style.visibility = "visible";
    }
}
function createAutoComplete()
{
    
    new AutoComplete(
        document.getElementById('searchKeyWord'),
        document.getElementById('theDiv'),
        5,'main'
        );
}
function createAutoCompleteForPopups()
{
    
    new AutoComplete(
        document.getElementById('searchKey'),
        document.getElementById('theDiv1'),
        5,'popup'
        );
}


var xmlhttp;
function init() {
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        alert("Your browser does not support XMLHTTP!");
        return false;
    }
    return true;
}

function callUrlAutoComplete(url,obj ) {
    var titles;
    if(init()) {
        xmlhttp.onreadystatechange=function() {
            if(xmlhttp.readyState==4) {

                if(xmlhttp.status==200)
                {
                    titles = xmlhttp.responseText;
                    populateDiv(titles,obj);
                }
                else
                {
					
                    //window.location="loadHomePage.jsp";
                }
            }
        }
        xmlhttp.open("GET",url,true);
        xmlhttp.send(null);
    }

}




