var param;
var sql;
var calendar="";
var no_submit=false;	


function ShowColor(kolor,field)
{
    try
    {
        field.style.backgroundColor=kolor;
    }
    catch(e)
    {}
}

function DoColor(color_field,value_field)
{

    var src="../pages/color.php";
    var zrodlo="../pages/iframe.php?src="+escape(src);
	var kontener=new Object();
	var window_width=200
	var window_height=200
	var window_top=(event.clientY+100)
	var window_left=(event.clientX+50)



	kontener.color_field=color_field;
    kontener.value_field=value_field;

	var parameters="dialogWidth:"+window_width +"px;dialogHeight:"+window_height+"px;dialogTop:"+window_top+";dialogLeft:"+window_left+";edge: Raised; center: Yes; help: No; resizable: No; status: No;scroll:no";
	var wynik=showModalDialog(zrodlo,kontener,parameters);
	//window.open(src);




}



function MakeRequired(field,required)
{
	var star="<font size=1 color=red>*</font>";
	try
	{
		if(required)
		{
			field.notnull=true;
		}
		else
		{
			field.notnull=false;
		
		}
	}catch(e){}

}


/////////////////////
function CleanFields(f1,f2)
{
	f1.value="";
	f2.value="";
	f1.fireEvent("OnChange");
	
}


function SelectMe(id)
{
	//alert(id)
	var main_dokument=top.window.dialogArguments.dokument;
	top.window.dialogArguments.key_field.value=id
	top.window.dialogArguments.key_desc_field.value=document.all("desc_area_"+id).value;
	top.window.dialogArguments.key_field.fireEvent("OnChange");
	top.window.dialogArguments.key_desc_field.fireEvent("OnChange");

	window.close();
}


function SelectMe_(id)
{
	//alert(id)
	var main_dokument=top.window.dialogArguments.dokument;
	top.window.dialogArguments.key_field.value=id
	top.window.dialogArguments.key_desc_field.value=document.all("desc_area_"+id).value;
	top.window.dialogArguments.key_field.fireEvent("OnChange");
	
	
	window.close();
}


function OpenManyToMany(src)
{
	var zrodlo="../pages/iframe.php?src="+escape(src);		
	var kontener=new Object();	
	var window_width=620
	var window_height=420
	var window_top=10
	var window_left=10	
	kontener.dokument=window.document;						
	var parameters="dialogWidth:"+window_width +"px;dialogHeight:"+window_height+"px;dialogTop:"+window_top+";dialogLeft:"+window_left+";edge: Raised; center: Yes; help: No; resizable: Yes; status: No;scroll:no";			   
	var wynik=showModalDialog(zrodlo,kontener,parameters);	
	//window.open(src);	

	results.document.forms[0].submit();
}
//////////////////////
var tab_name="tab";
function SwitchTab(nr)
{			
	var id=document.all("_id_").value;
	if(id<1)
	{
		//nr=1
	}
	//CheckForm(false);	
	SetCookie(tab_name,nr);
	for(var t=1;t<=tabs;t++)
	{
		var content_td=document.all("content_"+t);
		var tip=document.all("tip_tab_"+t);
		if(t!=nr)
		{
			content_td.className="invisible";
			tip.style.filter="progid:DXImageTransform.Microsoft.Alpha( Opacity=40, FinishOpacity=0, Style=0, StartX=0,  FinishX=100, StartY=0, FinishY=100);"
			tip.style.zIndex=50;
		}
		else
		{
			content_td.className="content_tab";		
			tip.style.filter="";
			tip.style.zIndex=1000;
		}
	}
		
}

function OpenFWindow(key_field,key_desc_field,key_table)
{
	
	
	var src="../pages/search_"+key_table+".php?fk=1";
	var filter="&nolock=1&";
	switch(key_table)
	{
		default:
			var id_field=0
			var fieldname=""
		
			params="";//_params
			
			for(var i=0;i<document.all.length;i++)
			{
				field=document.all[i];
				fieldname="";
				try{fieldname=field.name.toLowerCase()}catch(e){}
				//if((tmp.indexOf("filter")>=0))
				if(field.disabled==false)
					if(fieldname.indexOf("id_")>=0)
					{			
						switch(field.type)
						{
							case "text":
							case "select-one":
								id_field=field.value;
								if(id_field>0)
								{				
									filter+="&filter_"+fieldname+"="+id_field
									filter+="&"+fieldname+"="+id_field
									
								}
							break;
						}
					}
			}
			//alert(filter);
			src+=filter;
		break;
	}
	
	
	var zrodlo="../pages/iframe.php?src="+escape(src);		
	var kontener=new Object();	
	var window_width=570
	var window_height=600
	var window_top=event.clientY
	var window_left=event.clientX	
	kontener.dokument=window.document;	
	kontener.key_table=key_table
	kontener.key_field=key_field
	kontener.key_desc_field=key_desc_field	
	var parameters="dialogWidth:"+window_width +"px;dialogHeight:"+window_height+"px;dialogTop:"+window_top+";dialogLeft:"+window_left+";edge: Raised; center: Yes; help: No; resizable: no; status: No;scroll:no";			   
	var wynik=showModalDialog(zrodlo,kontener,parameters);	

}



