function restore_default(iter) {
    var def = get_id('defmsg_' + iter)
    var area = get_id('sysmsg_' + iter)

    area.value = def.innerHTML
}

function clear_default(iter) {
    var area = get_id('sysmsg_' + iter)
    area.value = ''
}

function clear_all() {
    var count = get_id('sysmsg_count').value
    var i = 1

    for (; i <= count; i++) {
        clear_default(i)
    }
}

function restore_all() {
    var count = get_id('sysmsg_count').value
    var i = 1

    for (; i <= count; i++) {
        restore_default(i)
    }
}
function toggle_block(id) {
    var disp = get_id(id).style.display;

    get_id(id).style.display =
        (disp == 'none') ? 'block' : 'none';
}
function toggle_display(id) {
    var q = get_id(id);

    q.style.display =
        (q.style.display == 'none' ? '' : 'none')
}


/*
    DOM INSPECTOR
*/
function bible(object, firstLetter) {
    if ( firstLetter == undefined ) firstLetter = '';
    var out = '';
    for ( var i in object ) {
        if ( firstLetter == '' || i.substr(0, 1) == firstLetter )
            out += i + ' = ' + object[i] + '\n';
    }
    if ( ( ta = get_id('bible') ) == null ) {
        ta = document.createElement('textarea');
        ta.setAttribute('cols', 80);
        ta.setAttribute('rows', 25);
        ta.setAttribute('id', 'bible');
        document.body.appendChild(ta);
    }
    ta.value = out;
    //alert(out);
}


function toggle_all (my_list) {
    for (var c=0; c < my_list.elements.length; c++) {
        if (my_list.elements[c].type == 'checkbox') {
            if (my_list.elements[c].checked == true) {
                my_list.elements[c].checked = false;
            } else {
                my_list.elements[c].checked = true;
            }
        }
    }
    return false;
}
function checkmark_all (my_list,checkmarked) {
    for (var c=0; c < my_list.elements.length; c++) {
        if (my_list.elements[c].type == 'checkbox') {
            if (checkmarked.checked == true) {
                my_list.elements[c].checked = true;
            } else {
                my_list.elements[c].checked = false;
            }
        }
    }
    return false;
}
//-->
function NewWindow(mypage, myname, w, h, scroll, pos) {
    if ( pos == "random" ) {
        LeftPosition = ( screen.availWidth ) ? Math.floor(Math.random() * ( screen.availWidth - w ) ) : 50;
        TopPosition = ( screen.availHeight ) ? Math.floor(Math.random() * ( ( screen.availHeight - h ) - 75 ) ) : 50;
    }
    if ( pos == "center" ) {
        LeftPosition = ( screen.availWidth ) ? ( screen.availWidth - w ) / 2 : 50;
        TopPosition = ( screen.availHeight ) ? ( screen.availHeight - h ) / 2 : 50;
    }
    if ( pos == "default" ) {
        LeftPosition = 50;
        TopPosition = 50;
    }
    else if ( ( pos != "center" && pos != "random" && pos != "default" ) || pos == null ) {
        LeftPosition = 0;
        TopPosition = 20;
    }
    settings = 'width=' + w + ',height=' + h + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + scroll + ',location=no,directories=no,status=yes,menubar=no,toolbar=no,resizable=no';
    win = window.open(mypage, myname, settings);
    if ( win.focus ) win.focus();
}

function CloseNewWin() {
    if ( win != null && win.open ) win.close();
}

function xml_to_array(node) {
    var ary = new Array();
    var i   = 0;

    while (node != null) {
        if (node.firstChild) {
            if (node.firstChild.data) {
                if (node.textContent)
                    ary[node.nodeName] = node.textContent;
                else
                    ary[node.nodeName] = node.firstChild.data;
            }
            else
                ary[i] = xml_to_array(node.firstChild);
        }

        node = node.nextSibling;
        i++;
    }

    return ary;
}
function xml_to_array_nest(node) {
    var ary = new Array();

    while (node != null) {
        if (node.firstChild) {
            // its either data or a list
            if (node.firstChild.data) {
                if (node.textContent)
                    ary[node.nodeName] = node.textContent;
                else
                    ary[node.nodeName] = node.firstChild.data;
            } else {
                if (!(node.nodeName in ary)) {
                    ary[node.nodeName] = new Array();
                    ary[node.nodeName].push(xml_to_array_nest(node.firstChild));
                } else {
                    ary[node.nodeName].push(xml_to_array_nest(node.firstChild));
                }
            }
        }

        node = node.nextSibling;
    }

    return ary;
}
/*
 * Return the named class for a line of conversation in a session
 * based on its integer message_class field.
 */

function message_class(id) {
    switch (id) {
        case 0:
            return "client";
        case 1:
            return "operator";
        case 2:
            return "system_message";
        default:
            return "";
    }
}

