jQuery(document).ready(function(){
	jQuery('a[rel=external]').attr('target','_blank');
	jQuery('#txt_search, #txt_nome, #txt_email').focus(function(){
		valor = jQuery(this).val();
		if(valor == 'o que você procura?' || valor == 'Seu nome' || valor == 'Seu e-mail'){
			jQuery(this).val('');
		};
	});
	jQuery('#txt_search, #txt_nome, #txt_email').blur(function(){
		valor = jQuery(this).val();
		id = jQuery(this).attr('id');
		if(valor == '' && id == 'txt_search'){
			jQuery(this).val('o que você procura?');
		}else if(valor == '' && id == 'txt_nome'){
			jQuery(this).val('Seu nome');
		}else if(valor == '' && id == 'txt_email'){
			jQuery(this).val('Seu e-mail');
		};
	});
	jQuery('.banner > ul').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 8000);
	jQuery("#slider").easySlider(
		{continuous: true}
	);
});

_send_newsletter = function(){
	var form = document.getElementById('frmNewsletter');
	
	if (form.txt_nome.value == "" || form.txt_nome.value == "Seu nome") {
		alert('Digite seu nome');
		form.txt_nome.focus();
		return false;
	}
	
	if (!validateEmail(form.txt_email.value)) {
		alert('Digite seu e-mail corretamente');
		form.txt_email.focus();
		return false;
	}
	
	return true;
};

_search = function(){
	var form = document.getElementById('frmBusca');
	
	if (form.txt_search.value == "" || form.txt_search.value == "o que você procura?") {
		alert('Digite uma palavra-chave');
		form.txt_search.focus();
		return false;
	}
	
	return true;
};

_search_2 = function(){
	var txt = document.getElementById('txt_busca2');
	
	if (txt.value == "") {
		alert('Digite uma palavra-chave');
		txt.focus();
		return false;
	}
	
	document.getElementById('frmBusca2').submit();
};

_send_contact = function(){
	var form = document.getElementById('frmContato');
	
	if (form.txt_fale_nome.value == "") {
		alert('Digite seu nome');
		form.txt_fale_nome.focus();
		return false;
	}
	
	if (!validateEmail(form.txt_fale_email.value)) {
		alert('Digite seu e-mail corretamente');
		form.txt_fale_email.focus();
		return false;
	}
	
	if (form.txt_fale_msg.value == "") {
		alert('Digite sua mensagem');
		form.txt_fale_msg.focus();
		return false;
	}
	
	return true;
};

/*remover string dentro de outro*/
function remove(str, strToRemove) {
   i = str.indexOf(strToRemove);
   r = "";
   if (i == -1) return str;
   r += str.substring(0,i) + remove(str.substring(i + strToRemove.length), strToRemove);
   return r;
}

/*validar email*/
function validateEmail(email) {
	var ret = false;
	var em = new String();
	em = String(email);
	var RegExPattern = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{ 1,3}){3}\])$/; 
	if ( em.match(RegExPattern) ) {
		ret = true;
	}
	return ret;
}

function trim(str){
	if (typeof(str) == "string") {
		return str.replace(/^\s+|\s+$/g,"");
	} else {
		return str;
	}	
}

