//-----------------------------------------------------------------
dom = (document.getElementById) ? true : false; 
ns5 = ((navigator.userAgent.indexOf("Gecko")>-1) && dom) ? true: false; 
ie5 = ((navigator.userAgent.indexOf("MSIE")>-1) && dom) ? true : false; 
ns4 = (document.layers && !dom) ? true : false; 
ie4 = (document.all && !dom) ? true : false; 
nodyn = (!ns5 && !ns4 && !ie4 && !ie5) ? true : false; 
//-----------------------------------------------------------------
function getId(id) { 
    if (typeof(id) == typeof({})) return id;
    return document.getElementById(id);
    return (ns4) ? document.layers[id] : (ie4) ? document.all[id] : (ie5||ns5) ? document.getElementById(id) : null; 
} 
//-----------------------------------------------------------------
function turnObj(id, ison){
    var obj = getId(id); 
    if (obj != null) obj.style.display =  ison ? '' : 'none';
}
//-----------------------------------------------------------------
function switchObj(id) {
    var obj = getId(id); 
    if (obj == null) return false;
    if (obj.style.display == 'none') { 
        obj.style.display = '';
        return true;
    } else {
        obj.style.display = 'none';
        return false;
    }
}
//-----------------------------------------------------------------
function switchObjVoid(id){ switchObj(id); }

// background color of the selected row 
var fnRowSelectedColor = '#ffffcc';

// background color of the row when a mouse is over it
var fnRowMouseOverColor = '#f8f8f8';

function taskMouseOver(obj) {
    if (!obj || !obj.style || obj.__fnSelected) return false;
    if (obj.style.backgroundColor == fnRowSelectedColor) return false;
    obj.style.backgroundColor = fnRowMouseOverColor;
}

function taskMouseOut(obj) {
    if (!obj || !obj.style || obj.__fnSelected) return false;
    if (obj.style.backgroundColor == fnRowSelectedColor) return false;  
    obj.style.backgroundColor = 'transparent';
}

function selectRow(rowId, select) {
    var oRow = getId(rowId);
    if (!oRow || !oRow.style) return false;
    if (select) {
        oRow.style.backgroundColor = fnRowSelectedColor;
        oRow.__fnSelected = true;
    } else {
        oRow.style.backgroundColor = 'transparent';
        oRow.__fnSelected = false;  
    }
}

var all_mails = new Array();
//-----------------------------------------------------------------
function spread_mail(id){
        switchObj('full_'+id);
}
//-----------------------------------------------------------------
//-----------------------------------------------------------------
//-----------------------------------------------------------------
function is_space(a){ return a==' '||a=='\t'||a=='\n'; }
//-----------------------------------------------------------------
function trim(a){
        if(typeof(a)!='string')
            return "NaS";
        var startpos=0;
        var endpos=0;
        for(var i=0; i < a.length; ++i){
            if(!is_space(a.charAt(i))){
                startpos=i; break;
            }
        }
        for(var i=a.length-1; i >= 0; --i){
            if(!is_space(a.charAt(i))){
                endpos=i+1; break;
            }
        }   
        
        return String(a).substring(startpos, endpos);
}

function nsEscape(string){
  if (!string) return false; 
 
  return string.replace(/[\r\n\s!"#$%&\/:;<=>?@\[\\\]^`{|}~]/g, function (str) { 
    return escape(str); 
  }) 
}

var Color = new Array('transparent','#ffe','#ffd','#ffc','#ffb','#ffa','#ff9');
var ColorDelete = new Array('transparent','#fee','#fdd','#fcc','#fbb','#faa','#f99');

function fadeElement(id, bg_color, delay, delAfrer, ColorArr) {
    if (!ColorArr) var ColorArr=Color;
    var el = getId(id);
    if (!el) return this;
    if (bg_color) ColorArr[0] = bg_color;  
    var idx = ColorArr.length-1;
    doFade(el, idx, delay, delAfrer, ColorArr);
}

function doFade(el, idx, delay, delAfrer, ColorArr) {
    if (!el || idx == -1) {
        if (delAfrer) el.parentNode.removeChild(el);
        return false;
    }
    el.style.backgroundColor = ColorArr[idx];
    idx -= 1;
    setTimeout(function() { doFade(el, idx, delay, delAfrer, ColorArr) }, delay || 150);
}

function Check (obj, field, uncheck_on_uncheck) {
  if (uncheck_on_uncheck == undefined) {
    uncheck_on_uncheck = true;
  }
  if (field.length) {
    for (var i = 0; i < field.length; i++) {
      var oField = field[i];
      if (oField.disabled || (!obj.checked && !uncheck_on_uncheck)) continue;
      if (oField.checked == obj.checked) continue;
      oField.checked = obj.checked;
      if (oField.onclick) oField.onclick();
    }
  } else {
    if (field.disabled || (!obj.checked && !uncheck_on_uncheck)) return;
    if (field.checked == obj.checked) return;
    field.checked = obj.checked;
    if (field.onclick) field.onclick();    
  }
  return true;
}


