var foto_dir = 'http://www.projekty.ign.com.pl/';
//var foto_dir = 'http://projekty/';
//var foto_dir = 'http://www.domfort.eu/';
//var foto_dir = 'http://domfort/';
var ETest  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var TTest  = /^([0-9\-\ \(\)])+$/;
var NTest  = /^([0-9\-\ ])+$/;
var KTest  = /^([0-9])+$/;

function changeFacebookStatus(url,rootdiv)
{       
	//tekst = "<div style=\"clear: both; width: 120px; margin: 0px 0 0px 0px;\"><a name=\"fb_share\" type=\"icon\" share_url=\"" + url + "\" href=\"http://www.facebook.com/sharer.php?t=Projekt domu\" title=\"Udostępnij na Facebook'u\">Udostępnij</a><div style=\"margin-top: -20px; margin-left: 20px;\"><iframe src=\"//www.facebook.com/plugins/like.php?href=" + url + "&amp;send=false&amp;layout=button_count&amp;width=250&amp;show_faces=true&amp;action=like&amp;colorscheme=light&amp;font=tahoma&amp;height=21\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:450px; height:21px;\" allowTransparency=\"true\"></iframe></div></div>";
	
	tekst = "<div style=\"clear: both; width: 230px; margin: 0px 0 0px 0px;\"><div style=\"margin-left: 5px;\"><iframe src=\"//www.facebook.com/plugins/like.php?href=" + url + "&amp;send=false&amp;layout=button_count&amp;width=250&amp;show_faces=true&amp;action=like&amp;colorscheme=light&amp;font=tahoma&amp;height=21\" scrolling=\"no\" frameborder=\"0\" style=\"border:none; overflow:hidden; width:450px; height:21px;\" allowTransparency=\"true\"></iframe></div></div>";

        document.getElementById(rootdiv).innerHTML = tekst;
        
}

function displayWindow(url, width, height)
{
   window.open(url,"displayWindow",'width=' + width + ',height=' + height +
      ',resizable=no,scrollbars=yes,menubar=no,personalbar=no,toolbar=no,' +
      'status=no' );
}

function zmien_projekt()
{
   if (document.getElementById('proj_id').value != '-')
      return true;
   else
      return false;
}

function nowe_okno(adres)
{
   window.open(adres,'','');
   return false;
}

function zm_mini(imgMini, imgZoom, id)
{
   Widok = imgZoom;
   document.getElementById('widoczek').src = foto_dir + 'pliki/' + id + '/widoki/' + imgMini;
}

function displayTablica(nazwa, width, height)
{
   window.open("popup/tablica.php?nazwa=" + nazwa,"displayWindow",'width=' + width + ',height=' + height +
      ',resizable=no,scrollbars=no,menubar=no,personalbar=no,toolbar=no,' +
      'status=no' );
}

function displayArchPakiet(nazwa, width, height)
{
   window.open("popup/archpakiet.php?nazwa=" + nazwa,"displayWindow",'width=' + width + ',height=' + height +
      ',resizable=no,scrollbars=no,menubar=no,personalbar=no,toolbar=no,' +
      'status=no' );
}

function duzyWidok(Widok, IDP)
{
   window.open('popup.php?f='+Widok+'&c=widoki&idp='+IDP, 'ZOOM', 'width=620,height=460,'+
               'resizable=no,scrollbars=no,menubar=no,personalbar=no,toolbar=no,status=no');
}

function odbLustrzane(Idn)
{
   window.open('popup/lustro.php?p='+Idn, 'Odbicie', 'width=620,height=420,'+
               'resizable=no,scrollbars=yes,menubar=no,personalbar=no,toolbar=no,status=no');
}
function drukuj_proj(Idn)
{
   window.open('popup/drukuj.php?p='+Idn, 'Drukowanie', 'width=620,height=420,'+
               'resizable=no,scrollbars=yes,menubar=no,personalbar=no,toolbar=no,status=no');
}

function obrotSwiatla(Idn)
{
   window.open('popup/obracanie.php?p='+Idn, 'Obrot', 'width=786,height=550,'+
               'resizable=no,scrollbars=yes,menubar=no,personalbar=no,toolbar=no,status=no');
}

