/**********************************************************************
***********************************************************************
***
***		Nombre: Topo Válido
***		Versión: 0.1-pre alpha
***		Descripción: Libreria para la validación de formularios
***		Autor: Felipe Mallea Sentis
***		Web: http://www.budin.cl
***		Empresa: Topo
***		Web: http://www.topo.cl
***		Ultima Modificación: 15 de junio de 2008
***		Licencia: MIT
***	
***********************************************************************
**********************************************************************/

/***** Validadores *****/
function validateForm(elementArray)
{
	var validForm = "true";
	if(isArray(elementArray))
	{
		for (var i=0; i<elementArray.length; i++)
		{
			clearAlert(elementArray[i]);
			if(isElementEmpty(elementArray[i]))
			{
				appendAlert(elementArray[i]);
				validForm = "false";
			}
		}
	}
	if(validForm=="true")
		return true;
	else
		return false;
}
function validateEmailField(element)
{
	clearAlert(element);
	if(!isEmail(element.value))
		appendAlert(element);
}
function validateNonEmptyTextBox(element)
{
	clearAlert(element);
	if(isElementEmpty(element))
		appendAlert(element);
}
function validatePasswordField(element1, element2)
{
	clearAlert(element1);
	clearAlert(element2);
	if(isElementEmpty(element1))
		appendAlert(element1);
	else
	{
		if(!repeatPasswordCorrect(element1, element2))
		{
			appendAlert(element2);
		}
	}
}
function validateNonDefaultValue(element, defaultValue)
{
	clearAlert(element);
	if(isDefaultValue(element, defaultValue))
		appendAlert(element);
}
/***** Funciones Genéricas *****/

//Agrega un * como alerta
function appendAlert(element)
{
	var span=document.createElement('span');
	span.setAttribute("class", "validador");		
	span.appendChild(document.createTextNode("*"));
	element.parentNode.appendChild(span);
}
//Elimina el * de alerta si es que existe
function clearAlert(element)
{
	var lastElement = element.parentNode.lastChild;
	if(lastElement.nodeName == "SPAN")
	{
		element.parentNode.removeChild(lastElement);
	}
}
//Determina si el valor de un elemento esta vacío
function isElementEmpty(element) 
{
	if(element.value == "")
		return true;
	return false;
}
function isElementEmptyById(elementId)
{
	var el = document.getElementById(elementId);
	if(el.value == "")
		return true;
	return false;
}
//Varifica que el texto introducido sea un correo electronico (true) de lo contrario retorna 'false'
function isEmail(inputvalue){	
    var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    if(pattern.test(inputvalue))
		return true
	return false;
}
//Verifica que ambos campos de contraseña sean iguales
function repeatPasswordCorrect(elementPassword, elementPassword2)
{
	if(elementPassword.value == elementPassword2.value)
		return true
	return false;
}
//Verifica que el control no tenga el valor por defecto, valor corresponde al valor inicial del elemento
function isDefaultValue(element, valor)
{
	if(element.value == valor)
		return true;
	return false;
}
function isArray(obj) 
{
	return (obj.constructor.toString().indexOf("Array") != -1);
}