$(document).ready(function(){
	$('a[rel=external]').attr('target','_blank');
	$('.newsletter input[type=text], #sidebar input[type=text], .search-parts2 input[type=text]').bind('focus', function (){
		var elem = $(this);
		var texto = elem.val();
		elem.val('');
	});
	$('#btnbusca, .btnbusca label ').click(function() {
		var inppeca = $('input[name=peca]');	
		if(inppeca.val() === 'Nome do produto'){
			inppeca.val('')
		}
	});
});

function buscar() {
	if ($('select[name=marca]').val() !== '') {
		$('.formprodutos').submit();
	} else {
		alert('O campo "Marcas" &eacute; obrigat&oacute;rio');	
	}
}
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;
}

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;
	}	
}

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;
}

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;
}

function validateUrl(txt) {
	var urlRegxp = /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$/;
	if ((txt.match(urlRegxp)) && (trim(txt!=''))) {
		return true;
	} else {
		return false;
	}
}
/*
function setGoogleAnalitics(tag) {
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
	try {
		var pageTracker = _gat._getTracker(tag);
		pageTracker._trackPageview();
	} catch(err) {}
}
*/
