/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function split( val ) {
    return val.split(" / ");
}
function updateUI() {
    var locSpanCount = $(".multiLocationSpan").length;
    //alert();
    if (locSpanCount>=4) {
        $("#LocationTxt").hide();
    } else {
        $("#LocationTxt").show();
    }
}
function addValueToSelectedLocations(newValue) {
    var initialValue = $("#SearchForm_LocationID").val();
    var locIds = new Array();
    if(initialValue!=0)
        locIds = initialValue.split(",");
    locIds.push(newValue);
    $("#SearchForm_LocationID").val(locIds.join(","));
}
function removeValueFromSelectedLocations(newValue) {
    var initialValue = $("#SearchForm_LocationID").val();
    var locIds = new Array();

    if(initialValue!=0)
        locIds = initialValue.split(",");

    locIds.pop(newValue);

    if(locIds.length==0) {
        $("#SearchForm_LocationID").val(0);
    } else {
        $("#SearchForm_LocationID").val(locIds.join(","));
    }
}            

function getSelectedLocations() {
    return $("#SearchForm_LocationID").val();
}
function extractLast( term ) {
    return split( term ).pop();
}

$(".autocompleteContainer").live("click", function() {
    $("#LocationTxt").val("");
    $("#LocationTxt").focus();
});

$(".multiLocationSpan span").live("click", function() {
    // Live handler called.
    $(this).parent().remove();
    var locId= $(this).parent().attr("id").substr(10);
    removeValueFromSelectedLocations(locId);
    updateUI();
});

