//TODO О_о беспорядок переписать.

$(document).ready(function(){

  // выбор города
  function adjustSity(end){
    var regionValue = $('#region' + end).val();
  	var tmpSelect = $('#sity' + end);
  	if(regionValue.length == 0) {
  		tmpSelect.attr('disabled','disabled');
  		tmpSelect.clearSelect();
  	} else {
      $.getJSON('/includes/functions/ajax_cascade_select_sity.php',{region:regionValue},function(data) { tmpSelect.fillSelect(data).attr('disabled','');  });
    }

  };

  $('#region').change(function(){adjustSity('');});
  $('#region_2').change(function(){adjustSity('_2');});


function changeSityform(){
  	var regionValue = $('#change_region').val();
  	var tmpSelect = $('#change_sity');
  	if(regionValue.length == 0) {
  		tmpSelect.attr('disabled','disabled');
  		tmpSelect.clearSelect();
  	} else {
      $.getJSON('/includes/functions/ajax_cascade_select_sity.php',{region:regionValue},function(data) { tmpSelect.fillSelect(data).attr('disabled','');  });
    }

  };

  $('#change_region').change(changeSityform);
  $('#change_sity').change(function () {var tmpsity = $('#change_sity').val();
    $('#sity_form').attr("action","/change_my_sity/" + tmpsity);});


//  function changeSityform(){
//    var tmpsity = $('#sity').val();
//    $('#sity_form').attr("action","/change_my_sity/" + tmpsity);
//  }
//  $('.changesityform').change(changeSityform);
// $('#sity').change(function(){
//  	if($(this).val().length != 0) { alert('Выбор сделан!'); }
//  });


 // выбор профессии
  function adjustProfession(end){
  	var sectionValue = $('#section' + end).val();
  	var tmpSelect = $('#profession' + end);
  	if(sectionValue.length == 0) {
  		tmpSelect.attr('disabled','disabled');
  		tmpSelect.clearSelect();
  	} else {
      tmpSelect.attr('disabled','');
      $.getJSON('/includes/functions/ajax_cascade_select_profession.php',{section:sectionValue},function(data) { tmpSelect.fillSelect(data).attr('disabled','');  });
    }

  };
  $('#section').change(function(){adjustProfession('');});
  $('#section_2').change(function(){adjustProfession('_2');});

    function adjustProfession2(){
  	var sectionValue = $('#section_resume').val();
  	var tmpSelect = $('#profession_resume');
  	if(sectionValue.length == 0) {
  		tmpSelect.attr('disabled','disabled');
  		tmpSelect.clearSelect();
  	} else {
      tmpSelect.attr('disabled','');
      $.getJSON('/includes/functions/ajax_cascade_select_profession.php',{section:sectionValue},function(data) { tmpSelect.fillSelect(data).attr('disabled','');  });
    }

  };
  $('#section_resume').change(adjustProfession2);
});