  /**
   *  fScript JS Library Core Module
	*  -----------------------------------------------------------------
	*  @copyright Copyright (c) 2006-2008 fCMS Development Team
	*  @author Arne Blankerts <theseer@fcms.de>
	*	
	*/


   /*
    * fScript core class
    */		
	var fScript={
      
      isIE: false,
      isGecko: false,
      isOpera: false,
      isWebkit: false,
	   
	   showExceptions: true,
	   
    	// --------------------------------------------------------------------------------------------------------------
    	// create XMLHttpRequest Object
	   xmlHttpRequest: function() {
         var xhttp=false;
         try {
      	      if (window.XMLHttpRequest) {
      	         // Gecko, Opera, Safari, ...
      	         xhttp=new XMLHttpRequest();
      	      } else if (window.ActiveXObject) {
            		try {
            			// Internet Explorer 6.0
            			xhttp = new ActiveXObject("Msxml2.XMLHTTP");
            		} catch(e) {
            			// Internet Explorer 5.x
         				xhttp = new ActiveXObject("Microsoft.XMLHTTP");
         			}
      	      }
      			return xhttp;      	      
         } catch(e) {
      		   return false;
         }	               	   	      
	   },
	   
    	// --------------------------------------------------------------------------------------------------------------
   	   // strip tags from string
      	stripTags: function(str) {
   	      return str.replace(/<\/?[^>]+>/gi, '');
      	},
      	
    	// --------------------------------------------------------------------------------------------------------------
      	// extend object
      	extend: function (obj, properties) {   	   
   	      for (var property in properties) {
   	         obj[property] = properties[property];
            }
      	},
      	
    	   // --------------------------------------------------------------------------------------------------------------
      	// add onload event handler
      	registerOnload: function (handler,obj) {   	   
      	   this.registerEvent('load',handler,obj);
      	},
      	
      	// --------------------------------------------------------------------------------------------------------------
      	// input counter
      	inputCounter: function (inputID, max, id) {
      	    if (document.getElementById(inputID).value.length + 0 >= max) {
      	        document.getElementById(inputID).value = document.getElementById(inputID).value.substring(0, max - 0);
      	    }
      	    document.getElementById(id).innerHTML = max - document.getElementById(inputID).value.length - 0;
      	},
      	
      	// --------------------------------------------------------------------------------------------------------------
      	// exception handler
      	handleException: function(e,func) {      	   
      	   if (this.showExceptions) {
         	   var msg='';
               if(typeof e != 'string') {
         	     for (var x in e)  msg += x+':'+e[x]+'\n';
               } else {
                  msg=e;
               }
         	   if (!func) func='unkown function';
      	      alert('Exception in '+func+':\n\n'+msg);
      	   }
      	},
      	
        // --------------------------------------------------------------------------------------------------------------
        // register an Event-Handler
         registerEvent: function(event,handler,obj) {
                    
            try {
               
               if (typeof handler!='function') {
                  throw "Handler is not a function";
               }               
               var args = Array.prototype.slice.call(arguments,3);
               
               if (!obj) { obj=window; }
               if (obj.addEventListener) {
                  obj.addEventListener(event, function(evt) {
                     var rc=handler.apply(null, [evt].concat(args));
                     if (!rc) { evt.preventDefault? evt.preventDefault() : evt.returnValue = false; }
                     return rc;
                  },false);
                  return true;
               }
               if (obj.attachEvent) {
                 return obj.attachEvent('on'+event,function() {
                     var evt=window.event;                     
                     var rc=handler.apply(null, [evt].concat(args));
                     if (!rc) { evt.preventDefault? evt.preventDefault() : evt.returnValue = false; }
                     return rc;                    
                 });
                 return true;
               }           
             
               if (typeof obj['on'+event]=='function') {
                  var oldHandler=this['on'+event];
                  obj['on'+event]=function(evt) {                    
                     if (!evt) { evt=window.event;}                     
                     oldHandler.apply(null, [evt].concat(args)); 
                     var rc=handler.apply(null, [evt].concat(args));
                     if (!rc) { evt.preventDefault? evt.preventDefault() : evt.returnValue = false; }
                     return rc;                    
                  }
               } else {
                  obj['on'+event]=function(evt) { 
                     if (!evt) { evt=window.event;}                     
                     var rc=handler.apply(null, [evt].concat(args));
                     if (!rc) { evt.preventDefault? evt.preventDefault() : evt.returnValue = false; }
                     return rc;
                  }
               }
               return true;
             
            } catch (e) {
               fScript.handleException(e,'registerEventHandler');
               return false;
            }
        },
        
        // --------------------------------------------------------------------------------------------------------------
        // returns array with elements matching the specified className
        getElementsByClassName: function(className, base) {
          if(!base) base = document;
          if (typeof base.getElementsByClassName=='function') {
             return base.getElementsByClassName(className);
          }
          var nodeList=[];
          
          var el = base.getElementsByTagName('*');
          var r = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
          for(var i=0; i < el.length; i++) {
            if (r.test(el[i].className)) {
              nodeList.push(el[i]);
            }
          }
          return nodeList;
        },
        
        // --------------------------------------------------------------------------------------------------------------
        // returns array with elements matching the specified className
        getChildrenByTagName: function(tag, element) {
           var list=element.childNodes;
           var res=[];
           for(var x=0; x<list.length; x++) {
              var y=list[x];
              if ((y.nodeType == 1) && (tag == '*' || y.nodeName == tag)) {
                 res.push(y);
              }
           }
           return res;
        },
        
        // --------------------------------------------------------------------------------------------------------------
        
        init: function() {
           if (window.opera) {
              this.isOpera=true;
           } else if (navigator.userAgent.indexOf('WebKit')!=-1) {
              this.isWebkit=true;
           } else if (navigator.userAgent.indexOf('MSIE')!=-1) {
              this.isIE=true;
           } else if (navigator.userAgent.indexOf('Gecko')!=-1) {
              this.isGecko=true;
           }
           return this;
        }
     
  }.init(); // fScript class
	
	
	//
	//  Add indexOf to Array object if not available
	//
   if (!Array.indexOf) {
      
      Array.prototype.indexOf=function(key) {
         for(var i=0; i<this.length; i++) {
            if (this[i]==key) {
               return i;
            }
         }
         return -1;
      }
      
   }
   