var theDoc = document,
    theAll = theDoc.all ? theDoc.all : null,
    isDOM = theDoc.getElementById ? true : false,
    pos = navigator.userAgent.toLowerCase().indexOf("opera"),
    isElektra = pos!=-1 && parseFloat(navigator.userAgent.substring(pos+6))<7,
    isntShowHideOK = !isDOM && theAll==null;


function popupx(){
	pop = window.open ('wysylka.php','pop','toolbar=no,location=no,directories=no,status=no,menubar=no,width=560,height=350,scrollbars=no,resizable=no');
	pop.focus();
}


function popupxo(){
	pop = window.open ('wysylkao.php','pop','toolbar=no,location=no,directories=no,status=no,menubar=no,width=560,height=350,scrollbars=no,resizable=no');
	pop.focus();
}

function popupxbw(){
	pop = window.open ('wysylkabw.php','pop','toolbar=no,location=no,directories=no,status=no,menubar=no,width=560,height=350,scrollbars=no,resizable=no');
	pop.focus();
}


function popcholsub(){
	pop = window.open ('zapiszsie.php','pop','toolbar=no,location=no,directories=no,status=no,menubar=no,width=360,height=590,scrollbars=no,resizable=no');
	pop.focus();
}


function ShowHideLayer(name, state) {
	if (isntShowHideOK) return null;
	var obj = isDOM ? theDoc.getElementById(name) : theAll[name];
	if (arguments.length<2) {
		state = obj.style.display=="none";
	}
	obj.style.display = state ? "block" : "none";
	return state;
}

//Funkcja pobrana z http://www.smartwebby.com/dhtml/
function echeck(str)
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}
	
	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }
	
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }
	
	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    return false
	 }
	 
	 if (str.substring(str.length-1,str.length)=='.') {
	 	return false
	 }
	
	 return true					
}


function kraj()
{
	if (document.x.D1.value!="9999")
	{
	var radios = document.forms.x.R1;
	for( var i=0 ; i < radios.length ; i++ )
	    if ( radios[ i ].value == 'platnosci' ) radios[ i ].checked = true;	
	    pok();
	}
}

function zaznacz_platnosci() {
	if (document.x.D1.value!="9999")
	{
		alert ("UWAGA: za granicę nie można zamawiać za pobraniem!");
		
		var radios = document.forms.x.R1;
		for( var i=0 ; i < radios.length ; i++ )
		    if ( radios[ i ].value == 'platnosci' ) radios[ i ].checked = true;	
		    
		    pok();
	}
}

function pok() {
	ShowHideLayer("nazwa",true);
	ShowHideLayer("dpobranie",false);
}

function pok2() {
	ShowHideLayer("dpobranie",true);
	ShowHideLayer("nazwa",false);
	
	zaznacz_platnosci();
}

function ukr() {
	ShowHideLayer("nazwa",false);
	ShowHideLayer("dpobranie",false);
}


function ukr2() {
	ShowHideLayer("nazwa",false);
	ShowHideLayer("dpobranie",false);
	
	zaznacz_platnosci();
}


function kodp(kp)
{
	var dl=kp.length;
	var wynik=true;
	var q='';

	if (document.x.D1.value=="9999")
	{
	if (dl!=6) return false;
	
	for(a=0; a<dl; a++)
	{
		q=kp.charAt(a);
		
		if (a!=2)
		{
			y=q.charCodeAt(0);
			if (y<48 || y>57) wynik=false;
		}
		else
		{
			if (q!='-') wynik=false;
		}
	}
	}
	else
	{
		if (dl<2) return false;
		wynik=true;
	}
	return wynik;
}

function valt(tel)
{
	var dl=tel.length;
	var ile=0;
	var q='';
	
	if (document.x.D1.value=="9999")
	{
	for (a=0; a<dl; a++)
	{
		q=tel.charCodeAt(a);
		if (q>47 && q<58) ile++;
	}
	
	if (ile<9) return false; else return true;
	}
	else
	{
	if (dl<5) return false;
	return true;
	}
}

function pole_ok(pole) {
        var tmpel;
        tmpel = document.getElementById(pole);

	tmpel.style.background = '#FFFFFF'
}

function pole_zle(pole) {
        var tmpel;
        tmpel = document.getElementById(pole);

	tmpel.style.background = '#FF0000'
}


function validateForm(AForm) {
	var blad=0;
	var tekst="";
	var foc='';
	
	if (document.x.imie.value.length<3) {
		tekst=tekst+"Wpisz swoje imię\n";
		pole_zle('imie');
		if (foc=='') foc='imie';
	}
	
	if (document.x.nazwisko.value.length<3) {
		tekst=tekst+"Wpisz swoje nazwisko\n";
		pole_zle('nazwisko');
		if (foc=='') foc='nazwisko';
	}

	if (document.x.adres1.value.length<5){
		tekst=tekst+"Nie podałeś swojego adresu\n";
		pole_zle('adres1');
		if (foc=='') foc='adres1';
	}
	
	if (!kodp(document.x.kod.value)){
		tekst=tekst+"Nieprawidłowy format kodu pocztowego\n";
		pole_zle('kod');
		if (foc=='') foc='kod';
	}
	
	if (document.x.miasto.value.length<3){
		pole_zle('miasto');
		tekst=tekst+"Wpisz nazwę miasta (poczty)\n";
		if (foc=='') foc='miasto';
	}
	
	if (document.x.tel.value==""){
		tekst=tekst+"Podaj swój numer telefonu\n";
		if (foc=='') foc='tel';
		pole_zle('tel');
	}
	
	if (!echeck(document.x.email.value)){
		tekst=tekst+"Nieprawidłowy format adresu e-mail!\n";
		if (foc=='') foc='email';
		pole_zle('email');
	}

	if (!document.x.polpr.checked){
		tekst=tekst+"Zaakceptuj politykę prywatności! \n";
		if (foc=='') foc='polpr';
		pole_zle('polpr');
	}

    if (tekst!="") {
        alert ("WYSTAPIŁY NASTĘPUJĄCE BŁĘDY\n"+tekst);
        if (foc!='') {
        	var tmpel;
        	tmpel = document.getElementById(foc);
        	tmpel.focus();
        }
        return false;
    } else {
        return true;
    }
}


