/*tworzenie obiektu XMLHTTPRequest dla każdej przeglądarki */
function getXMLHTTPRequest()
{
  var req = false;
  try
  { req = new XMLHttpRequest(); }
  catch(err1)
	{
	  try
	  { req = new ActiveXObject("Msxml2.XMLHTTP"); }
	  catch(err2)
	    {
	    try
	      { req = new ActiveXObject("Microsoft.XMLHTTP"); }
	      catch(err3)
		    { req = false; 
		    }
	    }
	}
  return req;
}

var myRequest = getXMLHTTPRequest(); //przypisanie obiektu do zmiennej

function callAjaxProvince(url) {
  var myRand = new Date().getTime();
  myRequest.open("GET", url+"&rand="+myRand, true);
  myRequest.onreadystatechange = responseAjaxProvince;
  myRequest.send(null);
}

function responseAjaxProvince() {
	if(myRequest.readyState == 4) {
		if(myRequest.status == 200) {
			if (document.getElementById('smallLoader')) {
				document.getElementById('smallLoader').style['visibility'] = 'hidden';
			}
			document.getElementById('bigLoader').style['display'] = 'none';
			document.getElementById('districtAjax').innerHTML = myRequest.responseText;
		} else { }
	} else {
		document.getElementById('smallLoader').style['visibility'] = 'visible';
		document.getElementById('bigLoader').style['display'] = 'none';
	}
}

function searchAjax() {
	document.getElementById('searchButton').disabled = true;
	searchOffer('action/search.php?province='+document.getElementById('province').value+'&district='+document.getElementById('district').value+'&tema='+document.getElementById('tema').value+'&advert='+document.getElementById('advertTypeSel').value+'&sortby='+document.getElementById('sortby').value);
}

function searchOffer(url) {
  var myRand = new Date().getTime();
  myRequest.open("GET", url+"&rand="+myRand, true);
  myRequest.onreadystatechange = responseOffer;
  myRequest.send(null);
}

function responseOffer() {
	if(myRequest.readyState == 4) {
		if(myRequest.status == 200) {
			document.getElementById('bigLoader').style['display'] = 'none';
			document.getElementById('smallLoader').style['visibility'] = 'hidden';
		    document.getElementById('container').innerHTML = myRequest.responseText;
			document.getElementById('searchButton').disabled = false;
		} else { }
	} else {
	    //document.getElementById('bigLoader').style['display'] = 'block';
		//document.getElementById('smallLoader').style['visibility'] = 'hidden';
	}
}

function loadAjaxPage(url) {
  var myRand = new Date().getTime();
  if (url.indexOf("?")>1) {
      myRequest.open("GET", url+"&rand="+myRand, true);
  } else { myRequest.open("GET", url+"?rand="+myRand, true);}
  myRequest.onreadystatechange = responseAjaxPage;
  myRequest.send(null);
}

function responseAjaxPage() {
	if(myRequest.readyState == 4) {
		if(myRequest.status == 200) {
		    document.getElementById('pageShow').innerHTML = myRequest.responseText;
		} else if (myRequest.status == 404) { 
		    document.getElementById('pageShow').innerHTML = '<div id="error404"><img src="images/error404.gif"><h1>Strona nie została znaleziona.</h1></div>';
		} else { }
	} else { }
}

function replaceSubstring(inputString, fromString, toString) {
      var temp = inputString;
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
     } return temp; 
} 

function val(id) {
	return replaceSubstring(escape(document.getElementById(id).value), "+", "%2B");
}

function loadAjaxPagePOST(url, data) {
  var myRand = new Date().getTime();
  myRequest.open("POST", url+"?rand="+myRand, true);
  myRequest.onreadystatechange = responseAjaxPage;
  myRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  myRequest.send(data);
}

