function next(){
	//alert('NEXT');
	_get('way').value='1';
	document.formularz.submit();
}
function prev(){
	_get('way').value='0';
	_get('formularz').submit();
}

function upperValid(field){
    if(field.value.length>=2){
        var str=field.value;
		str=str.replace(/\b(http:|https:|www\.)+[a-zA-Z0-9_\-\.\/]+\b/gi,'');		
		str=str.replace(/[~!\*\]\[\{\}\|]/g,'');		
		str=str.replace(/\s+/g,' ');	
		str=str.replace(/[-]{2,}/g,'-');	
	    str=str.replace(/[\?]{2,}/g,'?');	
		str=str.replace(/[_]{2,}/g,'_');
		str=str.replace(/[\+]{3,}/g,'+');		
		str=str.replace(/[\=]{2,}/g,'=');		
		str=str.replace(/['"@#%&*:^;$\\/()-]/g,'');		
		field.value=str.toUpperCase();       
    }else field.value='';
}

function ListInBack(ident,Ttab){
    var Iinput=_get(ident);
    var Iselect=_get(ident+'_sel');
    load_sel(ident+'_sel',Ttab,0);				
    if(Iinput==null) return;
    //alert(Iinput.value);
    Iselect.value=Iinput.value;
    if(Iselect.selectedIndex==0 && Iinput.value!='')
    { Show('inna_'+ident+'_layer'); Hide('wybierz_'+ident+'_layer'); }
}
function load_sel2(ident,tab,first,val){
  var l=(first==1)? 0:1;        
  var f = _get(ident);
  if(f!=null && tab!=null){
    f.length=tab.length+l;
    f[0].value='';
    f[0].text='- wybierz -';
    for (var i=0; i<tab.length; i++){
	f[i+l].value=(val==1)? tab[i]:i;
	f[i+l].text =tab[i];
	}
    }
}

function TbRelation(ident,ident2,t){
    var j = ident.selectedIndex;
    if(j>=0)
	ListInBack(ident2,t[ j ]);	    
    else ListInBack(ident2);	    	
    if(_get(ident2)!=null)
    _get(ident2).value=_get(ident2+'_sel').value;
}
function showObject(x,innerDiv){
    var wynik='';
    //var x=object;
    if(x==null) return;
    wynik+='<table border="1" class="etykiety">';
    wynik+='<tr><td>'+x+'</td></tr>';
    var i;
    for(i in x){
	wynik+='<tr><td>'+i+'</td>';
	//wynik+='<td>'+x[i]+'</td></tr>';
    }
    wynik+='</table>';
    if(_get(innerDiv)!=null) _get(innerDiv).innerHTML=wynik;
}

function checkPrice(){    
    if(_get('cena')==null) return true;
    if(_getT('r_transakcji_zak','checkbox')) return true;
    if(_getT('r_transakcji_zam','checkbox')) return true;
    var co=_get('cena').getAttribute('class');
    var co1=_get('cena').getAttribute('className')
    if(co=='niewymagane' || co1=='niewymagane') return true;
    var cena_od=_getT('cena_od','checkbox');
    var cena_do=_getT('cena_do','checkbox');
    if(cena_od && cena_do) str='cena + cena2';
    else if (cena_do) str=' cena2 ';
    else str=' cena ';
    setStyle(str,'');
    var wynik=Tart(str);
    wynik=setStyle(wynik,'error_sel');
    return (wynik!='')? false:true;
}

function checkContact()
{    
	if(_get('miejscowosc')==null) return true;
    var mjs=_get('miejscowosc');
    
    var re = /[a-zA-Z]+/;
    if(mjs.value.search(re)<0) mjs.value='';
   
    var tel=_get('telefon');
    if(tel.value!='') tel.value=testTel(tel.value);
    var tel=_get('telefon2');
    if(tel!=null)   if(tel.value!='') tel.value=testTel(tel.value);
    var str='';
	var rodzajOgl=(_get('button')!=null)? _get('button').value:0;
	var str='';
	if(rodzajOgl==0)
    	str='miejscowosc + kod_pocztowy + ((( telefon, telefon2) + !ulica ), ( ulica + !telefon + !telefon2 ))';
    else
    if(rodzajOgl==35)
    	str='miejscowosc + kod_pocztowy + telefon ';
    else	    
    	str='miejscowosc + kod_pocztowy + ((( telefon, telefon2) + !ulica ), ( ulica + !telefon + !telefon2 ))';
    

     if(rodzajOgl==18 || rodzajOgl==19) str+='+Thaslo';
    
    
    setStyle(str,'');
    var wynik=Tart(str);
    telXORadr();
    wynik=setStyle(wynik,'error_sel');
    return (wynik=='')? true:false;
}

function checkGForm(){
	 
   var t1=checkForm();  
 
    var t2=checkPrice();
    	
    var key=_get('klucz');
    if(key!=null)
    if(key.value=='ZAKUPIĘ'){
	 t2=true;
	_get('cena').className='niewymagane';	 
    }	 
    
    
    
    var t3=checkContact();
    if(t1 && t2 && t3) return true;
    else return false;

}
function checkTForm(){

    var typ=_get('button');
    if(typ==null) return 'objekt button nie istnieje!';
    var rejon=_get('rejon');
    if(rejon==null) return 'objekt rejon nie istnieje!';
    
    if(typ.value==''){ alert('Proszę wybrać typ Ogłoszenia'); return false;}
    if(rejon.value==''){ alert('Proszę wybrać rejon Ogłoszenia'); return false;}
    if(typ.value==1 && _get('cena_malejaca').checked==false){ alert('Proszę o akceptacje ceny malejącej'); return false;}
    if((typ.value==3 || typ.value==19) && (_get('zdjecie_upl').value=='')){ alert('Proszę o dołączenie zdjęcia'); return false; }
    var str=''; 
    if(typ.value==18 || typ.value==19) str='Thaslo+';
    if(typ.value==2 || typ.value==18) str+='kod_sms1'; 
    if(typ.value==3 || typ.value==19) str+='kod_sms2'; 
    //alert(str);
    //return false;
    setStyle(str,'');
    var wynik=Tart(str);
    if(wynik!=''){ alert('Proszę uzupełnić pole z kodem ogłoszenia');
	wynik=setStyle(wynik,'error_sel');
	return false;
    }
    return true;
}

function TFtest(zakladka){
    var test=false;
    if(zakladka==1){
	test=checkGForm();
	if(!test){ alert('Proszę wypełnić wszystkie wymagane pola!'); }
	var znakow=_get('il_znakow').value
	znakow=(znakow < 0)? false:true;
	if (!znakow){ 
		//Tz.child[1].onclick();
	    alert("Ogloszenie moze zawierac maksymalnie "+_getT('maxZn')+" znakow! (sama tresc - dane kontaktowe nie są liczone)");
	    test=false;
	}    
    }else test=true;
    _get('zakladka').value=zakladka;
    if(test)
     document.formularz.submit();


}
function test_wynik(tekst){
	var ident=_get('test_wynik');
	if(ident!=null) ident.innerHTML=tekst;
}
function sprawdz(zakladka){
    var test=false;
   
    if(zakladka==1){
    
	test=checkGForm();
	//test_wynik('test');
	if(!test){ alert('Proszę wypełnić wszystkie wymagane pola!'); }
	var znakow=_get('il_znakow').value
	znakow=(znakow < 0)? false:true;
	if (!znakow){ 
		//Tz.child[1].onclick();
	    alert("Ogloszenie moze zawierac maksymalnie "+_getT('maxZn')+" znakow! (sama tresc - dane kontaktowe nie są liczone)");
	    test=false;
	}    
    }else test=true;
   // _get('zakladka').value=zakladka;
    if(test)
     next();


}

function wyslij(){
    if(!checkTForm()) return;
    _get('zakladka').value='3';    
    document.formularz.submit();    
}

function img(ident,nazwa){ if(ident!=null) ident.src="images/"+nazwa;   }
function _get(ident){ return document.getElementById(ident); };
function _getN(ident){ return document.getElementsByName(ident); }
function _getT(ident,type){
    ident=_get(ident);
    switch (type){
    case 'checkbox': { return (ident!=null)? ident.checked:false; }
    default: { return (ident!=null)? ident.value:''; }
    }
}

function load_sel(ident,tab,first){
  var l=(first==1)? 0:1;        
  var f = _get(ident);
  if(f!=null && tab!=null){
    f.length=tab.length+l;
    f[0].value='';
    f[0].text='- wybierz -';
    for (var i=0; i<tab.length; i++){
	f[i+l].value=tab[i];
	f[i+l].text =tab[i];
	}
    }
}

function Tf(param_tab,halt,param_tab2){
    var wynik='';
    for(var i=0; i<param_tab.length; i++)
	{
		var patt=/\[(.+)\]/i;
		var result = patt.exec( param_tab[i] );
		if (result != null)	var t = result[1];
		//--
		//var t=param_tab[i].replace(/(.?)+(\[)(.+)(\])(.?)+/g,'$3');
		var val=(typeof(param_tab2) != 'undefined')? param_tab2[i]:_getT(t);
		if(val=='' && halt!=1) break;
		if(val!='') wynik+=param_tab[i].replace('['+t+']',val); 
    }
    return wynik;
}
function strZamien2(str, co, czym){
    var start=0;
    while(str.indexOf(co, start)!=-1){
	start = str.indexOf(co, start);
        if(str.charAt(start-1) == " " || start==0) str = str.substr(0, start)+czym+str.substr(start+co.length, str.length);
        start+=co.length;
    }    
    return str;
}

function useStyle(ident,style)
    {
    if (ident=='') return '';
    if (typeof _get(ident)!= 'undefined' )
	{
	_get(ident).setAttribute('class',style);    
	_get(ident).setAttribute('className',style);
	}
	return "";
    }
function setStyle(str,styl){
    str=str.replace(/[)(+ !]/g,',');
    str=str.replace(/[,]{1,}/g,',');
    str=str.replace(/(^[, ])|([, ]$)/g,'');    
    var t=str.split(',');
    for(var i=0 ;i<t.length ;i++){
	if(_get(t[i])!=null) useStyle(t[i],styl);
	if(_get(t[i]+'_sel')!=null) useStyle(t[i]+'_sel',styl);	
    }	
    return str;	
}

function zmiana_cena_uni(){
    var cena_od=_getT('cena_od','checkbox');
    var cena_do=_getT('cena_do','checkbox');
    var text='Cena';    
    Hide("cena_do_tab");
    if( cena_od && cena_do ){
	Hide("layer_do_uzg")
	Show("cena_do_tab","cena_norm");
	text='Cena od';
    }else
    if(cena_do){
	Hide("cena_norm","layer_do_uzg");
	Show("cena_do_tab");
    }else
    if(cena_od){
	Hide("cena_do_tab","layer_do_uzg");
	Show("cena_norm");
	text='Cena od';
    }else
    if( !cena_od && !cena_do ){
	Show("cena_norm","layer_do_uzg")
	if(_get("doUzg").checked)
	    Hide("cena_od_do_tab1","cena_od_do_tab2");
	else
	    Show("lnk_cena_maleje","cena_od_do_tab1","cena_od_do_tab2");	    
    }    
    var ident=_get('cena_od_text');
    if(ident!=null) ident.innerHTML=text;
}
function wstawHtml(elem_id, htmlStr){
    var _e = _get(elem_id);
    if(_e != null){
	 _e.innerHTML = htmlStr;
	 } else {
	// alert("brak obiektu "+elem_id);
    }
}
function Tzakladki(ident){
    ident=document.getElementById(ident);
    var child=ident.childNodes;
    this.child=child;
    this.reset = function(){
	for(i=0;i<child.length;i++)
	if( child[i].getAttribute('class')=='sel' || child[i].getAttribute('className')=='sel'){	
	    child[i].setAttribute('className','');	    
	    child[i].setAttribute('class','');	    
	    document.getElementById('zak'+i).style.display='none';
	}
    }
    this.init = function(){
	for(i=0;i<child.length;i++)
	if( child[i].getAttribute('class')!='empty')
	if( child[i].getAttribute('className')!='empty'){	
	    child[i].parent=this;
	    child[i].lp=i;
	    child[i].onclick=function(){  this.parent.reset(); document.getElementById('zak'+this.lp).style.display='';this.setAttribute('class','sel');this.setAttribute('className','sel'); scrollTo(0,0); }
	}
    }
}