function spr(pole) {
	var kOK='<span style="color: #00FF00;">OK</span>';
	var kBlad='<span style="color: #FF0000;">Błąd</span>';
	var komunikat='';

	if (pole==1) {
	   var tmp = document.getElementById('vimie'); 
	   if (document.x.imie.value.length<3) {
	   		komunikat = kBlad;
	   		pole_zle('imie');
	   	} else { 
		   	komunikat = kOK;
	   		pole_ok('imie');
		}
	}
	
	if (pole==2) {
	   var tmp = document.getElementById('vnazwisko'); 
	   if (document.x.nazwisko.value.length<3) {
	   		pole_zle('nazwisko');
		   	komunikat = kBlad; 
	   	} else {
	   		pole_ok('nazwisko');
	   		komunikat = kOK;
	   	}
	}

	if (pole==3) {
	   var tmp = document.getElementById('vadres1'); 
	   if (document.x.adres1.value.length<5) {
	   		pole_zle('adres1');
		   	komunikat = kBlad;
	   	} else {
	   		komunikat = kOK;
	   		pole_ok('adres1');
	   	}
	}
	
	if (pole==5) {
		var tmp = document.getElementById('vkod'); 
		var zw='';
		zw=kodp(document.x.kod.value);
		   
		if (document.x.D1.value=="9999") {
			if (!zw) {
			   		pole_zle('kod');
					komunikat = '<span style="color: #FF0000;">Wpisz kod pocztowy w formacie ab-cde</span>'; 
				} else 	{
			   		pole_ok('kod');
					komunikat = kOK;
				}
		} else {
			if (!zw) {
			   		pole_zle('kod');
					komunikat = '<span style="color: #FF0000;">Wpisz kod pocztowy</span>'; 
				} else {
			   		pole_ok('kod');
					komunikat = kOK;
				}
		}
	}

	if (pole==6) {
	   var tmp = document.getElementById('vmiasto'); 
	   if (document.x.miasto.value.length<3) {
	   		pole_zle('miasto');
	   		komunikat = kBlad; 
	   	} else {
	   		pole_ok('miasto');
	   		komunikat = kOK;
	   	}
	}

	if (pole==7) {
	   var tmp = document.getElementById('vtel'); 
	   if (!valt(document.x.tel.value)) {
	   		pole_zle('tel');
	   		komunikat = kBlad; 
	   	} else {
	   		komunikat = kOK;
	   		pole_ok('tel');
	   	}
	}

	if (pole==8) {
	   var tmp = document.getElementById('vemail'); 

	   if (!echeck(document.x.email.value)) {
	   		pole_zle('email');
	   		komunikat = kBlad;
	   	} else {
	   		pole_ok('email');
	   		komunikat = kOK;
	   	}
	}

	if (tmp!=null) tmp.innerHTML = komunikat;
}

function pola_obowiazkowe() {
	var po='<span style="color: red">pole obowiązkowe</span>';
	
	document.getElementById('vimie').innerHTML = po;
	document.getElementById('vnazwisko').innerHTML = po;
	document.getElementById('vadres1').innerHTML = po;
	document.getElementById('vkod').innerHTML = po;
	document.getElementById('vmiasto').innerHTML = po;
	document.getElementById('vtel').innerHTML = po;
	document.getElementById('vemail').innerHTML = po;
}

function CheckMultiple52(frm, name) {
	for (var i=0; i < frm.length; i++)
	{
		fldObj = frm.elements[i];
		fldId = fldObj.id;
		if (fldId) {
			var fieldnamecheck=fldObj.id.indexOf(name);
			if (fieldnamecheck != -1) {
				if (fldObj.checked) {
					return true;
				}
			}
		}
	}
	return false;
}

function CheckForm52(f) {
	var email_re = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;
	if (!email_re.test(f.email.value)) {
		alert("Wprowadź swój adres mail`owy.");
		f.email.focus();
		return false;
	}

		return true;
	}



function CheckMultiple53(frm, name) {
	for (var i=0; i < frm.length; i++)
	{
		fldObj = frm.elements[i];
		fldId = fldObj.id;
		if (fldId) {
			var fieldnamecheck=fldObj.id.indexOf(name);
			if (fieldnamecheck != -1) {
				if (fldObj.checked) {
					return true;
				}
			}
		}
	}
	return false;
}

function CheckForm53(f) {
	var email_re = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;
	if (!email_re.test(f.email.value)) {
		alert("Wprowadź swój adres mail`owy.");
		f.email.focus();
		return false;
	}

		return true;
	}
