var objBrow,LAST_ERR_VALUE="";  
var ERRO=REPET_ERR=false;
var LAST_FIELD=CURRENT_FIELD=LAST_ERR_FIELD=null;
var SZ_DATE=8,
SZ_CEP=8,
SZ_ACCOUNT=7,
SZ_AG_ACCOUNT=11,
SZ_AG_SAVINGS=SZ_AG_ACCOUNT, 
SZ_MONEY=10,
SZ_FLOAT=10,
SZ_CPF=11,
SZ_CNPJ=14,
SZ_CPF_CNPJ=SZ_CNPJ,
SZ_PERCENT=6,
SZ_BRANCH=4,
SZ_MONTH_YEAR=6,
SZ_TIME=4,
MAX_VALUE=9999999.99;

function brow(){
	if(typeof objBrow!="object")objBrow=new Browser();
	return objBrow;
}

function isValidValue(vr,tp,adarg1,adarg2){
	var re,isNum=isNumeric(vr);
	if(/^(ag_account|ag_savings|account|branch|cep)$/.test(tp))
		return isNum && vr.length==eval("SZ_"+tp.toUpperCase());
	else if(/^(percent|float(\d{0,1})|money(\d{0,1}))$/.test(tp))
		return isFloatNumber(vr);
	else if(/textnumber$/.test(tp))
		return isTextNumber((tp.indexOf("sp_")==0)?removeStr(vr," "):vr);
	else if(/alfanumeric$/.test(tp)){
		return isAlfaNumeric((tp.indexOf("sp_")==0)?removeStr(vr," "):vr);
	}else{
		switch(tp){
			case "time":
				switch(vr.length){
					case 1:vr="0"+vr+"00";break;
					case 2:vr+="00";break;
					case 3:vr="0"+vr+"0";break;
				}
				vr=repeatStr(vr,"0",4,"right");
				return(isNum && /^([0-1]\d[0-5]\d)|(2[0-3][0-5]\d)$/.test(vr))?vr:null;
			case "date":
				var obj=new DateValidation(vr);
				return (isNum && obj.isDate())?obj:null;
			case "month_year":
				var obj=new DateValidation("01"+vr);
				return (isNum && obj.isDate())?obj:null;
			case "text":return isAlfa(vr.replace(/[ ]/g,""));
			case "email":return isEmail(vr);
			case "cpf":return isCPF(vr);
			case "cnpj":return isCNPJ(vr);
			case "cpf_cnpj":return (vr.length <=SZ_CPF)?isCPF(vr):isCNPJ(vr);
			case "interval":
				vr=parseInt(vr,10);
				return vr>=adarg1 && vr<=adarg2;
			case "default":return !/'|"/.test(vr);
			default:return true;
		}
	}
}

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 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;
}

function formatType(f,tp,msgErr,adarg1,adarg2){
	var vr=unformatField(f.value,tp);
	LAST_FIELD=f;
	var ret=isValidValue(vr,tp,adarg1,adarg2);
	if(!ret){
		showError(tp,msgErr);
		return false;
	}else f.value=getFmtValue((typeof ret=="boolean")?vr:ret,tp);
	ERRO=false;
	return true;
}

function showError(type,msgU){
	ERRO=true;
	var b=brow();
	var canShow=true;
	if(typeof type=="object"){
		alert(msgU);
		focusCamp(type);
		return;
	}
	if(b.isIE() && parseInt(b.getMajorver(),10)<5){
		if(CURRENT_FIELD && LAST_ERR_FIELD==CURRENT_FIELD && LAST_ERR_VALUE==CURRENT_FIELD.value){
			REPET_ERR=true;
			CURRENT_FIELD.value=LAST_ERR_VALUE="";
			LAST_ERR_FIELD=null;
			canShow=false;
		}else{
			LAST_ERR_FIELD=CURRENT_FIELD;
			LAST_ERR_VALUE=(CURRENT_FIELD?CURRENT_FIELD.value:null);
			REPET_ERR=false;
		}
	}
	if(canShow){
		var m=". Digite novamente.";
		switch(type){
			case "date":msg="Data inválida"+m;break;
			case "time":msg="Hora inválida"+m;break;
			case "ag_account":msg="Agência/Conta corrente incorretos"+m;break;
			case "ag_savings":msg="Agência/Conta poupança incorretos"+m;break;
			case "branch":msg="Agência inválida"+m;break;
			case "account":msg="Conta corrente inválida"+m;break;
			case "cep":msg="CEP inválido"+m;break;
			case "email":msg="E-mail incorreto"+m;break;
			case "cpf":msg="CPF inválido"+m;break;
			case "cnpj":msg="CNPJ inválido"+m;break;
			case "cpf_cnpj":msg="CPF/CNPJ inválido"+m;break;
			case "month_year":msg="Mês e ano inválidos"+m;break;
			default:msg="Valor inválido"+m;
		}
		alert((!msgU || msgU=="")?msg:msgU);
	}
	if(brow().isNetscape())LAST_FIELD.value="";
	if(LAST_FIELD)LAST_FIELD.focus();
}

