var req = new JsHttpRequest();

function valid_partner(){
	value=GEByID('reg');
	JsHttpRequest.query(
		'/modul/scripts/valid_partner.php',
		{"status": "ok", "q": value},
		function(result, errors) {
			//alert(errors);
			if (result) {
				if(result["ch_mail"] == ""){ //email прошёл проверку
					if(result["pres_mail"] == ""){ //такого адреса нет, возможна регистрация
						if (result["empty_field"] != ""){ // не все обязательные поля заполнены				
							arr=result['empty_field'];
							for(i=0; i<arr.length; i++){
								err_field_style(arr[i]);
							}
							$("#"+arr[0]+"").focus();
							alert("Заполните обязательные поля!");
						}
						else{
							if(result["err_pass"] != ""){ //пароль содержит недопустимые символы
								err_field_style("anketa_pass*");
								$("#anketa_pass*").val("");
								$("#anketa_pass*").focus();
								alert(result["err_pass"]);	
							}
							else{
								if(result["err_int_field"] != ""){ //неправильный формат числовых полей
									arr=result['err_int_field'];
									for(i=0; i<arr.length; i++){
										err_field_style(arr[i]);
									}
									$("#"+arr[0]+"").focus();
									alert("Неправильный формат данных!");
								}
								else 
									location.href='/reg/';
							}
						} 
					}
					else{
						err_field_style("anketa_mail*");
						$("#anketa_mail*").focus();
						alert(result["pres_mail"]);					
					}
				}
				else{
					err_field_style("anketa_mail*");
					$("#anketa_mail*").focus();
					alert(result["ch_mail"]);
				}
			}
		},
		false
	);	
	return;
}

function err_field_style(item){
	$("#"+item).css("border", "solid #f00 1px");
	$("#"+item).css("background-color", "#FFF2F2");
	$("#"+item).keypress(function(){
		$(this).css("border", "solid #D5C3A7 1px");
		$(this).css("background-color", "#fff");
	});	
}

function login_form(value){
	JsHttpRequest.query(
		'/modul/scripts/auth.php',
		{'status': 'login', 'q': value},
		function(result, errors) {
			location.href='/opt/';	
		},
		false
	);	
}

function logout(){
	JsHttpRequest.query(
		'/modul/scripts/auth.php',
		{'status': 'logout'},
		function(result, errors) {
			location.href='/';	
		},
		false
	);	
}

function rem_pass(value){
	JsHttpRequest.query(
		'/modul/scripts/auth.php',
		{'status': 'rem_pass', 'q': value},
		function(result, errors) {
			//alert(errors)
			location.href='/rem/';	
		},
		false
	);	
}

function edit_firm(){
	$("#msg").html("");
	value=GEByID('reg');
	JsHttpRequest.query(
		'/modul/scripts/edit_firm.php',
		{"status": "ok", "q": value},
		function(result, errors) {
			//alert(errors);
			if (result["empty_field"] != ""){ // не все обязательные поля заполнены				
				arr=result['empty_field'];
				for(i=0; i<arr.length; i++){
					err_field_style(arr[i]);
				}
				$("#"+arr[0]+"").focus();
				alert("Заполните обязательные поля!");
			}
			else{
				if(result["err_int_field"] != ""){ //неправильный формат числовых полей
					arr=result['err_int_field'];
					for(i=0; i<arr.length; i++){
						err_field_style(arr[i]);
					}
					$("#"+arr[0]+"").focus();
					alert("Неправильный формат данных!");
				}
				else
					$("#msg").html("Данные успешно сохранены.<br /><br />");
			}				
		},
		false
	);
}
