﻿// JScript File

var length;
var textbox;
var Localdropdown;
var localVal;

var onPost = false;

function setPost(value)
{
    onPost = value;
}

function getLocations(textbox, Localdropdown, localVal)
{
    var textbox = $get(textbox)
    
    this.textbox = textbox;
    this.Localdropdown = Localdropdown;
    this.localVal = localVal;
    
    var dropdown = $get(this.Localdropdown);
    var val;
    
    if(textbox.value.length >= 3)
    {
        if(length > textbox.value.length)
        {
            val = true;
        }
        else
        {
            val = false;
        }
        
        JobsGroup.Locations.GetLocations(textbox.value, val, ResponseHandler);
        dropdown.style.display = 'block';
    }
    else if (textbox.value.length < 3)
    {
        dropdown.style.display = 'none';
        $get('successImage').style.display = 'none';
    }
    length = textbox.value.length;
}

function ResponseHandler(result)
{ 
    var dropdown = $get(this.Localdropdown);
    var textbox = $get('txtLocations');
    
    dropdown.innerHTML = "";
    
    if(result.length > 0)
    {
        for(var item in result)
        {
            var div = document.createElement("div");
            div.innerHTML = result[item].DisplayDescription;
            div.id = item;
            //div.style.color = '#5971d3';
            div.className = 'dropdownItems';
            div.onclick = new Function("evt", "select('"+ item +"');");
            dropdown.appendChild(div);
        }
    }
    else
    {
            var div = document.createElement("div");
            div.innerHTML = "<p>Sorry! We've never heard of "+ this.textbox.value +".</p> <p>Please check the spelling or try a different place name or UK post code.</p>";
            div.id = 0;
            //div.style.color = '#5971d3';
            div.className = 'ErrorItem';
            //div.onclick = new Function("evt", "select('"+ item +"');");
            dropdown.appendChild(div);
    }
    
    var itemWidth = dropdown.clientWidth + 15;
    // The below messes up with width when you keep typing.
    //dropdown.style.width = ''+ itemWidth +'px'; //'200';
    
    if(dropdown.childNodes.length < 10)
    {
        var dropLength = dropdown.childNodes.length
        if(dropdown.length == 1)
        {
            dropLength = dropLength + 1;
        }
        dropdown.size = dropLength;
    }
    else
    {
        dropdown.size = 10;
    }
}

function select(index)
{
    var dropdown = $get(this.Localdropdown)
    var item = dropdown.childNodes[index];

    var outputString = decHTMLifEnc(item.innerHTML);

    this.textbox.value = outputString;  // decode this
    $get(this.Localdropdown).style.display = 'none';
    //$get('successImage').style.display = 'block';
    if(this.localVal == 'True')
    {
        __doPostBack('Test', 'true');
    }
}


//function SelectListItem(localVal)
// {
//    //alert(value);
//    var dropdown = $get(this.Localdropdown);
//    //var textbox = $get('txtLocations')
//    
//    var index = dropdown.selectedIndex;
//    this.textbox.value = dropdown.options[index].text;
//    dropdown.style.display = 'none';
//    $get('successImage').style.display = 'block';
//    if(localVal == 'True')
//    {
//        __doPostBack('Test', 'true');
//    }
//}

//function SelectListItemOnKeyPress(localVal, e)
//{
//    //alert(value);
//    var KeyID = (window.event) ? event.keyCode : e.keyCode;
//    
//    if(KeyID == 13)
//    {
//        var dropdown = $get(this.Localdropdown);
//        //var textbox = $get('txtLocations')
//        
//        var index = dropdown.selectedIndex;
//        this.textbox.value = dropdown.options[index].text;
//        dropdown.style.display = 'none';
//        $get('successImage').style.display = 'block';
//        if(localVal == 'True')
//        {
//            __doPostBack('Test', 'true');
//        }
//    }
//}
