﻿// JavaScript File
//===============================================
//register namespace
//


/* Dusan 30.4.2009 - nefunguje v IE7
function lomtecRegisterNS(ns){
 var nsParts = ns.split(".");
 var root = window;

 for(var i=0; i<nsParts.length; i++){
    if(typeof (root[nsParts[i]]) == "undefined")  root[nsParts[i]] = new Object();
         root = root[nsParts[i]];
    }
};
*/

function isNamespaceRegistered(ns){
    try{
        if (eval("typeof(" + ns + ") == 'function'")){
            return true;
        } else {
            return false;
        }
    }catch(e){
        return false;
    }
}

function lomtecRegisterNS(ns){
    if (!isNamespaceRegistered(ns)){
        var nsParts = ns.split(".");
        var path = "";
    
        for(var i=0; i<nsParts.length; i++){
            if (i > 0) path += '.';
        
            path += nsParts[i];
        
            if (!isNamespaceRegistered(path)){
            
                if (i == 0){                
                    eval(path + ' = new Function();')           
                } else {
                    eval(path + ' = new Function();')           
                }
            }
        }    
        //alert(ns + ' namespace registered');
    } else {
        //alert(ns + ' namespace ALREDY registered');
    }
}

//===============================================
//    
//static functions for controls (find control, register event
//
//===============================================
//
//register lomtec.Utility.Namespace
lomtecRegisterNS("lomtec.Utility.UI");

//array for controls on page
lomtecRegisterNS("lomtec.Controls");
lomtec.Controls.Items = new Array();

//Utility.UI functions
lomtec.Utility.UI.addControl  = function(ctrl) {
    if(typeof(ctrl)=='object' && ctrl != null){
        return lomtec.Controls.Items.push(ctrl);
    }
    return 0;    
};

lomtec.Utility.UI.findControl  = function(id) {
    var ctrls = lomtec.Controls.Items;
    var len = ctrls.length;

    for(var i = 0;i<len;i++){
        if (ctrls[i].getID() == id) {
            if( ctrls[i].initialize() ) return null;
            return ctrls[i];
        }
    }
    return null;    
};

lomtec.Utility.UI.addEvent = function (id,type,f,ctrl){
    //find control object
    if(typeof(ctrl) == 'undefined' || ctrl == null){
        var ctrls = lomtec.Controls.Items;
        var len = ctrls.length;
        
        for(var i = 0;i<len;i++){
            if (ctrls[i].getID() == id) {ctrl = ctrls[i]; break; }
        }
    }
    
    //has control object (ctrl != null)
    if(ctrl != null){
       switch(type){
           case 'onchange':
                if(typeof(ctrl.onChange) != 'undefined') ctrl.onChange = f;
                break;
       }
       return ctrl;
    }
    return null;
};

//unregister event
lomtec.Utility.UI.removeEvent = function(id,type,ctrl){
    //find control object
    if(typeof(ctrl) == 'undefined' || ctrl == null){
        var ctrls = lomtec.Controls.Items;
        var len = ctrls.length;
        
        for(var i = 0;i<len;i++){
            if (ctrls[i].getID() == id) {ctrl = ctrls[i]; break; }
        }
    }
    
    //has control object (ctrl != null)
    if(ctrl != null){
       switch(type){
           case 'onchange':
                if(typeof(ctrl.onChange) != 'undefined') ctrl.onChange = null;
                break;
       }
       return ctrl;
    }
    return null;
};

//revoke method for object
lomtec.Utility.UI.revokeMethod = function(id,method){
    var ctrls = lomtec.Controls.Items;
    var len = ctrls.length;

    for(var i = 0;i<len;i++){
        if (ctrls[i].getID() == id) {
        
            if( ctrls[i].initialize() == 0 ) {
                var ctrl = ctrls[i];
                if(typeof(ctrl[method]) == 'function'){
                    switch(arguments.length){
                        case 2: return ctrl[method]();
                        case 3: return ctrl[method](arguments[2]);
                        case 4: return ctrl[method](arguments[2],arguments[3]);
                        case 5: return ctrl[method](arguments[2],arguments[3],arguments[4]);
                        case 6: return ctrl[method](arguments[2],arguments[3],arguments[4],arguments[5]);
                        default:return ctrl[method]();
                    }
                }
            }
        }
    }
    return false;
};

lomtec.Utility.UI.findChildrenByName = function(parent,name){
    if(parent != null && parent.childNodes.length != 0){
           for (var i=0;i<parent.childNodes.length;i++){
                if (parent.childNodes[i].nodeType == 1 && parent.childNodes[i].id.indexOf(name) > 0) return parent.childNodes[i];
                var ret = lomtec.Utility.UI.findChildrenByName(parent.childNodes[i],name);  if( ret != null) return ret;
           }
    }
    return null;
};

//alias for functions 
var $lomtecAddControl=lomtec.Utility.UI.addControl;
var $lomtecAddEvent=lomtec.Utility.UI.addEvent;
var $lomtecRemoveEvent=lomtec.Utility.UI.removeEvent;
var $lomtecFindControl=lomtec.Utility.UI.findControl;
var $lomtecRevokeControlMethod=lomtec.Utility.UI.revokeMethod;
//===============================================
//    
//extend string object 
//
//===============================================
//add zeros to begining
String.prototype.lomtecPadLeft = function(len,val){
    if(typeof(val) == 'undefined') val = '0';

    var result = '';
    for (var i=0;i<(len - this.length);i++) { result += '0'; }
    return (result + this);
};
//convert string to integer
String.prototype.lomtecParseInt = function(){
    var result = this;
    if(this.length >= 2){
        result = this.replace(/^0{1,}/g,'');
        if(result.length == 0) return 0;
    }
    return parseInt(result);
};


/*
Positioning HTML elements
*/

function getX(element){
    var x = 0;
    if(element.offsetParent){
        while(1) 
        {
          x += element.offsetLeft;
          if(!element.offsetParent)
            break;
          element = element.offsetParent;
        }
    }else if(element.x){
        x += element.x;
    }
   
    return x;
}

function getY(element){
    var y = 0;
    if(element.offsetParent){
        while(1)
        {
          y += element.offsetTop;
          if(!element.offsetParent)
            break;
          element = element.offsetParent;
        }
    } else if(element.y){
        y += element.y;
    }
    
    return y;
}

function getWidth(element){
    var width = 0;    
    // more standards compliant browsers (mozilla/netscape/opera/IE7)
    if (typeof(element.offsetWidth) != 'undefined')
    {
        width = element.offsetWidth;
    }     
    // others 
    else if (typeof(element.clientWidth) != 'undefined')
    {
         width = element.clientWidth;
    }
    
    return width;
}

function getHeight(element){
    var height = 0;
    // more standards compliant browsers (mozilla/netscape/opera/IE7)
    if (typeof(element.offsetHeight) != 'undefined')
    {
        height = element.offsetHeight;
    }     
    // others 
    else if (typeof(element.clientHeight) != 'undefined')
    {
         height = element.clientHeight;
    }
    
    return height;
}