function getFmtValue(vr,tp){
	if(tp=="cpf_cnpj")tp=(vr.length <=SZ_CPF)?"cpf":"cnpj";
	if(/^money/.test(tp))return fmtMoney(vr,_getNDec(tp));
	else{
		switch(tp){
			case "time":return vr.slice(0,2)+":"+vr.slice(2,4); 
			case "date":return vr.getDateValue();
			case "month_year":return vr.getMonthDateValue();
			case "ag_account":return vr.slice(0,4)+"-"+vr.slice(4,9)+"-"+vr.slice(9,11);
			case "ag_savings":return vr.slice(0,4)+"-"+vr.slice(4,10)+"-"+vr.slice(10,11);
			case "account":return vr.slice(0,5)+"-"+vr.slice(5,7);
			case "cep":return vr.slice(0,5)+"-"+vr.slice(5,8);
			case "uppercase":return vr.toUpperCase();
			case "cpf":return vr.slice(0,3)+"."+vr.slice(3,6)+"."+ vr.slice(6,9)+ "-"+vr.slice(9,11);
			case "cnpj":return vr.slice(0,2)+"."+vr.slice(2,5)+"."+vr.slice(5,8)+"/"+vr.slice(8,12)+"-"+vr.slice(12,14);
			default:return vr;
		}
	}
}

function Browser(){
	this.name=this.platform="Unknown";
	this.majorver=this.version=this.minorver="";
	this.init=_Init;
	this.getName=function (){return this.name};
	this.getMinorver=function (){return this.minorver};
	this.getMajorver=function (){return this.majorver};
	this.getVersion=function (){return parseFloat(this.version,10)};
	this.getPlatform=function (){return this.platform}; 
	this.isIE=function (){return(this.name=="IE")};
	this.isNetscape=function (){return(this.name=="Netscape")};
	this.isWindows=function (){return _has(this.platform,["Windows","WinNT"])};
	this.isWinNT=function (){return _has(this.platform,["WinNT","Windows NT"])};
	this.isWin95=function (){return _has(this.platform,["Win95","Windows 95"])};
	this.isWin98=function (){return _has(this.platform,["Win98","Windows 98"])};
	this.isLinux=function (){return _has(this.platform,"Linux")};
	this.isMac=function (){return _has(this.platform,"Mac");};
	this.init();
}
function _Init(){
	var ua=navigator.userAgent,t="",ts="",i,bv;
	bv=ua.slice(0,ua.indexOf("("));
	ts=ua.slice(ua.indexOf("(")+1,ua.indexOf(")")).split(";");
	for(i=0;i<ts.length;i++){
		t=ts[i].trim();
		if(_has(t,["MSIE","Opera"]))bv=t;
		else if(_has(t,["X11","SunOS","Linux"]))this.platform="Unix";
		else if(_has(t,["Mac","PPC","Win"]))this.platform=t;
	}
	var idx=bv.indexOf("MSIE"),lo="";
	if(idx>=0)bv=bv.slice(idx);
	if(bv.slice(0,7)=="Mozilla"){
		this.name="Netscape";lo=bv.slice(8);
	}else if(bv.slice(0,4)=="Lynx"){
		this.name="Lynx";lo=bv.slice(5);
	}else if (bv.slice(0,4)=="MSIE"){
		this.name="IE";lo=bv.slice(5);
	}else if (bv.slice(0,27)=="Microsoft Internet Explorer"){
		this.name="IE";lo=bv.slice(28);
	}else if (bv.slice(0,5)=="Opera"){
		this.name="Opera";lo=bv.slice(6);
	}
	lo=lo.trim();
	i=lo.indexOf(" ");
	if(i>=0)this.version=lo.slice(0,i);
	else this.version=lo;
	j=this.version.indexOf(".");
	if(j>=0){
		this.majorver=this.version.slice(0,j);
		this.minorver=this.version.slice(j+1);
	}else this.majorver=this.version;
}
function _has(s,a){
	s=String(s);
	if(typeof(a)=="string")return s.indexOf(a)!=-1;
	else{
		for(var i=0;i<a.length;i++)
			if(s.indexOf(a[i])!=-1) return true;
		return false;
	}
}
function _TRIM(){
	var s=0,e=this.length;
	while(s<e&&this.charAt(s)==' ')s++;
	while(e>0&&this.charAt(e-1)==' ')e--;
	return this.slice(s,e);
}
String.prototype.trim=_TRIM;
function BANESTESAlert(msg){parent.alert("BANESTES  \n\n"+msg);} 
//if(parent.frames&&parent.frames.length>0)window.alert=BANESTESAlert;

