﻿		var AddresID            = '';   // sem se poslou natazena data
		var LoadingID           = '';   // toto je ten item kterej ukazuje loding kdyz se tahaj data
		var SelectAddressButton = '';   // button kterej posle dalsi dotaz na server s tim se chceme presne tento provek
		
		var Organization    = '';
		var Addres_Line     = '';
		var Line_1   = '';
		var Line_2   = '';
		var Line_3   = '';
		var Line_4   = '';
		var Town     = '';
        var County	 = '';
        
		function SetItems(Form_Organization, Form_Address, Form_Line1, Form_Line2, Form_Line3, Form_Line4, 
		                  Form_Town, Form_SelectAddress_ID, Form_Loading_ID, 
		                  Form_SelectAddressButton, Form_NewAccInfo_County)
		{
            Organization        = Form_Organization;
            Addres_Line         = Form_Address;
            
            Line_1              = Form_Line1;
            Line_2              = Form_Line2;
            Line_3              = Form_Line3;
            Line_4              = Form_Line4;
            
            Town                = Form_Town;
            
            AddresID            = Form_SelectAddress_ID;
            LoadingID           = Form_Loading_ID;
            SelectAddressButton = Form_SelectAddressButton;
            County              = Form_NewAccInfo_County;
            return true;
		}
		
		function pcaByPostcodeBegin(Account_Code, License_Code, Form_PostCode_ID)
        {
            var postcode  = document.getElementById(Form_PostCode_ID).value;
            var scriptTag = document.getElementById("pcaScriptTag");
            var headTag   = document.getElementsByTagName("head").item(0);
            var strUrl    = "";
  
            //Build the url
            strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
            strUrl += "&action=lookup";
            strUrl += "&type=by_postcode";
            strUrl += "&postcode="        + escape(postcode);
            strUrl += "&account_code="    + escape(Account_Code);
            strUrl += "&license_code="    + escape(License_Code);
            strUrl += "&callback=pcaByPostcodeEnd";
            //Make the request
            if (scriptTag)
            {
                //The following 2 lines perform the same function and are interchangeable
                headTag.removeChild(scriptTag);
                //scriptTag.parentNode.removeChild(scriptTag);
            }
            scriptTag      = document.createElement("script");
            scriptTag.src  = strUrl
            scriptTag.type = "text/javascript";
            scriptTag.id   = "pcaScriptTag";

            
            if (LoadingID != null)
                document.getElementById(LoadingID).style.display              = ''; // toto je div kterej maskuje na formulari napis loading
            if (AddresID != null)
                document.getElementById(AddresID).style.display               = ''; // toto je ten select kam se nahrnou data z PC Any
            if (SelectAddressButton != null)
                document.getElementById(SelectAddressButton).style.display    = ''; // a toto je tlacitko za tim selectem ktere pak posle dalsi dotaz na PC Any
            headTag.appendChild(scriptTag); // pozor - script se zacne provadet ihned !!!! (asynchronne !!!) tedy ihned po teto radce -> nasledujici radka(y) muze klidne probehnout az po ukonceni funkce pcaScriptTag
            return false;
        }

		function pcaByPostcodeEnd()
        {
            if (LoadingID != null)
                document.getElementById(LoadingID).style.display = 'none';
            if (pcaIsError)
            {
                //Show the error message
                alert(pcaErrorMessage);
                return false;
            }
            else
            {
                //Check if there were any items found
                if (pcaRecordCount==0)
                {
                    alert("Sorry, no matching items found");
                    return false;
                }
                else
                {
                    ClearAddressBox(AddresID);
                    for (i=0; i<pca_id.length; i++)
		                document.getElementById(AddresID).options[document.getElementById(AddresID).length] = new Option(pca_description[i], pca_id[i]);
                    return false;
		        }
            }
		    return false;
        }
		
		function pcaFetchBegin(Account_Code, License_Code)
        {
            var address       = document.getElementById(AddresID).value;
            var scriptTag     = document.getElementById("pcaScriptTag");
            var headTag       = document.getElementsByTagName("head").item(0);
            var strUrl        = "";

            if (address == '')
            {
                alert('Please select at least one adress !');
                return false;
            }
            //Build the url
            strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
            strUrl += "&action=fetch";
            strUrl += "&id="            + escape(address);
            strUrl += "&account_code="  + escape(Account_Code);
            strUrl += "&license_code="  + escape(License_Code);
            strUrl += "&style=raw";
            strUrl += "&callback=pcaFetchEnd";

            //Make the request
            if (scriptTag)
             {
                //The following 2 lines perform the same function and are interchangeable
                headTag.removeChild(scriptTag);
                //scriptTag.parentNode.removeChild(scriptTag);
             }
            scriptTag         = document.createElement("script");
            scriptTag.src     = strUrl
            scriptTag.type    = "text/javascript";
            scriptTag.id      = "pcaScriptTag";
            
            if (AddresID != null)
                document.getElementById(AddresID).style.display            = 'none';
            if (SelectAddressButton != null)
                document.getElementById(SelectAddressButton).style.display = 'none';
            headTag.appendChild(scriptTag);
            
            return false;
        }

		function pcaFetchEnd()
        {
            //Test for an error
            if (pcaIsError)
            {
                //Show the error message
                alert(pcaErrorMessage);
                return false;
            }
            else
            {
                //Check if there were any items found
                if (pcaRecordCount==0)
                {
                    alert("Sorry, no matching items found");
                    return false;
                }
                else
                {
                    
                    if ((Organization != null) && (Organization != '')) document.getElementById(Organization).value = pca_organisation_name[0];
                    if (Addres_Line  != null) document.getElementById(Addres_Line).value  = pca_line1[0];
                    if (Line_1       != null) document.getElementById(Line_1).value       = pca_line2[0];
                    if (Line_2       != null) document.getElementById(Line_2).value       = pca_line3[0];
                    if (Line_3       != null) document.getElementById(Line_3).value       = pca_line4[0];
                    if (Line_4       != null) document.getElementById(Line_4).value       = pca_line5[0];
        		    if (Town         != null) document.getElementById(Town).value         = '' + pca_post_town[0];
        		    if (County       != null) document.getElementById(County).value       = '' + pca_county[0];
                    if (AddresID     != null) ClearAddressBox(AddresID);
        //		    if (pca_building_flat[0]!='')
        //          {
        //		        document.forms[0]["flat"].value = '' + pca_building_flat[0];
        //	        }
        //		    else
        //          {
        //		        document.forms[0]["flat"].value = '' + pca_sub_building_name[0];
        //	        }
        //		    document.forms[0]["building_number"].value  = '' + pca_building_number[0];
        //		    document.forms[0]["building_name"].value    = '' + pca_building_name[0];
        //		    document.forms[0]["street1"].value          = '' + pca_dependent_thoroughfare_name[0] + ' ' + pca_dependent_thoroughfare_descriptor[0];
        //		    document.forms[0]["street2"].value          = '' + pca_thoroughfare_name[0]           + ' ' + pca_thoroughfare_descriptor[0];
        //		    document.forms[0]["dd_locality"].value      = '' + pca_double_dependent_locality[0];
        //		    document.forms[0]["d_locality"].value       = '' + pca_dependent_locality[0];
                    return false;
                }
            }
        }
        
        function ClearAddressBox(AddresID)
        {
            for ( i = document.getElementById(AddresID).options.length-1; i >= 0; i--)
            {document.getElementById(AddresID).options[i] = null;}
            return true;
        }