regwindow=new Array();
maxindex=100;

function showDialog(panel,psrc,wid,hei,posx,posy){
	with(document.getElementById(panel)){
		width=wid;
		height=hei;
		style.display='';
	}
	
	with(document.getElementById('wdsrcpanel')){
		width=wid;
		height=hei-20;
		src=psrc;
	}
}

function closeDialog(panel,ifpanel){
	with(document.getElementById(panel)){
		style.display='none';
	}
	
	with(document.getElementById(ifpanel)){
		src="template/empty.html";
	}
}

function opentree($branch,$branchname){
	if(document.getElementById($branchname).style.display=='none'){
		document.getElementById($branchname).style.display='';
		$branch.src="images/exp_open.gif";
	} else {
		document.getElementById($branchname).style.display='none';
		$branch.src="images/exp_closed.gif";
	}
}

function selecttree(bid,branch,branchname,sekcja,linker){
	if(branchname!="" && document.getElementById(branchname).style.display=='none'){
		document.getElementById(branchname).style.display='';
		document.getElementById(branch).src="images/exp_open.gif";
		document.getElementById('filepath').value=bid;
		document.getElementById('secpath').value=sekcja;
		createlinkpoz(branch);
		if(window.explorer){
			explorer.location.href='backstage.php?'+linker+','+bid;
		}
	} else {
//		document.getElementById(branchname).style.display='none';
//		document.getElementById(branch).src="images/exp_closed.gif";
		document.getElementById('filepath').value=bid;
		document.getElementById('secpath').value=sekcja;
		createlinkpoz(branch);
		if(window.explorer){
			explorer.location.href='backstage.php?'+linker+','+bid;
		}
	}
}

function selecttree2(bid,branch,branchname,linker){
	if(branchname!="" && document.getElementById(branchname).style.display=='none'){
		document.getElementById(branchname).style.display='';
		document.getElementById(branch).src="images/exp_open.gif";
		document.getElementById('filepath').value=bid;
		if(window.explorer){
			explorer.location.href='backstage.php?'+linker+','+bid;
		}
	} else {
//		document.getElementById(branchname).style.display='none';
//		document.getElementById(branch).src="images/exp_closed.gif";
		document.getElementById('filepath').value=bid;
		if(window.explorer){
			explorer.location.href='backstage.php?'+linker+','+bid;
		}
	}
}

	var clp=0;
	
	function expandtree(treeid,parentid){
		var treeid=treeid;	
		if(parentid!=""){var mydoc=parentid} else {var mydoc=document.getElementById(treeid);}

		if(mydoc.nodeName=="UL" && mydoc.id!=""){
			if(clp==0) {createlinkpoz(mydoc.id.slice(2)); clp++;}
			mydoc.style.display='';
		};
		
		if(mydoc.id!="explorertree") expandtree('',mydoc.parentNode);
		return false;
	}
	
	function createlinkpoz(lpozid){
		document.getElementById('filename').value=document.getElementById(lpozid).parentNode.childNodes[2].innerHTML;
//		if(parentid!=""){mydoc=parentid} else {mydoc=document.getElementById(treeid)}
		if(document.getElementById('linkpoz')){
			if((mydoc=document.getElementById("my"+lpozid))){
				var allmychild=mydoc.childNodes.length;
			} else {var allmychild=0;}
			
			var doclpoz=document.getElementById('linkpoz');
			doclpoz.length=2;
			var itr=0;
			
			if(allmychild>0 && mydoc.nodeName=="UL" && mydoc.id!=""){
				for(var i=0;i<allmychild;i++){
					if(mydoc.childNodes[i].nodeName=="LI"){
						itr=doclpoz.length;
						doclpoz.length+=1;
	//					alert(mydoc.childNodes[i].childNodes[2].innerHTML);
						doclpoz.options[itr].text="po "+mydoc.childNodes[i].childNodes[2].innerHTML;
						doclpoz.options[itr].value=itr+0.5;
					}
				}
			}
		}
		return false;
	}


