﻿var request = false;
   try {
     request = new XMLHttpRequest();
   } catch (trymicrosoft) {
     try {
       request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (othermicrosoft) {
       try {
         request = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (failed) {
         request = false;
       }  
     }
   }

   if (!request)
     alert("AJAX nie działa w Twojej przeglądarce");

function wyswietlFormularz()
{
var htmlElement= document.documentElement;
var bodyElement= htmlElement.lastChild;
var contentarea= document.getElementById('productsarea');
var div = document.createElement('div');
div.setAttribute("style", "position: absolute; top: 80%; left: 30%; background: #DFDFDF url(uploads/images/3Tlagrindcut.jpg) no-repeat fixed top left; min-height: 300px; min-width: 500px; border-style: ridge; border-color: #BBBBBB; border-width: 4px;");
div.setAttribute("id", "popup");
var cancelButton = document.createElement('input');
cancelButton.setAttribute("type", "button");
cancelButton.setAttribute("value", "X");
cancelButton.setAttribute("onclick", "zamknijFormularz();");
cancelButton.setAttribute("style", "width: 10px, height: 10px; float: right;");
var header = document.createElement('h3');
var welcomeText = document.createTextNode("Napisz do nas:");
var form = document.createElement('form');
var text = [document.createTextNode('Adres E-mail*:'), document.createTextNode('Nr Telefonu:'), document.createTextNode('Temat:'), document.createTextNode('Treść wiadomości*:')];

contentarea.appendChild(div);
div.appendChild(cancelButton);
div.appendChild(header);
header.appendChild(welcomeText);
div.appendChild(form);
var field1 = document.createElement('input');
field1.setAttribute("type", "text");
field1.setAttribute("style", "width: 250px;")
field1.setAttribute("id", "field1");

var field2 = document.createElement('input');
field2.setAttribute("type", "text");
field2.setAttribute("id", "field2");

var field3 = document.createElement('input');
field3.setAttribute("type", "text");
field3.setAttribute("value", "Zapytanie - "+document.getElementById("product_name").childNodes[0].nodeValue);
field3.setAttribute("style", "width: 300px;")
field3.setAttribute("id", "field3");

var field4 = document.createElement('textarea');
field4.setAttribute("cols", "60");
field4.setAttribute("rows", "10");
field4.setAttribute("id", "field4");

var field = [field1, field2, field3, field4];
var table = document.createElement("table");
form.appendChild(table);

for (var x=0; x<text.length; x++)
{
var tr= document.createElement("tr");
table.appendChild(tr);
for (var i=0; i<2; i++)
{
if (i==0)
{
var td = document.createElement("td");
var strong = document.createElement("strong");
tr.appendChild(td);
td.appendChild(strong);
strong.appendChild(text[x]);
}
else
{
var td = document.createElement("td");
tr.appendChild(td);
td.appendChild(field[x]);
}
}
}

var submit = document.createElement('input');
submit.setAttribute("type", "button");
submit.setAttribute("style", "margin-left: 175px; margin-top: 50px;");
submit.setAttribute("value", "Wyślij zapytanie");
submit.setAttribute ("onclick","email();");
form.appendChild(submit);
}

function zamknijFormularz()
{
var contentarea= document.getElementById("productsarea");
var form = document.getElementById('popup');
contentarea.removeChild(form);
}

function email()
{
	if (document.getElementById('field1').value != '')
	{
	var email = document.getElementById('field1').value;
	}
	else
	{
	alert("Pole \"Adres E-mail\" nie może być puste");
	return false;
	}	
	var phone = document.getElementById('field2').value;
	var subject = document.getElementById('field3').value;
	if (document.getElementById('field4').value != '')
	{
	var msg = document.getElementById('field4').value;
	}
	else
	{
	alert("Pole \"Treść\" nie może być puste");
	return false;
	}
	var xml = "email="+email+"&phone="+phone+"&subject="+subject+"&msg="+msg;
	var url = "mail.php";
	request.open("POST", url, true);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.setRequestHeader("charset", "utf-8"); 
	request.onreadystatechange = function()
	{
		var popup=document.getElementById("popup");
		if (request.readyState != 4)
		{
		if (popup.hasChildNodes())
		{
			for (var x= popup.childNodes.length; x>0; x--)
			{
				popup.removeChild(popup.childNodes[x-1]);
				
			}
			
		}
		var gif = document.createElement("img");
		gif.setAttribute("src", "uploads/images/loading.gif");
		popup.appendChild(gif);
		}
	else
	{
    		if (request.status == 200) 
		{
			if (popup.hasChildNodes())
			{
				for (var x= popup.childNodes.length; x>0; x--)
				{
					popup.removeChild(popup.childNodes[x-1]);
				}
			}
			var info = document.createTextNode("Wiadomość została wysłana. Skontaktujemy się z Tobą w najbliższym czasie.");
			var h1 = document.createElement("h1");
			h1.appendChild(info);
			popup.appendChild(h1);
			var close = document.createElement("input");
			close.setAttribute("type", "button");
			close.setAttribute("onclick", "zamknijFormularz();");
			close.setAttribute("value", "Zamknij okno");
			popup.appendChild(close);
		}
	}
	}
request.send(xml);
}


