///////////////////
// externallinks.js

function ExternalLinks() {
  if (!document.getElementsByTagName) return;
  var anchors = document.getElementsByTagName("a");
  for (var i=0;i<anchors.length;i++) {
    var anchor = anchors[i];
    if (anchor.getAttribute("href")&&anchor.getAttribute("rel") == "external") anchor.target = "_blank";
  }
}


///////////////
// event.js

function attachEventListener(target, eventType, functionRef, capture) {
	if (typeof target.addEventListener != 'undefined') {
	   target.addEventListener(eventType, functionRef, capture);
	} else
	if (typeof target.attachEvent != 'undefined') {
	   target.attachEvent('on' + eventType, functionRef);
	} else {
	   return false;
	}
	return true;
}

function removeEventListener(target, eventType, functionRef, capture){
	if (target.removeEventListener){
	   target.removeEventListener(eventType, functionRef, capture);
	   return true;
	} else if (target.detachEvent){
	   var r = target.detachEvent("on"+eventType, functionRef);
	   return r;
	} else {
	   return false;
	}
}


///////////////////
// timer.js

// The constructor should be called with
// the parent object (optional, defaults to window).

function Timer(){
    this.obj = (arguments.length)?arguments[0]:window;
    return this;
}

// The set functions should be called with:
// - The name of the object method (as a string) (required)
// - The millisecond delay (required)
// - Any number of extra arguments, which will all be
//   passed to the method when it is evaluated.

Timer.prototype.setInterval = function(func, msec){
    var i = Timer.getNew();
    var t = Timer.buildCall(this.obj, i, arguments);
    Timer.set[i].timer = window.setInterval(t,msec);
    return i;
}
Timer.prototype.setTimeout = function(func, msec){
    var i = Timer.getNew();
    Timer.buildCall(this.obj, i, arguments);
    Timer.set[i].timer = window.setTimeout("Timer.callOnce("+i+");",msec);
    return i;
}

// The clear functions should be called with
// the return value from the equivalent set function.

Timer.prototype.clearInterval = function(i){
    if(!Timer.set[i]) return;
    window.clearInterval(Timer.set[i].timer);
    Timer.set[i] = null;
}
Timer.prototype.clearTimeout = function(i){
    if(!Timer.set[i]) return;
    window.clearTimeout(Timer.set[i].timer);
    Timer.set[i] = null;
}

// Private data

Timer.set = new Array();
Timer.buildCall = function(obj, i, args){
    var t = "";
    Timer.set[i] = new Array();
    if(obj != window){
        Timer.set[i].obj = obj;
        t = "Timer.set["+i+"].obj.";
    }
    t += args[0]+"(";
    if(args.length > 2){
        Timer.set[i][0] = args[2];
        t += "Timer.set["+i+"][0]";
        for(var j=1; (j+2)<args.length; j++){
            Timer.set[i][j] = args[j+2];
            t += ", Timer.set["+i+"]["+j+"]";
    }}
    t += ");";
    Timer.set[i].call = t;
    return t;
}
Timer.callOnce = function(i){
    if(!Timer.set[i]) return;
    eval(Timer.set[i].call);
    Timer.set[i] = null;
}
Timer.getNew = function(){
    var i = 0;
    while(Timer.set[i]) i++;
    return i;
}


///////////////
// textlimit.js

/**
 * TextLimit class v1.0
 * (c) 2007-02-13 Dominik Raœ
 */

if (typeof TextLimit == "undefined") var TextLimit = new Object();

TextLimit = function(id, maxChars) {

	if (!document.getElementById) { return; }

	this.timer = new Timer(this);

	this.CheckingInterval = 50;

	this.Interval = null;

	this.Listeners = new Array();

	this.TextID = id;
	this.maxChars = maxChars;

}

TextLimit.prototype = {

	SetCheckingInterval: function(time) {
          if (time>0) this.CheckingInterval=time;
	},

	SetMaxChars: function(maxChars) {
          if (time>maxChars) this.maxChars=maxChars;
	},

	getNew: function(){
	  var i = 0;
	  while (this.Listeners[i]) i++;
	  return i;
	},

	AddListener: function(id, text) {
          if (typeof id!='object') return;
          var i = this.getNew();
          this.Listeners[i]=new Array(id, text);
	},

	ClearListeners: function() {
          this.Listeners=new Array();
	},

        CheckListeners: function() {
	  var i = 0;
          while (this.Listeners[i]) {
            if (typeof this.Listeners[i][0]=='object') {

               var text=this.Listeners[i][1];

               // text length
               var regEx = new RegExp('%1', 'gi') ;
               text = text.replace(regEx, this.TextID.value.length);

               // free chars
               var regEx = new RegExp('%2', 'gi') ;
               text = text.replace(regEx, this.maxChars-this.TextID.value.length);

               // max chars
               var regEx = new RegExp('%3', 'gi') ;
               text = text.replace(regEx, this.maxChars);

               this.Listeners[i][0].value=text;

            }
            i++;
          }
        },

        CheckText: function() {
          if (this.TextID.value.length>this.maxChars) {
             this.TextID.value=this.TextID.value.substring(0, this.maxChars);
          }
          this.CheckListeners();
        },

	Start: function() {
          if (!document.getElementById) { return; }
          if (typeof this.TextID=='object') {
             if (this.Interval==null) this.Interval=this.timer.setInterval("CheckText", this.CheckingInterval);
          }
	},

	Stop: function() {
          if (!document.getElementById) { return; }
          if (this.Interval) this.timer.clearInterval(this.Interval);
	}

}




