﻿function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);

function insertAfter(parent, node, referenceNode) {
	parent.insertBefore(node, referenceNode.nextSibling);
}

function roundTopCorners(element, radius, cssclass)
{
    new _roundCorners(element,radius,cssclass,false,true);
}
function roundBottomCorners(element, radius, cssclass)
{
    new _roundCorners(element,radius,cssclass,true,false);
}
function roundCorners(element, radius, cssclass)
{
    new _roundCorners(element,radius,cssclass,true,true);
}
function _roundCorners(element, radius, cssclass, bottom, top)
{

    element = $(element);
    
    if (element != null)
    {
        this.element = element;
        this.radius = radius;
        this.cssclass = cssclass;
        this.top = top;
        this.bottom = bottom;
        this.draw();       
    }
    return this;
}
_roundCorners.prototype = 
{
    draw : function()
    {
        if (this.top)
        {
            var curnode = this.element;
            for (var i=1;i<=this.radius;i++)
            {
                var node = this.getDivElement(i);
                var par = curnode.parentNode
                par.insertBefore(node,curnode)
                curnode = node
            }
        }
        if (this.bottom)
        {
            var cur = this.element;
            for (var i=1;i<=this.radius;i++)
            {
                var node = this.getDivElement(i);
                var par = cur.parentNode
                insertAfter(par,node,cur)
                cur = node
            }
        }
    },
    
    curveWidth : function(y){
        return Math.floor(Math.sqrt(this.radius*this.radius-y*y));
    },
    
    layerWidth : function(y){
        return this.baseWidth() + 2*this.curveWidth(y);
    },
    
    paddingWidth : function(y){
        return this.radius - this.curveWidth(y);
    },
    
    baseWidth : function(){
        return this.element.offsetWidth - this.radius * 2;
    },
    
    getDivElement : function(y)
    {
        var n = document.createElement("div");
        n.style.height = '1px';
        n.className = this.cssclass;
        n.style.width = this.layerWidth(y)+'px';
        n.style.marginLeft = this.paddingWidth(y)+'px';
        n.style.marginRight = this.paddingWidth(y)+'px';
        var i = document.createElement("img");
        i.src="_images/blank.gif";
        i.style.width = '1px';
        i.style.height = '1px';
        i.border='0';
        i.alt = ''
        n.appendChild(i)
        return n;
    }
    
    
    
};
function watermarkText(textbox, text)
{
var box = $(textbox);
var t = text;
var fn = function(){if(box.value==t){box.value='';box.className = '';}};
var fn2 = function(){if (box.value==''){box.value=t;box.className = 'graytext';}};
fn2();
addEvent(box,'click',fn);
addEvent(box,'focus',fn);
addEvent(box,'blur',fn2);
}


function add_collapse(collapsediv, linkbutton)
{
    linkbutton = $(linkbutton);
    collapsediv = $(collapsediv);
    collapsediv.style.display = 'block';

    var t = collapsediv;
    linkbutton.onclick = function(){collapse(collapsediv,linkbutton);return false;}

}

function collapse(cdiv, button)
{   
    //check for valid objects
    if(cdiv && button)
    {
        if (cdiv.style.display == 'none')
        {
            button.innerText = 'Collapse';
            cdiv.style.display = 'block';
        }
        else
        {
            button.innerText = 'Expand';
            cdiv.style.display = 'none';
           
        }
    }
    return false;
    
}

//Used with the dynarch Calendar control
function checkDateWidth(cal, firstfield, lastfield, format) 
	{
        var date = cal.date;
        var time = date.getTime();
        // use the _other_ field
        var field = $(firstfield);
        var field2 = $(lastfield);
        if (field != null && field2 != null)
        {
			var diff = parseDate(field2.value).getTime() - parseDate(field.value).getTime();
	       
			time += diff; 
			var date2 = new Date(time);
			field.value = date2.print(format);
        }      
    }

//Used with the dynarch Calendar control    
function parseDate(str)
    {
		var reg = /(?:([01]?[0-9])\/([0123]?[0-9])\/([0-9]{2,4})(?: ([01]?[0-9]):([012345]?[0-9]) ([pa]m))?)/ig;
		reg.exec(str);
		return new Date(RegExp.$1,RegExp.$2 - 1,RegExp.$3,RegExp.$4,((RegExp.$6=='PM')?(RegExp.$5 + 12):(RegExp.$5)),0,0);
    }
