/**
 * Funkcja dodaje strone do ulubionych (tylko IE)
 *
 * @param		string		sURL						- adres strony
 * @param		string		sName						- nazwa strony
 */
function addToBookmarks(sURL, sName) {
	sNav = navigator.userAgent.toLowerCase();
	if (sNav.indexOf('msie') != -1 && sNav.indexOf('opera') == -1) {
		window.external.AddFavorite(sURL, sName);
	}
	else {
		alert ('Dostepne tylko dla przeglądarki Microsoft Internet Explorer');
	}
}

function SetCookie(sName, mValue, iDays) {
  if (iDays) {
    var iDate = new Date();
    iDate.setTime(iDate.getTime()+(iDays*24*60*60*1000));
    var sExpires = "; expires="+iDate.toGMTString();
  }
  else var sExpires = "";
  document.cookie = sName+"="+mValue+sExpires+"; path=/";
}

/**
 * Funkcja ustawia strone jako startowa (tylko IE)
 *
 * @param		string		sURL						- adres strony
 */
function setHomePage(oA, sURL) {
	sNav = navigator.userAgent.toLowerCase();
	if (sNav.indexOf('msie') != -1 && sNav.indexOf('opera') == -1) {
		oA.style.behavior='url(#default#homepage)';
		oA.setHomePage(sURL);
	}
	else {
		alert ('Dostepne tylko dla przeglądarki Microsoft Internet Explorer');
	}
}

