document.observe('dom:loaded', function(){
	$$('.scroll').each(function(element){
		window.onresize = function(){ scrollReset(); };
		scrollReset();
	});
});

var ShowReaktiveFlag = false;
function ShowReaktiveStart(){
	ShowReaktiveFlag = true;
	setTimeout("ShowReaktive()",100);
}
function ShowReaktive(){
	if (ShowReaktiveFlag){
		new Effect.Appear('rktv_show', {to: 1, duration: 0.4});
	}
}
function HideReaktiveStart(){
	ShowReaktiveFlag = false;
	setTimeout("HideReaktive()",100);
}
function HideReaktive(){
	if (!ShowReaktiveFlag){
		new Effect.Fade('rktv_show', {to: 0, duration: 0.4});
	}
}

var scroller;
var maxScroll;
var scrollDirection;
var scrollSpeed = 1;
var sObj;
var s = 'new';

function swapScroll(s){
	if (s == 'new'){
		s = 'new';
		$('old').hide();
		$('new').show();
		new Effect.Move('scroll', {y: 0, mode: 'absolute', duration: 0.2});
		sObj = $('line1');
		maxScroll = sObj.getWidth() - $('scroll').getWidth();
	}
	if (s == "old"){
		s = 'old';
		$('new').hide();
		$('old').show();
		new Effect.Move('scroll', {y: -91, mode: 'absolute', duration: 0.2});
		sObj = $('line2');
		maxScroll = sObj.getWidth() - $('scroll').getWidth();
	}
}

function startScroll(){
	if (scrollDirection == 'left'){
		// Абсолютно не универсальный способ прокрутки, зато с точностью до px и плавный
		if (parseInt(sObj.style.left,10) < 0) {
			sObj.style.left = parseInt(sObj.style.left,10)+scrollSpeed+'px';
		}
		if (parseInt(sObj.style.left,10) < 0) {
			sObj.style.left = parseInt(sObj.style.left,10)+scrollSpeed+'px';
		}
		if (parseInt(sObj.style.left,10) < 0) {
			sObj.style.left = parseInt(sObj.style.left,10)+scrollSpeed+'px';
		}
		if (parseInt(sObj.style.left,10) < 0) {
			sObj.style.left = parseInt(sObj.style.left,10)+scrollSpeed+'px';
		}
		if (parseInt(sObj.style.left,10) < 0) {
			sObj.style.left = parseInt(sObj.style.left,10)+scrollSpeed+'px';
		}
	}
	if (scrollDirection == 'right'){
		if (parseInt(sObj.style.left,10) > -maxScroll) {
			sObj.style.left = parseInt(sObj.style.left,10)-scrollSpeed+'px';
		}
		if (parseInt(sObj.style.left,10) > -maxScroll) {
			sObj.style.left = parseInt(sObj.style.left,10)-scrollSpeed+'px';
		}
		if (parseInt(sObj.style.left,10) > -maxScroll) {
			sObj.style.left = parseInt(sObj.style.left,10)-scrollSpeed+'px';
		}
		if (parseInt(sObj.style.left,10) > -maxScroll) {
			sObj.style.left = parseInt(sObj.style.left,10)-scrollSpeed+'px';
		}
		if (parseInt(sObj.style.left,10) > -maxScroll) {
			sObj.style.left = parseInt(sObj.style.left,10)-scrollSpeed+'px';
		}
	}
}

function scrollReset(){
	if (s == 'new'){
		sObj = $('line1');
	}
	if (s == 'old'){
		sObj = $('line2');
	}
	$('line1').style.left = '0px';
	if($('line2')) $('line2').style.left = '0px';
	maxScroll = sObj.getWidth() - $('scroll').getWidth();
}

function sLeft(){
	scrollDirection = 'left';
	scroller = new PeriodicalExecuter(startScroll, 0.01);
}
function sLeftStop(){
	if (scroller != undefined){
		scroller.stop();
	}
}
function sRight(){
	scrollDirection = 'right';
	scroller = new PeriodicalExecuter(startScroll, 0.01);
}
function sRightStop(){
	if (scroller != undefined){
		scroller.stop();
	}
}

