var currentUrl;
var currentTarget;
var currentOnLoadFunc;
var tinyLoaded = false;
var activeEditors = new Array();

function activateEditor(id) {
activeEditors[activeEditors.length] = id;
toggleEditor(id);
}

function deactivateEditors() {
for(x=0;x<activeEditors.length;x++) {
toggleEditor(activeEditors[x]);
}
activeEditors.length = 0;
}

function toggleEditor(id) {
var elm = document.getElementById(id);
if (tinyMCE.getInstanceById(id) == null){
tinyMCE.execCommand('mceAddControl', false, id);}
else{
tinyMCE.execCommand('mceRemoveControl', false, id);}
}

function evalJS(target){
var element =   dojo.byId(target);
var scripts = element.getElementsByTagName("script" );
	for(var i = 0; i < scripts.length; i++)
		{
			eval(scripts[i].text);
		} 
}

function checkTarget(target){

if(target == 'subContent' && dojo.byId('subDetailsContent')){
nodisplayDiv('subDetailsContent');
displayDiv('subContent');
}

}

function setTitle(){
    var t = document.title;
    var pos = t.indexOf("#");
    if(pos>0){
		document.title = t.substring(0,pos);
	} 
}

function ajaxRequest(target_url, target,noprocessing,nodisableTiny){

	  if(!nodisableTiny)deactivateEditors();
	  if(!noprocessing)showProcessing();
       dojo.xhrGet( { // 
        // The following URL must match that used to test the server.
        url: target_url, 
        handleAs: "text",
        timeout: 100000, // Time in milliseconds
        // The LOAD function will be called on a successful response.
        load: function(response, ioArgs) { // 
        dojo.byId(target).innerHTML = response; // 
		evalJS(target);
      	checkTarget(target);
		if(!noprocessing)hideProcessing();	   
		dojo.parser.parse(target);
		setTitle();
		return response; // 
        },
        // The ERROR function will be called in an error case.
        error: function(response, ioArgs) { // 
          console.error("HTTP status code: ", ioArgs.xhr.status); // 
		    if(!noprocessing)hideProcessing();	   
          return response; // 
          }
        });
}


function ajaxRequest2(target_url, target, onLoadFunc,noprocessing,nodisableTiny, onLoadFuncArg){

	  if(!nodisableTiny)deactivateEditors();
	  if(!noprocessing)showProcessing();
	  currentOnLoadFunc = onLoadFunc;
	  dojo.xhrGet( { // 
        // The following URL must match that used to test the server.
        url: target_url, 
        handleAs: "text",
        timeout: 100000, // Time in milliseconds

        // The LOAD function will be called on a successful response.
        load: function(response, ioArgs) { // 
        dojo.byId(target).innerHTML = response; // 
		evalJS(target);
   	    checkTarget(target);
		 if(!noprocessing)hideProcessing();	   
		currentOnLoadFunc(onLoadFuncArg);
		dojo.parser.parse(target);
		return response; // 
        },
        // The ERROR function will be called in an error case.
          error: function(response, ioArgs) { // 
          console.error("HTTP status code: ", ioArgs.xhr.status); //
            if(!noprocessing)hideProcessing();	    
          return response; // 
          }
        });
}

function sendDojoFile(uplComm,errComm,target,formName,onLoadFunc,uploaderId,onLoadTarget){

			if(!uploaderId)uploaderId = '';
			if(!onLoadTarget)onLoadTarget='';
			
			currentOnLoadFunc = onLoadFunc;
			dojo.style(dojo.byId('inputField'+uploaderId),"visibility","hidden");
			dojo.byId('preamble'+uploaderId).innerHTML = '<img src="view/img/progressbar.gif" alt="" />'; 
			dojo.io.iframe.send({
				url:target,
				method: "post",
				handleAs: "text",
				encoding:"utf-8",
				form: dojo.byId(formName),
				handle: function(data,ioArgs){
							
				var resp =new String(data);

				var pos = resp.indexOf('OK, ');
					
					if (pos == "0"){
						resp = resp.slice(4);
	 					dojo.byId('preamble'+uploaderId).innerHTML = resp; 		
						dojo.style(dojo.byId('inputField'+uploaderId),"visibility","visible");
						dojo.byId('userfile'+uploaderId).value = '';
						onLoadFunc(onLoadTarget);	
						
					}else{
					
					var pos2 = resp.indexOf('TypeError');
					
						if (pos2 == "0"){
							dojo.style(dojo.byId('inputField'+uploaderId),"visibility","visible");
							dojo.byId('preamble'+uploaderId).innerHTML = '<span style="color:red;">errComm</span>'; 
						}else{					
							dojo.style(dojo.byId('inputField'+uploaderId),"visibility","visible");
							dojo.byId('preamble'+uploaderId).innerHTML = '<span style="color:red;">'+resp+'</span>'; 
						}

					}	
				}
			});
		}	

