var newPage = false;
var participants = 1;

function imgOnOff(img, action) {
	
	var s;
	
	if (action == 'active')	{
		s = img.src.replace(/inactive/g, "active");
	} else {
		s = img.src.replace(/active/g, "inactive");
	}
	
	img.src	= s;
	
}
	
	function hideShow(id) {
		obj = $(id);
		if (obj.style.display == 'none')  obj.style.display = ''; 
		else obj.style.display = 'none';
	}
	
	function startSiteRegistration() {
		if (!document.regform.name.value || !document.regform.name.value || document.regform.name.value == nametext || document.regform.email.value == emailtext) alert(must_fill_in_all_fields);
		else document.regform.submit();
	}	
	
	function getNameBrouser() {
		 var ua = navigator.userAgent.toLowerCase();
		 // Определим Internet Explorer
		 if (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1) return "msie";
		 // Opera
		 if (ua.indexOf("opera") != -1) return "opera";
		 // Gecko = Mozilla + Firefox + Netscape
		 if (ua.indexOf("gecko") != -1) return "gecko";
		 // Safari, используется в MAC OS
		 if (ua.indexOf("safari") != -1) return "safari";
		 // Konqueror, используется в UNIX-системах
		 if (ua.indexOf("konqueror") != -1) return "konqueror";
		 return "unknown";
	}
	
	function loadIndex() {
		index_bgrd_height();
	}
	
	function loadMain() {
		setTimeout(main_blocks_view,500);
	}
	
	
	function setMainPageHeight() {
		if ($('content') && (document.body.offsetHeight - (350 + $('main_blocks').offsetHeight)) > 0) {// - 103
			razn = document.body.offsetHeight - (350 + $('main_blocks').offsetHeight - 95);
			cHeight = $('content').offsetHeight * 1;
			if (cHeight > 0) {
				$('main_footer').style.marginTop = (razn - 55) + 'px';
			} else {
				setTimeout(setMainPageHeight, 500);
			}
		}
		if ((document.body.offsetHeight - (350 + $('main_blocks').offsetHeight)) < 0) { // - 103
			$('main_bgrd_left').style.height = 350 + $('main_blocks').offsetHeight;
			$('main_bgrd_right').style.height = 350 + $('main_blocks').offsetHeight;
		}
		
	}
	
	function index_bgrd_height() {
		if ($('index_content').clientHeight > 170 && document.body.offsetHeight < (768 + ($('index_content').clientHeight - 170))) {
			height = (768 + ($('index_content').clientHeight - 170))+'px';
		} else if (document.body.offsetHeight > 768) {
			height = document.body.offsetHeight+'px';
		} else {
			height = '768px';
		}
		$('index_bgrd').style.height = height;
		$('index_bgrd_right').style.height = height;
		$('index_bgrd_left').style.height = height;
		if ($('index_blocks').style.display == 'none') setTimeout(index_blocks_view, 3500);
	}
	function main_blocks_hide() {
		Effect.Fade('main_blocks',{duration:1.5});
		Effect.Fade('year_btns');
	}
	function index_blocks_view() {
		Effect.Appear('index_bgrd_l_r');
		$('index_blocks').style.display = '';
		Effect.Appear('index_logo');
		Effect.Appear('index_asold_title');
		Effect.Appear('index_content');
	}
	function index_bgrd_view() {
		Effect.Appear('index_bgrd_l_r');
	}
	function index_blocks_hide() {
		Effect.Fade('index_bgrd_l_r');
		Effect.Fade('index_blocks');
	}
	function main_blocks_view() {
		Effect.Appear('main_bgrd');
		Effect.Appear('main_blocks',{duration:1.5});
		cpa = currentPage.split('/');
		if (cpa[0] == '2009') Effect.Appear('btn2009');
		else if (cpa[0] == '2008') Effect.Appear('btn2008');
		else if (cpa[0] == '1992') Effect.Appear('btn1992');
		else if (cpa[0] == '2010') Effect.Appear('btn2010');
		else Effect.Appear('btn2011');
		setMainPageHeight();
	}
	
	function setPage(newPage) {
		yearsPages = new Array('1992/','2008/','2009/','2010/','2011/');
		if (newPage == '/') newPage = '';
		if (!in_array(newPage,yearsPages)) location.href = http_path+langlink+newPage;
		else location.href = http_path+newPage;
		//ui.currentPage = newPage;
	}
	
	function loadPage(newPage) {
		setPage(newPage);
		if (currentPage != 'index/index/') main_blocks_hide();
		else Effect.Fade('index_blocks',{duration:1.5});
	}
	
	function openConactsTab() {
		hideShow('tab_contacts');
		hideShow('tab_opendoor');
		hideShow('content1');
		hideShow('content2');
		setMainPageHeight();
	}
	
	function closeRegForm() {
		Effect.Fade('reg_form_block');
		clearParticipantsList();
		$('participantsList').style.display = 'none';
	}
	
	function regForm() {
		//$('reg_form_block').style.display = '';
		if (document.body.offsetHeight < 735) {
			$('reg_form_distance').style.marginBottom = '-' + parseInt((document.body.offsetHeight / 2)) + 'px';
			$('reg_form').style.height = document.body.offsetHeight + 'px';
			$('reg_form_content').style.height = (document.body.offsetHeight - 8) + 'px';
		}
		Effect.Appear('reg_form_block');
	}
	
	function addParticipant() {
		$('participantsList').style.display = 'none';
		if (!$('uname').value || !$('upos').value) {
			alert('Для добавления участника введите его имя и должность');
		} else {
			item_uname = $('uname').value;
			item_upos = $('upos').value;
			$('reg_form_participants').innerHTML += '<input type="hidden" name="username['+participants+']" value="'+item_uname+'" />'+
				'<input type="hidden" name="position['+participants+']" value="'+item_upos+'" />';
			participants++;
			alert('Участник '+item_uname+' добавлен.\nВведите информацию о новом участнике');
		}
	}
	
	function participantList() {
		oFrom = document.regform;
		out = '<a href="#" onclick="Effect.Fade(\'participantsList\');return false" id="closeLP">X</a><h2>Список участников</h2><ol>';
		if (participants > 1 || (oFrom['username[0]'].value && oFrom['position[0]'].value)) {
			for (i = 0; i < participants; i++) {
				if (oFrom['username[' + i + ']'].value && oFrom['position[' + i + ']'].value) {
					out += '<li>';
					out += oFrom['username['+i+']'].value + ' - ' + oFrom['position['+i+']'].value;
					out += '<br /><a href="#" onclick="editParticipant('+i+');return false">Редактировать</a> / <a href="#" onclick="delParticipant('+i+');return false">Удалить</a></li>';
				}
			}
			$('participantsList').innerHTML = out + '</ol>';
			Effect.Appear('participantsList');
		} else {
			if ($('participantsList').style.display == '') {
				Effect.Fade('participantsList');
				setTimeout(noParticipants,1000);
			} else {
				noParticipants();
			}
			
		}
	}
	
	function noParticipants() {
		alert('Нет зарегистрированных участников');
	}
	
	function editParticipant(id) {
		Effect.Fade('participantsList');
		oFrom['username[0]'].value = oFrom['username['+id+']'].value;
		oFrom['position[0]'].value = oFrom['position['+id+']'].value;
		reCreateParticipansList(id);
	}
	
	function delParticipant(id) {
		if (confirm('Удалить участника ' + oFrom['username['+id+']'].value + '?')) {
			if (id != 0) {
				if (reCreateParticipansList(id)) participantList();
			} else {
				if (participants == 1) {
					Effect.Fade('participantsList');
					setTimeout(noParticipants,1000);
				}
				oFrom['username['+id+']'].value = '';
				oFrom['position['+id+']'].value = '';
				
			}
		}
		
	}
	function clearParticipantsList() {
		if (participants > 1) {
			lim = participants - 1;
			for (k = 1; k < lim; k++) {
				id = participants--;
				reCreateParticipansList(id);
			}
		}
	}
	function reCreateParticipansList(id) {
		oFrom = document.regform;
		if (participants > 1 && id > 0) {
			for (i = id; i < (participants - 1); i++) {
				oFrom['username['+i+']'].value = oFrom['username['+(i+1)+']'].value;
				oFrom['position['+i+']'].value = oFrom['position['+(i+1)+']'].value;
			}
			elem_name = oFrom['username['+(participants-1)+']'];
			elem_pos = oFrom['position['+(participants-1)+']'];
			elem_name.parentNode.removeChild(elem_name);
			elem_pos.parentNode.removeChild(elem_pos);
			participants = participants - 1;
			if (participants == 1) Effect.Fade('participantsList');
			return true;
		} else {
			return false;
		}
	}
	
	function chech_reg_from() {
		oFrom = document.regform;//
		if ((oFrom.asold.checked || oFrom.opendoor.checked) && 
				((oFrom['username[0]'].value && oFrom['position[0]'].value) || (oFrom['username[1]'] && (oFrom['username[1]'].value && oFrom['position[1]'].value))) &&
				oFrom.orgname.value && oFrom.tel.value && isValidEmail(oFrom.email.value)) {
			if (confirm(confirmRegistration(oFrom,oFrom.asold.checked,oFrom.opendoor.checked))) oFrom.submit();
		} else {
			alert('Проверьте введеные данные');
		}
	}
	
	function confirmRegistration(oFrom,asold,opendoor) {
		out = 'Вы регистрируетесь на мероприятие:\n';
		//oFrom = document.regform;
		if (asold) out += oFrom.asold.value + '\n';
		if (opendoor) out += oFrom.opendoor.value + '\n';
		out += '\nУчастники от организации "'+ oFrom.orgname.value +'":\n';
		for (i = 0; i < participants; i++) {
			if (oFrom['username['+i+']'].value) out += oFrom['username['+i+']'].value + ' - ' + oFrom['position['+i+']'].value + '\n';
		}
		out += '\nКонтактный телефон: ' + oFrom.tel.value;
		out += '\nЭлектронная почта: ' + oFrom.email.value;
		out += '\n\nПродолжить регистрацию?';
		return out;
	}
	
	function isValidEmail (email, strict)
	{
	 if ( !strict ) email = email.replace(/^\s+|\s+$/g, '');
	 return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
	}
	
	function in_array(what, where) {
	    for(var i=0; i<where.length; i++)
	        if(what == where[i]) 
	            return true;
	    return false;
	}
