function getModels(category_id, default_model, search)
{
	var ajax_script = '/all/models_ajax.html';
	if(search != true) search=false;
	var brand_id = $('#brandselect').val();
	if(!$('#modelselect')) return;

	$('#modelselect').attr('disabled','disabled');
	$('#modelselect').empty();
	if(brand_id <= 0) { // бренд НЕ выбран
		$('#modelselect').append('<option>-</option>');
		return;
	}
	
	$('#modelselect').append('<option>Загрузка данных...</option>');

	$.ajax({
		type: "POST",
		url: ajax_script,
		data: {action: 'get_models',brand_id: brand_id, category_id: category_id},
		dataType: "json",
		success: function(opt) {
			var tmp_echo = '';
			if(search == true)
				tmp_echo += '<option value="">Не важно</option>';
			else
				tmp_echo += '<option value="">Выберите модель</option>';

			if ($(opt).length)
				$(opt).each(function(){
					tmp_echo += '<option value="'+this.id+'">'+this.name+'</option>';
					if ($('#message_title').length) $('#message_title').remove();
				});
			else if (search == false) {
				tmp_echo = '<option value="">Модели не найдены. Заполните заголовок объявления</option>'
				if (!$('#message_title').length)
					$('#add_message_table').append('<tr id="message_title">'
												+'<td>Заголовок объявления</td>'
												+'<td><input type="text" name="title" style="width:165px;" value="" /></td>'
												+'</tr>');
			}
			$('#modelselect').empty();
			$('#modelselect').html(tmp_echo);
			$('#modelselect').removeAttr('disabled');
			if (default_model > 0)	$('#modelselect > option[value='+default_model+']').attr('selected','selected');
			else $('#modelselect > option:first-child').attr('selected','selected');
		}
		});
}

function get_defaults_val(category_id){
	var ajax_script = '/all/models_ajax.html';
	var model_id = $('#modelselect').val();
	if (model_id <= 0) return;
	 $.ajax({
		type: "POST",
		url: ajax_script,
		data: {action: 'get_default_values',model_id: model_id, category_id: category_id},
		dataType: "json",
		success: function(def_val) {
				$(def_val).each(function(){
					var ft = this.field_type;
					if (ft == 'select') {
						if($('#fields_'+this.id)) $('#fields_'+this.id+' > option[value="'+this.val_id+'"]').attr('selected','selected');
					} else if (ft == 'integer' || ft == 'float') {
						if($('#fields_'+this.id)) $('#fields_'+this.id).val(this.dival);
					} else if (ft == 'boolean') {
						if($('#fields_'+this.id)) { if (this.dival) $('#fields_'+this.id).attr('checked','checked'); else $('#fields_'+this.id).removeAttr('checked'); }
					}
			});
			
		}
		});
}

/*
function getModels(category_id, default_model, search)
{
  if(search != true) search=false;
  var brand_id = getElement('brandselect').value;
  var select = getElement('modelselect');
  if(!select) return;
  
  select.disabled=true;
  select.options.length = 0;
  if(brand_id <= 0) { // бренд НЕ выбран 
    select.options[0] = new Option('-', '', false, false);                                        
    return;
  }
  select.options[0] = new Option('Загрузка данных...', '-1', false, false);                                      
  
  var req = new Subsys_JsHttpRequest_Js();  
    
  req.onreadystatechange = function() {  
    if (req.readyState == 4) {          
      if (req.responseJS) {
        if(req.responseJS.models || req.responseJS.data_sended == 'Y') {
          var select = getElement('modelselect');
          select.disabled=false;          
          select.options.length = 0;
          
          if(search == true) 
            select.options[select.options.length] = new Option('Не важно', '0', false, false);
          
          var arr = req.responseJS.models;                      
          for(var i in arr){
            select.options[select.options.length] = new Option(arr[i][1], arr[i][0], false, false);                                      
          }                  
          if(search == false)                                
            select.options[select.options.length] = new Option('Другое', '0', false, false);
          if(default_model >= 0) select.value = default_model;
        }        
      }                
    }
  }    
  
  req.caching = true;
  req.open('POST', '/all/models_js.html', true);
  req.send({ brand_id: brand_id, category_id: category_id});
}
*/

function getMoreModels(category_id, default_model, brandselect, modelselect, search)
{
  if(search != true) search=false;
  var brand_id = getElement(brandselect).value;
  var select = getElement(modelselect);
  if(!select) return;
  
  select.disabled=true;
  select.options.length = 0;
  if(brand_id <= 0) { // бренд НЕ выбран 
    select.options[0] = new Option('-', '', false, false);                                        
    return;
  }
  select.options[0] = new Option('Загрузка данных...', '-1', false, false);                                      
  
  var req = new Subsys_JsHttpRequest_Js();  
    
  req.onreadystatechange = function() {  
    if (req.readyState == 4) {          
      if (req.responseJS) {
        if(req.responseJS.models || req.responseJS.data_sended == 'Y') {
          var select = getElement(modelselect);
          select.disabled=false;          
          select.options.length = 0;
          
          if(search == true) 
            select.options[select.options.length] = new Option('Не важно', '0', false, false);
          
          var arr = req.responseJS.models;                      
          for(var i in arr){
            select.options[select.options.length] = new Option(arr[i][1], arr[i][0], false, false);                                      
          }                  
          if(search == false)                                
            select.options[select.options.length] = new Option('Другое', '0', false, false);
          if(default_model >= 0) select.value = default_model;
        }        
      }                
    }
  }    
  
  req.caching = true;
  req.open('POST', '/all/models_js.html', true);
  req.send({ brand_id: brand_id, category_id: category_id});
}


function hideRegistrationElements() 
{
  getElement('but_check').disabled = false;
  showElement('div_wait', false);
  showElement('div_password', false);
  showElement('div_userdata', false);
}


function checkEmail()
{
  var email = getElement('email').value;
  if(email == '') return;  

	// проверка на правильность
	if (! (/^\w+[\-_\.]*\w+@[\w\.\-_]+\.[a-z]{2,4}$/.test(email)) )
  {
    alert("Введите правильный e-mail адрес");
    return false;
  }
  hideRegistrationElements();
  getElement('but_check').disabled = true;
  showElement('div_wait', true);
    
  getElement('reg_email').value=email;
  getElement('hidden_login_email').value=email;  
  
  var req = new Subsys_JsHttpRequest_Js();  
    
  req.onreadystatechange = function() {  
    if (req.readyState == 4) {          
      if (req.responseJS) {
        if(req.responseJS.email_unique) {
          if(req.responseJS.email_unique == 'N') {
            getElement('td_email').innerHTML = '<b>'+email+'</b>';          
            showElement('div_userdata', true);
          }
          else {          
            showElement('div_password', true);                       
            getElement('login_passwd').focus();
          }
        }
      }  
      showElement('div_wait', false);              
    }
  }    
  
  req.caching = true;
  req.open('POST', 'get_user_info_js.html', true);
  req.send({ go: 'email', email: email});  
}
