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)
{ 
	// se houver "." ou "-" devem ser retirados
	limpo = ""
	
	for(i = 0; i < cpf.length; i++)
	{
		if ((cpf.charAt(i) != '.') && (cpf.charAt(i) != '-'))
		{
			limpo += cpf.charAt(i);
		}
	}

	cpf = limpo;

	// cpf pronto para ser validado: apenas os números
	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 CNPJ digitado 
function ValidarCNPJ(cnpj)
{ 
	// se houver "." ou "-" ou "/" devem ser retirados
	limpo = ""
	
	for(i = 0; i < cnpj.length; i++)
	{
		if ((cnpj.charAt(i) != '.') && (cnpj.charAt(i) != '-') && (cnpj.charAt(i) != '/'))
		{
			limpo += cnpj.charAt(i);
		}
	}

	cnpj = limpo;
	
	// 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(dia, mes, ano) 
{
	var dataVerificada = new Date();
	var blnRet = false;
	var diaOK;
	var mesOK;
	var anoOK;

	dataVerificada.setFullYear(ano, mes -1, dia);

	diaOK = (dataVerificada.getDate()     == dia);
	mesOK = (dataVerificada.getMonth()    == mes -1);
	anoOK = (dataVerificada.getFullYear() == ano);

	if (diaOK && mesOK && anoOK)
	{
		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(novaDataIni))-(Date.parse(novaDataFim)))/(24*60*60*1000)).toFixed(0);
	
	return(diasEntreDatas);	
}