function isAnchor(){
	if(window.currentLink){
		var obj=window.currentLink;
		if(obj.tagName=="IMG")obj=obj.parentElement;
		if(obj.tagName=="A"){
			var locAnchor=obj.href.split('#')[0],
			locWin=location.href.split('#')[0],
			re=new RegExp('Ap{1,2}lic');
			if(locAnchor==locWin ||re.test(obj.mimeType))return true;
		}
	}
	window.currentLink=null;
	return false;
}
function testAnchor(){if(window.event&&window.event.srcElement)window.currentLink=window.event.srcElement;}
function popup(url,j,atr){var w= window.open(url,j,removeStr(atr," "));}
function random(r1,r2){return Math.round(Math.random()*(r2-r1))+ (r2>r1?r1:r2);}
function trim(s){return String(s).replace(/^\s+/,"").replace(/\s+$/,"");}
function autoSkip(field,orient){
	var ind=-1,f=field.form;
	for(i=0;i<f.elements.length;i++)
		if(field==f.elements[i]){ind=i;break;}
	focusCampByPos(f,ind,orient);
}
function autoFocus(f){focusCampByPos((arguments.length==0?document.forms[0]:f),-1);}
function focusCampByPos(fr,ind,orient){
	orient=orient?orient:"down";
	var iNext=(orient=="down"?1:-1),el;
	if((typeof fr.elements[ind+iNext])=="undefined"){
      if(ind!=-1)if(fr.elements[ind]&&fr.elements[ind].blur)fr.elements[ind].blur();
		return;
   }
	for(var i=ind+iNext;i<fr.elements.length;i+=iNext){
		el=fr.elements[i];
		if(/^(text|password|select.*|radio|checkbox.*)$/.test(el.type) && !el.disabled){el.focus();return;}
   }
	if(fr.elements[ind]&&fr.elements[ind].blur)fr.elements[ind].blur();
}
function isNumeric(v){return /^[0-9]+$/.test(v);}
function isAlfa(v){return /^[a-zA-ZáéíóúçãõâêôàÁÉÍÓÚÇÃÕÂÊÔÀ]+$/.test(v);}
function isAlfaNumeric(v){return /^[0-9a-zA-Z]+$/.test(v);}
function invertStr(s){
	var t="",i;
	for(i=0;i<s.length;i++)t=s.charAt(i)+t;
	return t;
}
function removeStr(src,arg){
	var v=(typeof arg=="string")?[arg]:arg;
	var r="";
	for(var i=0;i<v.length;i++)r=changeStr(src,v[i],"");
	return r;
}
function repeatStr(src,str,size,orient){
	var r=String(src);
	if(!orient)orient="left";
	while(r.length < size)r=orient.toLowerCase()=="right"?(r+str):(str+r);
	return r;
}
function changeStr(src,from,to)
{
	src=String(src);
	var i,li=0,lFrom= from.length,dst="";
	while((i=src.indexOf(from,li))!=-1){
		dst+=src.substring(li,i)+to;
		li=i+lFrom;
	}
	dst+=src.substring(li);
	return dst;
}
function justNumbersStr(s){return String(s).replace(/\D*/g,"");}
function onlySameNumber(s){return isNumeric(s)&& (new RegExp("^("+s.charAt(0)+")(\\1)*$")).test(s);}