function obrot3d(Idn)
{
   window.open('popup/obrot.php?p='+Idn, 'Obrot', 'width=500,height=430,'+
               'resizable=no,scrollbars=yes,menubar=no,personalbar=no,toolbar=no,status=no');
}

function fotos(Idn, ktg)
{
   window.open('popup/foteczki.php?p='+Idn+'&k='+ktg, 'Obrot', 'width=620,height=400,'+
               'resizable=no,scrollbars=yes,menubar=no,personalbar=no,toolbar=no,status=no');
}

function pokaz_ukryj (idn, disp)
{
   document.getElementById(idn).style.display = disp;
   
   if (disp == 'block')
   {
      document.getElementById(idn+'_t').innerHTML = "&laquo; ukryj";
      disp = 'none';
   }
   else
   {
      document.getElementById(idn+'_t').innerHTML = "zobacz &raquo;";
      disp = 'block'
   }
   return disp;
}

function powiekszRzut(rzut, IDP)
{
   window.open('popup.php?f='+rzut+'&c=rzuty&idp='+IDP, 'ZOOM', 'width=700,height=470,'+
               'resizable=no,scrollbars=no,menubar=no,personalbar=no,toolbar=no,status=no');
}

function danewysylka()
{
   if (document.getElementById('inne').checked == 1)
   {
      document.getElementById('dane_wysylka').style.display='block';
   }
   else
   {
     document.getElementById('dane_wysylka').style.display='none';
   }
}

function czk_formularz()
{
   var error= '';
   var wersja = 0;

   radio= document.getElementsByName('wersja');

   for(i=0; i<radio.length; i++)
   {
      if (radio[i].checked == 1) wersja = 1;
   }
   if (wersja != 1) error += "Proszę wybrać wersję projektu!\n";
   
   if (document.getElementById('firma').value == '') {
	if (document.getElementById('imie').value.length < 3) error += "Proszę podać imię!\n";
	if (document.getElementById('nazw').value.length < 3) error += "Proszę podać nazwisko!\n";
   } else {
	if (document.getElementById('nip').value == '') error += "Proszę podać numer NIP!\n";
   }
   if (document.getElementById('ulica').value.length < 3) error += "Proszę podać ulicę i nr domu!\n";
   if (document.getElementById('kod1').value.length < 2 || document.getElementById('kod2').value.length < 3)
      error += "Proszę podać kod pocztowy!\n";
   if (document.getElementById('kod1').value.length == 2 && document.getElementById('kod2').value.length == 3) {
       if(!KTest.test(document.getElementById('kod1').value) || !KTest.test(document.getElementById('kod2').value) ) error += "Podany kod jest błędny!\n";
   }
   if (document.getElementById('miasto').value.length < 3) error += "Proszę podać miejscowość!\n";
   
   if (document.getElementById('nip').value.length > 0) {
     if(!NTest.test(document.getElementById('nip').value)) error += "Podany numer nip jest błędny!\n";
   }
   
   if (document.getElementById('tel').value.length < 9) error += "Proszę podać telefon kontaktowy!\n";
   if (!TTest.test(document.getElementById('tel').value)) error += "Podany numer telefonu jest blędny!\n";   
   
   if (error != '')
   {
      alert (error);
      return false;
   }
   else
   {
      document.getElementById('subm').setAttribute('disabled','true');
      return true;
   }
}

function inp_email (idn,dostepny)
{
   window.open('popup_wyslij_email.php?i='+idn+'&d='+dostepny,'', 'width=200,height=70,'+
               'resizable=no,scrollbars=no,menubar=no,personalbar=no,toolbar=no,status=no');
}

function inp_drukuj (idn)
{
   window.open('popup_drukuj_zamowienie.php?p='+idn, '', 'width=600,height=400,'+
               'resizable=no,scrollbars=yes,menubar=no,personalbar=no,toolbar=no,status=no');
}

function reklamacja()
{
   error = '';
   if (document.getElementById('imie').value == '') error += "Podaj imię\n";
   if (document.getElementById('tel').value == '') error += "Podaj telefon\n";
   if (document.getElementById('meil').value == '') error += "Podaj adres e-mail\n";
   if (document.getElementById('tresc').value == '') error += "Podaj tresc reklamacji!\n";
   if (document.getElementById('WyrazamZgode').checked != 1) error += "Musisz wyrazić zgodę na przetwarzanie danych!";
   
   if (error != '')
   {
      alert (error);
      return false;
   }
   else
      return true;
}

