function ustawDOM(idname)
{
	if (document.getElementById)
		{ return document.getElementById(idname); }
	else if (document.all) 
		{ return document.all[idname]; }
	else if (document.layers)
		{ return document.layers[idname]; }
	else
		{ return null; }
}
	
	
function popup(url,name,features) 
{
  window.open(url,name,features);
}

function submitform(formname)
{
	var frm = document.getElementById(formname);
	frm.submit();
}

function show_pic(name, picture)
{
var thumb = document.getElementById(name);
document.getElementById('pic_main').src = 'incl/pic_resizer.php?fileName=produkty/'+picture+'&size=200';
document.getElementById('pic_main_link').href = 'upload/produkty/'+picture;
}


function validacjaTYP(cel,typ)
{
	if($(cel))
	{ 
		var vartosc = $(cel).value;
		if(typ=='tekst')
		{
			if(vartosc=='')
			{
				$(cel).style.backgroundColor="#FFD9D9";
				return false;
			}
			else
			{
				$(cel).style.backgroundColor="#ECFFEC";
				return true;
			}	
		}
		if(typ=='email')
		{
			if(vartosc=='')
			{
				$(cel).style.backgroundColor="#FFD9D9";
				return false;
			}
			else
			{
				if(vartosc.indexOf('@')>-1 && vartosc.indexOf('.')>-1)
				{
					$(cel).style.backgroundColor="#ECFFEC";
					return true;		
				}
				else
				{
					$(cel).style.backgroundColor="#FFD9D9";
					return false;
				}
			}	
		}
		if(typ=='liczba')
		{
			if(vartosc=='')
			{
				$(cel).style.backgroundColor="#FFD9D9";
				return false;
			}
			else
			{
				vartosc = parseFloat(vartosc );
				if(isNaN(vartosc))
				{
					$(cel).style.backgroundColor="#FFD9D9";
					return false;	
				}
				else
				{
					$(cel).style.backgroundColor="#ECFFEC";
					return true;	
				}
			}	
		}
	}
}

function sprawdzenie()
{
	var komunikat = '';
	if(validacjaTYP('imie','tekst')==false)				{ komunikat = komunikat + 'Imię' + '\r\n';      }
	if(validacjaTYP('nazwisko','tekst')==false)			{ komunikat = komunikat + 'Nazwisko' + '\r\n';      }
	if(validacjaTYP('login','tekst')==false)			{ komunikat = komunikat + 'Login' + '\r\n';      }
	if(validacjaTYP('password','tekst')==false)			{ komunikat = komunikat + 'Hasło' + '\r\n';      }
	if(validacjaTYP('password_second','tekst')==false)	{ komunikat = komunikat + 'Hasło powtórzone' + '\r\n';      }
	if($('password').value!=$('password_second').value)	{ komunikat = komunikat + 'Błędnie powtórzone hasło' + '\r\n'; $('password_second').style.backgroundColor="#FFD9D9"; }
	if(validacjaTYP('email','email')==false)			{ komunikat = komunikat + 'Email' + '\r\n';      }
	if(validacjaTYP('ulica_nr_domu','tekst')==false)	{ komunikat = komunikat + 'Ulica i numer domu' + '\r\n';      }
	if(validacjaTYP('wojewodztwo','liczba')==false)		{ komunikat = komunikat + 'Wojewodztwo' + '\r\n';      }
	if(validacjaTYP('miasto','tekst')==false)			{ komunikat = komunikat + 'Miasto' + '\r\n';      }
	if(validacjaTYP('kod_pocztowy','tekst')==false)		{ komunikat = komunikat + 'Kod pocztowy' + '\r\n';      }
	if(validacjaTYP('telefon','tekst')==false)			{ komunikat = komunikat + 'Telefon' + '\r\n';      }

    if ($('firma').checked)
	{
	   //alert('ok');
       if(validacjaTYP('nip','tekst')==false)			{ komunikat = komunikat + 'NIP' + '\r\n'; }
       if(validacjaTYP('nazwa_firmy','tekst')==false)			{ komunikat = komunikat + 'Nazwa firmy' + '\r\n'; }
	}

	if (komunikat!='')
	{
		alert ('Wymagane pola: \r\n \r\n ' + komunikat);
		return false;
	}
	else
	{
		submitform('rejestracja');
		return true;
	}
}





function newsletter_del()
{
	var eml = $('f_newsletter').value;
	var linkus='ajax.php';
	var czym="post";
	var par="oper=newsletterDel&eml="+eml;
	var myAjax = new Ajax.Request( linkus, { method: czym, parameters: par  , onComplete:function(odp) {
																											var stan = parseInt(odp.responseText);
																												if(stan==1)
																													{alert('Adres usuniety');	}
																												else
																													{alert('Blad zapisu');	} 
																										}
											});	
}
function newsletter()
{
	if(validacjaTYP('f_newsletter','email')==false)
		{ alert('Pusty  lub bledny adres email');}
	else
		{
			var eml = $('f_newsletter').value;
			var linkus='ajax.php';
			var czym="post";
			var par="oper=newsletter&eml="+eml;
			var myAjax = new Ajax.Request( linkus, { method: czym, parameters: par  , onComplete:function(odp)
																											 {
																												var stan = parseInt(odp.responseText);
																												if(stan==1)
																													{alert('Adres dodany do bazy danych');	}
																												else if(stan==2)
																												{
																													if(confirm("Podany adres isteje juz w bazie danych!! Usunac go ?"))
																														{newsletter_del();}
																												}
																												else
																													{alert('Blad zapisu');	}
																											 }
													});
		}
}

function wyczysc_wartosc(POLE)
{
	$(POLE).value = '';
}


function o_prawna(WART)
{
  if (WART=='firma')
  {
    $('nip_komunikat').innerHTML = '<span style="color:red;">Proszę podać numer NIP</span>';
    $('nazwa_firmy').style.display = 'inline';
    $('nazwa_firmy_opis').style.display = 'inline';
    $('nip').style.display = 'inline';
    $('nip_opis').style.display = 'inline';
  }
  else
  {
    $('nip_komunikat').innerHTML = '';
    $('nazwa_firmy').style.display = 'none';
    $('nazwa_firmy_opis').style.display = 'none';
    $('nip').style.display = 'none';
    $('nip_opis').style.display = 'none';
  }

}
