// Requer utils.js para funcionar
var http;
var loginForm = {
	validate: function() {
		if ($("user").value.isWhite()) {
			alert("O campo de usuario não pode estar em branco");
			$("user").focus();
			return false;
		}
		if ($("pass").value.isWhite()) {
		alert("O campo de senha não pode estar em branco!");
			$("pass").focus();
			return false;
		}
		http = getHTTPObject();
		return true;
	}
}
function onprocess() {
	$("login").onclick = "";
}

function onclear() {
	$("login").onclick = "if (loginForm.validate()) verificalogin();";
}

function verificalogin() {
	if (http && http.stateReady > 0 && http.stateReady < 4) {
		http.onreadystatechange = function() {}
		http.abort();
	}
	onprocess();
	var url = 'http://www.dialhost.com.br/dialadmin/publico/loga_cliente/';
	var user = document.getElementById('user').value;
	var pass = document.getElementById('pass').value;
	// envia este parametro caso queira grava numero de acesso e data de ultimo login.
	var registra_log = true;
	var $fullurl = 'user=' + user + '&pass=' + pass+ '&registra_log=' + registra_log;
	var anticache = "?anticache=" + new Date().getMilliseconds() + Math.random();

	http.open("POST", url + anticache, true);

	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Cache-Control", "no-cache");

	http.onreadystatechange = handleStateChange;
	http.send($fullurl);
}

function handleStateChange() {
	if (http.readyState == 4) {
		if(http.status == 200) {
			var resposta = http.responseText.split(":");
			if (resposta[1]=="A") {
				$("painel").action = "http://"+resposta[2]+":2082/login/";
				$("painel").submit();
			}
			else if ((resposta[1]=="B") || (resposta[1]=="C")) {
				$("painel").action = "http://www.dialhost.com.br/dialsuspenso/";
				$("painel").submit();
			}
			else {
				if (http.responseText == "0") {
					alert("Login ou Senha invalido! Por favor tente novamente.");
					$("pass").value = "";
					$("pass").focus();
					onclear();
					return false;
				}
				else {
					alert("Entre em contato com o nosso Atendimento. Erro: 999");
					processOnLoad();
					onclear();
					return false;
				}
			}
		}
		else {
			alert(http.statusText);
			onclear();
		}
	}
}

function getHTTPObject() {
var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'indefinido') {
		try
		{
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
		xmlhttp = false;
		}
	}
	return xmlhttp;
}