﻿/*
Author:      	Daniel Peel.
Contact: 		mintpants@tiscali.co.uk.
Details: 		LiveSearch JavaScript.
Requires:       Base.js, Ajax.js, Opacity.js
Created:		02.05.09
Updated: 		
*/

function LiveSearch(event, searchFormField, popUpList, form, startLen) {

    var searchFormField = GetObj(searchFormField);
    var searchPos;

    //Get the popUp List
    var popUpList = GetObj(popUpList);

    if (searchFormField.value.length < startLen) {
        popUpList.style.display = 'none'; //lazy hide the pop up if we have too few chars
        return;
    }

    var formEl = GetObj(form);

    var formFields = formEl.elements;
    var postStr = "submitted=true";

    for (var _i = 0; _i < formFields.length; _i++) {
        postStr += "&" + formFields[_i].name + "=" + encodeURI(formFields[_i].value);
    }

    //Get the key pressed
    //*********************************************
    // make the list selectable with the arrow keys
    if (window.event) // IE
    {
        keynum = event.keyCode;
    }
    else if (event.which) // Netscape/Firefox/Opera
    {
        keynum = event.which;
    }

    //*********************************************************
    //dont post ajax again we are cycling up and down the list
    if (keynum != 40) {
        if (keynum != 38) {
            //alert("searching");
            jax = new Ajax();

            jax.callBack = function(responseText) {

                if (responseText.length > 0) {

                    //Is the list already showing?
                    if (popUpList.style.display != 'block') {

                        //get positition of search box
                        searchPos = findPosition(searchFormField);

                        //position the the popUpList
                        popUpList.style.left = searchPos[0] + 'px';
                        popUpList.style.top = searchPos[1] + searchFormField.offsetHeight + 'px';
                        //popUpList.style.width = searchFormField.offsetWidth + 'px';
                        //content

                        popUpList.style.display = 'block';
                        //fade in
                        var opac = new Opacity(popUpList);
                        opac.StartOpacity = 0;
                        opac.EndOpacity = 100;
                        opac.Time = 500;
                        opac.Run();
                    }

                    //Add the content
                    popUpList.innerHTML = responseText;

                    //find the list and add the close events and select events
                    var liItems = popUpList.getElementsByTagName("li");

                    //Add the close events
                    for (var _i = 0; _i < liItems.length; _i++) {
                        liItems[_i].onclick = function() {
                            searchFormField.value = this.innerHTML;
                            var opac = new Opacity(popUpList);
                            opac.Hide = true;
                            opac.StartOpacity = 100;
                            opac.EndOpacity = 0;
                            opac.Time = 200;
                            opac.Run();
                        };
                    }
                }
            }

            jax.postData("AJAX/LiveSearch.aspx", postStr);
        }
    }
    //************************************************ Is key down or UP keys 
    var liItems = popUpList.getElementsByTagName("li");
    //Going Down
    if (keynum == 40) {
       
        for (var _i = 0; _i < liItems.length; _i++) {

            //set the list element as selected
            if (_i > 0) {
                if (liItems[(_i - 1)].className == 'selected') {                   
                    liItems[(_i - 1)].className = null;
                    liItems[_i].className = 'selected';
                    searchFormField.value = liItems[_i].innerHTML;                     
                    break;
                }
            }
        }
    }
    
    //Going Up
    else if(keynum == 38)
    {
        for (var _i = liItems.length; _i >= 0; _i--) {

            //set the list element as selected
            try {
                if (liItems[(_i + 1)].className == 'selected') {
                    liItems[(_i + 1)].className = null;
                    liItems[_i].className = 'selected';
                    searchFormField.value = liItems[_i].innerHTML;                 
                    break;
                }

            }
            catch (e)  { }            
        }
    }  
}