function checkErrorsArray() {
	for(i=0;fields[i];i++) {
		if(errors[fields[i]]) 
			return false;
	}
	return true;
}

function pass() {
	var pass_1 = document.getElementsByName('main[pass]');
	var pass_2 = document.getElementsByName('main[pass_repeat]');
	if(pass_1[0].value != pass_2[0].value) {
		$('passes').style.display = '';
		errors['pass'] = true;
	}else {
		errors['pass'] = false;
		$('passes').style.display = 'none';
	}
}

function checkFields() {
	if(!checkErrorsArray()) {alert('Пожалуйста заполните все необходимые поля!'); return false;}
	var elems;
	for(i=0;fields[i];i++) {
		elems = document.getElementsByName('main['+fields[i]+']');
		if(elems[0].value.length < 1) {alert('Пожалуйста заполните все необходимые поля!'); return false;}
	}
	if($('user_type').value == '...') {
		alert('Пожалуйста выберите тип регистрации!');
		return false;
	}
	return true;
}

function checkEmail(elem) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = elem.value;
   if(reg.test(address) == false) {
      alert('Пожалуйста введите правильный адрес электронной почты!');
      return false;
   }
   return true;
}

function checkRef(elem) {
	if(parseInt(elem.value) == 1) {
		$('ref').style.display = '';
	} else {
		$('ref').style.display = 'none';
	}
}

function checkField(elem) {
	var field = elem.name.substr(elem.name.indexOf('['));
	field = field.replace('[','');
	field = field.replace(']','');
	if(elem.value.length < 1) {
		elem.style.borderColor = '';
		return;
	}
	$(field).style.display = '';
	var params = {}; params[field] = elem.value;
	new Ajax.Request(loc,{method:'post',parameters:params, onSuccess:function(r) {
		if(r.readyState == 4 && r.status == 200) {
			if(r.responseText == 1) {
				errors[field] = true;
				elem.style.borderColor = 'red';
			} else {
				errors[field] = false;
				elem.style.borderColor = '';
			}
			$(field).style.display = 'none';
			return;
		}
		}
	});
}

function clearLoginFields(field) {
	if(field.name.indexOf('login')>-1 && !login_removed) {
		field.value = '';
		login_removed = 1;
	} else if(field.name.indexOf('pass')>-1 && !pass_removed) {
		field.value = '';
		pass_removed = 1;
	}
}
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);
} 

function sign(dir,par) {
	$('sgn').style.display = '';
	var url = window.location.protocol + '//' + window.location.host + dir +'/sign.php';
	new Ajax.Request(url,{method:'post',parameters:par, onSuccess:function(r) {if(r.readyState == 4 && r.status == 200 && r.responseText !== null) {alert(r.responseText); $('sgn').style.display = 'none';}}});
}

function vote(elem,aid,voted,dir) {
	if(typeof(aid) == 'undefined' || aid === null || isNaN(aid)) return false;
	if(typeof(elem) == 'undefined' || elem === null) return false;
	var ball = elem.id;
	var loc = window.location.protocol+'//'+window.location.host+dir+'/vote.php';
	new Ajax.Request(loc,{method:'post',parameters:{'imid':voted,'ball':ball}});
	alert('Ваш голос принят!');
}

// Проверка на запонение полей для комментария, а также генерация случайных символов для проверки
function CheckCommentText(){
	if(document.add_comment.comment_text.value == ""){
		alert("Вы не ввели текст комментария!");
	}
	else{
		if (document.add_comment.text.value != document.add_comment.textext.value){
			alert('Код введен не верно!');
			return false;
		}
		var vkp = Math.round(Math.random()*10000);
		document.add_comment.textext.value = vkp;
		document.add_comment.text.value = '';
		setTimeout(function(){document.add_comment.comment_text.value = '';}, 1000);
	}
}

// Очистка заполненого поля на пустое
var cfb;
function clearfeald(element){
	cfb = element.value;
	element.value = '';
}
function clearfealdblur(eb){
	if(eb.value == '' || eb.value == ' ' || eb.value == '  '){
		eb.value = cfb;
	}
}

// Проверка на заполнение полей для обратной связи
function conform(){ 
	var fght = document.getElementById('idmail');
	var fght2 = document.getElementById('comment_text');
	if(fght.value == 'Контактный E-mail'){
		alert('Введите свой e-mail');
		return false;
	}
	if(fght2.value == '' || fght2.value == ' ' || fght2.value == '  '){
		alert('Введите текст сообщения');
		return false;
	}
}