/* General functions
_____________________________________________________________ */

general = {
    
    formatNumber: function (pnumber,decimals) {
        
        if (isNaN(pnumber)) { return 0};
        if (pnumber=='') { return 0};
        
        var snum = new String(pnumber);
        var sec = snum.split('.');
        var whole = parseFloat(sec[0]);
        var result = '';
        
        if (sec.length > 1)
        {
            var dec = new String(sec[1]);
            dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals)));
            dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals));
            var dot = dec.indexOf('.');
            if(dot == -1){
                dec += '.';
                dot = dec.indexOf('.');
            }
            while(dec.length <= dot + decimals) { dec += '0'; }
            result = dec;
        }
        else
        {
            var dot;
            var dec = new String(whole);
            dec += '.';
            dot = dec.indexOf('.');       
            while(dec.length <= dot + decimals) { dec += '0'; }
            result = dec;
        }
        
        return result;
    },
    
    pad: function(number, length) {
    
        var str = '' + number;
        while (str.length < length) {
            str = '0' + str;
        }
        
        return str;
    },
    
    loadScript: function (sScriptSrc,callbackfunction)   
    {
        //gets document head element  
        var oHead = document.getElementsByTagName('head')[0];
        if(oHead && $('head script:last').attr('src') != sScriptSrc)  
        {  
            //creates a new script tag        
            var oScript = document.createElement('script');  
            
            //adds src and type attribute to script tag
            oScript.setAttribute('id','dynamic');
            oScript.setAttribute('src',sScriptSrc);  
            oScript.setAttribute('type','text/javascript');  
            
            //calling a function after the js is loaded (IE)  
            var loadFunction = function()  
            {  
                if (this.readyState == 'complete' || this.readyState == 'loaded')  
                {  
                    callbackfunction();   
                }  
            };  
            oScript.onreadystatechange = loadFunction;  
            
            //calling a function after the js is loaded (Firefox)  
            oScript.onload = callbackfunction;  
            
            //append the script tag to document head element          
            oHead.appendChild(oScript);  
        }
        else
        {
            callbackfunction();
        }
    },
    
    objDump: function (obj, name, indent, depth) {
        
        if (depth > 20)
        {
            return indent + name + ": <Maximum Depth Reached>\n";
        }

        if (typeof obj == "object")
        {
            var child = null;
            var output = indent + name + "\n";
            indent += "\t";
            
            for (var item in obj)
            {
                try {
                    child = obj[item];
                } catch (e) {
                    child = "<Unable to Evaluate>";
                }
                
                if (typeof child == "object")
                {
                    output += dumpObj(child, item, indent, depth + 1);
                }
                else
                {
                    output += indent + item + ": " + child + "\n";
                }
            }
            return output;
        }
        else
        {
            return obj;
        }
    }
}

/* External Link
########################################################################################*/

// Opens a link in a new window when class = external_link


$(function() {

    $("a.external_link").each(function(i){
        var tmp = $(this).html();
        $(this).html( tmp +'<span class="external_icon"> - (Opens in a new window)</span>');
        tmp = null;
    });
	
	$('a.external_link').click(function(){
		window.open(this.href);
		return false;
	});
    
});
