/*
	ABV 1.3
	AJAX BALOON VALIDATOR
	AUTOR: KRZYSZTOF SAMELKO
	CREATE DATE: 10-07-2008
	WERSJA TESTOWA- NIE POSIADA WSZYSTKICH FUNKCJONALNMOSCI

	DOPUSZCZALNE KOMBINACJE
	- POSITION = RELATIVE I BOXANIMATE = 0
	- POSITION = ABSOLUTE I BOXANIMATE = 1
*/

/*CONFIG BOX*/
var BoxAnimate = 0; //1 animacja on 0 animacja off
var TypAnimacji = 0; // 1 top -  0 to left
var Position = "absolute";
/*CONFIG BOX*/


var xmlHttp = createXmlHttpRequestObject(); 
var cache = new Array();
var test = new Array();
var Box = null;
var PhpFile = "include/walidacja_ajax.php";

var cache = new Array();

/*UCHWYT DLA SERWERA, SPRAWDZENIE Z JAKA PRZEGLADARKA MAMY DOCZYNIENIA*/
function createXmlHttpRequestObject() 
	{
  	var xmlHttp;
  	if(window.ActiveXObject)
  		{
    	try
    		{xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}
    	catch (e) 
    		{xmlHttp = false;}
  		}
	else
  		{
	    try 
    		{xmlHttp = new XMLHttpRequest();}
    	catch (e) 
    		{xmlHttp = false;}
  		}
  if (!xmlHttp)
    alert("Błąd podczas tworzenia obiektu");
  else 
    return xmlHttp;
    }	


/*glowna funkcja walidujaca*/
function ABV(fieldID)
	{
	if (xmlHttp)
		{
		if (fieldID)
            {
            atrybut = encodeURIComponent(document.getElementById(fieldID).name);
            wartosc = encodeURIComponent(document.getElementById(fieldID).value);

            currentField = atrybut; //ODNIESIENIE DLA NOWO WORZONEJ WARSTWY -- AKTUALNA WARSTWA
            fieldFailed = atrybut+"_failed"; //NAZWA DLA UTWORZENIA NOWEJ WARSTWY
    	
            /*dodanie wartosci do kolejki*/
            cache.push("atrybut="+atrybut+"&wartosc="+wartosc); //KOLEJKA ATRYBUT + WARTOSC
    	
            if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) && cache.length > 0)
                {
                var cacheEntry = cache.shift();
    	
                xmlHttp.open("POST",PhpFile, true);
                xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xmlHttp.onreadystatechange = ServerOdpowiedz;
                xmlHttp.send(cacheEntry);
                }
            else
                setTimeout('ABV()', 500);
                }
		}
	}
/*glowna funkcja walidujaca*/


/*WYSWIETLANY BOX Z ALERTEM*/
function ServerOdpowiedz() 
	{
  	if (xmlHttp.readyState == 4) 
  		{
    	if (xmlHttp.status == 200) 
    		{
    		if (xmlHttp.responseText != "")
    			{
               if (document.getElementById(fieldFailed))
                    {
                    if (xmlHttp.responseText == 'OK')
                        {
                        document.getElementById(fieldFailed).style.display = "none";
                        }
                    else if (xmlHttp.responseText != 'OK')
                        {
                        document.getElementById(fieldFailed).innerHTML = xmlHttp.responseText;
                        document.getElementById(fieldFailed).style.display = "block";
                        }
                    }
            else
                    {
                    if (xmlHttp.responseText != 'OK')
                        {
                        CreateBox(fieldFailed,currentField,xmlHttp.responseText);
                        setTimeout('ABV()', 500);
                        }
                    }
      			}
    		} 
    	else 
    		{
      		alert("Wystąpił błąd podczas uzyskiwania dostępu do serwera: " + xmlHttp.statusText);
    		}
  		}
	}
	

function BlokadaFormularza()
    {
    var odpowiedz = xmlHttp.responseText;

    alert(odpowiedz.lenght);
    
    stan =  true;
    alert(xmlHttp.responseText);
    if (xmlHttp.responseText != '')
        {
        stan =  false;
        }
    else
        {
        stan = true;
        }
    return stan;
    }


/*
 * @desc TUTAJ TWORZYMY DYNAMICZNA WARSTWE I NADAJEMY JEJ ODPOWIDNIE STYLE I WLASCIWOSCI
 *
 **/
