
    function ajaxCall() {
        this.send = function(data, url, method, success, type) {
          type = type||'json';
          var successRes = function(data) {
              success(data);
          }

          var errorRes = function(e) {
              console.log(e);
              alert("Error found \nError Code: "+e.status+" \nError Message: "+e.statusText);
          }
            $.ajax({
                url: url,
                type: method,
                data: data,
                success: successRes,
                error: errorRes,
                dataType: type,
                timeout: 60000
            });

          }

        }
function loadcountry()
{
    var country = $('#country').val();
    $(".countries option").each(function(){
        if($(this).val()==country){ // EDITED THIS LINE
            $(this).attr("selected","selected"); 
            var loc = new locationInfo();
             loc.getStates(country);
        }
    });
}
function loadcountry1()
{
    var country = $('#country1').val();
    if(country!="")
    {
    $(".countries1 option").each(function(){
        if($(this).val()==country){ // EDITED THIS LINE
            $(this).attr("selected","selected"); 
            var loc = new locationInfo1();
             loc.getStates(country);
        }
    });
   }
}
function loadstate()
{
    var state = $('#state').val();
    var items = state.split(',');
    for(var i=0;i<items.length;i++){
        
        if(items.length>0)
        {
        $(".states option").each(function(){
            if($(this).val()==items[i]){ // EDITED THIS LINE
                $(this).attr("selected","selected");    
            }
        });
       }
    }
     
}
function loadstate1()
{
    var state = $('#state1').val();
    $(".states1 option").each(function(){
        if($(this).val()==state){ // EDITED THIS LINE
            $(this).attr("selected","selected");    
        }
    });
}

function locationInfo() {
    var rootUrl = "http://lab.iamrohit.in/php_ajax_country_state_city_dropdown/api.php";
    var call = new ajaxCall();
 


    this.getCountries = function() {
        var url = rootUrl+'?type=getCountries';
        var method = "post";
        var data = {};
        $('.countries').find("option:eq(0)").html("Please wait..");
        call.send(data, url, method, function(data) {
            $('.countries').find("option:eq(0)").html("Select Country");
            console.log(data);
            if(data.tp == 1){
                $.each(data['result'], function(key, val) {
                    var option = $('<option />');
                    option.attr('value', key).text(val);
                    $('.countries').append(option);
                });
                $(".countries").prop("disabled",false);
                
                loadcountry();//this is for display selected country for job edit page
            }
            else{
                alert(data.msg);
            }
        }); 
    };
    
  
    this.getStates = function(id) {
        $(".states option:gt(0)").remove(); 
        $(".cities option:gt(0)").remove(); 
        var url = rootUrl+'?type=getStates&countryId=' + id;
        var method = "post";
        var data = {};
        $('.states').find("option:eq(0)").html("Please wait..");
        call.send(data, url, method, function(data) {
            $('.states').find("option:eq(0)").html("Select State");
            if(data.tp == 1){
                $.each(data['result'], function(key, val) {
                    var option = $('<option />');
                    option.attr('value', key).text(val.replace(/['"]/g, ''));
                    $('.states').append(option);
                });
                $(".states").prop("disabled",false);
                
                loadstate();//this is for display selected state for job edit page
            }
            else{
                alert(data.msg);
            }
        }); 
    };

}

$(function() {
var loc = new locationInfo();
loc.getCountries();
 $(".countries").on("change", function(ev) {
        var countryId = $(this).val()
        if(countryId != ''){
        loc.getStates(countryId);
        }
        else{
            $(".states option:gt(0)").remove();
        }
    });
    
    

});

function locationInfo1() {
    var rootUrl = "http://lab.iamrohit.in/php_ajax_country_state_city_dropdown/api.php";
    var call = new ajaxCall();
 

    this.getStates = function(id) {
        $(".states1 option:gt(0)").remove(); 
        $(".cities option:gt(0)").remove(); 
        var url = rootUrl+'?type=getStates&countryId=' + id;
        var method = "post";
        var data = {};
        $('.states1').find("option:eq(0)").html("Please wait..");
        call.send(data, url, method, function(data) {
            $('.states1').find("option:eq(0)").html("Select State");
            if(data.tp == 1){
                $.each(data['result'], function(key, val) {
                    var option = $('<option />');
                    option.attr('value', key).text(val.replace(/['"]/g, ''));
                    $('.states1').append(option);
                });
                $(".states1").prop("disabled",false);
                 loadstate1();//this is for display selected state for job edit page
            }
            else{
                alert(data.msg);
            }
        }); 
    };

    this.getCountries = function() {
        var url = rootUrl+'?type=getCountries';
        var method = "post";
        var data = {};
        $('.countries1').find("option:eq(0)").html("Please wait..");
        call.send(data, url, method, function(data) {
            $('.countries1').find("option:eq(0)").html("Select Country");
            console.log(data);
            if(data.tp == 1){
                $.each(data['result'], function(key, val) {
                    var option = $('<option />');
                    option.attr('value', key).text(val);
                    $('.countries1').append(option);
                });
                $(".countries1").prop("disabled",false);
                 loadcountry1();//this is for display selected country for job edit page
            }
            else{
                alert(data.msg);
            }
        }); 
    };
    
    

}

$(function() {
var loc = new locationInfo1();
loc.getCountries();
 $(".countries1").on("change", function(ev) {
        var countryId = $(this).val()
        if(countryId != ''){
        loc.getStates(countryId);
        }
        else{
            $(".states1 option:gt(0)").remove();
        }
    });

});


