$(function(){
	//CRIA TITULO
	var titulo01 = $('h3.titulo01');
	var titulo02 = $('h3.titulo02');
	var titulo03 = $('h3.titulo03');
	var titulo1 = new Titulo( titulo01[0], titulo01.html(), 'b7b8b7', "18", "false", "none", 350, 23, '' );
	var titulo2 = new Titulo( titulo02[0], titulo02.html(), 'b7b8b7', "18", "false", "none", 350, 23, '' );
	var titulo3 = new Titulo( titulo03[0], titulo03.html(), 'b7b8b7', "18", "false", "none", 350, 23, '' );

	//CRIA TITULO PRESSTRIP
	var titulo04 = $('body.pressTrip h3.titulo04');
	var titulo05 = $('body.pressTrip h3.titulo05');	
	var titulo4 = new Titulo( titulo04[0], titulo04.html(), 'b7b8b7', "18", "false", "none", 350, 23, '' );
	var titulo5 = new Titulo( titulo05[0], titulo05.html(), 'b7b8b7', "18", "false", "none", 350, 23, '' );

	$('a.btAdicionar').click(function(){
		var ddd = $('.telArea');
		var pai = ddd.parent();
		
		pai.css('height', 'auto').append('<input class="texto telArea ddd" type="text" name="ddd'+(ddd.length + 1)+'"/>&nbsp;<input class="texto telNumero numero" type="text" name="telefone'+(ddd.length + 1)+'"/>');
		return false;
	});

	$('body:not(.pressTrip) div.btEnviar button.btEnviar').click(function(){
		validaCadastro(".obrigatorio");
		return false;
	});


	$('body.pressTrip div.btEnviar a.btEnviar').click(function(){
		//validaCadastro("#formPresstrip input:not([@name=outraDistribuicao]):not([@name=outraPeriodicidade]):not([@name=outroSegmento]), #formPresstrip select, #formPresstrip textarea");
		validaCadastro("#formPresstrip input, #formPresstrip select, #formPresstrip textarea");
		return false;
	});

	
	//DESMARCA CAMPO
	$("#formPresstrip input, #formPresstrip textarea, #formPresstrip select").focus(function(){
	    $(this).css("background","#fff");
	});	

	$("select[@name^='segmento']").mousedown(function(){
        	if($.browser.msie) {
            		$(this).css("width","auto");
	        }
	});
	$("select[@name^='segmento']").change(function(){
        	if ($.browser.msie) {
	            $(this).css("width", '166px');
        	}
	});
	$("select[@name^='segmento']").blur(function(){
        	if ($.browser.msie) {
	            $(this).css("width", '166px');
        	}
	});



});
$(function(){
   $(".data").mask("99/99/9999", {placeholder:""});
   //$(".numero").mask("99999999",{placeholder:""});
   $(".ddd").mask("999",{placeholder:""});
});


//validaCadastro(<seletor jquery ou objeto à virar um objeto jquery>);

function validaCadastro(selector){
	elems = $(selector);
	var el=null, prob={itens: "", alertas: "", email: "", usuario: ""}, frases = $("#frasesValidaForm");
	for(var i=0; i < elems.length; i++){
		el = elems[i];
		switch(el.tagName){
			case 'TEXTAREA':
			case 'INPUT':
				if(el.value == ""){
					prob.itens += $(el).prevAll("label").text().replace(/(:)|(\*)/gi, "");
					$(el).css("background","#f8acac");
				} else if((el.className.indexOf("campoEmail") != -1) && (el.value.indexOf("@") == -1 || el.value.indexOf(".") == -1) ){
					prob.email = " "+frases.find("option[@value=email]").text();
					$(el).css("background","#f8acac");
				} else if(el.className.indexOf("campoEmail") != -1 && el.className.indexOf("naoValida") == -1){
					$.ajax({
						url: "../../system/modules/br.com.embratur.brazilnetwork/persistencia/validaEmail.jsp",
						data: "email="+el.value,
						async: false,
						success: function(resp){
							//console.log(resp);
							resp = resp.replace(/(\n)|(\r)/gi, "");
							if(resp == "Email ja cadastrado"){
								prob.usuario = " "+frases.find("option[@value=usuario]").text();
							}
						}});
				} else if(el.name == "senha" && el.value.length < 6){
					prob.alertas += " "+frases.find("option[@value=senha]").text();
				} else if(el.name == "confirmaSenha" && el.value != $(el).parents("form").find("input[@name=senha]").val() ){
					prob.alertas += " "+frases.find("option[@value=confirmaSenha]").text();
				}
			break;
			case 'SELECT':
				if(el.selectedIndex == 0){
					prob.itens += $(el).prevAll("label").text().replace(/:/gi, "");
					$(el).css("background","#f8acac");
				}
			break;
		}
	}
	if(prob.itens.length > 0 || prob.alertas.length > 0 || prob.email.length > 0 || prob.usuario.length > 0){
		var msg = "";
		if(prob.itens.length > 0){
			msg += frases.find("option[@value=obrigatorio]").text();
		}
		msg += prob.alertas + prob.email + prob.usuario;
		$("div#aviso").text(msg).show();
		scrollTo(0,0);
	} else {
		elems.parents("form").submit();
	}
}


