function dataInvalida(str) { 

	dia = (str.value.substring(0,2)); 
    mes = (str.value.substring(3,5)); 
	ano = (str.value.substring(6,10)); 

	cons = true; 
	
	// verifica se foram digitados números
	if (isNaN(dia) || isNaN(mes) || isNaN(ano)){
		alert("Preencha a data somente com números."); 
		str.value = "";
		str.focus(); 
		return false;
	}
		
    // verifica o dia valido para cada mes 
    if ((dia < 01)||(dia < 01 || dia > 30) && 
		(mes == 04 || mes == 06 || 
		 mes == 09 || mes == 11 ) || 
		 dia > 31) { 
    	cons = false; 
	} 

	// verifica se o mes e valido 
	if (mes < 01 || mes > 12 ) { 
		cons = false; 
	} 

	// verifica se e ano bissexto 
	if (mes == 2 && ( dia < 01 || dia > 29 || 
	   ( dia > 28 && 
	   (parseInt(ano / 4) != ano / 4)))) { 
		cons = false; 
	} 
    
	if (cons == false) { 
		return true;
	} 
	return false;
}

// colocar no evento onKeyUp passando o objeto como parametro
function formata(val)
{
   	var pass = val.value;
	var expr = /[0123456789]/;
		
	for(i=0; i<pass.length; i++){
		// charAt -> retorna o caractere posicionado no índice especificado
		var lchar = val.value.charAt(i);
		var nchar = val.value.charAt(i+1);
	
		if(i==0){
		   // search -> retorna um valor inteiro, indicando a posição do inicio da primeira
		   // ocorrência de expReg dentro de instStr. Se nenhuma ocorrencia for encontrada o método retornara -1
		   // instStr.search(expReg);
		   if ((lchar.search(expr) != 0) || (lchar>3)){
			  val.value = "";
		   }
		   
		}else if(i==1){
			   
			   if(lchar.search(expr) != 0){
				  // substring(indice1,indice2)
				  // indice1, indice2 -> será usado para delimitar a string
				  var tst1 = val.value.substring(0,(i));
				  val.value = tst1;				
 				  continue;			
			   }
			   
			   if ((nchar != '/') && (nchar != '')){
				 	var tst1 = val.value.substring(0, (i)+1);
				
					if(nchar.search(expr) != 0) 
						var tst2 = val.value.substring(i+2, pass.length);
					else
						var tst2 = val.value.substring(i+1, pass.length);
	
					val.value = tst1 + '/' + tst2;
			   }

		 }else if(i==4){
			
				if(lchar.search(expr) != 0){
					var tst1 = val.value.substring(0, (i));
					val.value = tst1;
					continue;			
				}
		
				if	((nchar != '/') && (nchar != '')){
					var tst1 = val.value.substring(0, (i)+1);

					if(nchar.search(expr) != 0) 
						var tst2 = val.value.substring(i+2, pass.length);
					else
						var tst2 = val.value.substring(i+1, pass.length);
	
					val.value = tst1 + '/' + tst2;
				}
   		  }
		
		  if(i>=6){
			  if(lchar.search(expr) != 0) {
					var tst1 = val.value.substring(0, (i));
					val.value = tst1;			
			  }
		  }
	 }
	
     if(pass.length>10)
		val.value = val.value.substring(0, 10);
	 	return true;
}
 /*
 obj=nome do campo
 tammax = limite de caracteres
 teclapress=event
 */
	function formataValor(obj,tammax,teclapres) {	 
	
		var valor = parseFloat( strRealToFloat(obj.value) )
				
	 var tecla = teclapres.keyCode;
	 

	 vr = obj.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( ".", "" );
	 
//	 alert("1");
	 
	 tam = vr.length;
	
// 	 alert("1");
	 if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }
	 
	 if (tecla == 8 ){ tam = tam - 1 ; }
// 	 alert("1");
	  
	 if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