function CreateBox(NewDiv,CurrentDiv,Text)
	{
	var sprawdz_warstwe = document.getElementById(NewDiv);
		
	if (!sprawdz_warstwe) //SPRAWDZAMY CZY ISTNIEJE JUZ TAKA WARSTWA
		{
		var nowa_warstwa = document.createElement("div"); //UTWORZENIE NOWEJ WARSTWY
		nowa_warstwa.setAttribute("id",NewDiv); //NADANIE NOWEGO ID DLA WARSTWY

        /*NADANIE STYLU WARSTWIE*/
        //W PRZYPADKU IE
        if(navigator.appName == 'Microsoft Internet Explorer')
            {
            nowa_warstwa.setAttribute("className","BoxClass"); //NADANIE NOWEGO ID DLA WARSTWY
            }
        else
            {
            nowa_warstwa.setAttribute("class","BoxClass"); //NADANIE NOWEGO ID DLA WARSTWY
            }

		var nowa_warstwa_content = document.createTextNode(Text);

		nowa_warstwa.appendChild(nowa_warstwa_content); //AKCEPACJA WCZESNIEJ DODANYCH ATRYBUTOW DO WARSTWY
	
		var warstwa_bazowa = document.getElementById(CurrentDiv);
		var parentDiv = warstwa_bazowa.parentNode;

		parentDiv.insertBefore(nowa_warstwa, warstwa_bazowa);	

		/*WLACZENIE WYLACZENIE ANIMACJ*/
		if (BoxAnimate == 1 && Position == "absolute")
			{
            //alert("crbox1");
            //InitBoxAnimation(NewDiv,CurrentDiv);
            }
        if (BoxAnimate == 0 && Position == "absolute")
			{
            // alert("crbox2");
            SetBoxAtributes(NewDiv,CurrentDiv);
            }
        if (BoxAnimate == 0 && Position == "relative")
			{
            //alert("crbox3");
            SetBoxAtributes(NewDiv,CurrentDiv);
            }
		}
        //setTimeout('ABV()', 500);
	}

		
/*NADANIE STYLI NOWYM WARSTWOM*/
function SetBoxAtributes(NewDiv,CurrentDiv)
	{
	//alert("SetBoxAtributes");
    
    if (BoxAnimate == 0 && Position == "relative")
		{
       // alert("0 relative");
        Box = document.getElementById(NewDiv);

        }

    if (BoxAnimate == 0 && Position == "absolute")
		{
       // alert("0 absolute");
        Box = document.getElementById(NewDiv);
        Box.style.left = '0px';
        Box.style.top = '0px';
        }

    //return false;
    
  		if (CurrentDiv)
			{
			var CurrDivX = document.getElementById(CurrentDiv).offsetLeft;
			var CurrDivY = document.getElementById(CurrentDiv).offsetTop;
			var CurrDivHeight = document.getElementById(CurrentDiv).offsetHeight;
			var CurrDivWidth = document.getElementById(CurrentDiv).offsetWidth;
			}
	
	/*TA FUNKCJONALNOSCI NIE DZIALA - NIE UZYWAC BOX NIMATE = 1*/
	if (BoxAnimate == 1 && Position == "absolute")
		{
		Box.style.position = Position;
		count = CurrDivY;
	 	if (TypAnimacji == 1)
	 		{
			count = parseInt(Box.style.top)+1;
			Box.style.top = count+'px';
			}
		else if (TypAnimacji == 0)
			{
			count = parseInt(Box.style.left)+1;
			Box.style.left = count+'px';				
			}
				
		/*ANIMOWANIE TAK DLUGO AZ OSIAGNIE X PX*/
		if (count <= CurrDivY+5)
			{
			document.getElementById("getcount").value = count;
			setTimeout(SetBoxAtributes,120);
			}
        //alert("1");
		}
    
    if (BoxAnimate == 0 && Position == "absolute")
        {
		Box.style.position = Position;
        Box.style.left = CurrDivX + CurrDivWidth + 15 + 'px';
        Box.style.top = CurrDivY - CurrDivHeight + 'px';
        //alert("2");
        }
	
    if (BoxAnimate == 0 && Position == "relative")
		{
		Box.style.position = Position;
        Box.style.left = 400;
        alert("3");
		}
	}
/*end funct SetBoxAtributes*/	

/*FUNKCJA LADUJACA ANIMACJE*/
/*begin funct InitBoxAnimation*/
function InitBoxAnimation(NewDiv,CurrentDiv) 
	{
  	alert("ss");
    Box = document.getElementById(NewDiv); // get the "foo" object
  
  		if (CurrentDiv)
			{
			var CurrDivX = document.getElementById(CurrentDiv).offsetLeft;
			var CurrDivY = document.getElementById(CurrentDiv).offsetTop;
			var CurrDivHeight = document.getElementById(CurrentDiv).offsetHeight;
			var CurrDivWidth = document.getElementById(CurrentDiv).offsetWidth;		
			}
			
  	if (TypAnimacji == 1)
  		{
  		Box.style.top = CurrDivY+'px';
  		Box.style.left = CurrDivWidth + 10+ 'px';
  		}
  	else if (TypAnimacji == 0)
  		{
  		Box.style.top = '0px';
  		Box.style.left = '0px';}
  	
  	SetBoxAtributes(NewDiv,CurrentDiv); // POCZATEK ANIMACJI
	}
/*end funct InitBoxAnimation*/

/*ustawia kursor w pierwszym polu*/
function setFocus()
    {
    document.getElementById("ImieNazwisko").focus();    
    }
