
function free_ebook_reg_form_submit( form_id, container_id, submit_button_id ){

  $('#'+submit_button_id).attr('disabled', true);

  $.post(
    '/free-ebook/reg-form-submit.php',
    $('#' + form_id).serialize(),
    function( response ){
      $('#' + container_id + '-alerts').html( response.msg );
      if( response.status > 0 )
      $('#' + container_id + '-html').html( null );
      else
      $('#'+submit_button_id).attr('disabled', false);
    },
    'json'
  );
}

function free_ebook_reg_form( book_id, container_id ){
  $.post(
    '/free-ebook/reg-form.php',
    {book_id: book_id, container_id: container_id},
    function( response ){

      $('#' + container_id ).dialog({
        modal:true,
        width:500,
        position:'top'
      });
      $('#' + container_id + '-alerts').html( null );
      $('#' + container_id + '-html').html( response );
      
    }
  );
}

function free_ebook_geo_state_update( country_element, state_element, state_selected ){

  country_code = $('#'+country_element).val();
  $.getJSON(
    '/geo/states-by-country-code.php?country_code=/'+country_code,
    function( response ){

      html = ''

      $('#'+state_element).html( null );
      $.each( response, function(key, value){
        html += '<option value="'+key+'"';
        if( state_selected == key )
        html += ' selected ';
        html += '>'+value+'</option>';
      });

      if( state_selected == null )
      state_selected = ''
      
      if( html == '' )
      field = "<input type='text' name='state' value='"+state_selected+"' id='"+state_element+"'>";
      else
      field = "<select name='state' id='"+state_element+"'>"+html+"</select>";

      $('#'+state_element).html( field );
    }
  );
}
