﻿var Library = window.Library || {};

Library = function()
{
	var isIe = false;
	var isMoz = false;
	var isSaf = false;
	var isOp = false;
	var emptyGuid = "00000000-0000-0000-0000-000000000000";
	
	return {
	
	getCenterPointOnScreen: function(width, height)
	{
		var w = Library.isIe ? document.documentElement.clientWidth : window.innerWidth;
		var h = Library.isIe ? document.documentElement.clientHeight : window.innerHeight;
		var left = w / 2 - (width / 2);
		var top = h / 2 - (height / 2);

		return new Array(left, top);
	},

	getCenterObjectOnScreen: function(el)
	{
		var reg = YAHOO.util.Dom.getRegion(el);
		return Library.getCenterPointOnScreen(reg.right - reg.left, reg.bottom - reg.top);
	},

	getFirstElement: function(parent)
	{
		var i = 0;
		while(parent.childNodes[i] != null && parent.childNodes[i].nodeName == "#text")
			i++;
				
		return parent.childNodes[i];
	},

	replaceStr: function(str, strLookFor, strReplaceWith)
	{
		if(str == null)
			return "";
		
		var re = eval("/" + Library.escapeRegExp(strLookFor) + "/g");
		str = str.replace(re, strReplaceWith);
		return str;
	},

	isEnter: function(cancel, e)
	{
		var elem;
		if(Library.isIe)
		{
			e = window.event;
			elem = e.srcElement.tagName;
		}
		else
			elem = e.target.tagName;
		
		if(elem.toLowerCase() == "textarea")
			return false;
		
		if(e.keyCode == 13)
		{
			if(cancel)
			{
				e.cancelBubble = true;
				
				if(Library.isIe)
					e.returnValue = false;

				if(Library.isMoz)
				{
					e.preventDefault();
					e.stopPropagation();
				}
			}
			
			return true;
		}
		
		return false;
	},

	clickButtonOnEnter: function(strButtonId, e)
	{
		if(Library.isEnter(true, e))
		{
			if(strButtonId != null)
			{
				var objButton = null;
				if(typeof(strButtonId) == "string")
					objButton = document.getElementById(strButtonId);
				else
					objButton = strButtonId;
				
				if(objButton != null)
					objButton.click();
			}
		}
	},

	getWidthStrict: function(el)
	{
		var offset = el.offsetWidth;
		var width = offset;
		
		if(el.style.borderRightWidth != "")
			width -= parseInt(el.style.borderRightWidth);

		if(el.style.borderLeftWidth != "")
			width -= parseInt(el.style.borderLeftWidth);
		
		if(el.style.paddingRight != "")
			width -=  parseInt(el.style.paddingRight);
		
		if(el.style.paddingLeft != "")
			width -= parseInt(el.style.paddingLeft);

		return width;
	},

	getHeightStrict: function(el)
	{
		var offset = el.offsetHeight;
		var height = offset;
		
		if(el.style.borderBottomWidth != "")
			height -= parseInt(el.style.borderBottomWidth);

		if(el.style.borderTopWidth != "")
			height -= parseInt(el.style.borderTopWidth);
		
		if(el.style.paddingBottom != "")
			height -=  parseInt(el.style.paddingBottom);
		
		if(el.style.paddingTop != "")
			height -= parseInt(el.style.paddingTop);

		return height;
	},

	setBrowsers: function()
	{
		var agent = navigator.userAgent.toLowerCase();
		if(agent.indexOf("msie") > -1)
			Library.isIe = true;
		
		if(agent.indexOf("gecko") > -1 && agent.indexOf("safari") == -1)
			Library.isMoz = true;

		if(agent.indexOf("safari") > -1)
			Library.isSaf = true;

		if(agent.indexOf("opera") > -1)
			Library.isOp = true;
	},

	trace: function(str, flush)
	{
		window.setTimeout(function(){Library.traceWrite(str, flush)}, 100);
	},

	traceWrite: function(str, flush)
	{
		try
		{
			var obj = document.getElementById("TraceMessage");
			
			if(obj == null)
			{
				obj = document.createElement("div");
				document.body.appendChild(obj);
				obj.id = "TraceMessage";
			}
			
			if(!flush)
				obj.innerHTML += "<br/>trace: " + str;
			else
				obj.innerHTML = str;
		}
		catch(e){}
	},

	traceObject: function(obj)
	{
		Library.trace(obj);
		Library.trace("---------------------------------");
		
		for(var j in obj)
			Library.trace(j + ": " + obj[j]);
	},

	dateIsValid: function(source, args)
	{
		var textBox = document.getElementById(source.controltovalidate);
		args.IsValid = !isNaN(Date.parse(textBox.value));
	},

	requiredEmailAddressIsValid: function(source, args)
	{
		var textBox = document.getElementById(source.controltovalidate);

		if(textBox.value.length == 0)
		{
			args.IsValid = false;
			return;
		}
		
		Library.emailAddressIsValid(source, args);
	},

	emailAddressIsValid: function(source, args)
	{
		var regFilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var textBox = document.getElementById(source.controltovalidate);
		args.IsValid = regFilter.test(textBox.value);
	},

	moveToAnchor: function(anchor)
	{
		window.location = replaceStr(window.location.toString(), "#" + anchor, "") + "#" + anchor;
	},

	clearSelectList: function(lst)
	{
		while(lst.options.length > 0)
			lst.options[0] = null;
	},

	checkMaxLength: function(txt)
	{
		var max = txt.getAttribute("maxLength");
		if(max == null)
			return;
		
		max = parseInt(max);
		if(txt.value.length > max)
			txt.value = txt.value.substr(0, max);
	},

	autoIncreaseTextBoxHeight: function(txt, event)
	{
		var ctrl = false;
		var keyCode = event.keyCode;
		
		if(document.all)
			ctrl = event.ctrlKey
		else
			ctrl = event.ctrlKey
		
		if(ctrl)
		{
			var height = parseInt(txt.style.height);
			if(keyCode == 40 && height < 1000)
				txt.style.height = height + 10 + "px";
			
			if(keyCode == 38 && height > 100)
				txt.style.height = height - 10 + "px";
		}
	},

	autoSizeScrollBox: function(txt, max, min)
	{
		txt.style.height = min + "px";

		if(txt.scrollHeight > 0)
		{
			var newHeight = txt.scrollHeight + 10;
			if(max != null && max < txt.scrollHeight)
				newHeight = max;

			if(newHeight < min)
				newHeight = min;
		}
		else
			return;

		txt.style.height = newHeight + "px";
	},

	setOpacity: function(el, val)
	{
		if(Library.isIe)
			el.style.filter = val < 1 ? "alpha(opacity=" + val * 100 + ")" : null;
		else
			el.style.opacity = val;
	},

	escapeRegExp: function(s)
	{
		return s.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1');
	}
	
	};
}();

