$$=function(x){return document.getElementById(x)};

function aLoad1()
{
   return '<div class="load1">Proszę czekać...</div>';
}

function aErr1(txt)
{
   return '<div class="load1">'+txt+'</div>';
}

function createXMLHttpRequest()  
{
    if (window.XMLHttpRequest) {
      return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      return new ActiveXObject('Microsoft.XMLHTTP')
    } else {
      _error("Could not create XMLHttpRequest on this browser");
      return null;
    }
  }

function aRequest(id,url)
{
	var el = $$(id);
	//zaladowanie strony
	var xhReq = createXMLHttpRequest();
	xhReq.open("GET", url, false);
 	xhReq.send(null);
 	var data = xhReq.responseText;
 	//zaladowanie plikow CSS
 	var headTag = document.getElementsByTagName('head')[0];
 	var CssSrc = '<link href="(.*?)" rel="stylesheet" type="text/css" />';
	var match = new RegExp(CssSrc, 'img');
	var cssFiles = data.match(match);
	data = data.replace(match, ''); //calosc bez plikow CSS
	match = new RegExp(CssSrc, 'im');
	if(cssFiles!=null)
	{
		for (var i = 0; i < cssFiles.length; i++)
		{
			var file = cssFiles[i].match(match)[1];
			var exists = 0;
			for(var j=0;j<loadedCSS.length;j++)
			{
				if(file == loadedCSS[j]){exists = 1;break;}
			}
			if(exists) {continue;}
			loadedCSS.push(file);
			var fileref=document.createElement("link")
			fileref.setAttribute("rel", "stylesheet");
			fileref.setAttribute("type", "text/css");
			fileref.setAttribute("href", file);
			headTag.appendChild(fileref);
		}
	}
 	//zaladowanie plikow js
 	var ScriptSrc = '<script.*?src="(.*?)".*?>((\n|.)*?)<\/script>';
	var match = new RegExp(ScriptSrc, 'img');
	var scriptFiles = data.match(match);
	data = data.replace(match, ''); //calosc bez skryptów JS
	match = new RegExp(ScriptSrc, 'im');	 
	if(scriptFiles!=null)
	{
		for (var i = 0; i < scriptFiles.length; i++)
		{
			var file = scriptFiles[i].match(match)[1];
			var exists = 0;
			for(var j=0;j<loadedJS.length;j++)
			{
				if(file == loadedJS[j]){exists = 1;break;}
			}
			if(exists) {continue;}
			loadedJS.push(file);
			var fileref=document.createElement("script")
			fileref.setAttribute("type", "text/javascript");
			fileref.setAttribute("src", file);
			headTag.appendChild(fileref);			
      	xhReq.open("GET", file, false);
	 		xhReq.send(null);
 			var scrdata = xhReq.responseText;
 			eval(scrdata);		      
		}
	}
	//wykonanie inlajnowego kodu js
	var ScriptFragment = '(?:<script.*?>)((\n|.)*?)(?:<\/script>)';
	match = new RegExp(ScriptFragment, 'img');
	var response = data.replace(match, ''); //calosc bez skryptów JS
	var inlineCode = data.match(match); //skrypty JS
	match = new RegExp(ScriptFragment, 'im');
	el.innerHTML = response;
	if(inlineCode!=null)
	{
		for (var i = 0; i < inlineCode.length; i++)
		{
			eval(inlineCode[i].match(match)[1]);
		}
	}	
}

function aPost(id,form)
{
	var el = $$(id);
	var fo = $$(form);
	fo.onsubmit = function()
	{		
		if(window.fckNames!=undefined)
		{
			for(var i=0;i<window.fckNames.length;i++)
			{
				
				if($$(window.fckNames[i]))
				{
					$$(window.fckNames[i]).value = FCKeditorAPI.GetInstance(window.fckNames[i]).GetXHTML();
					window.fckNames[i] = undefined;
				}
			}
		}
		
	}
	advAJAX.assign(fo, {
		onSuccess : function(obj) {
			var data=obj.responseText;
			var ScriptFragment = '(?:<script.*?>)((\n|.)*?)(?:<\/script>)';
			var match = new RegExp(ScriptFragment, 'img');
			var response = data.replace(match, ''); //calosc bez skryptów JS
			var inlineCode = data.match(match); //skrypty JS
			match = new RegExp(ScriptFragment, 'im');
			el.innerHTML = response;
			if(inlineCode!=null)
			{
				for (var i = 0; i < inlineCode.length; i++)
				{
					eval(inlineCode[i].match(match)[1]);
				}
			}
      },
      onError : function(obj) {
          el.innerHTML = aErr1('błąd pobierania danych');
      }
	});
}

