
var arPostfixes = new Array();
var sUserInfo;
var sUserLogin;
var sLoginFunction; 


function setAfterLoginFunc(sFunc) {
	sLoginFunction = sFunc;
}

function activateLoginForm(sPostfix) {
	var oButton = document.getElementById("buttonZaloguj" + sPostfix);
	var oForm = document.getElementById("formularzLogowania" + sPostfix);
//	var oSubmit = document.getElementById("buttonSubmit" + sPostfix);
	if(bIsIE) {
//		addEventHandler(oForm, "submit", test);
//		addEventHandler(oSubmit, "submit", test);
		addEventHandler(oForm, "submit", login);
	}
	else
	addEventHandler(oForm, "submit", login);
//	addEventHandler(oForm, "submit", preventDefaultEvent);
	addEventHandler(oButton, "click", login);
	oButton.sPostfix = sPostfix;
	oForm.sPostfix = sPostfix;
	arPostfixes.push(sPostfix);
}

function test(oEvent) {
	alert("Logujesz się do serwisu...");
	return true;
}

function deactivateLoginForm(sPostfix) {
	var oButton = document.getElementById("buttonZaloguj" + sPostfix);
	removeEventHandler(oButton, "click", login);
}

function modyfikujBloczekStatyczny() {
	var oLoginDiv = document.getElementById("bloczekLogowaniaStatyczny0");
	oLoginDiv.innerHTML = sUserInfo;

	if(sLoginFunction)
		eval(sLoginFunction + "()");
}

function modyfikujBloczekUkryty() {
 	hideLoginBlock(null);

	var oLogin = document.getElementById("zalogujSie");

	var oLoginDiv = oLogin.parentNode.parentNode;
	oLoginDiv.parentNode.removeChild(oLoginDiv);
}

function login(oEvent)
{
  preventDefaultEvent(oEvent);
 
  var oEventSrc = getEventSrc(oEvent);
//  oEventSrc = oEventSrc.parentNode.parentNode.sPostfix;
//  	alert(oEventSrc.parentNode.parentNode.id + "--" + oEventSrc.parentNode.parentNode.sPostfix);
  var sPostfix = oEventSrc.sPostfix;
  if(!sPostfix)
  	sPostfix = "Statyczny";

  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
	var oLogin = document.getElementById("login" + sPostfix);
	//alert(oLogin);
	var oPassword = document.getElementById("password" + sPostfix);

	if(trim(oLogin.value)) {
		for(var i=0; i<arPostfixes.length; i++) {
			showProgress(true, "bloczekLogowania" + arPostfixes[i] + "Postep", "bloczekLogowania" + arPostfixes[i] + "Normalny");
		}
	    xmlHttp.open("POST", "/login.php", true);  
	    xmlHttp.onreadystatechange = getLoginResults;
	    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	    xmlHttp.send("login=" + oLogin.value + "&password=" + oPassword.value + 
    			"&postfix=" + sPostfix + "&backUrl=" + location.pathname);
	}
	else {
		loginFailed(1, sPostfix);		
	}
  }
  else
    // jeśli połączenie jest zajęte, ponawia próbę po 1 sekundzie
    setTimeout('login(oEvent)', 1000);
  
  return false;
}

function getLoginResults() 
{
  if (xmlHttp.readyState == 4)  {
    if (xmlHttp.status == 200)  {

		var sResponse = xmlHttp.responseText;
	 	var arResponse = sResponse.split("\n");	
	  	if(arResponse[1].indexOf("ERROR") == -1) {
			sUserInfo = arResponse[7];
			sUserLogin = arResponse[1];
			for(var i=0; i<arPostfixes.length; i++) {
			 	eval("modyfikujBloczek" + arPostfixes[i] + "()");
			}
		}
		else {
	      loginFailed(arResponse[2], arResponse[0]); //error code, postfix
		}
		
		for(var i=0; i<arPostfixes.length; i++) {
			showProgress(false, "bloczekLogowania" + arPostfixes[i] + "Postep", "bloczekLogowania" + arPostfixes[i] + "Normalny");
		}
	} 
    else {
      loginFailed(3, oEventSrc.sPostfix);
    }
  }
}

function clearMessageBlock($sPostfix) {
	var oMessageBlock = document.getElementById("komunikat" + $sPostfix);
	oMessageBlock.parentNode.style.height = "";
	oMessageBlock.style.display = "";
	oMessageBlock.innerHTML = "" ;
}

function loginFailed(iErrorCode, $sPostfix) {
	var oMessageBlock = document.getElementById("komunikat" + $sPostfix);
	oMessageBlock.style.display = "block";

	iErrorCode = Number(iErrorCode);
	switch(iErrorCode) {
		case 1:
			if (sLanguage == "uk")
				oMessageBlock.innerHTML = "Please enter your Login.";
			else
				oMessageBlock.innerHTML = "Login nie został podany.";			
			break;
		case 2:
			if (sLanguage == "uk")
				oMessageBlock.innerHTML = "Submitted login and/or password is invalid.";
			else
				oMessageBlock.innerHTML = "Podano błędny login lub nieprawidłowe hasło.";			
			break;
		case 3:
			if (sLanguage == "uk")
				oMessageBlock.innerHTML = "Can't connect to server. Please try again.";
			else
				oMessageBlock.innerHTML = "Nie mogę nawiązać połączenia z serwerem.";			
		break;	

	}
}

function addLoginToAddCommentForm() {
	var oSignField = document.getElementById("formularzPodpis");
	oSignField.value = sUserLogin;
	oSignField.readOnly = "readonly";
	oSignField.style.cursor = "default";
}
