/**
 * Address JS osztály
 * @param blockId
 * @param blockUrl
 */

function addressClass(blockId, blockUrl){

   this.blockId = blockId;
   this.blockUrl = blockUrl;

   this.getCities = function(id, id2, selected){

      if (!id2) id2 = id;

      $("select#"+id+'_city').attr('disabled',true);
      $("select#"+id+'_zip').html('<option value="">Válasszon irányítószámot!</option>');
      $("select#"+id+'_zip').attr('disabled',true);

//     if ($('#'+id+'_country').val().length == 0) return false;


//      $("select#"+id+'_country').attr('disabled',true);

      $.getJSON(
          this.blockUrl,
         {
            ajax: this.blockId,
            action: 'getCities',
            country_id: 1 //$("select#"+id2+'_country').val()
         },
         function(j){
//            $("select#"+id+'_country').removeAttr('disabled');
            $("select#"+id+'_city').removeAttr('disabled');
            $("select#"+id+'_city').html('');

            var options = '<option value="">Válasszon!</option>';
            for (var i=0; i<j.length; i++) {
               options += '<option value="' + j[i].aci_name + '">' + j[i].aci_name + '<\/option>';
            }

            $("select#"+id+'_city').html(options);
            if (selected) $("select#invoice_city option[value='"+selected+"']").attr("selected", "selected");

         }
      );
   }

   this.getZips = function (id, id2, selected){

      if (!id2) id2 = id;
      //$("select#"+id+'_city').attr('disabled',true);
      //$("select#"+id+'_zip').attr('disabled',true);
      if ($('#'+id+'_city').val().length == 0) return false;

      $.getJSON(
         this.blockUrl,
         {
          ajax:      this.blockId,
          action:    'getZips',
          city_name: $("select#"+id2+'_city').val()
         },
         function(j) {
            $("select#"+id+'_city').removeAttr('disabled');
            $("select#"+id+'_zip').removeAttr('disabled');

            if(j.length > 0){
               $("select#"+id+'_zip').html('');

               var options = '<option value="">Válasszon!</option>';
               for (var i = 0; i < j.length; i++) {
                  options += '<option value="' + j[i].aci_id + '">' + j[i].aci_zip + '<\/option>';
               }
            }
            else{
               var options = '<option>Nincs irányítószám!</option>';
            }

            $("select#"+id+'_zip').html(options);
            if (selected) $("select#invoice_zip option[value='"+selected+"']").attr("selected", "selected");

         }
      );
   }

   this.sameAddr = function (from, to){
        $("select#"+to+"_country option[value='"+$("select#"+from+"_country").val()+"']").attr("selected","selected");

        $("select#"+to+"_city").attr('disabled',false);
        address.getCities(to, from, $("select#"+from+"_city").val());

        $("select#"+to+"_zip").attr('disabled',false);
        address.getZips(to, from, $("select#"+from+"_zip").val());

        $("select#"+to+"_street_type_id option[value='"+$("select#"+from+"_street_type_id").val()+"']").attr("selected","selected");

        $("[ name='"+to+"[street]' ]").val( $("[ name='"+from+"[street]' ]").val() );
        $("[ name='"+to+"[number]' ]").val( $("[ name='"+from+"[number]' ]").val() );
        $("[ name='"+to+"[level]' ]").val( $("[ name='"+from+"[level]' ]").val() );
        $("[ name='"+to+"[door]' ]").val( $("[ name='"+from+"[door]' ]").val() );

        var invoiceName = $("#last_name").val() + " " + $("#first_name").val();
        $("#"+to+"_name").val(invoiceName);
   }

}