function setCrit(id,name,val)
{
	$$('a_'+id).href = crit[id][0]+','+val+crit[id][1];
	$$('a_'+id).innerHTML = name;
	$$('i_'+id).value = val;
	if($$('c_'+id)!=undefined){$$('c_'+id).value = name;}
	if($$('formcrit')!=null){$$('formcrit').submit();}
}

var markedRows = new Array();
var allowMultiple = new Array();
var valid = new Array();

function markRowInit(id,multiple,required)
{
   var el = $$(id);
   if(!el) {return;}
   
   allowMultiple[id] = multiple;
   if(required){valid[id] = 0;}
   
   if(typeof(markedRows[id])=='undefined') {markedRows[id] = new Array();}
   
   var rows = el.getElementsByTagName('tr');
   for ( var i = 0; i < rows.length; i++ )
   {
      if ( rows[i].className.substr(0,4) != 'mark') {continue;}
      
      rows[i].onmousedown = function()
      {
         var id = this.id.match(/^([a-zAZ0-9]+)_/)[1];
         var checkbox = this.getElementsByTagName( 'input' )[0];
         if(!checkbox || (checkbox && checkbox.type != 'checkbox')) {return;}
         
         if(typeof(allowMultiple[id])!='undefined' && !allowMultiple[id]) {unmarkRows(id,this.id);}
         
         if (typeof(markedRows[id][this.id])=='undefined' || !markedRows[id][this.id])
         {
            this.className += ' marked';
            markedRows[id][this.id] = true;
            if(typeof(valid[id])!='undefined'){valid[id]++;}
         }
         else
         {
            this.className = this.className.replace(' marked','');
            markedRows[id][this.id] = false;
            if(typeof(valid[id])!='undefined'){valid[id]--;}
         }
         if (checkbox.disabled == false )
         {
         	checkbox.checked = markedRows[id][this.id];
         }
         if(required)
         {
         	enableButton();
         }
      }  
   }
}

function unmarkRows(id,elid)
{
   var el = $$(id);
   if(!el) {return;}
   
   var rows = el.getElementsByTagName('tr');
   for ( var i = 0; i < rows.length; i++ )
   {
      if ( typeof(markedRows[id][rows[i].id])!='undefined' && markedRows[id][rows[i].id] && rows[i].id != elid )
		{
      	rows[i].className = rows[i].className.replace(' marked','');
      	markedRows[id][rows[i].id] = false;
      	var checkbox = rows[i].getElementsByTagName( 'input' )[0];
         if(!checkbox || (checkbox && checkbox.type != 'checkbox')) {continue;}
         if (checkbox.disabled == false )
         {
             checkbox.checked = markedRows[id][rows[i].id];
         }
         if(typeof(valid[id])!='undefined'){valid[id]--;}
      }	
   }
}

function enableButton()
{
	var disable = true;
	for(var i in valid)
	{
		disable=(valid[i])?false:true;
		if(!disable){break;}
	}

	var buttons = document.getElementsByTagName('input');
	for(var i=0;i<buttons.length;i++)
	{
		if(buttons[i].type == 'submit' && buttons[i].className.search(/enable/!=-1))
		{
			buttons[i].disabled = disable;
			if(!disable)
			{
				buttons[i].className = buttons[i].className.replace('graynext','bluenext');
			}
			else
			{
				buttons[i].className = buttons[i].className.replace('bluenext','graynext'); 
			}
		}	
	}
}

