/**
 *reload state list depend on country
 *reload area list depend on state
 *
 */


$(function(){
    $("#country_id").change(function()
    {
        $("#state_id").children().remove();
        $("#area_id").children().remove();
        
        $("#area_id").append('<option value="">Select City</option>');
        
        $.post(
        'app/register/get_state_list/',
        { 'country_id': $(this).val() },
        function(data){
           //alert(data);
           if(data == 'no_data')
            {
                $("#state_id").hide();
                $("#area_id").hide();
                $("#state_others").show();
                $("#area_others").show();
            }
            else
            {
                $("#state_id").show();
                $("#area_id").show();
                $("#state_others").hide();
                $("#area_others").hide();
                
                $("#state_id").append(data);
            }
        }, "html");
    });
    
    $("#state_id").change(function(){
        $("#area_id").children().remove();
        var url = 'app/register/get_area_list/';
        $.post(
               url,
               { 'state_id': $(this).val() },
                function(data){
                    if(data == 'no_data')
                    {
                        
                    }else
                    {
                        $("#area_id").append(data);
                    }
                }, "html");
    });
});

function setStateArea(country_id, state_id, state_others, area_id , area_others)
{
    if(country_id){
        var url = 'app/register/get_state_list/';
        $.post(
            url,
            { 'country_id': country_id , 'selected': state_id },
            function(data){
                if(data == 'no_data'){
                     $("#state_id").hide();
                     $("#area_id").hide();
                     $("#state_others").show();
                     $("#state_others").val(state_others) ;
                     $("#area_others").show();
                      $("#area_others").val(area_others)  ;
                }else{
                     $("#state_id").children().remove();
                     $("#area_id").children().remove();
                     
                     $("#state_id").show();
                     $("#area_id").show();
                     var url = 'app/register/get_area_list/';
                     $.post(
                     url,
                     { 'state_id': state_id, 'selected': area_id},
                      function(data){
                         //alert(data);
                          if(data == 'no_data'){
                              $("#area_id").hide();
                              $("#area_others").show();
                          }else{
                              $("#area_id").show();
                              $("#area_others").hide();
                              $("#area_id").append(data);
                          }
                      }, "html");
                     $("#state_others").hide();
                     $("#area_others").hide();
                     
                     $("#state_id").append(data);
                }
            },
        "html");
    }
}

