// Bloqueia copia e cola	
function BloqueiaCopiaCola()
{
			var ctrl=window.event.ctrlKey;
			var tecla=window.event.keyCode;
			if (ctrl && tecla==67) {event.keyCode=0; event.returnValue=false;}
			if (ctrl && tecla==86) {event.keyCode=0; event.returnValue=false;}
}


//Bloco de código para esconder e mostra form - formatadadosie
var Ver4 = parseInt(navigator.appVersion) >= 4
var IE4 = ((navigator.userAgent.indexOf("MSIE") != -1) && Ver4)
var block = "formulario";
function esconde() {document.form.style.visibility = "hidden" }
function mostra() {document.form.style.visibility = "visible" }
//Bloco de código para esconder e mostra form

var da = (document.all) ? 1 : 0;
var pr = (window.print) ? 1 : 0;
var mac = (navigator.userAgent.indexOf("Mac") != -1); 

function printPage()
{
  if (pr) // NS4, IE5
    window.print()
  else if (da && !mac) // IE4 (Windows)
    vbPrintPage()
  else // other browsers
    alert("Desculpe seu browser não suporta esta função. Por favor utilize a barra de trabalho para imprimir a página.");
  return false;
}

if (da && !pr && !mac) with (document) {
  writeln('<OBJECT ID="WB" WIDTH="0" HEIGHT="0" CLASSID="clsid:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>');
  writeln('<' + 'SCRIPT LANGUAGE="VBScript">');
  writeln('Sub window_onunload');
  writeln('  On Error Resume Next');
  writeln('  Set WB = nothing');
  writeln('End Sub');
  writeln('Sub vbPrintPage');
  writeln('  OLECMDID_PRINT = 6');
  writeln('  OLECMDEXECOPT_DONTPROMPTUSER = 2');
  writeln('  OLECMDEXECOPT_PROMPTUSER = 1');
  writeln('  On Error Resume Next');
  writeln('  WB.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER');
  writeln('End Sub');
  writeln('<' + '/SCRIPT>');
}


//***********************************
//
function MouseNormal()
{
	document.body.style.cursor = 'default';
}

function MouseAmpulheta()
{		
	document.body.style.cursor = 'wait';
}
  
   //Funcao para abertura de popup
  function winpop( strPagina, intLargura, intAltura, booRolagem )
		
	{
        strJanela = "pop" + (intLargura+intAltura);
        var objjan = window.open( strPagina, strJanela, 'width=' + intLargura + ',height=' + intAltura +',menubar=no,location=no,scrollbars=' + booRolagem + ',status=no,toolbar=no,resizable=no,top=100,left=100' ); 
        objjan.focus();
    }
  
  //Funcao para abertura de popup do tipo modal
  function winpopmodal( strPagina, intLargura, intAltura, strArg )
	{
		strPagina = strPagina + "?" + strArg;
		var strParametros = "dialogHeight: " + intAltura + "px; dialogWidth: " + intLargura + "px; dialogTop: 100px; dialogLeft: 100px; edge: Raised; center: Yes; help: No; resizable: No; status: No;";
		var objjan = window.showModalDialog(strPagina, "", strParametros);
    }
    
    <!--
function currencyFormat(fld, e) {
	var milSep = '.';
	var decSep = ',';
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) return true;  // Enter
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
	len = fld.value.length;
	for(i = 0; i < len; i++) if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	aux = '';
	for(; i < len; i++) if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 2) {
		aux2 = '';
		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += milSep;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}
		fld.value = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--) fld.value += aux2.charAt(i);
		fld.value += decSep + aux.substr(len - 2, len);
	}
	return false;
}		

function currencyFormatPeso(fld, e) {
	var milSep = '.';
	var decSep = ',';
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) return true;  // Enter
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
	len = fld.value.length;
	for(i = 0; i < len; i++) if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	aux = '';
	for(; i < len; i++) if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + aux;
	//if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 1) {
		aux2 = '';
		for (j = 0, i = len - 2; i >= 0; i--) {
			if (j == 3) {
				aux2 += milSep;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}
		fld.value = '';
		len2 = aux2.length;
		for (i = len2 ; i >= 0; i--) fld.value += aux2.charAt(i);
		fld.value += decSep + aux.substr(len - 1, len);
	}
	return false;
}		