function shext(id,bid,atxt,ntxt)
{
	var el = $$(id);
	var bl = $$(bid);
	var act = (el.style.display=='block')?'none':'block';
	el.style.display = act;
	bl.innerHTML = (act=='none')?ntxt:atxt;
}

function moveValue(sid,did,but)
{
    var myQuery = document.getElementById(did);
    var myListBox = document.getElementById(sid);
    var myButton = document.getElementById(but);

    if(myListBox.options.length > 0) {
        sql_box_locked = true;
        var chaineAj = "";
        var NbSelect = 0;
        for(var i=0; i<myListBox.options.length; i++) {
            if (myListBox.options[i].selected){
                NbSelect++;
                if (NbSelect > 1)
                    chaineAj += ", ";
                chaineAj += myListBox.options[i].value;
            }
        }

        //IE support
        if (document.selection) {
            myQuery.focus();
            sel = document.selection.createRange();
            sel.text = chaineAj;
            myButton.focus();
        }
        //MOZILLA/NETSCAPE support
        else if (myQuery.selectionStart || myQuery.selectionStart == "0") {
            var startPos = myQuery.selectionStart;
            var endPos = myQuery.selectionEnd;
            var chaineSql = myQuery.value;

            myQuery.value = chaineSql.substring(0, startPos) + chaineAj + chaineSql.substring(endPos, chaineSql.length);
        } else {
            myQuery.value += chaineAj;
        }
        sql_box_locked = false;
    }
}

function info(img,txt)
{
	var parent = img.parentNode;
	
	var divs = getElementsByClassName(parent,'div','infocont');
	if(divs[0]==undefined)
	{
		var div = document.createElement('DIV');
		div.setAttribute('class','infocont');
		div.setAttribute('style','display:block;left:0px;top:0px;z-index:2;');
		div.setAttribute('onmouseout',"this.style.display = 'none'");
		div.innerHTML = txt;
		parent.appendChild(div);
		
	}
	else
	{
		divs[0].style.display = 'block';
	}
	//alert(divs);
}

function getElementsByClassName(oElm, strTagName, oClassNames)
{
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var arrRegExpClassNames = new Array();
    if(typeof oClassNames == "object")
    {
        for(var i=0; i<oClassNames.length; i++)
        {
            arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
        }
    }
    else
    {
        arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
    }
    var oElement;
    var bMatchesAll;
    for(var j=0; j<arrElements.length; j++)
    {
        oElement = arrElements[j];
        bMatchesAll = true;
        for(var k=0; k<arrRegExpClassNames.length; k++)
        {
            if(!arrRegExpClassNames[k].test(oElement.className))
            {
                bMatchesAll = false;
                break;
            }
        }
        if(bMatchesAll)
        {
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)
}

function changeTitle(t,h1,h2)
{
	document.title = t;
	if(h1)
	{
		$$('headh1').innerHTML = h1;
		$$('headh1').style.display = 'block';
	}
	else
	{
		$$('headh1').style.display = 'none';
	}
	
	if(h2)
	{
		$$('headh2').innerHTML = h2;
		$$('headh2').style.display = 'block';
	}
	else
	{
		$$('headh2').style.display = 'none';
	}
}

function flash(id, kolor, czas, kolor2, czas2)
{
  // <![CDATA[
	document.getElementById(id).style.color = kolor;
	setTimeout('flash("' + id + '","' + kolor2 + '",' + czas2 + ',"' + kolor + '",' + czas + ')', czas);
	// ]]>
}

function zmienTo(nr)
{
  switch (nr){
  case "1":
  show1();
  setTimeout('zmienTo("2")',8000);
  break;
  case "2":
  show2();
  setTimeout('zmienTo("1")',12000);
  break;
  }
  
}

function poka(id){
  var doc = $$(id);
  doc.style.display = 'block';
}

function usun(id_em,email){
  var odp = confirm("Czy na pewno chcesz usunąć adres: "+email+"?");
  if (odp){
    var adr = "/p_newsletter.html?nr="+id_em;
    aRequest("cntrc",adr);
  }
}