function trim(str){
	// exclui todos os " " no inicio da string
	while (str.charAt(0) == " ")
		str = str.substr(1,str.length -1);
	//
	while (str.charAt(str.length-1) == " "){
		str = str.substr(0,str.length-1);
	}			
	//
	
	return str;
}


function  filtra_teclas_numero()
{
	cod = window.event.keyCode;
	if ( (( cod < 48 ) || (cod > 57) ) && ( cod != 8 ) && ( cod != 13 )  ){
		return false;
	}
	return true;
}


//////////////////////////////////////////////////////////

//////////////////	FORMATA DATA, CEP E TELEFONE
			
//////////////////////////////////////////////////////////

function FormataData(Campo,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.formulario[Campo].value;
	vr = vr.replace( ".", "", "all" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;

	if ( tecla != 9 && tecla != 8 ){
		if ( tam > 2 && tam < 5 )
			document.formulario[Campo].value = vr.substr( 0, 2  ) + '/' + vr.substr( 2, tam - 2 );
		if ( tam >= 5 && tam <= 10 )
			document.formulario[Campo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 );
	}
}

function FormataData2(Campo,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.formulario[Campo].value;
	//vr = vr.replace( ".", "", "all" );
	vr = vr.replace( "/", "" );
	//vr = vr.replace( "/", "" );
	tam = vr.length + 1;

	if ( tecla != 9 && tecla != 8 ){
		if ( tam > 2)
			//document.formulario[Campo].value = vr.substr( 0, 2  ) + '/' + vr.substr( 2, tam - 2 );
			document.formulario[Campo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 4 );
		//if ( tam >= 5 && tam <= 10 )
			//document.formulario[Campo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 );
	}
}


function FormataCep(Campo,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.formulario[Campo].value;
	vr = vr.replace( ".", "", "all" );
	vr = vr.replace( "-", "", "all" );
	tam = vr.length + 1;

	
	if ( tecla != 9 && tecla != 8 ){
		if ( tam > 2  && tam <= 5 )
			document.formulario[Campo].value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, tam - 2 );
		if ( tam > 5 && tam <= 8)
			document.formulario[Campo].value = vr.substr( 0, 2 ) + '.' + vr.substr( 2, 3 ) + '-' + vr.substr( 5, 3 );
	}
}

function FormataTel(Campo,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.formulario[Campo].value;
	vr = vr.replace( "-", "", "all" );
	tam = vr.length + 1;

	
	if ( tecla != 9 && tecla != 8 ){
		if ( tam > 4)
			document.formulario[Campo].value = vr.substr( 0, 4 ) + '-' + vr.substr( 4, 4 );
	}
}

function FormataTelOnblur(Campo) {
	vr = document.formulario[Campo].value;
	vr = vr.replace( "-", "", "all" );
	tam = vr.length;
	if ( tam == 8){
		document.formulario[Campo].value = vr.substr( 0, 4 ) + '-' + vr.substr( 4, 4 );
	}
	else if ( tam == 7){
		document.formulario[Campo].value = vr.substr( 0, 3 ) + '-' + vr.substr( 3, 4 );
	}
	else if ( tam == 6){
		document.formulario[Campo].value = vr.substr( 0, 2 ) + '-' + vr.substr( 2, 4 );
	}
	else{
		document.formulario[Campo].value = vr;
	}
	
}
function FormataTelOnblurLogin(Campo) {
	vr = document.frmlogin[Campo].value;
	vr = vr.replace( "-", "", "all" );
	tam = vr.length;
	if ( tam == 8){
		document.frmlogin[Campo].value = vr.substr( 0, 4 ) + '-' + vr.substr( 4, 4 );
	}
	else if ( tam == 7){
		document.frmlogin[Campo].value = vr.substr( 0, 3 ) + '-' + vr.substr( 3, 4 );
	}
	else if ( tam == 6){
		document.frmlogin[Campo].value = vr.substr( 0, 2 ) + '-' + vr.substr( 2, 4 );
	}
	else{
		document.frmlogin[Campo].value = vr;
	}
	
}
//////////////////////////////////////////////////////////

