function message(retorno, obj){
  alert("Dados enviados com sucesso");
  //alert(retorno);
  //location.href = 'sec.asp?id=cont_associese.asp';
}
function validaEmail(mail){
  var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
  if(typeof(mail) == "string"){
    if(er.test(mail)){
        return true;
    }else{
      return false;
    }
  }else 
      if(typeof(mail) == "object"){
        if(er.test(mail.value)){
            return true;
        }else{
          return false;
        }
      }else{
            return false;
      }
}

function validForm(obj,form){
 
    var myObject = eval(obj); 
    x= new Ajax();
    x.url= "associese_envia.asp";
    x.callBack= message;

	for(var i=0;i<myObject.fields.length;i++)
	{	
		if (document.getElementById(myObject.fields[i].field).value == ""){
			alert(myObject.fields[i].message);
			document.getElementById(myObject.fields[i].field).focus();
			return false;
		}else{      
         x.addPost(document.getElementById(myObject.fields[i].field).id, escape(document.getElementById(myObject.fields[i].field).value));
    }    
	}

  if(!validaEmail(document.getElementById('TxEmail').value)){
    alert ("Email inválido");
    document.getElementById('TxEmail').focus();
    return false;
  }
  if(document.getElementById('Ativo').checked){
    x.addPost('BoVinculo','Ativo');
  }
  if(document.getElementById('Aposentado').checked){
    x.addPost('BoVinculo','Aposentado');
    x.addPost('TxDataAp',document.getElementById('TxDataAp').value);
  } 
  if(document.getElementById('Substituto').checked){
    x.addPost('BoVinculo','Substituto');
    x.addPost('TxDataIn',document.getElementById('TxDataIn').value);
    x.addPost('TxDataTer',document.getElementById('TxDataTer').value);
  } 
  if(document.getElementById('Pensionista').checked){
    x.addPost('BoVinculo','Pensionista');
  } 
  
  
  //variaveis que estão no JSON

  // Variaveis que não estavam encapsuladas no JSON
  x.addPost('TxUnidade',escape(document.getElementById('TxUnidade').value));
  x.addPost('TxDepartamento',escape(document.getElementById('TxDepartamento').value));
  x.addPost('Txpensionista',escape(document.getElementById('Txpensionista').value));
  x.addPost('DtObito',escape(document.getElementById('DtObito').value));
  x.addPost('TxPUnidade',escape(document.getElementById('TxPUnidade').value));
  x.addPost('TxNroPensionista',escape(document.getElementById('TxNroPensionista').value));
  x.addPost('TxNominar',escape(document.getElementById('TxNominar').value));
  x.addPost('TxInstituicaoPensionista',escape(document.getElementById('TxInstituicaoPensionista').value));
  x.addPost('TxDepartamentoPensionista',escape(document.getElementById('TxDepartamentoPensionista').value));
  
  //Pegar Campos Dinamicos
  // Vai no máximo até 50 campos
  for(var y=1;y<50;y++){
      try{
            x.addPost(escape(document.getElementById('txNomeP'+y).id,document.getElementById('txNomeP'+y).value));
            x.addPost(escape(document.getElementById('TxPParentesco'+y).id,document.getElementById('TxPParentesco'+y).value));
            if(document.getElementById('RadPensaoV'+y).checked)
              x.addPost(escape(document.getElementById('RadPensaoV'+y).id,document.getElementById('RadPensaoV'+y).value));
            if(document.getElementById('RadPensaoT'+y).checked)
              x.addPost(escape(document.getElementById('RadPensaoT'+y).id,document.getElementById('RadPensaoT'+y).value));
            
        }catch(e){}
  } 
  x.call();
  
}

function validDate(field) {
    var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
    if (!field.value.match(expReg)){        
        alert("Formato inválido de data");
        field.focus();
        return false;
    }
}