// sprawdzanie poprawno»ci emaila
function emailCheck (emailStr) {
var checkTLD=1; // sprawdzanie ko˝c·wki
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=emailStr.match(emailPat);
if (matchArray==null) {
alert("Niepoprawny adres email (sprawdĽ położenie @ oraz) .");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];
for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Nazwa użytkownika zawiera niedozwolone znaki.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Nazwa domeny zawiera niedozwolone znaki.");
return false;
   }
}
if (user.match(userPat)==null) {
alert("Niepoprawna nazwa użytkownika.");
return false;
}
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Docelowy adres IP jest niepoprawny!");
return false;
   }
}
return true;
}
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("Niepoprwana nazwa domeny.");
return false;
   }
}
if (checkTLD && domArr[domArr.length-1].length!=2 &&
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("Adres musi się kończyć nazwę domeny lub dwuliterowej nazwy " + "kraju.");
return false;
}
if (len<2) {
alert("W adresie brakuje nazwy hosta!");
return false;
}
return true;
}

function obetnij_spacje(Str){
	dlugosc=Str.length;
 // obcinanie spacji
	while (Str.slice(0,1) == " "){
 	 Str = Str.substr(1,dlugosc-1);
 	 dlugosc = Str.length
	}
	while(Str.slice(dlugosc-1,dlugosc)== " "){
 	 Str = Str.substr(0,dlugosc-1);
 	 dlugosc = postcodeStr.length
 	}
	return Str;
}


// czy puste pole
function blank(pole){
	return (pole=="");
}


function visible(menu,off) {
	if (document.getElementById) { // IE NS6
		obj = document.getElementById(menu);
		off = parseInt(off);
		if(off>0) obj.style.display='none';
		else obj.style.display='';
	}
	else { // NS4
		if(off>0) eval("document.layers['"+menu+"'].display='none';");
		else eval("document.layers['"+menu+"'].display='';");
	}
}

function setValue(obj, param, val) {
	if (document.getElementById) { // IE NS6
		obj = eval("document.mailer_zapytanie."+obj+"."+param+"="+val);
	}
}

function wspolpraca_f(off) {
	off = parseInt(off);
	if(off>0){
		visible('tr_rodz_opis', 1);
		visible('tr_rodz', 1);
		visible('tr_lok_opis', 1);
		visible('tr_lok', 1);
		visible('tr_klips_opis', 1);
		visible('tr_klips', 1);
		visible('tr_etykieta_opis', 1);
		visible('tr_etykieta', 1);
		visible('tr_ilosc_opis', 1);
		visible('tr_ilosc', 1);
		setValue('il_przejsc[0]','checked','true');
		visible('tr_szer_opis', 1);
		visible('tr_sz_przejscia_1', 1);
		visible('tr_sz_przejscia_2', 1);
		visible('tr_sz_przejscia_3', 1);
		visible('tr_montaz_opis', 1);
		visible('tr_montaz', 1);
	}
	else{
		visible('tr_rodz_opis', 0);
		visible('tr_rodz', 0);
		visible('tr_lok_opis', 0);
		visible('tr_lok', 0);
		visible('tr_klips_opis', 0);
		visible('tr_klips', 0);
		visible('tr_etykieta_opis', 0);
		visible('tr_etykieta', 0);
		visible('tr_ilosc_opis', 0);
		visible('tr_ilosc', 0);
		setValue('il_przejsc[0]','checked','true');

		visible('tr_szer_opis', 0);
		visible('tr_sz_przejscia_1', 0);
		visible('tr_sz_przejscia_2', 1);
		visible('tr_sz_przejscia_3', 1);
		visible('tr_montaz_opis', 0);
		visible('tr_montaz', 0);
	}
}


function test(forma){
ok=true;
if(ok&&blank(obetnij_spacje(forma.elements['p_firma'].value)))
	{	forma.elements['p_firma'].style.background = "#ff0000";	ok=false;}

if(ok&&blank(obetnij_spacje(forma.elements['miasto'].value)))
	{	forma.elements['miasto'].style.background = "#ff0000";	ok=false;}

if(ok&&blank(obetnij_spacje(forma.elements['telefon_2'].value)))
	{	forma.elements['telefon_2'].style.background = "#ff0000";	ok=false;} //tel


if(ok&&!emailCheck(forma.elements['email_address'].value))
	{	forma.elements['email_address'].style.background = "#ff0000";	ok=false;} 			//email

(ok==true)?forma.submit():alert("Wprowadzono niepoprawne dane!");
}