//////////////////		VALIDA DATA - DD/MM/YYYY
		
//////////////////////////////////////////////////////////


function func_checkdate(campo)
    {
	var object_value = document.formulario[campo].value;
    //Returns true if value is a date format or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;
	
	//Returns true if value is a date in the dd/mm/yyyy format
	isplit = object_value.indexOf('/');
		
	if (isplit == -1 || isplit == object_value.length)
		return false;

    sDay = object_value.substring(0, isplit);
	isplit = object_value.indexOf('/', isplit + 1);

	if (isplit == -1 || (isplit + 1 ) == object_value.length)
		return false;

    sMonth = object_value.substring((sDay.length + 1), isplit);

	sYear = object_value.substring(isplit + 1);

	if (!func_checkinteger(sMonth)) //check month
		return false;
	else
	if (!func_checkrange(sMonth, 1, 12)) //check month
		return false;
	else
	if (!func_checkinteger(sYear)) //check year
		return false;
	else
	if (!func_checkrange(sYear, 1900, 2079)) //check year
		return false;
	else
	if (!func_checkinteger(sDay)) //check day
		return false;
	else
	if (!func_checkday(sYear, sMonth, sDay)) // check day
		return false;
	else
		return true;
    }
	

function func_checkday(checkYear, checkMonth, checkDay)
    {

	maxDay = 31;

	if (checkMonth == 4 || checkMonth == 6 ||
			checkMonth == 9 || checkMonth == 11)
		maxDay = 30;
	else
	if (checkMonth == 2)
	{
		if (checkYear % 4 > 0)
			maxDay =28;
		else
		if (checkYear % 100 == 0 && checkYear % 400 > 0)
			maxDay = 28;
		else
			maxDay = 29;
	}

	return func_checkrange(checkDay, 1, maxDay); //check day
    }

function func_checkinteger(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;

    //The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)
	return func_checknumber(object_value);
    else
	return false;
    }


function func_numberrange(object_value, min_value, max_value)
    {
    // check minimum
    if (min_value != null)
	{
        if (object_value < min_value)
		return false;
	}

    // check maximum
    if (max_value != null)
	{
	if (object_value > max_value)
		return false;
	}
	
    //All tests passed, so...
    return true;
    }


function func_checknumber(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks

		}
	        else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    //All tests passed, so...
    return true
    }

function func_checkrange(object_value, min_value, max_value)
    {
    //if value is in range then return true else return false

    if (object_value.length == 0)
        return true;

    if (!func_checknumber(object_value))
	{
	return false;
	}
    else
	{
	return (func_numberrange((eval(object_value)), min_value, max_value));
	}
	
    //All tests passed, so...
    return true;
    }
	
function Maximo(campo,num){
	if(campo.value.length >= num){ 
	alert("Este Campo só permite " + num + " caracteres !");
	campo.focus();
	return false;

	}
}


//////////////////////////////////////////////////////////

//////////////////		VALIDA DATA - MM/YYYY
		
//////////////////////////////////////////////////////////

function func_checkdate2(campo)
    {
	var object_value = document.formulario[campo].value;
    //Returns true if value is a date format or is NULL
    //otherwise returns false

    if (object_value.length == 0)
        return true;
	
	//Returns true if value is a date in the mm/yyyy format
	isplit = object_value.indexOf('/');
		
	if (isplit == -1 || isplit == object_value.length)
		return false;

	sMonth = object_value.substring(0, isplit);
	sYear = object_value.substring(isplit + 1);

	if (!func_checkinteger(sMonth)) //check month
		return false;
	else
	if (!func_checkrange(sMonth, 1, 12)) //check month
		return false;
	else
	if (!func_checkinteger(sYear)) //check year
		return false;
	else
	if (!func_checkrange(sYear, 1900, 2079)) //check year
		return false;
	else
		return true;
}

