/*
//this file copyright Apposite Ltd 2005
*/
var DEBUG_MODE = 'alert'; //alert|status|window|ignore

var ERROR_MODE = 'alert'; // alert|log|ignore;
var ERROR_IMG_SRC = ''; //'javascript_error.php';

//set error handler event 
//window.onerror = error_handler;

function toggle_display(id, src_img){
    var div = get_obj(id);
    div.style.display = (div.style.display != 'none') ? 'none' : 'block' ;
    
    //change the image (if given)
    if (src_img && src_img.src){
        if (div.style.display != 'none'){
            src_img.src = src_img.src.replace('open','close');
        }
        else {
            src_img.src = src_img.src.replace('close','open');
        }
    }
}

function remove_class(ele,classname){
    var classes_to_find = classname.split(",");
    
    //remove other classes
    for (var i=0; i<classes_to_find.length; i++){
        var exp = new RegExp('\\b'+ trim(classes_to_find[i]) +'\\b');
        ele.className = ele.className.replace(exp,'');
    }
}

function replace_class(ele, find, replace){
    //replaces a comma separaed list of classnames

    remove_class(ele, find);
    
    //remove the replacement class (if it exists)
    remove_class(ele, replace);
    
    //then append the new class
    ele.className += ' '+ replace;
}

function dump(obj) {
    var str = "";
    for (var prop in obj){
        str += "obj." + prop + " = " + obj[prop] + "\n";
    }
    alert(str);
}

function trim(s){
    //trims leading and trailing whitespace from a string
    s = s.replace(/^\s+/,'');
    s = s.replace(/\s+$/,'');
    return s;
}

function error_handler(description, url, line){
    //supress all errors within the error handler
    //otherwise endless loops could occur?
    window.onerror = null;
    
    //mozilla doesn't give a url or line number.
    url = (url) ? url : window.location;
    line = (line) ? line : 'unknown';
    
    var msg = 'Msg = '+ description;
    msg += '\nUrl  = '+ url;
    msg += '\nLine = '+ line;

    switch (ERROR_MODE){
    case 'alert':
        window.alert(msg);
        break;

    case 'log':
        //log to an image
        //this doesn't appear to work in Firefox 0.9
        //errors are caught by the built in error handler
        var error_img = document.getElementById('ERROR_IMG');
        var img_src = ERROR_IMG_SRC +'?msg='+ escape(description) +'&url='+ escape(url) +'&line='+ line;
        
        if (error_img){
            error_img.src = img_src;
        }
        else if (document.body){
            //try to create the image
            var img = document.createElement('img');
            img.src = img_src;
            img.width = 1;
            img.height = 1;
            img.id = 'ERROR_IMG';
            document.body.appendChild(img);
        }
        else {
            //nothing we can do?
            //save to cookie?
        }
        break;
        
    case 'ignore':
        //do nothing
        break;
        
    default:
        throw Error('Invalid ERROR_MODE ('+ ERROR_MODE +')');
    }
    
    //restore the error handler
    window.onerror = error_handler;
    return true;
}


function debug(text,destination){
    var var_type = typeof(text);
    var msg = text +' ['+ var_type +']';
    
    if (undef(destination)){
        destination = DEBUG_MODE;
    }
    
    switch (destination){
    //pop-up an alert box
    case 'a':
    case 'alert':
        window.alert(msg);
        break;
        
    //write to the status bar
    case 's':
    case 'status':
        window.status = msg;
        window.setTimeout('window.status="";',3000);
        break;
    
    //send the text to a pop-up window.
    case 'w':
    case 'window':
        var win_options = 'height=600,width=250,directories=no';
        
        var debug_win = window.open('','DEBUG_WINDOW',win_options);
        //NOTE: pop-up blockers will stop this window from opening
        if (debug_win){
            debug_win.document.write(text +'<hr>');
            debug_win.focus();
        }
        else {
            throw Error('Debugging window couldn\'t be opened.\n\n'+ msg);
        }
        break;
        
    case 'ignore':
        //do nothing
        break;
        
    default :
        throw Error('Invalid debug destination ('+ destination +')');
    }
}