function _SPLT(c){
   var r=[],t="",i,l=this.length;
   if(l==0)return null;
   for(i=0;i<l;i++){
      var ch=this.slice(i,i+1);
      if(ch==c){r.push(t);t="";}
      else t+=ch;
   }
   r.push(t);
   return r;
}
function _PSH(i){this[this.length]=i;return this.length;}
function _POP(){
	l=this[this.length-1];
	this.length=Math.max(this.length-1,0);
	return l;
}
function _SPLI(ind,c){
	var t=arguments.length,i;
	if(t==0)return ind;
	if(typeof ind!="number")ind=0;
	if(ind<0)ind=Math.max(0,this.length+ind);
	if(ind>this.length){
		if(t>2)ind=this.length;
		else return [];
	}
	if(t<2)c=this.length-ind;
	c=(typeof c=="number")?Math.max(0,c):0;
	rmv=this.slice(ind,ind+c);
	end=this.slice(ind+c);
	this.length=ind;
	for(i=2;i<t;i++)this[this.length]=arguments[i];
	for(i=0;i<end.length;i++)this[this.length]=end[i];
	return rmv;
}

function _SHF(){var r=this[0];this.splice(0,1);return r;}
Array.prototype.shift=_SHF;
Array.prototype.splice=_SPLI;
Array.prototype.pop=_POP;
Array.prototype.push=_PSH;
String.prototype.split=_SPLT;
var tIDP,_TimeP=5500,_Win,ObjP=null,_PEND,_Brow=new Browser();
var _IMAC=_Brow.isMac()&&_Brow.isIE();
if(!canP())window.printFrame=_pFrm;
function _pFrm(f,of){
  if(!f)f=window;
  if(f.document.readyState!=="complete" && !confirm("Continue with printing?")){
    if(of)of();
    return;
  }
  var eS=printGetEventScope(f);
  window.printHelper=function(){
    _Win.execScript("on error resume next: printWB.ExecWB 6,1", "VBScript");
    printFireEvent(f,eS,"onafterprint");
    _Win.printWB.outerHTML="";
    window.printHelper=null;
  }
  _Win.document.body.insertAdjacentHTML("beforeEnd","<object id=\"printWB\" width=0 height=0 classid=\"clsid:8856F961-340A-11D0-A96B-00C04FD705A2\"></object>");
  printFireEvent(f,eS,"onbeforeprint");
  f.focus();
  window.printHelper=printHelper;
  setTimeout("window.printHelper()", 0);
}
if(!canP())window.printFireEvent=_pFire;
function _pFire(f,o,name){
	var handler=o[name];
	switch(typeof(handler)){
		case "string":f.execScript(handler);break;
		case "function":handler();
	}
	if(name=="onafterprint")if(_PEND)endP();
}
if(!canP())window.printGetEventScope=_pGE;
function _pGE(frm){
	var d=frm.document,f=d.all.tags("FRAMESET");
 	return f.length?f[0]:d.body;
}
function canP(){
  var ag=navigator.userAgent;var i=ag.indexOf("MSIE ")+5;
  return navigator.appName.indexOf("Netscape")!=-1 ||(parseInt(ag.substr(i)) >= 5 && ag.indexOf("5.0b1")<0);
}
function printPage(w,end){
	_PEND=(typeof end =="undefined"?true:end);
	_Win=w;
	if(!_Win)_Win=window;
	if(typeof end=="undefined")end=true;
	_Win.focus();
	if(_IMAC)return;
	if(!canP())printFrame(_Win);
	else{_Win.print();if(_PEND)endP();}
}
function closeP(){
	var t=(typeof _Win.parent);
	if(t!="unknown"&&t!="undefined")_Win.parent.close();
}
function endP(){
	tIDP=(parent.frames.length==0)?setTimeout("_Win.close();",_TimeP):setTimeout("closeP()",_TimeP);
}
function getObjPrint(){return ObjP;}
function printWindow(attr){ObjP=new PrintWindow(attr);}
function PrintWindow(attr){
	this.attr=attr;this.w=403;this.h=200;
	this.getURL=_getURL;this.getParam=_getParam;this.getParms=_getParms;
	this.show=_show;this.init=_init;this.printHTM=_printHTM;
	this.init();
}
function _init(){
	this.getParms("method,action,same_content,xml,print_logo,submit_function,form,debug,orientation");
	var s=trim(this.same_content);
	this.same_content=((!s && !this.action)||s=="false")?false:true;
	this.debug=(!this.debug||this.debug=="no")?false:true;
	if(_IMAC)this.debug=true;
	this.m=(this.method?this.method.toLowerCase():"get");
	if(this.m=="post"){
		var f=this.form;
		if(!f||typeof(f)=="undefined"||f.length==0){
			alert('printWindow) The form attribute must be defined.');return;
		}
	}
	this.show();
}
function focusNetscape(f){
	CURRENT_FIELD=f;
	var b=brow();
	if(b.isNetscape()){
		if(ERRO){LAST_FIELD.focus();ERRO=false;}
	}else if(b.isIE()&& parseInt(b.getMajorver(),10)>4)if(f.select)f.select();
}
function _getURL(){
	var a=this.action;var h=a?a:document.location.href;
	var r=/\?xml=([^&]+)/.exec(h),mp="media=print";
	if(r&&r.length==2)h=String(h).replace(/\.xml/,"_print.xml");
	else{
		if(!this.form)this.form=0;
		var f=document.forms[this.form];
		if(this.m=="get"||(!f ||!f.media ||f.media.value!="print"))h+=h.indexOf("?")==-1?"?"+mp:"&"+mp;
	}
	return h;
}
function _getParam(prop,def){
	var re,p=trim(prop);
	if(p.toLowerCase()!="submit_function")re=new RegExp(".*" +p+"=([^,]*)(,.*|$)");
	else re=/.*submit_function=([^,]*\\(.*\\)[^,]*)(,.*|$)"/;
	var r=re.exec(trim(this.attr));
	if(!r||!r.length||r.length<2)return def?def:"";
	else return String(r[1]);
}
function _getParms(p){
	var a=String(p).split(","),i=0;
	for(i=0;i<a.length;i++)eval("this."+a[i]+"=this.getParam('"+a[i]+"')");
}

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 saltaCampo(ev,field,tp,size){
	var tc,max,nargs=saltaCampo.arguments.length;
	if(/^float/.test(tp))max=(nargs>3)?size:SZ_FLOAT;
	else if(/^money/.test(tp)){
		if(!verifyMaxValue(field.value))max=(nargs>3)?size:SZ_MONEY;
		else field.value="0,00";
	}else if(/^(date|month_year|time|cpf|cnpj|cpf_cnpj|cep|account|branch|percent|ag_account|ag_savings)$/.test(tp))
		max=eval("SZ_"+tp.toUpperCase());
	else if(/^(text|uppercase|interval|(sp_)?alfanumeric|(sp_)?textnumber|default|numeric|email)$/.test(tp))
		max=size;
	tc=brow().isNetscape()?ev.which:ev.keyCode;
	if(String(field.value).length>=max && tc>=48){
		autoSkip(field);
		return true;
	}else return false;
}
function removeCaracs(f,type){
	var vr=unformatField(f.value,type);
	if(f.value!=vr)f.value=vr;
	focusNetscape(f);
}
function unformatField(valor,tipo){
	var vr=(typeof valor=="object")?valor.value:String(valor);
	var t=(arguments.length<1)?"default":String(tipo);
	if(!trim(vr)||trim(vr).length==0)return "";
	if(/^(date|month_year|time|ag_account|ag_savings|branch|account|cep|numeric|interval)$/.test(t))
		vr=justNumbersStr(vr);
	else if(/^(percent|float(\d{0,1})|money(\d{0,1}))$/.test(t))vr=toFloat(vr,_getNDec(t));
	else{
		if(/^(cpf|cnpj|cpf_cnpj)$/.test(t)){
			vr=justNumbersStr(vr);
			var isCPF=tipo=="cpf"||(tipo=="cpf_cnpj" && vr.length<=SZ_CPF);
			vr=repeatStr(vr,"0",isCPF?SZ_CPF:SZ_CNPJ);
			if(parseInt(vr,10)==0)vr="";
		}else if(t=="email")vr=trim(vr)
	}
	if(typeof valor=="object")valor.value=vr;
	else return vr;
}
function formatCamp(campo,tp,par1,par2,par3){
	var ie=(brow().isIE()&&!brow().isMac()),v=brow().getVersion();
	var b=parent.buttons;
	if(ie&&(v>=5 && v<5.2)&&b&&b.myBarra&&b.myBarra.clicked==true){
		parent.buttons.myBarra.clicked=false;
		eval("try{campo.focus();}catch(e){}");
		return false;
	}
	var nArg=formatCamp.arguments.length;
	ERRO=false;
	var vr=trim(campo.value);
	if(!campo|| !vr ||vr.length==0){
		if(/^(money|money2)$/.test(tp))campo.value="0,00";
		return false;
	}
	if(nArg==2)par1="";
	if(tp=="interval"){
		if(nArg<=4) par3="";
		return formatType(campo,tp,par3,par1,par2);
	}else return formatType(campo,tp,par1);
}
function toFloat(src,ndec){
src=trim(src);
if(!/^\-?([0-9]|\.)*\,{0,1}[0-9]*$/.test(src)||src.charAt(0)==".")return src;
var tam=src.length,pDec=src.indexOf(",");
if(src.length==0)src="0";
if(pDec==-1){
	var p=src.indexOf(".");
	if(p!=-1&&p==(tam-ndec-1))src=src.replace(/\.(\d*)$/,",$1");
	else return removeStr(src,".")+","+repeatNStr("0",ndec);
	pDec=src.indexOf(",");
}
src=removeStr(src,".");
if(pDec==0)return "0"+src+repeatNStr("0",ndec+1-src.length);
else{
	if(pDec>(tam-ndec-1))src+=repeatNStr("0",pDec-(tam-ndec-1));
	pDec=src.indexOf(",");
	return parseInt(src.slice(0,pDec),10)+src.slice(pDec,pDec+ndec+1);
}
}