function openActiveDoc(actdoc){
	if(actdoc){
//		javascript:void(setmyWindow(\'cc\'+((!maxindex)?100:maxindex),\'700\',\'500\',\'backstage.php?6,%activedocument%\',\'\',\'\'));
		setmyWindow('cc'+((!maxindex)?100:maxindex),'700','500','backstage.php?6,'+actdoc,'','');
	} else {
		alert("Nie wybrano dokumentu do edycji,\nproszę otworzyć dokument na stronie lub skorzystać z eksploratora");
	}
}

function openDoc(panel,psrc,wid,hei,posx,posy){
//	parent.showDialog('wdpanel','backstage.php?6,'+document.getElementById('fileid').value, 600, 550);"
}

function checkDuplicateFile(){
	
}

/*
opis parametrow okna
0 lub pusty - nie dzieje sie nic
1 - po wykonaniu zamyka

*/

function saveasDoc(ifpanel,forma,fid,fname,fpath){
//	closeDialog(panel,ifpanel);
//alert(eval(ifpanel+".wndback"));
		top.frames[eval(ifpanel+".wndback")+"if"].document.getElementById('dzial').submit();
// t1		top.frames[eval(ifpanel+".wndback")+"if"].xinhasend();
	
		if(fid!="") top.frames[eval(ifpanel+".wndback")+"if"].document.getElementById('fileid').value=fid;
		top.frames[eval(ifpanel+".wndback")+"if"].document.getElementById('filename').value=fname;
		top.frames[eval(ifpanel+".wndback")+"if"].document.getElementById('filepath').value=fpath;
	
		top.frames[eval(ifpanel+".wndback")+"if"].document.getElementById(forma).submit();
		
		switch(eval(ifpanel+".srcparam")){
			case 1:
				document.body.removeChild(document.getElementById(ifpanel+"e"));
			break;
		}
}

function saveas(okienko){
//	okienko.document.getElementById('parentwindow').value=okienko;
	eval(okienko+".wndback='"+okienko+"'");
//	alert(eval(okienko+".wndback"));
	wndname=okienko;
	setmyWindow('cc'+((!maxindex)?100:maxindex),'700','500','backstage.php?8', okienko, 1);
}

function saveDoc(panel,ifpanel,forma,okienko){
	if(top.frames[okienko+"if"].document.getElementById('newdoc').value==0){
		saveas(okienko);
//		setmyWindow('cc'+((!maxindex)?100:maxindex),'700','500','backstage.php?8');
		alert("To jest nowo utworzony dokument, proszę wybrać katalog w którym ma być zapisany");
//		showDialog('wdpanel','backstage.php?8',500,400);
	} else {
// t1		xinhasend();
		with(top.frames[okienko+"if"].document.getElementById(forma)){
			submit();
		}
	}
}

function publikuj(fid,statid){
	if(statid==0){
		setmyWindow('cc'+((!maxindex)?100:maxindex),'700','500','backstage.php?8');
		alert("To jest nowo utworzony dokument, aby mógł zostać opublikowany, najpierw proszę go zapisać");
	} else {
		setmyWindow('cc'+((!maxindex)?100:maxindex),'400','420','backstage.php?16,'+fid);
	}
}

function publikujlink(fid,statid){
//	if(statid==0){
//		setmyWindow('cc'+((!maxindex)?100:maxindex),'700','500','backstage.php?8');
//		alert("To jest nowo utworzony dokument, aby mógł zostać opublikowany, najpierw proszę go zapisać");
//	} else {
		setmyWindow('cc'+((!maxindex)?100:maxindex),'400','420','backstage.php?35,'+fid+','+statid);
//	}
}

function savepublikuj(){
		
}

function divshowmanager(shdiv,alldiv){
	for(xd=0,xda=alldiv.length;xd<xda;xd++){
		document.getElementById(alldiv[xd]).style.display='none';
	}
		document.getElementById(shdiv).style.display='';
//		document.getElementById('opcje').value=shdiv;
}

function getWdSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return Array(myWidth,myHeight);
}

var maxindex=100;

function registerwindow(){
	regwindow.push();
}