function Favourite(table,key,id)
{
	//window.onbeforeunload = "";
	var field,save=true;
	var forma=document.forms[0];
	if(save)
	{
		try
		{
			
			forma.action="../admin/addfavourite.php?pos=1";
			//forma.action="../tools/session.php";
			forma.submit();
			
		}
		catch(e)
		{
			
		}
		
	}
}

function RemoveFavourite(table,key,id)
{
	//window.onbeforeunload = "";
	var field,save=true;
	var forma=document.forms[0];
	if(save)
	{
		try
		{
			forma.action="../admin/addfavourite.php?pos=0";
			//forma.action="../tools/session.php";
			forma.submit();
		}
		catch(e)
		{
			
		}
	}
}


function ChangeMenuPic(pic)
{
	var pic=document.all(pic);
	if(pic.src.indexOf("plus.gif")>0)	
		pic.src="../img/minus.gif";
	else
		pic.src="../img/plus.gif";
	
}






function OpenUpload(prefix,name)
{
	var list="_"+prefix+"_"+name;
	//OnChange="ChangePicture(this,obrazek_<?=($prefix)?>);Change(this,'<?=($prefix)?>')
	var zrodlo="../tools/upload_obrazki.php?select="+list;	
	var window_width = 250;
	var window_height = 100;			
	var window_top = 150;
	var window_left = 150;	
						
	hdNewWindow = window.open(zrodlo,'upload','scrollbars=no,resizable=no,status=no,width=' + window_width + ',height=' + window_height + ',top=' + window_top + ',left=' + window_left + '');					   
	hdNewWindow.focus();
}


//'//////////////////////////////////////////////////////////////////
//'//////////////////////////////////////////////////////////////////
function ChangeFile(div,path)
{
	//alert(path);
	div=document.all(div);
	var tmp=""
	if(path!="")
	{
		tmp+="<a href='"+path+"' target=_blank >"	
		tmp+="<img src='../img/document.gif' alt='"+path+"' border=0></a>";
	}
	else
		tmp="&nbsp;";
		
	div.innerHTML=tmp;
	

}
function ChangePicture(obrazek,path)
{
	//alert(path);
	obrazek.src=path;	
	
	if(path=="")
		obrazek.style.visibility='hidden'
	else
		obrazek.style.visibility='visible'
	
	/*if(obrazek.width>max_size);
		obrazek.width=max_size;
	if(obrazek.height>max_size);
		obrazek.height=max_size;
		*/

}

//'//////////////////////////////////////////////////////////////////	

function unloadMess()
{
	return("----\n"+DoYouReallyWantToLeaveWithoutSavingTheChanges+"\n----");
}

var warn=true;
function Change(field,prefix)
{
	
	if(warn)
		window.onbeforeunload = unloadMess;
	
	try
	{	
		var button=document.all("savebutton")
		button.className="button"
		button.disabled=false;
	}
	catch(e){}
	
	
	if(field.value!='')
		field.className="";
	
	
	
	try
	{
		var button=document.all("saveandexitbutton")
		button.className="button"
		button.disabled=false;
	}
	catch(e){}
	
	
	var polecenie="document.all('"+prefix+"_change').value=1";	
	try
	{
		eval(polecenie);
	}
	catch(e)
	{
		//alert("brak pola '"+prefix+"_change'");
	}
		

}


var hdNewWindow=null;