function ContactSend(obj){
	if (trim(obj.ucontactname.value)==""){ alert('Сообщение не отправлено. Вы не представились'); return false; }
	if (!emailCheck(obj.ucontactmail.value)){ alert('Сообщение не отправлено. Вы не указали корректный адрес электронной почты'); return false; }
	if (trim(obj.ucontacttext.value)=="") {alert("Сообщение не отправлено. Вы оставили текст письма пустым");return false;}
	var waitobj = document.getElementById("contactwait");
	var buttobj = document.getElementById("contactbutt");
	var sentobj = document.getElementById("contactsent");
	var date=new Date();
	buttobj.style.display = "none";
	sentobj.style.display = "none";
	waitobj.style.display = "";
	requrl = "/inc/sendmail.php?mode=contact&uname="+encodeURI(obj.ucontactname.value)+"&umail="+encodeURI(obj.ucontactmail.value)+"&utext="+encodeURI(obj.ucontacttext.value)+"&uniq="+date.getTime();
//	alert(requrl);
	var XHttpRequest = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	if (XHttpRequest){
		XHttpRequest.onreadystatechange = function(){ 
			if (XHttpRequest.readyState == 4) 
				if (XHttpRequest.status == 200){
					var req = XHttpRequest.responseText;
					sentobj.style.display = "";
					waitobj.style.display = "none";
					buttobj.style.display = "none";
					obj.ucontactname.value = "";
					obj.ucontactmail.value = "";
					obj.ucontacttext.value = "";
//					alert("Сообщение отправлено");
					setTimeout("ReadyToSend()",3000);
					setTimeout("Lightview.hide()", 5000); 
					return true;
				}
				else{
					alert("Ошибка. Попробуйте позднее");
					buttobj.style.display = "";
					waitobj.style.display = "none";
					sentobj.style.display = "none";
					return false;
				}
		};
		XHttpRequest.open("GET", requrl, true);
		XHttpRequest.send("null");
	}
}
function RegistrationSendSimple(obj){
	if (trim(obj.ulastname.value)==""){ alert('Введите фамилию'); return false; }
	if (trim(obj.ufirstname.value)==""){ alert('Введите имя'); return false; }
	obj.submit();
}

function RegistrationSend(obj){
	if (trim(obj.ulastname.value)==""){ alert('Введите фамилию'); return false; }
	if (trim(obj.ufirstname.value)==""){ alert('Введите имя'); return false; }
//	if (trim(obj.utel.value)=="") {alert("Укажите Ваш телефон");return false;}
//	if (!emailCheck(obj.umail.value)){ alert('Укажите, пожалуйста, корректный адрес электронной почты'); return false; }
	var waitobj = document.getElementById("wait");
	var buttobj = document.getElementById("butt");
	var sentobj = document.getElementById("sent");
	var date=new Date();
	buttobj.style.display = "none";
	sentobj.style.display = "none";
	waitobj.style.display = "";
	requrl = "/inc/sendmail.php?mode=conference&ulastname="+encodeURI(obj.ulastname.value)+"&ufirstname="+encodeURI(obj.ufirstname.value)+"&usecondname="+encodeURI(obj.usecondname.value)+"&uage="+encodeURI(obj.uage.value)+"&udegree="+encodeURI(obj.udegree.value)+"&ucity="+encodeURI(obj.ucity.value)+"&ujob="+encodeURI(obj.ujob.value)+"&uaddress="+encodeURI(obj.uaddress.value)+"&umail="+encodeURI(obj.umail.value)+"&utitle="+encodeURI(obj.utitle.value)+"&uauthors="+encodeURI(obj.uauthors.value)+"&utype="+encodeURI(obj.utype.value)+"&usection="+encodeURI(obj.usection.value)+"&uniq="+date.getTime();
	var XHttpRequest = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	if (XHttpRequest){
		XHttpRequest.onreadystatechange = function(){ 
			if (XHttpRequest.readyState == 4) 
				if (XHttpRequest.status == 200){
					var req = XHttpRequest.responseText;
					sentobj.style.display = "";
					waitobj.style.display = "none";
					buttobj.style.display = "none";
					obj.ufirstname.value = "";
					obj.usecondname.value = "";
					obj.ulastname.value = "";
					obj.uage.value = "";
					obj.udegree.value = "";
					obj.ucity.value = "";
					obj.ujob.value = "";
					obj.uaddress.value = "";
					obj.umail.value = "";
					obj.utitle.value = "";
					obj.uauthors.value = "";
					obj.utype.value = "";
					obj.usection.value = "";
					setTimeout("ReadyToSend()",3000);
//					alert("Сообщение отправлено");
					return true;
				}
				else{
					alert("Ошибка. Попробуйте позднее");
					buttobj.style.display = "";
					waitobj.style.display = "none";
					sentobj.style.display = "none";
					return false;
				}
		};
		XHttpRequest.open("GET", requrl, true);
		XHttpRequest.send("null");
	}
}

function ReadyToSend(){
	var waitobj = document.getElementById("wait");
	var buttobj = document.getElementById("butt");
	var sentobj = document.getElementById("sent");
	buttobj.style.display = "";
	sentobj.style.display = "none";
	waitobj.style.display = "none";
}

function	trim(s){
	return 	s.replace(/^(\s*)/,"$`").replace(/(\s*)$/,"$'");
}

function	emailCheck (emailStr) 
{
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) return false;
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++)	if (user.charCodeAt(i)>127) 	return false;
	for (i=0; i<domain.length; i++)	if (domain.charCodeAt(i)>127)	return false;
	if (user.match(userPat)==null) 	return false;
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {	for (var i=1;i<=4;i++) if (IPArray[i]>255) return false;
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) if (domArr[i].search(atomPat)==-1)	return false;
	if (checkTLD && domArr[domArr.length-1].length!=2 &&	domArr[domArr.length-1].search(knownDomsPat)==-1)			return false;
	if (len<2)		return false;
	return true;
}


