var radiotipo
radiotipo = ""

function validacao(theForm)
{

  if (theForm.dominio.value.length < 1)
  {
    alert("Você precisa digitar o domínio.");
    theForm.dominio.focus();
    return (false);
  }
  
  if (theForm.sub.value.length < 1)
  {
    alert("Falta o complemento do domínio.");
    theForm.sub.focus();
    return (false);
  }

  if (theForm.nome.value.length < 1)
  {
    alert("Você precisa digitar seu nome.");
    theForm.nome.focus();
    return (false);
  }
  
  if (theForm.email.value.length < 1)
  {
    alert("Você precisa digitar o e-mail.");
    theForm.email.focus();
    return (false);
  }

  if (theForm.endereco.value.length < 1)
  {
    alert("Você precisa digitar o endereco.");
    theForm.endereco.focus();
    return (false);
  }
  
  if (theForm.bairro.value.length < 1)
  {
    alert("Você precisa digitar o bairro.");
    theForm.bairro.focus();
    return (false);
  }
  
  if (theForm.cidade.value.length < 1)
  {
    alert("Você precisa digitar a cidade.");
    theForm.cidade.focus();
    return (false);
  }
            
  if (theForm.estado.value.length < 1)
  {
    alert("Você precisa escolher o ESTADO.");
    theForm.estado.focus();
    return (false);
  }
  
  if (theForm.cep.value.length < 1)
  {
    alert("Você precisa digitar o cep.");
    theForm.cep.focus();
    return (false);
  }
              
  if (theForm.telefone.value.length < 10)
  {
    alert("Você precisa digitar seu DDD e TELEFONE .");
    theForm.telefone.focus();
    return (false);
  }
 
  if (theForm.cgccpf.value.length < 1)
  {
    alert("Você precisa digitar o CNPJ/CPF.");
    theForm.cgccpf.focus();
    return (false);
  }
  
  if(theForm.tipopessoa[0].checked == true)
  {
  	    if(!(checa1(theForm.cgccpf.value))) 
		{
			theForm.cgccpf.focus()
			return false;
		}
  }
  if(theForm.tipopessoa[1].checked == true)
  {

		if(!(checa1cgc(theForm.cgccpf.value))) 
		{
			theForm.cgccpf.focus()
			return false;
		}

  } 

  if (theForm.responsavel.value.length < 1)
  {
    alert("Você precisa digitar o nome do Responsavel pela conta.");
    theForm.responsavel.focus();
    return (false);
  }

  if (theForm.login.value.length < 4)
  {
    alert("Você precisa digitar um login com mais de 4 caracteres.");
    theForm.login.focus();
    return (false);
  }
  
  if (theForm.senha.value.length < 4)
  {
    alert("Você precisa digitar uma senha com mais de 4 caracteres.");
    theForm.senha.focus();
    return (false);
  }

  if (theForm.textimage.value.length < 1)
  {
    alert("Você precisa digitar o que está escrito na imagem.");
    theForm.textimage.focus();
    return (false);
  }

  return (true);
}
////////////////////////////////
function ajustar_tel(input)
{
	if ((event.keyCode<48)||(event.keyCode>57)){
		event.returnValue =false;
		} else { 
		if((input.value.length==2)||(input.value.length==3))
			  input.value=input.value + " " ;
		 else {
    		 if(input.value.length==7)
				input.value=input.value + "-" ;
		}
	}
}

///////////////////////////////
function modulocgc(str) {
 soma=0;
 ind=2;
 for(pos=str.length-1;pos>-1;pos=pos-1) {
  soma = soma + (parseInt(str.charAt(pos)) * ind);
  ind++;
  if(str.length>11) {
	if(ind>9) ind=2;
	}
  }
 resto = soma - (Math.floor(soma / 11) * 11);
 if(resto < 2) {
  return 0;
  }
 else {
  return (11 - resto);
  }
}

function checa1cgc(str) {
 while (str.indexOf(".") > 0) {
	str = str.replace(".","")
 }
 while (str.indexOf("/") > 0) {
	str = str.replace("/","")
 }
 while (str.indexOf("-") > 0) {
	str = str.replace("-","")
 }
 while (str.indexOf(",") > 0) {
	str = str.replace(",","")
 }
 if (str.length >= 15) {
	 str =  str.substring(str.length - 15,str.length)
 }
else {
 while (str.length < 15) {
   str = '0' + str
  }
}

 if(!(modulocgc(str.substring(0,str.length - 2)).toString()+modulo(str.substring(0,str.length - 1)).toString() == str.substring(str.length - 2,str.length))) {
	  alert("Digite um CNPJ Valido");
	  return false;
 }
return true;
}


//////////////
function modulo(str) {
    	soma=0;
    	ind=2;
    	for(pos=str.length-1;pos>-1;pos=pos-1) {
    		soma = soma + (parseInt(str.charAt(pos)) * ind);
    		ind++;
    		if(str.length>11) {
    			if(ind>9) ind=2;
    		}
		}
    	resto = soma - (Math.floor(soma / 11) * 11);
    	if(resto < 2) {
	    	return 0
    	}
    	else {
    		return 11 - resto
    	}
	}

function checa1(valor) {
		cpfcompleto = valor
		valorfinal = cpfcompleto.replace(".","");
		valorfinalb = valorfinal.replace(".","");
		valor = valorfinalb.replace("-","")
		//alert(valor)
		if (valor.length != 11) {
    		alert("Os dados do seu CPF aparentam não ser válidos.\nTente novamente!")
    		return false;
		}
    	if(modulo(valor.substring(0,valor.length - 2)) + "" + modulo(valor.substring(0,valor.length - 1)) != valor.substring(valor.length - 2,valor.length)) {
    		alert("Os dados do seu CPF aparentam não ser válidos.\nRedigite-os sem pontos (.) traços (-) ou vírgulas (,).");
    		return false;
    	}
		return true;
	}

///////////////////////

function FormataCep(Campo,teclapres) {
 var tecla = teclapres.keyCode;
 vr = document.cadastro[Campo].value;
 vr = vr.replace( ".", "" );
 vr = vr.replace( "-", "" );
 vr = vr.replace( "/", "" );
 tam = vr.length + 1;
 
 if ( tecla != 9 && tecla != 8 ){
  if ( tam > 6 && tam < 8 )
   document.cadastro[Campo].value = vr.substr( 0, tam - 5  ) + '-' + vr.substr( tam - 5, tam );
  if ( tam >= 6 && tam <= 10 )
   document.cadastro[Campo].value = vr.substr( 0, 5 ) + '-' + vr.substr( 5, 3 ) ; }
}

function FormataCPF(pForm,pCampo){
if(document.cadastro.tipopessoa[0].checked == true)
{
 var wTecla, wVr, wTam, pTamMax, pPos1, pPos2, pPosTraco, pTeclaPres;
 pTamMax = 11
 pPos1 = 8
 pPos2 = 5
 pPosTraco = 2
 pTeclaPres = event
 
       // alert(document.cadastro[pCampo].value);
  
 wTecla = pTeclaPres.keyCode;
 wVr = document.cadastro[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 ){
    document.cadastro[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);
  }
  document.cadastro[pCampo].value = wVr;
 
 }
}
}

function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}