function showPopup(sURL, sImgSrc, iImgWidth, iImgHeight, sImgAlt) {
  var iWindowWidth  = iImgWidth;
  var iWindowHeight = iImgHeight;
  var sParameters   = '';
  var sWindowName   = '';
  var sScrollBars   = '';
  iWindowWidth += 20;
  iWindowHeight += 20;
  if (iWindowHeight > screen.availHeight || iWindowWidth > screen.availWidth) {
    sScrollBars = 'scrollbars=yes';
    if (iWindowHeight > screen.availHeight) {
      iWindowHeight = screen.availHeight - 20;
    }
    if (iWindowWidth > screen.availWidth) {
      iWindowWidth = screen.availWidth - 20;
    }
  }
  else {
    sScrollBars = 'scrollbars=no';
  }
  iLeft = eval((screen.availWidth - iWindowWidth) / 2);
  iTop = eval((screen.availHeight - iWindowHeight) / 2);
  
  sParameters = "width=" + iWindowWidth + ",height=" + iWindowHeight + ", toolbar=no, location=no, directories=no, status=no, menubar=no, resizable=no, left=" + iLeft + ", top=" + iTop + ", " + sScrollBars;
  sURL += '?img=' + sImgSrc + '&w=' + iImgWidth + '&h=' + iImgHeight +
    '&alt=' + sImgAlt;
  sImgSrc = sImgSrc.replace(/\//gi, '_');
  sImgSrc = sImgSrc.replace(/\\/gi, '_');
  sImgSrc = sImgSrc.replace(/\./gi, '_');
  window.open(sURL, sImgSrc, sParameters);
}

function openPopup(sURL, iImgWidth, iImgHeight) {
  var iWindowWidth  = iImgWidth;
  var iWindowHeight = iImgHeight;
  var sParameters   = '';
  var sWindowName   = 'print';
  var sScrollBars   = 'scrollbars=yes';
  if (iWindowHeight > screen.availHeight || iWindowWidth > screen.availWidth) {
    if (iWindowHeight > screen.availHeight) {
      iWindowHeight = screen.availHeight;
      if ((screen.availWidth - iWindowWidth) > 20) {
        iWindowWidth += 20;
      }
      else {
        iWindowWidth = screen.availWidth;
      }
    }
    if (iWindowWidth > screen.availWidth) {
      iWindowWidth = screen.availWidth;
      if ((screen.availHeight - iWindowHeight) > 20) {
        iWindowHeight += 20;
      }
      else {
        iWindowHeight = screen.availHeight;
      }
    }
  }
  
  sParameters = "width=" + iWindowWidth + ",height=" + iWindowHeight + ", toolbar=no, location=no, directories=no, status=no, menubar=no, resizable=no, left=" + eval((screen.width - iWindowWidth) / 2) + ", top=" + eval((screen.height - iWindowHeight) / 2) + ", " + sScrollBars;
  
  sWindowName = sURL.replace(/\W/g, '');
  window.open(sURL, sWindowName, sParameters);
}

function ShowMessageBox(sPrefix, sMsg, sPostfix, bError) {
	alert(sPrefix + "\n" + sMsg + sPostfix);
	return !bError;
}

function getObject(sID, oDoc) {
  if (!oDoc) {
  	oDoc = document;
  }
  if(document.layers) {
    if(oDoc.layers[sID]) {
    	return oDoc.layers[sID];
    }
    else {
      for(var x = 0, y; !y && x < oDoc.layers.length; x++) {
        y = getObject(sID, oDoc.layers[x].document);
      }
      return y;
    }
  }
  if(document.getElementById) {
  	return oDoc.getElementById(sID);
  }
  if(document.all) {
  	return oDoc.all[sID];
  }
  return document[sID];
}

function resizeToContent(sObject, iMargin) {
	var x = window;
	
	// okreslenie rozmiarow obiektu do ktorego bedzie dopasowywany rozmiar okna
	var oObj = getObject(sObject, x.document);
	if (!oObj) {return false;}
  var oW = oObj.style.width ? parseInt(oObj.style.width) : (oObj.offsetWidth ? oObj.offsetWidth : oObj.clip.width);
  if(!oW) {return false;}
  oW += 2 * iMargin;
  var oH = oObj.offsetHeight ? oObj.offsetHeight : oObj.clip.height;
  if(!oH) {return false;}
  oH += 2 * iMargin;
  
  // okreslenie aktualnych rozmiarow okna
  var myW = 0;
  var myH = 0;
  var d = x.document.documentElement;
  var b = x.document.body;
  if (x.innerWidth ) {
  	myW = x.innerWidth;
  	myH = x.innerHeight;
  }
  else if (d && d.clientWidth) {
  	myW = d.clientWidth;
  	myH = d.clientHeight;
  }
  else if (b && b.clientWidth) {
  	myW = b.clientWidth;
  	myH = b.clientHeight;
  }
  if(window.opera && !document.childNodes) {
  	myW += 16;
  }
  
  //alert(myW + ' => ' + myH);
  x.resizeBy(oW - myW, oH - myH);
  
  return false;
}

function toggleActivity(oCheckbox, fieldPrefix, disabledClass, enabledClass) {
	var oForm = oCheckbox.form;

	for (var i = 0; i < oForm.elements.length; i++) {
		if (oForm.elements[i].id.indexOf(fieldPrefix) == 0) {
			oForm.elements[i].disabled = !oCheckbox.checked;
			oForm.elements[i].className = (oCheckbox.checked ? enabledClass : disabledClass);
			if (getObject(oForm.elements[i].name + '_label') != null) {
				getObject(oForm.elements[i].name + '_label').style.display = (oCheckbox.checked ? 'inline' : 'none');
			}
		}
	}
}

function toggleVisibility(oCheckbox, sId) {
	if ((oObj = getObject(sId)) != null) {
		oObj.style.display = (oCheckbox.checked ? 'block' : 'none');
	}
}

function deobfuscate(sStr) {
	var aChars = sStr.split(/-/);
	var sOut = '';
	for (var i = 0; i < aChars.length; i++) {
		sOut += String.fromCharCode(aChars[i]);
	}
	return sOut;
}

function showArticle(iId) {
	oLNew = getObject('artykul_' + iId);
	oLOld = getObject('artykul_' + iSelArt);
	oANew = getObject('numerek_' + iId);
	oAOld = getObject('numerek_' + iSelArt);
	oLOld.style.display = 'none';
	oAOld.className = '';
	oLNew.style.display = 'block';
	oANew.className = 'selected';
	iSelArt = iId;
	SetCookie('selArt', iId, 0);
}

function changeCircle(iNo) {
	if (iNo != iSelCircl) {
		oCircle = getObject('circle');
		oNewBtn = getObject('cbtn_' + iNo);
		oOldBtn = getObject('cbtn_' + iSelCircl);
		
		oNewBtn.src = '/images/gfx/kwadrat_on.gif';
		oOldBtn.src = '/images/gfx/kwadrat_off.gif';
		
		changeImages(oCircle, oLoader);
		
		oImg = new Image();
		oImg.onload = function() {changeImages(oCircle, oImg)};
		oImg.src = '/images/gfx/kregi/' + iNo + '.jpg';
		
		iSelCircl = iNo;
		SetCookie('mitril_circle', iNo, 365);
	}
}

function changeImages(oImg, oImg2) {
	oImg.src = oImg2.src;
}
