function set_cookie(name, value, expires) {
	document.cookie = name + '=' + escape(value) + '; path=/' + (typeof expires != 'undefined' ? '; expires=' + expires.toGMTString() : '');
}

function delete_cookie(name) {
	document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT' +  '; path=/';
}

function get_cookie(name) {
	cookie_name = name + '=';
	cookie_length = document.cookie.length;
	cookie_begin = 0;
	while (cookie_begin < cookie_length) {
		value_begin = cookie_begin + cookie_name.length;
		if (document.cookie.substring(cookie_begin, value_begin) == cookie_name) {
			var value_end = document.cookie.indexOf (';', value_begin);
			if (value_end == -1) {
				value_end = cookie_length;
			}
			return unescape(document.cookie.substring(value_begin, value_end));
		}
		cookie_begin = document.cookie.indexOf(' ', cookie_begin) + 1;
		if (cookie_begin == 0) {
			break;
		}
	}
	
	return false;
}

function fetch_cookie(name) {
	return get_cookie(name);
}

JSDrops = Class.create();

Object.extend(JSDrops, {
	current: false,
	obsFunc: function(evt) {
		var dropObj = JSDrops.current;
		
		if (dropObj.open) {
			if (!$(dropObj.drop).mouseWithin(evt) && !$(dropObj.anchor).mouseWithin(evt)) {
				dropObj.collapse();
			}
		}
	}
});

JSDrop = Class.create({
	initialize: function(links, anchor, objID) {
		
		var anchor	= this.anchor = $(anchor);
		var id		= 'drop_' + anchor.id;
		var pos		= anchor.cumulativeOffset();
		var drop 	= this.drop = new Element('div', {
			'id': 			id,
			'class':		'JSDrop_outer panel rounded'
		});
		
		this.objID	= objID;
		this.open	= false;
		
		if (typeof links == 'string') {
			drop.update($(links).innerHTML);
		} else
		if (typeof links == 'object') {
			links.each(function(link) {
				var thislink = new Element('div', {
					'class': 	'JSDrop_link darkpanel',
					'title':	link.title
				});
				
				var a = new Element('a', {
					'href':		(typeof link.href != undefined && link.href != undefined) ? link.href : '#',
					'onclick':	(typeof link.onclick != undefined && link.onclick != undefined) ? link.onclick : false,
					'title':	link.title
				}).update(link.title);
				
				thislink.appendChild(a);
				drop.appendChild(thislink);
			});
		} else {
			drop.update(links);
		}
		
		$('win').appendChild(drop);
		
		drop.setStyle({
			'position':	'absolute',
			'left':		parseInt(pos.left) + 'px',
			'top':		parseInt(pos.top) + parseInt(anchor.getHeight()) + 'px',
			'zIndex':	5000,
			'display':	'none'
		});
		
		anchor.setStyle({
			'cursor':	'pointer'
		});
		
		Event.observe(window, 'click', JSDrops.obsFunc);
	},
	
	expand: function() {
		JSDrops.current = this;
		
		this.drop.setStyle({display: 'block'});
		this.open = true;
	},
	
	collapse: function() {
		this.drop.setStyle({display: 'none'});
		this.open = false;
	}
});

Element.addMethods({
	limit: function(element, minValue, maxValue) {
		element = $(element);

		if (parseInt($F(element)) < parseInt(minValue)) element.update(minValue);
		if (parseInt($F(element)) > parseInt(maxValue)) element.value = maxValue;
	},
	
	hidden: function(element) {
		if (element.style.display == 'none') return true;
		return false;
	},
	
	mouseWithin: function(element, evt) {
		var mouse	= {'left': Event.pointerX(evt), 'top': Event.pointerY(evt)};
		var pos 	= element.cumulativeOffset();
		var range	= {
			'left':	$R(pos.left, parseInt(pos.left + element.getWidth())),
			'top': $R(pos.top, parseInt(pos.top + element.getHeight()))
		};
		
		if (range.left.include(mouse.left) && range.top.include(mouse.top)) return true;
	}
});

function $RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }

    var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}