/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
// | Geeklog 1.3                                                               |
// +---------------------------------------------------------------------------+
// | Commmon javascript functions                                              |
// |                                                                           |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2005,2006 by the following authors:                         |
// |                                                                           |
// |            Blaine Lang - blaine@portalparts.com                           |
// +---------------------------------------------------------------------------+
// |                                                                           |
// | This program is free software; you can redistribute it and/or             |
// | modify it under the terms of the GNU General Public License               |
// | as published by the Free Software Foundation; either version 2            |
// | of the License, or (at your option) any later version.                    |
// |                                                                           |
// | This program is distributed in the hope that it will be useful,           |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
// | GNU General Public License for more details.                              |
// |                                                                           |
// | You should have received a copy of the GNU General Public License         |
// | along with this program; if not, write to the Free Software Foundation,   |
// | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           |
// |                                                                           |
// +---------------------------------------------------------------------------+

// -------------------------------------------------------------------
// caItems(form object)
// Check All Items - generic function that can be used to check and un-check all items in a list
// Used in the Admin Lists - like on the moderation page
// -------------------------------------------------------------------
   function caItems(f) {
       var n=f.elements.length;
       for (i=0;i<n; i++) {
           var field=f.elements[i];
           if (field.type == 'checkbox' && field.name.match("delitem")) {
                if (f.chk_selectall.checked) {
                    field.checked=true;
                } else {
                    field.checked=false;
                }
           }

       }
   }


    // Sets a variable for the users current width and height of the main display area
    function setMainDisplayHW() {
        obj = document.getElementById('mainDisplayArea');

        // Method1 to get width of the main plugin display area
        mainDisplayHeight = obj.offsetHeight;
        // mainDisplayWidth =  obj.offsetWidth ;

        // Code example to detect horizontal scrollbars are being shown */
        if (document.body.clientWidth < document.body.scrollWidth) {
             //alert("horizontal scrolling - " + obj.clientWidth + ':' + document.body.clientWidth + ':' + document.body.scrollWidth);
        }

        // Method2 to get width of the main plugin display area
        // Using clientWidth appears to be the best
        if (document.body.clientWidth < obj.clientWidth) {
            //alert('1: ' + document.body.clientWidth + ':' + obj.clientWidth + ':' + document.body.scrollWidth);
            mainDisplayWidth = document.body.clientWidth - 150;
        } else {
            //alert('2: ' + document.body.clientWidth + ':' + obj.clientWidth + ':' + document.body.scrollWidth + ':' + obj.offsetWidth );
            /* Subtract the number of pixels that are over flowing */
            mainDisplayWidth = obj.clientWidth - (document.body.scrollWidth - document.body.clientWidth);
        }

        cookievalue = mainDisplayHeight + ',' + mainDisplayWidth;
        setMyCookie('windowhw',cookievalue,'','/');
    }


    function setMyCookie (name,value,expires,path,theDomain,secure) {
      var theCookie = name + "=" + escape (value) +
      ((expires)    ? "; expires=" + expires.toGMTString() : "") +
      ((path)       ? "; path="    + path   : "") +
      ((theDomain)  ? "; domain="  + theDomain : "") +
      ((secure)     ? "; secure"            : "");
      document.cookie = theCookie;
    }


    function getCookie (name)
    {
        var dc = document.cookie;
        var cname = name + "=";
        var clen = dc.length;
        var cbegin = 0;

        while (cbegin < clen) {
            var vbegin = cbegin + cname.length;
            if (dc.substring(cbegin, vbegin) == cname) {
                var vend = dc.indexOf (";", vbegin);
                if (vend == -1) vend = clen;
                    return unescape(dc.substring(vbegin, vend));
            }
            cbegin = dc.indexOf(" ", cbegin) + 1;
            if (cbegin== 0) break;
        }
    return null;
    }


    function activatetab(tabid,starttab,endtab) {

        // Clear the alert or status message in the plugin header
        var obj = document.getElementById('pluginerrmsg');
        if (obj) {
            obj.style.display='none';
        }
        for (var i = starttab; i <= endtab; i++ ) {
            // Clear tabs and the tab panel div
            if (document.getElementById('etab' + i))
                document.getElementById('etab' + i).className = '';
            if (document.getElementById('etab' + i + '_panel'))
                document.getElementById('etab' + i + '_panel').style.display = 'none';
        }
        // Select the desirec active tab and panel
        if (document.getElementById(tabid))
            document.getElementById(tabid).className = 'selected';
        if (document.getElementById(tabid + '_panel'))
            document.getElementById(tabid + '_panel').style.display = '';
    }

    function hideElement(elm) {

        var obj=document.getElementById(elm);
        if (obj) obj.style.display='none';
    }

    function showElement(elm) {

        var obj=document.getElementById(elm);
        if (obj) obj.style.display='';
    }

    function toggleElement(elm) {

        var obj = document.getElementById(elm);
        if(obj) {
            if(obj.style.display == 'none') {
                obj.style.display = '';
            } else {
                obj.style.display = 'none';
            }
        }
    }

    function toggleElements(elm1,elm2) {
        var obj1 = document.getElementById(elm1);
        var obj2 = document.getElementById(elm2);
        if (obj1 && obj2) {
            if(obj1.style.display == 'none') {
                obj1.style.display = '';
                obj2.style.display = 'none';
            } else {
                obj1.style.display = 'none';
                obj2.style.display = '';
            }
        }
    }

    // Used with a link to show/hide another element like a div
    // Link label will be toggled as well
    function linkToggleElement(link,elm,labelshow,labelhide) {
        var obj = document.getElementById(elm);
        if(obj) {
            if(obj.style.display == 'none') {
                obj.style.display = '';
                link.innerHTML = labelhide;
            } else {
                obj.style.display = 'none';
                link.innerHTML = labelshow;
            }
        }
    }


    function parseURL(url,parm) {
        parm = parm.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+parm+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( url );
        if( results == null )
            return "";
        else
            return results[1];
    }

    function parseXML(root,tagname) {
        try {
            var pnode = root.getElementsByTagName(tagname)[0];
            retval = '';
            for (var i = 0; i < pnode.childNodes.length; i++) {
                retval += pnode.childNodes[i].nodeValue;
            }
            return retval;
        } catch(e) { return false; }
    }
    
    function trim(str, chars) {
    	return ltrim(rtrim(str, chars), chars);
    }
     
    function ltrim(str, chars) {
    	chars = chars || "\\s";
    	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
    }
     
    function rtrim(str, chars) {
    	chars = chars || "\\s";
    	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
    }