function openHTMLEditor(forma,pole,prefix) 
{
	
	var tmp=window.onbeforeunload;
	window.onbeforeunload = "";
	
	
	
	//Change(pole,prefix);
	
	var zrodlo="../pages/iframe.php?src="+escape("../pages/htmleditor.php?returnfield="+pole.name);	
	var window_width = 800;
	var window_height = 650;
	var window_top = 50;
	var window_left = 50;	
			
	
						
	//hdNewWindow = window.openModal(zrodlo,'uchwyt','scrollbars=yes,resizable=yes,status=no,width=' + window_width + ',height=' + window_height + ',top=' + window_top + ',left=' + window_left + '');					   
	var wynik=showModalDialog(zrodlo,pole,"dialogWidth:"+window_width+"px;dialogHeight:"+window_height+"px;dialogTop:"+window_top+";dialogLeft:"+window_left+";edge: Raised; center: Yes; help: No; resizable: yes; status: No;");
	//hdNewWindow.focus();
	//pole.fireEvent("OnChange");
	window.onbeforeunload=tmp;
	
}	
function ShowPicture(src,src2,x,y)
{		
	
	var window_width = x;
	var window_height = y;			
	var window_top = 50;
	var window_left = 50;	

	if(src=="")
		src=src2;
	if((src=="")||(src.indexOf("img/nic.gif")>0))
		return;
	
	var zrodlo="../pages/picture.php?src="+src;	
	//try
	{
		//var wynik=showModalDialog(zrodlo,null,"dialogWidth:"+window_width+"px;dialogHeight:"+window_height+"px;dialogTop:"+window_top+";dialogLeft:"+window_left+";edge: Raised; center: Yes; help: No; resizable: Yes; status: No;");
	}
	//catch(e)
	{
		var img=window.open(zrodlo,'','titlebar=no,scrollbars=auto,resizable=yes,status=no,width='+window_width+',height='+window_height+',top='+window_top+',left='+window_left);
		img.focus
	}


}



function OpenPicturesUpload(prefix,name)
{
	var field_name="_"+prefix+"_"+name;
	var field=document.all(field_name);
	var obrazek=document.all("obrazek_"+name);
	//alert(obrazek.src);
	
	var kontener=new Object();
	kontener.pole=field;
	kontener.obrazek=obrazek;
	
	var zrodlo="../pages/iframe.php?src="+escape("../upload_pictures.php");	
	var window_width = 350;
	var window_height = 500;			
	var window_top = 50;
	var window_left = 350;	
						
	//hdNewWindow = window.openModal(zrodlo,'uchwyt','scrollbars=yes,resizable=yes,status=no,width=' + window_width + ',height=' + window_height + ',top=' + window_top + ',left=' + window_left + '');					   
	var wynik=showModalDialog(zrodlo,kontener,"dialogWidth:"+window_width+"px;dialogHeight:"+window_height+"px;dialogTop:"+window_top+";dialogLeft:"+window_left+";edge: Raised; center: Yes; help: No; resizable: No; status: No;");
	//hdNewWindow.focus();
}	
function SaveAndExit(forma)
{
	
	Update2(forma,true);
}
function Update(forma)
{
	Update2(forma,false);
}

function CheckForm(msg)
{
	
	var tab=1;
	for(var i=0;i<document.all.length;i++)
	{
		field=document.all[i];
		
		
		
		if(((field.type=="password")&&!(document.all("_id_").value>0))||(field.type=="text")||(field.type=="select-one")||(field.type=="select-multiple")||(field.type=="textarea")||(field.type=="file"))
		{
			var tmp_value=ReplaceString2(field.value,' ','');
			tmp_value=ReplaceString2(tmp_value,'\t','');
			tmp_value=ReplaceString2(tmp_value,'\n','');
			
			
			if(field.tab>0)
				tab=field.tab;
			else
				tab=1;
			
			if((escape(field.notnull)=='true')&&((tmp_value=='')||(tmp_value=='null')))
			{
				if(msg)
					try{SwitchTab(tab)} catch(e){}//pierwsza zakładka jeśli to forma z zakładkami
			
				field.className="red";
				if(msg)
					alert(RequiredFieldCannotBeEmpty);
				
				try{
				field.focus();
				}catch(e){}
				
				
				save=false;
				return(false);
			}
			
			//alert(parseInt(field.min) + " " + (parseInt(field.min)>tmp_value.length))
			
			
			if((parseInt(field.min)>tmp_value.length) && !(document.all("_id_").value>0) )
			{
				if(msg)
					try{SwitchTab(tab)} catch(e){}//pierwsza zakładka jeśli to forma z zakładkami
				
				field.className="red";
				if(msg)
					alert(MinimalLenghtOfThisFieldIs+" "+field.min+" "+Characters);
				
				try{
				field.focus();
				}catch(e){}
				save=false;
				return(false)
			}
			
		}
		
	}
	return(true);
}


function Update2(forma,closeit)
{	
	var tmp=window.onbeforeunload;
	window.onbeforeunload = "";
	var field,save=true;
	
	if(closeit)
		document.all("returnvalue").value=1;
	
	if(!CheckForm(true))
	{
		window.onbeforeunload=tmp;
		return;
		
	}
	
	if(save)
	{
		forma.action="../admin/update.php";
		forma.enctype="multipart/form-data";
		forma.method="post";
		//forma.action="../tools/session.php";
		try
		{
			forma.submit();
		}catch(e){}
	}
}
function New(forma,table,key)
{		
	
	//forma.action="administracja.php?page=new";			
	//forma.submit();
	Edit(forma,table,key,0)
}

