function intval (mixed_var, base) {
    // Get the integer value of a variable using the optional base for the conversion
    //
    // version: 910.813
    // discuss at: http://phpjs.org/functions/intval
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: stensi
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Matteo
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: intval('Kevin van Zonneveld');
    // *     returns 1: 0
    // *     example 2: intval(4.2);
    // *     returns 2: 4
    // *     example 3: intval(42, 8);
    // *     returns 3: 42
    // *     example 4: intval('09');
    // *     returns 4: 9
    // *     example 5: intval('1e', 16);
    // *     returns 5: 30
    var tmp;

    var type = typeof( mixed_var );

    if (type === 'boolean') {
        return (mixed_var) ? 1 : 0;
    } else if (type === 'string') {
        tmp = parseInt(mixed_var, base || 10);
        return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
    } else if (type === 'number' && isFinite(mixed_var) ) {
        return Math.floor(mixed_var);
    } else {
        return 0;
    }
}

function inserirLegendaFoto(idImg, idLegenda){
	var legenda = $('#'+idLegenda);
	
	if(!legenda)
		return false;

	var img = $('#'+idImg);
	var pimg = img.position();
	
	legenda.hide();
	legenda.css({
		//position : 'absolute',
		//width: (img.width()-5)+'px',
		//top: (pimg.top + img.height() + 20 - legenda.height())+'px',
		//left: (pimg.left)+'px'
	});
	
	legenda.show('slow');
	return true;
}

function abre(id){
		$("#"+id).fadeIn(1500);		
	}
	
function fecha(id){
		$("#"+id).hide();
}

function trocaClass(id,classe){

	var nom = navigator.appName;  
	if (nom == "Microsoft Internet Explorer"){  
		document.getElementById(id).setAttribute("className",classe);
	}else if (nom == "Netscape"){  
		document.getElementById(id).setAttribute("class",classe);
	}  
}	
	
function alteraValor(id,valor,tipo){
	
	
	var div = document.getElementById(id);

	if(tipo == 1) {
		if (div.value == valor) {
			div.value = "";
		}
	} else {
		if (div.value==""){
			div.value = valor;
		}
	}
	
}

function val_cpf(numero2) {
	dig_1 = 0;
	dig_2 = 0;
	controle_1 = 10;
	controle_2 = 11;
	lsucesso = 1;

	var numero = numero2.substr(0,3) + numero2.substr(4,3) + numero2.substr(8,3) + numero2.substr(12,2);

	if (numero == "00000000000" || numero == "11111111111" || numero == "22222222222" || numero == "33333333333" || numero == "44444444444" || numero == "55555555555" || numero == "66666666666" || numero == "77777777777" || numero == "88888888888" || numero == "99999999999") {
		return false;
	}

	if ((numero.length != 11) && (numero.length != 0)) {
		return false;
	}
	else {
		for ( i=0 ; i < 9 ; i++) {
			dig_1 = dig_1 + parseInt(numero.substring(i, i+1) * controle_1);
			controle_1 = controle_1 - 1;
		}

		resto = dig_1 % 11;
		dig_1 = 11 - resto;
		if ((resto == 0) || (resto == 1)) {
			dig_1 = 0;
		}
		for ( i=0 ; i < 9 ; i++) {
			dig_2 = dig_2 + parseInt(numero.substring(i, i + 1) * controle_2);
			controle_2 = controle_2 - 1;
		}
		dig_2 = dig_2 + 2 * dig_1;
		resto = dig_2 % 11;
		dig_2 = 11 - resto;
		if ((resto == 0) || (resto == 1)) {
			dig_2 = 0;
		}
		dig_ver = (dig_1 * 10) + dig_2;
		if (dig_ver != parseFloat(numero.substring(numero.length-2,numero.length))) {
			return false;
		}
	}

	return true;
}

function valida_cnpj (numero,form) {
	if(numero.length>=18){
		if (!val_cnpj(numero)){
			document.getElementById('erroCNPJ').innerHTML='<br>CNPJ incorreto';		
				
			return false;
		} else {
			document.getElementById('erroCNPJ').innerHTML=''
			confirmarDadosForm(form);
		}
	} else {
		return false;
	}
}

function val_cnpj(numero) {
	//numero = numero1.value;
	dig_1 = 0;
	dig_2 = 0;
	controle_1 = 5;
	controle_2 = 6;
   	
if ( (numero.length != 18)  || (numero.substring(2, 3) != ".") || (numero.substring(6, 7) != ".") || (numero.substring(10, 11) != "/") || (numero.substring(15, 16) != "-") ) {
	return false;
}else{ 
  	   numero = (numero.substring(0, 2)) + "" + (numero.substring(3, 6)) + "" + (numero.substring(7, 10))+ "" + (numero.substring(11, 15)) + "" + (numero.substring(16, 18)) 
	   for ( i=0 ; i < 12 ; i++) {
	        dig_1 = dig_1 + parseFloat(numero.substring(i, i+1) * controle_1);
	          controle_1 = controle_1 - 1;
	        if (i == 3) {
	           controle_1 = 9;
	        }
	   }
	   
	   resto = dig_1 % 11;
	   dig_1 = 11 - resto;
	   
	   if ((resto == 0) || (resto == 1)){
	        dig_1 = 0;
	   }
	   
	   for ( i=0 ; i < 12 ; i++) {
	        dig_2 = dig_2 + parseInt(numero.substring(i, i+1) * controle_2);
	          controle_2 = controle_2 - 1;
	        if (i == 4) {
	           controle_2 = 9;
	        }
	   }
	   
	   dig_2 = dig_2 + (2 * dig_1);
	   resto = dig_2 %11;
	   dig_2 = 11 - resto;
	   
	   if ((resto == 0) || (resto == 1)){
	        dig_2 = 0;
	   }
	   
	   dig_ver = (dig_1 * 10) + dig_2;
	   
	   if (dig_ver != parseFloat(numero.substring(numero.length-2,numero.length))) {
	   		  
	          return false;
	   }
 }
 
 return true;
}


