﻿
 function $(id)
{
    return document.getElementById(id);
}

var theLBControl = null;
var theTBControl = null;
var theDiv = null;

document.onclick = onDocumentClick;
//document.forms[0].onsubmit = onSubmit;
var calculateClicked = false;
var textBoxFocused = false;
var itemSelected = false;

function onDocumentClick(e)
{
    
    e = e || event;
    var target = e.srcElement || e.target;
    //var keyCode = e.keyCode || e.which;
    //alert(theDiv.id);
    var keyCode = e.keyIdentifier;
    var isValid = (theLBControl!=null && theTBControl!=null && theDiv!=null);
    var hasValues = (isValid && theLBControl.selectedIndex != -1);
    var divVisible = (isValid && theDiv.style.visibility == "visible") ? true : false;
    if(isValid && divVisible && hasValues && !calculateClicked && !textBoxFocused)
    {
        itemSelected = true;
        theTBControl.value = theLBControl.options[theLBControl.selectedIndex].value;
        theLBControl.length = 0;
     }   
    if(isValid && !textBoxFocused) {theDiv.style.visibility = "hidden";}
    if(isValid && textBoxFocused) {theDiv.style.visibility = "visible";  hideOthers(theDiv.id); textBoxFocused = false;}
    if(isValid && calculateClicked) {theDiv.style.visibility = "visible"; hideOthers(theDiv.id); calculateClicked = false;}
    //alert(itemSelected);
    /*
    var To = (TextBoxes!=null && TextBoxes[1]!=null && $(TextBoxes[1].toString())!=null) ? $(TextBoxes[1].toString()).value : "";
    var Via = (TextBoxes!=null && TextBoxes[2]!=null && $(TextBoxes[2].toString())!=null) ? $(TextBoxes[2].toString()).value : "";
    if(itemSelected && target.id == "ctl00_ContentPlaceHolder1_calculatorList_tabCalculator_ctl00_imgTabCalculator")
    {
        //itemSelected = false;
        return false;
    }
    else if(itemSelected && (To == "" || Via == "") && target.id == "ctl00_ContentPlaceHolder1_calculatorList_tabCalculator_ctl00_imgTabCalculator")
    {
        //itemSelected = false;
        return false;
    }    
    //var Via = $(TextBoxes[2].toString()).value
    if(isValid && target.id == TextBoxes[0].toString())
        return false;
    if(isValid && target.id == TextBoxes[1].toString())
        return false;

    if(isValid && target.id == TextBoxes[2].toString() && Via=="")
        return false;
    */
    //theLBControl = null;theTBControl = null;theDiv = null;
}

function hideOthers(currentDiv)
{
    for(var i=0;i<DIVs.length;i++)
    {   
        var divID = DIVs[i].toString();
        if(divID != currentDiv)
        {
            var div = $(divID)
            div.style.visibility = "hidden";
        }
    }   
}   

function hideAll()
{
    for(var i=0;i<DIVs.length;i++)
    {   
        var divID = DIVs[i].toString();
        var div = $(divID)
        div.style.visibility = "hidden";
    }   
} 

function populateOnAction()
{
    var isValid = (theLBControl && theTBControl && theDiv);
    var hasValues = (isValid && theTBControl.value.length>0);
    if(isValid && hasValues && theDiv.style.visibility == "visible" && theLBControl.selectedIndex != -1)
    {    
        theTBControl.value = theLBControl.options[theLBControl.selectedIndex].value;                         
        theDiv.style.visibility = "hidden";
        //theTBControl.focus();
        return false;
    }
 }
 
 function lstOnBlur(divId,listId,filterId,evt)
 {
    
    textBoxFocused = false;
    populateOnAction(evt);
 }
 
  function txtClick(divId,listId,filterId,items,maxItemsToShow,labelId,evt ,objTB)
 {
    //alert('txtClick' + evt.keyCode);
    
    var lstControl = $(listId);
    var filter = $(filterId).value;
    var div = $(divId);
    theLBControl = lstControl;
    theTBControl = $(filterId);
    theDiv = div;
    /*
    if(filter.length>0)
        textBoxFocused = true;
    //populateOnAction(evt);
    //alert('txtClick' + theDiv.id);
    
    hideOthers(theDiv.id);
    if(filter.length>0)
    {
        txtKeyUp(divId,listId,filterId,items,maxItemsToShow,labelId,evt)
        return false;
    }
    */
    theTBControl.value = "";
 }
 function populatelist(obj,form)
 {  
    var results3 = new Array();
    calculateClicked = false;
    try
    {
        for(var i=0;i<TextBoxes.length;i++)
        {
            var txtValue = form[TextBoxes[i].toString()];
            if(txtValue.value.length>0)
            {
                results3 =  getFilteredList(txtValue.value, airports,100,"");
                
                if(results3.length==0)
                {
                    var qry = txtValue.value.substring(0,3).toLowerCase();
                    txtValue.value = qry;
                    if(airportExists(qry,airports))
                    {
                        txtKeyUp(DIVs[i].toString(),ListBoxes[i].toString(),TextBoxes[i].toString(),airports,100,Labels[i].toString(),null);
                        form[TextBoxes[i]].focus();
                    }
                    else
                    {
                        var qry2 = txtValue.value.substring(0,2);
                        txtValue.value = qry2;
                        txtKeyUp(DIVs[i].toString(),ListBoxes[i].toString(),TextBoxes[i].toString(),airports,100,Labels[i].toString(),null);
                        form[TextBoxes[i]].focus();
                    }
                    calculateClicked = true;
                    return false;
                 } 
             }
         }
         return true;
    }
    catch(ex)
    {
        //alert(ex.message);
        return false;
    }
 }

