function findPosition(oElement){
	if(typeof(oElement.offsetParent)!='undefined'){
		for(var posX=0,posY=0;oElement;oElement=oElement.offsetParent) {
			posX+=oElement.offsetLeft;
			posY+=oElement.offsetTop;
		}
		return [posX,posY];
	}else{
		return [ oElement.x, oElement.y ];
	}
}

function sklep(pid,f,pole) {
	if(pole=='sklep')dest='inc/inc_sklep_query.php';
	try {
		if(pole=='sklep' && f!='b_update')xmlhttpsklep = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
		if(pole=='sklep' && f=='b_update')xmlhttpsklep_koszyk = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	}catch (e) {

	}

	if(pole=='sklep' && f!='b_update')xmlhttpsklep.onreadystatechange = sklep_query;
	if(pole=='sklep' && f=='b_update')xmlhttpsklep_koszyk.onreadystatechange = sklep_koszyk_query;
	if(pole=='sklep'){
		if(f=='p_add'){
			document.getElementById('sklep_div').style.top=findPosition(document.getElementById('k'+pid))[1]+'px';
			document.getElementById('sklep_div').style.left=findPosition(document.getElementById('k'+pid))[0]+'px';
			dest+='?f='+f;
			dest+='&pid='+pid;
			if(document.getElementById('i'+pid))dest+='&ilosc='+document.getElementById('i'+pid).value;
		}
		if(f=='b_update'){
			dest+='?f='+f;
		}
	}

	if(pole=='sklep' && f!='b_update'){
		xmlhttpsklep.open("GET", dest);
		xmlhttpsklep.send(null);
	}
	if(pole=='sklep' && f=='b_update'){
		xmlhttpsklep_koszyk.open("GET", dest);
		xmlhttpsklep_koszyk.send(null);
	}

}

function sklep_query(){
	if ((xmlhttpsklep.readyState == 4) && (xmlhttpsklep.status == 200)) {
		document.getElementById('sklep_div').innerHTML = xmlhttpsklep.responseText;
		document.getElementById('sklep_div').style.visibility='visible';
		$j('#sklep_div').fadeTo('slow',1)
		$j('#sklep_div').delay(500).fadeOut('slow')
		sklep('0','b_update','sklep');
	}
}

function sklep_koszyk_query(){
	if ((xmlhttpsklep_koszyk.readyState == 4) && (xmlhttpsklep_koszyk.status == 200)) {
//		$j('#sklep_koszyk').fadeTo('fast',0);
//		$j('#sklep_koszyk').fadeTo('fast',1);
		document.getElementById('sklep_koszyk').innerHTML = xmlhttpsklep_koszyk.responseText;
//		document.getElementById('sklep_koszyk').style.visibility='visible';
//		$j('#sklep_koszyk').fadeTo('slow',1)
//		$j('#sklep_koszyk').delay(500).fadeOut('slow')
	}
}


