﻿function findValueProvince(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ){ 
	    var sValue = li.extra[0];
	    $("#txtThanhPhoID").val(sValue);
	       $("#txtQuan")[0].autocompleter.setExtraParams(
                // set the parameters to send
                {
                        matt: sValue
                }
           );
         $("#txtQuan").val('');
         $("#txtQuan").focus();
	}

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	//alert("The value you selected was: " + sValue);
	
}

function selectItemProvince(li) {
	findValueProvince(li);
}
////////////////////////////////////////////////////// use for District 
function findValueDistrict(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ){ 
	    var sValue = li.extra[0];
	    $("#txtQuanHuyenID").val(sValue);
	    $("#txtXa")[0].autocompleter.setExtraParams(
                // set the parameters to send
                {
                        maqu: sValue
                }
           );
        $("#txtXa").val('');
        $("#txtXa").focus();
	}

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	//alert("The value you selected was: " + sValue);
	
	//alert($("#txtQuanHuyenID").val()+"ddd");
}

function selectItemDistrict(li) {
	findValueDistrict(li);
}
///////////////////////////////////////////////////////////////////// phuong xa
function findValueXa(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ){ 
	    var sValue = li.extra[0];
	    $("#txtXaID").val(sValue);
	    $("#txtDiaChiChiTiet").focus();
//	    $("#txtXa")[0].autocompleter.setExtraParams(
//                // set the parameters to send
//                {
//                        maqu: sValue
//                }
//           );
	}

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	//alert("The value you selected was: " + sValue);
	
	//alert($("#txtQuanHuyenID").val()+"ddd");
}

function selectItemXa(li) {
	findValueXa(li);
}
/////////////////////////////////////////////////////////////////////
function formatItem(row) {
    
	return row[0] //+ " (id: " + row[1] + ")";
}



function lookupAjax(){
	var oSuggest = $("#TextLocation")[0].autocompleter;

	oSuggest.findValue();

	return false;
}



$(document).ready(function() {
    $('input').keypress(function (event){ return event.keyCode == 13 ? false : true; });

	$("#TextLocation").autocomplete(
		"AJAX_ThanhPho.aspx",
		{
			delay:10,
			minChars:1,
			matchSubset:1,
			matchContains:1,
			cacheLength:0,
			//onItemSelect:selectItemProvince,
			//onFindValue:findValueProvince,
			formatItem:formatItem,
			maxItemsToShow:20,
			autoFill:true,
			selectFirst: true

		}
	)
	
	
	
	$("#txtQuan").autocomplete(
		"Ajax_QuanHuyen.aspx",
		{
			delay:10,
			minChars:1,
			matchSubset:1,
			matchContains:1,
			cacheLength:0,
			onItemSelect:selectItemDistrict,
			onFindValue:findValueDistrict,
			formatItem:formatItem,
			maxItemsToShow:20,
			autoFill:true,
			extraParams:{//matt:101
			    matt:$("#txtThanhPhoID").val()
//			    matt: function(){
//			        return '101';}//$('input[name=txtThanhPhoID]').html();}
			}
		}
	)
	
	$("#txtXa").autocomplete(
		"Ajax_XaPhuong.aspx",
		{
			delay:10,
			minChars:1,
			matchSubset:1,
			matchContains:1,
			cacheLength:0,
			onItemSelect:selectItemXa,
			onFindValue:findValueXa,
			formatItem:formatItem,
			maxItemsToShow:20,
			autoFill:true,
			extraParams:{
			    maqu:$("#txtQuanHuyenID").val()
			}
		}
	)
	
	 $("#txtQuan").click(function(){
        //alert($("#txtThanhPhoID").val()+"asd");
    });
});


