// Abre um popup
function popUP(sURL, sName, iWidth, iHeight, scrollb) { 
	posHoriz = parseInt((screen.availWidth / 2) - parseInt(iWidth / 2));
	posVert = parseInt((screen.availHeight / 2) - parseInt(iHeight / 2));
	
	open (sURL, sName, "status=no, scrollbars="+scrollb+", left=" + posHoriz + ", top=" + posVert + ", width="+iWidth+", height="+iHeight)
}

function validaForm(descricao){
    var erro = 0;
    var inputFocus = null;
	var inputs = document.getElementsByTagName( 'input' );
    $x=0;
	$x2=0;
    while( $x < inputs.length ){
       if( inputs[$x].getAttribute( 'not' ) != 'not' ){
           if( inputs[$x].value == '' ){
               erro = 1;
			   inputs[$x].setAttribute('class', 'formulario_erro');
			   inputs[$x].setAttribute('className', 'formulario_erro');
			   if($x2 == 0){
				   inputFocus =  inputs[$x];
				   $x2++;
			   }
           }
       }
        $x++;
    }
	
    var inputs = document.getElementsByTagName( 'input' );
    $x=0;
    temCheckbox=0;
    errorCheckBox=1;
    var box;
    
	while( $x < inputs.length ){
		if( inputs[$x].getAttribute( 'name' ) == 'periodoEntrega' && inputs[$x].getAttribute( 'type' ) != 'hidden' ){
		   if( inputs[$x].getAttribute( 'not' ) != 'not' ){
	       		temCheckbox = 1;
       		  if( inputs[$x].checked == true ){
               	errorCheckBox = 0;
              }
	       }
		}
       
        $x++;
    }
    
    
    if( temCheckbox == 1 && errorCheckBox == 1 ){
    	erro = 1;
    	naoMsg = 1;
	    alert("Escolha um periodo para entrega!");
    }else{
    	naoMsg = 0;
    }
    
	var inputs = document.getElementsByTagName( 'select' );
    $x=0;
    while( $x < inputs.length ){
       if( inputs[$x].getAttribute( 'not' ) != 'not' ){
           if( inputs[$x].value == '' ){
               erro = 1;
			   inputs[$x].setAttribute('class', 'formulario_erro');
			   inputs[$x].setAttribute('className', 'formulario_erro');
			   if($x2 == 0){
				   inputFocus =  inputs[$x];
				   $x2++;
			   }
           }
       }
        $x++;
    }
	
    
	if(erro == 1 && naoMsg == 0 ){
    	alert('Campo(s) obrigatorio(s)!'+descricao);
		inputFocus.focus();
		return false;
	}
	else if( naoMsg == 1 ){
		return false;
	}
	
    return true;    
}

function FormataCPF(pForm,pCampo,pTamMax,pPos1,pPos2,pPosTraco,pTeclaPres){
	var wTecla, wVr, wTam;
	
	wTecla = pTeclaPres.keyCode ? pTeclaPres.keyCode : pTeclaPres.which ? pTeclaPres.which : pTeclaPres.charCode;
	wVr = pForm[pCampo].value;
	wVr = wVr.toString().replace( "-", "" );
	wVr = wVr.toString().replace( ".", "" );
	wVr = wVr.toString().replace( ".", "" );
	wVr = wVr.toString().replace( "/", "" );
	wTam = wVr.length ;
	
	if (wTam < pTamMax && wTecla != 8) { 
		wTam = wVr.length + 1 ; 
	}
	
	if (wTecla == 8 ) { 
		wTam = wTam - 1 ; 
	}
	
	if ( wTecla == 8 || wTecla == 88 || wTecla >= 48 && wTecla <= 57 || wTecla >= 96 && wTecla <= 105 ){
		if ( wTam <= 2 ){
			pForm[pCampo].value = wVr ;
		}
		if (wTam > pPosTraco && wTam <= pTamMax) {
			wVr = wVr.substr(0, wTam - pPosTraco) + '-' + wVr.substr(wTam - pPosTraco, wTam);
		}
		if ( wTam == pTamMax){
			wVr = wVr.substr( 0, wTam - pPos1 ) + '.' + wVr.substr(wTam - pPos1, 3) + '.' + wVr.substr(wTam - pPos2, wTam);
		}

		if(apenas_numero(pTeclaPres) == false)
		{
			pForm[pCampo].value = pForm[pCampo].value;
		}else{
			pForm[pCampo].value = wVr;
		}
	}
}

function apenas_numero(evento)
{
	var keypress = evento.keyCode ? evento.keyCode : evento.which ? evento.which : evento.charCode;

	if(keypress == 9 || keypress == 8 || keypress == 37 || keypress == 39 || keypress == 46)
	{
		return true;	
	}
	
	if((keypress > 47 && keypress < 58))
	{
		return true;
	}
	
	return false;
}

/////////////// FORMULÁRIO TOTAL DE DIGITOS  ////////////////////////////

var ns6=document.getElementById&&!document.all

function restrictinput(maxlength,e,placeholder){
	if (window.event&&event.srcElement.value.length>=maxlength)
	return false
	else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){
	var pressedkey=/[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys
	if (pressedkey.test(String.fromCharCode(e.which)))
	e.stopPropagation()
	}
}

function countlimit(maxlength,e,placeholder){
	var theform=eval(placeholder)
	var lengthleft=maxlength-theform.value.length
	var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder)
	if (window.event||e.target&&e.target==eval(placeholder)){
	if (lengthleft<0)
	theform.value=theform.value.substring(0,maxlength)
	placeholderobj.innerHTML=lengthleft
	}
}


function displaylimit(thename, theid, thelimit){
	var theform=theid!=""? document.getElementById(theid) : thename
	var limit_text='<b><span id="'+theform.toString()+'">'+thelimit+'</span></b> caracteres disponível para digitação'
	if (document.all||ns6)
	document.write(limit_text)
	if (document.all){
		eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
		eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
	}
	else if (ns6){
		document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true); 
		document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true); 
	}
}
