/*
	Basic javascript utilities.
*/
/* vim: set softtabstop=4 shiftwidth=4 expandtab: */

/*
    Select the text range of the given element.
*/
  function selectTextRange (input, selectionStart, selectionEnd) {
    input = document.getElementById (input);
    if (input.setSelectionRange) {
      input.focus();
      input.setSelectionRange(selectionStart, selectionEnd);
    }
    else if (input.createTextRange) {
      var range = input.createTextRange();
      range.collapse(true);
      range.moveEnd('character', selectionEnd);
      range.moveStart('character', selectionStart);
      range.select();
    }
  }


function REMselectTextRange (id, start, end) {
    try {
        var text = document.getElementById (id);
        text.focus ();

        if (text.createTextRange) {
            var trange = text.createTextRange ();
            trange.moveStart (start);

            if (! end)
                end = text.length;

            trange.moveEnd (end);
            trange.select ();
        }
    } catch (E) { }
}

/* trim a string of white space. */
function trim (str) {
    str += "";
    return ltrim (rtrim (str));
}

/* trim whitespace from the left of a string. */
function ltrim (str) {
    str += "";
    return str.replace (/^\s*[^\w]*/, "");
}

/* trim whitespace from the right of a string. */
function rtrim (str) {
    str += "";
    return str.replace(/\s*$/, "");
}

/*
    Check if the given needle is found in the haystack.
*/
function inArray (haystack, needle) {
    try {
        for (var i = 0; i < haystack.length; i++) {
            if (trim (haystack[i]) == trim (needle)) {
                return true;
            }
        }
    } catch (E) { }

    return false;
}

/*
    Return the directory name of the given path. This function 
    uses '/' as directory seperators.
*/
function dirname (path, seperator) {
    path = path + "";
    if (seperator == null)
        seperator = "/";

    if (path == "" || path == seperator)
        return seperator;

    var pieces = path.split (seperator);
    if (pieces.length <= 1)
        return path;

    var pathstr = "";
    for (var i = 0; i < pieces.length - 1; i++) {
        if (pathstr != "")
            pathstr += seperator;
        pathstr += pieces[i];
    }

    if (seperator.toString () == path.charAt(0).toString ())
        pathstr = seperator + pathstr;
   
 
    pathstr += seperator;
    return pathstr;
}

/*
    Swap an image src from on to off depending on what it current
    has set.
*/  
function toggleImageSrc (id, on, off) {
    try {
        var img = document.getElementById (id);
        if (img.src.match (".*?" + off))
            setImageSrc (id, on);
        else
            setImageSrc (id, off);

        return getImageSrc (id);
    } catch (E) { }
}

/*
    Set an image src attribute to the specified src parameter.
*/
function setImageSrc (id, src) {
    try {
        var img = document.getElementById (id);
        img.src = src;
    } catch (E) {}
}

/* 
    get the current image src to this img container. 
*/
function getImageSrc (id) {
    try {
        var img = document.getElementById (id);
        return img.src;
    } catch (E) {}
}

/*
    set the focus on the given container.
*/
function setFocus (id) {
    try {
        var input = document.getElementById (id);
        input.focus ();
    } catch (E) {}
}

// used with radio buttons.
function updateInputBoxType_radio (selected, prefix) {
    switch (selected) {
        case "0":
            // search terms
            show (prefix + "-terms");
            hide (prefix + "-cats");
            hide (prefix + "-groups");
            break;

        case "1":
            // Link Categories
            hide (prefix + "-terms");
            show (prefix + "-cats");
            hide (prefix + "-groups");
            break;

        case "2":
            // Custom Link Groups
            hide (prefix + "-terms");
            hide (prefix + "-cats");
            show (prefix + "-groups");
            break;
        default:
            // default to 0
            show (prefix + "-terms");
            hide (prefix + "-cats");
            hide (prefix + "-groups");
            break;
    }
}

// used with select lists.
function updateInputBoxType (selected, prefix) {
    switch (selected) {
        case "0":
            // search terms
            show (prefix + "-terms");
            hide (prefix + "-cats");
            hide (prefix + "-groups");
            break;

        case "1":
            // Link Categories
            hide (prefix + "-terms");
            show (prefix + "-cats");
            hide (prefix + "-groups");
            break;

        case "2":
            // Custom Link Groups
            hide (prefix + "-terms");
            hide (prefix + "-cats");
            show (prefix + "-groups");
            break;
        default:
            // default to 0
            show (prefix + "-terms");
            hide (prefix + "-cats");
            hide (prefix + "-groups");
            break;
    }
}


/*
    Change the class of the given container.
*/
function changeContainerClass (id, newclass) {
    try {
        var d = document.getElementById (id);

        // get the current class name.
        var curr = d.getAttribute ("class");
        if (curr != "")
            curr = d.getAttribute ("className");

        /* set for gecko. */
        d.setAttribute ("class", newclass);

        /* set for ie. */
        d.setAttribute ("className", newclass);
        
        return curr;
    } catch (E) {}
}