function zglosblad()
{
   error = '';
   if (document.getElementById('tresc').value == '') error += "Podaj opis bledu\n";
   if (document.getElementById('lnk').value == '') error += "Podaj link do bledu\n";
   if (!ETest.test(document.getElementById('mejl').value)) error += "Podaj poprawny adres e-mail.\n";
   
   if (error != '')
   {
      alert (error);
      return false;
   }
   else
      return true;
}

function lewemenu(nr_id)
{
   if (document.getElementById(nr_id).style.display == 'none')
   {
      document.getElementById(nr_id).style.display = 'block';
      document.getElementById(nr_id+'i').src = 'pics/minus.gif';
   }
   else
   {
      document.getElementById(nr_id).style.display = 'none';
      document.getElementById(nr_id+'i').src = 'pics/plus.gif';
   }
}

function polaczenie_szyfrowane()
{
	if (location.protocol=="http:") {
  		location.href = location.href.replace(/^http:/, "https:")
	}
}

function polaczenie_nieszyfrowane()
{
	if (location.protocol=="https:") {
  		location.href = location.href.replace(/^https:/, "http:")
	}
}

function czyregulamin()
{
if (document.getElementById('regulamin').checked == 1)
   { return true; } 
   else {    
     alert ("Aby złożyć zamówienie trzeba zaakceptować regulamin");
     return false; 
   }
}

function pokaz_regulamin(url, width, height)
{
   window.open(url,"displayWindow",'width=' + width + ',height=' + height +
      ',resizable=no,scrollbars=yes,menubar=no,personalbar=no,toolbar=no,' +
      'status=no' );
}

function szczegoly(plik, typ, Id, proj)
{
   window.open('popup/'+plik+'.php?p='+Id, typ+' do projektu '+proj, 'width=620,height=400,'+
               'resizable=no,scrollbars=yes,menubar=yo,personalbar=no,toolbar=no,status=no');
}

function wstecz()
{
  history.back();
}

function promesa(url, width, height, nazwa)
{
   window.open(url + '?nazwa=' + nazwa ,"displayWindow",'width=' + width + ',height=' + height +
      ',resizable=yes,scrollbars=yes,menubar=no,personalbar=no,toolbar=no,' +
      'status=no' );
}

function charakterystyka(url, width, height, nazwa)
{
   window.open(url + '?nazwa=' + nazwa ,"displayWindow",'width=' + width + ',height=' + height +
      ',resizable=no,scrollbars=yes,menubar=no,personalbar=no,toolbar=no,' +
      'status=no' );
}

function rodzinanaswoim()
{
   window.open('popup/rodzinanaswoim.php',
   "displayWindow", 'width=620,height=400,' + 'resizable=no,scrollbars=yes,menubar=yo,personalbar=no,toolbar=no,status=no');
}

function dodajdoulubionych() {

    var tytul = 'Domfort - projekty domów';
    var adres = 'http://www.domfort.eu';

    //FireFox
    if (window.sidebar) {

        window.sidebar.addPanel(tytul, adres, "");

    //IE
    } else if (window.external) {

        window.external.AddFavorite(adres, tytul);

    }
}

function main_szukaj_func(co,gdzie) {
var pokaz = 'WYSZUKIWARKA PROJEKTÓW - rozwiń';
var ukryj = 'WYSZUKIWARKA PROJEKTÓW - ukryj';
	if(document.getElementById(gdzie).innerHTML == pokaz) { 
		document.getElementById(co).style.display = "block";
		document.getElementById(gdzie).innerHTML = ukryj;
	} else {
		if(document.getElementById(gdzie).innerHTML == ukryj) { 
			document.getElementById(co).style.display = "none";
			document.getElementById(gdzie).innerHTML = pokaz;
		}
	}
}

