function TrimJS(controle){
	str = controle.value;
    Resultado = str;
    
    var i;
    i = 0;

    while (Resultado.charCodeAt(0) == '32'){
       Resultado = str.substring(i,str.length);
      i++;}
    
    while(Resultado.charCodeAt(Resultado.length-1) == "32"){
       Resultado = Resultado.substring(0,Resultado.length-1);
      }
    
    return Resultado;
}


function right(e) {
	if (navigator.appName == 'Netscape' && 
		(e.which == 3 || e.which == 2))
		return false;
	else if (navigator.appName == 'Microsoft Internet Explorer' && 
		(event.button == 2 || event.button == 3)) {
		alert("Função Desabilitada!");
		return false;
	}
return true;
}

/*
document.onmousedown=right;
if (document.layers) window.captureEvents(Event.MOUSEDOWN);
window.onmousedown=right;
*/



function ValidarEmail(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		var i
		
		if(str.charAt(0) == at || str.charAt(lstr-1) == at || str.charAt(0) == dot || str.charAt(lstr-1) == dot){
		   return false;
		}		 
		
		if (str.indexOf(at)==-1){
		   return false;
		}
		
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}
		
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr-1){
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false;
		 }

     	 for (var i = 0; i < str.length; i++)
     	 {
          	if (str.charAt(i) == ",")
          	{
          	   return false;
     		   break;
          	}
     	 }
		  
 		 return true					
	}

	
//valida telefone 
function ValidaTelefone(tel){ 
        exp = /\(\d{2}\)\ \d{4}\-\d{4}/ 
        if(!exp.test(tel.value))
		{ 
            return false;
		}
		else
		{
		    return true;
		} 
} 
 
//valida CEP 
function ValidaCep(cep){ 
        cep = cep.replace("-","")    
        cep = cep.replace(".","")    

		if (cep.length != 8)
		{
		   return false;
		}
		else
		{
		 	if (isFinite(cep) == false)
			{
		   	   return false;
			}
			else
			{
			 	return true;
			}
		}
} 

