//-----POPUPS E PROMPTS-----

function confirma(txt) {
    return confirm(txt);
}

function confirma_exclusao() {
    return confirm('Deseja realmente excluir estes registros?');
}

function popup(mypage, myname, w, h, scroll) {
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',noresizable'
    win = window.open(mypage, myname, winprops)
    if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function detalhes(pagina){
    popup(pagina,'Detalhes','520','520','yes');
}

function mapa(arquivo,tipo){
    popup('../ref/popup_mapa.aspx?mapa='+arquivo+'&tipo='+tipo,'Mapa','800','600','no');
}

function imagens(id,tipo){
    popup('popup_imagens.aspx?id='+id+'&tipo='+tipo,'Imagens','800','600','yes');
}

function video(id,tipo){
    popup('popup_video.aspx?id='+id+'&tipo='+tipo,'Video','450','450','no');
}

function manutencoes(id_area){
    popup('popup_manutencoes.aspx?id_area='+id_area,'Manutencoes','800','600','yes');
}

function especies(id_area){
    popup('popup_especies.aspx?id_area='+id_area,'Especies','800','600','yes');
}

function tabela_tecnicas(tec){
    popup('popup_tabela_tecnicas.aspx?ver='+tec,'TabelaTratametos','690','650','no');
}

function na_pratica(){
    popup('popup_na_pratica.aspx','Calculadoras','800','400','no');
}

function madeira(id_madeira){
    popup('popup_madeiras.aspx?id_madeira='+id_madeira,'Madeira','500','600','yes');
}

//function boleto(){
//    popup('','pop_boleto','780','580','yes');
//}

//function visa(){
//    popup('','mpg_popup','780','580','yes');
//}

//function master(){
//    popup('','pop_master','780','580','yes');
//}

//-----FORMATACOES-----

function fnum(campo, e) {
	var digito;
	if(window.event) { digito = e.keyCode; } // IE
	else if(e.which) { digito = e.which;   } // Netscape/Firefox/Opera

	if ((digito < 48 || digito > 57) && digito!= 8 && digito!= 9 && digito!= 0) { //Permite backspace, del e setas
    	return false;
    }
}

function fdec(campo, e) {
	var digito;
	if(window.event) { digito = e.keyCode; } // IE
	else if(e.which) { digito = e.which;   } // Netscape/Firefox/Opera
	
	if (digito==44 && (campo.value=='' || campo.value.indexOf(',',1)>=0)) { //Não permite duas virgulas nem virgula na primeira casa
    	return false;
    }
    
    if ((digito < 48 || digito > 57) && digito!= 8 && digito!= 9 && digito!= 0 && digito!= 44) { //Permite backspace, del, setas e virgula
    	return false;
    }
	
	return true;
}

function fdata(campo,e) {

	var digito;
	if(window.event) { digito = e.keyCode; } // IE
	else if(e.which) { digito = e.which;   } // Netscape/Firefox/Opera
	
    if ((digito < 48 || digito > 57) && digito!= 8 && digito!= 9 && digito!= 0) { //Permite backspace, del e setas
    	return false;
    }
    
    if (campo.value.length >= 10) { //Permite só 10 digitos
        campo.value = '';
    }
	
	valor = campo.value;
    valor = valor.replace(/\//g,'');
    tamanho = valor.length + 1;

	if ( tamanho > 2 && tamanho <= 4 )
	{
		campo.value = valor.substr( 0, tamanho - 2  ) + '/' + valor.substr( tamanho - 2, tamanho );
	}

	if ( tamanho >= 5 && tamanho <= 10 )
	{
		campo.value = valor.substr( 0, 2 ) + '/' + valor.substr( 2, 2 ) + '/' + valor.substr( 4, 4 );
	} 
	
	return true;
}

function fcep(campo,e) {

    if (campo.value.length >= 9) { //Permite só 9 digitos
    	return false;
    }
    
	var digito;
	if(window.event) { digito = e.keyCode; } // IE
	else if(e.which) { digito = e.which;   } // Netscape/Firefox/Opera
	
    if ((digito < 48 || digito > 57) && digito!= 8 && digito!= 9 && digito!= 0) { //Permite backspace, del e setas
    	return false;
    }
	
	valor = campo.value;
	valor = valor.replace(/\-/g,'');
	tamanho = valor.length + 1;

	if ( tamanho > 5 && tamanho < 9 )
	{
		campo.value = valor.substr( 0, tamanho - 3  ) + '-' + valor.substr( tamanho - 3, tamanho );
	}
	
	return true;
}

function ftel2(campo,e) {
    
	var digito;
	if(window.event) { digito = e.keyCode; } // IE
	else if(e.which) { digito = e.which;   } // Netscape/Firefox/Opera
	
    if ((digito < 48 || digito > 57) && digito!= 8 && digito!= 9 && digito!= 0) { //Permite números, backspace, del e setas
    	return false;
    }
    
    if (campo.value.length >= 14 && digito!= 8) { //Permite num max digitos e repete backspace
    	return false;
    }
	
	valor = campo.value;
    valor = valor.replace(/\D/g,"");               
    valor = valor.replace(/^(\d\d)(\d)/g,"($1) $2");
    valor = valor.replace(/(\d{4})(\d)/,"$1-$2");
    campo.value = valor;
}

function ftel(campo,e) {

    if (campo.value.length >= 9) { //Permi só 9 digitos
    	return false;
    }
    
	var digito;
	if(window.event) { digito = e.keyCode; } // IE
	else if(e.which) { digito = e.which;   } // Netscape/Firefox/Opera
	
    if ((digito < 48 || digito > 57) && digito!= 8 && digito!= 9 && digito!= 0) { //Permite backspace, del e setas
    	return false;
    }
	
	valor = campo.value;
	valor = valor.replace(/\-/g,'');
	tamanho = valor.length + 1;

	if ( tamanho > 4 && tamanho < 9 )
	{
		campo.value = valor.substr( 0, tamanho - 4  ) + '-' + valor.substr( tamanho - 4, tamanho );
	}
	
	return true;
}

function fddd(campo,e) {

    if (campo.value.length >= 2) { //Permi só 2 digitos
    	return false;
    }
    
	var digito;
	if(window.event) { digito = e.keyCode; } // IE
	else if(e.which) { digito = e.which;   } // Netscape/Firefox/Opera
	
    if ((digito < 48 || digito > 57) && digito!= 8 && digito!= 9 && digito!= 0) { //Permite backspace, del e setas
    	return false;
    }
	
	valor = campo.value;
	valor = valor.replace(/\-/g,'');
	tamanho = valor.length + 1;
	
	return true;
}

function fcpf(campo,e) {

    if (campo.value.length >= 14) { //Permite só 14 digitos
    	return false;
    }
    
	var digito;
	if(window.event) { digito = e.keyCode; } // IE
	else if(e.which) { digito = e.which;   } // Netscape/Firefox/Opera
	
    if ((digito < 48 || digito > 57) && digito!= 8 && digito!= 9 && digito!= 0) { //Permite backspace, del e setas
    	return false;
    }
	
	valor = campo.value;
	valor = valor.replace(/\./g,'');
	valor = valor.replace(/\-/g,'');
	tamanho = valor.length + 1;

	if ( tamanho > 2 && tamanho < 7 )
	{
		campo.value = valor.substr( 0, tamanho - 3  ) + '.' + valor.substr( tamanho - 3, tamanho );
	}

	if ( tamanho >= 7 && tamanho <= 10 )
	{
		campo.value = valor.substr( 0, 3 ) + '.' + valor.substr( 3, 3 ) + '.' + valor.substr( 6, 3 );
	} 
	
	if ( tamanho >= 10 && tamanho <= 11 )
	{
		campo.value = valor.substr( 0, 3 ) + '.' + valor.substr( 3, 3 ) + '.' + valor.substr( 6, 3 ) + '-' + valor.substr(9,2);
	} 
	
	return true;
}

function fcnpj(campo,e) {

    if (campo.value.length >= 18) { //Permi só 18 digitos
    	return false;
    }
    
	var digito;
	if(window.event) { digito = e.keyCode; } // IE
	else if(e.which) { digito = e.which;   } // Netscape/Firefox/Opera
	
    if ((digito < 48 || digito > 57) && digito!= 8 && digito!= 9 && digito!= 0) {  //Permite backspace, del e setas
    	return false;
    }
	
	valor = campo.value;
	valor = valor.replace(/\./g,'');
	valor = valor.replace(/\-/g,'');
	valor = valor.replace(/\//g,'');
	tamanho = valor.length + 1;

	if ( tamanho > 2 && tamanho < 6 )
	{
		campo.value = valor.substr( 0, tamanho - 2  ) + '.' + valor.substr( tamanho - 2, tamanho );
	}

	if ( tamanho >= 6 && tamanho <= 10 )
	{
		campo.value = valor.substr( 0, 2 ) + '.' + valor.substr( 2, 3 ) + '.' + valor.substr( 5, 3 );
	} 
	if ( tamanho >= 9 && tamanho <= 13 )
	{
		campo.value = valor.substr( 0, 2 ) + '.' + valor.substr( 2, 3 ) + '.' + valor.substr( 5, 3 ) + '/' + valor.substr(8,4);
	} 
	if ( tamanho >= 13 && tamanho <= 14 )
	{
		campo.value = valor.substr( 0, 2 ) + '.' + valor.substr( 2, 3 ) + '.' + valor.substr( 5, 3 ) + '/' + valor.substr(8,4) + '-'+ valor.substr(12,2);
	} 
	
	return true;
}

function fmax(campo,maximo) {
    if (campo.value.length >= maximo) {
        return false;
    }
}

//-----VALIDACOES-----

function vnum(campo) {
	if (campo.value!='') {
	var expReg = /^([0-9]{1,})$/;
		if (!campo.value.match(expReg)) {
			alert("Utilize apenas números.");
			campo.value='';
			campo.focus();
			return false;
		}
	}
}

function vdec(campo) {
	if (campo.value!='') {
	var expReg = /^([0-9]{1,})*[,]*([0-9]{1,2})/; //duas casas decimais
		if (!campo.value.match(expReg)) {
			alert("Utilize apenas números decimais.");
			campo.value='';
			campo.focus();
			return false;
		}
	}
}

function vdata(campo) {
	if (campo.value!='') {
	var expReg = /^(((0[1-9]|[12][0-9]|3[01])([/])(0[13578]|10|12)([/])(\d{4}))|(([0][1-9]|[12][0-9]|30)([/])(0[469]|11)([/])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([/])(02)([/])(\d{4}))|((29)(\.|-|\/)(02)([/])([02468][048]00))|((29)([/])(02)([/])([13579][26]00))|((29)([/])(02)([/])([0-9][0-9][0][48]))|((29)([/])(02)([/])([0-9][0-9][2468][048]))|((29)([/])(02)([/])([0-9][0-9][13579][26])))$/;
	    if (!campo.value.match(expReg)) {
		    alert("Data inválida. Tente novamente");
		    campo.value='';
		    campo.focus();
		    return false;
	    }
	}
}

function vcep(campo) {
	if (campo.value!='') {
	var expReg = /^([0-9]{5,5})[-]([0-9]{3,3})$/;
		if (!campo.value.match(expReg)) {
			alert("CEP com formato incorreto. Tente novamente.");
			campo.value='';
			campo.focus();
			return false;
		}
	}
}

function vtel2(campo) {
	if (campo.value!='') {
		if (campo.value.length!=14) {
			alert("Telefone com formato incorreto. Tente novamente.");
			campo.value='';
			campo.focus();
			return false;
		}
	}
}

function vtel(campo) {
	if (campo.value!='') {
	var expReg = /^([0-9]{4,4})[-]([0-9]{4,4})$/;
		if (!campo.value.match(expReg)) {
			alert("Telefone com formato incorreto. Tente novamente.");
			campo.value='';
			campo.focus();
			return false;
		}
	}
}

function vcpf(campo) {
	if (campo.value!='') {
		cpf = campo.value;
		cpf = cpf.replace(/\./g,'')
		cpf = cpf.replace(/\-/g,'')
		erro = new String;
		if (cpf.length < 11) { erro = "CPF inválido. Tente novamente." }
		var nonNumbers = /\D/;
		if (nonNumbers.test(cpf)) { erro = "CPF inválido. Tente novamente." }
		if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999") { erro = "CPF inválido. Tente novamente." }
		var a = [];
		var b = new Number;
		var c = 11;
		for (i=0; i<11; i++){
		a[i] = cpf.charAt(i);
		if (i < 9) b += (a[i] * --c);
		}
		if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
		b = 0;
		c = 11;
		for (y=0; y<10; y++) b += (a[y] * c--); 
		if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
		if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
			   erro = "CPF inválido. Tente novamente.";
		}
		if (erro.length > 0){
			   alert(erro);
			   campo.value='';
			   campo.focus();
			   return false;
		}
		return true;
	}
}

function vcnpj(campo) {
	if (campo.value!='') {
	CNPJ = campo.value;
	CNPJ = CNPJ.replace(/\./g,'')
	CNPJ = CNPJ.replace(/\-/g,'')
	CNPJ = CNPJ.replace(/\//g,'')
	erro = new String;
	if (CNPJ.length < 14) { erro = "CNPJ inválido. Tente novamente." }
	var nonNumbers = /\D/;
	if (nonNumbers.test(CNPJ)) { erro = "CNPJ inválido. Tente novamente." }
	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	for (i=0; i<12; i++) {
		   a[i] = CNPJ.charAt(i);
		   b += a[i] * c[i+1];
	}
	if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
	b = 0;
	for (y=0; y<13; y++) {
		   b += (a[y] * c[y]); 
	}
	if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
	if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
		   erro = "CNPJ inválido. Tente novamente.";
	}
	if (erro.length > 0){
		   alert(erro);
		   campo.value='';
		   campo.focus();
		   return false;
	}
	return true;
  }
}

function vemail(campo) {
	if (campo.value!='') {
	var expReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!campo.value.match(expReg)) {
			alert("E-mail inválido");
			campo.value='';
			campo.focus();
			return false;
		}
	}
}

function vmax(campo,maximo) {
    if (campo.value.length > maximo) {
        campo.value = campo.value.substr(0,maximo);
    }
}

//-----EFEITOS-----

function showhide(div) {
	var state = document.getElementById(div).style.display; 
	if (state == 'block') { 
		state = 'none'; 
	} else { 
		state = 'block'; 
	} 
	if (document.all) { //IS IE 4 or 5 (or 6 beta) 
		eval("document.all." + div + ".style.display = state"); 
	} 
	if (document.layers) { //IS NETSCAPE 4 or below 
		document.layers[div].display = state; 
	} 
	if (document.getElementById &&!document.all) { 
		document.getElementById(div).style.display = state; 
	} 
}

function show(div) {
	var state = 'block'; 
	if (document.all) { //IS IE 4 or 5 (or 6 beta) 
		eval("document.all." + div + ".style.display = state"); 
	} 
	if (document.layers) { //IS NETSCAPE 4 or below 
		document.layers[div].display = state; 
	} 
	if (document.getElementById &&!document.all) { 
		document.getElementById(div).style.display = state; 
	} 
}

function hide(div) {
	var state = 'none'; 
	if (document.all) { //IS IE 4 or 5 (or 6 beta) 
		eval("document.all." + div + ".style.display = state"); 
	} 
	if (document.layers) { //IS NETSCAPE 4 or below 
		document.layers[div].display = state; 
	} 
	if (document.getElementById &&!document.all) { 
		document.getElementById(div).style.display = state; 
	} 
}

function imprimir(id_controle)  {
    var conteudo = document.getElementById(id_controle).innerHTML;
    var Win = window.open('','imprimir','letf=0,top=0,width=600,height=600,toolbar=0,scrollbars=1,status=0');
    Win.document.write('<link rel="stylesheet" href="App_Themes/padrao/geral.css" type="text/css" />');
    Win.document.write(conteudo.replace('TBODY','TABLE').replace('<TABLE>','<TABLE CELLPADDING=0 CELLSPACING=0>'));
    Win.document.close();
    Win.focus();
    Win.print();
}


// Adicinal Lightbox Flash

function lightboxflash(img,titulo,ww,hh) {
    var img_flash = document.createElement("A");
    img_flash.rel = "lightbox";
    img_flash.href = img;
    img_flash.title = titulo;
	if (typeof ww != 'undefined'){
		img_flash.setAttribute('width',ww);
	}
	if (typeof hh != 'undefined'){
		img_flash.setAttribute('height',hh);
	}
    myLightbox.start(img_flash);
    img_flash = null;
}