function Search(forma)
{		
	
	//forma.action="administracja.php?page=new";			
	forma.submit();
}

function View(forma,table,key,id)
{
	if(IsInfo(table))
	{	
			Info(table,key,id,false)
			return;		
	}
	
	var kontener=new Object();	
	var src="../pages/view.php?"
	//src+="&sql="+escape(document.all("_sql_").value);	
	src+="&table="+table+"&id="+id+"&key="+key;	
	
	var zrodlo="../pages/iframe.php?src="+escape(src);		
	//var window_width = "550";
	//var window_height = "600";			
	var window_top = "50";
	var window_left = "50";
	kontener.dokument=window.document;						
	var parameters="dialogWidth:"+window_width +"px;dialogHeight:"+window_height+"px;dialogTop:"+window_top+";dialogLeft:"+window_left+";edge: Raised; center: Yes; help: No; resizable: Yes; status: No;scroll:no";			   
	var wynik=showModalDialog(zrodlo,kontener,parameters);		
	//window.document.forms[0].submit();
	
}	

var window_width = "700";
var window_height = "600";			

function Edit(forma,table,key,id)
{	
	if(IsInfo(table))
	{	
			Info(table,key,id,true)
			return;		
	}
	var kontener=new Object();	
	var src="../pages/edit.php?edit=1"
	try
	{
		//src+="&sql="+escape(document.all("_sql_").value);	
	}
	catch(e){}
		
	src+="&table="+table+"&id="+id+"&key="+key;	
	
	if(table=='calc_depend')
	{
		var id_calc_step='';
		try
		{
			id_calc_step=parent.document.all("id_calc_step").value
		}catch(e){}
		
		try
		{
			if(id_calc_step=='')
				 id_calc_step=document.all("id_calc_step").value
		}catch(e){}
		
		src+="&id_calc_step="+id_calc_step;
	
	}
	if(table=='calc_calculation_step')
	{
		var id_calc_calculation='';
		try
		{
			id_calc_calculation=parent.document.all("filter_id_calc_calculation").value
		}catch(e){}
		
		src+="&id_calc_calculation="+id_calc_calculation;
		
		
	
	}
	
	if(id==0)
		if(!IsEmpty(param))
			src+=ReplaceString2(param,"filter_","");
	
	//alert(src);
	
	var zrodlo="../pages/iframe.php?src="+escape(src);		
	//var window_width = "550";
	//var window_height = "600";			
	var window_top = "50";
	var window_left = "50";
	kontener.dokument=window.document;						
	var parameters="dialogWidth:"+window_width +"px;dialogHeight:"+window_height+"px;dialogTop:"+window_top+";dialogLeft:"+window_left+";edge: Raised; center: Yes; help: No; resizable: Yes; status: No;scroll:no";			   
		
	var wynik=showModalDialog(zrodlo,kontener,parameters);	
	var forma=document.forms[0];
	if(forma.action!="")
		forma.action=forma.action+"#record_"+id;
		
	if(!no_submit)		
		forma.submit();
	
}	



function Delete(forma,table,key,id)
{
	if(confirm(DoYouWantToDeleteIt))
	{
		var polecenie="document.all('"+id+"_delete').value=1";	
		eval(polecenie);
					
		forma.action="../admin/delete.php";
		forma._table_.value=table;
		forma._key_.value=key;
		forma._id_.value=id;
		
		forma.submit();
	}
}



function Print(sql)
{
	var window_width=700
	var window_height=500
	var window_top=10
	var window_left=10
	
	var parameters="width="+window_width +",height ="+window_height+",top="+window_top+",left="+window_left+", help= no, resizable= yes, menubar= no,toolbar=no,scrollbars =yes";
	
	//window.open("../tools/xml.php?format=print&sql="+escape(sql),"",parameters)
	
	var src="../tools/xml.php?format=print&sql="+escape(sql);
	var kontener=new Object();	
	var zrodlo="../pages/iframe.php?src="+escape(src);		
	//var window_width = "550";
	//var window_height = "600";			
	var window_top = "50";
	var window_left = "50";
	kontener.dokument=window.document;						
	var parameters="dialogWidth:"+window_width +"px;dialogHeight:"+window_height+"px;dialogTop:"+window_top+";dialogLeft:"+window_left+";edge: Raised; center: Yes; help: No; resizable: Yes; status: No;scroll:no";			   
	var wynik=showModalDialog(zrodlo,kontener,parameters);		
}
 