function escape_unicode(message) {
    var out = "";
    var i;

    for (i = 0; i < message.length; i++) {
        if (message.charCodeAt(i) > 255)
            out += "&#" + message.charCodeAt(i) + ";";
        else
            out += message.charAt(i);
    }

    return out;
}
// From RFC 2045 (http://tools.ietf.org/html/2045), page 24

var base64_dec = {
    'A':  0, 'B':  1, 'C':  2, 'D':  3, 'E':  4, 'F':  5, 'G':  6, 'H':  7,
    'I':  8, 'J':  9, 'K': 10, 'L': 11, 'M': 12, 'N': 13, 'O': 14, 'P': 15,
    'Q': 16, 'R': 17, 'S': 18, 'T': 19, 'U': 20, 'V': 21, 'W': 22, 'X': 23,
    'Y': 24, 'Z': 25, 'a': 26, 'b': 27, 'c': 28, 'd': 29, 'e': 30, 'f': 31,
    'g': 32, 'h': 33, 'i': 34, 'j': 35, 'k': 36, 'l': 37, 'm': 38, 'n': 39,
    'o': 40, 'p': 41, 'q': 42, 'r': 43, 's': 44, 't': 45, 'u': 46, 'v': 47,
    'w': 48, 'x': 49, 'y': 50, 'z': 51, '0': 52, '1': 53, '2': 54, '3': 55,
    '4': 56, '5': 57, '6': 58, '7': 59, '8': 60, '9': 61, '-': 62, '!': 63,
    '=': 0
};

var base64_enc = {
     0: 'A',  1: 'B',  2: 'C',  3: 'D',  4: 'E',  5: 'F',  6: 'G',  7: 'H',
     8: 'I',  9: 'J', 10: 'K', 11: 'L', 12: 'M', 13: 'N', 14: 'O', 15: 'P',
    16: 'Q', 17: 'R', 18: 'S', 19: 'T', 20: 'U', 21: 'V', 22: 'W', 23: 'X',
    24: 'Y', 25: 'Z', 26: 'a', 27: 'b', 28: 'c', 29: 'd', 30: 'e', 31: 'f',
    32: 'g', 33: 'h', 34: 'i', 35: 'j', 36: 'k', 37: 'l', 38: 'm', 39: 'n',
    40: 'o', 41: 'p', 42: 'q', 43: 'r', 44: 's', 45: 't', 46: 'u', 47: 'v',
    48: 'w', 49: 'x', 50: 'y', 51: 'z', 52: '0', 53: '1', 54: '2', 55: '3',
    56: '4', 57: '5', 58: '6', 59: '7', 60: '8', 61: '9', 62: '-', 63: '!'
};

function base64_elshift(m, i, sh) {
    return (m.charCodeAt(i) << sh) & 63;
}

function base64_ershift(m, i, sh) {
    return (m.charCodeAt(i) >> sh) & 63;
}

// Base-64 encode a string, essentially by taking a 3-character block
// and turning it into a 4-character block using the base-64 alphabet.
// If less than 3 characters exist in the last block, the equal sign is
// used as padding (2 equal signs if only 1 character, 1 equal sign if 2
// characters).

function base64_encode(message) {
    var out = "";
    var buf0;
    var buf1;
    var buf2;
    var buf3;
    var i;

    message = escape_unicode(message);

    for (i = 0; i < message.length; i += 3) {
        buf0 = base64_enc[base64_ershift(message, i+0, 2)];
        buf2 = "_";
        buf3 = "_";

        if ((i+1) < message.length)
            buf1 = base64_enc[base64_elshift(message, i+0, 4) | base64_ershift(message, i+1, 4)];
        else
            buf1 = base64_enc[base64_elshift(message, i+0, 4)];

        if ((i+2) < message.length) {
            buf2 = base64_enc[base64_elshift(message, i+1, 2) | base64_ershift(message, i+2, 6)];
            buf3 = base64_enc[base64_elshift(message, i+2, 0)];
        } else if ((i+1) < message.length)
            buf2 = base64_enc[base64_elshift(message, i+1, 2)];

        out += buf0 + buf1 + buf2 + buf3;
    }

    return out;
}

function base64_dlshift(c, sh) {
    return (base64_dec[c] << sh) & 255;
}

function base64_drshift(c, sh) {
    return (base64_dec[c] >> sh) & 255;
}

