// 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 MascaraCEP (formato, evento, objeto)
{
	var keypress = evento.keyCode ? evento.keyCode : evento.which ? evento.which : evento.charCode;
	
	if(keypress == 8 || keypress == 37 || keypress == 39 || keypress == 46)
	{
		return true;	
	}
	
	if(!(keypress > 47 && keypress < 58))
	{
		return false;
	}
	
	campo = eval (objeto);
	if (formato=='CEP')
	{
		caracteres = '01234567890';
		separacoes = 1;
		separacao1 = '-';
		conjuntos = 2;
		conjunto1 = 5;
		conjunto2 = 3;
		if ((caracteres.search(String.fromCharCode (keypress))!=-1) && campo.value.length < (conjunto1 + conjunto2 + 1))
		{
			if (campo.value.length == conjunto1) 
			campo.value = campo.value + separacao1;
		}
		else 
			return true;
	}
}

function validaForm( ){
    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)!');
		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 == 8 || keypress == 37 || keypress == 39 || keypress == 46)
	{
		return true;	
	}
	
	if((keypress > 47 && keypress < 58))
	{
		return true;
	}
	
	return false;
}
	



