/**
 * function createCookie(name, value, days)
 * -----------------------------------------------------------------------------------------
 * Creates a client-side cookie which can be referred to later;
 * returns void
 **/
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


/**
 * function readCookie(name)
 * -----------------------------------------------------------------------------------------
 * Creates a client-side cookie which can be referred to later
 * returns data of selected cookie
 **/
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/**
 * function eraseCookie(name)
 * -----------------------------------------------------------------------------------------
 * Erases a cookie
 */
function eraseCookie(name) {
	createCookie(name,"",-1);
}





/**
 * function render_modal_view(view, frame_width, frame_height, params)
 * -----------------------------------------------------------------------------------------
 * An easier way of incorporating 'render_view' into a page - displays options as a modal
 * dialog, centered in the page sitting on top of an overlay.
 * 
 **/

function render_modal_view(view_name, frame_width, frame_height, params)
{
	// build up our POST request to pass on to Ajax.Updater
	_postBody = 'view=' + view_name;
	if(params != null) {
		_postBody += '&' + params;
	}

	// initialise our lightbox
	modal_frame = new ModalFrame(view_name, frame_width, frame_height);

	// render the view into the lightbox	
	new Ajax.Updater(modal_frame.frame_id, '/render_view.php', { 
		postBody: _postBody, 
		onComplete:	modal_frame.showFrame()
	});	
	
}







/**
 * function render_view(view, target, params, _onComplete)
 * -----------------------------------------------------------------------------------------
 * Nicely wraps an AjaxUpdater in a simple, easy-to-use function 
 * that fits in with the rest of Glue. accepts a callback function as it's 4th paramater.
 * 
 **/
function render_view(view, target, params, _onComplete) 
{	
	console.log("Rendering " + view);
	
	_postBody = 'view=' + view;
	if(params != null) {
		_postBody += '&' + params;
	}
	
	opt = { postBody: _postBody, onComplete: _onComplete }
	new Ajax.Updater(target, '/render_view.php', opt);	
}


/**
 * function remoteFunction(namespace_function, data, _onSucccess, _onFail)
 * -----------------------------------------------------------------------------------------
 * Remote Functionthingy
 **/
function remote_function(namespace_function, data, _onSuccess, _onFail) {
	
	params = namespace_function.split('.');
	req_uri = '/ajax/' + params[0] + '/' + params[1] + '/';
	
	new Ajax.Request(req_uri, { postBody : data,
								 onSuccess : _onSuccess,
								 onFailure : _onFail });
}
































function $I(iframe_id) {
	
	_iframe = $(iframe_id);
	r = null;
	
	if(_iframe.contentDocument) {
		r = _iframe.contentDocument;
	} else if(_iframe.contentWindow.document) {
		r = _iframe.contentWindow.document;
	}
	
	return r;
}





String.prototype.pad = function(length, padchar, type) {
	pad = '';
	
	if(length > this.length) {
		(length - this.length).times( function(index) {
			pad += padchar;
		});		
	}
	
	if(type) {
		return this + pad;
	}  else {
		return pad + this
	}
}