//change image onmouseover
function changeSrc(el,img){
	if(el!=null){
		el.src=img;
	}
}

//change class name
function changeTlo(el,tlo){
	if(el!=null){
		el.className=tlo;
	}
}

//show question to confirm
function czy_usun(linkp,kom){
	if(confirm(kom)){
		location.href=linkp;
	}
}

//check checkboxes 1- check, 2 - unckech, 3- invert 
function zaznacz_checkboxy(formularz,tablica,statusel) {
	var elts=(typeof(formularz.elements[tablica+'[]'])!= 'undefined') ? formularz.elements[tablica+'[]'] : '';
	var elts_cnt = (typeof(elts.length) != 'undefined') ? elts.length : 0;
	if (elts_cnt){
		for (var i = 0; i < elts_cnt; i++){ 
			if(statusel==1||(statusel==3&&!elts[i].checked)){
			 	elts[i].checked=true;
			} else {
			 	elts[i].checked=false;			
			}
		}
  } else { 
	
		if(statusel==1||(statusel==3&&!elts.checked)){
		 	elts.checked=true;
		} else {
		 	elts.checked=false;			
		}	
		
	}
}


//add tag to textarea
function dodajtag(FormName,FieldName,tag,prompttext) {
	if (tag=='b'){ tag_prompt='bold'; }
	if (tag=='i'){ tag_prompt='italic'; }
	inserttext = prompt(prompttext+'\n<'+tag+'>xxx</'+tag+'>','');
	if ((inserttext != null) && (inserttext != '')) {
		document.forms[FormName].elements[FieldName].value += '<'+tag+'>'+inserttext+'</'+tag+'>';
	}
	document.forms[FormName].elements[FieldName].focus();
}


//add emotikon
function emotikony_obrazek(pole,znaczek) {
  emotikony_pole=document.getElementById(pole);
  emotikony_pole.value=emotikony_pole.value+znaczek;
  emotikony_pole.focus();
}

//show popup witn no margin
function popup_image(okno,imageURL,imageTitle,w,h,margines){

	if(margines==''){
		$margines=0;
	}

	w=w+(2*margines);
	h=h+(2*margines);
	
	PositionY=(screen.height-h)/2; 
	if (PositionY<0){
		PositionY=0;
	}
	
	PositionX=(screen.width-w)/2; 
	if (PositionY<0){
		PositionY=0;
	}	
	
	
  var imgWin = window.open('',okno,'scrollbars=no,resizable=1,width='+w+',height='+h+',left='+PositionX+',top='+PositionY);
	//popup blockers should not cause errors
  if( !imgWin ) { 
		return true; 
	}

	imgWin.moveTo(PositionX,PositionY);
	imgWin.resizeTo(w,h);	
  imgWin.document.write('<html><head><title>'+imageTitle+'<\/title><\/head><body style="margin:0px; padding:0px; text-align:center"><div style="padding:'+margines+'px">'+'<img src="'+imageURL+'" alt=\"\" style=\"cursor:pointer\" onclick=\"window.close();\" />'+'<\/div><\/body><\/html>');
	
	imgWin.document.close();	
	
  if( imgWin.focus ) { 
		imgWin.focus(); 
	}

}

//show popup
function popup_open(okno,adres,w,h,margines) {

	if(margines==''||!margines||margines=='undefined'||isNaN(margines)){
		margines=0;
	}

	w=w+margines;
	h=h+margines;

	top_okno=(screen.height-h)/2; 
	if (top_okno<0){
		top_okno=0;
	}
	left_okno=(screen.width-w)/2; 
	
	if (left_okno<0){
		left_okno=0;
	}

	imgWin=window.open(adres,okno,'top='+top_okno+',left='+left_okno+',width='+w+',height='+h+',toolbar=no,menubar=no,location=no,directories=no,alwaysRaised=no,status=no,scrollbars=no,resizable=yes,fullscreen=0');
	
	imgWin.moveTo(left_okno,top_okno);
	imgWin.resizeTo(w,h);	
			
	//popup blockers should not cause errors
  if( !imgWin ) { 
		return true; 
	}		
	
  if( imgWin.focus ) { 
		imgWin.focus(); 
	}

} 


//check valid email
function spr_email(email){
	var forma_email=/^(?:(?:\w\-*)+\.?)*\w+@(?:(?:\w\-*)+\.)+\w{1,4}$/;
	return forma_email.test(email);
}

//trim
String.prototype.trim=function(){
	return this.replace(/^\s*|\s*$/g,"");
}

//clear default form value
function field_clear(field,val,czysc){

	wartosc=document.getElementById(field);

	if(czysc){
		if(wartosc.value==val){
			wartosc.value='';
		}		
	} else {
		if(wartosc.value==''){
			wartosc.value=val;
		}				
	}
}