function verifica_mail(email){
	var campo = email;
	//se a "@" não existe e é o primeiro caractere
	if (campo.indexOf("@") < 1) return false
	//se o caracter antes da "@" é "."
	if(campo.charAt(campo.indexOf("@")-1) == ".") return false;
	//se a última incidência de "." está antes da @
	if (campo.lastIndexOf(".") <= campo.indexOf("@")) return false;
	//se o último caracter é ponto,
	if (campo.lastIndexOf(".")  == (campo.length-1)) return false;

	return true;
}


function trocaImagem(img,txt){
	parent.document.getElementById("imgGalGrande").src = img;
	parent.document.getElementById('Legenda').innerHTML = txt;
}

function formatar(src, mask)
{
  var i = src.value.length;
  var saida = mask.substring(0,1);
  var texto = mask.substring(i)
if (texto.substring(0,1) != saida)
  {
	src.value += texto.substring(0,1);
  }
}

//
var myTime;
function scrollUp(){
	window.fotosPequenas.scrollBy(0,-5);//sobe a barra de rolagem
	myTime = setTimeout('scrollUp()', 5);//myTime objeto q mantém a recursividade
}

function scrollDown(){
	window.fotosPequenas.scrollBy(0,5);//desce a barra de rolagem
	myTime = setTimeout('scrollDown()', 5);//myTime objeto q mantém a recursividade
}

var myTime2;
function scrollEsquerda(){
	window.fotosPequenas.scrollBy(-5,0);//rola para a esquerda a barra de rolagem
	myTime2 = setTimeout('scrollEsquerda()', 5);//myTime objeto q mantém a recursividade
}

function scrollRight(){
	window.fotosPequenas.scrollBy(5,0);//rola para a direita a barra de rolagem
	myTime2 = setTimeout('scrollRight()', 5);//myTime objeto q mantém a recursividade
}


//executa funções quando o documento estiver pronto
$(document).ready(function(){
	
	$(".mascaraData").mask("99/99/9999", {placeholder:" "});
	$(".mascaraHora").mask("99:99", {placeholder:" "});
	$(".mascaraHoraCompleta").mask("99:99:99", {placeholder:" "});
	$(".mascaraTelefone").mask("(99)9999-9999", {placeholder:" "});
	$(".mascaraCPF").mask("999.999.999-99", {placeholder:" "});
	$(".mascaraCNPJ").mask("99.999.999/9999-99", {placeholder:" "});
	$(".mascaraCEP").mask("99999-999", {placeholder:" "});
	$(".marcaraFloat").maskMoney({symbol:"",decimal:",",thousands:"."});
	$(".mascaraDinheiro").maskMoney({symbol:"R$",decimal:",",thousands:"."});
	$(".mascaraFloatPrecisao1").maskMoney({symbol:"",decimal:",",thousands:"",precision:1});
	$(".mascaraFloatPrecisao2").maskMoney({symbol:"",decimal:",",thousands:"",precision:2});
	$(".mascaraFloatPrecisao3").maskMoney({symbol:"",decimal:",",thousands:"",precision:3});
	$(".mascaraFloatPrecisao4").maskMoney({symbol:"",decimal:",",thousands:"",precision:4});
	$(".mascaraFloatPrecisao5").maskMoney({symbol:"",decimal:",",thousands:"",precision:5});
	$(".mascaraFloatPrecisao6").maskMoney({symbol:"",decimal:",",thousands:"",precision:6});

});


//confirmar dados do formulário
function confirmarDadosForm(form){
	if (form == '') { form = 'form'; }
	
	//checa a validação
	var validado = $("#"+form).valid();
	
	if ( validado == true ) {
		$("#"+form).submit();		
	}
}

//confirmar dados do formulário
function confirmarDadosFormAjax(caminho,form){
	if (form == '') { form = 'form'; }
	
	//checa a validação
	var validado = $("#"+form).valid();
	
	if ( validado == true ) {
		_getAjax(caminho,form);
	}
}


function popup(url){

	params = 'width='+screen.width;
	params += ', height='+screen.height;
	params += ', top=0, left=0'
	params += ', fullscreen=yes';

	newwin=window.open(url,'windowname4', params);

	if (window.focus) {newwin.focus()}

	return false;
}

