function sprawdz_usun(form){
	zmienna = '';
	for (i=0; i<form.elements.length; i++){
		if (!form.elements[i].name.indexOf("usun",0)){
			if(form.elements[i].checked){
				zmienna += form.elements[i].name
			}
		}
	}
	if (zmienna.length > 0){
		if ( confirm('Zdjęcie zostanie usunięte. Czy jesteś pewien ?') ){
			return true;
		} else {
			return false;
		}
	} else {
		return true;
	}

}
function selectedValue(fieldname){
	//funkcja zwraca aktualna wartosc z pola typu "select"
	return fieldname.options[fieldname.selectedIndex].value;
}

function selectedText(fieldname){
	//funkcja zwraca aktualny text z pola typu "select"
	return fieldname.options[fieldname.selectedIndex].text;
}

function radioValue(fieldname){
	//funkcja zwraca aktualna wartosc z zestawu pol typu radio
	var result = 0;
	for (var i = 0; i < fieldname.length; i++){
		if (fieldname[i].checked){
			result = fieldname[i].value;
		}
	}
	return result;
}

function LTrim(str) { 
	for (var k=0; k<str.length && str.charAt(k)<=" " ; k++);
	return str.substring(k,str.length);
}

function RTrim(str) {
	for (var j=str.length-1; j>=0 && str.charAt(j)<=" " ; j--);
	return str.substring(0,j+1);
}

function Trim(str) {
	return LTrim(RTrim(str));
}  

function deleteConfirm(_link){
	if ( confirm('Bieżący rekord zostanie usunięty. Czy jesteś pewien, że chcesz to zrobić?') ){
		window.location = _link;
	}
}

function check_email(email) {
    invalid = " /:,;";
    if(email == "") { return false }
    for(i=0; i < invalid.length; i++) {
        bad = invalid.charAt(i);
        if (email.indexOf (bad, 0) != -1) { return false }
    }
    at = email.indexOf("@", 0);
    if( (at == -1) || (at == 0) ) { return false }
    if(email.indexOf("@", at+1) != -1) { return false }
    period = email.indexOf(".", at);
    if( (period == -1) || (period == at+1) ) { return false }
    if(period+3 > email.length == -1) { return false }
    return true;
}

function isemail(e){
	return /^[\w\.-]{2,}@[\w\.-]+\.[a-z]{2,5}$/i.test(e)
}

function checkLogin(text, min_lenght){
	text = Trim(text);
	var re=/^[A-Za-z_][\w-.]+[A-Za-z0-9]$/i;
	
	if (text.length < min_lenght){
		return 'Podana nazwa użytkownika (login) jest za krótka.\nMinimalna, dopuszczalna ilość znaków to : ' + min_lenght;
	} else {
		if (re.test(text) === false){
			return 'Podana nazwa użytkownika (login) jest błędna.\nLogin nie powinien zawierać znaków specjalnych, polskich znaków diakrytycznych ("ogonków"), spacji, a na pierwszym miejscu nie powinna znajdować się cyfra';
		}
	}
	return '';
}

function checkNIP(text){
	text = Trim(text);
	var max_lenght = 13;
	var min_lenght = 10;
	
	var re=/^[0-9][\w-.]+[-0-9]$/i;
	
	if ( (text.length < min_lenght) || (text.length > max_lenght) || (re.test(text) === false) ){
		return false;
	} else {
		return true;
	}
}

function checkZIP(text){
	text = Trim(text);
	var min_lenght = 5;
	
	var re=/^[0-9][\w-.]+[-0-9]$/i;
	
	if ( (text.length < min_lenght) || (re.test(text) === false) ){
		return false;
	} else {
		return true;
	}
}

function gotoSelected(fieldName, _link){
	window.location="index.php?" + _link + "&" + fieldName.name + "=" + selectedValue(fieldName);
}

function permsMarker(form,curr_sectionid,parent_sectionid){
	if (form.elements['perm_sectionid['+parent_sectionid+']']){
		if (!( form.elements['perm_sectionid['+parent_sectionid+']'].checked)){
			if (form.elements['perm_sectionid['+curr_sectionid+']'].checked){
				form.elements['perm_sectionid['+parent_sectionid+']'].checked = true
			}
		}
	}
}