//close top layer
function adv_close() { 
	adv=document.getElementById('adv_layer');
  adv.style.display='none';
}

//add to favourite
function dodaj_favorite(tytul,adres) {

 //FireFox
 if (window.sidebar) {
	
 	window.sidebar.addPanel(tytul, adres, "");

 //IE
 } else if (window.external) {
	
 	window.external.AddFavorite(adres, tytul);

 //Opera
 } else if (window.opera && window.print) {
	
 	var a = document.createElement('a'); a.setAttribute('href', adres); a.setAttribute('title', tytul); a.setAttribute('rel','sidebar'); a.click();
	
 }

}


// show/hide
function wlacz_wylacz(id){
	if(id){	
		element=document.getElementById(id);		
		if(element){
			if(element.style.display=='none'){
				element.style.display='';
			} else {
				element.style.display="none";
			}
		}
	}	
}

// show/hide DIV by checkbox
function show_checkbox(id_checkbox,id_field){

	if(id_checkbox&&id_field){	
		if(document.getElementById(id_checkbox).checked){
			document.getElementById(id_field).style.display='';
		} else {
			document.getElementById(id_field).style.display='none';		
		}
	}
	
}

//error variables
var form_ok=true;
var form_error_nr=0; 
var form_error_defalut="";
var form_errors_tab=new Array();
var form_fields_tab=new Array();

//show error label - html
function form_error_wyr(field,blad){

	wyr=document.getElementById(field+"_label");
	
	if(wyr!=null){

		if(blad){
			wyr.style.color="#ff0000";
		} else {
			wyr.style.color="";		
		}
			
	}
	
}

//start error variables
function form_start_errors(){

	form_ok=true;
	form_error_nr=0; 
	form_errors_tab=new Array();
	form_fields_tab=new Array();

}

//set form error
function form_set_error(field,comment){

	if(field){
	
		form_ok=false;
		form_error_nr++;
		form_fields_tab[form_error_nr]=field;
		if(comment){
			form_errors_tab[form_error_nr]=comment;		
		} else {
			form_errors_tab[form_error_nr]=form_error_defalut;			
		}

		form_error_wyr(field,true);
		
		if(form_error_nr==1){
			if(document.getElementById(field)!=null){
				document.getElementById(field).focus();
			}
		}

	}

}

//check fck editor field
function form_fck_spr(field){

	ok=true;
	EditorInstance=FCKeditorAPI.GetInstance(field).GetXHTML();		
	EditorInstance.trim();				
	if(EditorInstance.length<=0){
		ok=false;
	}
		
	return ok;
}

//check the field
function form_error_spr(form_name,field,comment,wyr){

	if (form_name[field]!=null) {
		form_name[field].value.trim();		
		if (form_name[field].value==''||(wyr&&!form_name[field].value.test(wyr))) {	
			form_set_error(field,comment);
		} else {
			form_error_wyr(field,false);				
		}		
	}
						
}


//show the error message
function error_komunikat(main_text){	

	if(form_error_nr){
	
		text="";

		for(i=1;i<=form_error_nr;i++)	{
			if(form_errors_tab[i]){
				if(!text){
					text=text+form_errors_tab[i]+"\n\r";
				}
			}
			
		}
		
		if(text){
		
			text=main_text+"\n\r"+text;			
			err_validatek=document.getElementById('err_validate');				
			if(err_validatek!=null){			
				text=text.replace('\n\r','<br />');		
   			err_validatek.innerHTML = "<div style=\"color:#ff0000;\">"+text+"</div>";
				$('#err_box').slideDown('medium');
			} else {
				alert(text);
			}
		}
						
	}
	
	return form_ok;
	
}

//hide error comment
function error_hide(){
	$('#err_box').slideUp('fast');
}

//check the delete action and show message
function spr_akcjau(pole,akcja,komunikat){

	ok=true;

	if(pole.value==akcja){ 				
		if(!confirm(komunikat)){ 
			ok=false; 
		}
	}
	
	return ok;
	
}


function setCookie(name, value, days, path, domain, secure){

	var expires=null;
	if(days){
		expires=new Date();
		var theDay=expires.getDate();
		theDay=theDay+days;
		expires.setDate(theDay);
	}
	
	var coo=name+"="+escape(value) +((expires) ? "; expires="+expires.toGMTString() : "") + ((path) ? "; path="+path : "") + ((domain) ? "; domain="+domain : "") + ((secure) ? "; secure" : "");

	document.cookie=coo;

}



function getCookie(name){

	var dc=document.cookie;
	var cname=name+"=";
	var clen=dc.length;
	var cbegin=0;

	while (cbegin<clen){ 
		var vbegin=cbegin+cname.length;
		if (dc.substring(cbegin, vbegin)==cname){ 
			var vend=dc.indexOf (";", vbegin);
			if (vend==-1) vend=clen;
			return unescape(dc.substring(vbegin, vend));
		}
		cbegin=dc.indexOf(" ", cbegin)+1;
		if (cbegin== 0) break;

	}
	return null;
}

