﻿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];
	    $("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtThanhPhoID").val(sValue);
	       $("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtQuan")[0].autocompleter.setExtraParams(
                // set the parameters to send
                {
                        matt: sValue
                }
           );
         $("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtQuan")[0].autocompleter.flushCache();

         $("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtQuan").val('');
         $("#ctl00_ContentPlaceHolder1_frvCharityRegister_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];
	    $("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtQuanHuyenID").val(sValue);
//	    $("#txtXa")[0].autocompleter.setExtraParams(
//                // set the parameters to send
//                {
//                        maqu: sValue
//                }
//           );
//        $("#txtXa")[0].autocompleter.flushCache();
//        $("#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);
	    $("#txtThonPho").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 = $("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtThanhPho")[0].autocompleter;

	oSuggest.findValue();
	
//	var oSuggest1 = $("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptProvince")[0].autocompleter;

//	oSuggest1.findValue();

	return false;
}

//helper for Tim theo ho ten autocomplete
    function findValueHoTen(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];
            $("#txtMaBN").val(sValue);  
            $("#bmabn").click();                   
        }

    }

    function selectItemHoTen(li) {
        findValueHoTen(li);
    }
    function formatItemHoTen(row) {
        
        return row[0] + " - " + row[1] + " - " + row[2];
    }
//end heper tim theo ho ten

$(document).ready(function() {
    $('input').keypress(function (event){ return event.keyCode == 13 ? false : true; });

	$("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtThanhPho").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

		}
	)
	
	$("#ctl00_ContentPlaceHolder1_frvCharityRegister_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:$("#ctl00_ContentPlaceHolder1_frvCharityRegister_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()
			}
		}
	)
	
	//autocomplete tim theo ho ten
            $("#txtHoTenTimBN").autocomplete(
	            "Ajax_TimTheoHoTen.aspx",
	            {
		            delay:10,
		            minChars:3,
		            matchSubset:1,
		            matchContains:1,
		            cacheLength:1,
		            onItemSelect:selectItemHoTen,
		            onFindValue:findValueHoTen,
		            formatItem:formatItemHoTen,
		            maxItemsToShow:20,
		            autoFill:true,
		            selectFirst: true
	            }
	        )
           //end autocomplete tim theo ho ten 
  //////////////////////////////////////////
  //autocomplete noi nhan ung ho
  function findValueReceiptProvince(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];
	    $("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptProvinceID").val(sValue);
	       $("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptDistrict")[0].autocompleter.setExtraParams(
                // set the parameters to send
                {
                        matt: sValue
                }
           );
         $("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptDistrict")[0].autocompleter.flushCache();

         $("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptDistrict").val('');
         $("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptDistrict").focus();
         var districtId = 0 ;
         var provinceId = $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptProvinceID').val() ;
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data: "{ _provinceId: '" + provinceId + "',_districtId: '" + districtId + "'}",
                url: "VnrcWebService.asmx/getInfo",
                dataType: "json",
                success: function(data) {
                  var Company = data;
                    if(Company.Address!="")
                    {
                        $('#CentralAddr').html("Địa Chỉ: " + Company.Address + "<br />Điện Thoại: " + Company.PhoneNumber + "<br />Tải Khoản Ngân Hàng: " + Company.AccNumber);
                        $('#ctl00_ContentPlaceHolder1_frvCharityRegister_InsertButton').removeAttr('disabled');
                        $('#ctl00_ContentPlaceHolder1_frvCharityRegister_InsertButton').css("display", "block");

                    }
                    else{
                       $('#CentralAddr').html('Chưa có thông tin về chi hội này. Bạn không thể gửi đăng ký.');
                       $('#ctl00_ContentPlaceHolder1_frvCharityRegister_InsertButton').css("display", "none");
                    }
                }        
             });//END AJAX LAY MADOTKHAM
	}

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	//alert("The value you selected was: " + sValue);
	
}

function selectItemReceiptProvince(li) {
	findValueReceiptProvince(li);
}
////////////////////////////////////////////////////// use for District 
function findValueReceiptDistrict(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];
	    $("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptDistrictID").val(sValue);
        
        var districtId = $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptDistrictID').val() ;
        var provinceId = $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptProvinceID').val() ;
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data: "{ _provinceId: '" + provinceId + "',_districtId: '" + districtId + "'}",
                url: "VnrcWebService.asmx/getInfo",
                dataType: "json",
                success: function(data) {
                  var Company = data;
                    if(Company.Address!="")
                    {
                        $('#CentralAddr').html("Địa Chỉ: " + Company.Address + "<br />Điện Thoại: " + Company.PhoneNumber + "<br />Tải Khoản Ngân Hàng: " + Company.AccNumber);
                        //$('#ctl00_ContentPlaceHolder1_frvCharityRegister_InsertButton').removeAttr('disabled');
                         $('#ctl00_ContentPlaceHolder1_frvCharityRegister_InsertButton').css("display", "block");

                    }
                    else{
                       $('#CentralAddr').html('Chưa có thông tin về chi hội này. Bạn không thể gửi đăng ký');
                       //$('#ctl00_ContentPlaceHolder1_frvCharityRegister_InsertButton').attr('disabled', 'disabled'); 
                       $('#ctl00_ContentPlaceHolder1_frvCharityRegister_InsertButton').css("display", "none");
                    }
                }        
             });//END AJAX LAY MADOTKHAM
	}

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;
}