/*maskFormat('NN-NN',this)*/
function maskFormat(mask,obj){
	if(obj.value != ""){
		sObj = obj.value.toString();
		nString = "";
		for(i=0;i<(sObj.length);i++){
			if(!isNaN(sObj.charAt(i)) && (sObj.charAt(i) != " ")) {
				nString = nString + sObj.charAt(i);
			}
		}
		j = 0;
		k = 0;
		mString = "";
		while(j < nString.length && k < mask.length){
			if(mask.charAt(k).toUpperCase() == "N"){
				mString = mString + nString.charAt(j);
				j = j+1;
			} else {
				mString = mString + mask.charAt(k);
			}
			k = k+1;
		}
		obj.value = mString;
	}
}
function validarCPF(cpf){
	var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
	if(!filtro.test(cpf)){
		return false;
	}
	cpf = remove(cpf, ".");
	cpf = remove(cpf, "-");
	if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
		cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
		cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
		cpf == "88888888888" || cpf == "99999999999"){
			return false;
	}
	soma = 0;
	for(i = 0; i < 9; i++)
		soma += parseInt(cpf.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if(resto == 10 || resto == 11)
		resto = 0;
	if(resto != parseInt(cpf.charAt(9))){
		return false;
	}
	soma = 0;
	for(i = 0; i < 10; i ++)
		soma += parseInt(cpf.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if(resto == 10 || resto == 11)
		resto = 0;
	if(resto != parseInt(cpf.charAt(10))){
		return false;
	}
	return true;
}

/*
EXEMPLO DE CHAMADA DE entraNoEdit E saiDoEdit
$('#apelido').blur(function() {
	saiDoEdit(this,'digite seu apelido');
});
$('#apelido').click(function() {
	entraNoEdit(this,'digite seu apelido');
});
$('#apelido').focus(function() {
	entraNoEdit(this,'digite seu apelido');
});
*/

function entraNoEdit(obj, txt) {
	if ($.trim(obj.value) == txt) {
		obj.value =	"";
	}
}
function saiDoEdit(obj,	txt) {
	if ($.trim(obj.value) == "") {
		obj.value =	txt;
	}
}

function getEndereco(cep) {
	var arrCep = new Array();
	var cepLimpo = $.trim(cep);
	cepLimpo = cepLimpo.replace("-","");
	if(cepLimpo != ""){
		$.getScript("http://cep.republicavirtual.com.br/web_cep.php?formato=javascript&cep=" + cepLimpo, function(){
			if(resultadoCEP["resultado"]){
				var txtLogradouro = ($.trim(unescape(resultadoCEP["tipo_logradouro"]) + " " + unescape(resultadoCEP["logradouro"])));
				var txtBairro = (unescape(resultadoCEP["bairro"]));
				var txtCidade = (unescape(resultadoCEP["cidade"]));
				var slEstado = (unescape(resultadoCEP["uf"]));
				arrCep.push(txtLogradouro);
				arrCep.push(txtBairro);
				arrCep.push(txtCidade);
				arrCep.push(slEstado);
			}
		});				
	}
	return arrCep;
}

/*
MODELO DE VALIDA SALVAR
function modeloValidaSalvar() {
	var msgErro = document.getElementById("msgErro");
	var countErro = 0;
	var countOk = 0;
	
	var txtNome = document.getElementById("txtNome");
	var divNome = document.getElementById("divNome");
	if (txtNome.value == "") {
		divNome.className = "bgL erro";
		countErro++;
	} else {
		divNome.className = "bgL";
		countOk++;
	}
		
	var txtCpf = document.getElementById("txtCpf");
	var divCpf = document.getElementById("divCpf");
	if (txtCpf.value == "") {
		divCpf.className = "bgL erro";
		countErro++;
	} else {
		divCpf.className = "bgL";
		countOk++;
	}
		
	if (countErro > 0) {
		msgErro.innerHTML = "Campos inv�lidos!";
		msgErro.style.display = "block";
		document.location.href = '#msgErro';
	} else {
		document.getElementById("frmCadastro").submit();
	}
}
*/

/*
MODELO DE CHAMADA DE AJAX
var dataNasc = {
	dia: txtDiaNasc.value,
	mes: txtMesNasc.value,
	ano: txtAnoNasc.value
};
$.ajax({type: "POST",url: "verificaDataNasc.asp", data:dataNasc, dataType: "xml", async:false, success: function(xml) {
	if(xml.getElementsByTagName("return")[0].firstChild.nodeValue == "0") {
		divDiaNasc.className = "bgL erro";
		divMesNasc.className = "bgL erro";
		divAnoNasc.className = "bgL erro";
		countErro++;
	} else {
		dataMinima = new Date(1996,4,10);
		data2 = new Date(parseInt(txtAnoNasc.value),parseInt(txtMesNasc.value)-1,parseInt(txtDiaNasc.value));
		if (data2 > dataMinima) {
			divDiaNasc.className = "bgL erro";
			divMesNasc.className = "bgL erro";
			divAnoNasc.className = "bgL erro";
			countErro++;
			msgErro.innerHTML = "Menores de 14 anos n�o poder�o se increver!";
		} else {
			divDiaNasc.className = "bgL";
			divMesNasc.className = "bgL";
			divAnoNasc.className = "bgL";
		}
	}

}});
*/