/*
function startdrag(moveob){

if(moveob){
		posx = moveob.style.left;
		posy = moveob.style.top;
		
		maxindex+=1;
		moveob.style.zIndex=maxindex;
}
*/

var gie = document.all;
var gnn6 = document.getElementById && !document.all;
var mouseDownX, mouseDownY;
var curobject;
var startX, startY;

function startDrag(e, myobject){

	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) targ = targ.parentNode;

	if(myobject && targ.id!="przykrywka"){
	mouseDownX = gnn6 ? e.clientX : event.clientX;
	mouseDownY = gnn6 ? e.clientY : event.clientY;	

	curobject = document.getElementById(myobject+"e");
	maxindex++;
	curobject.style.zIndex=maxindex;
//	alert(document.getElementById(myobject+"e").style[0]);
	startX = parseInt(curobject.style.left);
	startY = parseInt(curobject.style.top);
	
	var przykrywka = document.getElementById('przykrywka');
	przykrywka.style.width = window.document.body.scrollWidth + "px";
	przykrywka.style.height = window.document.body.scrollHeight + "px";
	curtype='mov';
	przykrywka.style.cursor = 'move';
	przykrywka.style.display = 'block';
	przykrywka.style.visibility="visible";
	}
}

function evtClick(e, myobject){

	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) targ = targ.parentNode;

		mouseDownX = gnn6 ? e.clientX : event.clientX;
		mouseDownY = gnn6 ? e.clientY : event.clientY;	
	
		curobject = document.getElementById(myobject+"e");
		maxindex++;
		curobject.style.zIndex=maxindex;

switch(targ.id){
	case myobject:
	//	alert(document.getElementById(myobject+"e").style[0]);
		startX = parseInt(curobject.style.left);
		startY = parseInt(curobject.style.top);
		
		var przykrywka = document.getElementById('przykrywka');
		wdSize=getWdSize();
/*		przykrywka.style.width = window.document.body.scrollWidth + "px";
		przykrywka.style.height = window.document.body.scrollHeight + "px"; */
		przykrywka.style.width = wdSize[0] + "px";
		przykrywka.style.height = wdSize[1] + "px";
		
		curtype='mov';
		przykrywka.style.cursor = 'move';
		przykrywka.style.display = 'block';
		przykrywka.style.visibility="visible";
//		document.title=getWdSize()[0];
	break;
	case myobject+"res":
/*
		mouseDownX = gnn6 ? e.clientX : event.clientX;
		mouseDownY = gnn6 ? e.clientY : event.clientY;	
	
		curobject = document.getElementById(myobject+"e");
		maxindex++;
		curobject.style.zIndex=maxindex;
*/
		curtypeid=myobject;
	//	alert(document.getElementById(myobject+"e").style[0]);
		startX = parseInt(curobject.style.left);
		startY = parseInt(curobject.style.top);
		
		var przykrywka = document.getElementById('przykrywka');
		wdSize=getWdSize();
		przykrywka.style.width = wdSize[0] + "px";
		przykrywka.style.height = wdSize[1] + "px";

/*		przykrywka.style.width = window.document.body.scrollWidth + "px";
		przykrywka.style.height = window.document.body.scrollHeight + "px";*/
		curtype='res';
		przykrywka.style.cursor = 'se-resize';
		przykrywka.style.display = 'block';
		przykrywka.style.visibility="visible";
	break;
	case myobject+"ahr":
		document.body.removeChild(curobject);
	break;
	
	}
/*
	if(myobject && targ.id!="przykrywka"){
	mouseDownX = gnn6 ? e.clientX : event.clientX;
	mouseDownY = gnn6 ? e.clientY : event.clientY;	

	curobject = document.getElementById(myobject+"e");
	maxindex++;
	curobject.style.zIndex=maxindex;
//	alert(document.getElementById(myobject+"e").style[0]);
	startX = parseInt(curobject.style.left);
	startY = parseInt(curobject.style.top);
	
	var przykrywka = document.getElementById('przykrywka');
	przykrywka.style.width = window.document.body.scrollWidth + "px";
	przykrywka.style.height = window.document.body.scrollHeight + "px";
	curtype='mov';
	przykrywka.style.cursor = 'move';
	przykrywka.style.display = 'block';
	przykrywka.style.visibility="visible";
	} */
}

