function createXHR()
{
    if ( window.XMLHttpRequest )
    {
        // code for all new browsers
        return new XMLHttpRequest();
    }
    else if ( window.ActiveXObject )
    {
        // code for IE5 and IE6
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
}

function trim( str )
{
    if ( str )
    {
        return str.replace(/^\s+|\s+$/g, '') ;
    }
    else
    {
        return null;
    }
}

function validate()
{
    var agencyValid = false;
    var noteValid = false;


    if (document)
    {
        // todo replace input field id
        if ( document.getElementById( "informationRequestAgency" ) )
        {
            var informationRequestAgencyValue = document.getElementById( "informationRequestAgency" ).value;
            if ( informationRequestAgencyValue && trim(informationRequestAgencyValue) != "" )
            {
                agencyValid = true;
            }
        }
        // todo replace input field id
        if ( document.getElementById("informationRequestNote") )
        {
            var informationRequestNoteValue = document.getElementById( "informationRequestNote" ).value;
            if ( informationRequestNoteValue && trim(informationRequestNoteValue) != "" )
            {
                noteValid = true;
            }
        }
        // todo replace input field id

    }

    return agencyValid && noteValid;

}

function buildInformationRequestURL( baseURL )
{
    var informationRequestURL = baseURL;
    if ( baseURL )
    {
        if ( document )
        {
            if ( document.getElementById( "informationRequestAgency" ) )
            {
                var agencyValue = escape( trim( document.getElementById( "informationRequestAgency" ).value ) );


                informationRequestURL += "?agency=" + agencyValue;

            }
            if ( document.getElementById( "informationRequestNote" ) )
            {
                var noteValue = escape( trim( document.getElementById( "informationRequestNote" ).value ) );
                informationRequestURL += "&note=" + noteValue;

            }

        }
        return informationRequestURL;
    }
    else
    {
        return null;
    }
}

function handleInformationResponse()
{
    if(this.readyState == 4 && this.status == 200)
    {
        if(this.responseText != null)
        {
            if (document)
            {
                //a válaszszöveg megjelenítése
                var newdiv = document.createElement("div");
				newdiv.innerHTML = this.responseText;
				var container = document.getElementById("container");
				container.appendChild(newdiv);
                //a felesleges mezők eltüntetése
				
				document.getElementById("container").style.display = "block";
                document.getElementById("agency").style.display = "block";
                document.getElementById("textarea").style.display = "none";
                document.getElementById("errortext").style.display=  "none";
                document.getElementById("descriptionDiv").style.display=  "block";
                //gombok lecserélése
                document.getElementById("sendButton").style.display=  "none";
                document.getElementById("noButton").style.display=  "block";
                
            }
        }
        else
        {
            alert("A kérésre nem érkezett válasz!");
        }
    }
    else if (this.readyState == 4 && this.status != 200)
    {
        alert("A kérésre nem érkezett válasz!");
    }
}

function sendInformationRequest()
{
    if (validate())
    {
        // todo replace url
        var informationRequestURL = buildInformationRequestURL( infoUrl );

        if ( !informationRequestURL )
        {
            return;
        }
        //alert(informationRequestURL);
        var xhr = createXHR();

        if (xhr != null)
        {
            xhr.onreadystatechange = handleInformationResponse;
            xhr.open( "GET", informationRequestURL, true );
            xhr.send( null );
        }
        else
        {
            alert( "A böngészője nem támogatja az XMLHTTP objektumot." );
        }
    }
    else
    {
        // invalid data
        if (document)
        {
            //a v?laszsz?veg megjelen?t?se
            document.getElementById("errortext").innerHTML =  "Kérem töltsön ki minden mezőt!";



        }
    }
}