function Substitui(vr,oldChar,newChar){
    var newString;
    var re = new RegExp(oldChar,"gi");
  	newString = vr.replace(re,newChar);
  	return newString;
}
        
//Limitador de tamanho de texto para campos do tipo Textarea	
function LimitaTextArea(tammax,teclapres){
	vr = event.srcElement.value;
	tam = vr.length;
	
	if (document.selection){ 
		txt = document.selection.createRange().text;
		if (txt.length > 0){
			document.selection.clear();
			vr = '';
			event.srcElement.value = '';
		}
	}	
	
	if (tam >= tammax){
		event.srcElement.value = event.srcElement.value.substr(0,tammax);
		return false;
	}
	return true;
}


function FormataCgc(tammax,teclapres) {
	var tecla = teclapres.keyCode;
	vr = event.srcElement.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	tam = vr.length;

	if (document.selection){ 
		txt = document.selection.createRange().text;
		if (txt.length > 0){
			document.selection.clear();
			vr = '';
			event.srcElement.value = '';
		}
	}
		
	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

	if (tecla == 8 ){	tam = tam - 1 ; }
		
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 2 ){ 
	 		event.srcElement.value = vr ; }
	 	if ( (tam > 2) && (tam <= 6) ){
	 		event.srcElement.value = vr.substr( 0, tam - 2 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 7) && (tam <= 9) ){
	 		event.srcElement.value = vr.substr( 0, tam - 6 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 10) && (tam <= 12) ){
	 		event.srcElement.value = vr.substr( 0, tam - 9 ) + '.' + vr.substr( tam - 9, 3 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 13) && (tam <= 14) ){
	 		event.srcElement.value = vr.substr( 0, tam - 12 ) + '.' + vr.substr( tam - 12, 3 ) + '.' + vr.substr( tam - 9, 3 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 15) && (tam <= 17) ){
	 		event.srcElement.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ;}
	}		
}


function textCounter(field, countfield, maxlimit)
{
	if (field.value.length > maxlimit)
		field.value = field.value.substring(0, maxlimit);
	else
	countfield.value = maxlimit - field.value.length;
}
		

// Formata o CPF enquanto o usuário está digitando
function FormataCPF(objCampo,intKeyCode){
	if((parseInt(intKeyCode) < 48) || (parseInt(intKeyCode) > 57)){
		return false
	}
	else
	{
		strValor = objCampo.value
		intLengthValor = parseInt(strValor.length)
		if(intLengthValor == 3){
			objCampo.value = strValor + "."
		}
		if(intLengthValor == 7){
			objCampo.value = strValor + "."
		}
		if(intLengthValor == 11){
			objCampo.value = strValor + "-"
		}
		return true
	}
}

// Substitui caracteres de um determinado valor informado
function Substitui(strValor, strCaracterAntigo, strCaracterNovo){

	// Inicializa a variável
	strValorRetorno = ""

	// Percorre a string e utiliza seus caracteres
	for(x = 0; x < strValor.toString().length; x++){

		// Armazena o caracter corrente
		strCaracter = strValor.toString().substring(x, x + 1)

		// Verifica se o caracter corrente é igual ao caracter que deverá ser substituído e realiza as devidas manutenções
		if(strCaracter == strCaracterAntigo){
			strValorRetorno = strValorRetorno + strCaracterNovo
		}
		else
		{
			strValorRetorno = strValorRetorno + strCaracter
		}

	}

	// Retorna a string com os devidos caracteres substituídos, conforme a necessidade
	return strValorRetorno

}

