var mail,senha,errors;

//Prototype para incluir função de retirada de strings em branco
String.prototype.trim = function() {
	// Retira os espaços em branco do começo e fim da string
	return this.replace(/^\s*|\s*$/g, "");
}

/**
 * Validação de formulário
 * @param form
 * @param inputs
 * @return boolean
 * @author Michael
 */
function validateForm(form, inputs)
{
	var empty = [];
	var email = [];
	errors = [];
	
	for( type in inputs )
	{
		if( inputs[type] == 'empty' )
			empty.push(type);
		else if( inputs[type] == 'email' )
			email.push(type);
	}
	
	if( email.length > 0 )
		isMail(email, form);
	
	if( empty.length > 0 )
		isEmpty(empty, form);
		
	if( errors.length > 0 )
	{
		alert('Verifique o(s) campo(s):\n\n'+errors.join("\n"));
		return false;
	}
	else
	{
		return true;
	}
	
	
	return false;
	
}

/**
 * Verifica se os campos do formulário estão vazios
 * @param inputs
 * @param form
 * @return int number os elements is empty
 * @author Michael
 */
function isEmpty(inputs,form)
{	
	for( name in inputs )
	{
		for( i = 0; i < form.elements.length; i++ )
		{
			if( ( inputs[name] == form.elements[i].id ) && (form.elements[i].value.trim() == "") )
				
				if( form.elements[i].title != '' )
					errors.push("- "+form.elements[i].title);				
				else
					errors.push("- "+inputs[name]);				
		}
	}
}

/**
 * Verifica se a string passada é um e-mail válido
 * @param mail
 * @return
 * @author Michael
 */
function isMail(mail, form)
{
	var valid = false;
	var reMail = /\w+@\w{2,}\.\w+/;
	
	if( typeof(mail) == 'string' )
	{
		if( !reMail.test(mail) )
		{
			errors.push(mail+" não é um e-mail válido.");
		}
		
	}
	else
	{
		for( name in mail )
		{
			if( !reMail.test(document.getElementById(mail[name]).value) )
				errors.push("- "+mail[name]);
		}
	}
}