function valida(theForm)
{
  if (isBlank(theForm.nome))
  {
    alert("Preencha o campo \"nome\".");
    return false;
  }
  if (isBlank(theForm.cpf))
  {
    alert("Preencha o campo \"CPF\".");
    return false;
  }
  else if (!isMail(theForm.email))
  {
    alert("Formato incorreto de e-mail.");
    return false;
  }
  return true;
}

function isMail(mailField){
  strMail = mailField.value;
  var re = new RegExp;
  re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  var arr = re.exec(strMail);
  if (arr == null)
    return(false);
  else
    return(true);
}

function minLen(txtField, minVal){
  strExp = txtField.value;
  l = strExp.length;
  if (l < minVal)
    return(true);
  else
    return(false);
}

function maxLen(txtField, maxVal){
  strExp = txtField.value;
  l = strExp.length;
  if (l > maxVal)
    return(true);
  else
    return(false);
}

function isBlank(txtField){
  if (txtField.value)
    return (false);
  else
    return(true);
}

function isSelectedZero(txtField){
  selected = txtField.selectedIndex;
  if (selected == 0)
    return(true);
  else
    return(false);
}

function isNumber(txtField){
  numExp = txtField.value;
  if (isNaN(numExp) || (numExp.length == 0))
    return (false);
  else
    return(true);
}

function isCPF(txtField){ 

  var i; 
  s = txtField.value;  
  var c = s.substr(0,9); 
  var dv = s.substr(9,2); 
  var d1 = 0; 
  
  for (i = 0; i < 9; i++){ 
    d1 += c.charAt(i)*(10-i); 
  } 
  
  if (d1 == 0) return false;   
  
  d1 = 11 - (d1 % 11); 
  
  if (d1 > 9) d1 = 0; 
  
  if (dv.charAt(0) != d1) return false; 
  
  d1 *= 2; 
  
  for (i = 0; i < 9; i++){ 
    d1 += c.charAt(i)*(11-i);   
  } 
  
  d1 = 11 - (d1 % 11); 
  
  if (d1 > 9) d1 = 0; 
  
  if (dv.charAt(1) != d1) return false; 
  
  return true; 
  
}

function verificarCPF(c){
var i; 
s = c;
var c = s.substr(0,9); 
var dv = s.substr(9,2); 
var d1 = 0; 
var v = false;
for (i = 0; i < 9; i++) 
{ 
d1 += c.charAt(i)*(10-i); 
} 
if (d1 == 0){ 
alert("CPF Inválido")
v = true; 
return false; 
} 
d1 = 11 - (d1 % 11); 
if (d1 > 9) d1 = 0; 
if (dv.charAt(0) != d1) 
{ 
alert("CPF Inválido") 
v = true;
return false; 
} 

d1 *= 2; 
for (i = 0; i < 9; i++) 
{ 
d1 += c.charAt(i)*(11-i); 
} 
d1 = 11 - (d1 % 11); 
if (d1 > 9) d1 = 0; 
if (dv.charAt(1) != d1) 
{ 
alert("CPF Inválido") 
v = true;
return false; 
} 
if (!v) {
alert(c + "\nCPF Válido") 
}
}


function validar(theForm)
{

  if (isBlank(theForm.nome))
  {
    alert("Preencha o campo \"nome\".");
	theForm.nome.focus();
    return false;
  }
  if (isBlank(theForm.cpf))
  {
    alert("Preencha o campo \"CPF\".");
	theForm.cpf.focus();
    return false;
  }
  if (!isCPF(theForm.cpf))
  {
    alert("CPF inválido.");
	theForm.cpf.focus();
    return false;
  }
   
  if (isBlank(theForm.dia))
  {
    alert("Preencha o \"Dia\" na Data de Nascimento.");
	theForm.dia.focus();
    return false;
  }
    if (isBlank(theForm.mes))
  {
    alert("Preencha o \"Mes\" na Data de Nascimento.");
	theForm.mes.focus();
    return false;
  }
    if (isBlank(theForm.ano))
  {
    alert("Preencha o \"Ano\" na Data de Nascimento.");
	theForm.ano.focus();
    return false;
  }
	marcado = -1;
  	for (i=0; i< theForm.sexo.length; i++) {
		if (theForm.sexo[i].checked) {
			marcado = i
		}
	}
	
	if (marcado == -1) {
		alert("Preencha o campo \"Sexo\".");
		theForm.sexo[0].focus();
		return false;
	}

    if (isBlank(theForm.endereco))
  {
    alert("Preencha o campo \"Endereço\".");
	theForm.endereco.focus();
    return false;
  }

 /* if (isBlank(theForm.complemento))
  {
    alert("Preencha o campo \"Complemento\".");
    return false;
  }*/

    if (isBlank(theForm.bairro))
  {
    alert("Preencha o campo \"Bairro\".");
	theForm.bairro.focus();
    return false;
  }

    if (isBlank(theForm.cep))
  {
    alert("Preencha o campo \"CEP\".");
	theForm.cep.focus();
    return false;
  }
	x = theForm.cep.value;
	y = x.length;
  if (y != 8)
  {
    alert("O campo \"CEP\" deve conter 8 dígitos.");
    return false;
  }

    if (isBlank(theForm.residencial))
  {
    alert("Preencha o campo \"Fone Residencial\".");
	theForm.residencial.focus();
    return false;
  }
    if (isBlank(theForm.opcao1))
  {
    alert("Preencha no mínimo 1 opção de Login.");
	theForm.opcao1.focus();
    return false;
  }
    if (isBlank(theForm.numero_provedor))
  {
    alert("Preencha o número da assinatura.");
	theForm.numero_provedor.focus();
    return false;
  }
  	marcado = -1;
  	for (i=0; i< theForm.vel.length; i++) {
		if (theForm.vel[i].checked) {
			marcado = i
		}
	}
	
	if (marcado == -1) {
		alert("Selecione um plano.");
		theForm.vel[0].focus();
		return false;
	}

 return true;
}