//  		  alert("9");
		  if ( tam <= 2 ){
		   obj.value = vr ; }
		  if ( (tam > 2) && (tam <= 5) ){
		   obj.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
		  if ( (tam >= 6) && (tam <= 8) ){
		   obj.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
		  if ( (tam >= 9) && (tam <= 11) ){
		   obj.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) ){
		   obj.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) ){
		   obj.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 ) ;}
		   //alert(obj.value);
	 }	 
	 else
	 	 return validNumber(teclapres);

	}
	
	
function addFormatoValor(valor) {	 

	var str = "";
	
	var vr = valor + "";
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
		 
	var tam = vr.length;		
	
	if ( tam <= 2 ){
		str = vr ; }
	if ( (tam > 2) && (tam <= 5) ){
		str = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
	if ( (tam >= 6) && (tam <= 8) ){
		str = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	if ( (tam >= 9) && (tam <= 11) ){
		str = 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) ){
		str = 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) ){
		str = 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 ) ;}

	return str;
}	
	
	
 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 
 
 function getKeyCode(evt){
	var key;
	if (ExpYes)
		key = evt.keyCode;
	else
		key = evt.which;
	return key;
}
 
 function validaCaracter(evt, tipo){
	
   var key;
   var keychar;
	key = getKeyCode(evt);	
   /**
   * C?digos de teclas do teclado num?rico
	de 96 ? 105 =  0 - 9
        106 = *
        107 = +
        109 = -
        110 = ,
        111 = /
        194 = .
      */

   // array das setas
   var keyseta = new Array(37,39);

   // array dos numeros + setas
   var keynum = new Array(96,97,98,99,100,101,102,103,104,105,37,39);

   // array de data + numeros
   var keynumD = new Array(96,97,98,99,100,101,102,103,104,105,37,39,111);

   // array dos numeros
   var keydigit = new Array(96,97,98,99,100,101,102,103,104,105);

   keychar = String.fromCharCode(key);	

   if ((key==null) || (key==0) || (key==8) || (key==9)|| (key==27) ||
		(key==46))
      return true;
   else if (tipo=="V" && ((("0123456789").indexOf(keychar) > -1) ||
		validaKeyArray(key,keynum)))
   		return true;
   else if (tipo=="D" && ((("/0123456789").indexOf(keychar) > -1) ||
		validaKeyArray(key,keynumD)))
	    return true;
   else if (tipo=="DT" && (((" :/0123456789").indexOf(keychar) > -1) ||
		validaKeyArray(key,keynumD)))
	    return true;
   else if (tipo=="H" && (((":0123456789").indexOf(keychar) > -1) ||
		validaKeyArray(key,keynum)))
   		return true;
   else if (tipo=="A" && (("0123456789").indexOf(keychar) == -1 || 
   			validaKeyArray(key,keyseta)))
		return true;
		
   return false;
}

function validaKeyArray(keycode, keyArray){
	var achou = false;

	if(keyArray != null){
		for(var i = 0; i < keyArray.length; i++){
			//alert("keycode:"+keycode+" = keyArray["+i+"]:"+keyArray[i]);
			if(keyArray[i] == keycode){
				achou = true;
				break;
			}
		}
		return achou;
	} else {
		return true;
	}
}

 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

function iniciarBtn(formName){
	formName.method.value = 'iniciar';	
	formName.submit();
}

function imprimirBtn(formName){
	formName.method.value = 'imprimir';	
	formName.submit();
}

function cancelarBtn(formName){
	formName.method.value = 'cancelar';	
	formName.submit();
}


function procurarBtn(formName){
	formName.method.value = 'procurar';	
	formName.submit();
}

function mudarNomeBtn(formName){
	formName.incluirAlterar.value = 'Alterar';
}

function mudarNome(formName){
	formName.incluir.value = 'Alterar';
}


function limparBtn(formName){
	if(formName.incluirAlterar != undefined)
		formName.incluirAlterar.value = 'Incluir';
	formName.method.value = 'limpar';	
	formName.submit();
}

function incluir(){
	var f = document.forms[0];
	with(f){
		method.value = "saveOrUpdate";
		modo.value = "INCLUSAO";
	}	
	f.submit();
}
function acaoBtn(acaoStr){
	var f = document.forms[0];
	with(f){
		method.value = acaoStr;
	}	
	f.submit();
}


