////////////////// START DRAGGING DIVS FUNCTIONS ///////////////////////
var currentdiv="";
var openedHelp=0;

var currentsubdiv=0;
var currentsubdiv2=0;

var startSubdivX=-3; //270
var startSubdivY=39;  //73

function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 0;

function dragStart(event, id) {

  var el;
  var x, y;

  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = topStartPositionForDivs;

  // Update element's z-index.

  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  // Capture mousemove and mouseup events on the page.

  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event) {

  var x, y;

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Move drag element by the same amount the cursor has moved.

  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop(event) {

  // Stop capturing mousemove and mouseup events.

  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}

////////////////// END DRAGGING DIVS FUNCTIONS ///////////////////////
/**************************************************************/

function openDiv(id)
{

	if(currentdiv!="") document.getElementById(currentdiv).style.top="-1000";
	currentdiv=id;

	for (i=0;i<hiddableSelects.length ;i++ )
	{
		var element=hiddableSelects[i];
		if(document.getElementById(element)) document.getElementById(element).style.visibility="hidden";
	}
 
	document.getElementById(id).style.zIndex=2000;

	var scrollTop=document.body.scrollTop;
	document.getElementById(id).style.top=top_position_draggable_divs+scrollTop;

}

function closeDiv(id)
{
 
	document.getElementById(id).style.top="-1000";
	document.getElementById(id).style.zIndex=1;
	
	for (i=0;i<hiddableSelects.length ;i++ )
	{
		var element=hiddableSelects[i];
		if(document.getElementById(element)) document.getElementById(element).style.visibility="visible";
	}

}

function openCloseDraggableDiv(id)
{
 
	var html=document.getElementById("switch_"+id).innerHTML;
	//alert(html);
	
	if(html=="-")
	{
		document.getElementById("inner_"+id).className="hidden";
		document.getElementById("switch_"+id).innerHTML="+";

	}
	else if(html=="+")
	{
		document.getElementById("inner_"+id).className="active";
		document.getElementById("switch_"+id).innerHTML="-";
		
	}
}

var ns = (navigator.appName.indexOf("Netscape") != -1);
var d = document;
function JSFX_FloatDiv(id, sx, sy)
{
	var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
	var px = document.layers ? "" : "px";
	window[id + "_obj"] = el;
	if(d.layers)el.style=el;
	el.cx = el.sx = sx;el.cy = el.sy = sy;
	el.sP=function(x,y){this.style.left=x+px;this.style.top=y+px;};

	el.floatIt=function()
	{
		var pX, pY;
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth : 
		document.documentElement && document.documentElement.clientWidth ? 
		document.documentElement.clientWidth : document.body.clientWidth;
		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? 
		document.documentElement.scrollTop : document.body.scrollTop;
		if(this.sy<0) 
		pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? 
		document.documentElement.clientHeight : document.body.clientHeight;
		this.cx += (pX + this.sx - this.cx)/8;this.cy += (pY + this.sy - this.cy)/8;
		this.sP(this.cx, this.cy);
		setTimeout(this.id + "_obj.floatIt()", 20);
	}
	return el;
}


function trim(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
return sString;
}

function is_number(nr)
{
	
	var expr=/\D/g; 
	var result=expr.test(nr);
	
	if(result) return false; 
	else return true;
	
}

function in_array(string, array2)
{
	 
	var r=false;
	for(j=0;j<array2.length;j++)
	{	
		if(array2[j]==string) r=true;
	}

	return r;
}

function getExtensionByName(filename)
{
	
	var text=filename.split(".");
	var len=text.length-1;
	var ext=text[len];

	return ext.toLowerCase();

}

function number_format(number, decimals, dec_point, thousand_sep, p)
{

	var n=(''+(Math.round(number*(p=Math.pow(10,decimals||0)))/p)).split('.');
	for(var i=(n[0]=n[0].split('')).length-3,j=n[0][0]=='-'?1:0; i>j; i-=3)
	{
		n[0].splice(i,0,thousand_sep||',');
	}
	
	var string=n[0].join('')+(n[1]?(dec_point||'.')+n[1]:'');

	if(string.indexOf(",")!=-1)
	{
	var p=string.split(",");
	var l=p.length-1;
	var ext=p[l];
	if(ext.length==1) string+="0";
	}
	else string+=",00";

	return string;

}

function popUp(urlnow, namenow, widthnow, heightnow, activation) 
{ 
	
	newWindow = window.open(urlnow, namenow, 'width='+widthnow+',height='+heightnow+',toolbar=no,focus=yes,menubar=yes,location=no,scrollbars=yes,resizable=yes,status'); 
	if(activation) newWindow.focus(); 

}

function printSite(elementid)
{
	
	
	if(document.getElementById(elementid))
	{
		popUp(server+"/print_site.php?id="+elementid,"MoiPrawnicy",500,500,1);
	}

}

function enlargeImage(id, name, path)
{
	popUp("enlarge_image.php?id="+id+"&name="+escape(name)+"&path="+escape(path),"zdjęcie",500,500,1);

}

function enlargeImageFront(id, name, path)
{

	popUp(server+"/enlarge_image.php?id="+id+"&name="+escape(name)+"&path="+escape(path),"zdjęcie",500,500,1);
}

function check_emailform()
{
	
	var d=document.emailform;
	
	hideNotificationDiv();

	if(!trim(d.f_mail_to.value))
	{
		alert("Podaj adres email odbiorcy");
		d.f_mail_to.focus();
		return false;
	}

	var email=trim(d.f_mail_to.value);
	var expr=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	var result=expr.test(email);
	
	if(!result)
	{
		alert("Wprowadź poprawny adres email odbiorcy");
		d.f_mail_to.focus();
		return false;
	}

	if(d.f_mail_copy) if(trim(d.f_mail_copy.value))
	{
		
		var email=trim(d.f_mail_copy.value);
		var expr2=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		var result2=expr2.test(email);
		
		if(!result2)
		{
			alert("Wprowadź poprawny adres email kopii");
			d.f_mail_copy.focus();
			return false;
		}

	}

	if(d.f_mail_hiddencopy) if(trim(d.f_mail_hiddencopy.value))
	{
		
		var email=trim(d.f_mail_hiddencopy.value);
		var expr3=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		var result3=expr2.test(email);
		
		if(!result3)
		{
			alert("Wprowadź poprawny adres email kopii ukrytej");
			d.f_mail_hiddencopy.focus();
			return false;
		}

	}
	
	if(!trim(d.f_mail_subject.value))
	{
		
		alert("Wpisz temat wiadomości");
		d.f_mail_subject.focus();
		return false;

	}

	if(!trim(d.f_mail_message.value))
	{
		
		alert("Wpisz treść wiadomości");
		d.f_mail_message.focus();
		return false;

	}

	return true;
}

function pressTab(id, preffixid)
{
	
	var maxid=10;
	
	for(i=1;i<=maxid;i++)
	{
		 var idno=preffixid+""+i;
		 if(document.getElementById(idno)) document.getElementById(idno).style.display='none';
		
		var idno="tab_left"+i;
		 if(document.getElementById(idno)) document.getElementById(idno).className='tab_left_off';
		 
			var idno="tab_middle"+i;
		if(document.getElementById(idno)) document.getElementById(idno).className='tab_middle_off';

		var idno="tab_right"+i;
		 if(document.getElementById(idno)) document.getElementById(idno).className='tab_right_off';
	}
	
	var idno=preffixid+""+id;
	if(document.getElementById(idno)) document.getElementById(idno).style.display='block';
	
	var idno="tab_left"+id;
		 if(document.getElementById(idno)) document.getElementById(idno).className='tab_left_on';
		 
			var idno="tab_middle"+id;
		if(document.getElementById(idno)) document.getElementById(idno).className='tab_middle_on';

		var idno="tab_right"+id;
		 if(document.getElementById(idno)) document.getElementById(idno).className='tab_right_on';

	if(document.searchpartform) document.searchpartform.activeTab.value=id;
	if(document.beheerform) document.beheerform.activeTab.value=id;
	
}

function setlang(lang)
{
	
	var list=document.getElementById("langslist").value;
	list=list.split(",");

	for(i=0;i<list.length-1;i++)
	{
		var idnow=list[i];
		var nameid="name_"+idnow;
		var separatorid="separator_"+idnow;
		var editorid="editor_"+idnow;

		if(document.getElementById(nameid)) document.getElementById(nameid).style.display="none";
		if(document.getElementById(separatorid)) document.getElementById(separatorid).style.display="none";
		if(document.getElementById(editorid)) document.getElementById(editorid).style.display="none";
	}
	
	var nameid="name_"+lang;
	var separatorid="separator_"+lang;
	var editorid="editor_"+lang;

	if(document.getElementById(nameid)) document.getElementById(nameid).style.display="block";
	if(document.getElementById(separatorid)) document.getElementById(separatorid).style.display="block";
	if(document.getElementById(editorid)) document.getElementById(editorid).style.display="block";

}


function hideNotificationDiv()
{
	if(document.getElementById('notificationdiv'))
	{
		document.getElementById('notificationdiv').style.top='-1000';
		document.getElementById('notificationdiv').style.visibility='visible';
	}
}

function showNotificationDiv()
{

	
	if(document.getElementById('notificationdiv'))
	{
		
		var top=document.getElementById('notificationdiv').offsetTop;
		if(top<0) top=110;

		if (browser.isIE) {
			var y =document.body.scrollTop;
		  }
		  if (browser.isNS) {
			var y = window.scrollY;
		  }
		
		document.getElementById('notificationdiv').style.top=top+y;
		document.getElementById('notificationdiv').style.visibility='visible';

	}
}

function changeStatus(section, sql_table, sql_column, sql_id, sql_set_value, tdid)
{

	// change image in list
	if(sql_set_value==0) 
	{
		srcnow="images/cancel.gif";
		var sql_value=1;
	}	
	
	else if(sql_set_value==1) 
	{
		srcnow="images/accept.gif";
		var sql_value=0;
	}
	var newcontent="<a href=\"javascript:changeStatus('"+section+"', '"+sql_table+"', '"+sql_column+"','"+sql_id+"', '"+sql_value+"', '"+tdid+"')\"><img src='"+srcnow+"' width=16 height=16></a>";
	
	showNotificationDiv();

	$.ajax({
		   
	type: "POST",
  
	url:	server+"/ajax/set_activation.php",
   
	data: "sql_table="+sql_table+"&sql_column="+sql_column+"&sql_id="+sql_id+"&sql_set_value="+sql_set_value,

	success: function(msg)
	{
		
		hideNotificationDiv();

		if(document.getElementById(tdid)) document.getElementById(tdid).innerHTML=newcontent;

	}
	});

}

function checkSondaAnswer(sondaid, nr)
{
	nr=nr*1;
	
	var is_checked=false;
	var answer=0;

	for(i=0;i<nr;i++)
	{
		
		var id="sonda_answer_"+i;
		if(document.getElementById(id))
		{
			if(document.getElementById(id).checked)
			{
				is_checked=true;
				var answer=i+1;
				break;
			}
		}
	}
	
	if(!is_checked) alert("Wybierz odpowiedź!");
	else
	{
		$.ajax({
		   
		type: "POST",
	  
		url:	server+"/ajax/set_sonda.php",
	   
		data: "sondaid="+sondaid+"&answer="+answer,

		success: function(msg)
		{
	 
			document.location.href=server+"/index.php?sondaid="+sondaid;			

		}
		});
	}
}

function checkNewsletterSubscription()
{
	
	var val=trim(document.getElementById("newslettermail").value);

	if(!val || val=='wpisz e-mail')
	{
		
		alert("Wpisz adres email!");
		document.getElementById("newslettermail").focus();
	}

	else
	{

		var expr=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		var result=expr.test(val);
		
		if(!result)
		{
			alert("Wprowadź poprawny adres email!");
			document.getElementById("newslettermail").focus();
		}

		else
		{
			$.ajax({
		   
			type: "POST",
		  
			url:	server+"/ajax/set_newsletter.php",
		   
			data: "email="+val,

			success: function(msg)
			{
		 
				if(document.getElementById("newsletter-content")) document.getElementById("newsletter-content").innerHTML=msg;
			}
			});

		}

	}

}

function changePartsAmountPerPage(val)
{
	
	if(document.getElementById('key_perpage')) document.getElementById('key_perpage').value=val;
	
	setNewSearchForParts();
	document.searchpartform.submit();
}

function rewindParts(action, curval, maxval)
{
	
	curval=curval*1;
	maxval=maxval*1;
	
	if(action=='start') curval=1;
	else if(action=='previous') curval-=1;
	else if(action=='next') curval+=1;
	else if(action=='end') curval=maxval;

	document.getElementById("key_currentpage").value=curval;
	showNotificationDiv();
	freezePartDetailsForSearchForm();
	document.searchpartform.submit();
}

function freezePartDetailsForSearchForm()
{
}

function setNewSearchForParts()
{
	if(document.getElementById('key_newsearch')) document.getElementById('key_newsearch').value="yes";
}

function setLinkAsHomePage(text,adressURL)
{

	//FireFox
    if (window.sidebar) 
	{

		//document.write("<a href='javascript:void(0)' onclick='alert(\"Chwyć i przeciągnij ikonę strony w adresie URL na ikonę strony domowej w przeglądarce!\")' title='"+text+"'>"+text+"</a>");
	}
	else 
	{
		document.write("&nbsp;|&nbsp;<a href='javascript:void(0)' onclick=\"this.style.behavior='url(#default#homepage)'; this.setHomePage(server)\" title='"+text+"'>"+text+"</a>");
	 
	}
	
}

function add2favourite(tytul, adres) 
{

    //var tytul = 'siteurl';
    //var adres = 'http://kurs24.pl';

    //FireFox
    if (window.sidebar) { 

        window.sidebar.addPanel(tytul, adres, ""); 

    //IE
    } else if (window.external) {

        window.external.AddFavorite(adres, tytul); 

    //Opera
    } else if (window.opera && window.print) {

        var a = document.createElement('a');
        a.setAttribute('href', adres);
        a.setAttribute('title', tytul);
        a.setAttribute('rel','sidebar');
        a.click();
    }
}

function showSubmenu(menuid, additionalX, additionalY)
{
	
	var id="mainmenu_"+menuid;
	
	var top=parseInt(document.getElementById(id).offsetTop);
	var left=parseInt(document.getElementById(id).offsetLeft);
	var width=parseInt(document.getElementById(id).offsetWith);
	
	

	var brws=navigator.userAgent;
	
	if (brws.indexOf("MSIE 6.0")!=-1)
	{
		//if(additionalY) additionalX=additionalX-274;
		//if(additionalY) additionalY=additionalY-35;
	}

 	if (browser.isNS && brws.indexOf("MSIE 7.0")==-1)
	{	
	
		//left=left-274;
		//top=top-35;
	}

	else if(brws.indexOf("MSIE 7.0")!=-1)
	{
		//left=left+85;
	}


	top=top*1+additionalY*1;
	left=left*1+additionalX*1;	

	var subid="submenu_"+menuid;
	
	if(menuid!=currentsubdiv && currentsubdiv && !additionalX && !additionalY) hideSubmenu(currentsubdiv);

	//if(menuid!=currentsubdiv2 && currentsubdiv2 && additionalX && additionalY) hideSubmenu(currentsubdiv2);
	
	if(document.getElementById(subid))
	{
	
		if(!additionalX && !additionalY) currentsubdiv=menuid;
		if(additionalX && additionalY) currentsubdiv2=menuid;

		document.getElementById(subid).style.top=top+startSubdivY;
		document.getElementById(subid).style.left=left+startSubdivX;
	
	}

}

function hideSubmenu(menuid)
{
		
		if(currentsubdiv2)
		{
			var subid="submenu_"+currentsubdiv2;
			
			if(document.getElementById(subid))
			{
				document.getElementById(subid).style.top=-1000;
				currentsubdiv2=0;
			}

		}

		var subid="submenu_"+currentsubdiv;
		
		if(document.getElementById(subid))
		{
			document.getElementById(subid).style.top=-1000;
			currentsubdiv=0;
		}
}


function activateMenu(menuid, action)
{

	var menuLeft="menu"+menuid+"a";
	var menuMiddle="menu"+menuid+"b";
	var menuRight="menu"+menuid+"c";
	
	if(action=='on')
	{
		
		if(menuid==1) document.getElementById(menuLeft).className='menu_left_last_on';
		else document.getElementById(menuLeft).className='menu_left_on';

		if(document.getElementById(menuMiddle)) document.getElementById(menuMiddle).className='menu_middle_on';

		if(document.getElementById(menuRight))	
		document.getElementById(menuRight).className='menu_right_on';

	}

	if(action=='off')
	{
		
		if(menuid==1) document.getElementById(menuLeft).className='menu_left_off';
		else document.getElementById(menuLeft).className='menu_left_off';

		if(document.getElementById(menuMiddle)) document.getElementById(menuMiddle).className='menu_middle_off';

		if(document.getElementById(menuRight)) document.getElementById(menuRight).className='menu_right_off';
	}

}


function putFlashHere( sPath, iWidth, iHeight, sID) {
with (document) {
write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="../download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0/#version=5,0,0,0/default.htm" width="'+iWidth+'" height="'+iHeight+'" id="'+sID+'">');
write('  <param name="movie" value="'+sPath+'" />');
write('  <param name="quality" value="best" />');
write('  <param name="wmode" value="transparent" />');
write('  <param name="menu" value="false" />');
write('  <embed src="'+sPath+'" quality="best" wmode="transparent" menu="false" width="'+iWidth+'" height="'+iHeight+'" swLiveConnect=true name="'+sID+'" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="../www.macromedia.com/go/getflashplayer" />');
write('</object>');
}
}