//////////////
// showhide.js

function ShowHideDiv(div_id, span) {
  var _style=(span===true) ? 'inline' : 'block';
  var obj=document.getElementById(div_id);
  obj.style.display=(obj.style.display=='none') ? _style : 'none';
  return ((obj.style.display=='none') ? false : true);
}

function ShowDiv(div_id, span) {
  var _style=(span===true) ? 'inline' : 'block';
  document.getElementById(div_id).style.display=_style;
}

function HideDiv(div_id) {
  document.getElementById(div_id).style.display='none';
}


/////////////
// cookie.js

function SetCookie(name,value,minutes) {
  if (minutes) {
    var date = new Date();
    date.setTime(date.getTime()+(minutes*60*1000));
    var expires = "; expires="+date.toGMTString();
  } else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}


//////////////
// bookmark.js

function SetBookmark(title,url) {
  // firefox
  if (window.sidebar) window.sidebar.addPanel(title, url, ""); else
  // opera
  if (window.opera && window.print) {
     var elem = document.createElement('a');
     elem.setAttribute('href',url);
     elem.setAttribute('title',title);
     elem.setAttribute('rel','sidebar');
     elem.click();
  } else
  // msie, wie
  if (document.all) window.external.AddFavorite(url, title);
}


/////////////////////////
// checkstrongpassword.js

function GenerateRandomPassword(frm) {
  var pwchars='abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNPQRSTUVWYXZ.-_';
  var passwd='';
  for (i=0;i<10;i++) {
      passwd+=pwchars.charAt(Math.floor(Math.random()*pwchars.length));
  }
  frm.haslo.value=passwd;
  CheckPassword(frm);
}

function CheckPasswordDifferentChars(s) {
  var safe=s.replace(/([\*\\\/\.\?\{\}\:\$\^\[\]\(\)\,\+])/g,'\\$1');
  var re=new RegExp(safe,'i');
  var letters='abcdefghijklmnopqrstuvwxyz';
  var numbers='01234567890';
  if (letters.match(re)||numbers.match(re)) return true;
  var first=s.charAt(0);
  for (var i=1;i<s.length;i++) if(s.charAt(i)!=first) return false;
  return true;
}

function CheckPasswordUpcase(s1,s2) {
  return (s1.toUpperCase().indexOf(s2.toUpperCase()));
}

function CheckPassword(frm) {

  var poziom=0;
  var v=frm.haslo.value;

  var regEx=new RegExp(' ', 'gi');
  v=v.replace(regEx, '');
  frm.haslo.value=v;

  if (v!='')
  if (v.length<6) {
     poziom=-1;
  } else
  if (!v.match('^([0-9a-zA-Z_.-]){6,}$')) {
     poziom=-2;
  } else {
     poziom=1;
     var zle=new Array('');
     zle=zle.concat(frm.login.value,'');
     for (var i=0;i<zle.length;i++) {
         if (zle[i]!=''&&CheckPasswordUpcase(v,zle[i])!=-1) {
            poziom=-1;
            break;
         }
     }
     if (poziom>-1) {
        if (!CheckPasswordDifferentChars(v)) {
           if (v.match(/[A-Z].*[A-Z]/i)&&v.match(/[^a-zA-Z\s].*[^a-zA-Z\s]/)) poziom++;
           if (v.length>=8) poziom++;

           szifty='\~\!\@\#\$\%\^\&\*\(\)\_\+\`\{\}\|\:\"\<\>\?';
           for (var i=1;i<szifty.length;i++) {
               if (v.indexOf(szifty.charAt(i))>0) {
                  poziom++;
                  break;
               }
           }

           var ciag_znakow=new Array();
           if (frm.login.value!='') ciag_znakow=ciag_znakow.concat(frm.login.value);
           for (var i=0;i<ciag_znakow.length;i++) {
               if (CheckPasswordUpcase(v,ciag_znakow[i])!=-1) {
                  poziom--;
                  break;
               }
           }
        } else {
           poziom=-1;
        }
     }
  }

  frm.securitylevel.value=poziom;
  var divs=new Array(-2,-1,0,1,2,3,4);
  for (var i=0;i<divs.length;i++) {
      div='SecurityLevelInfo'+divs[i];
      document.getElementById(div).style.display=(poziom==divs[i])?'inline':'none';
  }

}


//////////////////
// other functions

function setCheckboxes(fieldName, do_check) {
  var elts     = (typeof(fieldName) != 'undefined') ? fieldName : '';
  var elts_cnt = (typeof(elts.length) != 'undefined') ? elts.length : 0;
  if (elts_cnt) {
     for (var i = 0; i < elts_cnt; i++) elts[i].checked = do_check;
  } else {
     elts.checked = do_check;
  }
}

function CountCheckboxes(form_field) {
  var _count=0;
  if (typeof(form_field.length)=='undefined') {
     if (form_field.checked) _count++;
     return (_count);
  }
  for (i=0;i<form_field.length;i++) {
      if (form_field[i].checked) _count++
  }
  return (_count);
}

function OpenLinkInNewWindow(newURL) {
  newWindow=window.open(newURL,'','');
  newWindow.focus();
}

function CheckURL(url) {
  if (url!=''&&!url.match(/^(.+?)(?:\?(?:(.*?)@)?(.+))?$/)) {
     return false;
  }
  return true;
}

function escape_params(paramValue) {
  paramValue=paramValue.replace('&','%26');
  return paramValue;
}
