// RETIRA OS ESPAÇOS DO INÍCIO E DO FIM DA STRING
function Trim(str) {
	while (str.charAt(0) == " ") {
		str = str.substr(1,str.length -1);
	}

	while (str.charAt(str.length-1) == " ") {
		str = str.substr(0,str.length-1);
	}

	return str;
}

// VERIFICAÇÃO DE EMAIL VÁLIDO
function checkMail(mail) {
	var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	if (typeof(mail) == "string") {
		if (er.test(mail)) {
			return true;
		}
	}
	else if (typeof(mail) == "object") {
		if (er.test(mail.value)) {
			return true;
		}
	}
	else {
		return false;
	}
}

// ADICIONA EVENTOS ONLOAD
function addLoadEvent(func) {
	var oldonload = window.onload;
	
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			
			func();
		}
	}
}

// ADICIONA A TAG TARGET="_BLANK" PARA LINKS QUE ABREM EM NOVA JANELA
function createExternalLinks() {
    if (document.getElementsByTagName) {
        var anchors = document.getElementsByTagName("a");
        for (var i=0; i<anchors.length; i++) {
            var anchor = anchors[i];
            if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") { // <-- É necessário inserir rel="externo" no link
                anchor.target = "_blank";
                //var title = anchor.title + ' (Este link abre uma nova janela)'; // <-- Insere este texto no final do Title do link
                //anchor.title = Trim(title);
            }
        }
    }
}

function tratarFoco(obj, txt) {
	if (obj.value == txt) {
		obj.value = '';
		obj.style.color = '#000000';
	} else if (obj.value == '') {
		obj.value = txt;
		obj.style.color = '#999999';
	}
}

function validarContato() {
	var nome    = $('#nome');
	var email   = $('#email');
	var assunto = $('#assunto');
	var codigo  = $('#codigo');
	var msg     = $('#msg');
	
	if (Trim(nome.val()) == '') {
		alert('Informe o seu NOME completo!');
		nome.focus();
		return false;
	} else if (!checkMail(email.val())) {
		alert('Informe um endereço de E-MAIL válido!');
		email.focus();
		return false;
	} else if (Trim(assunto.val()) == '') {
		alert('Informe o ASSUNTO do seu contato!');
		assunto.focus();
		return false;
	} else if (Trim(codigo.val()) == '') {
		alert('Informe o CÓDIGO DE SEGURANÇA!');
		codigo.focus();
		return false;
	} else if (Trim(msg.val()) == '') {
		alert('Digite a sua MENSAGEM!');
		msg.focus();
		return false;
	} else {
		return true;
	}
}

addLoadEvent(createExternalLinks);
