		function saveUser() {
			var err	= new Array();
			var errCode	= 0;
			$.each($("#register-form :input"),function() {  
				$(this).removeClass('error');
			}); 
			
			if ($('#login').val() == "") err.push('login');	
			if ($('#title').val() == "") err.push('title');	
			if ($('#email').val() == "") err.push('email');	
			if ($('#company').val() == "") err.push('company');	
			if ($('#position').val() == "") err.push('position');	

			if ((!$('#changepwd').val() || ($('#changepwd').val() && $('#changepwd').attr('checked'))) && ($('#pwd').val() == "") )	err.push('pwd');	
			if ((!$('#changepwd').val() || ($('#changepwd').val() && $('#changepwd').attr('checked'))) && ($('#confirmation').val() == "") )	err.push('confirmation');	

			if (err.length) errCode=1;
			if  (!errCode){
				if (!$('#changepwd') || ($('#changepwd') && $('#changepwd').attr('checked')))	
					if ($('#pwd').val() != $('#confirmation').val() ) {
						err.push('confirmation');	
						errCode=2;
					}
			}


		if (errCode==0){

					JsHttpRequest.query(
					'/include/js_back.php', // backend
					{
						// pass a text value 
						'a': 'saveuser',
						'title':$('#title').val(),		
						'form':document.getElementById('register-form')		
						
					},
					// Function is called when an answer arrives. 
					function(result, errors) {

						if (result["error"]>0){
							errCode=result["error"];
							showErrors(errCode,Array(),$('#error-message'));
						}else{
							if (result["nid"]==0){
								$('#error-message').html("Информация сохранена");
								$('#error-message').show();
								window.location.reload(location.href);

							}else{
								$('#error-message').html("Информация внесена в базу пользователей.<br>На Ваш Email васлано письмо с инструкцией по активации учетной записи.");
								$('#error-message').show();
//								$('#register-form').reset();
								$('#register-div').hide();

							}

						}
					},
					true  // do not disable caching
				);
			}
			if (errCode>0){
//				alert(errCode);
				showErrors(errCode,err,$('#error-message'));
			}
			delete err;
		}



		function emailPWD() {
			var err	= new Array();
			var errCode	= 0;
			$.each($("#register-form :input"),function() {  
				$(this).removeClass('error');
			}); 

			if ($('#email').val() == "") err.push('email');	
			if (err.length) errCode=1;

		if (errCode==0){

					JsHttpRequest.query(
					'/include/js_back.php', // backend
					{
						// pass a text value 
						'a': 'emailpwd',
						'email':$('#email').val()	
						
					},
					// Function is called when an answer arrives. 
					function(result, errors) {

						if (result["error"]>0){
							errCode=result["error"];
							showErrors(errCode,Array(),$('#error-message'));
						}else{
								$('#error-message').html("На Ваш Email выслано письмо с новым паролем и инструкцией по активации.");
								$('#error-message').show();
								//$('#register-form').reset();
						}
					},
					true  // do not disable caching
				);
			}
			if (errCode>0){
//				alert(errCode);
				showErrors(errCode,err,$('#error-message'));
			}
			delete err;
		}


function showErrors (errCode,err,div){

		if (errCode==1)	
			div.html("Не заполнены поля обязательные для заполнения !");
		else if (errCode==2)
			div.html("Неверное подтверждение пароля!");
		else if (errCode==3){
			div.html("Неверный формат поля email");
			err.push('email');
		}else if (errCode==4){
			div.html("Неверное значение поля проверки!");
			err.push('code');
		}else if (errCode==5){
			div.html("Пользователь с указанным email уже существует!Укажите другой email.");
			err.push('email');
		}else if (errCode==6){
			div.html("Не заполнены поля Логин и Пароль.");
		}else if (errCode==7){
			div.html("Неверный пароль или логин!");
		}else if (errCode==9){
			div.html("Пользователь с таким Email не найден.");
			err.push('email');
		}else if (errCode==11){
			div.html("Не выбрано мероприятие.");
		}else {};

		if (err.length)	
			$.each(err,function(item) {  
				$("#"+this).addClass('error') 
			}); 
		div.show();
}

 function changePwd(){
  if (document.getElementById("pwd").disabled==true){
		document.getElementById("pwd").disabled=false;
		document.getElementById("confirmation").disabled=false;
  }else{
		document.getElementById("pwd").disabled=true;
		document.getElementById("confirmation").disabled=true;
  }
 }
 
 function doLogin(){
			var err	= new Array();
			var errCode	= 0;
			$.each($("#login-form :input"),function() {  
				$(this).removeClass('error');
			}); 

			if ($('#u_login').val() == "" || $('#u_pass').val() == "") {
				$("#login-message").html("Не заполнены поля Логин и Пароль.");
				$("#login-message").show();
				return false;
			}	

		if (errCode==0){
					JsHttpRequest.query(
					'/include/js_back.php', // backend
					{
						// pass a text value 
						'a': 'dologin',
						'login':$('#u_login').val(),		
						'pwd':$('#u_pass').val(),
						'saveme':$('#saveme').attr('checked') 
					},
					// Function is called when an answer arrives. 
					function(result, errors) {
						if (result["error"]>0){
							$("login-message").html(result["message"]);
							$("login-message").show();
						}else{
							window.location.reload(window.location.href);
						}
					},
					true  // do not disable caching
				);
			}
			
			
			
			delete err;
}


 function doUnlogin(){
			if (window.confirm('Выйти?'))
			{
					JsHttpRequest.query(
					'/include/js_back.php', // backend
					{
						// pass a text value 
						'a': 'dounlogin'
					},
					// Function is called when an answer arrives. 
					function(result, errors) {
							$('#user-div').html(result["content"]);
							window.location.reload(window.location.href);
					},
					true  // do not disable caching
				);
			}
			return false;
}


function forgotPwd(){
	window.location.href='/forgotpw/';
}


function doRegister(){
	window.location.href='/register/';
}

function showForgotPW(){
			var strQuery = '';
			var x,y;
			x=300;y=150;


			$("#forgotpw").hide();
			m_top = document.body.scrollTop+document.body.clientHeight/2;
			m_left = (document.body.clientWidth-x)/2;
	
			$("#forgotpw").css("left",m_left);
			$("#forgotpw").css("top",m_top);	
			$("#forgotpw").show();
}

function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function showLoginForm(){
	

			m_top = f_scrollTop()+200;
			m_left = (document.body.clientWidth)/2-150;
	
//			$('#login-form').reset();
			$("#tip").css('left',m_left);
			$("#tip").css('top',m_top);	
			$("#tip").show();;
}

function hideLoginForm(){
			$("#tip").hide();
}

function delPhoto(){
 if (window.confirm("Удалить фотографию?")){
		JsHttpRequest.query(
			'/include/js_back.php', // backend
			{
				// pass a text value 
				'a': 'delphoto'
			},
			// Function is called when an answer arrives. 
			function(result, errors) {
				if (result["error"]==0)
					$('#photo').hide();
			},
			true  // do not disable caching
		);

	}	
}