function startRes(e, myobject){

	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) targ = targ.parentNode;

	if(myobject && targ.id!="przykrywka"){
	mouseDownX = gnn6 ? e.clientX : event.clientX;
	mouseDownY = gnn6 ? e.clientY : event.clientY;	

	curobject = document.getElementById(myobject+"e");
	maxindex++;
	curobject.style.zIndex=maxindex;
	curtypeid=myobject;
//	alert(document.getElementById(myobject+"e").style[0]);
	startX = parseInt(curobject.style.left);
	startY = parseInt(curobject.style.top);
	
	var przykrywka = document.getElementById('przykrywka');
	przykrywka.style.width = window.document.body.scrollWidth + "px";
	przykrywka.style.height = window.document.body.scrollHeight + "px";
	curtype='res';
	przykrywka.style.cursor = 'move';
	przykrywka.style.display = 'block';
	przykrywka.style.visibility="visible";
	}
}

function getposition(e){
	if(curobject){
//document.body.onmousemove=function(e){
//document.getElementById('dragCover').onmousemove=function(e){
	/*
	if(!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
	}
	*/
	if(curtype=="mov"){
		var posx = gnn6 ? startX + e.clientX - mouseDownX : startX + event.clientX - mouseDownX;
		//left = Math.max(left, 0);
		//left = Math.min(left, document.body.clientWidth - parseInt(gCurrContainer.style.width));
		curobject.style.left = posx+"px";
    	
		var posy = gnn6 ? startY + e.clientY - mouseDownY : startY + event.clientY - mouseDownY;
    	//top = Math.max(top, 0);
    	//top = Math.min(top, document.body.clientHeight - parseInt(gCurrContainer.style.height) - 4);
    	curobject.style.top = posy+"px";
	} else if(curtype=="res"){
		var posx = gnn6 ? e.clientX - startX  : event.clientX - startX ;
		curobject.style.width = posx+"px";
    	
		var posy = gnn6 ? e.clientY  - startY : event.clientY - startY ;
		document.getElementById(curtypeid+"if").style.height=posy-43+"px";
    	curobject.style.height = posy+"px";
	}
	
//	przesun(posx,posy);
	}
	return false;
}


function stopDrag(e){
//document.body.onmouseup=function(e){
//document.getElementById('dragCover').onmouseup=function(){
	var przykrywka = document.getElementById('przykrywka');
	przykrywka.style.cursor = 'default';
	przykrywka.style.display = 'none';
	przykrywka.style.visibility="hidden";
	activeobject="";
	curobject="";
	curobject=false;
}



//return posx;
//}
//	startmove();
//	alert(ruchomyobjekt.id);
//	ruchomyobjekt.style.left=event.x;
//	ruchomyobjekt.style.top=event.y;
//	przesun=setTimeout(startdrag,200)

//}

function przesun(pozyx,pozyy){
	if(activeobject!=""){
//		cpozx=activeobject.style.left;
//		cpozy=activeobject.style.top;
		activeobject.style.left=pozyx-245+"px";
		activeobject.style.top=pozyy-5+"px";
//		activeobject.style.left = cpozx+(pozyx-cpozx)*0.1
//		activeobject.style.top = cpozy+(pozyy-cpozy)*0.1
//		thCos = Math.abs(pozyx-cpozx);
//		thSos = Math.abs(pozyy-cpozy);
//		if((thCos < 0.8) && (thSos <0.8)){
//			activeobject="";
//		}
	}
}

function setnewindex(obje){
	maxindex+=1;
	document.getElementById(obje+"e").style.zIndex=maxindex;
}

wndname="";

function setmyWindow(myid,wwiedth,wheight,opwname, srcwindwow, srcaction){
	(!maxindex)?maxindex=100:maxindex+=1; 
//	window.scrollbars=false;
	document.body.style.overflow='hidden'; 
	eval(myid+"=new mywindow()");
	eval(myid+".createwindow(myid,wwiedth,wheight,opwname,srcwindwow,srcaction)");
}