//Permite somente numero no campo
function so_numero(campo){
	 if(isNaN(document.formulario[campo].value) == true && document.formulario[campo].value != ""){
	 	alert("Este campo só pode conter números");
		//document.formulario[campo].value= "";
		document.formulario[campo].focus();
	 }
}

// ** Verifica se CPF ou CGC eh valido

function CGC_OK(Numero_CGC)
{
var Parcela;
var Quociente;
var Resto;
var Soma;
var Fator;
var I;
var C1;
var C2;
var dv1;
var dv2;

//Verificação dos dois digitos finais em relação ao número completo
C1 = parseInt(Numero_CGC.substring(12, 13)); //13º caracter =primeiro dígito verificador
C2 = parseInt(Numero_CGC.substring(13, 14)); //14º caracter = segundo dígito verificador

//Verificação do primeiro dígito (C1)
Soma = 0;
Parcela = 0;
Fator = 0;

for(I=1; I<=12; I++)
{
   if(I < 9)
     Fator = I + 1;
  else
      Fator = I - 7;


Parcela = Fator * parseInt(Numero_CGC.substring(12 - I, 12 - I + 1));
Soma = Soma + Parcela;
} //fechando o "for".

dv1 = (Soma % 11);
dv1 = 11 - dv1;

if(dv1 > 9)
   dv1 = 0;

if(C1 != dv1)
   return false;

//Verificação do segundo dígito (C2)
Soma = 0;
Parcela = 0;
Fator = 0;

for(I=1; I<=13; I++)
{
   if(I < 9)
      Fator = I + 1;
   else
     Fator = I - 7;

Parcela = Fator * parseInt(Numero_CGC.substring(13 - I, 13 - I + 1));
Soma = Soma + Parcela;
}

dv2 = (Soma % 11);
dv2 = 11 - dv2;

if(dv2 > 9)
  dv2 = 0;


if(C2 != dv2)
  return false;

return true;
}




//*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/




function CPF_OK(Numero_CPF)
{
var Parcela;
var Quociente;
var Resto;
var Soma;
var Fator;
var I;
var C1;
var C2;
var dv1;
var dv2;

//Verificação dos dois digitos finais em relação ao número completo
C1 = parseInt(Numero_CPF.substring(9, 10)); //10º caracter = primeiro dígito verificador
C2 = parseInt(Numero_CPF.substring(10, 11)); //11º caracter = segundo dígito verificador

//Verificação do primeiro dígito (C1)
Soma = 0;
Parcela = 0;
Fator = 0;

for(I=1; I<=9; I++)
{
Fator = I + 1;

Parcela = Fator * parseInt(Numero_CPF.substring(9 - I, 9 - I + 1));
Soma = Soma + Parcela;
} //fechando o "for".

dv1 = (Soma % 11);
dv1 = 11 - dv1;

if(dv1 >9 )
    dv1 = 0;


if(C1 != dv1)
   return false;


//Verificação do segundo dígito (C2)
Soma = 0;
Parcela = 0;
Fator = 0;

for(I=1; I<=10; I++)
{
Fator = I + 1;

Parcela = Fator * parseInt(Numero_CPF.substring(10 - I, 10 - I + 1));
Soma = Soma + Parcela;
}

dv2 = (Soma % 11);
dv2 = 11 - dv2;

if(dv2 > 9)
     dv2 = 0;


if(C2 != dv2)
    return false;


return (true);

}