function openMap(){
	var popupWindow
	popupWindow = window.open('images/swf/mapa.html', 'Mapka', 'height=380, width=520, menubar=no, status=no, toolbar=no, scrollbars=no, screenX=100, screenY=0, left=100, top=0');
}

function checkDel(_link){
	if ( confirm('Bieżący rekord zostanie usunięty. Czy jesteś pewien, że chcesz to zrobić?') ){
		window.location="index.php?" + _link;
	}
}

function if_exists(field,a,b){
	var popupWindow
	if (field.value == ''){
		alert("Chcąc przeszukać bazę danych \nmusisz podać \"Symbol produktu\"!\n ");
	} else {
		popupWindow = window.open('system/check_if_exists.php?string='+field.value, 'DodajWpis', 'height=450, width=350, menubar=no, status=no, toolbar=no, scrollbars=no, screenX=100, screenY=0, left=100, top=0');
	}
}

function getSupplierPrice(supplier_id, form){
	var product_symbol = form.elements['supplier_symbol['+supplier_id+']'].value
	var popupWindow
	if (product_symbol == ''){
		alert("Chcąc pobrać cenę\nmusisz wpisać symbol danego produktu u dostawcy!\n ");
	} else {
		popupWindow = window.open('system/get_supplier_price.php?supplier_id='+supplier_id+'&product_symbol='+product_symbol, 'GetPrice', 'height=450, width=350, menubar=no, status=no, toolbar=no, scrollbars=no, screenX=100, screenY=0, left=100, top=0');
	}
}

function previewNewsletter(newsletter_id){
	var width = 620;
	var height = 800;
	var scrollbars = 'yes';
	previewNewsletterWindow = window.open('include/actions/newsletter_preview.php?newsletter_id='+newsletter_id, 'Podglad', 'height='+height+', width='+width+', menubar=no, status=no, toolbar=no, scrollbars='+scrollbars+', screenX=100, screenY=0, left=100, top=0');
}

var changeStatusWindow
function change_status(order_id){
	changeStatusWindow = window.open('system/orders_change_status.php?order_id='+order_id, 'ZmienStatus', 'height=450, width=350, menubar=no, status=no, toolbar=no, scrollbars=no, screenX=100, screenY=0, left=100, top=0');
}

var descriptionPreviewWindow
function descriptionPreview(product_id, width, height, scrollbars){
	descriptionPreviewWindow = window.open('system/description_preview.php?product_id='+product_id, 'Podglad', 'height='+height+', width='+width+', menubar=no, status=no, toolbar=no, scrollbars='+scrollbars+', screenX=100, screenY=0, left=100, top=0');
}

var helpWindow
function help_window(topic){
	var fileName;
	if (topic == 'placeholder') {
		fileName = 'help_placeholder.html';
	} else {
		fileName = 'help_placeholder.html';
	}
	helpWindow = window.open('system/' + fileName, 'HelpDodajWpis', 'height=450, width=350, menubar=no, status=no, toolbar=no, scrollbars=no, screenX=100, screenY=0, left=100, top=0');
}

function openWindow(imgAddr, width, height, scrollbars){
	var popupWindow;
	popupWindow = window.open(imgAddr, 'PopUp', 'height='+height+', width='+width+', menubar=no, status=no, toolbar=no, scrollbars='+scrollbars+', screenX=100, screenY=0, left=100, top=0');
}

function editWindow(img_id){
	var imgEditWindow;
	imgEditWindow = window.open('include/actions/img_edit.php?img_id='+img_id, 'ImgEdit', 'height=500, width=400, menubar=no, status=no, toolbar=no, scrollbars=yes, screenX=100, screenY=0, left=100, top=0');
}

function selectAll(field, form){
	ch = field.checked;
	for(i=0; i < form.elements.length; i++){
		if (form.elements[i].type == "checkbox") {
			form.elements[i].checked = ch;
		}
	}
}
  
function validateFormSendInfo(form){
	if (confirm("Czy jesteś pewien, że chcesz wysłać wiadomości do wybranych adresatów?") == 1) return true;
	return false;
}