function selecaoUnicaCheck(formName, checkbox){
		var form = eval(formName);
		var checkbox = eval("document."+formName+"."+checkbox);
		var checked = 0;
		
		if (checkbox == undefined) {
		    return checked;
		}
		
		//se contiver apenas um
		if (checkbox.length == undefined) {
			//verifica se esta marcado
			if(checkbox.checked){
				checked = 1;
			}
		    return checked;
		}
		
		for (c=0;c<checkbox.length;c++) {
		    if (checkbox[c].checked) {
		        checked++;
		    }
		}
		return checked;
}


function returnValueCheckMarcado(formName, check){
		var checkbox = eval("document."+formName+"."+check);
		var checked;

		if (checkbox.length != undefined){
			for (c=0;c<checkbox.length;c++) {
			    if (checkbox[c].checked) {
			        checked = checkbox[c].value;
		    	    break;
			    }
			}
		}else{
			checked = checkbox.value;
		}
		return checked;
}


function preecherCampos(id, formname){
	//obs: o campo transformado n?o pode ter o nome iniciado por n?meros

	var nomeCampos = (eval("document."+formname+".campos").value).split(",");
	var valoresCampos = (eval("document."+formname+"."+id).value).split(",");
	
	for(i=0; i<valoresCampos.length;i++){
		campo = eval("document."+formname+"."+nomeCampos[i]);
		//campo.tagName - imprime: SELECT, INPUT
		if(campo.type == "select-one"){
			for(j=0; j<campo.length;j++){
				if(campo.options[j].value == valoresCampos[i]){
					campo.options[j].selected = 'selected';
				}
			}
		}else if(campo.type == "checkbox"){
			//o valor para checkbox deve conter string: true ou false
			campo.checked = eval(valoresCampos[i]);
		}else{
			campo.value = valoresCampos[i];
		}
	}
	var ident = id.substring(3, id.length);
	(eval("document."+formname+".identificador")).value = ident;
	
}

function preecherCamposMonetario(id, formname){
	//obs: M?todo modificado para usar delimitador com ponto_e_virgula(;) ao
	//inves de virgula(,) pois os valores monetarios possuem virgula

	var nomeCampos = (eval("document."+formname+".campos").value).split(";");
	var valoresCampos = (eval("document."+formname+"."+id).value).split(";");
	
	for(i=0; i<valoresCampos.length;i++){
		campo = eval("document."+formname+"."+nomeCampos[i]);
		//campo.tagName - imprime: SELECT, INPUT
		if(campo.type == "select-one"){
			for(j=0; j<campo.length;j++){
				if(campo.options[j].value == valoresCampos[i]){
					campo.options[j].selected = 'selected';
				}
			}
		}else if(campo.type == "checkbox"){
			//o valor para checkbox deve conter string: true ou false
			campo.checked = eval(valoresCampos[i]);
		}else{
			campo.value = valoresCampos[i];
		}
	}
	var ident = id.substring(3, id.length);
	(eval("document."+formname+".identificador")).value = ident;
}

function chamarPopup(html, popWidth, popHeight){
	var nome = html.substring(0, html.indexOf("."));
   	var popLeft    =  (screen.width  - popWidth)  /2;
   	var popTop     =  (screen.height - popHeight) /2;
   	
	window.open(html, nome,"fullscreen=no,menubar=no,location=no,titlebar=no,toolbars=no,resizable=no,status=yes,scrollbars=yes,width=" + popWidth + ",height=" + popHeight  +",top=" + popTop + ",left=" + popLeft);
}
function chamarPopupModal(html, popWidth, popHeight){
		var nome = html.substring(0, html.indexOf("."));
	   	var popLeft    =  (screen.width  - popWidth)  /2;
	   	var popTop     =  (screen.height - popHeight) /2;
		window.showModalDialog(html, "", "fullscreen:no,menubar:no,location:no,titlebar:no,toolbars:no,resizable:no,status:yes,scrollbars:yes,dialogWidth:" + popWidth + "px,dialogHeight:" + popHeight  +"px,dialogTop:" + popTop + "px,dialogLeft:" + popLeft + "px")
}