function regStep1() {
	var url = 'page/register.php';
	var data = 'step=1';
	data += '&name='+val('in_name');
	data += '&lastname='+val('in_lastname');
	data += '&email='+val('in_email');
	data += '&passw='+val('in_passw');
	data += '&passw2='+val('in_passw2');
	data += '&company='+val('in_company');
	data += '&nip='+val('in_nip');
	data += '&adress='+val('in_adress');
	data += '&postal='+val('in_postal');
	data += '&city='+val('in_city');
	if (document.getElementById('chb_reg').checked == true) {
		data += '&regulations=on'
	}
	if (document.getElementById('dzialgosp').checked == true) {
		data += '&dzialgosp=on'
	}
	loadAjaxPagePOST(url, data);
}

function regStep2() {
	var url = 'page/register.php';
	var data = 'step=2&portalName='+val('in_portalName');
	data += '&portalURL='+val('in_portalURL');
	data += '&portalEmail='+val('in_portalEmail');
	data += '&province='+val('province');
	data += '&district='+val('district');
	data += '&portalVisit='+val('in_portalVisit');
	data += '&portalUnique='+val('in_portalUnique');
	data += '&tema='+val('tema');
	data += '&portalStatLink='+val('in_portalStatLink');
	data += '&portalContactPerson='+val('in_portalContactPerson');
	data += '&portalContactTel='+val('in_portalContactTel');
	data += '&portalDesc='+val('in_portalDesc');
	loadAjaxPagePOST(url, data);
}

function loadAjaxSite(url, data) {
  var myRand = new Date().getTime();
  myRequest.open("POST", url+"?rand="+myRand, true);
  myRequest.onreadystatechange = responseAjaxSite;
  myRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  myRequest.send(data);
}

function responseAjaxSite() {
	if(myRequest.readyState == 4) {
		if(myRequest.status == 200) {
		    document.getElementById('content').innerHTML = myRequest.responseText;
		} else { }
	} else { }
}

function showInfoOffert(url) {
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}
	else {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) { }
		}
	}
	if (xhr) {
		xhr.onreadystatechange = function() {
				var prevWin = document.getElementById("infoWrapper");
				if (xhr.readyState == 4) {
					if (xhr.status == 200) {
						var outMsg = xhr.responseText;
					}
					else {
						var outMsg = "Wystąpił następujący problem: " + xhr.status;
					}
					prevWin.innerHTML = outMsg;
				} else {

					if (window.innerWidth) var x = (window.innerWidth/2)-300;
					else var x = (document.body.clientWidth/2)-300;
										
					if(document.body.scrollTop) var scroll = document.body.scrollTop;
					else if(window.pageYOffset) var scroll = window.pageYOffset;
					else var scroll = document.documentElement.scrollTop-100;
					
					if (window.innerHeight) var y = ((window.innerHeight/2)-200)+scroll;
					else var y = ((document.body.clientHeight/2)-200)+scroll;
					prevWin.style['display'] = 'block';
					prevWin.style.left = x+'px';
					prevWin.style.top = y+'px';
					prevWin.innerHTML = '<div style="text-align:center; width:100%; height:100%; background:url(images/infoloader.gif) no-repeat center;"><p style="padding-top:70px; font-size:11px; font-family:tahoma; font-weight:bold;">Trwa ładowanie...</p></div>';
				}
		    }			
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	return false;
}

function showAdvertTypeInfo(url) {
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}
	else {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) { }
		}
	}
	if (xhr) {
		xhr.onreadystatechange = function() {
				var output = document.getElementById("advertTypeDesc");
				if (xhr.readyState == 4) {
					if (xhr.status == 200) {
						var outMsg = xhr.responseText;
					}
					else {
						var outMsg = "Wystąpił następujący problem: " + xhr.status;
					}
					output.innerHTML = outMsg;
				} else {
					/* === put loader here or something === */
				}
		    }			
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	return false;
}

function enter_pressed(e){
/* funkcja sprawdza czy został wcisniety ENTER */
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return false;
	return (keycode == 13);
}

function showDiv(id) {
	document.getElementById('question'+id).style['display'] = 'block';
	//document.getElementById('rL'+id).innerHTML = '<a onclick="hideDiv('+id+');">schowaj</a>';
}

function hideDiv(id) {
	document.getElementById('question'+id).style['display'] = 'none';
	//document.getElementById('rL'+id).innerHTML = '';
}