

function checkData(edit)
{
	
	var result = true;
	
	if(!checkEmpty(document.getElementById('surname')))
	{
		alert("Вы не ввели фамилию.");
		return false;
	}
	if(!checkEmpty(document.getElementById('name')))
	{
		alert("Вы не ввели имя.");
		return false;
	}
	if(edit != 1)
	{
	login = document.getElementById('input_login').value;
	
	login = login.replace("/^\s+|\s+$/g", '');
	
	if((/^[0-9a-zA-Z_]*$/).test(login))
	{
		if(login == "")
		{
			alert("Введите логин");
			document.getElementById('login').focus();
			return false;
		}
		JsHttpRequest.query(
	            'ajax.php', // backend
	            {
	                'mode': 'isset_login',  
	                'login' : login
	            },
	            function(result, errors) 
	            {  
	                  	
	            	if(result.isset == '1')
	            	{
	            		alert("Этот логин уже занят");
	            		document.getElementById('login').focus();
	            		return false;
	            	}
	            	else
	            	{
	            		var password = document.getElementById('password_insert').value;
	            		var password_repeat = document.getElementById('password_repeat').value;
	            		
	            		
	            		if(password == login)
	            		{
	            			alert("Пароль не должен совпадать с логином!");
	            			document.getElementById('password').focus();
	            			return false;
	            		}
	            		pass_result = checkPassword(password);
	            		if(!pass_result)
	            		{
	            			return false;
	            		}
	            		
	            		if(password != password_repeat)
	            		{
	            			alert("Вы неправильно повторили пароль!");
	            			document.getElementById('password_repeat').focus();
	            			return false;
	            		}
	            		
	            		
	            	
						if(document.getElementById('email').value == "")
						{
							alert("Вы не ввели e-mail.");
							document.getElementById('email').focus();
								return false;
						}
						else
						{		
							if(!checkEmail(document.getElementById('email').value))
							{
								alert("Введите корректный e-mail!");
								document.getElementById('email').focus();
								result = false;
							}
						}
						if(document.getElementById('phone').value == "" || document.getElementById('phone').value == "+380")
						{
							alert("Введите номер телефона.");
							document.getElementById('phone').focus();
								return false;
						}
						else
						{
							if(!checkPhone(document.getElementById('phone').value))
							{
								alert("Введите корректный номер телефона!");
								document.getElementById('phone').focus();
								return false;
							}
						}
						if(document.getElementById('phone2').value != "")
						{
							if(!checkPhone(document.getElementById('phone2').value))
							{
								alert("Введите корректный номер телефона!");
								document.getElementById('phone2').focus();
								return false;
							}
						}						
						if(!checkEmpty(document.getElementById('country')))
						{
							alert("Вы не ввели страну.");
							return false;
						}
						if(!checkEmpty(document.getElementById('city')))
						{
							alert("Вы не ввели населенный пункт.");
							return false;
						}
						
						if(!checkEmpty(document.getElementById('street')))
						{
							alert("Вы не ввели улицу.");
							return false;
						}
						if(!checkEmpty(document.getElementById('house')))
						{
							alert("Вы не ввели номер дома.");
							return false;
						}
						
						
						document.getElementById('registration_form').submit();
						
						return true;
						
						
	            	}
	            	
	            },
	            true  
	        );
	}
	else
	{
		alert("Логин может состоять только из цифр, латинских букв и нижнего подчеркивания.");
		document.getElementById('login').focus();
		return false;
	}
	}
	else
	{
		if(document.getElementById('email').value == "")
						{
							alert("Вы не ввели e-mail.");
							document.getElementById('email').focus();
								return false;
						}
						else
						{		
							if(!checkEmail(document.getElementById('email').value))
							{
								alert("Введите корректный e-mail!");
								document.getElementById('email').focus();
								result = false;
							}
						}
						if(document.getElementById('phone').value == "" || document.getElementById('phone').value == "+380")
						{
							alert("Введите номер телефона.");
							document.getElementById('phone').focus();
								return false;
						}
						else
						{
							if(!checkPhone(document.getElementById('phone').value))
							{
								alert("Введите корректный номер телефона!");
								document.getElementById('phone').focus();
								return false;
							}
						}
						if(document.getElementById('phone2').value != "")
						{
							if(!checkPhone(document.getElementById('phone2').value))
							{
								alert("Введите корректный номер телефона!");
								document.getElementById('phone2').focus();
								return false;
							}
						}						
						if(!checkEmpty(document.getElementById('country')))
						{
							alert("Вы не ввели страну.");
							return false;
						}
						if(!checkEmpty(document.getElementById('city')))
						{
							alert("Вы не ввели населенный пункт.");
							return false;
						}
						
						if(!checkEmpty(document.getElementById('street')))
						{
							alert("Вы не ввели улицу.");
							return false;
						}
						if(!checkEmpty(document.getElementById('house')))
						{
							alert("Вы не ввели номер дома.");
							return false;
						}
						
						
						document.getElementById('registration_form').submit();
						
						return true;
	}
	
	
	
	return result;
}

function checkEmail(email)
{

 	email = email.replace("/^\s+|\s+$/g", '');
 	return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
}

function checkPhone(phone)
{
	
	phone = phone.replace("/^\s+|\s+$/g", '');
	return (/^[0-9+()-]*$/).test(phone);
}

function checkPassword(password)
{		
	var reg = /^[0-9a-zA-Z]{1,}$/;
	if (password.length <6) {
		alert("Пароль должен содержать не менее 6-ти латинских символов и/или цифр!");		
		element.focus();        
        return false;        
    }else if(!reg.test(password)){
		alert("Пароль может состоять только из латинских букв и цифр!");		
		element.focus();        
        return false;        
	}else{
    	return true;
    }
}

function trim(string)
{
	return string.replace(/(^\s+)|(\s+$)/g, "");
}

function checkEmpty(el)
{
	value = el.value;
	value = value.replace("/^\s+|\s+$/g", '');
	if(value == "")
	{
		el.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function checkLogin(login)
{
	var res;
	login = login.replace("/^\s+|\s+$/g", '');
	if((/^[0-9a-zA-Z_]*$/).test(login))
	{
		JsHttpRequest.query(
	            'ajax.php', // backend
	            {
	                'mode': 'isset_login',  
	                'login' : login
	            },
	            function(result, errors) 
	            {  
	            	
	            	if(result.isset == '1')
	            	{
	            		alert("Этот логин уже занят");
	            		res = false;
	            	}
	            	else
	            	{
	            		res =  true;
	            	}
	            	return res;
	            },
	            true  
	        );
	}
	else
	{
		alert("Логин может состоять только из цифр, латинских букв и нижнего подчеркивания.");
		res = false;
		return res;
	}
}