function returnReferenciaPopup(html, popWidth, popHeight){
	var nome = html.substring(0, html.indexOf("."));
   	var popLeft    =  (screen.width  - popWidth)  /2;
   	var popTop     =  (screen.height - popHeight) /2;
	return window.open(html, nome,"fullscreen=no,menubar=no,location=no,titlebar=no,toolbars=no,resizable=no,status=yes,scrollbars=yes,width=" + popWidth + ",height=" + popHeight  +",top=" + popTop + ",left=" + popLeft);
}

function sortSelect(name) {
  		obj = document.getElementById(name);
		var o = new Array();
		
		if (!hasOptions(obj)) { 
			return; 
		}

		for (var i=0; i<obj.options.length; i++) {
			o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
		}

		if (o.length==0) { 
			return; 
		}

  		o = o.sort( 
			function(a,b) { 
				if ((a.text+"") < (b.text+"")) { 
					return -1; 
				}
				if ((a.text+"") > (b.text+"")) { 
					return 1; 
				}
				return 0;
			} 
		);

		for (var i=0; i<o.length; i++) {
			obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
}


function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { 
		return true; 
	}
	return false;
}

function efetuaTransferencia(form, de, para){
	var selecionado = false;
	with(form){
		for(i=de.options.length-1; i>=0;i--){
			if(de.options[i].selected){
				var text = de.options[i].text;
				var value = de.options[i].value;
				de.options[i] = null;
				para.options[para.options.length] = new Option(text,value, false,'selected');
				selecionado = true;  
			}
		}
		
		if(selecionado){
			sortSelect(de.name);
			sortSelect(para.name);
		}
	}	
}

function retornaDataAtual(){
   	hoje = new Date();
	dia = hoje.getDate();
	mes = hoje.getMonth() + 1;
	ano = hoje.getYear();
	if (dia < 10){
		dia = "0" + dia;
	}
	if (mes < 10){
		mes = "0" + mes;
	}
	
   	return dia + "/" + mes + "/" + ano;
}

function verificaMaiorData(data1, data2) {
	data1 = data1.substr(6,4) + data1.substr(3,2) + data1.substr(0,2);
	data2 = data2.substr(6,4) + data2.substr(3,2) + data2.substr(0,2);
    return eval(data1 < data2);
}



function atualizarAgencias(codBanco, formName){
	formName.identificador.value = codBanco;
 	formName.method.value='atualizarAgencias';
 	formName.banco.value = codBanco;
 	ajaxAnywhere.formName = "ContaBancariaForm"; 	
 	ajaxAnywhere.submitAJAX();
}

function preparaAlteracao(formName,param){
	formName.method.value = "preparaAlteracao";
	formName.identificador.value = param;
	formName.submit();
}

function preparaAlteracaoAreaAtuacao(formName,param){
	formName.method.value = "preparaAlteracao";
	formName.idPessoa.value = param;
	formName.submit();
}

	// atualiza (3 parametros) pagina que deu origem a um popup
	function updateParentLimite2( idPessoa, cnpjPessoa, nomePessoa) {
	
	    opener.document.forms[0].idPessoa.value = idPessoa;
	    opener.document.forms[0].cnpjPessoa.value = cnpjPessoa;
   	    opener.document.forms[0].nomePessoa.value = nomePessoa;
	    
	    //opener.document.forms[0].method.value = 'loadPageFromPopUp';
	    //opener.document.forms[0].submit();
	
	    self.close();
	    return false;
	}

	// atualiza (2 parametros) pagina que deu origem (limitecreditocooperativa.jsp) a um popup
	function updateParentLimite(codigo, dataIni, dataFim) {
	
	    opener.document.forms[0].codigoPeriodoCompra.value = codigo;
	    opener.document.forms[0].dataIni.value = dataIni;
	    opener.document.forms[0].dataFim.value = dataFim;
	    
	    //opener.document.forms[0].
	}
	