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;
		
		},
		insertHtml : function(where, el, html)
		{
		    where = where.toLowerCase();
	        if(el.insertAdjacentHTML){
	            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 + '"';
	    },
		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.Popup = NB.popup;



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)
			{
				numberEnd += dec;
				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) {
		var value = document.cookie.match('(?:^|;)\\s*' + key + '=([^;]*)');
		return value ? decodeURIComponent(value[1]) : false;
	}
};