var zasobnik=0;

function uzupelnijZasobnik(elementId,imageId){
	$.ajax({
		type: "POST",			
		url: "ajax.php",
		data: "akcja=mypdf_zasobnikAdd&id="+imageId+"&elementId="+elementId,						
		cache: false,
		success: function(html){
			if(html) {
				$('#'+elementId).html(html);	
			}
		}
	});	
}
function add_img(id){

	if($('#zasobnikRoot0').hasClass('zasobnik-active')){

		if($('#zasobnik0-0-0').html()=='')
		{
			uzupelnijZasobnik('zasobnik0-0-0',id);
		}
		else if($('#zasobnik0-1-0').html()=='')
		{
			uzupelnijZasobnik('zasobnik0-1-0',id);
		}
		else alert('zasobnik pełen');
	}
	else if($('#zasobnikRoot1').hasClass('zasobnik-active'))
	{
		if($('#zasobnik1-0-0').html()=='')
		{
			uzupelnijZasobnik('zasobnik1-0-0',id);
		}
		else if($('#zasobnik1-0-1').html()=='')
		{
			uzupelnijZasobnik('zasobnik1-0-1',id);
		}
		else if($('#zasobnik1-1-0').html()=='')
		{
			uzupelnijZasobnik('zasobnik1-1-0',id);
		}
		else if($('#zasobnik1-1-1').html()=='')
		{
			uzupelnijZasobnik('zasobnik1-1-1',id);			
		}
		else alert('zasobnik pełen');
	}

}
function wyczyscZasobnik(id)
{
	if(id==0)
	{
		for(i=0;i<2;i++)
			for(j=0;j<1;j++)
			{
				$('#zasobnik0-'+i+'-'+j).html('');
			}
	}
	else if(id==1)
	{
		for(i=0;i<2;i++)
			for(j=0;j<2;j++)
				$('#zasobnik1-'+i+'-'+j).html('');
	}
}

function wyczyscZasobniki(){
	wyczyscZasobnik(0);
	wyczyscZasobnik(1);	
}

function changeZasobnik(id){

	if(!$('#zasobnikRoot'+id).is(':checked')){
		$('.zasobnik-active').addClass('zasobnik');
		$('.zasobnik-active').removeClass('zasobnik-active');

		$('#zasobnikRoot'+id).addClass('zasobnik-active');
		if(zasobnik==0)
		{
			wyczyscZasobnik(0);
			zasobnik = 1
		}
		else
		{
			wyczyscZasobnik(1);
			zasobnik = 0;
		}
	}
}
function zasobnikRemove(id){
	$('#'+id).html('');
}
function zasobnikPobierz(){
	tab = $('.zasobnikItem').children();
	link = '';
	i = 0;

	$(tab).each(function() {
		
		if($($(this).find('img')).attr('id'))
		{
			link+="&item"+i+"=";
			link+=$($(this).find('img')).attr('id');
			i++;
		}
	});

	if(link)
	{
		$.ajax({
			type: "POST",			
			url: "ajax.php",
			data: "akcja=mypdf_pobierz"+link+"&zasobnik="+zasobnik,						
			cache: false,
			success: function(html){
    				window.open('zasobnik/'+html,'Download'); 
			}
		});
	}
	else
	{
		form_set_error("zasobnikRoot"+zasobnik,'pusty zasobnik');		
		error_komunikat('Błąd po stronie zasobnika');
	}

}
function zasobnikPokazEmail() {
	$('#zasobnikEmail').show('slow');
}
function zasobnikWyslij(){
	if(!spr_email($('#email').val())) { 	
		form_set_error('email','błędny email');
		error_komunikat('znaleziono błędy w formularzu');
	}
	else
	{
		tab = $('.zasobnikItem').children();
		link = '';
		i = 0;

		$(tab).each(function() {
		
			if($($(this).find('img')).attr('id'))
			{
				link+="&item"+i+"=";
				link+=$($(this).find('img')).attr('id');
				i++;
			}
		});

		if(link)
		{
			$.ajax({
				type: "POST",			
				url: "ajax.php",
				data: "akcja=mypdf_wyslij"+link+"&zasobnik="+zasobnik+"&email="+$('#email').val(),						
				cache: false,
				success: function(html){
					if(html==1)
						alert('wyslano pomyślnie');
					else alert('niestety nie udało się wysłać zasobnika');
					$('#zasobnikEmail').hide('slow');
				}
			});
		}
		else
		{
			form_set_error("zasobnikRoot"+zasobnik,'pusty zasobnik');		
			error_komunikat('Błąd po stronie zasobnika');
		}
	}
}