//valida o CPF digitado 
function ValidarCPF(cpf)
{ 
	// retira "." e "-"
	cpf = cpf.replace(".","");
	cpf = cpf.replace("-","");
	cpf = cpf.replace(".","");

	if (cpf.length != 11)
	{
		return false;
	}
	
	if ((cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"))
	{
		return false;
	}
	
	pos1 = cpf.substring(0,1); 
	pos2 = cpf.substring(1,2); 
	pos3 = cpf.substring(2,3); 
	pos4 = cpf.substring(3,4); 
	pos5 = cpf.substring(4,5); 
	pos6 = cpf.substring(5,6); 
	pos7 = cpf.substring(6,7); 
	pos8 = cpf.substring(7,8); 
	pos9 = cpf.substring(8,9); 
	pos10 = cpf.substring(9,10); 
	pos11 = cpf.substring(10,11); 

	primDigito = (pos1*10) + (pos2*9) + (pos3*8) + (pos4*7) + (pos5*6) + (pos6*5) + (pos7*4) + (pos8*3) + (pos9*2);
	primDigito = primDigito % 11;

	if (primDigito < 2)
	{
		primDigito = 0;
	}
	else
	{
		primDigito = 11 - primDigito;
	}
	
	seguDigito = (pos1*11) + (pos2*10) + (pos3*9) + (pos4*8) + (pos5*7) + (pos6*6) + (pos7*5) + (pos8*4) + (pos9*3) + (primDigito*2);
	seguDigito = seguDigito % 11;
	if (seguDigito < 2)
	{
		seguDigito = 0;
	}
	else
	{
		seguDigito = 11 - seguDigito;
	}
	
	if ((primDigito != pos10) || (seguDigito != pos11))
	{
		return false;
	}
	else
	{
		return true;
	}                   
} 


//valida o RG digitado 
function ValidarRG(rg)
{ 
	// retira "." e "-"
	rg = rg.replace(".","");
	rg = rg.replace("-","");
	rg = rg.replace(".","");

	if (rg.length != 9)
	{
		return false;
	}	

	// obtendo cada número do RG // 
	pos1 = rg.substring(0,1); 
	pos2 = rg.substring(1,2); 
	pos3 = rg.substring(2,3); 
	pos4 = rg.substring(3,4); 
	pos5 = rg.substring(4,5); 
	pos6 = rg.substring(5,6); 
	pos7 = rg.substring(6,7); 
	pos8 = rg.substring(7,8); 
	pos9 = rg.substring(8,9);
	
	if ((pos9 == "x") || (pos9 == "X"))
	{
		pos9 = 10;
	}
	
	soma = (pos1*2) + (pos2*3) + (pos3*4) + (pos4*5) + (pos5*6) + (pos6*7) + (pos7*8) + (pos8*9) + (pos9*100)
	soma = soma % 11;

	if (soma == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
} 


//valida o CNPJ digitado 
function ValidarCNPJ(cnpj)
{ 
	// obtendo cada número do cnpj // 
	pos1 = cnpj.substring(0,1); 
	pos2 = cnpj.substring(1,2); 
	pos3 = cnpj.substring(2,3); 
	pos4 = cnpj.substring(3,4); 
	pos5 = cnpj.substring(4,5); 
	pos6 = cnpj.substring(5,6); 
	pos7 = cnpj.substring(6,7); 
	pos8 = cnpj.substring(7,8); 
	pos9 = cnpj.substring(8,9); 
	pos10 = cnpj.substring(9,10); 
	pos11 = cnpj.substring(10,11); 
	pos12 = cnpj.substring(11,12); 
	pos13 = cnpj.substring(12,13); 
	pos14 = cnpj.substring(13,14); 

	primDigito = (pos1*5) + (pos2*4) + (pos3*3) + (pos4*2) + (pos5*9) + (pos6*8) + (pos7*7) + (pos8*6) + (pos9*5) + (pos10*4) + (pos11*3) + (pos12*2);
	primDigito *= 10;
	primDigito = primDigito % 11;
	if (primDigito == 10)
	{
		primDigito = 0;
	}

	seguDigito = (pos1*6) + (pos2*5) + (pos3*4) + (pos4*3) + (pos5*2) + (pos6*9) + (pos7*8) + (pos8*7) + (pos9*6) + (pos10*5) + (pos11*4) + (pos12*3) + (primDigito*2);
	seguDigito *= 10;
	seguDigito = seguDigito % 11;
	if (seguDigito == 10)
	{
		seguDigito = 0;
	}

	if ((primDigito != pos13) || (seguDigito != pos14))
	{
		return false;
	}
	else
	{
		return true;
	}
} 




//valida a data digitada (recebe os valores, sem "/" ou "-"
function ValidarData(day, month, year) 
{
	var date = new Date();
	var blnRet = false;
	var blnDay;
	var blnMonth;
	var blnYear;

	date.setFullYear(year, month -1, day);

	blnDay   = (date.getDate()      == day);
	blnMonth = (date.getMonth()     == month -1);
	blnYear  = (date.getFullYear()  == year);

	if (blnDay && blnMonth && blnYear)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function CalcularDiferencaDatas(diaIni, mesIni, anoIni, diaFim, mesFim, anoFim)
{	
	var mes, dataIni, dataFim, arrDataIni, arrDataFim, novaDataIni, novaDataFim, diasEntreDatas;

	mes = [];
	mes[0] = "January";
	mes[1] = "February";
	mes[2] = "March";
	mes[3] = "April";
	mes[4] = "May";
	mes[5] = "June";
	mes[6] = "July";
	mes[7] = "August";
	mes[8] = "September";
	mes[9] = "October";
	mes[10] = "November";
	mes[11] = "December";

	dataIni = diaIni + "/" + mesIni + "/" + anoIni;
	arrDataIni = dataIni.split('/');
	novaDataIni = mes[(arrDataIni[1] - 1)] + ' ' + arrDataIni[0] + ' ' + arrDataIni[2];
	
	dataFim = diaFim + "/" + mesFim + "/" + anoFim;
	arrDataFim = dataFim.split('/');
	novaDataFim = mes[(arrDataFim[1] - 1)] + ' ' + arrDataFim[0] + ' ' + arrDataFim[2];
		
	diasEntreDatas = (((Date.parse(dataIni))-(Date.parse(dataFim)))/(24*60*60*1000)).toFixed(0);
	
	return(diasEntreDatas);	
}

function verificaCampo(campo, verQtdMinCaracteres, verNumerico, verNaoNumerico, verNumeroInteiro, verEstilo)
{
	if (verQtdMinCaracteres > 0) 
	{		
		if (campo.length < verQtdMinCaracteres) 
		{
			return "Insira, ao menos, " + verQtdMinCaracteres + " caracteres";
		}
	}
	
	if (verNumerico == true) 
	{		
		if (isFinite(campo)==false)
		{
			return "Insira apenas valores numéricos";
		}
	}

	if (verNaoNumerico == true) 
	{		
		if (isFinite(campo))
		{
			return "Insira apenas valores não-numéricos";
		}
	}

	if (verNumeroInteiro == true) 
	{		
		for (var i = 0; i < campo.length; i++)
		{
			if ((campo.charAt(i) == ".") || (campo.charAt(i) == ",")) 
			{
				return "Insira apenas valores inteiros";
				break;
			}
		}	
	}	

	if (verEstilo == 'email') 
	{		
		if (ValidarEmail(campo) == false)
		{
			return "Insira um endereço de e-mail válido";
		}	
	}	
	else if (verEstilo == 'telefone') 
	{		
		if (ValidaTelefone(campo) == false)
		{
			return "Insira um número de telefone válido";
		}	
	}	
	else if (verEstilo == 'cep') 
	{		
		if (ValidaCep(campo) == false)
		{
			return "Insira um CEP válido";
		}	
	}	
	else if (verEstilo == 'rg') 
	{		
		if (ValidarRG(campo) == false)
		{
			return "Insira um RG válido";
		}	
	}		
	else if (verEstilo == 'cpf') 
	{		
		if (ValidarCPF(campo) == false)
		{
			return "Insira um CPF válido";
		}	
	}		
	else if (verEstilo == 'cnpj') 
	{		
		if (ValidarCNPJ(campo) == false)
		{
			return "Insira um CNPJ válido";
		}	
	}		
	else if (verEstilo == 'data') 
	{		
		if (ValidarData(campo) == false)
		{
			return "Insira uma data válida";
		}	
	}		
	else if (verEstilo == 'select') 
	{		
		if (campo.length == 0) 
		{
			return "Selecione uma opçao";
		}
	}	
	return "";
}






