
function mostra_div(div_id)
{
	var div = document.getElementById(div_id);

	if(div.style.display=='block')
		div.style.display = 'none';
	else
		div.style.display = 'block';
}

function popup(url, w, h)
{
	window.open(url, '','width='+w+',height='+h);
}


//FORM FUNCTIONS
function testa_campos(campos)
{
	for(var x=0;x<campos.length;x++)
	{
		var campo = document.getElementById(campos[x]);

		if(campo.value=='')
		{
			alertError(campo.id,'Preencha os campos Obrigatórios');
			return false;
		}

//Testes de validade para campos especiais
		switch(campo.id)
		{
			case "cpf": 
				if(!testa_cpf(campo.value))
				{ 
					alertError(campo.id);
					return false;
				}
			break;		
			case "email": 
				if(!testa_email(campo.value))
				{ 
					alertError(campo.id,'Email Inválido');
					return false;
				}
			break;		
		}
		
	}
	return true;
}

function toInt(str)
{
	var new_str = '';
	for(var i=0;i<str.length;i++)
		if(!isNaN(str.charAt(i)))
			new_str = new_str+str.charAt(i);
	
	if(isNaN(new_str))		
		return false;	
	else		
		return new_str;
}


function testa_email(email)
{
		if( email.lastIndexOf('@')==-1 || email.lastIndexOf('.')==-1)
			return false;
		else
			return true;
}


function trimAll(val)
{
	new_val =  val.substring(0,val.lastIndexOf(' ')) + val.substring(val.lastIndexOf(' ')+1,val.length);
	if(new_val.lastIndexOf(' ')!=-1 )
		trimAll(new_val);

	return new_val;
}


function alertError(elementoId,msg) 
{
	var el = document.getElementById(elementoId);
//	var oldBg = el.style.background;
//	var oldColor = el.style.color;

	el.style.background='red';
	el.style.color='white';
	alert(msg);
	el.style.background='white';
	el.style.color= 'black';
	el.focus();
}

function compare(objId1,objId2)
{
	obj1 = document.getElementById(objId1);
	obj2 = document.getElementById(objId2);
	
	if(obj1.value == obj2.value)
		return true;
	else
		return false;
		
}



function FormataValor(campo,tammax,teclapres) 
{
	var tecla = teclapres.keyCode;
	vr = document.form[campo].value;
	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 (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 ){ 
	 		document.form[campo].value = vr ; }
	 	if ( (tam > 2) && (tam <= 5) ){
	 		document.form[campo].value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		document.form[campo].value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 9) && (tam <= 11) ){
	 		document.form[campo].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) ){
	 		document.form[campo].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) ){
	 		document.form[campo].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 ) ;}
	}
		
	for (var ct = 0; ct < document.form.elements.length; ct++) {
		if (document.form.elements[ct].name == document.form.elements[campo].name) {
			if ( !teclapres.shiftKey && tecla == 9 && document.form.elements[ct+1] && document.form.elements[ct+1].name == "senhaConta" && document.applets['tclJava'] ){
				document.applets['tclJava'].setFocus();
			}	
		}
	}
}
