// JavaScript Document

function radioCheckboxBold(){
	var el = $('.formElement label input');
		el.each(function(n){
			if(this.checked){
				$(this).parents('label').addClass('active');
				}
			else{
				$(this).parents('label').removeClass('active');
				}
			});
	}


$(function(){
//====================================
//Чекбоксы и радиобатоны становтся жирными
$('.formElement label input')
	.click(function(){
		var i = $(this);
		var l2 = i.parent('label').attr('class')=='level2';
		var tl2 = i.parents('.level2parent');
		if(l2 && tl2.length>0){
			i.parents('.level2parent').children('label:first').addClass('active').children('input')[0].checked=true;
			}
		else{
			if(i.attr('class')!='level2main'){
				//i.parents('.formElement').children('.level2parent').children('label.level2').children('input').attr('disabled','disabled');
				i.parents('.formElement').children('.level2parent').children('label.level2').hide().children('input').hide();
				}
			else{
				//i.parents('.formElement').children('.level2parent').children('label.level2').children('input').attr('disabled','');
				i.parents('.formElement').children('.level2parent').children('label.level2').show().children('input').show();
				}
			}
			radioCheckboxBold();
	   });
radioCheckboxBold();

//Вставляет "не знаю"
$('.rub .interiorLink')
	.click(function(){
		$(this).parents('.formElement').children('input').attr('value',this.innerHTML);
		return false;
		});


// есть нет сайта
/*
	$('.notSite')
		.change(function(){
			if(this.checked){
				$('.siteHideEl').hide();
				}
			else{
				$('.siteHideEl').show();
				}
			})
		.each(function(){
			if(this.checked){
				$('.siteHideEl').hide();
				}
			else{
				$('.siteHideEl').show();
				}
			});
	$('.ySite')
		.change(function(){
			if(this.checked){
				$('.siteHideEl').show();
				}
			else{
				$('.siteHideEl').hide();
				}
			});

*/
$('.siteHideEl').show();
$('.ySite')
	.click(function(){
		if(this.checked){
			$('.siteHideEl').show();
			}
		else{
			$('.siteHideEl').hide();
			}
	})
	;
$('.notSite')
	.click(function(){
		if(this.checked){
			$('.siteHideEl').hide();
			}
		else{
			$('.siteHideEl').show();
			}
	})
	;


//Селект, если
$('.teamContract select')
	.change(function(){
		if(this.value==1){
			$(this).next('input').hide();
			}
		else{
			$(this).next('input').show();
			}
	})
	.each(function(){
		if(this.value==1){
			$(this).next('input').hide();
			}
		else{
			$(this).next('input').show();
			}
		});

// свой вариант - если

$('#goals4')
	.click(function(){
		if(this.checked){
			$('#goals_text').show();
			}
		else{
			$('#goals_text').hide();
			}
	})
	;
////////////////
// место проживания аудитории - если

$('#other_place')
	.click(function(){
		if(this.checked){
			$('#other').show();
			}
		else{
			$('#other').hide();
			}
	})
	;
////////////////

// необходимые услуги - если

$('#dop6')
	.click(function(){
		if(this.checked){
			$('#dop_text').show();
			}
		else{
			$('#dop_text').hide();
			}
	})
	;
////////////////
//Календарь
//$.datepicker.setDefaults($.extend($.datepicker.regional["ru"]));
$(".calendar2").datepicker({
						showOn:'button',
						buttonImage:'/img/icons/bgButtonCalendar.gif'
						});


$('.file a')
	.click(function(){
		var i = $(this);
		var n = i.attr('rel');
		var t = '<div class="addFile"><input type="file" name="'+n+'[]" /><a href="#" onclick="removeFile(this);return false">Удалить</a></div>'
		i.parents('.applItem').children('.formElement')[0].innerHTML +=t;
		i.html('Прикрепить еще<br />один файл').parent().children('span').hide();
		return false;
		});

//====================================
});

function removeFile(el){
	var p = $(el).parents('.applItem');
	$(el).parent().remove();
	var n = p.children('.formElement').children('.addFile').length;
		if(n!=0){
			p.children('.file').children('a').html('Прикрепить еще<br /> один файл').parent().children('span').hide();
			}
		else{
			p.children('.file').children('a').html('Прикрепить файл').parent().children('span').show();
			}
	}





//======================================
//Вставка новых элементов
//Нумерует все элементы попорядку
function numberPage(){
	$('.addContext .formText span')
		.each(function(n){
			this.innerHTML = n+Number(1);
		});
	}



function hideAddEl(el){
		var n = el.children('.addEl').length;
		if(n==1){
			el.removeClass('many');
			}
		else{
			el.addClass('many');
			}
		}

