﻿function evil(text) {
	eval(text);	
	return result;
}

var NB = function()
{
	var popup = {};
	popupDefaultConfig = "location=no,menubar=no,personalbar=no,resizeable=no,scrollbars=no,status=no,toolbar=no";
	openedWindows = [];

	return {
		InArray : function(str, arr) {
			for(var i = 0; i < arr.length; i++)
			{
				if( arr[i] == str )
				{
					return true;
				}
			}
			
			return false;
		
		},
		Popup : function(url, width, height, config, returnVal)
		{
			if(typeof(returnVal) == "undefined")
			{
				returnVal = false;
			}
			
			var popupName = url.replace(/[^a-z]/gi, "");
			
			xPos = Math.ceil((screen.availWidth - width) / 2);
			yPos = Math.ceil((screen.availHeight- height) / 2);
			
			
			config = (typeof(config) == "string") ? config : popupDefaultConfig;
			config += ",width="+width+",height="+height+",left="+xPos+",top="+yPos;
			
			var win = window.open(url, popupName, config);
			
			if(!win)
			{
	            alert("Das Popup kann nicht geöffnet werden.\nBitte deaktivieren Sie Ihren Popup-Blocker.");
			}
			
			if(win)
			{
			    win.focus();
			}
			
			
			if(returnVal)
			{
				return win;
			}
		},
		NewWindow : function(url)
		{
		    window.open(url, "_blank", "");
		}
	}
}();
NB.Math = function()
{
	return  {
		GetFloat : function(str, precision)
		{
			// if already numeric value
			if(typeof(str) == "number")
				return this.FormatFloat(str, precision);
			if(str == null || str == "")	return 0;

			str.indexOf(",");
			str = str.replace(",",".");
			str = str.replace(" ", "");
			re = new RegExp("([0-9.]+)");
			result = str.match(re);
			if(result != null)
				str = result[0];
			str = parseFloat(str);
			if(isNaN(str)) str = 0;

			return str;
		},
		FormatFloat : function(num, precision)
		{
			num = parseFloat(num);
			if(!isNaN(num))
			{
				num *= Math.pow(10, precision);
				num  = Math.round(num);
				num /= Math.pow(10, precision);
			}
			else
				return "NaN";
			return num;
		},
		FormatNumber : function(num, decplaces, decPointChar, thousandsSepChar)
		{
			var num = (String)(num);
			num = num.replace(",", ".");
			var re = new RegExp("^([0-9]|[0-9]{2}|[0-9]{3}])?(([0-9]{3})*)(\\.[0-9]*)?$", "i");
			var matches = re.exec(num);

			var dec = 0;
			if(typeof(matches[4]) == "string")
			{
				dec = (matches[4].replace(".", ""));
			}

			var thousandGroups = [];
			if(typeof(matches[2]) == "string")
			{
				for(var i = 0; i < matches[2].length; i+=3)
				{
					thousandGroups[thousandGroups.length] = matches[2].substr(i, 3);
				}
			}

			var numberBegin = "";
			if(typeof(matches[1]) == "string")
			{
				numberBegin = matches[1];
			}

			var numberMiddle = thousandGroups.join(thousandsSepChar);

			if(numberBegin.length > 0 && numberMiddle.length > 0)
			{
				numberBegin += thousandsSepChar;
			}


			var numberEnd = "";
			if(decplaces > 0)
			{
				dec = (String)(dec);
				numberEnd += dec.substr(0, Math.min(decplaces, dec.length));
				for(var i = numberEnd.length; i < decplaces; i++)
				{
					numberEnd += "0";
				}
			}

			var output = numberBegin + numberMiddle + decPointChar + numberEnd;
			return output;
		}
	};
}();
NB.Cookie = {
	Set : function(key, value, options)
	{
		value = encodeURIComponent(value);

		if(!options)
		{
			options = {};
		}

		if (options.hasOwnProperty("domain")) value += '; domain=' + options.domain;
		if (options.hasOwnProperty("path")) 	value += '; path=' + options.path;
		if (options.hasOwnProperty("duration")){
			var date = new Date();
			date.setTime(date.getTime() + options.duration * 24 * 60 * 60 * 1000);
			value += '; expires=' + date.toGMTString();
		}
		if (options.hasOwnProperty("secure")) value += '; secure';
		document.cookie = key + '=' + value;
		return true;
	},
	Get : function(key, defaultValue) {
		var value = document.cookie.match('(?:^|;)\\s*' + key + '=([^;]*)');
		return value ? decodeURIComponent(value[1]) : defaultValue;
	}
};


NB.DomHelper = {
	/**
	 * Ext.js DomHelper.js insertHTML
	 * @param {Object} where
	 * @param {Object} el
	 * @param {Object} html
	 */
	insertHtml : function(where, el, html)
	{
		var tableRe = /^table|tbody|tr|td$/i;
		where = where.toLowerCase();

        if(el.insertAdjacentHTML){

			if(tableRe.test(el.tagName)){
                var rs;
                if((rs = insertIntoTable(el.tagName.toLowerCase(), where, el, html)))
				{
                    return rs;
                }
            }

            switch(where){
                case "beforebegin":
                    el.insertAdjacentHTML('BeforeBegin', html);
                    return el.previousSibling;
                case "afterbegin":
                    el.insertAdjacentHTML('AfterBegin', html);
                    return el.firstChild;
                case "beforeend":
                    el.insertAdjacentHTML('BeforeEnd', html);
                    return el.lastChild;
                case "afterend":
                    el.insertAdjacentHTML('AfterEnd', html);
                    return el.nextSibling;
            }
            throw 'Illegal insertion point -> "' + where + '"';
        }


        var range = el.ownerDocument.createRange();
        var frag;
        switch(where){
             case "beforebegin":
                range.setStartBefore(el);
                frag = range.createContextualFragment(html);
                el.parentNode.insertBefore(frag, el);
                return el.previousSibling;
             case "afterbegin":
                if(el.firstChild){
                    range.setStartBefore(el.firstChild);
                    frag = range.createContextualFragment(html);
                    el.insertBefore(frag, el.firstChild);
                    return el.firstChild;
                }else{
                    el.innerHTML = html;
                    return el.firstChild;
                }
            case "beforeend":
                if(el.lastChild){
                    range.setStartAfter(el.lastChild);
                    frag = range.createContextualFragment(html);
                    el.appendChild(frag);
                    return el.lastChild;
                }else{
                    el.innerHTML = html;
                    return el.lastChild;
                }
            case "afterend":
                range.setStartAfter(el);
                frag = range.createContextualFragment(html);
                el.parentNode.insertBefore(frag, el.nextSibling);
                return el.nextSibling;
            }
            throw 'Illegal insertion point -> "' + where + '"';
	}
};

NB.loadScript = function(src, callback, scope) {
	scope = scope || window;
					
	var script = document.createElement("script");
	script.src = src;
	script.type = "text/javascript";
			
	function loaded()
	{
		if(typeof(callback) == "function")
		{
			callback.call(scope);
		}
	}
	
	if(typeof YAHOO == "undefined")
	{
		throw "YUI wasn't found";
	}
			
	// onload in ie6 doesn't work :(
	if(YAHOO.env.ua.ie)
	{
		script.onreadystatechange = function()
		{
			if(script.readyState == "loaded" || script.readyState == "complete")
			{
				loaded();
			}
		}
	}
	else
	{
		script.onload = function() 
		{
			loaded();
		}
	}
			
	var head = document.getElementsByTagName('head')[0];
	head.appendChild(script);	
};


