
function isInteger (s)
{
    var i;

     if (isEmpty(s))
     if (isInteger.arguments.length == 1) return 0;
     else return (isInteger.arguments[1] == true);

     for (i = 0; i < s.length; i++)
     {
        var c = s.charAt(i);

        if (!isDigit(c) ){
	         return c == "-";
        }
     }

    return true;
}

/** You must specify value of DOM element */
function isEmpty(s)
{
   return ((s == null) || (s.length == 0))
}

function isDigit (c)
{
     return ((c >= "0") && (c <= "9"))
}

/** dom element as argument*/
function isVisible( c ) {
  return c.style.display != 'none';
}

/**
* checks whether given email address is valid.
*/
function isEmailValid(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   return reg.test(email);
}


function BrowserCheck() {
	var b = navigator.appName
	if (b=="Netscape") this.b = "ns"
	else if (b=="Microsoft Internet Explorer") this.b = "ie"
	else this.b = b
	this.version = navigator.appVersion
	this.v = parseInt(this.version)
	this.ns = (this.b=="ns" && this.v>=4)
	this.ns4 = (this.b=="ns" && this.v==4)
	this.ns5 = (this.b=="ns" && this.v==5)
	this.ie = (this.b=="ie" && this.v>=4)
	this.ie4 = (this.version.indexOf('MSIE 4')>0)
	this.ie5 = (this.version.indexOf('MSIE 5')>0)
	this.ie6 = (this.version.indexOf('MSIE 6')>0)
	this.ie7 = (this.version.indexOf('MSIE 7')>0)
	this.ff  = (navigator.userAgent.indexOf("Firefox")>0)
	this.ff2 = (navigator.userAgent.indexOf("Firefox/2")>0)
	this.min = (this.ns||this.ie)
}
is = new BrowserCheck()

var stat

function statBar(txt,time) {
   clearTimeout(stat);
   window.status = txt;
   stat=setTimeout("statErase()",time);
}

function statErase() {
   window.status="dLibra - biblioteka cyfrowa";
}

function preload(imgObj,imgSrc) {
	if (document.images) {
		eval(imgObj+' = new Image()')
		eval(imgObj+'.src = "'+imgSrc+'"')
	}
}

function changeImage(layer,imgName,imgObj) {
	if (document.images) {
		if (document.layers && layer!=null) eval('document.'+layer+'.document.images["'+imgName+'"].src = '+imgObj+'.src')
		else document.images[imgName].src = eval(imgObj+".src")
	}
}

function setCookie(name, value, expires, path, domain)
{
    var exp = new Date();
    var ex = new Date(exp.getTime()+expires);
    var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + ex.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "");
    document.cookie = curCookie; 
} 

function getCookie ( cookie_name )
{
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}

function hideLinks( whichSelect, link, val )
{
   if(document.forms.collections[whichSelect].value == val)
   {
     document.getElementById(link).style.visibility = 'hidden';
   }
   else
   {
     document.getElementById(link).style.visibility = 'visible'
   }
}

/*
  hide all select boxes on page.
*/
function hideSelect(){
	$("select").hide();
}

/*
 shows all select boxes on page
*/
function unhideSelect(){
	$("select").show();
}


function showPopup(homepageUrl, subPage, windowName) {
  pictureWindow = window.open(homepageUrl + subPage,windowName,'scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,height=500,width=470');
  pictureWindow.focus();
}

function showHelp(helpId, homepageUrl) {
  showPopup(homepageUrl, "/dlibra/help?id=" + helpId,"dlibra_help_window");
}

function showContact(homepageUrl) {
  showPopup(homepageUrl, "/dlibra/contact","dlibra_contact_window");
}


/**
 prepares box with ajax please wait dialog
 @param message - message which will be displayed.
 @param baseUrl - base url of webpage, you must specify this when 
                  using this method from main page. 
*/
function getAjaxWarning(message, baseUrl) 
{
     if (  baseUrl == null )
		return  message + '<br/> <img src="../style/common/img/pleasewait.gif" alt="[||--]" />';
 	 else
		return  message + '<br/> <img src="'+baseUrl+'/style/common/img/pleasewait.gif" alt="[||--]" />';
		
}


/**
* puts message into dom, and hides it after some time
  @param message - message which will be displayed.
  @param isSuccess - if true success image will be used
  @param messageDomId - dom id of message object which will be created.
  @param statusDomId - dom id of object which will be appended with messageDom object.
  @param timeout - optional time, default value is 2000;
  @param cssClass - optional css class name which will be assigned to message.
  @param baseUrl - base url of webpage, use this always when invoking this method
   on main webpage.
*/
function statusMessage(message, isSuccess, messageDomId, statusDomId, timeout, cssClass, baseUrl)
{
  if ( cssClass == null )   {
	  cssClass = 'error';
	  if ( isSuccess ) cssClass = 'success';
  }  
  if ( timeout == null ) 
      timeout=2000;
      
  if  ( baseUrl == null )
	  baseUrl = '../'; 
      
  var imageUrl = baseUrl+'/style/common/img/icons/statusok2.gif';
  if ( !isSuccess ) {
      imageUrl = baseUrl+'/style/common/img/icons/statuserror2.gif';
  }    
  $('#'+statusDomId).html('<span id="'+messageDomId+'" class="'+cssClass+'"><img src="'+imageUrl+'" alt="&nbsp" style="vertical-align:bottom"/>&nbsp;'+message+'</span>');
  setTimeout('$("#'+messageDomId+'").hide("medium")', timeout);
}

/**
  This function removes action parameter and its value 
  from given url. 
  Use this function always when you want to send dlibra url to
  external system.
  @param url - page url
*/
function removeAction(pageUrl) {
    var url = new String(pageUrl);
	var result = url;
	var startIndex = url.indexOf("?action=");
	if (startIndex == -1) {
		startIndex = url.indexOf("&action=");
	}
	if (startIndex != -1) {
		var secondPart = url.substring(startIndex + 1);
		var endPart = secondPart.indexOf('&');
		if (endPart == -1) {
			result = url.substring(0, startIndex);
		}
		else {
			result = url.substring(0, startIndex + 1)
					+ secondPart.substring(endPart + 1);
		}
	}
	return result;
}

function replaceAll( str, searchTerm, replaceWith, ignoreCase )	{
	var regex = "/"+searchTerm+"/g";
	if( ignoreCase ) regex += "i";
	return str.replace( eval(regex), replaceWith );
}