/* 
#  $Header: /aplic/ITED/src/repository/aplic/ITED/www/htdocs/common/scripts/email.js,v 1.6 2002-04-10 14:41:18-03 b918145 Beta $
#  © Copyright HSBC Bank Brasil S.A. - Banco Múltiplo - All rights reserved
#  IT E-Channels E-Publishing
#  Description : Validar email.
#  Author      : LUCIANO MELEXENCO RIBAS
*/
function isEmail(email)
{
	//alert(email);
	var v=trim(email);
	// exp1: Trata erros grosseiros (@...@ , .. , .@ , etc.)
	// exp2: Garante carac. validos e estrutura: <usuario>@<maquina>
	// exp3: Garante no minimo um ponto depois do "@" 
	var exp1= /(\@.*\@)|(.*\.\..*)|(.*\@\..*)|(^\.)|(\.$)|(\@\/)|(.*\@\-.*)|(.*\.$)/;
	var exp2= /^[_\w\d][\w\d\_\/\-\.]*\@[\d\w\-\.]+[0-9A-z]$/;
	var exp3= /.*\@.*[\.].*/;
	return(!exp1.test(v)&& exp2.test(v)&& exp3.test(v));
}
/* 
#  $Header: /aplic/ITED/src/repository/aplic/ITED/www/htdocs/common/scripts/date.js,v 1.11 2002-01-07 11:19:27-02 b918145 EP $
#  © Copyright HSBC Bank Brasil S.A. - IT E-Channels E-Publishing
#  Description : Validacao de datas e funcoes uteis para utilizacao das mesmas
#  Author      : LUCIANO MELEXENCO RIBAS
*/
function DateValidation(d){
	this.dtSrc=d;
	this.dtValue="";
	this.isDate=_isDate;
	this.getDateValue=function() {return(this.dtValue);};
	this.getMonthDateValue=function() {return(this.dtValue.slice(3));} 
}
function _isDate(){
	var vrs=/^(0[1-9]|[1-2][0-9]|3[0-1])(0[1-9]|1[0-2])(\d{2}|19\d{2}|20\d{2})$/.exec(justNumbersStr(this.dtSrc));
	if(!vrs || vrs.length<4)return false;
	var d=parseInt(vrs[1],10),m=parseInt(vrs[2],10),a=parseInt(vrs[3],10);		
	if(a<100)a+=(a<30?2000:1900);
	if(/^(4|6|9|11)$/.test(m) && d==31)return false;
	if(m==2){
		var bissexto=(((a%4==0)&&a%100!=0)||a%400==0);
		if(d>29 ||(d==29 && !bissexto))return false;
	}
	this.dtValue=repeatStr(d,"0",2)+"/"+repeatStr(m,"0",2)+"/"+a;
	return true;
}
function DateObj(d){
	d=trim(d);
	if(!d)return;
	var t=d.length;
	if(t==10||t==8||t==6){
		this.isValid=true;
		this.srcDate=d;
		this.date=d.replace(/\//g,"");
		this.day=this.date.slice(0,2);
		this.month=this.date.slice(2,4);
		this.year=this.date.slice(4);
		var a=parseInt(this.year,10);
		if(a<100){a+=(a<30?2000:1900);this.year=String(a);}

		this.daysTo=_DaysTo;
		this.lesserThan=function(d){return Number(this.year+this.month+this.day) < Number(d.year+d.month+d.day)};
		this.biggerThan=function(d){return !this.lesserThan(d)&& !this.equal(d)};
		this.equal=function(d){return this.date==d.date.replace(/\//g,"");};
		this.biggerOrEqualThan=function(d){return this.biggerThan(d)||this.equal(d)};
		this.lesserOrEqualThan=function(d){return this.lesserThan(d)||this.equal(d)};
	}else this.isValid=false;
}
function _DaysTo(d){
	var msDay=24*60*60*1000;
	var s=new Date(this.month+"/"+this.day+"/"+this.year);
	var f=new Date(d.month+"/"+d.day+"/"+d.year);
	return Math.floor((f.getTime()-s.getTime())/msDay);
}
function isInInterval(dtIn,dtFi,pIn,pFi,msg1,msg2,msg3){
	var iDt=new DateObj(dtIn),fDt=new DateObj(dtFi);
	var iPer=new DateObj(pIn),fPer=new DateObj(pFi);
	if(!msg1||msg1=="")msg1='Data inicial maior que a data final. Digite novamente.';
	if(!msg2||msg2=="")msg2='Data inicial fora do período disponível. Digite novamente.';
	if(!msg3||msg3=="")msg3='Data final fora do período disponível. Digite novamente.';
	if(iDt.isValid&&fDt.isValid&&iPer.isValid&&fPer.isValid){
		if(fDt.lesserThan(iDt)){alert(msg1);return false;}
		else if(iDt.lesserThan(iPer)){alert(msg2);return false;}
		else if(fPer.lesserThan(fDt)){alert(msg3);return false;}
		return true;
	}
}
function isInDaysLimit(dtIn,dtFi,dias,msg){
	if(!msg)msg="O intervalo entre as datas não pode ultrapassar "+dias+(dias>1?" dias":" dia")+". Digite novamente.";
	var sDt=new DateObj(dtIn),fDt=new DateObj(dtFi);
	if(sDt.isValid && fDt.isValid){
		if((sDt.daysTo(fDt)+1)>dias){alert(msg);return false;}
		return true;
	}
}
/* 
#  $Header: /aplic/ITED/src/repository/aplic/ITED/www/htdocs/common/scripts/cpf.js,v 1.3 2001-09-11 14:44:49-03 b918145 EP $
#
#  HSBC Bank Brasil S.A. - IT E-Channels E-Publishing
#  Description : Validacao de CPF
#  Author      : LUCIANO MELEXENCO RIBAS
#  Created     : Tue Jul 24 14:33:59 AST 2001
*/
function isCPF(cpf)
{
	var OK;
	cpf= justNumbersStr(trim(cpf));
	if(onlySameNumber(cpf)) return false;
	var size=cpf.length;
	if(size>10)
	{
		var vr=cpf.substring(0,size-2)
		var resto= getVerificationDigit(vr);
		OK= resto==parseInt(cpf.charAt(size-2));
		if(OK)
		{
			vr+=resto;
			resto=getVerificationDigit(vr);
			OK= resto==parseInt(cpf.charAt(size-1));
		}
	}
	return OK;
}

function getVerificationDigit(S)
{
	var s=0,i;
	var inv=invertStr(justNumbersStr(S));
   for(i=0;i<inv.length;i++)
        s+=(i+2)*parseInt(inv.charAt(i));
   s*=10;
   return (s%11)%10;
}
/* 
#  $Header: /aplic/ITED/src/repository/aplic/ITED/www/htdocs/common/scripts/cnpj.js,v 1.3 2001-09-11 14:44:40-03 b918145 EP $
#
#  HSBC Bank Brasil S.A. - IT E-Channels E-Publishing
#  Description : Validacao de CNPJ
#  Author      : LUCIANO MELEXENCO RIBAS
#  Created     : Tue Jul 24 14:33:30 AST 2001
*/

function isCNPJ(cnpj) 
{
	if(cnpj.length==0) return false;
	cnpj= trim(cnpj);
	var digs=[],i;
	for(i=0; i<14; i++)
		digs[i]= parseInt(cnpj.charAt(i),10);
	var sDig=0,soma=0,resto=0,dVer1=-1,dVer2=-1;
	var fat1=[5,4,3,2,9,8,7,6,5,4,3,2];
	var fat2=[6,5,4,3,2,9,8,7,6,5,4,3,2];
	for(var i=0; i<12; i++)
		sDig+= (digs[i]*fat1[i]);
	resto= sDig % 11;
	dVer1= (resto==0)?0:(11 - resto)%10;
	if(digs[12]==dVer1) 
	{
		sDig=resto=0;
		for(i=0;i<13;i++) 
			sDig+= (digs[i]*fat2[i]);
		resto=sDig%11;
		dVer2=(resto==0)?0:(11-resto)%10;
	}
	return digs[12]==dVer1 && digs[13]==dVer2;
}
function isCEP(cep)
{
	var OK;
	cep= justNumbersStr(trim(cep));
	if(onlySameNumber(cep)) return false;
	var size=cep.length;
	if(size !=8)
	{
	  return false;
	}
	else
	{
		return true;
	}
}