function validateform(formularz,tryb){
	var ok=true;
	var pola='';
	if(formularz=='firma_dodaj'){
		var polat=new Array('sklep_imie','sklep_nazwisko','sklep_mail','sklep_firma','sklep_ulica','sklep_kod','sklep_miasto','sklep_kraj','sklep_nip','sklep_login','sklep_pass');
		var polan=new Array(15,15,13,15,15,15,15,15,10,14,1);
		var polar=new Array(1,1,1,1,1,1,1,1,1,1,1);
		var polao=new Array('<li>Imię</li>','<li>Nazwisko</li>','<li>Adres mail</li>','<li>Firma</li>','<li>Ulica</li>','<li>Kod</li>','<li>Miasto</li>','<li>Kraj</li>','<li>NIP</li>','<li>Login</li>','<li>Hasło</li>');
	}
	if(formularz=='firma_uaktualnij'){
		var polat=new Array('sklep_imie','sklep_nazwisko','sklep_mail','sklep_firma','sklep_ulica','sklep_kod','sklep_miasto','sklep_kraj','sklep_nip','sklep_login','sklep_pppass');
		var polan=new Array(15,15,13,15,15,15,15,15,10,14,1);
		var polar=new Array(1,1,1,1,1,1,1,1,1,1,1);
		var polao=new Array('<li>Imię</li>','<li>Nazwisko</li>','<li>Adres mail</li>','<li>Firma</li>','<li>Ulica</li>','<li>Kod</li>','<li>Miasto</li>','<li>Kraj</li>','<li>NIP</li>','<li>Login</li>','<li>Hasło</li>');
	}

	if(formularz=='firma_haslo'){
		var polat=new Array('d_login','d_imienazwisko','d_pass','d_ppass');
		var polan=new Array(14,1,1,1);
		var polar=new Array(1,1,1,1);
		var polao=new Array('<li>Nazwa użytkownika (dozwolone znaki "1-9","a-z",".","-","_")</li>','<li>Imię i nazwisko</li>','<li>Hasło</li>','<li>Potwierdzenie hasła</li>');
	}

	if(polat.length!=polan.length || polat.length!=polao.length || polat.length!=polar.length){alert('za malo w tablicy');ok=false;}
	for(i=0;i<polat.length;i++)if(sprawdzpole(polat[i],polar[i],polan[i],tryb)==1){ok=false;pola=pola+polao[i];}

	if(formularz=='firma_dodaj' && document.getElementById('sklep_pass').value!=document.getElementById('sklep_ppass').value){ok=false;pola=pola+'<li>Oba hasła muszą być takie same</li>';document.getElementById('sklep_pass').style.backgroundColor='#FDB8A9';document.getElementById('sklep_ppass').style.backgroundColor='#FDB8A9';}
	if(formularz=='firma_uaktualnij' && document.getElementById('sklep_pass').value){if(document.getElementById('sklep_pass').value!=document.getElementById('sklep_ppass').value){ok=false;pola=pola+'<li>Oba hasła muszą być takie same</li>';document.getElementById('sklep_pass').style.backgroundColor='#FDB8A9';document.getElementById('sklep_ppass').style.backgroundColor='#FDB8A9';}}
//	if(ok==false && tryb!='silent')myalert('<p>Proszę wypełnić następujące pola:</p><ul>'+pola+'</ul>','BŁĘDY W FORMULARZU','OK',0,0);
	return(ok);
	alert(ok);
}


function validate_nip(co){
	kod=co.replace(/[^A-Z]+/g,"")
	kod=kod.substr(0,2)
	co=co.replace(/[^0-9]+/g,"")
//	alert(kod+co)
	if(!kod || kod=='PL'){
		if(co.length!=10)return(0);
		arrSteps=new Array(6,5,7,2,3,4,5,6,7);
		intSum=0;
		for(k=0;k<9;k++)intSum+=arrSteps[k]*co[k];
		int=intSum % 11;
		intControlNr=(int==10)?0:int;
		if(intControlNr==co[9])return(kod+co);
		return(0);
	}else return(kod+co);
}

function validate_mail(co){
	co=co.match(/^[^@]+@[a-zA-Z0-9\._\-]+\.[a-zA-Z]+$/g,"")
	return(co)
}
function validate_tel(co){
	stacjonarne=new Array(11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,31,32,33,34,35,36,37,38,39,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,61,62,63,64,65,67,68,70,71,72,73,74,75,76,77,80,81,82,83,84,85,86,87,89,90,91,92,93,94,95,96,97,98,99)

	co=co.replace(/[^0-9]+/g,"")
	if(!co.match(/^00\d{2}/)){
		if(co.match(/^[1-9]/))co='0048'+co;
		else co=co.replace(/^0/,'0048')
	}
	if(co.length!=13)return(0);

	if(stacjonarne.in_array(co.substr(4,2)))return(co.replace(/^([0-9]{4})([0-9]{2})([0-9]{3})([0-9]{2})([0-9]{2})/,"($1) $2 $3 $4 $5"));
	else return(co.replace(/^([0-9]{4})([0-9]{3})([0-9]{3})([0-9]{3})/,"($1) $2 $3 $4"))
}


