function toggle(id) {
	var state = document.getElementById(id).style.display;
	if (state == 'none') {
		document.getElementById(id).style.display = 'block';
	} else
	if (state == 'block') {
		document.getElementById(id).style.display = 'none';
	}
}
function turn_on(element) {
	document.getElementById(element).style.display = 'block';
}
function turn_off(element) {
	document.getElementById(element).style.display = 'none';
}
function checkRegMail(LE,domain,flag) {
	if (flag) {
		document.getElementById('membermail_a').style.display = 'none';
		document.getElementById('membermail_b').style.display = 'none';
		document.getElementById('membermail_c').style.display = 'none';
		document.getElementById('membermail_d').style.display = 'none';
		document.getElementById('projectemail1').style.display = 'table-cell';
		document.getElementById('projectemail2').style.display = 'block';
		document.getElementById('projectemail3').style.display = 'block';
		document.getElementById('note1').style.display = 'none';
		document.getElementById('note2').style.display = 'block';
		document.getElementById('errors_field').innerHTML = '';
		document.getElementById('errors_field2').innerHTML = '';
		return false;
	}
	if (!LE) {
		(domain) ? alert("Введите E-Mail (до знака @) !") : alert('Введите E-Mail !');
		return false;
	}
	var string = (domain) ? LE+'@'+domain : LE;
	JsHttpRequest.query('/lib/JsHttpRequest/checkLogin.php', { 'AJAX_email' : string }, function(result, errors) {
		if (errors) {
			alert(errors);
			return false;
		}
		if (!result) {
			document.getElementById((domain)?'errors_field2':'errors_field').innerHTML = '<span style="color:green;"><nobr><strong> "'+string+'" </strong> не занят!</nobr></span>';
		} else {
			(domain)
				? document.getElementById((domain)?'errors_field2':'errors_field').innerHTML = '<span style="color:red;"><nobr><strong> "'+string+'" </strong> занят!</nobr></span>'
				: document.getElementById('password_field').innerHTML = '<div class="pwdf"><nobr>Введённый E-Mail (<strong>'+string+'</strong>) уже используется в системе.<br />Введите ниже пароль, присвоенный введёному E-Mail`у.<br /><input type="password" name="pswd" value="" id="pswd" />&nbsp;'+"<button onclick=\"checkRegPassword('"+string+"');return false;\">вперёд</button><button onclick=\"document.getElementById('password_field').outerHTML='<div id=password_field></div>';return false;\">Отмена</button></nobr></div><br />";
			(domain) ? null : document.getElementById('pswd').focus();
		}
	},true);
}

function checkRegPassword(email) {
	var password = document.getElementById('pswd').value;
	if ( password=="" ) {
		document.getElementById('errors_field').innerHTML = '<span style="color:red;"><nobr>E-Mail пустым быть не может!</nobr></span>';
		return;
	}
	JsHttpRequest.query('/lib/JsHttpRequest/checkPassword.php', { 'AJAX_email' : email, 'AJAX_password' : password, 'AJAX_password_hashed' : false }, function(result, errors) {
		if (errors) {
			alert(errors);
			return false;
		}
		if ( result ) {
			document.getElementById('form_password').value	= password;
			document.getElementById('form_email').value		= email;
			document.forms['login'].submit();
		} else {
			document.getElementById('errors_field').innerHTML = '<span style="color:red;"><nobr>Пароль неверен!</nobr></span>';
			document.getElementById('password_field').innerHTML = '';
			return;
		}
	},true);
}

function checkSubmitLogin(form) {
	var Stop = 0;
	var msg = '';
	if (document.getElementById('reg_nick').value=="") {
		Stop = 1;
		msg += "Укажите псевдоним!\n";
	}
	if (document.getElementById('reg_mail').value=="") {
		Stop = 1;
		msg += "Укажите E-Mail!\n";
	}
	if (document.getElementById('reg_password').value=="") {
		Stop = 1;
		msg += "Укажите пароль!\n";
	} else if (document.getElementById('reg_password2').value=="") {
		Stop = 1;
		msg += "Укажите проверочный пароль!\n";
	} else if (document.getElementById('reg_password').value != document.getElementById('reg_password2').value) {
		Stop = 1;
		msg += "Пароли не совпадают!\n";
	}
	if (Stop) alert(msg);
	else form.submit();
}