function Excel(sql)
{
	var window_width=700
	var window_height=500
	var window_top=10
	var window_left=10
	var parameters="width="+window_width +",height ="+window_height+",top="+window_top+",left="+window_left+", help= no, resizable= yes, menubar= no,toolbar=no,scrollbars =yes";
	
//	window.open("../tools/xml.php?format=print&sql="+escape(sql),"",parameters)
	try{
	var forma=results.document.forms[0];
	forma.action="../tools/xml.php?format=excel&sql="+escape(sql);
	forma.submit();
	}catch(e){}
	
	document.forms[0].action="";
}
  

function DoFK(elTarget,table,key,value) 
{
	
	if (showModalDialog)
	{
		var sRtn;
		sRtn = showModalDialog("FK.asp?table="+table+"&key="+key+"&value="+value,"","dialogHeight: 400px; dialogWidth: 300px; dialogTop:"+(event.clientY+100)+" px; dialogLeft:"+(event.clientX+50)+" px; help: No; resizable: Yes; status: No;");
		try{
		if (sRtn!="")
		{
			var div=sRtn.indexOf(";")
			var number=sRtn.substr(0,div);
			elTarget.value = number;
			elTarget.fireEvent("onChange");
			
		}
		}catch(e){}
	}
	else{alert("Internet Explorer 4.0 or later is required.")}
}
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
function DoCal(elTarget) 
{
	var cal="../pages/calendar.php"
	if (showModalDialog)
	{
		cal=cal+"?value="+escape(elTarget.value);
		var kontener=new Object();
		kontener.field=elTarget;
		
		var sRtn;
		sRtn = showModalDialog(cal,kontener,"dialogHeight: 260px; dialogWidth: 280px; dialogTop:"+(event.clientY+100)+" px; dialogLeft:"+(event.clientX+50)+" px; help: No; resizable: No; status: No;");
		
	}
	else{alert("Internet Explorer 4.0 or later is required.")}
}

//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''	
function Groups(table,key,id)
{
	var kontener=new Object();	
	var src="../pages/edit_manytomany.php?table1="+table+"&id1="+id+"&option=group";	
	
	var zrodlo="../pages/iframe.php?src="+escape(src);		
	//var window_width = "550";
	//var window_height = "600";			
	
	var window_width=620
	var window_height=420
	var window_top=10
	var window_left=10
	
	kontener.dokument=window.document;						
	var parameters="dialogWidth:"+window_width +"px;dialogHeight:"+window_height+"px;dialogTop:"+window_top+";dialogLeft:"+window_left+";edge: Raised; center: Yes; help: No; resizable: Yes; status: No;scroll:no";			   
	var wynik=showModalDialog(zrodlo,kontener,parameters);		
	
	//window.open(src);
	
	//window.document.forms[0].submit();
}
function Rights(table,key,id)
{
	var kontener=new Object();	
	var src="../pages/edit_manytomany.php?table1="+table+"&id1="+id+"&option=rights";	
	
	var zrodlo="../pages/iframe.php?src="+escape(src);		
	//var window_width = "550";
	//var window_height = "600";			
	
	var window_width=620
	var window_height=450
	var window_top=10
	var window_left=10
	
	kontener.dokument=window.document;						
	var parameters="dialogWidth:"+window_width +"px;dialogHeight:"+window_height+"px;dialogTop:"+window_top+";dialogLeft:"+window_left+";edge: Raised; center: Yes; help: No; resizable: Yes; status: No;scroll:no";			   
	var wynik=showModalDialog(zrodlo,kontener,parameters);		
	
	//window.open(src);
	
	//window.document.forms[0].submit();
}

function Sort(order)
{
	var forma=document.forms[0];
	document.all("_order_").value=order;
	forma.method="post";
	forma.submit();
	//alert(dir+"  "+kolumn);
}


function CloseOpenMenu()
{
	
	var img=document.all("search_buttonik");
		
	var menu=document.all("search_div");
	var content=document.all("search_content_div");
	if(img.max==1)
	{
		img.src="../img/minimize.gif";
		img.max=0;
		
		img.alt=hide_filter;
		menu.className="search_div";
		content.className="search_content_div";
		SetCookie("search_menu_product",null)
	}
	else
	{
		img.src="../img/maximize.gif";
		img.max=1;
		img.alt=show_filter;
		menu.className="invisible";
		content.className="search_content_div_big";
		SetCookie("search_menu_product","1")
	}
	
	
}
