﻿
var TextUtil = new Object;

TextUtil.autosuggestMatch = function (sText, arrValues) 
                            {
                                var arrResult = new Array;

                                if (sText != "") 
                                {
                                
                                    if (validateNumber(sText))
                                    {
                                        var postcode = '';
                                        var helper = '';
                                        for (var i=0; i < arrValues.length; i++) 
                                        {
                                            helper = arrValues[i]["Text"].split(', ');
                                            postcode = helper[1];
                                            helper = postcode.split(' ');
                                            postcode = helper[1];
                                            
                                            
                                            if ((postcode.toString()).indexOf(sText) == 0) 
                                                arrResult.push(arrValues[i]["Text"]);

                                        }                                        
                                        
                                    }
                                    else
                                    {
                                            for (var i=0; i < arrValues.length; i++) 
                                            {
                                                if ((arrValues[i]["Text"].toLowerCase()).indexOf(sText.toLowerCase()) == 0) 
                                                    arrResult.push(arrValues[i]["Text"]);
                                            }
                                    }
                                }
                                
                                return arrResult;
                                };



TextUtil.autosuggest = function (oTextbox, arrValues, sListboxId) 
                        {
                            var oListbox = $(sListboxId);
                            var arrMatches = TextUtil.autosuggestMatch(oTextbox.value, arrValues);
                            
                            ListUtil.clear(oListbox);

                            if (arrMatches)
                            {
                                if (arrMatches.length > 0)
                                {                            
                                    for (var i=0; i < arrMatches.length; i++) 
                                    {
                                        ListUtil.add(oListbox, arrMatches[i]);
                                    }
                                    showHideSuggestList(oListbox, true);
                                    var skipToListBoxHandler = function(event)
                                                    {
                                                        skipToListBox(oTextbox, oListbox, event);
                                                        EventUtil.removeEventHandler(oTextbox, 'keydown', skipToListBoxHandler);
                                                    };
                                    EventUtil.addEventHandler(oTextbox, 'keydown', skipToListBoxHandler);
                                }
                                else
                                    showHideSuggestList(oListbox, false);
                            }
                            else
                                showHideSuggestList(oListbox, false);
                            
                        };

function showHideSuggestList(el, isShow)
{
    var IDtoReplace = el.id;
    
    var l = window.location.toString().toLowerCase();
    if(l.indexOf('completed.aspx') > -1)
        changeCssClass(IDtoReplace, isShow ? 'suggestListBoxVisibleCompl' : 'suggestListBoxHiddenCompl');
    else        
        changeCssClass(IDtoReplace, isShow ? 'suggestListBoxVisible' : 'suggestListBoxHidden');
    
    
}


function skipToListBox(element, oListbox, e)
{
    if(oListbox)
    {
        e = EventUtil.formatEvent(e);

        if (e.keyCode == 40)
            oListbox.focus();
    }

}

function setSuggestText(oListbox, sTextboxId)
{
    var oTextbox = $(sTextboxId);
    var txt = '';
    if (oListbox.selectedIndex > -1) 
    {
        txt = oListbox.options[oListbox.selectedIndex].text;
        oTextbox.value = txt;
        setLabelsOnCompletedPage(sTextboxId, txt);
    }
    showHideSuggestList(oListbox, false);
}

function setLabelsOnCompletedPage(sTextboxId, txt)
{
    var l = window.location.toString().toLowerCase();
    if( l.indexOf('completed.aspx') > -1 )
    {
        if (sTextboxId.indexOf('JobTitle') > -1)
        {
            if (isMoz)
                $(ASPprefixShort + 'lblSalaryReportFor').textContent = txt;
            else
                $(ASPprefixShort + 'lblSalaryReportFor').innerText = txt;
        }
        else if (sTextboxId.indexOf('WorkplaceLocation') > -1)
        {
            if (isMoz)
                $(ASPprefixShort + 'lblSalaryReportLocation').textContent = txt;
            else
                $(ASPprefixShort + 'lblSalaryReportLocation').innerText = txt;
        }
        
    }
}


function acceptListBoxValueOnEnter(oListbox, sTextboxId, e)
{
    var key = getKeyPressed(e);
    if(key =='13')
    {
        var oTextbox = $(sTextboxId);
        var txt = '';
        if (oListbox.selectedIndex > -1) 
        {
            txt = oListbox.options[oListbox.selectedIndex].text;
            oTextbox.value = txt;
            setLabelsOnCompletedPage(sTextboxId, txt);

        }
        showHideSuggestList(oListbox, false);
    }

}

function getKeyPressed(e)
{
   var key = document.all ? window.event.keyCode : e.which;
   return key;
}

function suggestJobTitle(el, afterNbOfKeys, prefix)
{
    var txt = el.value;
    if (txt.length >= 1)
    { 
        getJobTitleSuggestions(txt, el, prefix + 'JobTitle_lbJobTitle', afterNbOfKeys);
    }
    else
    {
        if (txt.length == 0)
           showHideSuggestList($(prefix + 'JobTitle_lbJobTitle'), false);
    }        
}

function suggestWorkplaceLocation(el, afterNbOfKeys, prefix)
{
    var txt = el.value;
    if (txt.length > 1)
    { 
        getWorkLocationSuggestions(txt, el, prefix + 'WorkplaceLocation_lbWorkplaceLocation', afterNbOfKeys);
    }    
    else
    {
        if (txt.length == 0)
            showHideSuggestList($(prefix + 'WorkplaceLocation_lbWorkplaceLocation'), false);
    }

}


function getJobTitleSuggestions(txt, el, listboxID, afterNbOfKeys)
{
    if (txt.length >= afterNbOfKeys)
    { 
        
        new Ajax.Request('/WebServices/SaveDataFromJS.asmx/GetJobTitleSuggestions',
        {   parameters:'txt='+txt,  
            method: 'post',
             onSuccess: function(transport)
                        {  
                            var xml = GetXml( transport );
                            fillSuggestions(xml, el, listboxID);
                        }
            }
        );
        
    }
}

function getWorkLocationSuggestions(txt, el, listboxID, afterNbOfKeys)
{
    if (txt.length > afterNbOfKeys)
    { 
        
        new Ajax.Request('/WebServices/SaveDataFromJS.asmx/getWorkLocationSuggestions',
        {   parameters:'txt='+txt,  
            method: 'post',
             onSuccess: function(transport)
                        {  
                            var xml = GetXml( transport );
                            fillSuggestions(xml, el, listboxID);
                        }
            }
        );
        
    }
}


function fillSuggestions(xml, el, listboxID)
{
    var JSON = xml.childNodes[0].nodeValue;
    var dataArray = convertJSONtoArray(JSON);
    var data = dataArray.data;

    TextUtil.autosuggest(el, data, listboxID);
}