function validaDadosContrato(){

	var erro = "";

	if( document.sel.elements['nome'].value.length == 0){
		erro+="\nNome deve ser preenchido";
	}
	if( !validaCPF(document.sel.elements['cpf'].value)){
		erro+="\nCPF inválido";
	}
	if( document.sel.elements['nascimento_dia'].value.length == 0 || document.sel.elements['nascimento_mes'].value.length == 0 || document.sel.elements['nascimento_ano'].value.length == 0){
		erro+="\nData de nascimento deve ser preenchido";
	}
	if( document.sel.elements['endereco'].value.length == 0){
		erro+="\nEndereco deve ser preenchido";
	}
	if( document.sel.elements['bairro'].value.length == 0){
		erro+="\nBairro deve ser preenchido";
	}
	if( document.sel.elements['cep'].value.length < 8){
		erro+="\nCEP inválido";
	}
	if( document.sel.elements['tel_residencial'].value.length == 0){
		erro+="\nTelefone residencial deve ser preenchido";
	}
	return erro;

}



function validaDadosCobranca(){

	var erro = "";

	var tipo_pagamento = document.sel.elements['tipo_pagamento'].options[document.sel.elements['tipo_pagamento'].selectedIndex].value;


	document.sel.elements['operadora_cartao'].value = document.getElementById('opcart').options[document.getElementById('opcart').selectedIndex].value;
	document.sel.elements['cartao_numero'].value =    document.getElementById('numcart').value;
	document.sel.elements['cartao_validade2'].value = document.getElementById('valcart2').value;
	document.sel.elements['cartao_validade3'].value = document.getElementById('valcart3').value;

	document.sel.elements['banco'].value = document.getElementById('bc').options[document.getElementById('bc').selectedIndex].value;
	document.sel.elements['banco_agencia'].value = document.getElementById('bcag').value;
	document.sel.elements['banco_conta'].value = document.getElementById('bcct').value;

	if(document.getElementById('drb').checked){
		document.sel.elements['desejo_receber_boleto'].value = 'sim';
	}
	else{
		document.sel.elements['desejo_receber_boleto'].value = '';
	}




	if( tipo_pagamento == 'selecione'){
		erro +="\nSelecione uma forma de pagamento";
	}
	else{

		if( tipo_pagamento == 'cartao'){

			if( document.sel.elements['cartao_numero'].value.length == 0){
				erro+="\nNúmero do cartão deve ser preenchido";
			}
			if( document.sel.elements['cartao_validade2'].value.length == 0 || document.sel.elements['cartao_validade3'].value.length == 0){
				erro+="\nValidade do cartão deve ser preenchido";
			}

		}


		if( tipo_pagamento == 'debito'){

			if( document.sel.elements['banco_agencia'].value.length == 0){
				erro+="\nAgência deve ser preenchida";
			}
			if( document.sel.elements['banco_conta'].value.length == 0 ){
				erro+="\nConta deve ser preenchido";
			}

		}


		if( tipo_pagamento == 'boleto'){

			if( document.sel.elements['desejo_receber_boleto'].value.length == 0){
				erro+="\nMarque a opção \"Desejo receber boleto\"!";
			}


		}




	}

	return erro;

}


function validaCPF(CPF){
	var op=false;

	dig_1 = 0;
	dig_2 = 0;
	controle_1 = 10;
	controle_2 = 11;
	lsucesso = 1;
	numero = CPF;
	if( numero.length != 11 || CPF=='00000000000' || CPF=='11111111111' || CPF=='22222222222' || CPF=='33333333333' || CPF=='44444444444'
							|| CPF=='55555555555' || CPF=='66666666666' || CPF=='77777777777' || CPF=='88888888888' || CPF=='99999999999'){
		return false;
		
	}
	else{
		for (i=0 ; i < 9 ; i++){
			dig_1 = dig_1 + parseInt(numero.substring(i, i+1) * controle_1);
			controle_1 = controle_1 - 1;
		}
		resto = dig_1 % 11;
		dig_1 = 11 - resto;

		if ((resto == 0) || (resto == 1))
			dig_1 = 0;

		for ( i=0 ; i < 9 ; i++){
			dig_2 = dig_2 + parseInt(numero.substring(i, i + 1) * controle_2);
			controle_2 = controle_2 - 1;
		}
		dig_2 = dig_2 + 2 * dig_1;
		resto = dig_2 % 11;
		dig_2 = 11 - resto;
		if ((resto == 0) || (resto == 1))
			dig_2 = 0;
		dig_ver = (dig_1 * 10) + dig_2;
		if (dig_ver != parseFloat(numero.substring(numero.length-2,numero.length))){
			return false;
		}
	}
	return true;
}