function checkPassport(form) {
	var Stop = 0;
	document.getElementById('errors_field').innerHTML = '';
	document.getElementById('errors_field').innerHTML +=  '';
	if (document.getElementById('passport_nick').value=="") {
		Stop = 1;
		document.getElementById('errors_field').innerHTML += 'Псевдоним не может быть пустым!\n';
	}
	if (document.getElementById('passport_mail').value=="") {
		if (document.getElementById('passport_email').value=="") {
			Stop = 1;
			document.getElementById('errors_field').innerHTML += 'Укажите E-Mail!\n';
		}
	}
	if (!Stop) {
		document.getElementById('errors_field').innerHTML += 'Сохраняю... Пожалуйста подождите.\n';
		form.submit();
	}

}

function checkProfile(form) {
	var Stop = 0;
	document.getElementById('errors_field').innerHTML = '';
	if (document.getElementById('profile_nick').value=="") {
		if (document.getElementById('passport_nick').value!="" && !confirm('Псевдоним не может быть пустым !nХотите подставить псевдоним из Паспорта?')) {
			Stop = 1;
			document.getElementById('errors_field').innerHTML += 'Укажите псевдонип Профиля!\n';
		} else {
			document.getElementById('profile_nick').value = document.getElementById('passport_nick').value;
			if (confirm('Псевдоним вставлен из Пасспорта.\nПродолжить сохранение?')) form.submit();
			else {
				Stop = 1;
			}
		}
	}
	if (!Stop) {
		document.getElementById('errors_field').innerHTML += 'Сохраняю... Пожалуйста подождите.\n';
		form.submit();
	}
}

function checkPassword(form, email) {
	var Stop = 0;
	var password = document.getElementById('password_old').value;
	document.getElementById('errors_field').innerHTML = '';
	if (document.getElementById('password_old').value=="") {
		Stop = 1;
		document.getElementById('errors_field').innerHTML += '<span style="color:red;">Укажите старый пароль!</span><br />';
	}
	if (document.getElementById('password_new').value=="") {
		Stop = 1;
		document.getElementById('errors_field').innerHTML += '<span style="color:red;">Укажите новый пароль!</span>';
	}
	if (!Stop) {
		JsHttpRequest.query(
			'/lib/JsHttpRequest/checkPassword.php', {
				'AJAX_email' : email,
				'AJAX_password' : password,
				'AJAX_password_hashed' : false
			},
			function(result, errors) {
				if (errors) {
					alert(errors);
					return false;
				}
				if ( result ) {
					document.getElementById('errors_field').innerHTML = '<span style="color:green;">Меняю пароль...</span>';
					return form.submit();
				} else {
					document.getElementById('errors_field').innerHTML = '<span style="color:red;">Пароль неверен!</span>';
					return false;
				}
			},true
		);
	}
}

function getCountry(id) {
	if (!id) return false;
	JsHttpRequest.query('/lib/JsHttpRequest/getCountries.php', { 'dummy' : true }, function(result, errors) {
		if (errors) {
			alert(errors);
			return false;
		}
		if (result) {
			document.getElementById('country').innerHTML = '';
			var option = document.createElement('option');
			var optionText = document.createTextNode('теперь выберите страну');
			option.appendChild(optionText);
			document.getElementById('country').appendChild(option);
			for(i=0; i<result['options'].length; i++) {
				var option = document.createElement('option');
				var optionText = document.createTextNode(result['options'][i][1]);
				option.appendChild(optionText);
				option.setAttribute('value',result['options'][i][0]);
				if (result['options'][i][0] == id) option.setAttribute('selected','selected');
				document.getElementById('country').appendChild(option);
			}
		}
	},true);
	//getRegion(id,id);
}

function getRegion(country_id, selected_region, lock) {
	if (!country_id) return false;
	JsHttpRequest.query('/lib/JsHttpRequest/getRegion.php', { 'AJAX_country_id' : country_id }, function(result, errors) {
		if (errors) {
			alert(errors);
			return false;
		}
		document.getElementById('region').disabled = (lock)?'true':'';
		document.getElementById('region').innerHTML = '';
		var option = document.createElement('option');
		var optionText = document.createTextNode('теперь выберите регион');
		option.appendChild(optionText);
		document.getElementById('region').appendChild(option);
		for(i=0; i<result['options'].length; i++) {
			var option = document.createElement('option');
			var optionText = document.createTextNode(result['options'][i][1]);
			option.appendChild(optionText);
			option.setAttribute('value',result['options'][i][0]);
			if (result['options'][i][0] == selected_region) option.setAttribute('selected','selected');
			document.getElementById('region').appendChild(option);
		}
	},true);
}

