function ajaxUsunPlikZListy(li, typRekordu, idRekordu, idPliku){
	li.parentNode.removeChild(li);
	//wyswietlObiektOId('loading');
	new ajax('ajax_pliki_usun.php', {
		method: 'post', 
		parameters:'typ_rekordu='+typRekordu+'&id_rekordu='+idRekordu+'&id_pliku='+idPliku,  
		onComplete:function(request){
			//ukryjObiektOId('loading');
			return true;
		}
	});
	return true;
}

function ajaxUsunAtrybutZListy(idListy, ukrytePole, idSzablonu, idAtrybutu){
	wyswietlObiektOId('loading');
	new Ajax.Updater(ukrytePole, 'ajax_atrybuty_usun.php', {
		method: 'post', 
		ghosting:false,
		constraint:false,
		hoverclass:'over',
		onComplete:function(request){
			new Effect.Highlight(idListy,{});
		}, 
		parameters:'id_listy='+idListy+'&ukryte_pole='+ukrytePole+'&id_szablonu='+idSzablonu+'&id_atrybutu='+idAtrybutu, 
		evalScripts:true, 
		asynchronous:true
	});
	return true;
}


function usunAtrybutZListy(idLi, idListy, ukrytePole, idSzablonu, idAtrybutu){
	$(idLi).parentNode.removeChild($(idLi));
	akualizujSortowanaListe(idListy, idSzablonu, ukrytePole);
	return true;
}


function ajaxDodajAtrybutDoListy(idListy, ukrytePole, idSzablonu, idAtrybutu){
	wyswietlObiektOId('loading');
	new Ajax.Updater(ukrytePole, 'ajax_atrybuty_dodaj.php', {
		method: 'post', 
		ghosting:false,
		constraint:false,
		hoverclass:'over',
		onComplete:function(request){
			new Effect.Highlight(idListy,{});
		}, 
		parameters:'id_listy='+idListy+'&ukryte_pole='+ukrytePole+'&id_szablonu='+idSzablonu+'&id_atrybutu='+idAtrybutu, 
		evalScripts:true, 
		asynchronous:true
	});
	return true;
}

function dodajAtrybutDoListy(idListy, ukrytePole, idSzablonu, idAtrybutuSzablonu, idAtrybutu, nazwa, status){
	var elLi = null;
	elLi = document.createElement('li');
	elLi.id = 'item_'+idAtrybutu;
	
	elDiv = document.createElement('div');
	elDiv.className = 'label';
	//elDiv.setAttribute('class', 'label');
	elDivText = document.createTextNode(nazwa);
	elDiv.appendChild(elDivText);
	elLi.appendChild(elDiv);
	
	elDiv = document.createElement('div');
	elDiv.className = 'ch_pomin';
	//elDiv.setAttribute('class', 'ch_pomin');
//elDiv.appendChild(document.createTextNode(idAtrybutu+' | '));	
	elImg = document.createElement('img');
	elImg.setAttribute('width', '16');
	elImg.setAttribute('height', '16');
	if(status){
		elImg.setAttribute('src', 'img/ok.gif');
		elImg.setAttribute('alt', 'aktywny');
	}else{
		elImg.setAttribute('src', 'img/no.gif');
		elImg.setAttribute('alt', 'nieaktywny');
	}
	elDiv.appendChild(elImg);
	elDiv.appendChild(document.createTextNode(' '));
	
	elA = document.createElement('a');
	elA.setAttribute('href', 'javascript:void(0)');
	elA.onclick = function() {
    	if(confirm('Czy chcesz usunąć wpis?')){
			ajaxUsunAtrybutZListy('pola', 'komunikat', parseInt(idSzablonu), parseInt(idAtrybutu));
		}else{
			return false;
		}
    }
	
	//elA.setAttribute('onclick', 'if(confirm(\'Czy chcesz usunąć wpis?\')) ajaxUsunAtrybutZListy(\'pola\', \'komunikat\', '+parseInt(idSzablonu)+', '+parseInt(idAtrybutu)+'); else return false;');
	elImg = document.createElement('img');
	elImg.setAttribute('width', '16');
	elImg.setAttribute('height', '16');
	elImg.setAttribute('src', 'img/del.gif');
	elImg.setAttribute('alt', 'usuń');
	elA.appendChild(elImg);
	elDiv.appendChild(elA);
	
	elLi.appendChild(elDiv);
	
	$(idListy).appendChild(elLi);
	
	
	
	akualizujSortowanaListe(idListy, idSzablonu, ukrytePole);
	return true;
}

