// специальная тикалка против спам-ботов
dateObject = new Date();
time_load = dateObject.getTime();


// функции проверки форм

function frmRegSubmit() {
	frm = document.forms.frmRegister;
	errorMSG="";
	if (frm.val1.value == "" || frm.val2.value == "" || frm.val3.value == "" || frm.val4.value == "" || frm.val5.value == "") errorMSG="Необходимо заполнить все поля!";
	frmRegColor();
	
	dateObject = new Date();
	time_send = dateObject.getTime();
	if (time_send-time_load<5000) errorMSG="Шустрый бот!!!";
	
	if (errorMSG) alert(errorMSG);
		else frm.submit();
}

function frmRegColor() {
	if (errorMSG) {
		frm = document.forms.frmRegister;
		frm.val1.style.backgroundColor = ( frm.val1.value == "" ? "#ffb2b2" : "white" );
		frm.val2.style.backgroundColor = ( frm.val2.value == "" ? "#ffb2b2" : "white" );
		frm.val3.style.backgroundColor = ( frm.val3.value == "" ? "#ffb2b2" : "white" );	
		frm.val4.style.backgroundColor = ( frm.val4.value == "" ? "#ffb2b2" : "white" );	
		frm.val5.style.backgroundColor = ( frm.val5.value == "" ? "#ffb2b2" : "white" );	
	}
}

function frmLoginSubmit() {
	frm = document.forms.frmLogin;
	errorMSG="";
	if (frm.lgn.value == "" || frm.pwd.value == "") errorMSG="Необходимо заполнить все поля!";
	frmLoginColor();
	if (errorMSG) alert(errorMSG);
		else frm.submit();
}

function frmLoginColor() {
	if (errorMSG) {
		frm = document.forms.frmRegister;
		frm.lgn.style.backgroundColor = ( frm.lgn.value == "" ? "#ffb2b2" : "white" );
		frm.pwd.style.backgroundColor = ( frm.pwd.value == "" ? "#ffb2b2" : "white" );	
	}
}

function frmProfileSubmit() {
	frm = document.forms.frmProfile;
	errorMSG="";
	if (frm.email.value == "" || frm.iname.value == "" || frm.fname.value == "") errorMSG="Необходимо заполнить все поля!";
	frmProfileColor();
	if (errorMSG) alert(errorMSG);
		else frm.submit();
}

function frmProfileColor() {
	if (errorMSG) {
		frm = document.forms.frmProfile;
		frm.email.style.backgroundColor = ( frm.email.value == "" ? "#ffb2b2" : "white" );
		frm.iname.style.backgroundColor = ( frm.iname.value == "" ? "#ffb2b2" : "white" );
		frm.fname.style.backgroundColor = ( frm.fname.value == "" ? "#ffb2b2" : "white" );
	}
}

function frmPasswordSubmit() {
	frm = document.forms.frmPassword;
	errorMSG="";
	if (frm.newpwd.value == "") errorMSG="Необходимо таки заполнить!";
	if (errorMSG) alert(errorMSG);
		else frm.submit();
}

function frmRecoverSubmit() {
	frm = document.forms.frmRecover;
	errorMSG="";
	if (frm.email.value == "") errorMSG="Необходимо таки заполнить!";
	if (errorMSG) alert(errorMSG);
		else frm.submit();
}

function frmCommentSubmit() {
	frm = document.forms.frmComment;
	errorMSG="";
	if (!frm.c_text.value) errorMSG="Вы, кажется, хотели что-то сказать?";
	if (frm.c_aid.value==0 && !frm.c_name.value) errorMSG="Таки представьтесь пожалуйста!";
	dateObject = new Date();
	time_send = dateObject.getTime();
	if (time_send-time_load<5000) errorMSG="Шустрый бот!!!";
	if (errorMSG) alert(errorMSG);
		else frm.submit();
}

function frmCommentShow() {
	frm = document.forms.frmComment;
	frm.style.display=(frm.style.display=="none"?"inline":"none");
}



// функции публикациии и редактирования постов

function frmPostTurnTag(x) {
	obj=document.getElementById("tag"+x);
	field=document.getElementById("taglist");
	if (obj.className=="frmTagOff") {
		obj.className="frmTagOn";
		field.value = field.value + (field.value==""?"":"-") + x;
	} else {
		obj.className="frmTagOff";
		arr=field.value.split("-");
		str="";
		first=true;
		for (i=0; i<arr.length; i++) {
			if ( Number(x)!=Number(arr[i]) ) {
				str = str + ( first ? "" : "-") + arr[i];
				first=false;
			}
		}
		field.value=str;
	}
}

function frmPostSelectNameMode(x) {
	if (x==0) {
		document.getElementById("namemodefields0").style.display="block";
		document.getElementById("namemodefields1").style.display="none";
	}
	if (x==1) {
		document.getElementById("namemodefields0").style.display="none";
		document.getElementById("namemodefields1").style.display="block";
	}
}

function frmPostSelectName() {
	objS=document.getElementById("name_mode_text");
	if (objS.options[objS.selectedIndex].value != "custom") {
		document.getElementById("author_name").value=objS.options[objS.selectedIndex].value;
		document.getElementById("author_name_box").style.display="none";
	} else {
		document.getElementById("author_name_box").style.display="inline";
	}
}

function frmPostSubmit() {
	frm = document.forms.frmPost;
	errorMSG="";
	if (frm.header.value == "") errorMSG="Необходимо заполнить заголовок!";
	if (errorMSG) alert(errorMSG);
		else frm.submit();
}

function frmPostAddTag(x) {
	frm = document.forms.frmPost;
	switch (x) {
		case "left-aligned-img-text":
			frm.text.value+="<div style=\"margin-left: 215px;\"></div>";
			break;
	}
}

// функция промотки ленты превьюх

function getDocSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [ myWidth, myHeight ];
}

function previewStripScroll(x) {
	var IE = document.all?true:false;
	docsize=getDocSize();
	window.document.getElementById("previewStrip").scrollLeft=Math.round(110*x + 55 - (docsize[0]-448)/2);
}

// функция загрузки изображения

function loadImg(uid, x, xn) {
	// Create new JsHttpRequest object.
	var req = new JsHttpRequest();
	//Prepare preloader
	document.getElementById("imgResult").src = "http://kvn.baikal.ru/img/gagarin_loading.gif";
	// Code automatically called on load finishing.
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			// Write result to page element (_RESULT becomes responseJS). 
			document.getElementById("imgResult").src = req.responseJS.url;
			
			document.getElementById("imgResultComment").innerHTML =req.responseJS.output;
				
			document.getElementById("imgResultContainer").style.display = "inline";
		}
	}
	// Prepare request object (automatically choose GET or POST).
	req.open(null, "../people/imgloader_backend.php", true);
	// Send data to backend.
	req.send( { u: uid } );
	previewStripScroll(x);
	for(i=0; i<xn; i++) if (document.getElementById("psc"+i)) document.getElementById("psc"+i).className="previewStripCell" + (i==x ? "Active" : "");
}