function getCity(region_id, selected_city, lock) {
	if (!region_id) return false;
	JsHttpRequest.query('/lib/JsHttpRequest/getCity.php', { 'AJAX_region_id' : region_id }, function(result, errors) {
		if (errors) {
			alert(errors);
			return false;
		}
		document.getElementById('passport_city').disabled = (lock)?'true':'';
		document.getElementById('passport_city').innerHTML = '';
		var option = document.createElement('option');
		var optionText = document.createTextNode('теперь выберите город');
		option.appendChild(optionText);
		document.getElementById('passport_city').appendChild(option);
		if(result['options']) {
			for(i=0;i<result['options'].length; i++) {
				var option = document.createElement('option');
				var optionText = document.createTextNode(result['options'][i][1]);
				option.appendChild(optionText);
				option.setAttribute('value',result['options'][i][0]);
				if (result['options'][i][0] == selected_city) option.setAttribute('selected','selected');
				document.getElementById('passport_city').appendChild(option);
			}
		}
	},true);
}

function makeCountry(user_id) {
	document.getElementById('makecountry').innerHTML = "<fieldset style=\"width:1%;\"><legend>Новая Страна для списка стран</legend><nobr><input type=\"text\" name=\"newCountry\" id=\"newCountry\" value=\"\" /><button onclick=\"if(document.getElementById('newCountry').value!='' && confirm('Вы уверены, что хотите добавить новую страну в список стран?')) addCountry(document.getElementById('newCountry').value,"+user_id+");document.getElementById('makecountry').innerHTML='';return false;\">Добавить</button><button onclick=\"document.getElementById('makecountry').innerHTML=''\">Закрыть</button></nobr></fieldset>";
}

function makeRegion(user_id) {
	document.getElementById('makeregion').innerHTML = "<fieldset style=\"width:1%;\"><legend>Новый Регион для списка регионов</legend><nobr><nobr><input type=\"text\" name=\"newRegion\" id=\"newRegion\" value=\"\" /><button onclick=\"if(document.getElementById('newRegion').value!='' && confirm('Вы уверены, что хотите добавить новый регион в список регионов?')) addRegion(document.getElementById('newRegion').value,document.getElementById('country').value,"+user_id+"); document.getElementById('makeregion').innerHTML = ''; return false;\">Добавить</button><button onclick=\"document.getElementById('makeregion').innerHTML=''\">Закрыть</button><br></nobr></fieldset>";
}

function makeCity(user_id) {
	document.getElementById('makecity').innerHTML = "<fieldset style=\"width:1%;\"><legend>Новый Город для списка городов</legend><nobr><nobr><input type=\"text\" name=\"newCity\" id=\"newCity\" value=\"\" /><button onclick=\"if(document.getElementById('newCity').value!='' && confirm('Вы уверены, что хотите добавить новый город в список городов?')) addCity(document.getElementById('newCity').value,document.getElementById('country').value,document.getElementById('region').value,"+user_id+"); document.getElementById('makecity').innerHTML=''; return false;\">Добавить</button><button onclick=\"document.getElementById('makecity').innerHTML=''\">Закрыть</button><br></nobr></fieldset>";
}

function addCountry(input,user_id) {
	if (!input) return false;
	JsHttpRequest.query('/lib/JsHttpRequest/addCountry.php', { 'AJAX_input' : input, 'AJAX_user_id' : user_id }, function(result, errors) {
		if (errors) {
			alert(errors);
			return false;
		}
		getCountry(result);
	},true);
}

function addRegion(input,country_id,user_id) {
	if (!input) {
		return false;
	} else if (!country_id) {
		alert('Сначала выберите страну !');
		return false;
	}
	JsHttpRequest.query('/lib/JsHttpRequest/addRegion.php', { 'AJAX_input' : input, 'AJAX_country_id' : country_id, 'AJAX_user_id' : user_id }, function(result, errors) {
		if (errors) {
			alert(errors);
			return false;
		}
		getRegion(country_id, result);
	},true);
}