/*
    Check all checkbox inputs that have the specified pattern in their id.
*/
function checkInputByPattern (pattern) {
    var inputs = document.getElementsByTagName ("input");
    var id = "";

    // check all inputs
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == "checkbox") {
            id = inputs[i].id;
            if (id.match (pattern)) {
                // check this input.
                inputs[i].checked = true;
            } 
        }
    }   
}

/*
    Uncheck all checkbox inputs that have the specified pattern in their id.
*/
function unCheckInputByPattern (pattern) {
    var inputs = document.getElementsByTagName ("input");
    var id = "";

    // check all inputs
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == "checkbox") {
            id = inputs[i].id;
            if (id.match (pattern)) {
                // uncheck this input.
                inputs[i].checked = false;
            } 
        }
    }   
}

/*
    Toggle the checked or unchecked inputs.
*/
function toggleInputByPattern (pattern) {
    var inputs = document.getElementsByTagName ("input");
    var id = "";

    // check all inputs
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == "checkbox") {
            id = inputs[i].id;
            if (id.match (pattern)) {
                if (inputs[i].checked)
                    inputs[i].checked = false;
                else
                    inputs[i].checked = true;
            }
        }
    }
}

/*
    Set the cookie value.
*/
function setCookie (key, value, expires_on) {
    var default_year_offset = "2"

    if (expires_on) {
        var expires = expires_on;
    } else {
        var expires = new Date ();
        expires.setFullYear (eval (expires.getFullYear () + default_year_offset));
    }

    // set our cookie value.
    document.cookie = key + "=" + value + "; expires=" + expires.toGMTString ();
}

/*
    Get the cookie value.
*/
function getCookie (key) {
    var cookies = getCookies ();
    return cookies[key];
}

/*
    get all current cookies into an associtive array.
*/
function getCookies () {
    var cookies = new Object ();
    var name, value;
    var beginning, middle, end;
    for (name in cookies) {
        cookies = new Object ();
        break;
    }

    beginning = 0;
    while (beginning < document.cookie.length) {
        middle = document.cookie.indexOf ('=', beginning);
        end = document.cookie.indexOf (';', beginning);

        if (end == -1)
            end = document.cookie.length;
        if ((middle > end) || (middle == -1)) {
            name = document.cookie.substring (beginning, end);
            value = "";
        } else {
            name = document.cookie.substring (beginning, middle);
            value = document.cookie.substring (middle + 1, end);
        }

        cookies[name] = unescape (value);
        beginning = end + 2;
    }

    return cookies;
}

/*
    Display an inline popup container that is attached onto the given
    container.
*/
function print_hover_popup (title, body, title_class, body_class) {
    document.write ("<div style=\"\"></div>");

    

    if (title)
        document.write ("<div style=\"background-color: #fff; color: #000; border-bottom: 1px solid;\" class=\"" + title_class + "\">" + title + "</div>");

    if (body)
        innerHTML += "<div class=\"" + body_class + "\">" + body + "</div>";

    if (innerHTML != "") {
        var outerDiv = "<div";
        innerHTML = "<div style=\"position: absolute; z-index: 101; display: none;\">" + "<div style=\"background-color: white; color: black;\">" + innerHTML + "</div></div>";

        
    }

}

function openWindow(url) { 
    popupWin = window.open(url,'remote','menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=no,dependent,width=500,height=350,left=50,top=50');
}

function openWindowSmall (url) {
    popupWin = window.open(url,'remote','menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,dependent,width=600,height=350,left=50,top=50');
}

function openWindowLarge (url) {
    popupWin = window.open(url,'remote','menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,dependent,width=600,height=500,left=50,top=50');
}

function openWindow_resize (url) {
    popupWin = window.open(url,'remote','menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,dependent,width=600,height=350,left=50,top=50');
}

function openWindow_Size (url, width, height) {
    popupWin = window.open(url,'remote','menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,dependent,width=' + width + ',height=' + height);
}

function openNamedWindow (url, name) {
    var width, height;
    width = 600;
    height = 410;
    popupWin = window.open(url, name,'menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,dependent,width=' + width + ',height=' + height + ',left=50,top=50');
}

function openNamedWindow_Size (url, name, width, height) {
    popupWin = window.open(url, name,'menubar=no,toolbar=no,status=no,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height + ',left=50,top=50');
}

function openGenericWindow (url, name, options) {
    popWin = window.open (url, name, options);
}

    /*
        Populate the select list with the given items array.
    */
    function populateSelectList (id, values, clearfirst) {
        if (clearfirst) {
            clearSelectList (id);
        }

        var s = document.getElementById (id);
        var node = null;
        for (var i = 0; i < values.length; i++) {

            values[i] = trim (values[i]);
            if (values[i] != "") {
                node = document.createElement ("option");

                // set some values on this option.
                node.text = values[i];
                node.value = values[i];
                node.setAttribute ("innerText", values[i]);
                node.setAttribute ("value", values[i]);
                s.appendChild (node);
                node = null;
            }
        }

        values = null;
    }

    /*
        Clear the select list options.
        Note that this may be a slow operation on large lists.
    */
    function clearSelectList (id) {
        var s = document.getElementById (id);
s.innerHTML = "";
return;
        try {
            while (s.length > 0) {
                s.removeChild (s[0]);
                s = document.getElementById (id);
            }
        } catch (E) {}
    }