$(function(){
//============================================
$('.deleteInput')
	.click(function(){
		var t = $(this).parents('.addContext').length>0?1:0;
		var n = $(this).parents('.applSection').children('.applItem').length - t;
		var p = $(this).parents('.applSection');
		if(n>1){
			$(this).parents('.applItem').remove();
			numberPage();
		}

		hideAddEl(p);
		return false;
		});


//
/*  добавляет блок с кнопкой

$('.addSite button')
	.click(function(){
		var el = $(this).parents('.applItem').clone(true);
		$(this).parents('.applItem').after(el);
		el.children('div').children('.inputText').each(function(){this.value=''});
		hideAddEl($(this).parents('.applSection'));
		return false;
		}); */

$('.addSite button')
	.click(function(){
		var el = $(this).parents('.applItem').prev('.applItem').clone(true);
		$(this).parents('.applItem').prev('.applItem').after(el);
		el.children('div').children('.inputText').each(function(){this.value=''});
		hideAddEl($(this).parents('.applSection'));
		return false;
		});

$('.addElBut')
	.click(function(){
			var el = $('.addContext .addEl:last');
			var nEl = el.clone(true);
			el.after(nEl);
			nEl.children('.formElement').children('input').attr('value','');
			numberPage();
			hideAddEl($(this).parents('.applSection'));
		return false;
		});
//============================================
// Автозаполнение

function setCookie (name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires : "") +
		"; path=/" +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

$('.autoComplete')
	.click(function(){
		setCookie('autoComplete', '1');
		});

var forAutoComplete = [
	'company_name',
	'concurents',
	'company_site',
	'services',
	'budjet',
	'contact_fio',
	'contact_staff',
	'contact_phone',
	'contact_email',
	'other',
	'time',
	'mytime'
];
var places = [
	'spb',
	'spb_obl',
	'severo-zapad',
	'moscow',
	'msk_obl',
	'russia',
	'world'
]

$('.briefForm')
	.submit(function(){
		// сохранение в куки текстовых полей и значений выпадающих списков
		for (var count=0; count<forAutoComplete.length; count++) {
			if ($('*[name=' + forAutoComplete[count] + ']')[0]!==undefined && $('*[name=' + forAutoComplete[count] + ']')[0].value!='') {
				setCookie(forAutoComplete[count], $('*[name=' + forAutoComplete[count] + ']')[0].value);
			}
		}
		// сохранение в куки места проживания аудитории
		if ($('*[name=spb]')[0]!==undefined) {
			for (var count=0; count<places.length; count++) {
				if ($('*[name=' + places[count] + ']')[0].checked) {
					setCookie(places[count], 1);
				} else {
					setCookie(places[count], 0);
				}
			}
		}
		// сохранение в куки пола аудитории
		if ($('*[name=sex1]')[0]!==undefined) {
			if ($('*[name=sex1]')[0].checked) {
				setCookie('sex1', 1);
			} else {
				setCookie('sex1', 0);
			}
			if ($('*[name=sex2]')[0].checked) {
				setCookie('sex2', 1);
			} else {
				setCookie('sex2', 0);
			}
		}
	});

if (getCookie('autoComplete')==1) {
	// открытие поля "Свой вариант" для сроков
	if (getCookie('time')==2) {
		$('*[name=mytime]:eq(0)').show();
	}
	if (getCookie('other')) {
		$('*[name=other]:eq(0)').parents('label').prev('label').children('input')[0].checked=true;
	}
	// автозаполнение текстовых полей и значений выпадающих списков
	for (var count=0; count<forAutoComplete.length; count++) {
		if ($('*[name=' + forAutoComplete[count] + ']')[0]!==undefined && getCookie(forAutoComplete[count])) {
			$('*[name=' + forAutoComplete[count] + ']')[0].value = getCookie(forAutoComplete[count]);
			setCookie(forAutoComplete[count], '');
		}
	}
	// автозаполнение места проживания аудитории
	if ($('*[name=spb]')[0]!==undefined) {
		for (var count=0; count<places.length; count++) {
			if (getCookie(places[count])==1) {
				$('*[name=' + places[count] + ']')[0].checked = true;
				$('*[name=' + places[count] + ']:eq(0)').parents('label').addClass('active');
			}
			setCookie(places[count], '');
		}
	}
	// автозаполнение пола аудитории
	if ($('*[name=sex1]')[0]!==undefined) {
		if (getCookie('sex1')==1) {
			$('*[name=sex1]')[0].checked = true;
			$('*[name=sex1]:eq(0)').parents('label').addClass('active');
		}
		if (getCookie('sex2')==1) {
			$('*[name=sex2]')[0].checked = true;
			$('*[name=sex2]:eq(0)').parents('label').addClass('active');
		}
		setCookie('sex1', '');
		setCookie('sex2', '');
	}
	setCookie('autoComplete', '0');
}
//============================================
});








/* открытие флеш игры в попап окне заданного размера */

var v=parseInt(navigator.appVersion);
isNetscape=navigator.appName.indexOf('Netscape')>=0;
isExplorer=navigator.appName.indexOf('Explorer')>=0;
if(v>=3) self.focus();

function wopen(id,ww,hh) {

  positionCode='';
  if (v>=4) {
    sw=screen.width; sh=screen.height;
    wbx=Math.round((sw-ww)/2); wby=Math.round((sh-hh)/2);
    if (isExplorer) positionCode='left='+wbx+',top='+wby+',';
    if (isNetscape) positionCode='screenX='+wbx+',screenY='+wby+',';
  }
if (ww =='100%') {
	var ff=1;
} else {
	ff=0;
}

window.open('/php_includes/flash.phtml?project_id='+ id +'','newWindow','width='+ ww +',height='+ hh +','+positionCode+'toolbar=0,'+'scrollbars=0,'+'resizable=1'+',fullscreen='+ ff);

  if (isNetscape && v>=3) newWindow.focus();
}