function _getNDec(t){
	var arr=t.match(/(\d+)\s*$/);
	return arr?parseInt(arr[1],10):2;
}


function validaConteudo(event,el,tp)
{
	var t=(typeof event.which!="undefined"&& event.which!=null?event.which:event.keyCode),key=String.fromCharCode(t);
	if(t<20)return true;
	var tp_sp=/^sp_/.test(tp);
	if(/^(percent|float(\d{0,1})|money(\d{0,1}))$/.test(tp)){
		return isNumeric(key)||(key=="," && el.value.indexOf(",")==-1);
	}else if(/^(sp_)?alfanumeric$/.test(tp))
		return isAlfaNumeric(key)||(tp_sp && key==" ");
	else if(/^(sp_)?textnumber$/.test(tp))
		return isTextNumber(key)||(tp_sp && key==" ");
	else{
		switch(tp){
			case "email":
			case "uppercase":return true;
			case "text":return isAlfa(key)|| /[ ]/.test(key);
			case "default":return !/'|"/.test(key);
			default:return isNumeric(key);
		}
	}
}
function isTextNumber(v){return /^[0-9a-zA-ZáéíóúçãõâêôàÁÉÍÓÚÇÃÕÂÊÔÀ]+$/.test(v);}
function isFloatNumber(n){return /^\-?\d+(,\d+|\d*)$/.test(n);}
function fmtMoney(vr,ndec){
var neg=vr.indexOf("-")==0;
if(verifyMaxValue(vr)){ERRO=true;return "0,00";}
vr=toFloat(vr,ndec);
var vraux="",p,pDec=vr.indexOf(","),vrDec=vr.slice(pDec+1);
for(var i=pDec;i>(neg?1:0);i--){
	p=i-pDec;
	if(i!=pDec&&(p%3==0))vraux+=".";
	vraux+=vr.charAt(i-1);
}
return (neg?"-":"")+invertStr(vraux)+","+vrDec;
}
function repeatNStr(vr,n){
var r="",i;
for(i=0;i<n;i++)r+=vr;
return r;
}
function verifyMaxValue(vr){return vr.length>0 &&(parseFloat(vr)>MAX_VALUE);}
//Função utilizada para preenchimento do campo valor à partir da linha digitável
function seta_valorLinhaDig(CodBarrasBlco1, CodBarrasBlco2, campo_valor)
{	
	if((CodBarrasBlco1 != '') && (CodBarrasBlco2 != ''))
	{	
		if((CodBarrasBlco1.length == 12) && (CodBarrasBlco2.length == 12))
		{
			var valor1= CodBarrasBlco1.substring(4,11);
			var valor2 = CodBarrasBlco2.substring(0,4);
			var total = valor1 + valor2;
			tamanho = total.length;
			var val_final = total.substring(0, tamanho -2) + ',' + total.substring(tamanho - 2, tamanho);
			campo_valor.value = val_final;
			formatCamp(campo_valor,"money");
		}
	}

}

//Função utilizada para preenchimento do campo valor à partir do Código de Barras
function seta_valorCodBarras(codBarra, campo_valor)
{
	if(codBarra.length == 44)
	{
		var valor = codBarra.substring(4,15);
		tamanho = valor.length;
		var val_final = valor.substring(0, tamanho -2) + ',' + valor.substring(tamanho - 2, tamanho);
		campo_valor.value = val_final;
		formatCamp(campo_valor,"money");
	}
}