function sprawdzpole(co,req,typ,tryb){
	var blad=0;
	var c_error='#FDB8A9';
	obj=document.getElementById(co);
	if(typ==1){ //input
		if(obj.value=='' && req){
			if(tryb!='silent')obj.style.backgroundColor=c_error;
			blad=1;
		}else if(tryb!='silent')obj.style.backgroundColor='#FFFFFF';
	}
	if(typ==14){ //input login - ograniczenie do a-z . _ -
		if(!obj.value.match(/^[a-zA-Z0-9\._\-]+$/g,"") && req){
			if(tryb!='silent')obj.style.backgroundColor=c_error;
			blad=1;
		}else if(tryb!='silent')obj.style.backgroundColor='#FFFFFF';
	}
	if(typ==15){ //input login - ograniczenie do a-z . _ - " spacja
		if(!obj.value.match(/^[a-zA-Z0-9\._\-\"\s]+$/g,"") && req){
			if(tryb!='silent')obj.style.backgroundColor=c_error;
			blad=1;
		}else if(tryb!='silent')obj.style.backgroundColor='#FFFFFF';
	}
	if(typ==16){ //input pass - ograniczenie do a-z . _ -
		if(!obj.value.match(/^[a-zA-Z0-9\._\-]+$/g,"") && req){
			if(tryb!='silent')obj.style.backgroundColor=c_error;
			blad=1;
		}else if(tryb!='silent')obj.style.backgroundColor='#FFFFFF';
	}
	if(typ==2){ //checkbox
		if(obj.checked==0 && req){
			if(tryb!='silent')obj.style.backgroundColor=c_error;
			blad=1;
		}else if(tryb!='silent')obj.style.backgroundColor='#FFFFFF';
	}
	if(typ==3 && req){ //select
		if(!obj.options[obj.selectedIndex].value){
			if(tryb!='silent')obj.style.backgroundColor=c_error;
			blad=1;
		}else if(tryb!='silent')obj.style.backgroundColor='#FFFFFF';
	}
	if(typ==10){ //NIP
		pom=validate_nip(obj.value)
		if((!pom && obj.value) || (!pom && req)){
			if(tryb!='silent')obj.style.backgroundColor=c_error;
			blad=1;
		}else{
			if(tryb!='silent'){
				obj.style.backgroundColor='#FFFFFF';
				if(pom)obj.value=pom
			}
		}
	}
	if(typ==11){ //telefon
		pom=validate_tel(obj.value)
		if((!pom && obj.value) || (!pom && req)){
			if(tryb!='silent')obj.style.backgroundColor=c_error;
			blad=1;
		}else{
			if(tryb!='silent'){
				obj.style.backgroundColor='#FFFFFF';
				if(pom)obj.value=pom
			}
		}
	}
	if(typ==13){ //mail
		pom=validate_mail(obj.value)
		if((!pom && obj.value) || (!pom && req)){
			if(tryb!='silent')obj.style.backgroundColor=c_error;
			blad=1;
		}else{
			if(tryb!='silent')obj.style.backgroundColor='#FFFFFF';
		}
	}
	if(!req){
		if(obj.className=='data' || obj.className=='datar'){
			if(validatedate(obj.value)==0 && obj.value!=''){
				if(tryb!='silent')obj.style.backgroundColor=c_error;
				blad=1;
			}else if(tryb!='silent')obj.style.backgroundColor='#FFFFFF';
		}
	}
	return(blad);
}


function sklep_white_bg(form){
	if(form=='finalizuj')co=new Array('sklep_imie','sklep_nazwisko','sklep_firma','sklep_nip','sklep_ulica','sklep_kod','sklep_kraj');
	if(co)for(i=0;i<co.length;i++)document.getElementById(co[i]).style.backgroundColor='#FFFFFF';
}