function runScripts(scripts) {
    if (!scripts) return false;
    for (var i = 0; i < scripts.length; i++) {
        var thisScript = scripts[i];   
        var text; 
        if (thisScript.src) {
            var newScript = document.createElement("script");
            newScript.type = thisScript.type;       
            newScript.language = thisScript.language;
            newScript.src = thisScript.src;             
            document.body.appendChild(newScript);   
        } else if (text = (thisScript.text || thisScript.innerHTML)) {
            var text = (""+text).replace(/^\s*<!\-\-/, '').replace(/\-\->\s*$/, '');
            eval(text);
        }
    }
}

function ajaxLoadTo(elt, url) {
    var to = (typeof(elt) == typeof({}))? elt : document.getElementById(elt);
    if (!to) {
        alert("Cannot load to "+elt+": no such element");
        return;
    }
    do_ajax2_request(url, function(text) { 
        to.innerHTML = text; 
        runScripts(to.getElementsByTagName('SCRIPT')); 
    });
}

// Assume that box is always closed on the stage of calling this function.
function prepareBox(boxId, hot, switchers, max_height, isOpened) {
    var cookName = 'switchers';
    
    // Box body is also switchable.
    switchers.push(document.getElementById(boxId));
    
    // Prepare hot area.
    var switcherFunc = function(e, initialCall) {
        var opened = getCookie(cookName);
        opened = opened? opened.split(' ') : [];
        
        // Detect if box is opened.
        var isOpened = false;
        for (var i=0; i<opened.length; i++) {
            if (opened[i] == boxId) {
                opened.splice(i, 1);
                isOpened = true;
                break;
            }
        }
        
        // Switch objects.
        if (initialCall && isOpened || !initialCall) {
            if (!initialCall) isOpened = !isOpened;
            for (var i=0; i<switchers.length; i++) {
                if (switchers[i] && switchers[i].id != boxId) switchObj(switchers[i]);
                else SlideBox(boxId);
            }
        }
        
        if (isOpened) opened.push(boxId);
        setCookie(cookName, opened.join(' '), '/', new Date(new Date().getTime()+3600*24*365*1000));
    }
    
    // Assign mousedown event.
    getId(hot).onmousedown = switcherFunc;
    
    // If we must NOT save state of this box, it is always closed.
    if (isOpened != null) {
        var opened = getCookie(cookName) || '';
        if (!isOpened) {
            opened = opened.replace(new RegExp('(^| )' + boxId + '( |$)'), '$1');
        } else {
            opened = (opened? opened + ' ' : '') + boxId;
        }
        setCookie(cookName, opened, '/', new Date(new Date().getTime()+3600*24*365*1000));
    }
    
    // Initialize box (possible open or not).
    switcherFunc(null, true);
}

function rotateFlag(elements) {
    // Find current value.
    var cur = null;
    for (var i=0; i<elements.length; i++) {
        if (elements[i].style.display != 'none') {
            cur = i;
            break;
        }
    }
    if (cur==null) cur = elements.length-1;
    cur = (cur+1) % elements.length;
    for (var i=0; i<elements.length; i++) {
        elements[i].style.display = (i==cur)? '' : 'none';
    }
    return elements[cur];
}

// Cross-browser addEventListener()/removeEventListener() replacement.
function addEvent(obj, type, fn) {
    if (obj.addEventListener) {
        obj.addEventListener( type, fn, false );
    } else if (obj.attachEvent) {
        obj["e"+type+fn] = fn;
        obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
        obj.attachEvent( "on"+type, obj[type+fn] );
    }
    return fn;
}

function removeEvent(obj, type, fn) {
    if (obj.removeEventListener) {
        obj.removeEventListener( type, fn, false );
    } else if (obj.detachEvent) {
        obj.detachEvent( "on"+type, obj[type+fn] );
        obj[type+fn] = null;
        obj["e"+type+fn] = null;
    }
}

function cancelEvent(e) {
    if (!e) e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	if (e.preventDefault) e.preventDefault();
    return e.returnValue = false;    
}

function incrementInnerHTML(e, step, min, max) {
    e = getId(e);
    if (!e) return;
    var n = parseInt(e.innerHTML) + step;
    if (min != null && n < min) n = min;
    if (max != null && n > max) n = max;
    e.innerHTML = "" + n;
}

function getAbsPos(p) {
    var s = { x:0, y:0 };
    while (p.offsetParent) {
        s.x += p.offsetLeft;
        s.y += p.offsetTop;
        p = p.offsetParent;
    }
    return s;
}

function UserTZ() {
    var DefTZ=false;
    var d= new Date();
    if (d.getTimezoneOffset) DefTZ=-(d.getTimezoneOffset()/60);
    return DefTZ;
}

function showLoadingImage(e, img, w) {
    e = getId(e);
    var pos = getAbsPos(e);
    var i = document.createElement('IMG');
    i.src = img;
    i.style.position = 'absolute';
    i.style.left = pos.x + e.offsetWidth/2 - w/2;
    i.style.top = pos.y + 100;
    e.appendChild(i);  
}

function iterate(cont, f) {
    for (var i = 0; i < cont.length; i++) f(cont[i], i);
}
