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 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 pok()
{
ShowHideLayer("nazwa",true);
ShowHideLayer("dpobranie",false);
}

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

if (document.x.D1.value!="9999")
{
	alert ("UWAGA: jeśli chcesz zamówić towar za granicę, to jedyna możliwa forma płatności to płatność przez Platnosci.pl");
	
	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 ukr()
{
ShowHideLayer("nazwa",false);
ShowHideLayer("dpobranie",false);

if (document.x.D1.value!="9999")
{
	alert ("UWAGA: jeśli chcesz zamówić towar za granicę, to jedyna możliwa forma płatności to płatność przez Platnosci.pl");
	
	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 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 validateForm(AForm)
{
	var blad=0;
	var tekst="";
	
	if (document.x.imie.value==""){
		tekst=tekst+"Wpisz swoje imię\n";
	}
	
	if (document.x.nazwisko.value==""){
		tekst=tekst+"Wpisz swoje nazwisko\n";
	}

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

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

	
	if (!echeck(document.x.email.value)){
		tekst=tekst+"Nieprawidłowy format adresu e-mail!\n";
	}
	

//    if (tekst!="") blad=1;
    if (tekst!="") {
        alert ("WYSTAPIŁY NASTĘPUJĄCE BŁĘDY\n"+tekst);
        return false;
    } else {
        return true;
    }
}


function spr(pole)
{
	var kOK='<font color=green>OK</font>';
	var kBlad='<font color=red>Błąd</font>';
	var komunikat='';

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

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

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

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

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

	   if (!echeck(document.x.email.value)) komunikat = kBlad; else komunikat = kOK;
	}
	
	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;
}