﻿
//var http_request = false;
var hashurl = '';
var hashquery = '';
var ohashurl = '';
var ohashquery = '';
var lastHash = '';
var lastPage = '';
var lastOther = '';
var i = 0;
var j = 0;
var nav = new RegExp("^(#p[0-9]+b[0-9]+)?$");

function makeRequest(url,id) {
		if(document.getElementById('sort_form')){
						if(document.getElementById('usun_button'))	document.getElementById('usun_button').disabled = true;
						if(document.getElementById('akceptuj_button'))	document.getElementById('akceptuj_button').disabled = true;
						if(document.getElementById('zablokuj_button'))	document.getElementById('zablokuj_button').disabled = true;
						else if(document.getElementById('odblokuj_button'))	document.getElementById('odblokuj_button').disabled = true;
						if(document.getElementById('wyslij_button'))	document.getElementById('wyslij_button').disabled = true;
					}  	
		url= url.split("&amp;").join("&");
        http_request = false;
		
		if(id == 'note_ajax') document.getElementById(id).innerHTML = '<img src="img/loading.gif" alt="loading" style="margin-left: 45px; margin-top: 10px;" />';
     //   else document.getElementById(id).innerHTML = '<img src="img/loading.gif" alt="loading" style="display: block; margin-left: 300px; margin-top: 0px;" />';
		if (window.XMLHttpRequest) { 
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');

            }
        } else if (window.ActiveXObject) { 
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Nie mozna stworzyc instancji obiektu XMLHTTP');
            return false;
        }
        http_request.onreadystatechange = function() { Contents(http_request,id); };
        http_request.open('GET', url, true);
        http_request.send(null);

    }



    function Contents(http_request,id) {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
            	var TextHTML = http_request.responseText;
            	if(hashurl == 'opinions_ajax'){
            		var startpos = TextHTML.indexOf( "#opinion_comm_pre_start", 1 );
            		var endpos = TextHTML.indexOf( "#opinion_comm_pre_end", 1 );
               		if(startpos>0){
            		var comm = TextHTML.substr(startpos+23,endpos-startpos-23);	
					document.getElementById('opinion_alert').innerHTML = comm;
					TextHTML = TextHTML.replace("#opinion_comm_pre_start"+comm+"#opinion_comm_pre_end", "");
					}
				}else if(document.getElementById('sort_form')){
						if(document.getElementById('usun_button'))	document.getElementById('usun_button').disabled = false;
						if(document.getElementById('akceptuj_button'))	document.getElementById('akceptuj_button').disabled = false;
						if(document.getElementById('zablokuj_button'))	document.getElementById('zablokuj_button').disabled = false;
						else if(document.getElementById('odblokuj_button'))	document.getElementById('odblokuj_button').disabled = false;
						if(document.getElementById('wyslij_button'))	document.getElementById('wyslij_button').disabled = false;
					}  	
            
                document.getElementById(id).innerHTML = TextHTML;
            } else {
                document.getElementById(id).innerHTML = '<div class="info_box_bottom"><div class="info_box_top"><div class="info_box_ok">Wystąpił problem z zapytaniem.</div></div></div>';
            }
        }

    }
    
    function makeRequest2(url,id) {
		
		url= url.split("&amp;").join("&");
        http_request2 = false;
		
		if(id == 'note_ajax') document.getElementById(id).innerHTML = '<img src="img/loading.gif" alt="loading" style="margin-left: 45px; margin-top: 10px;" />';
     //   else document.getElementById(id).innerHTML = '<img src="img/loading.gif" alt="loading" style="display: block; margin-left: 300px; margin-top: 0px;" />';
		if (window.XMLHttpRequest) { 
            http_request2 = new XMLHttpRequest();
            if (http_request2.overrideMimeType) {
                http_request2.overrideMimeType('text/xml');

            }
        } else if (window.ActiveXObject) { 
            try {
                http_request2 = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request2 = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request2) {
            alert('Nie mozna stworzyc instancji obiektu XMLHTTP');
            return false;
        }
        http_request2.onreadystatechange = function() { Contents(http_request2,id); };
        http_request2.open('GET', url, true);
        http_request2.send(null);

    }



    function Contents2(http_request2,id) {

        if (http_request2.readyState == 4) {
            if (http_request2.status == 200) {
            	var TextHTML = http_request2.responseText;
            	if(hashurl == 'opinions_ajax'){
            		var startpos = TextHTML.indexOf( "#opinion_comm_pre_start", 1 );
            		var endpos = TextHTML.indexOf( "#opinion_comm_pre_end", 1 );
               		if(startpos>0){
            		var comm = TextHTML.substr(startpos+23,endpos-startpos-23);	
					document.getElementById('opinion_alert').innerHTML = comm;
					TextHTML = TextHTML.replace("#opinion_comm_pre_start"+comm+"#opinion_comm_pre_end", "");
					}
				}  	
            
                document.getElementById(id).innerHTML = TextHTML;
            } else {
                document.getElementById(id).innerHTML = '<div class="info_box_bottom"><div class="info_box_top"><div class="info_box_ok">Wystąpił problem z zapytaniem.</div></div></div>';
            }
        }

    }
    
  function setHashObserver ()
{
window.setInterval ("observeHash()", 1);
}



function setHashUrl(url, query){
	hashurl = url;
	hashquery = query;
}

function setHashOtherUrl(url, query){
	ohashurl = url;
	ohashquery = query;
}

function getNaviSite(newhash, num){
//window.location.hash != ''
	if(nav.test(window.location.hash)){
		var hashes = window.location.hash.split("b");
		 
		if(newhash == 'p'){
			if(!hashes[1]) hashes[1] = '1';
			var dest = '#p'+num+'b'+hashes[1];  
			}
		else if(newhash == 'b'){
			if(!hashes[0]) hashes[0] = '#p1';
			var dest = ''+hashes[0]+'b'+num;  
			}
		
	}else{
		
		if(newhash == 'p') var dest = '#p'+num+'b1';
		else if(newhash == 'b') var dest = '#p1b'+num;
		
	}
	window.location.href = window.location.href.replace(window.location.hash,"") + dest;
	return false;
}



function observeHash ()
{

if(window.location.hash != '#op' && window.location.hash != "#opinion_box" && window.location.hash != "#opinion_alert"){
	

	if ((window.location.hash != lastHash || i == 0 || j == 0) && (nav.test(window.location.hash) || window.location.hash == ''))
	{
		
		if(window.location.hash == '') var hash = '#p1b1';
		else  var hash = window.location.hash;
     	var hashes = window.location.hash.split("b");
     	if(hashes[0])	var page = hashes[0].replace ('#p', '');
     	else var page = 1;
		if(hashes[1]) var other = hashes[1]; 
		else var other = 1;
	     
		if (page != lastPage || i == 0){
              	
		if(document.getElementById('comm') && i != 0) document.getElementById('comm').innerHTML = '';
		if(document.getElementById(hashurl)){
		document.getElementById(hashurl).innerHTML = '<div class="main_top_border"></div><div class="main_article_border"><div id="grey_loader_text">Trwa ładowanie...</div></div><div class="main_bottom_border">';
		i = page;
		makeRequest(hashurl+'.php?asite='+i+'&'+hashquery, hashurl);
		scroll(0,0);
		lastPage = i;
		}
		}
//		alert(other+':'+j+':'+lastOther);
		
		if (document.getElementById(ohashurl) && (other != lastOther || j == 0)){
			
			document.getElementById(ohashurl).innerHTML = '<div id="darkgrey_loader_text">Trwa ładowanie...</div>';
		j = other;
	//	alert(ohashurl+'.php?asite='+j+'&'+ohashquery, ohashurl);
	
		makeRequest2(ohashurl+'.php?asite='+j+'&'+ohashquery, ohashurl);
		lastOther = j;			
			
		}
		lastHash = window.location.hash;
		
	}
}else{
	
	if(i == 0){
		
		var hash = '#p1';
		
		if(j == 0 ) hash = '#p1b1';
		
		
		document.getElementById(hashurl).innerHTML = '<div class="main_top_border"></div><div class="main_article_border"><div id="grey_loader_text">Trwa ładowanie...</div></div><div class="main_bottom_border">';
		if(document.getElementById('comm') && i != 0) document.getElementById('comm').innerHTML = '';
		i = 1;
		makeRequest(hashurl+'.php?asite='+i+'&'+hashquery, hashurl);
		scroll(0,0);
		lastPage = i;
		
		if(document.getElementById(ohashurl) && j == 0 ){
			
			document.getElementById(ohashurl).innerHTML = '<div id="darkgrey_loader_text">Trwa ładowanie...</div>';
		j = 1;

	
		makeRequest2(ohashurl+'.php?asite='+j+'&'+ohashquery, ohashurl);
		lastOther = j;	
			
		}
		lastHash = window.location.hash;
		
		
	}
	
	
}


}
