// JavaScript Document
//Author; Jacek Adamkiewicz (init7 AT i7.com.pl)
//Użytkowanie: tylko na warunkach licencji GPL
function changeDisplayObj(obiekt) {
	i=0;
	while(i<arguments.length){
		objFind = document.getElementById(arguments[i]);
		if (objFind.style.display == 'block'){
			objFind.style.display = 'none';
		}else{
			objFind.style.display = 'block';
		}
		i++;
	}
}

banery = new Array(
'./img/banery/baner_0.jpg',
'./img/banery/baner_1.jpg',
'./img/banery/baner_2.jpg',
'./img/banery/baner_3.jpg',
'./img/banery/baner_4.jpg',
'./img/banery/baner_5.jpg',
'./img/banery/baner_6.jpg',
'./img/banery/baner_7.jpg',
'./img/banery/baner_8.jpg',
'./img/banery/baner_9.jpg'
);

function random() {
	today = new Date();
	num = today.getTime();
	num = Math.round(Math.abs(Math.sin (num)*1000000)) % 10; //limits;
	return num;
}


function showImage(img_file, id){
	image_Load = new Image();
	image_Load.src = img_file;
	document.getElementById(id).src = image_Load.src;
}


function posElement(pos){
	if (typeof(window.innerWidth) == 'number'){
		WidthPos = window.innerWidth+pos;//126
	}else{
		if (document.body && typeof(document.body.offsetWidth) == 'number'){
			WidthPos = document.body.offsetWidth+pos;//126
		}else{
			WidthPos = 0;
		}
	}
	WidthPos = WidthPos/2;
	return WidthPos;
}


function DisplayObj(obiekt) {
		objFind = document.getElementById(arguments[0]);
		if (objFind.style.display == 'block'){
			objFind.style.display = 'none';
		}else{
			objFind.style.display = 'block';
			document.getElementById(arguments[0]).style.left = posElement(-460)+"px";
		}
}

var mydiv1 = null;
var newDiv1 = null;

function addElement(){
	newDiv1 = document.createElement(arguments[2]);
	newDiv1.setAttribute("id", arguments[3]);
	newDiv1.style.color = "red";
	newDiv1.innerHTML = arguments[1];
	mydiv1 = arguments[0];
	var parentDiv = mydiv1.parentNode;
	parentDiv.insertBefore(newDiv1, mydiv1.nextSibling);
}

function GetXmlHttpObject(){
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
	// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e){
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	}
	return xmlHttp;
}


function SendMyForm(){
var http_request = GetXmlHttpObject();
//id <form>
var objForm = document.getElementById(arguments[1]);
//-----
var url_request = window.location.hostname;//"axalux.pl";
var parameters = "f="+arguments[0]+""; 

//if(window.XMLHttpRequest) { // Mozilla, Safari, ...
//	http_request = new XMLHttpRequest();
//}else if (window.ActiveXObject) { // IE
//	http_request = new ActiveXObject("Microsoft.XMLHTTP");
//}

http_request.onreadystatechange = handle_data;
http_request.open("POST", "./xhr.php", true); //"http://"+url_request+
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
//http_request.setRequestHeader("Connection", "close"); //hym, chyba to jest problem przy IE

http_request.send(parameters);

	function handle_data() {
		if(http_request.readyState == 4) {
			if(http_request.status == 200) {
				JSONobj = eval('('+http_request.responseText+')');
				if(JSONobj == null){
					var element_ID = document.getElementById('FeedBack');
					element_ID.innerHTML = "<h2>Twója wiadomość została wysłana!</h2>";
				}else{
					if(JSONobj.constructor == Object){
						if(JSONobj.errorSend == 1){
							alert("KOMUNIKAT: Twoja wiadomość NIE została wysłana, spróbuj ponownie później. Przepraszamy!")
						}
						var a = 0;
						while(a<objForm.length){
							c = objForm[a];
							if(document.getElementById(c.name+'_e')){
								kil = document.getElementById(c.name+'_e')
								kil.parentNode.removeChild(kil); //kil.parentNode.lastChild
							}
							if(JSONobj.errorSend[c.name] == 1){
								if(!document.getElementById(c.name+'_e')){
									addElement(c,"&nbsp;popraw!","span",c.name+'_e');
								}
							}
							a++;
						}
					}
				}
			}else{
				alert('Błąd! Wystąpił problem z wybranym adresem URL.');
			}
			http_request = null;
			if(http_request == null){changeDisplayObj('infoSend');}
		}
	}
	
}


function checkForms(){
	changeDisplayObj('infoSend');
	var b = 0;
	var postMessages = "";
	//id <form>
	var objForm = document.getElementById(arguments[0]);
	//-----
	if(typeof(arguments[1]) == "undefined"){ arguments[1]=objForm.length }
		while(b<=(arguments[1] - 1)){
			var messages = objForm[b];
			var valueInput = messages.value;
			var re = /\"/g;
			var regResult = valueInput.replace(re, "\\u0022");
			regResult = encodeURIComponent(regResult);
			postMessages += '["'+messages.name+'","'+regResult+'"]';
			if(b<(arguments[1] - 1)){ postMessages += "," }
			b++;
		}
		
		SendMyForm("["+postMessages+"]", objForm.name);
}