function mywindow(){
//	var maxindex+=1;
	this.namex="";
	this.wndback="";
	this.createwindow=createwnd;
	
	function createwnd(setparent,mywidthe,myheighte,wndname,srcwnd,srcact){
//	this.namex=setparent;
	
	switch(srcact){
		case 1:
			this.wndback=srcwnd;
		break;
	}
	
	this.srcparam=srcact;

	var nazwax=setparent;
	this.myDiv=document.createElement("div");
	with(this.myDiv){
		id=nazwax+"e";
		name=nazwax+"e";
		width=mywidthe;
		height=myheighte;
		style.display="block";
		style.width=mywidthe+"px";
		style.height=myheighte+"px";
		style.position="absolute";
		style.left=window.document.body.scrollWidth/2-mywidthe/2+"px";
		style.top="60px";
		style.zIndex=maxindex;
		style.backgroundColor="#CCCCCC";
	}
	
//	alert("iddiva"+this.myDiv.id);
	
	this.my2Div=document.createElement("div");
	with(this.my2Div){
		style.width="100%";//(mywidthe-20)+"px";
		style.height="20px";
		id=nazwax;
		name=nazwax;
		setAttribute("value",nazwax);
		value=nazwax;
		style.backgroundImage="url(images/windowtop.gif)";
//		style.styleFloat="left";
//		style.cssFloat="left";
		style.display="block";
		mytext=nazwax;
//		onclick=function()
//		onmousedown=function(event){startDrag(event, gnn6 ? this.value : this.id);}
//		addEventListener('onclick',function(event){startDrag(event, gnn6 ? this.value : this.id);},false)

//		onmouseup=function(event){stopDrag(event);}
//		onmouseup=function(){activeobject="";}
		style.backgroundColor="#CCCCCC";
	}

	this.myAhref=document.createElement("div");
	with(this.myAhref){
//		innerHTML="<img src=\"images/windowclose.gif\" width=\"20\" height=\"20\" align=\"right\" border=\"0\">";
//		href="javascript:void(document.body.removeChild(document.getElementById('"+nazwax+"e')));";
		style.backgroundImage="url(images/windowclose.gif)";
		width="20";
		style.width="20px";
		id=nazwax+"ahr";
		height="20";
		style.height="20px";
		style.cursor='pointer';
		style.styleFloat="right";
		style.cssFloat="right";
		style.display="block";
	}
	
	this.my3Div=document.createElement("div");
	with(this.my3Div){
		width="20";
		height="20";
		style.height="20px";
		style.width="20px";
		id=nazwax+"3d";
		name=nazwax+"3d";
		style.backgroundImage="url(images/windowtop.gif)";
		style.styleFloat="right";
		style.cssFloat="right";
//		onclick=function(){mymove=1;}
//			startdrag(document.getElementById(document.getElementById(nazwax).parentNode.id));
//		}
//		onmouseup=function(){stopdrag();}
//		onmouseup=function(){startdrag();}
		style.backgroundColor="#CCCCCC";
	}

	this.myIframe=document.createElement("iframe");
	with(this.myIframe){
		style.scroll="yes";
//		style.width=(mywidthe-3)+"px";
		style.width="100%";
		style.height=(myheighte-43)+"px";
		marginwidth='0';
		marginheight='0';
		src=wndname;
		id=nazwax+"if";
		name=nazwax+"if";
		style.border='0';
		style.borderBottom='2px inset';
	}

	this.myInput=document.createElement("input");
	with(this.myInput){
	type="hidden";
	id="rep";
	name="rep";
	value=nazwax;
	style.height='20px';
	}

	this.myRes=document.createElement("div");
	with(this.myRes){
		style.backgroundImage="url(images/windowresize.gif)";
		id=nazwax+"res";
		name=nazwax+"res";
		width="20";
		style.width="20px";
		height="20";
		style.height="20px";
		style.cursor='se-resize';
		style.styleFloat="right";
		style.cssFloat="right";
	}

	this.myDiv.appendChild(this.my2Div);
	this.my2Div.appendChild(this.myAhref);
//	my2Div.appendChild(myAhref);
//	this.myDiv.appendChild(this.my3Div);
	this.myDiv.appendChild(this.myIframe);
	this.myDiv.appendChild(this.myRes);
	this.myDiv.appendChild(this.myInput);
	document.body.appendChild(this.myDiv);

	if (document.addEventListener) {
		this.myDiv.addEventListener('mousedown',function(event){evtClick(event,nazwax)},false);
//		this.myDiv.addEventListener('click',function(event){evtClick(event,nazwax)},false);
		this.my2Div.addEventListener('mouseup',function(event){stopDrag(event);},false);
//		this.myAhref.addEventListener('click',function(event){startClick(event,nazwax)},false);
	} else if (document.attachEvent) {
		this.myDiv.attachEvent('onmousedown',function(event){evtClick(event, nazwax);});
		this.my2Div.attachEvent('onmouseup',function(event){stopDrag(event);},false);
	}
	
//	this.my2Div.addEventListener('mouseup',function(event){stopDrag(event,nazwax)},false)
	eval(nazwax+"if.window.name='"+nazwax+"'");
//	if(setparent!="aaa"){
//		wndname=setparent;
//		window[nazwax+"if"].document.getElementById('parentwindow').value=setparent;
//	}
}

	function zaznacz(path){
		document.getElementById('adres').value=sciezka+path;
		if(path!=".."){
			zaznczas=setTimeout("zaznaczenie('"+path+"')",500);
		}
	}
	
	function odznacz(){
		clearTimeout(zaznczas);
		if(zaznaczone) document.getElementById('adres').value=sciezka+zaznaczone;
	}
	
	function zaznaczenie(tid){
		if(zaznaczone) document.getElementById(zaznaczone).style.background="";
		zaznaczone=tid;
		document.getElementById(zaznaczone).style.background="#cccccc";
	}
}


