function VerifyMessageForm()
{
    var oForm = document.forms['sendmess'];
    if(oForm.elements['message'].value.length == 0)
    {
        alert('Fill message field');
    }
    else
    {
        if((oForm.elements['mail'].value.length == 0))
        {
            alert('Fill e-mail field');
        }
        else
        {
            if(checkemail(oForm.elements['mail'].value))
            {
                HideDiv('contact');
                ShowDiv('loader')
                sendPOSTRequest('loader', 'sendmess');
            }
            else
            {
                alert('Fill e-mail field with proper e-mail');
            }
        }
        /*
        if((oForm.elements['name'].value.length == 0) && (oForm.elements['mail'].value.length == 0))
        {
            alert('Jedno z pól: adres e-mail bądź numer telefonu powinno zostać wypełnione.');
        }
        else
        {
            HideDiv('contact');
            ShowDiv('loader')
            sendPOSTRequest('loader', 'sendmess');
        }
        */
    }
}

function HideDiv(name)
{
    document.getElementById(name).style.visibility = "hidden";
    document.getElementById(name).style.display="none";
}

function ShowDiv(name)
{
    document.getElementById(name).style.visibility = "visible";
	document.getElementById(name).style.display="block";
}

function checkemail(mail)
{
    var str=mail;
    var filter=/^.+@.+\..{2,3}$/
    if (filter.test(str))
    {
        result=true;
    }
    else
    {
        result=false;
    }
    return (result)
}

function displayGetData(sText, divID)
{
	var divCustomerInfo = document.getElementById(divID);
	divCustomerInfo.innerHTML = sText;
}

function sendPOSTRequest(divID, form_name)
{ 	
	var oForm = document.forms[form_name];
	var sBody = getRequestBody(oForm);
	var oXmlHttp = zXmlHttp.createRequest();
	
	oXmlHttp.open("post", oForm.action, true);
        //alert(oForm.action);
	oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	oXmlHttp.onreadystatechange = function ()
	{
		 	
	    if (oXmlHttp.readyState == 4)
		{		 	
	    	if (oXmlHttp.status == 200)
			{
	            displayGetData(oXmlHttp.responseText, divID);
	            //HideDiv('loader');
	            //ShowDiv('contact');
	        }
			else
			{
			 	displayGetData("Wyst±pił bład: " + oXmlHttp.statusText, divID);
	        }
	    }
	
	};
	oXmlHttp.send(sBody);
	
}


function getRequestBody(oForm) {
    var aParams = new Array();

    for (var i=0 ; i < oForm.elements.length; i++)
	{
     	if(oForm.elements[i].type=='radio')
     	{
			if(oForm.elements[i].checked)	
			{
				var sParam = encodeURIComponent(oForm.elements[i].name);
	    	    sParam += "=";
    		    sParam += encodeURIComponent(oForm.elements[i].value);
	        	aParams.push(sParam);	
			}
		}
		else
		{
		 	if(oForm.elements[i].type=='checkbox')
		 	{
				if(oForm.elements[i].checked)	
				{
					var sParam = encodeURIComponent(oForm.elements[i].name);
	    	    	sParam += "=";
    		    	sParam += encodeURIComponent(oForm.elements[i].value);
	        		aParams.push(sParam);	
				}		
			}
			else
			{
	        	var sParam = encodeURIComponent(oForm.elements[i].name);
		        sParam += "=";
    		    sParam += encodeURIComponent(oForm.elements[i].value);
        		aParams.push(sParam);
        	}
        }
    }

    return aParams.join("&");
}