function defaultUploadFunction(){
}

function dojoInitForm(url_target,target,formName,onLoadFunc,nodisableTiny, onLoadFuncArg){
	  
	  if(!nodisableTiny)deactivateEditors();
	  showProcessing();	   
 
var kw = {
        url: url_target,
        load: function(data){
		if(target != null){
        dojo.byId(target).innerHTML = data;
   		evalJS(target);
	   checkTarget(target);
   		hideProcessing();	   
   		dojo.parser.parse(target);
   		}
   		if(onLoadFunc)onLoadFunc(onLoadFuncArg);
        },
        error: function(data){
		        hideProcessing();	  
                alert(data.message);
        },
        timeout: 100000,
        form: formName
};


dojo.xhrPost(kw);
}

  
function closeWindow(nm){
	dojo.byId(nm).style.display='none';
}  

function showWindow(nm){
	dojo.byId(nm).style.display='block';
}  

function toggleDiv(nm){
	if(dojo.byId(nm)){
		if(dojo.byId(nm).style.visibility == "hidden"){
				showDiv(nm);
		}
	else{		
				hideDiv(nm);
		}
	}
}

function toggleDojoDiv(nm){

	if(dojo.byId(nm)){
		if(dojo.byId(nm).style.display == "none"){
				displayDiv(nm);
		}
	else{		
				nodisplayDiv(nm);
		}
	}
}

function displayDiv(nm){
	dojo.byId(nm).style.display='block';
}

function nodisplayDiv(nm){
	dojo.byId(nm).style.display='none';
}


function showDiv(nm){
	dojo.byId(nm).style.visibility='visible';
}

function hideDiv(nm){
	dojo.byId(nm).style.visibility='hidden';
}

function disableForm(id) {
	
		theform =dojo.byId(id)
	
		if (document.all || document.getElementById) {
			for (i = 0; i < theform.length; i++) {
			var formElement = theform.elements[i];
				if (true) {
					if (formElement.disabled == false){
					formElement.disabled = true;
					
					}else{
					formElement.disabled = false;
					}
				}
			}
		}
	}

function changeLocation(url){
document.location.href=url;
}


function changeCheckbox(form_name,in_name,action){
	var form = document.forms[form_name];
    for (i = 0; i < form.length; i++)
      {  
         if(form.elements[i].name == in_name || in_name == 'ALL_FORM'){
         form.elements[i].checked = action;
        } 
      }
	}
	
function checkLoginEvent(e) {
var kod = (window.event) ? window.event.keyCode : e.which;
if(kod == 13 && dojo.byId('usr_password').value != '')
{
document.loginForm.submit();
return false;
}
}


function showProcessing(){
  
  if (dom && !document.all){
  document.getElementById("pr_full").style.display='block';  
  document.getElementById("pr_wnd").style.margin= ((window.innerHeight-19)/2) + "px auto auto auto";

  }
   if(document.all){
	 document.all["pr_full"].style.display='block';
	 document.all["pr_wnd"].style.margin= ((document.documentElement.clientHeight-19)/2) + "px auto auto auto";

  }
  
  
 window.progressTimeout = window.setTimeout("progressInsert()", 5); 

//dijit.byId('progressDialog').show();
//dojo.byId('dialogProcessing').style.visibility='visible';

}

function hideProcessing(){

  window.clearTimeout(window.progressTimeout);

   if (dom && !document.all){
		document.getElementById("pr_full").style.display='none';
	}
   
   if(document.all){
    document.all["pr_full"].style.display='none';
   }
   
//dijit.byId('progressDialog').hide();
//dojo.byId('dialogProcessing').style.display='none';
} 
 
function openWindow(target) {
newWindow = window.open(target, 'wind', 'menubar=yes, toolbar=no, location=no, scrollbars=no, resizable=no, status=no, width=1024, height=768 left=30, top=30')
newWindow.focus()
}

function showAllProductCategories() {
	document.getElementById("cuttedSearchResults").style.overflow = "auto";
	document.getElementById("cuttedSearchResults").style.height = "auto";
	document.getElementById("cuttedSearchResults").style.display = "none";
	Effect.toggle('cuttedSearchResults', 'slide', { delay: 0.5 });
	document.getElementById("showAllButton").innerHTML = "ukryj wszystkie znalezione kategorie"
	document.getElementById("showAllButton").setAttribute("onclick", "hideAllProductCategories()");		
	
}

function hideAllProductCategories() {
	document.getElementById("cuttedSearchResults").style.overflow = "hidden";
	document.getElementById("cuttedSearchResults").style.height = "92px";
	document.getElementById("showAllButton").innerHTML = "pokaż wszystkie znalezione kategorie"
	document.getElementById("showAllButton").setAttribute("onclick", "showAllProductCategories()");
}

function scrollToTop(){
	window.scrollTo(0,0);
}
