function $$(id) { return document.getElementById(id); }
function createXMLHttpRequest(){
 try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
 try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
 try { return new XMLHttpRequest(); } catch (e) {}
 alert("Brak obslugi XMLHttpRequest");
 return null;
}

function getContent(url,id){
 var xhr = createXMLHttpRequest();
 xhr.onreadystatechange = function() {
  if (xhr.readyState==4){
   if (xhr.status==200){
    $$(id).innerHTML=xhr.responseText;
   } else {
    $$(id).innerHTML="wystapil blad";
   }
  }
 }
xhr.open("POST", url, true);
xhr.send(null);
}

function refresh() { window.location.reload(true); }
function LogReg(a){
 if(a==1){
 $$("logging").className="active_button";
 $$("register").className="";
 getContent("/log.html","lr");
 }
 if(a==2){
 $$("logging").className="";
 $$("register").className="active_button";
 getContent("/reg.html","lr");
 }
}


function check(what){
 var response;
 var value=$$(what).value;
if(value){
 var xhr = createXMLHttpRequest();
 xhr.onreadystatechange = function() {
  if (xhr.readyState==4){
   if (xhr.status==200){
     ins(xhr.responseText);
   } else {
    alert('bad');
   }
  }
 }
xhr.open("GET", "/register.php?check="+what+"&value="+value, true);
xhr.send(null);
	function ins(text){
	 var re = text.split("|");
	 if(re[0]=="1") $$(re[1]).className="green";
	 else $$(re[1]).className="red";
	 $$(re[1]).innerHTML=re[2];
	}
}
}

function enter(v) { if(v.keyCode==13) register(); }
function enter2(v) { if(v.keyCode==13) login(); }

function register(){
  $$("lrinfo").innerHTML='<img src="/img/ajax.gif" alt="ładowanie..." />';
  var login = $$("login").value;
  var pass = $$("pass").value;
  var email = $$("email").value;
  var xhr = createXMLHttpRequest();
  xhr.open("post", "/register.php", true);
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

  xhr.send("login="+login+"&pass="+pass+"&email="+email);
  xhr.onreadystatechange = function(){
  	if(xhr.readyState==4){
  		if(xhr.status==200) ins(xhr.responseText);
  		else alert("Rejestracja nie powiodła się.");
	}
 }
  //xhr.setRequestHeader("Connection", "close");
  function ins(text){
 	var re = text.split("|");
    if(re[0]=="1") $$("lr").innerHTML=re[1];
    else $$("lrinfo").innerHTML="Proszę poprawnie wypełnić wszystkie pola.";
  }
}

function login(){
	var login = $$("login").value;
	var pass = $$("pass").value;
	var remember = "no";
	if($$("remember").checked==true) remember="yes";
	$$("lrinfo").innerHTML='<img src="/img/ajax.gif" alt="ładowanie..." />';
	var xhr = createXMLHttpRequest();
	xhr.open("post","/logging.php",true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Connection", "close");
	xhr.onreadystatechange = function(){
		if(xhr.readyState==4){
			if(xhr.status==200) ins(xhr.responseText);
			else alert("Wystąpił błąd podczas logowania.");
		}
	}
	xhr.send("login="+login+"&pass="+pass+"&remember="+remember);
	function ins(text){
	  if(text=="1") location.reload(true);
	  else if(text=="0") $$("lrinfo").innerHTML="Nieprawidłowe dane.";
	  else if(text=="2") $$("lrinfo").innerHTML="Nie potwierdziłeś swojego emaila.<br /><b>Sprawdź pocztę</b> i kliknij w link aktywacyjny.";
	  else $$("lrinfo").innerHTML="Proszę wpisać swój login oraz hasło.";
	}
}

function logout(){
	var xhr = createXMLHttpRequest();
	xhr.open("get","/logging.php?logout=true",true);
	xhr.send(null);
	xhr.onreadystatechange = function (){
		if(xhr.readyState==4){
			if(xhr.status==200) if(xhr.responseText==1) window.location.reload(true);
			else alert("Błąd podczas wylogowania.");
		}
	}
}

function sign(what){
	var xhr = createXMLHttpRequest();
	xhr.open("get","/sign.php?w="+what,true);
	xhr.send(null);
	xhr.onreadystatechange = function (){
		if(xhr.readyState==4){
			if(xhr.status==200) $$("sign").innerHTML=xhr.responseText;
		}
	}
setTimeout("refresh()",1000);
}

function art(id,act){
	var xhr=createXMLHttpRequest();
	xhr.open("get","/art.php?id="+id+"&act="+act,true);
	xhr.send(null);
	xhr.onreadystatechange = function(){
		if(xhr.readyState==4){
			if(xhr.status==200){
				if(xhr.responseText==1) $$("pub"+id).innerHTML="zgłoszone";
				else if(xhr.responseText==2) $$("del"+id).innerHTML='<span style="font-weight:bold;color:red;">artykuł usunięto</span>';
			}
		}
	}
}