function previewWindow(aid, item_id){
	window.open('include/actions/previev_window.php?aid='+aid+'&item_id='+item_id, 'PreviewWindowd', 'height=500, width=400, menubar=no, status=no, toolbar=no, scrollbars=yes, screenX=100, screenY=0, left=100, top=0');
}

function openWindow(imgAddr, width, height, scrollbars){
	var popupWindow;
	tresc='<HTML>\n<HEAD>\n'
	tresc += '<TITLE>Powiększenie</TITLE>\n'
	tresc += '</HEAD>\n<BODY bottommargin="0" leftmargin="0" topmargin="0" rightmargin="0" style="margin:0px">\n'
	tresc += '<center><a href="javascript:window.close();"><img src="' + imgAddr + '" border=0></a></center>\n'
	tresc +=' </BODY leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0">\n</HTML>\n'
	popupWindow = window.open('', 'PopUp', 'height='+height+', width='+width+', menubar=no, status=no, toolbar=no, scrollbars='+scrollbars+', screenX=100, screenY=0, left=100, top=0');
	popupWindow.document.open();
	popupWindow.document.write(tresc);
	popupWindow.document.close();
}

function onlyDigits(e, decReq) {
	var isIE = document.all?true:false;
	var isNS = document.layers?true:false;

	var key = (isIE) ? window.event.keyCode : e.which;
	var obj = (isIE) ? event.srcElement : e.target;
	var isNum = (key > 47 && key < 58) ? true:false;
	var dotOK = (key==46 && decReq=='decOK' && (obj.value.indexOf(".")<0 || obj.value.length==0)) ? true:false;
	window.event.keyCode = (!isNum && !dotOK && isIE) ? 0:key;
	e.which = (!isNum && !dotOK && isNS) ? 0:key;
	return (isNum || dotOK);
}

PositionX = 272;
PositionY = 120;

defaultWidth  = 250;
defaultHeight = 150;

var AutoClose = true;

if (parseInt(navigator.appVersion.charAt(0))>=4){
	var isNN=(navigator.appName=="Netscape")?1:0;
	var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;
}
var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;
function popImage(imageURL,imageTitle){
	if (isNN){imgWin=window.open('about:blank','',optNN);}
	if (isIE){imgWin=window.open('about:blank','',optIE);}
	with (imgWin.document){
		writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');
		writeln('<sc'+'ript>');
		writeln('var isNN,isIE;');
		writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
		writeln('isNN=(navigator.appName=="Netscape")?1:0;');
		writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
		writeln('function reSizeToImage(){');
		writeln('if (isIE){');
		writeln('window.resizeTo(100,100);');
		writeln('width=100-(document.body.clientWidth-document.images[0].width);');
		writeln('height=100-(document.body.clientHeight-document.images[0].height);');
		writeln('window.resizeTo(width,height);}');
		writeln('if (isNN){');       
		writeln('window.innerWidth=document.images["George"].width;');
		writeln('window.innerHeight=document.images["George"].height;}}');
		writeln('function doTitle(){document.title="'+imageTitle+'";}');
		writeln('</sc'+'ript>');
		if (!AutoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
		else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
		writeln('<a href="javascript:window.close();"><img name="George" src='+imageURL+' border="0" style="display:block"></a></body></html>');
		close();
		imgWin.document.close()		
	}
}

var DHTML = (document.getElementById || document.all || document.layers);

function getObj(name){
	if (document.getElementById) {
	  	this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	} else if (document.all) {
		this.obj = document.all[name];
		this.style = document.all[name].style;
	} else if (document.layers) {
		this.obj = document.layers[name];
		this.style = document.layers[name];
	}
}

function hideDiv(obj) {
	if (!DHTML) return;
	var x = new getObj(obj);
	x.style.visibility = 'hidden'
}

function showDiv(obj){
	var poz_left
	var poz_top
	var height = 100
	var width = 300
	
	poz_left = Math.round( (screen.width - width) / 2 )
	poz_top = 100

	if (!DHTML) return;
	var x = new getObj(obj);
	x.style.position = 'absolute';
	x.style.border = '1px solid #DCDCDC';
	x.style.width = 300;
	x.style.height = height;
	x.style.top =  poz_top;
	x.style.left =  poz_left;
	x.style.visibility = 'visible';
}