function base64_decode(message) {
    var out = "";
    var i;

    // All base-64 blocks are multiples of four characters.  Try it:
    // encode a one-letter string.  You'll get four characters
    // in return.  If that's not the case with this message, then it's
    // not really base-64 encoded (or not encoded correctly).

    if ((message.length % 4) != 0)
        return message;

    // Each block of four encoded characters can be decoded to, at most,
    // three unencoded ones.  (Which makes sense: 4 * 6bits = 24bits,
    // and 3 * 8bits = 24bits.)  The bits in base-64 are encoded
    // left-to-right, that is, starting with the high-order bit and
    // moving to the low-order bit.  Each number we consider has a bit
    // mask of 255 applied, so only (low-order) 8 bits are considered at
    // any given moment.

    // The equal sign is considered "padding" in an encoded string, but
    // they also represent the end marker.  A block of four bytes with
    // two equal signs on the end is a signal that only one character is
    // encoded; with one equal sign, two characters encoded.  No equal
    // sign is necessary if the initial string's length was a multiple
    // of 3.

    for (i = 0; i < message.length; i += 4) {
        out += String.fromCharCode(base64_dlshift(message.charAt(i+0), 2) | base64_drshift(message.charAt(i+1), 4)); if (message.charAt(i+2) == '_') break;
        out += String.fromCharCode(base64_dlshift(message.charAt(i+1), 4) | base64_drshift(message.charAt(i+2), 2)); if (message.charAt(i+3) == '_') break;
        out += String.fromCharCode(base64_dlshift(message.charAt(i+2), 6) | base64_drshift(message.charAt(i+3), 0));
    }

    return out;
}

/* cpaint escapes ampersands, angle brackets, and all non-printables... */

function remove_unicode(message) {
    if (message == "" || message == null || message == undefined || message == 0)
        return "";

    message = base64_decode(message);

    message = message.replace(/\\u0026/g, '&');
    message = message.replace(/\\u003c\?/g, '&lt;?');   /* <? */
    message = message.replace(/\?\\u003e/g, '?&gt;');   /* ?> */
    message = message.replace(/\\u003c[ \t\r\n]*script/gi, '&lt;script');
    message = message.replace(/\\u003c[ \t\r\n]*\/[ \t\r\n]*script/gi, '&lt;/script');
    message = message.replace(/\\u003c/g, '<');
    message = message.replace(/\\u003e/g, '>');
    message = message.replace(/\\u000a/g, ' ');

    message = message.replace(/<!--c(#[0-9A-Fa-f]+)-->(.*)/, '<span style="color: $1">$2</span>');

    return message;
}

/*
 * This returns the actual data contained in the given named tag.
 * If there are several tags with that name, only the first one will
 * have its data returned.
 */


function cpaint_node(res, name) {
    var col;

    col = res.getElementsByTagName(name);

    if (col.length < 1 || col[0].firstChild == null)
        return "";
    else {
        if (col[0].textContent)
            return col[0].textContent;
        else
            return col[0].firstChild.data;
    }
}

/*
 * Cross-browser attempt at supporting the regular function,
 * document.getElementById().  That function is a DOM-2 level function,
 * which is something that IE -- even at version 6 -- does not yet fully
 * support.
 */

function get_id(id) {
    if (document.getElementById)
        return document.getElementById(id);
    else
        return document.all[id];        /* probably IE */
}

/*
 * Apparently, IE does not support the (somewhat useful) indexOf method,
 * which returns the index of a given value in an array.  So we've got
 * to reinvent the wheel here.
 *
 * NOTE: this function won't work for an array where the index could be
 * a string (or for an object with properties, which is essentially the
 * same thing).  In fact, it's probably not a good idea even for an
 * array where string and numeric indices are mixed.
 */

function index_of(ary, val) {
    var i;

    for (i = 0; i < ary.length; i++) {
        if (ary[i] == val)
            return i;
    }

    return -1;
}

function lookup_id(id) {
    return get_id(id).value
}

function run_command(type, param) {
    switch (type) {
        case 'push_url':
            window.open(param, "_blank");
            break;
        default:
            break;
    }
}
/*
    CLONER FUNCTIONS
*/

function clone_1st_div (node) {
    var original = node.getElementsByTagName ('div') [0];
    var new_node = original.cloneNode (true);
    clear_inputs (new_node);
    node.appendChild (new_node);
}

function clear_inputs (node)
{
    var newinput = node.getElementsByTagName ('input');
    for (var i=0; i<newinput.length; i++) {
        if (newinput[i].type == 'text') newinput[i].value = '';
    }
}

function remove_element (node)
{
    var papa = node;

    if (node.parentNode.getElementsByTagName ('div').length > 1)
    {
        node.parentNode.removeChild (node);
    }
    else
    {
        clear_inputs (node);
    }
}

function moveUp_element (node)
{
/*
    get value of my input
*/
    var myinput = node.getElementsByTagName ('input') [0];
    var myinputvalue = myinput.value;
/*
    find previous input, if exists
*/
    while (node.previousSibling && node.previousSibling.nodeName.toLowerCase () != 'div')
    {
        node = node.previousSibling;
    }
    if (node.previousSibling)
    {
/*
    now swap the values
*/
        node = node.previousSibling;
        myinput.value = node.getElementsByTagName ('input') [0].value;
        node.getElementsByTagName ('input') [0].value = myinputvalue;
    }
}
/*
    CLONER END
*/


function affect_if_checked (check_div,affect_div) {
	if (get_id(check_div).checked) {
		get_id(affect_div).style.display = 'block';
	} else {
		get_id(affect_div).style.display = 'none';
	}
}