function akualizujSortowanaListe(idListy, idSzablonu, ukrytePole){
	Sortable.create(idListy, {
		onUpdate:function(){
			wyswietlObiektOId('loading');
			new Ajax.Updater(ukrytePole, 'ajax_atrybuty_kolejnosc.php', {
				ghosting:false,
				constraint:false,
				hoverclass:'over',
				onComplete:function(request){
					new Effect.Highlight(idListy,{});
				}, 
				parameters:Sortable.serialize('pola')+'&id_szablonu='+idSzablonu, 
				evalScripts:true, 
				asynchronous:true})
			}
		}
	);
	return true;
}


function trim(txt){
    return txt.replace(/(^\s+)|(\s+$)/g, '');
}

function dodajOption(elSelectId, value, label) {
    var elOption = null;
	elOption = document.createElement('option');
	elOption.value = value;
	elOption.text = label;
	elSelect = document.getElementById(elSelectId);

	if(document.all && !window.opera){
		elSelect.add(elOption);
	}else{
		elSelect.add(elOption, null);
	}
}

function wyczyscListe(elId){
	elObject = document.getElementById(elId);
    if(elObject != null){
        while(elObject.hasChildNodes()){
            elObject.removeChild(elObject.firstChild);
        }
    }
}

function ukryjObiektOId(elId){
	elObject = document.getElementById(elId);
	elObject.style.display = 'none';
}

function wyswietlObiektOId(elId){
	elObject = document.getElementById(elId);
	elObject.style.display = 'block';
}

function przeladuj(loc){
		document.location.href = loc;
}

function limit_dla_pola(limit, pole, span_pole){
	if (document.getElementById(pole).value.length > limit){
		document.getElementById(pole).value = document.getElementById(pole).value.substring(0, limit);
	}
	if(span_pole.length > 0){
		txt = '';
		ile_pozostalo = limit - document.getElementById(pole).value.length;
		txt = '<br />Pozostało ' + ile_pozostalo + ' z ' + limit + ' znaków.';
		document.getElementById(span_pole).innerHTML = txt;
	}
}

function wklej(nazwa_pola){
	document.getElementById(nazwa_pola).value = window.clipboardData.getData('Text');
	return true;
}

function kopiuj(nazwa_pola){
	window.clipboardData.setData('Text', document.getElementById(nazwa_pola).value);
	return true;
}

function limit_dla_pola(limit, pole, span_pole){
	if (document.getElementById(pole).value.length > limit){
		document.getElementById(pole).value = document.getElementById(pole).value.substring(0, limit);
	}
	if(span_pole.length > 0){
		txt = '';
		ile_pozostalo = limit - document.getElementById(pole).value.length;
		txt = '<br />Pozostało ' + ile_pozostalo + ' z ' + limit + ' znaków.';
		document.getElementById(span_pole).innerHTML = txt;
	}
}
/*
function OpenList(rok,head) {
var allElems = document.getElementsByTagName('*');
	for (var i = 0; i < allElems.length; i++) {
		var thisElem = allElems[i];
		if (thisElem.className && thisElem.className == 'alink') {
			thisElem.style.color = '';
		}
	}
	document.getElementById(head).style.color='#CE0000';
	document.getElementById('wydania_lista').innerHTML='<img style="padding-top:80px; padding-bottom:80px" src="img/loading2.gif">';
			var variables='rok='+rok;
			var url = 'a_wydania_pobierz.php';
	new ajax(''+url+'', {
			postBody: ''+variables+'', onComplete:function(request){
			document.getElementById('wydania_lista').innerHTML=request.responseText;
			}});
			
}
*/
function check_sms_form(elementId){
		var kod=document.form1.smscode.value;
		var email=document.form1.email.value;
		var regulamin=document.form1.regulamin;
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if (!regulamin.checked){
			document.getElementById('check_mail').innerHTML='';
			document.getElementById(elementId).innerHTML='';
			document.getElementById('check_reg').style.color='red';
			document.getElementById('check_reg').innerHTML='Wymagana jest akceptacja regulaminu!';
		} else if(reg.test(email) == false) {
			document.getElementById('check_reg').innerHTML='';
			document.getElementById('check_mail').style.color='red';
			document.getElementById('check_mail').innerHTML='Niepoprawny adres e-mail!';
		}else{
			document.getElementById('check_mail').style.color='green';
			document.getElementById('check_mail').innerHTML='OK';
			document.getElementById('check_reg').innerHTML='';
			document.getElementById(elementId).innerHTML='<img src="img/loading.gif">';
			var variables='kod='+kod;
			var url = 'a_sprawdz_kod.php';
			new ajax(''+url+'', {
			postBody: ''+variables+'', onComplete:function(request){
				if (request.responseText=='1'){
					document.getElementById(elementId).style.color='green';
					document.getElementById(elementId).innerHTML='OK';
					document.form1.submit();
				}else{
					document.getElementById(elementId).style.color='red';
					document.getElementById(elementId).innerHTML='Niepoprawny kod sms';
				}
			}});
		}
		return false;
}
