$(function() {
  $('FORM#auth').hide();
  $('.menu_str LI:last').css('border-right','none');
  $('.menu_str LI:first').css({'border-left': 'none', 'padding-left': '0'});
  
	$("#div_registr_diler FORM").hide();
  $("#div_registr_diler H4").toggle(
  	function(event) {$(this).next().show("fast");},
  	function(event) {$(this).next().hide("fast");}
  );
  $("#div_registr_diler H4").hover(
  	function(event) {$(this).addClass("over");},
  	function(event) {$(this).removeClass("over");}
  );

  $('SELECT[name=num_in_page]').change(function(event) {
    SetCookieSimple('num_in_page',$(this).val());
    window.location = $(this).attr('alt');
  });
  $('#ps4prompt').hide();

  $('*').click(function(event) {
  	$('#ps4prompt').hide();
  });

	$('#search_input').keydown(function(event) {
		if (event.which == 40) {
			if ($('#ps4prompt LI').hasClass('sel')) {
				$('#ps4prompt LI.sel').removeClass('sel').next().addClass('sel');
				$('#search_input').attr('value',$('#ps4prompt LI.sel').html() ? $('#ps4prompt LI.sel').html() : '');
			} else {
				$('#ps4prompt LI:first').addClass('sel');
				$('#search_input').attr('value',$('#ps4prompt LI:first').html());
			}
		}
		else if (event.which == 38) {
			if ($('#ps4prompt LI.sel').hasClass('sel')) {
				$('#ps4prompt LI.sel').removeClass('sel').prev().addClass('sel');
				$('#search_input').attr('value',$('#ps4prompt LI.sel').html() ? $('#ps4prompt LI.sel').html() : '');
			} else {
				$('#ps4prompt LI:last').addClass('sel');
				$('#search_input').attr('value',$('#ps4prompt LI:last').html());
			}
		} else {$('#ps4prompt LI').removeClass('over').removeClass('sel');};
	});
  $('INPUT[name^=cb_]').click(function() {
    getItems({'vars': {'id': this.name.substring(3)},'act': 'ajax_in_karusel'});
  });
  
  $('.show_vacansy').toggle(
  	function(event) {$(this).next().show()},
  	function(event) {$(this).next().hide()}
  );
  
});