function addCity(input,country_id,region_id,user_id) {
	if (!input) {
		return false;
	} else if (!country_id) {
		alert('Сначала выберите страну !');
		return false;
	} else if (!region_id) {
		alert('Сначала выберите регион !');
		return false;
	}
	JsHttpRequest.query('/lib/JsHttpRequest/addCity.php', { 'AJAX_input' : input, 'AJAX_country_id' : country_id, 'AJAX_region_id' : region_id, 'AJAX_user_id' : user_id }, function(result, errors) {
		if (errors) {
			alert(errors);
			return false;
		}
		getCity(region_id,result);
	},true);
}

function isEmail(sEmail){
	sEmail = sEmail.replace( new RegExp('/\(.*?\)/'), '' );
	oRegExp = /^[A-Za-z0-9_][-\w]*([\.|\+][A-Za-z0-9_+][-\w]*)*@[A-Za-z0-9][-\w]*(\.[A-Za-z0-9][-\w]*)*\.[a-zA-Z]{1,4}$/;
	return oRegExp.test(sEmail);
}

function menu(idmenu){
   id = document.getElementById('rhb');
   if (idmenu == 'ya'){
     document.getElementById('tdya').className="ms_on";
     document.getElementById('tdwe').className="we";
     document.getElementById('tdkonk').className="konk";
     document.getElementById('ya').style.display="";
     document.getElementById('we').style.display="none";
     document.getElementById('konk').style.display="none";
     document.getElementById('tdya').innerHTML = '<div><div><a href="javascript:void(0);" onclick="javascript:menu(\'ya\');"><img src="/images/ya_sub.gif" border="0" width="114" height="51" alt=""></a></div></div>';
     document.getElementById('tdwe').innerHTML = '<a href="javascript:void(0);" onclick="javascript:menu(\'we\')"><img style="margin-top:19px" src="/images/we.gif" border="0" width="114" height="52" alt=""></a>';
     document.getElementById('tdkonk').innerHTML = '<a href="javascript:void(0);" onclick="javascript:menu(\'konk\')"><img style="margin-top: 10px;" src="/images/konk.gif" border="0" width="136" height="56" alt=""></a>';
   }
   else if (idmenu == 'we'){
     document.getElementById('tdya').className="ya";
     document.getElementById('tdwe').className="ms_on";
     document.getElementById('tdkonk').className="konk";
	 document.getElementById('ya').style.display="none";
	 document.getElementById('we').style.display="";
	 document.getElementById('konk').style.display="none";
 	 document.getElementById('tdya').innerHTML = '<a href="javascript:void(0);" onclick="javascript:menu(\'ya\');"><img style="margin-top:5px" src="/images/ya.gif" border="0" width="139" height="58" alt=""></a>';
	 document.getElementById('tdwe').innerHTML = '<div><div><a href="javascript:void(0);" onclick="javascript:menu(\'we\')"><img src="/images/we_sub.gif" border="0" width="114" height="52" alt=""></a></div></div>';
	 document.getElementById('tdkonk').innerHTML = '<a href="javascript:void(0);" onclick="javascript:menu(\'konk\')"><img style="margin-top: 10px;" src="/images/konk.gif" border="0" width="136" height="56" alt=""></a>';
   }
   else if (idmenu == 'konk'){
    document.getElementById('tdya').className="ya";
    document.getElementById('tdwe').className="we";
    document.getElementById('tdkonk').className="ms_on";
	document.getElementById('ya').style.display="none";
    document.getElementById('we').style.display="none";
    document.getElementById('konk').style.display="";
    document.getElementById('tdya').innerHTML = '<a href="javascript:void(0);" onclick="javascript:menu(\'ya\');"><img style="margin-top:5px" src="/images/ya.gif" border="0" width="139" height="58" alt=""></a>';
    document.getElementById('tdwe').innerHTML = '<a href="javascript:void(0);" onclick="javascript:menu(\'we\')"><img style="margin-top:19px" src="/images/we.gif" border="0" width="114" height="52" alt=""></a>';
    document.getElementById('tdkonk').innerHTML = '<div><div><a href="javascript:void(0);" onclick="javascript:menu(\'konk\')"><img src="/images/konk_sub.gif" border="0" width="112" height="51" alt=""></a></div></div>';
   }
   return true;
}