function czy_puste(nazwa,messagex,form_name,send_action){
//	telwzor=/^[0-9\-\s\(\)]*$/;
	ppuste="";
		for(pp=0,ppx=nazwa.length;pp<ppx;pp++){
			nazwa_arr=nazwa[pp].split('|');
			switch(nazwa_arr[2]){
				case "mailtest":
					emailwzor=/^[0-9a-zA-Z\.\-\_]+\@(([0-9a-zA-Z\-])+\.)+([0-9a-zA-Z\-])+$/;
					if(document.forms[form_name].elements[nazwa_arr[0]].value!="" && emailwzor.test(document.forms[form_name].elements[nazwa_arr[0]].value)==false) ppuste+=nazwa_arr[1]+"\n";
				break;
				case "porownaj":
					dopor=nazwa_arr[0].split(':');
					if(document.forms[form_name].elements[dopor[0]].value!=document.forms[form_name].elements[dopor[1]].value) ppuste+=nazwa_arr[1]+"\n";
				break;
				case "haslo":
					haslowzor=/^[\D\d]{4,8}$/;
					if(haslowzor.test(document.forms[form_name].elements[nazwa_arr[0]].value)==false) ppuste+=nazwa_arr[1]+"\n";
				break;
				case "issetin":
					dopor=nazwa_arr[0].split(':');
					for(dop=0,dopi=eval(dopor[1]).length;dop<dopi;dop++){
						if(document.forms[form_name].elements[dopor[0]].value!='' && document.forms[form_name].elements[dopor[0]].value==eval(dopor[1])[dop]) ppuste+=nazwa_arr[1]+"\n";
					}
				break;
				default:
					if(document.forms[form_name].elements[nazwa_arr[0]].value=="") ppuste+=nazwa_arr[1]+"\n";
				break;
			}
		}
	if(ppuste==""){document[form_name].action=send_action;document[form_name].submit();} else {alert('uzupełnij lub popraw wymienione poniżej pola\n'+ppuste);}
//	return pustepole;
}