//--------------------------
function VacancyView() {
  var table_education = '<table class="table_edit_tr" id="table_education">'+
  		'<tr>'+
  			'<th>Годы учебы</th>'+
  			'<th>Учебное заведение</th>'+
  			'<th>Факультет</th>'+
  			'<th>Специальность</th>'+
  			'<th>Форма обучения</th>'+
  		'</tr>'+
  		'<tr>'+
  			'<td><input type="text" name="" value=""></td>'+
  			'<td><input type="text" name="" value=""></td>'+
  			'<td><input type="text" name="" value=""></td>'+
  			'<td><input type="text" name="" value=""></td>'+
  			'<td><input type="text" name="" value=""></td>'+
  		'</tr>'+
  		'<tr class="add_str">'+
  			'<td colspan=5><a href="javascript:">Добавить строку</a></td>'+
  		'</tr>'+
  	'</table>';

  var table_professional = '<table class="table_edit_tr" id="table_professional">'+
  		'<tr>'+
  			'<th>Период работы</th>'+
  			'<th>Название организации</th>'+
  			'<th>Направление деятельности организации</th>'+
  			'<th>Должность</th>'+
  			'<th>Причина ухода</th>'+
  		'</tr>'+
  		'<tr>'+
  			'<td><input type="text" name="" value=""></td>'+
  			'<td><input type="text" name="" value=""></td>'+
  			'<td><input type="text" name="" value=""></td>'+
  			'<td><input type="text" name="" value=""></td>'+
  			'<td><input type="text" name="" value=""></td>'+
  		'</tr>'+
  		'<tr class="add_str">'+
  			'<td colspan=6><a href="javascript:">Добавить строку</a></td>'+
  		'</tr>'+
  	'</table>';

  $('#tr_education_table B').html(table_education);
  $('#tr_professional_table B').html(table_professional);
  $('#table_professional TR:nth-child(2)').clone().insertBefore('#table_professional .add_str').clone().insertBefore('#table_professional .add_str');
  $('.add_str').click(function(event){$(this).prev().clone().insertBefore($(this)).find('INPUT').val('');});

  $(".as_date").mask("99.99.9999");
  
	$('#comp1 SELECT').val(2);
	$('#car INPUT').attr('disabled','disabled').css('background','#EEEEEE');
  $('SELECT[name=motor_car]').change(function(event){
  	if ($(this).val() == 1) {$('#car INPUT').attr('disabled','disabled').css('background','#EEEEEE');}
  	else{$('#car INPUT').removeAttr('disabled').css('background','#fff')};
  })
  $('INPUT').focus(function(event){
  	$(this).css('background','#fff');
  })

}
//--------------------------
function VacancySubmit(vars) {
  if (!CheckAndSayRequared($('#frm_vacancy')[0],vars)) {return false;}

  	  var table_education_clone = $('#table_education').clone();
			$(table_education_clone).find('TD').each(function(){
			  var val = $(this).find('INPUT').val();
				$(this).html((val) ? val : '');
			})
			$(table_education_clone).find('TR').each(function(){
				if ($(this).text() == '') {$(this).remove();}
			})
			$(table_education_clone).find('TD').each(function(){
				if ($(this).html() == ''){$(this).html('&nbsp;')};
			})
			$('INPUT[name=education_table]').val('<table class="table_edit_tr" id="table_education" cellspacing=0>' + $(table_education_clone).html() + '</table>');

  	  var table_professional_clone = $('#table_professional').clone();
			$(table_professional_clone).find('TD').each(function(){
			  var val = $(this).find('INPUT').val();
				$(this).html((val) ? val : '');
			})
			$(table_professional_clone).find('TR').each(function(){
				if ($(this).text() == '') {$(this).remove();}
			})
			$(table_professional_clone).find('TD').each(function(){
				if ($(this).html() == ''){$(this).html('&nbsp;')};
			})
			$('INPUT[name=professional_table]').val('<table class="table_edit_tr" id="table_professional" cellspacing=0>' + $(table_professional_clone).html() + '</table>');

	return true;
}
//--------------------------
function BasketPage() {
  $('INPUT[name^=act_zakaz]').click(function(event) {
    var s = $(this).next('LABEL').html();
    var t = $('#tr_message TEXTAREA').val();
    var r = t + s + "\n";
    if (!$(this).attr('checked')) {
      var p = t.indexOf(s)
      if (p == -1) {return;}
      r = t.substring(0,p) + t.substring(p + 1 + s.length);
    }
    $('#tr_message TEXTAREA').val(r);
  });
}
//--------------------------
function LoginView() {
  $('FORM#auth').show();
  $('FORM#reg').hide();
}
//--------------------------
function RegView() {
  $('FORM#auth').hide();
  $('FORM#reg').show();
}
//--------------------------
function set_form_search_width () {
//  $('#search_input').width($('#td_content').width() - 400);  // 170
//  $('#ps4prompt LI').width($('#search_input').width());
}
//--------------------------
function psPromptView () {
  $('#ps4prompt LI').removeClass('over').removeClass('sel');
	$('#ps4prompt LI:last').css('border-bottom-color', '#ccc');
  $('#ps4prompt').show();

  $('#ps4prompt LI').hover(
  	function(event) {$(this).toggleClass('over');},
  	function(event) {$(this).toggleClass('over');}
  );

  $('#ps4prompt LI').click(function(event) {
  	$(this).parents('#ps4prompt').hide().prev().attr('value',$(this).html());
  	$('#search_btn').click();
  });
}
//--------------------------
function SetCookieSimple(c_name,c_val) {
  document.cookie = "cookie[" + c_name + "]=" + c_val + "; expires=" + CalcTime(30) + '; path=/';
}
//--------------------------
function CalcTime(days) {
	var plusDays = days*24*60*60*1000;
	var expDate = new Date();
	expDate.setTime(expDate.getTime() + plusDays);
	return expDate.toGMTString();
}
//--------------------------
function CheckAndSayRequared(o_frm,vars) {
  for (var k in vars) {
    if (o_frm[k].value && o_frm[k].value != '0') {continue;}
    $(o_frm[k]).css('background','#f00');
    alert('Заполните пожалуйста поле «' + vars[k] + '»');
    ScrollToElement(o_frm[k]);
    return false;
  }
  if (o_frm.submit) {o_frm_now = o_frm; setTimeout("o_frm_now.submit.disabled = true;",300);}
  return true;
}
function ScrollToElement(el) {
	el_top = $(el).offset().top - 100; 
	$('html').animate({scrollTop: el_top}, 500); 
}
//--------------------------

//-- not jQuery
var o;
var o_color = 'red';
var counter = 1;
var o_frm_now;
//----------------------------------------------------------------------------
function CheckRequared(o_frm,vars) {
  for (var k in vars) {
    if (o_frm[vars[k]].value && o_frm[vars[k]].value != '0') {continue;}
    o = o_frm[vars[k]];
    counter = 1;
    o_color = 'red';
    o.value = ' Введите данные';
    o.focus();
    setTimeout("ChangeBg()",10);
    return false;
  }
  if (o_frm.submit) {o_frm_now = o_frm; setTimeout("o_frm_now.submit.disabled = true;",300);}
  return true;
}
//----------------------------------------------------------------------------
function ChangeBg() {
  counter++;
  if (counter > 8) {o.value = ''; return;}
  o_color = (o_color == 'red') ? 'white' : 'red';
  o.style.background = o_color;
  setTimeout("ChangeBg()",300)
}

