function initRespForm()
{
	var prows = $('tr[id^="phone_row_"]').length;
	if (prows==3) {
		$('#add_phone_row').css('display','none');
	}
	
	var f_clear = function() {
		if (prows>1) {
			prows--;
			$('#phone_row_'+$(this).attr('rown')).remove();
			if (prows==2) {
				$('#add_phone_row').removeAttr('style');
			}
		}
		return false;
	};

	$('a[id^="clear_row"]').click(f_clear);
	
	$("a[id^='add_phone']").click(function() {
		if (prows<3) {
			var toadd = ($('#phone_row_1').length) ? 2 : 1;
			var newrow = $('#phone_for_add').clone().attr('id','phone_row_'+toadd);
			$('#add_phone_row').before(newrow.removeAttr('style'));
			$('#phone_row_'+toadd+' [name="new_code"]').attr('name','phone['+(toadd)+'][code]');
			$('#phone_row_'+toadd+' [name="new_num"]').attr('name','phone['+(toadd)+'][num]');
			$('#phone_row_'+toadd+' [name="new_com"]').attr('name','phone['+(toadd)+'][com]');
			$('#phone_row_'+toadd+' [id^="clear_row_"]').attr('rown',toadd).click(f_clear);
			prows++;
			if (prows==3) {
				$('#add_phone_row').css('display','none');
			}
		}
		return false;
	});
}

function setNewRespNum($num_all,$num_new)
{
	$('a[href="/personal/response/"]').append(' (<span class="resp_new">'+$num_new+'</span>/<span class="resp_all">'+$num_all+'</span>)');
	if ($num_new>0) {
		$('a[href="/personal/response/"]').css('font-weight','bold');
	}
}

function initAddVForm($phone)
{
	$("a[id^='place_phone']").click(function() {
		$('#PHONE_CONTACT').val($phone);
		return false;
	});
}

function initRegForm() {
	$(document).ready(function(){
		$('input[name=WORK_COMPANY]').focus();
		$('#shortcut_city a').click(function(){
			$('input[name=WORK_CITY]').val($(this).html());
			return false;
		});
		$('.empl-dirs .checkbox').change(function(){
			if ($(this).attr('checked') && $('.empl-dirs .checkbox:checked').length>3) {
				alert('Выбрать можно до 3-х направлений.');
				$(this).removeAttr('checked');
			}
		});
		$('.captcha-reload').click(function(){
			$.ajax({
				url: '/data/c.php',
				success: function(data) {
					$('input[name=captcha_sid]').val(data.captcha_sid);
					$('#captcha_img').attr('src','/bitrix/tools/captcha.php?captcha_sid='+data.captcha_sid);
				},
				dataType: 'json'
			});
			return false;
		});
	});
}

var table_visible = '';
//var table_visible = 'table-row';

function initAddVacancyForm() {
	$(document).ready(function(){
		$('input[name=WORK_COMPANY]').focus();
		$('#shortcut_city a').click(function(){
			$('input[name="PROPERTY[Town]"]').val($(this).html());
			return false;
		});
		$('#insert_phone').click(function(){
			$('input[name="PROPERTY[PHONE_CONTACT][C_PRFX]"]').val(p_phone['CNT']);
			$('input[name="PROPERTY[PHONE_CONTACT][CODE]"]').val(p_phone['CODE']);
			$('input[name="PROPERTY[PHONE_CONTACT][NUM]"]').val(p_phone['NUM']);
			return false;
		});
		$('#show_all_1').click(function(){
			$(this).parent().css('display','none');
			$('#hide_unp_1').parent().css('display','block');
			$('#e-dir1 > span').css('display','block');
			return false;
		});
		$('#hide_unp_1').click(function(){
			$(this).parent().css('display','none');
			$('#show_all_1').parent().css('display','block');
			$('#e-dir1 > span[class!="dir-priority"]').css('display','none');
			return false;
		});
		$('#show_all_2').click(function(){
			$(this).parent().css('display','none');
			$('#hide_unp_2').parent().css('display','block');
			$('#e-dir2 > span').css('display','block');
			return false;
		});
		$('#hide_unp_2').click(function(){
			$(this).parent().css('display','none');
			$('#show_all_2').parent().css('display','block');
			$('#e-dir2 > span[class!="dir-priority"]').css('display','none');
			return false;
		});
		$('#add_edir a').click(function(){
			$(this).parent().css('display','none');
			$('tr .e-dir-second').css('display',table_visible);
			$('#rm_edir').css('display','block');
			dirsTextDesc();
			return false;
		});
		$('#rm_edir a').click(function(){
			$(this).parent().css('display','none');
			$('tr .e-dir-second').css('display','none');
			$('#add_edir').css('display','block');
			dirsTextDesc();
			return false;
		});
		$('input[name="E_DIR[0]"]:radio').click(function(){
			if (!e_spec[$(this).val()].length) {
				return;
			}
			var opts = '';
			$.each(e_spec[$(this).val()], function(key, value) {
				opts += '<input type="checkbox" name="E_SPEC[]" id="s1_'+ value.id +'" value="' + value.id + '" class="checkbox"><label for="s1_'+ value.id +'">' + value.name + '</label><br>';
			});
			$('#e-spec1').html(opts);
		});
		$('input[name="E_DIR[1]"]:radio').click(function(){
			if (!e_spec[$(this).val()].length) {
				return;
			}
			var opts = '';
			$.each(e_spec[$(this).val()], function(key, value) {
				opts += '<input type="checkbox" name="E_SPEC[]" id="s2_'+ value.id +'" value="' + value.id + '" class="checkbox"><label for="s2_'+ value.id +'">' + value.name + '</label><br>';
			});
			$('#e-spec2').html(opts);
		});
		$('#e-spec1 .checkbox').live('change', function() {
			if ($(this).attr('checked') && $('#e-spec1 .checkbox:checked').length>3) {
				alert('Выбрать можно до 3-х направлений.');
				$(this).removeAttr('checked');
			}
			dirsTextDesc();
		});
		$('#e-spec2 .checkbox').live('change', function() {
			if ($(this).attr('checked') && $('#e-spec2 .checkbox:checked').length>3) {
				alert('Выбрать можно до 3-х направлений.');
				$(this).removeAttr('checked');
			}
			dirsTextDesc();
		});
		
		dirsTextDesc();
		$(':radio[name^="E_DIR["]').change(dirsTextDesc);
	});
}
	
	function dirsTextDesc()
	{
		var dirs_list = $('.e-dirs-text-list');
		dirs_list.empty();
		$('.e-dirs-selected').css('display', 'none');
		
		$.each([$(':radio[name="E_DIR[0]"]:checked+label, '), $('.e-dir-second:visible :radio[name="E_DIR[1]"]:checked+label')], function(index, value) { 
			if (value.text()) {
				dirs_list.append('- Направление трудоустройства: <b>'+value.text()+'</b>;');
				var sp_l = $('#e-spec'+ (index+1) +' .checkbox:checked+label');
				$.each(sp_l,function (i,sp_label) {
					if (i==0) {
						dirs_list.append(' специализаци'+(sp_l.length==1?'я':'и')+':');
					}
					dirs_list.append((i>0 ? ',' : '') + ' <b>' + $(this).text() + '</b>' + (i==sp_l.length-1 ? '.' : ''));
				});
				dirs_list.append('<br>');
			}
		});
		$('.e-dirs-selected').css('display', dirs_list.text() ? table_visible : 'none')
	}
