function trim(str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}

function is_visible(element){
	if (element.style != null && element.style.display == 'none')
		return false

	if (element.parentNode == null)
		return true
	
	return is_visible(element.parentNode)
}

function validar(form){
	num_erros = 0

	var first = null
	
	regex_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	regex_date = /^(19|20)[0-9][0-9][- \/.](0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])$/

	for(i = 0; i < form.elements.length; i++)
		if (is_visible(element = form.elements[i]))
			if (element.name != undefined && element.name.length > 0)
				if (element.previousSibling != undefined)
					for(child = element.previousSibling; child != undefined; child = child.previousSibling)
						if (child.tagName == "LABEL"){
							if (child.firstChild != undefined)
								for(great_child = child.firstChild; great_child != undefined; great_child = great_child.nextSibling)
									if (great_child.tagName == "SPAN")
										if (trim(great_child.firstChild.data) == '*'){
											for(outro = great_child.nextSibling; (outro != null) && (outro != undefined); outro = outro.nextSibling)
												child.removeChild(outro)
											if (trim(element.value) == '') {
												span = document.createElement("SPAN")
												span.className = "erroSmall"
												child.appendChild(span)
												
												text = document.createTextNode("obrigatório")
												span.appendChild(text)
												
												num_erros++
												if (first == null)
													first = element
											}
										}
						} else if (child.tagName == "SPAN"){
							if(child.firstChild == undefined)
								child.appendChild(document.createTextNode(""))
							
							text = child.firstChild
							
							if (element.className == "email")
								if (trim(element.value).length > 0)
									if (regex_email.test(trim(element.value)))
										text.data = ""
									else{
										child.className = "erroSmall"
										text.data = "Formato incorrecto"
										num_erros++
										if (first == null)
											first = element
									}
								else
									child.firstChild.data = ""
							else if (element.className == "date")
							{
								if (trim(element.value).length > 0)
									if (regex_date.test(trim(element.value)))
										text.data = ""
									else{										
										child.className = "erroSmall"
										text.data = "Formato incorrecto"
										num_erros++
										if (first == null)
											first = element
									}
								else
									span.firstChild.data = ""
							}
						}

	if (num_erros){
		message = document.getElementById("p_erro")
		message.style.display = "block"
		
		if(message.firstChild == undefined)
			message.appendChild(document.createTextNode(""))
			
		message.firstChild.data = "Foram encontrados " + num_erros + " erro(s)."
		first.focus();
		
		return false		
	}
	
	return true
}