function promocja_func(co,gdzie) {
var pokaz = 'pokaż';
var ukryj = 'ukryj';
	if(document.getElementById(gdzie).innerHTML == pokaz) { 
		document.getElementById(co).style.display = "block";
		document.getElementById(gdzie).innerHTML = ukryj;
	} else {
		if(document.getElementById(gdzie).innerHTML == ukryj) { 
			document.getElementById(co).style.display = "none";
			document.getElementById(gdzie).innerHTML = pokaz;
		}
	}
}

function zgoda_func(co,gdzie) {
var pokaz = 'pokaż';
var ukryj = 'ukryj';
	if(document.getElementById(gdzie).innerHTML == pokaz) {
		document.getElementById(co).style.display = "block";
		document.getElementById(gdzie).innerHTML = ukryj;
	} else {
		if(document.getElementById(gdzie).innerHTML == ukryj) {
			document.getElementById(co).style.display = "none";
			document.getElementById(gdzie).innerHTML = pokaz;
		}
	}
}

function fj_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=fj_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function fj_showHideLayers() {
  var i,p,v,obj,args=fj_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=fj_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

var bluractive = false;

function str_replace (search, replace, subject, count) {
    var i = 0,
        j = 0,
        temp = '',
        repl = '',
        sl = 0,
        fl = 0,
        f = [].concat(search),
        r = [].concat(replace),
        s = subject,
        ra = Object.prototype.toString.call(r) === '[object Array]',
        sa = Object.prototype.toString.call(s) === '[object Array]';
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
 
    for (i = 0, sl = s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j = 0, fl = f.length; j < fl; j++) {
            temp = s[i] + '';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length - s[i].length) / f[j].length;
            }
        }
    }
    return sa ? s : s[0];
}

function OnBlur () {
	if(bluractive) document.getElementById('sug_div').style.display='none';
}

function ajaxFunction(){
	var szukajnazwa = document.getElementById('nazwa').value;
if(szukajnazwa.length > 0) {
	var sug_div = document.getElementById('sug_div');
	sug_div.innerHTML += '<p><img src="../gfx/ajax-loader.gif" width="50" height="50" /></p>';
	sug_div.style.display = "block";
	var ajaxRequest; // The variable that makes Ajax possible!

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
 			if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200){
		   		var sug_div = document.getElementById('sug_div');
 			  	var pl_z = new Array ('ą','ę','ó','ł','ź','ż','ś','ć','ń',' ','(',')','"');
				var pl_na = new Array('a','e','o','l','z','z','s','c','n','_','','','');
				//clear document
				sug_div.innerHTML = '';
				var xmldoc = ajaxRequest.responseXML;
				var projekt_nodes = xmldoc.getElementsByTagName("projekt"); 
				var n_projekt = projekt_nodes.length;
				for (i = 0; i < n_projekt; i++) {
				  	var id = projekt_nodes[i].getAttribute('id');
					var nazwa_node = projekt_nodes[i].getElementsByTagName("nazwa");
					var nazwaurl = str_replace (pl_z, pl_na, nazwa_node[0].firstChild.nodeValue.toLowerCase());
					var thumb_node = projekt_nodes[i].getElementsByTagName("thumb");
					var pow_uz_node = projekt_nodes[i].getElementsByTagName("pow_uz");
					sug_div.innerHTML += '<table class="projekt" onclick="location.href=\'http://www.domfort.eu/projekt,'+nazwaurl+','+id+'.htm\'"><tr onmouseover="this.className=\'over\'; bluractive=false;" onmouseout="this.className=\'out\'; bluractive=true;"><td><img class="miniatura" src="'+foto_dir+'pliki/'+ id +'/'+ thumb_node[0].firstChild.nodeValue + '" alt="Projekt domu '+thumb_node[0].firstChild.nodeValue+'" /></td><td><table><tr><td><strong>'+nazwa_node[0].firstChild.nodeValue+'</strong> - '+id+'</td></tr><tr><td>pow. użytkowa: <span>'+pow_uz_node[0].firstChild.nodeValue+'</span></td></tr></table></td></tr></table>';
				}
				if(n_projekt > 0) sug_div.style.display = "block";
			}
	}
	var param = 'nazwa=' + szukajnazwa;
	ajaxRequest.open("POST", "sugestions/getSugestions.php", true);
	ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-2');
	ajaxRequest.send(param);
} else {
	var sug_div = document.getElementById('sug_div');
	sug_div.style.display = "none";
}
}