function selectedAirportExists(query,availableItems)
{
    query = query.toLowerCase();
    for (var i = 0; i < availableItems.length; i ++) 
    {
           if(query || availableItems[i].toLowerCase() == query)
           {
                return true;
           }
    }
    return false;
 }
 
function airportExists(query,availableItems)
{
    for (var i = 0; i < availableItems.length; i ++) 
    {
           if(!query || availableItems[i].toLowerCase().indexOf(query.toString())>-1)
           {
                return true;
           }
    }
    return false;
 }
 function lstKeuUp(divId,listId,filterId,evt)
 {
    if(evt!=null && (evt.keyCode == 13))
    {
        populateOnAction(evt);
    }
 }
 function txtKeyUp(divId,listId,filterId,items,maxItemsToShow,labelId,evt)
{
   // alert('txtKeyUp' + evt.keyCode);
    if($(filterId)!=null){
        //var items = eval(listVariable);
        var div = $(divId);
        var filter = $(filterId).value;
        var lstControl = $(listId);
        theLBControl = lstControl;
        theTBControl = $(filterId);
        theDiv = div;
        
        if(filter.length>0)
        {
            
            theDiv.style.visibility = "visible";
            
            //alert(evt.keyCode);
            if(evt!=null && (evt.keyCode == 13))
            {
                populateOnAction();
                if(theLBControl.length == 0)
                   theDiv.style.visibility = "hidden";
                return false;
            }
            
            if(evt!= null && evt.keyCode == 40)
            {
                lstControl.focus();
            }           
           
            var filteredList = getFilteredList(filter,items,maxItemsToShow,labelId);
            
            
            var selectedIndex=-1;

            //Populate the new options    
            for(var index=0;index<filteredList.length;index++)
            {
                if(index>=lstControl.options.length)
                    lstControl[index]=document.createElement("option");
                            
                lstControl[index].text=filteredList[index];
                lstControl[index].value=filteredList[index];    
                
                //if(selectedValue && selectedValue==filteredList[index])
                //    selectedIndex=index;
            } 
            
            
            lstControl.selectedIndex = selectedIndex;
            if(lstControl.selectedIndex ==-1)
            {
                lstControl.selectedIndex = 0;
            }
            //Remove the unused options
            for (var index=lstControl.options.length-1; index>=filteredList.length; index--)
            {
                lstControl.options[index] = null;
            }
            
            var c = lstControl.options.length;
             
        }
        else
        {
            theDiv.style.visibility = "hidden";
        }        
    }
   hideOthers(theDiv.id);
}
function getFilteredList(query, availableItems,maxItemsToShow,labelId) 
{
    //window.status = query;
    var result = [];
    var message="";     
    var result2 = new Array();
    if(labelId!=null && labelId !="")
        $(labelId).innerHTML ="Filtering";   
    
    query = query.toLowerCase();
    //var queries = new Array();
    //queries = query.split(' ');
    // maxItemsToShow = 500;
    for (var i = 0; i < availableItems.length; i ++) 
    {
//        for(var j=0; j < queries.length; j++)
//        {
//            if(!query || availableItems[i].toLowerCase().indexOf(queries[j].toString())>-1  )
//                result.push(availableItems[i]);
//         }
           if(!query || availableItems[i].toLowerCase().indexOf(query)>-1  )
                result.push(availableItems[i]);
        if(result.length>maxItemsToShow)
        {
           // message="Too many matching items, results truncated";
            break;
        }
    }
    
    if(result.length==0)
        message="No results found for your filter";
    if(labelId!=null && labelId !="")
        $(labelId).innerHTML=message;
    result.sort();
    for (var j = 0; j < result.length; j++) {
		    var str = result[j].toString().substring(2);
		    result2.push([str]);
		}		
		return result2;
    return result2;   
}
 
 
 /*
    for(var i=0;i<TextBoxes.length;i++)
    {
        var txtValue = document.forms[0][TextBoxes[i].toString()];
        if(txtValue.value.length>0)
        {
            alert(txtValue.value);
            if(!selectedAirportExists(txtValue.value,airports))
            {
               
                return false;
            }
        }
    }   
    
function onSubmit(e)
{
    alert('onSubmit');
    return false;
}

*/
