var idComboCidadeVizinha;

function AtivarCidadeText(txtCidade, cboEstado) 
{
    var comboEstado = document.getElementById(cboEstado);

    if (comboEstado.value != null && comboEstado.value != '')
        document.getElementById(txtCidade).disabled = false;
    else
        document.getElementById(txtCidade).disabled = true;
}

function VerificarCheckCidadeVizinha(chk,cbo,cboEstado,txtCidade) 
{	
	idComboCidadeVizinha = cbo;
	
	var chkbox = document.getElementById(chk); 
	var cboBox = document.getElementById(cbo);
	var comboEstado = document.getElementById(cboEstado);
	var textoCidade = document.getElementById(txtCidade);
	
	if(chkbox != null && cboBox != null)
	{	
		if (comboEstado.value != null &&
			comboEstado.value != '' &&
			textoCidade.value != null &&
			textoCidade.value.trim() != '')
		{
			if(chkbox.checked) 
			{
				cboBox.disabled  = false;
				WebMotors.ssComum.ssServicos.InformacaoCidades.FindCidadeVizinhas(comboEstado.value, textoCidade.value, FindCidadeVizinhas_OnComplete, FindCidadeVizinhas_OnError, FindCidadeVizinhas_OnTimeOut);
			}
			else
			{   
				limpaCombo(idComboCidadeVizinha);
				cboBox.disabled  = true;
			}
		}
		else
		{
			limpaCombo(idComboCidadeVizinha);
			chkbox.checked = false;
			document.getElementById(cbo).selectedIndex = 0;
			cboBox.disabled  = true;
		}
	}
}

function AtualizarUF(controleAutoComplete, valor)
{
	$find(controleAutoComplete).set_contextKey(valor);
}	

function FindCidadeVizinhas_OnComplete(list)
{
	if (list)
	{
	    limpaCombo(idComboCidadeVizinha);
		var e = document.getElementById(idComboCidadeVizinha);
        var selectedValueOption = null;
		for (i = 1; i <= list.length; i++)
		{
		    var item = list[i-1];
            var listItemName = item.name;
            var listItemValue = item.value;
            var listItemIsDefaultValue = item.isDefaultValue;
            var optionElement = new Option(listItemName, listItemValue);
            if (listItemIsDefaultValue) 
                selectedValueOption = optionElement;

            e.options[e.options.length] = optionElement;
        }
        if (selectedValueOption)
            selectedValueOption.selected = true;
	}
	else
		alert(msg);
}

function FindCidadeVizinhas_OnTimeOut(arg)
{
	alert("Ocorreu Timeout na busca de marcas no servidor.");
}

function FindCidadeVizinhas_OnError(arg)
{
	alert("Ocorreu erro na busca de marcas no servidor: " + arg._message);
}
function LimparCidade(txtBox)
{
	document.getElementById(txtBox).value = '';
}

function limpaCombo(combo)
{
	if (document.getElementById(combo) != null)
		document.getElementById(combo).options.length = 0;
}