var xmlHttp1 = createXmlHttpRequestObject();	//do obslugi wysylania
var xmlHttp = createXmlHttpRequestObject();	//do obslugi bierzacego sprawdzania
var memory = new Array();

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 XMLHttpRequest.");
    }
    else
    {
	return xmlHttp;
    }
}

function check(inputValue,fieldID)
{
    if (xmlHttp)
    {
    	if (fieldID)
    	{
            inputValue = encodeURIComponent(inputValue);
    	    fieldID = encodeURIComponent(fieldID);
    	    memory.push("inputValue="+inputValue+"&fieldID="+fieldID);
    	}
    	try
    	{
            if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0) && memory.length > 0)
    	    {
    		var EntryInformation = memory.shift();
    		xmlHttp.open("POST","/ajax.checker.php",true);
    		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    		xmlHttp.onreadystatechange = handleRequestStateChange;
    		xmlHttp.send(EntryInformation);
    	    }
    	}  
    	catch(e)
    	{
    	}
    }   
}

function handleRequestStateChange()
{
    if (xmlHttp.readyState==4)
    {
	if (xmlHttp.status == 200)
	{
	    var hellomessage,myDiv;
	    xmlResponse = xmlHttp.responseXML;
	    xmlDocumentElement = xmlResponse.documentElement;
	    hellomessage = xmlDocumentElement.firstChild.data;
	    myDiv = document.getElementById("comunication");
	    myDiv.innerHTML = hellomessage;
	}
    }
}

function handleRequestStateChange1()
{
    if (xmlHttp1.readyState==4)
    {
	   if((xmlHttp1.status==200))
	   {
            xmlResponse = xmlHttp1.responseXML;
	        xmlDocumentElement = xmlResponse.documentElement;
            if (document.getElementById("CHCOUNTRYLBL"))
            {
                document.getElementById("CHCOUNTRYLBL").className=xmlDocumentElement.childNodes[0].firstChild.data;
            }

    	    document.getElementById("CHNAMELBL").className=xmlDocumentElement.childNodes[1].firstChild.data;
    	    document.getElementById("CHPHONELBL").className=xmlDocumentElement.childNodes[2].firstChild.data;
    	    document.getElementById("CHMAILLBL").className=xmlDocumentElement.childNodes[3].firstChild.data;
    	    document.getElementById("CHAGREEMENTPOLICYLBL").className=xmlDocumentElement.childNodes[4].firstChild.data;
            document.getElementById("formprogress").style.display="none";
    	    if(xmlDocumentElement.childNodes[0].firstChild.data!="error" && xmlDocumentElement.childNodes[1].firstChild.data!="error" && xmlDocumentElement.childNodes[2].firstChild.data!="error" && xmlDocumentElement.childNodes[3].firstChild.data!="error" && xmlDocumentElement.childNodes[4].firstChild.data!="error")
    	    {
        		document.getElementById("forminfo").style.display="none";
        		document.getElementById("formsend").style.display="block";
        		document.getElementById("formcontent").style.visibility="hidden";
    	    }
    	    else
    	    {
        		document.getElementById("forminfo").style.display="block";
        		document.getElementById("formsend").style.display="none";
        		document.getElementById("formcontent").style.visibility="visible";
    	    }
    	}
    	else
    	{
    	    alert("Blad");
    	}
    }
}

function send_affiliation()
{
    document.getElementById("formprogress").style.display="block";
    document.getElementById("forminfo").style.display="none";
    document.getElementById("formsend").style.display="none";
    document.getElementById("formcontent").style.visibility="hidden";

    var params="";
    var fieldID;
    var inputValue;
    
    if(xmlHttp1)
    {
    	var x = document.getElementById("taxRefund");
    	for(i=1; i<(x.length-1); i++)
    	{
            fieldID=encodeURIComponent(x.elements[i].id);
    	    if(fieldID == "CHAGREEMENTPOLICY"){
                if(document.getElementById("CHAGREEMENTPOLICY").checked){
                    inputValue = 1;
                }
                else{
                    inputValue = 0;
                }
            }
            else{
                inputValue=encodeURIComponent(x.elements[i].value);
            }

    	    params=params+fieldID+"="+inputValue+"&";
    	}	

        //var tocut=params.length-1;
    	//params=params.substring(0,tocut);
        params=params+"pid="+Get_Cookie('zp_partnerid');
        params=params+"&pdid="+Get_Cookie('zp_partnerdomainid');
        memory.push(params);
    }
    try
    {
    	if ((xmlHttp1.readyState==4 || xmlHttp1.readyState==0))
        {
            EntryInformation = memory.shift();
		    xmlHttp1.open("POST","/ajax.send_affiliation.php",true);
		    xmlHttp1.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		    xmlHttp1.onreadystatechange = handleRequestStateChange1;
		    xmlHttp1.send(EntryInformation);  
    	}
    }
    catch(e)
    {
    }
}

function Get_Cookie(check_name) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}	

