function valida(form, cssValido, cssInvalido)
{
	var retorno, retornoTemp, tipoValidacao, valor, obrigatorio, label;
	retorno = true;
	retornoTemp = false;
	
	for(var i=0; i < parseInt(form.length); i++)
	{
		if(form[i].getAttribute("id") != null)
		{
			if(form[i].getAttribute("tipoValidacao") != null)
				tipoValidacao = form[i].getAttribute("tipoValidacao");
				
			if(form[i].getAttribute("value") != "")
				valor = form[i].value;
				
			if(form[i].getAttribute("obrigatorio") != "")
				obrigatorio = form[i].getAttribute("obrigatorio");
				
			if(form[i].getAttribute("label") != "")
				label = form[i].getAttribute("label");
				
			if(tipoValidacao != null)
			{
				if((valor != "") && (valor != null))
				{
					retornoTemp = eval(ConverteTipoValidacao(tipoValidacao, valor));
				}
			}
			
			if((obrigatorio == "true") && (retornoTemp == false))
			{
				if(form[i].type == "select-one")
				{
					retornoTemp = validaSelect(form[i]);
				}
				else if((form[i].type == "text") || (form[i].type == "textarea"))
				{
					if(valor == null || valor == "")
						retornoTemp = true;
				}
				else if(form[i].type == "checkbox")
					retornoTemp = validateRadio(form[i]);
			}

			if(retornoTemp == true && retorno == true)
				retorno = false;
			
			if(retornoTemp == true)
			{
				if(label != null)
					document.getElementById(label).className = cssInvalido;
			}
			else
			{
				if(label != null)
					document.getElementById(label).className = cssValido;
			}			
		}
		
		label = null;
		obrigatorio = null;
		valor = null;
		tipoValidacao = null;
		retornoTemp = false;
	}

	return retorno;
}

function ConverteTipoValidacao(alias, obj)
{
	
	if(alias == "email")
		return validaEmail(obj);
	else if(alias == "numero")
		return validaNumerico;
		else if(alias == "select")
		return validaSelect;	
}

/**********************************************************************/
function validaEmail(formObj)
{			
	parte1 = formObj.indexOf("@");
    parte2 = formObj.indexOf(".");
    parte3 = formObj.length;
	
    if (!(parte1 >= 3 && parte2 >= 6 && parte3 >= 9))
	{
          return true;
    }
	else
	{
		return false;
	}
	
	/*var emailStr = formObj.value;
	var emailReg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
	var emailReg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$/; // valid
	if (!(!emailReg1.test(emailStr) && emailReg2.test(emailStr)))
	{		
		return true;
	}
	return false;*/
}

/**********************************************************************/
function validaNumerico(formObj, len)
{
	if (len == '*')
	{
		var regex = /^\d+$/;
		if (!regex.test(formObj.value))
		{
			return true;
		}
	}
	else
	{
		numReg = "^\\d{"+parseInt(len)+",}$"
		var regex = new RegExp(numReg);
		if (!regex.test(formObj.value))
		{
			return true;
		}
	}
	return false;
}

/**********************************************************************/
function validaSelect(formObj)
{
	if (formObj.selectedIndex == 0)
	{
		return true;
	}
	return false;
}

/**********************************************************************/
