
function populateLocations(inputId, callbacks)
{
    var input = $('#' + inputId)[0];
    
    $.ajax({
        'dataType' : 'json',
        'error' : function(_, status) {
            if(callbacks.error) {
                callbacks.error({'errorMessage' : 'An error occurred'});
            }
        },
        'success' : function(data) {
            if(data.errorMessage) {
                if(callbacks.error) {
                    callbacks.error(data);
                }
            } else {
                if(callbacks.pre) {
                    callbacks.pre(data);
                }
                $.each(data.results, function(_, item) {
                    if(callbacks.each) {
                        callbacks.each(item);
                    }
                });
                if(callbacks.post) {
                    callbacks.post(data);
                }
            }
        },
        'url' : '/knowwhere/QueryLocator.aspx?query=' + input.value
    });
}

var blinkId;
var resultCount;

function startBlink()
{
    var opacity = 0;
    var deltaOp = 0.1;
    var statusTxt = $('#statusTxt')[0];
    
    statusTxt.style.opacity = 0;
    statusTxt.style.color = 'white';
    statusTxt.innerHTML = 'Loading...';
    
    blinkId = setInterval(function() {
        opacity += deltaOp;
        
        if(opacity >= 1.0) {
            opacity = 1.0;
            deltaOp = -deltaOp;
        } else if(opacity <= 0.0) {
            opacity = 0.0;
            deltaOp = -deltaOp;
        }
        
        statusTxt.style.opacity = opacity;
    }, 50);
}

function stopBlink()
{
    clearInterval(blinkId);
    $('#statusTxt')[0].innerHTML = '';
    $('#statusTxt')[0].style.opacity = '1.0';
}

function popLocSelect(result)
{
    resultCount++;

    var locationsSelect = $('#locationsSelect')[0];
    var opt = document.createElement('option');
    opt.value = result.KEY;
    opt.innerHTML = result.NAME;
    locationsSelect.appendChild(opt);
}

function hideAndClearLocations()
{
    $('#locationsSelect')[0].style.visibility = 'hidden';
    $('#locationsSelect')[0].innerHTML = '';
    $('#errorCities')[0].style.display = 'none';
    $('#statusTxt')[0].innerHTML = '';
}

function showLocations()
{
    stopBlink();
    if(resultCount > 0) {
	$('#locationsSelect')[0].style.visibility = 'visible';
    } else {
	var statusTxt = $('#statusTxt')[0];
	statusTxt.style.color = 'red';
	statusTxt.innerHTML = 'No results found';
    }
    return resultCount;
}

function refillLocationInput(ev)
{
    //START ge03052009
    //Giancarlo Espinosa: Added to fix IE null pointer error.
    if(navigator.appName == 'Microsoft Internet Explorer'){
    var ev = window.event;
    }
    //END ge03052009
    var target = ev.target || ev.srcElement;
    $('#locatorInput')[0].value = target.innerHTML;
    locatorCallback();
}
			
function locateError(data)
{
    stopBlink();
    var statusTxt = $('#statusTxt')[0];
    
    if(data.matches) {
        var errorCities = $('#errorCities')[0];
        errorCities.innerHTML = '';
        $.each(data.matches, function(_, match) {
            var li = document.createElement('li');
            var anchor = document.createElement('a');

            anchor.innerHTML = match;
            anchor.href = 'javascript:void(0)';
            anchor.onclick = refillLocationInput;
            li.appendChild(anchor);
            errorCities.appendChild(li);
        });
        errorCities.style.display = 'block';
    }
    
    if(data.results) {
    
        $.each(data.results, function(_, item) {
            popLocSelect(item);
        });  
        showLocations();
    }
    statusTxt.style.color = 'red';
    statusTxt.innerHTML = data.errorMessage;
}

function clearResultCount()
{
    resultCount = 0;
}

function locatorCallback()
{
    hideAndClearLocations();
    startBlink();
    //START ge03032009
    //Giancarlo Espinosa: Added per April's request
    //March 3, 2009
    
    var opt = document.createElement('option');
    opt.value = '';
    opt.innerHTML = '-PLEASE SELECT YOUR STORE-';
    $('#locationsSelect')[0].appendChild(opt);
    $('#locationsSelect')[0].selectedIndex = 0;
    
    //END ge03032009
    populateLocations('locatorInput', {
	'pre' : clearResultCount,
        'each' : popLocSelect,
        'post' : showLocations,
        'error' : locateError
    });
}

//START ge03032009

//Giancarlo Espinosa: Added per April's request to clear field

// March 3, 2009

function clearField(field) {

    field.value = "";

}

//END ge03032009