function FormataValor(tammax,teclapres)	 {
	var tecla = teclapres.keyCode;
	var vr = event.srcElement.value;
	var txt;
	
	if(parseInt(tecla) != 13){
		if((parseInt(tecla) < 48) || (parseInt(tecla) > 57)){
			event.returnValue = false;
			return false;
		}
	}

	if (document.selection){ 
		txt = document.selection.createRange().text;
		if (txt.length > 0){
			document.selection.clear();
			vr = '';
			event.srcElement.value = '';
		}
	}
	
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );


	if (vr.length > 0){ vr = parseInt(vr,10); }
	vr = '' + vr

	tam = vr.length;

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

	if (tecla == 8 ){	tam = tam - 1 ; }
		
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 2 ){ 
			if (tam == 1){ 
				event.srcElement.value = "0,0" + vr ; 
			}else if (tam == 2){ 
				event.srcElement.value = "0," + vr ; 
			}else{
				event.srcElement.value = "" ; 
			}
		}
	 	if ( (tam > 2) && (tam <= 5) ){
	 		event.srcElement.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; 
		}
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		event.srcElement.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; 
		}
	 	if ( (tam >= 9) && (tam <= 11) ){
	 		event.srcElement.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; 
		}
	 	if ( (tam >= 12) && (tam <= 14) ){
	 		event.srcElement.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; 
		}
	 	if ( (tam >= 15) && (tam <= 17) ){
	 		event.srcElement.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;
		}
	}		
}

function SelectMeOnly(objRadioButton, grdName)  
{  
	 // Encontra todos os chebox
     var inputs = document.getElementsByTagName("input");
      
     // Dá um loop na coleção de chekbox
     for(index = 0; index < inputs.length; index++)
     {
		if (inputs[index].type == "radio")
		{
			var chkSecundarias = inputs[index];
			var chkPrincipal =  objRadioButton;
						
			if(chkSecundarias.id != chkPrincipal.id)
			{
				chkSecundarias.checked = false;
			}
			
		}
     }
}


/* Calcula o grid  */
function CalculaGridNotas(pchkNotaFiscal, plblPesoBruto, plblDtMinima, plblDtMaxima, plblValorTotal, plblQuantidadeProduto)
{
	
	var lblResultQtdItens;
	var intQtdItens = 0;
	var decPesoTotal = 0;
	lblResultQtdItens = document.getElementById("lblResultQtdeItens");
	lblResultPesoBruto = document.getElementById("lblResultPesoTotal");
	
	if(pchkNotaFiscal.checked)
	{
		if (!lblResultQtdItens.innerText == "")
		{	
			intQtdItens = parseInt(lblResultQtdItens.innerText);
		}
		lblResultQtdItens.innerText = intQtdItens + 1;
		
		// Valida o peso total e soma ao resultado.
		if (!lblResultPesoBruto.innerText == "")
		{
			decPesoTotal = parseFloat(lblResultPesoBruto.innerText.replace(".","").replace(",","."));
		}
		decPesoTotal = parseFloat(plblPesoBruto.innerText) + decPesoTotal;
		lblResultPesoBruto.innerText = decPesoTotal.toFixed(2).replace(".",",");
		
		var datAtual;
		var datMenor;
		var lblDataMinima;
		
		datAtual = Date.parse(plblDtMinima.innerText);
		lblDataMinima = document.getElementById("lblResultDataMinima");
		
		if(!lblDataMinima.innerText == "")
		{
			datMenor = Date.parse(lblDataMinima.innerText);
		}
		else
		{
			lblDataMinima.innerText = plblDtMinima.innerText;
			datMenor = Date.parse(lblDataMinima.innerText);
		}
		
		
		if ((datMenor - datAtual) / 24*60*60*1000 > 0)
		{
			lblDataMinima.innerText = plblDtMinima.innerText;
		}
		
		var datMaior;
		var lblDataMaxima;
		
		datAtual = Date.parse(plblDtMaxima.innerText);
		lblDataMaxima = document.getElementById("lblResultDataMaxima");
		
		if(!lblDataMaxima.innerText == "")
		{
			datMaior = Date.parse(lblDataMaxima.innerText);
		}
		else
		{
			lblDataMaxima.innerText = plblDtMaxima.innerText;
			datMaior = Date.parse(lblDataMaxima.innerText);
		}
		
		if ((datMaior - datAtual) / 24*60*60*1000 < 0)
		{
			lblDataMaxima.innerText = plblDtMaxima.innerText;
		}
	
	}
	else
	{
		if (!lblResultQtdItens.innerText == "")
		{	
			intQtdItens = parseInt(lblResultQtdItens.innerText);
		}
		lblResultQtdItens.innerText = intQtdItens - 1;
		
		// Valida o peso total e soma ao resultado.
		if (!lblResultPesoBruto.innerText == "")
		{
			decPesoTotal = parseFloat(lblResultPesoBruto.innerText.replace(".","").replace(",","."));
		}
		decPesoTotal = decPesoTotal - parseFloat(plblPesoBruto.innerText);
		lblResultPesoBruto.innerText = decPesoTotal.toFixed(2).replace(".",",");
		
	
		SelecionaDatas();
		
	}

}

