var debug = true; 
var ErrorMessages = []; // new Array()
var dosetRoundedCorners = true;
var ShowAlerts = false;
var prototypeAvailable = false;
var ConsoleAvailable = false;

/**
 * Debug log wrapper
 * Uses console.log if available, otherwise alerts the log messages
 * Use when building functions
 * @param {String} msg
 */
function log(msg){
    if (ErrorMessages && typeof ErrorMessages == 'object') {
        ErrorMessages.push(msg);
    }
}

/**
 * Show gathered messages in one alert
 * Use this to output intermediate log messages in the code on execution
 * @param {String} msg
 */
function dump(msg){
    if (msg != null && msg.length > 0) {
        //old_alerts = alerts;
        alerts = msg;
    }
    else {
        for (var i = 0; i < ErrorMessages.length; i++) {
            alerts += ErrorMessages[i] + '\r\n';
        }
    }
    if (debug) {
        // use console if available? Works in FireFox firebug, Safari 3
        if (window.console && console.log) {
            console.log(alerts);
        }
        else {
            // opera alternative
            if (window.opera && opera.postError) {
                opera.postError(alerts);
            }
            else {
                // only alert if requested
                if (window.alert && ShowAlerts) {
                    alert(alerts);
                }
            }
        }
    }
}

/**
 * Create a browser popup window
 *
 * @param {String} url
 * @param {String} name
 * @param {String} w
 * @param {String} h
 * @param {String} scroll
 * @param {String} menubar
 */
function PopUp(url, name, w, h, scroll, menubar){
    var Window = null;
    LeftPosition = (screen.width) ? (screen.width - w) / 2 : 0;
    TopPosition = (screen.height) ? (screen.height - h) / 2 : 0;
    if(menubar == null){
		menubar = 0;
	}
	settings = 'height=' + h + ',width=' + w + ',top=' + TopPosition + ',left=' + LeftPosition + ',scrollbars=' + scroll+',resizable=1,menubar=' + menubar;
	Window = window.open(url, "_blank", settings);
    
}

function OpenWindow(URL) {
	var MyPopupWindow = window.open(URL,"Window","scrollbars=1,status=1,toolbar=0,menubar=1,resizable=1");
}

/**
 * Simple load handler
 * @param {Object} func
 */
function addLoadEvent(func){
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function(){
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

/**
 *	aliases for addLoadEvent
 */
addloadevent = addLoadEvent;
RegisterOnLoad = addLoadEvent;
addLoadListener = addLoadEvent;

//DOM ready watcher - scripting by brothercake -- http://www.brothercake.com/
function domFunction(f, a){
    var n = 0;
    var t = setInterval(function(){
        var c = true;
        n++;
        if (typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null)) {
            c = false;
            if (typeof a == 'object') {
                for (var i in a) {
                    if ((a[i] == 'id' && document.getElementById(i) == null) ||
                    (a[i] == 'tag' && document.getElementsByTagName(i).length < 1)) {
                        c = true;
                        break;
                    }
                }
            }
            if (!c) {
                f();
                clearInterval(t);
            }
        }
        if (n >= 60) {
            clearInterval(t);
        }
    }, 250);
};

/**
 * Get Elements by classname
 * @param {Object} classname
 * @param {Object} node
 */
function getElementsByClassName(classname, node){
    if (!node) 
        node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for (var i = 0, j = els.length; i < j; i++) 
        if (re.test(els[i].className)) 
            a.push(els[i]);
    return a;
}

/**
 *	Adds divs in side a container that can be positioned and styled using css.
 *	These divs will be positioned in the top left, top right, bottom left and bottom right corner of the block element.
 *	This of course requires the styles to be defined.
 */
function CreateRoundedCorners(className, containerId){
    dump("CreateRoundedCorners");
	className = (className != null) ? className : "rounded";
    containerId = (containerId != null) ? containerId : "container"; // PageContent only add the rounded corners to the elements in this container with the assigned classname
    var allcorners = '<div class="topleft"><!-- tl --></div><div class="topright"><!-- tr --></div><div class="bottomleft"><!-- bl --></div><div class="bottomright"><!-- br --></div>';
    var topcorners = '<div class="topleft"><!-- tl --></div><div class="topright"><!-- tr --></div>';
    var bottomcorners = '<div class="bottomleft"><!-- bl --></div><div class="bottomright"><!-- br --></div>';
    var corners = allcorners;
	var els;
	var container = document.getElementById(containerId);
	if(typeof container == undefined) return false;
	
	els = getElementsByClassName(className, container);
	
	for (var i = 0, j = els.length; i < j; i++) {
		
		dump('Adding Rounded Corners to ' + els[i].id);
		
		if(typeof Element != 'undefined'){
			// uses Prototype
			Element.insert(els[i], corners);
		}else{
			els[i].innerHTML = els[i].innerHTML + corners;
		}
	}
	
}

function SetRoundedCorners(){
    CreateRoundedCorners();
}

// set rounded corners / create the divs when the dom is loaded
if (dosetRoundedCorners) {	
	var rounding = new domFunction(function(){
		if (!document.getElementById('Footer')) return false;
		CreateRoundedCorners();
    }, {
        'Footer': 'id' // execute the function when this element is loaded
    });
}