Library.setBrowsers();

//=======================================================================================
// PROTOTYPE EXTENSIONS
//=======================================================================================

var gsMonthNames = new Array('January', 'February',
'March','April','May','June','July','August','September','October','November','December');

var gsDayNames = new Array('Sunday',
'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

Date.prototype.format = function(f)
{
    if (!this.valueOf())
        return '&nbsp;';

    var d = this;

    return f.replace(/(yyyy|mmmm|mmm|mm|dddd|ddd|dd|hh|nn|ss|a\/p)/gi,
        function($1)
        {
            switch ($1.toLowerCase())
            {
            case 'yyyy': return d.getFullYear();
            case 'mmmm': return gsMonthNames[d.getMonth()];
            case 'mmm':  return gsMonthNames[d.getMonth()].substr(0, 3);
            case 'mm':   return (d.getMonth() + 1).zf(2);
            case 'dddd': return gsDayNames[d.getDay()];
            case 'ddd':  return gsDayNames[d.getDay()].substr(0, 3);
            case 'dd':   return d.getDate().zf(2);
            case 'hh':   return ((h = d.getHours() % 12) ? h : 12).zf(2);
            case 'nn':   return d.getMinutes().zf(2);
            case 'ss':   return d.getSeconds().zf(2);
            case 'a/p':  return d.getHours() < 12 ? 'am' : 'pm';
            }
        }
    );
}

String.prototype.trim = function() {return str.replace(/^\s*|\s*$/g,"")}
String.prototype.string = function(l) { var s = '', i = 0; while (i++ < l) { s += this; } return s; }
String.prototype.zf = function(l) { return '0'.string(l - this.length) + this; }
Number.prototype.zf = function(l) { return this.toString().zf(l); }