function getHTTPObject() {
  var xmlhttp;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
     try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     } catch (E) {
        xmlhttp = false;
     }
  }

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }

  return xmlhttp;
}
var theURL = "login.php";
var zURL = "z.php";
var http = getHTTPObject(); 

function handleHttpResponse() {
  if (http.readyState == 4) {
    var result = http.responseText.split('|');
    if(parseInt(result[0]) > 0){
		document.getElementById('logindiv').innerHTML = '<label id="lbl"><strong>Вы вошли как: </strong>'+result[2]+'</label><br /><a href="http://berega.crimea.ua/client.php">персональная страница</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:doLogout();">Выход</a>';
    }else{
	document.getElementById('logindiv').innerHTML ='<label id="lbl"><strong>Логин</strong><input id="login" name="login" type="text" size="10" onfocus="ClearFields()"/></label><label id="lbl"><strong>Пароль</strong><input id="pswd" name="pswd" type="password" size="10" onfocus="ClearFields()"/></label><a href="javascript:doLogin();"><img src="images/go.gif" width="20" height="20" border="0" alt="Вход"/></a>';
       document.getElementById('msgbox').innerHTML = "Неправильный логин или пароль, <a href='password.php'>восстановить?</a>";
    }
  }else{
    document.getElementById('logindiv').innerHTML = "Ваши данные проверяются...";
  }
}

function doLogin() {
  var lValue = document.getElementById("login").value;
  var pValue = document.getElementById("pswd").value;
  http.open("POST", theURL, true);
  http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  http.onreadystatechange = handleHttpResponse;
  http.send("login=" + escape(lValue) + "&pswd=" + escape(pValue));
}
function doLogout() {
  http.open("POST", 'logout.php', true);
  http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  http.onreadystatechange = handleLogoutResponse;
  http.send("");
}

// begin zapros
function SendZapros(data) {
  http.open("POST", zURL, true);
  http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  http.onreadystatechange = handleZaprosResponse;
  http.send(data);
}

function handleZaprosResponse() {
  if (http.readyState == 4) {
    var result = http.responseText;
    if(result == 1){
	 	document.getElementById("zaprospans").style.backgroundColor="#fff"; 
		document.getElementById("zaprospans").style.paddingTop="100px";
		document.getElementById("zaprospans").style.textAlign="center";
		document.getElementById("zaprospans").style.height="200px";
		document.getElementById('zaprospans').innerHTML = '<h2>Ваш запрос успешно отправлен</h2><p class=text>Ваш запрос будет обработан в ближайшее время. Менеджер свяжется с Вами по электронной почте или по телефону</p>';
    }else{
	 	document.getElementById("msg").style.backgroundColor="#e8e8e8"; 
		document.getElementById("msg").style.textAlign="center";
	document.getElementById('msg').innerHTML ='Запрос не был отправлен. Пожалуйста, повторите  попытку через несколько минут.';
    }
  }else{
    document.getElementById('msg').innerHTML = "Идет отправка запроса...";
  }
}

// end of zapros

function handleLogoutResponse() {
  if (http.readyState == 4) {
	//document.getElementById('logindiv').innerHTML ='<label id="lbl"><strong>Логин</strong><input id="login" name="login" type="text" size="10" onfocus="ClearFields()"/></label><label id="lbl"><strong>Пароль</strong><input id="pswd" name="pswd" type="password" size="10" onfocus="ClearFields()"/></label><a href="javascript:doLogin();"><img src="images/go.gif" width="20" height="20" border="0" alt="Вход"/></a>';
	  window.location.href = "http://berega.crimea.ua/index.php";
  }else{
    document.getElementById('logindiv').innerHTML = "Выходим...";
  }
}


function hideLoginDiv() {
  if(document.getElementById) {
    document.getElementById('logindiv').style.visibility = 'hidden'; 
    document.getElementById('logindiv').style.display= 'none'; 
  }else{
    document.logindiv.visibility = 'hidden'; 
    document.logindiv.display= 'none'; 
  }
}
function showLoginDiv() {
  if(document.getElementById) {
    document.getElementById('logindiv').style.visibility = 'visible'; 
    document.getElementById('logindiv').style.display= 'block'; 
  }else{
    document.logindiv.visibility = 'visible'; 
    document.logindiv.display= 'block'; 
  }
}
function ClearFields(){
javascript:document.getElementById('msgbox').innerHTML='';
}