function get_obj(id){
    if (typeof(id) == 'string'){
        if (document.getElementById){
            if (document.getElementById(id)){
                return document.getElementById(id);
            }
            else {
                throw Error('id not found(' + id +').');
                return false;
            }
        }
        else {
            throw Error('Browser does not support document.getElementById.');
        }
    }
    else {
        return id;
    }
}

function objX(obj){
    if (obj.offsetParent == null){
        return obj.offsetLeft;
    }
    else{
        return obj.offsetLeft + objX(obj.offsetParent);
    }
}

function objY(obj){
    if (obj.offsetParent == null){
        return obj.offsetTop;
    }
    else{
        return obj.offsetTop + objY(obj.offsetParent);
    }
}

function undef(v) {
    var undefined_variable;
    return (v === undefined_variable);
}
function isdef(v) {
    return !undef(v) 
}

function get_parent(child, valid_tag_list){
    //loop through parent until it finds a parent tag of type parent_tag
    //parent tag can now be a comma separated list of tags (eg td,th,table)
    
    var NODE_TYPE_ELEMENT = 1;
    
    var valid_tags = valid_tag_list.split(',');
    
    //get the parents
    if (undef(child)){
        throw Error('Invalid child node');
        return null;
    }
    if (child.parentNode){
        var parent = child.parentNode;
    }
    else {
        throw Error('Child has no parent node or <node>.parentNode not supported');
        return null;
    }
    
    if (undef(parent)){
        //no more parents
        return null;
    }
    if (parent.nodeType == NODE_TYPE_ELEMENT){
         
        var tag_name = parent.tagName.toLowerCase();
        
        //loop through the tags
        for (var i=0; i<valid_tags.length; i++){
            if (valid_tags[i].toLowerCase() == tag_name){
                //found parent node
                return parent;
            }
        }
    } 
    //otherwise try the next parent
    return get_parent(parent, valid_tag_list);
}

function add_event(obj, event_name, func){
    //mz
    if (obj.addEventListener){
        obj.addEventListener(event_name, func , true);
        return true;
    } 
    //ie
    else if (obj.attachEvent){
        return obj.attachEvent("on"+ event_name, func);
    } 
    else {
        throw Error("Unable to add_event()");
        return false;
    }
} 

function cancelBubble(e){
    //stops an event from bubbling up the element heirachy
    if (e.stopPropagation){
        e.stopPropagation();
        return true;
    }
    else if (e.cancelBubble === false){
        e.cancelBubble = true;
        return true;
    }
    else {
        return false;
    }
}

function getElementsByClass(classname,tagname){
    //returns an array of elements that match the classname given
    //the classname can use wildcard the characters * and ? but is CASE SENSITIVE
    //returns an empty Array() if no elements found
    
    var elements = Array();
    
    if (undef(tagname)){
        tagname = '*';
    }
    //get the elements
    var tags = document.getElementsByTagName(tagname);
    //KLUDGE:
    //document.getElementsByTagName('*') only works for IE6+ (not IE5 or IE5.5);
    if ((tagname == '*') && (tags.length == 0) && (document.all)){
        //shouldn't use document.all
        tags = document.all;
    }
    
    for (var i=0; i<tags.length; i++){
        tag_classname = tags[i].className;
        
        var exp = new RegExp('\\b'+ classname +'\\b');
        
        if (tags[i].className.match(exp)){
            //add it to the return array
            elements[elements.length] = tags[i];
        }
    }
    
    return elements;
}

function event_obj(e,tag){
    //returns the object that fired the event
    var obj;
    
    if (isdef(e.srcElement)){
        obj = e.srcElement;
    }
    else if (e.target){
        obj = e.target;
    }
    else {
        throw Error('event source not found');
        return false;
    }
    if (isdef(tag)){
        //make sure we have the right element
        var obj_tag = (obj.tagName) ? obj.tagName : '';
        if (tag.toLowerCase() != obj_tag.toLowerCase()){
            obj = get_parent(obj,tag);
        }
    } 
    return obj;
}
function preventDefault(e){
    if (e.preventDefault){
        e.preventDefault();
    }
    else if (e.returnValue){
        e.returnValue = false;
    }
}