//Testa CNPJ e CPF usando as duas funcoes acima 
function testa_cpf_cgc(nro,campo){
	if(nro.length == 11){
		if(CPF_OK(nro) == false){
			alert('Número de CPF inválido !');
			document.formulario.all[campo].focus();
		}
	}else{
		if(nro.length == 14){
			if(CGC_OK(nro) == false){
				alert('Número de CNPJ inválido !');
				document.formulario.all[campo].focus();
			}
		}else{
			if(nro.length == 0){
				return true;
			}else{
				alert('O número do CPF é contituído de 11 dígitos!\nO CNPJ de 14 dígitos!');
				document.formulario.all[campo].focus();
				return false;
			}
						  
		}
	}
}
function conf_data(campo){
		if (!func_checkdate(campo)){ //check date
			alert("A data tem que ser no formato dd/mm/aaaa !");
			document.formulario[campo].focus();
		}
}

 function Adicionar_Lista(){
	var i; 
	var j;
	var contem = 0;
	var lst_para = document.formulario.motivo_devolucao;
	var lst_de = document.formulario.sel_motivo;
	var valor ;
	var texto ;
	
	for(i=0 ; i < lst_de.length ; i++){
		contem = 0;
		if(lst_de.options[i].selected == true){
				
			valor = lst_de.options[i].value;
			texto = lst_de.options[i].text;
	
			for(j=0 ; j < lst_para.length ; j++){
				if(lst_para.options[j].value == valor){
					alert(texto + " já está na lista");
					contem = 1
				}
			} 

			if(contem == 0){
				lst_para.length = lst_para.length + 1;
				lst_para.options[lst_para.length - 1].text = texto;
				lst_para.options[lst_para.length - 1].value = valor;
			}
		}
	}
}
	

function Confirma_Exclusao(){
	if(confirm("Deseja excliuir o(s) item(ns) selecionado(s) ?") == true){
		Excluir_Lista();
	}
}


function Excluir_Lista(){
	var i;
	var lst_para = document.formulario.telefone_usuario;
	
	for(i=0 ; i <= lst_para.length ; i++){
		if(lst_para.length == i){
			return false;
			break;
		}else{
			if(lst_para.options[i].selected == true){
				lst_para.options[i] = null;
				Excluir_Lista();
			}
		}
	}
}


function Adicionar_Combo(){

	var i;
	var texto = "";
	var lst_para = document.formulario.telefone_usuario;
	

	var caixa_de2 = document.formulario.nro_ddd;
	var caixa_de3 = document.formulario.nro_telefone;
	//var caixa_de4 = document.formulario.nro_ramal;
	var lst_de = document.formulario.tpo_telefone;
	

	//var valor = caixa_de2.value + "@" + caixa_de3.value + "@" + caixa_de4.value + "@" + lst_de.options[lst_de.selectedIndex].value;
	
	var valor = caixa_de2.value + "@" + caixa_de3.value + "@" + lst_de.options[lst_de.selectedIndex].value;
	
	
	texto = texto + "(" + caixa_de2.value + ") " + caixa_de3.value;
	
	//if(caixa_de4.value.length > 0){
		//texto = texto + " Ext. " + caixa_de4.value;
	//}
	
	texto = texto + " - " + lst_de.options[lst_de.selectedIndex].text
	
	
	if(caixa_de2.value == ""){
		alert("O DDD é obrigatório !");
		caixa_de2.focus();
		return false;
	}
	if(caixa_de3.value == ""){
		alert("O Telefone é obrigatório !");
		caixa_de3.focus();
		return false;
	}
	
	
	
	if(valor != ""){
		for(i=0 ; i < lst_para.length ; i++){
			if(lst_para.options[i].value == valor){
				alert(texto + " já está na lista.");
				return false;
			}
			if(lst_de.options[lst_de.selectedIndex].text == "Principal"){
				if(lst_para.options[i].text.indexOf("Principal") >= 0){
					alert("Só é permitido o cadastro de 1 (um) telefone Principal.");
					return false;
				}
				
				
			}
			
			
		} 
			lst_para.length = lst_para.length + 1;
			lst_para.options[lst_para.length - 1].text = texto;
			lst_para.options[lst_para.length - 1].value = valor;
			caixa_de2.value = "";
			caixa_de3.value = "";
			//caixa_de4.value = "";
	}
}

function Maximo(campo){
	if(campo.value.length > 255){ 
	alert("Este Campo só permite 255 caracteres !");
	campo.focus();
	return false;

	}
}

function mudacor(name){      
	document.all[name].style.backgroundColor="#ffffff";
  }  
 function normalcor(name){
	document.all[name].style.backgroundColor="#eaf0f9";    
  }  