// 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++;
	}
}

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(-520)+"px";
			document.getElementById(arguments[0]).style.top = "100px";
		}
}

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;
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", "./input_forms/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) {
				var JSONobj = JSON.parse(http_request.responseText);//eval('('+http_request.responseText+')');
				//var 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);
}
function sendData()
{
  if(window.location.search != '' ){
  //alert(window.location.search);
	GetContent('./data.php'+ window.location.search);
	}
}
//sendData();
function GetContent(url_data){

	function onProgress(e) {
		var percentComplete = (e.position / e.totalSize)*100;
		// ...
	}
	function onError(e) {
			alert("Error " + e.target.status + " occurred while receiving the document.");
	}

	function onLoad(e) {
		// ...
	}

	var http_request = GetXmlHttpObject();
	//http_request.onprogress = onProgress;
	http_request.open("GET", url_data, true);
	//http_request.onload = onLoad;
	///http_request.onerror = onError;
	http_request.onreadystatechange = handle_data;
	http_request.send(null);

	function handle_data() {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
								element_ID = document.getElementById('content_input_text');
								element_ID.innerHTML = http_request.responseText;
			} else {
					alert('Wystąpił problem z wybranym adresem URL.');
			}
		http_request = null;
	}
	}
}