function SelecionaDatas()
{
	var intCount = 0;
	var strNomeControle = "radGridNotaFiscal__ctl1__ctl";
	var lblMenorData = document.getElementById("lblResultDataMinima");
	var datMenorData;
	lblMenorData.innerText = "";
	
	var lblMaiorData = document.getElementById("lblResultDataMaxima");
	var datMaiorData;
	lblMaiorData.innerText = "";
	
	var datMinimaAnterior = 0;
	var datMaximaAnterior = 0;
	
	for (intCount = 0; intCount <= 10; intCount++)
	{
		var chkNotaFiscal;
		chkNotaFiscal = document.getElementById(strNomeControle + intCount + "_chkNotaFiscal");
		
		if(chkNotaFiscal != null)
		{
			if(chkNotaFiscal.checked)
			{
				var lblDataMinima;
				var datMinima;
				
				lblDataMinima = document.getElementById(strNomeControle + intCount + "_lblDtMinima");
				datMinima = Date.parse(lblDataMinima.innerText);
				
				if (datMinimaAnterior = 0)
				{
					lblMenorData.innerText = lblDataMinima.innerText;
				}
				
				if (((datMinima - datMinimaAnterior) / 24*60*60*1000) > 0)
				{
					lblMenorData.innerText = lblDataMinima.innerText;
					datMinimaAnterior = datMinima;
				}
				
				var lblDataMaxima;
				var datMaxima;
				lblDataMaxima = document.getElementById(strNomeControle + intCount + "_lblDtMaxima");
				datMaxima = Date.parse(lblDataMaxima.innerText);
				
				if ((datMaxima - datMaximaAnterior) / 24*60*60*1000 > 0)
				{
					lblMaiorData.innerText = lblDataMaxima.innerText;
					datMaximaAnterior = datMaxima;
				}
				
			}
		}
	}
}

 

function FormataHora(objCampo,intKeyCode){
 if((parseInt(intKeyCode) < 48) || (parseInt(intKeyCode) > 57)){
  return false
 }
 else
 {
  if(objCampo.value == ''){
   objCampo.value = '00:00'
  }
  if(objCampo.value.substr(0,1) == '0' || objCampo.value.length == 5){
   objCampo.value = objCampo.value.substr(1, objCampo.value.length)
  }
  strValor = Substitui(objCampo.value, ':', '') + (intKeyCode - 48).toString();
  strHora = strValor.substr(0, strValor.length - 2)
  strMinuto = strValor.substr(strValor.length - 2, strValor.length)
  objCampo.value = strHora + ':' + strMinuto
  return false
 }
}







// Desabilita os objetos da página
function DisableControls()
{
    for (var i=0; i<document.forms[0].elements.length; i++)
    {            
        var obj = document.forms[0].elements[i];
        obj.disabled = true;
    }
}

// Habilita os objetos da página
function EnableControls()
{
    for (var i=0; i<document.forms[0].elements.length; i++)
    {        
        var obj = document.forms[0].elements[i];
        obj.disabled = false;
    }
}
