/*
 * Method to test if a string is empty
 */
String.prototype.blank = function () {
	return /^\s*$/.test(this);
}

/*
 * URL Redirection function
 */
function URLRedirect(url)
{
	window.location.replace(url,true);
}

/*
 * Queue object for generic use
 */
/*
 * Fila
 * Classe definindo os atributos e metodos de uma Fila
 */

function SAEQueue()
{
	this.elements = Array();
	return this;
}

SAEQueue.prototype.getSize = function()
{
	return this.elements.length;
}

SAEQueue.prototype.isEmpty = function()
{
	return (this.elements.length == 0);
}

SAEQueue.prototype.add = function()
{
	var a = arguments, i;
	for(i = 0; i < a.length; i++) {
		this.elements.push(a[i]);
	}
}

SAEQueue.prototype.clear = function()
{
	this.elements = Array();
}

SAEQueue.prototype.first = function ()
{
	if(!this.isEmpty()) {
		return this.elements.shift();
	} else {
		return null;
	}
}

SAEQueue.prototype.getQueueElements = function ()
{
	return this.elements;
}


function SAE()
{
	this.initQueue = new SAEQueue();
	this.form = {};
	return this;
}

SAE.prototype.init = function()
{
	var f, q = this.initQueue;
	while(!q.isEmpty()) {
		f = q.first();
		if($type(f) == 'function')
			f();
	}
}

function SAECss()
{
	return this;
}

SAECss.prototype.show = function(name)
{
	var o = $(name);
	if($chk(o))
		o.setStyle('display','block');
}

SAECss.prototype.hide = function(name)
{
	var o = $(name);
	if($chk(o))
		o.setStyle('display','none');
}

function SAEForm()
{
	this.messages = Array();
	this.errorClass = '';
	return this;
}

SAEForm.prototype.setErrorClass = function(name)
{
	if(name.length)
		this.errorClass = name;
}

SAEForm.prototype.hilightErrorFields = function()
{
	var a = arguments, i, o = {};
	for(i = 0; i < a.length; i++) {
		if(a[i].length) {
			o = $(a[i]);
			if($chk(o) && !o.hasClass(this.errorClass) &&
			(o.type == 'text' || o.type == 'textarea' || o.type == 'password' || o.type == 'file')) {
				o.addClass(this.errorClass);
			}
		}
	}
}

SAEForm.prototype.addMessage = function(msg)
{
	var i;
	if($type(msg) == 'array'){
		for(i = 0; i < msg.length; i++)
			this.messages.push(msg[i]);
	} else {
		this.messages.push(msg);
	}
}

SAEForm.prototype.showMessages = function()
{
	var i, j, msgs, msg, el, f;

	msgs = $('msgContainer');

	if($chk(msgs) && this.messages.length){
		ul = new Element('ul');
		// Show every message and hilight each field
		for(i = 0; i < this.messages.length; i++){
			msg = this.messages[i];
			li = new Element('li', {'html': msg.message});
			li.inject(ul);
			f = msg.field.split(' ');
			for(j = 0; j < f.length; j++)
				this.hilightErrorFields(f[j]);
		}
		el = new Element('p', {'html': 'Problemas com os dados do formul&aacute;rio:', 'class': 'redBold'});
		el.inject(msgs);
		ul.inject(msgs);
		this.focusFirstField();
	}
	msgs.setStyle('display','block');
}

SAEForm.prototype.focusFirstField = function()
{
	var o;
	if(this.messages.length) {
		o = $(this.messages[0].field);
		if($chk(o))
			o.focus();
	}
}


SAEForm.prototype.check = function(id)
{
	$$(id).each(function(el) { el.checked = true; });
}

SAEForm.prototype.uncheck = function(id)
{
	$$(id).each(function(el) { el.checked = false; });
}

SAEForm.prototype.invertCheck = function(id)
{
	$$(id).each(function(el) { el.checked = el.checked ? false : true; });
}

SAEForm.prototype.send = function(name)
{
	o = $(name);
	if($chk(o))
		o.submit();
}

SAEForm.prototype.setValue = function(field, value)
{
	var o = $(field);
	if($chk(o) && o.type && (o.type == 'edit' || o.type == 'textarea' || o.type == 'hidden')) {
		o.value = value;
	}
}


var sae = new SAE();
var saeCss = new SAECss();
var saeForm = new SAEForm();
saeForm.setErrorClass('fieldError');