function selectItemReceiptDistrict(li) {
	findValueReceiptDistrict(li);
}
$("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptProvince").autocomplete(
		"Ajax_ThanhPho.aspx",
		{
			delay:10,
			minChars:1,
			matchSubset:1,
			matchContains:1,
			cacheLength:0,
			onItemSelect:selectItemReceiptProvince,
			onFindValue:findValueReceiptProvince,
			formatItem:formatItem,
			maxItemsToShow:20,
			autoFill:true,
			selectFirst: true

		}
	)
	
	$("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptDistrict").autocomplete(
		"Ajax_QuanHuyen.aspx",
		{
			delay:10,
			minChars:1,
			matchSubset:1,
			matchContains:1,
			cacheLength:0,
			onItemSelect:selectItemReceiptDistrict,
			onFindValue:findValueReceiptDistrict,
			formatItem:formatItem,
			maxItemsToShow:20,
			autoFill:true,
			extraParams:{//matt:101
			    matt:$("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptProvince").val()
//			    matt: function(){
//			        return '101';}//$('input[name=txtThanhPhoID]').html();}
			}
		}
	)
	
    $('#ctl00_ContentPlaceHolder1_frvCharityRegister_radDistrict').click(function(){
            if($(this).attr("checked")==true)
            {
                $("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptProvince").removeAttr('disabled');
                $("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptDistrict").removeAttr('disabled'); 
                $("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptProvince").focus(); 
                $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptDistrict').val('');
                $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptDistrictID').val('');
                $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptProvince').val('');
                $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptProvinceID').val('');        
                $('#CentralAddr').html('');
                $('#required').html('*');
                $('#required1').html('*');
            }
            else
            {
               alert('000');
//                $('#txtbh4').removeAttr('disabled'); 
//                 $('#txtbh4').focus();
//                $('#txtDoiTuong').attr('disabled', 'disabled'); 
            }
               
   });
   $('#ctl00_ContentPlaceHolder1_frvCharityRegister_radProvince').click(function(){
            if($(this).attr("checked")==true)
            {
                $('#CentralAddr').html('');
                $("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptProvince").removeAttr('disabled'); 
                $("#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptProvince").focus();         
                $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptDistrict').attr('disabled', 'disabled'); 
                $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptDistrict').val('');
                $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptDistrictID').val('');
                $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptProvince').val('');
                $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptProvinceID').val('');
                $('#required').html('*');
                $('#required1').html('');
            }  
   });
   $('#ctl00_ContentPlaceHolder1_frvCharityRegister_radCentral').click(function(){
            if($(this).attr("checked")==true)
            {
                $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptProvince').val('');
                $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptDistrict').val('');
                $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptProvinceID').val('');
                $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptDistrictID').val('');
                $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptProvince').attr('disabled', 'disabled');      
                $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptDistrict').attr('disabled', 'disabled'); 
                $('#ctl00_ContentPlaceHolder1_frvCharityRegister_FullNameTextBox').focus();
//                $('#ctl00_ContentPlaceHolder1_frvCharityRegister_charityNameTextBox').focus()
                $('#required').html('');
                $('#required1').html('');
            }  
            $('#CentralAddr').html('HỘI CHỮ THẬP ĐỎ VIỆT NAM Địa chỉ: 82 Nguyễn Du, Hà Nội. Điện thoại: (844)8224030 - 8263703 - Fax: (844)9424285. Email: vnrchq@netnam.org.vn Tên tài khoản: TRUNG ƯƠNG HỘI CHỮ THẬP ĐỎ VIỆT NAM; Số tài khoản: 12010000043522 VNĐ, 12010370036438 USD Tại: Sở Giao dịch I - Ngân hàng Đầu tư và phát triển Việt Nam ');
   });
   
   $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptProvinceID').blur(function(){
//            var districtId = $('#ctl00_ContentPlaceHolder1_frvCharityRegister_txtReceiptDistrictID').val() ;
//            $.ajax({
//                type: "POST",
//                contentType: "application/json; charset=utf-8",
//                data: "{ _provinceId: '" + $(this).val() + "',_districtId: '" + districtId + "'}",
//                url: "VnrcWebService.asmx/getInfo",
//                dataType: "json",
//                success: function(data) {
//                  var Company = data;
//                    if(Company.Address!="")
//                    {
//                        $('#CentralAddr').html(Company.Address);
//                        
//                    }
//                    else
//                       $('#CentralAddr').html('Chưa có thông tin về chi hội này');
//                }        
//             });//END AJAX LAY MADOTKHAM
        });
   
   
  //end noi nhan ung ho
});

