//////////////////////////////////////////////////////////////////////////
//PEGA VALOR DO EDITOR
//////////////////////////////////////////////////////////////////////////
function GetInnerHTML()
{
	// Get the editor instance that we want to interact with.
	var oEditor = FCKeditorAPI.GetInstance('Editor') ;

	return oEditor.EditorDocument.body.innerHTML;
}	
//////////////////////////////////////////////////////////////////////////
//CONCATENA PARAMETROS
//////////////////////////////////////////////////////////////////////////
function createParams(pForm) {
	var strParametros = '';
	for (var i=0; i < document.forms[pForm].elements.length; i++) {
		//SE O OBJETO FOR UM CHECKBOX APENAS CONSIDERAR OS CHECKADOS
		if (document.forms[pForm].elements[i].type=='checkbox') {
			if (document.forms[pForm].elements[i].checked==true)
				strParametros += '&' + document.forms[pForm].elements[i].name + '=' + escape(document.forms[pForm].elements[i].value);
			}
		else if (document.forms[pForm].elements[i].type=='radio') {
			if (document.forms[pForm].elements[i].checked==true)
				strParametros += '&' + document.forms[pForm].elements[i].name + '=' + escape(document.forms[pForm].elements[i].value);
			}			
		else
			strParametros += '&' + document.forms[pForm].elements[i].name + '=' + escape(document.forms[pForm].elements[i].value);
	}
	strParametros = strParametros.substring(1, strParametros.length);
	return strParametros;
}
//////////////////////////////////////////////////////////////////////////
//SOLICITACAO DO COMPONENTE DO BROWSER (AJAX)
//////////////////////////////////////////////////////////////////////////
function openXmlHttp() {
var Ajax;
try {
	Ajax = new XMLHttpRequest(); // XMLHttpRequest para browsers mais populares, como: Firefox, Safari, dentre outros.
}catch(ee) {
	try {
		Ajax = new ActiveXObject("Msxml2.XMLHTTP"); // Para o IE da MS
	}catch(e) {
		try {
			Ajax = new ActiveXObject("Microsoft.XMLHTTP"); // Para o IE da MS
		}catch(e) {
			Ajax = false;
			}
	}
}
return Ajax;
}
//////////////////////////////////////////////////////////////////////////
//SUBMETENDO A INFORMACAO E REQUISITANDO A RESPOSTA (AJAX)
//////////////////////////////////////////////////////////////////////////
function loadAjax(pObjeto, pPagina, pTextoCarregando, pValores) {
	if(document.getElementById) { // Para os browsers complacentes com o DOM W3C.
			var exibeResultado = document.getElementById(pObjeto); // div que exibirá o resultado.
			var Ajax = openXmlHttp(); // Inicia o Ajax.
			Ajax.open("POST", pPagina, true); // fazendo a requisição
			Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			Ajax.setRequestHeader("Content-length", pValores.length);
			Ajax.setRequestHeader("Connection", "close");

			Ajax.onreadystatechange = function() {
				if(Ajax.readyState == 1) { // Quando estiver carregando, exibe: carregando...						
				}
				if(Ajax.readyState == 4) { // Quando estiver tudo pronto.
					if(Ajax.status == 200) {	
						var resultado = Ajax.responseText; // Coloca o retornado pelo Ajax nessa variável
						exibeResultado.innerHTML = resultado;						
					}
				}
			}
			
			Ajax.send(pValores);
	}
}
//////////////////////////////////////////////////////////////////////////
//XHTMLHTTP
//////////////////////////////////////////////////////////////////////////
function syncCall(pPagina, pValores) {
  //document.getElementById("divLoad").style.display='block';
  var objHttp = null;

  if ( window.XMLHttpRequest )
  { objHttp = new XMLHttpRequest(); }

  else if ( window.ActiveXObject )
  { objHttp = new ActiveXObject( "Microsoft.XMLHTTP" ); }

  if ( objHttp == null )
  { alert( "Seu navegador não possui suporte à AJAX" ); }

  objHttp.open("POST", pPagina, false); // fazendo a requisição
  objHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  objHttp.setRequestHeader("Content-length", pValores.length);
  objHttp.setRequestHeader("Connection", "close");
  objHttp.send(pValores);

  if ( objHttp.readyState == 4 || objHttp.readyState == "complete" ) {
    if ( objHttp.statusText == 200 || objHttp.statusText == "OK" ) {
		//document.getElementById("divLoad").style.display='none';
		if (objHttp.responseText.substring(0, 2) == "OK")
			return objHttp.responseText;
		else {
			alert(objHttp.responseText);
			return objHttp.responseText;
		}
    } else {
      alert( "Erro na resposta do servidor" );
      return false;
    }
  }
}
//////////////////////////////////////////////////////////////////////////
//FUNÇÃO QUE BLOQUEIA QUALQUER CARACTER QUE NÃO SEJA NÚMERO
//ENTRE 0 E 9
//////////////////////////////////////////////////////////////////////////
function bloqueia_alfa(pEvento)
{
navegador = /msie/i.test(navigator.userAgent);
if (navegador)
	var tecla = event.keyCode;
else
	var tecla = pEvento.which;
	
	if (!tecla)
		return true;
	
	if(tecla > 47 && tecla < 58) // numeros de 0 a 9
		return true;
	else
	{
		if (tecla != 8) // backspace
			return false;
		else
			return true;
	}
}
//////////////////////////////////////////////////////////////////////////
//FUNCAO CARREGA COMBO
//////////////////////////////////////////////////////////////////////////
function carregaCbo(pPagina, pParametros, pObjeto, pTamanhoValor, pValorSelecionado) {
	var cboObjeto = eval(document.getElementById(pObjeto));
	cboObjeto.innerHTML = "";
	
	if(document.getElementById) { // Para os browsers complacentes com o DOM W3C.
			var Ajax = openXmlHttp(); // Inicia o Ajax.
			Ajax.open("POST", pPagina, true); // fazendo a requisição
			Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			Ajax.setRequestHeader("Content-length", pParametros.length);
			Ajax.setRequestHeader("Connection", "close");
			Ajax.send(pParametros);

			while(cboObjeto.options.length>0) cboObjeto.options[0]=null
				cboObjeto.options[0]=new Option(" -- Carregando ... -- "," -- Carregando ... -- ")

			Ajax.onreadystatechange = function() {
				if(Ajax.readyState == 1) { // Quando estiver carregando, exibe: carregando...
				}
				
				if(Ajax.readyState == 4) { // Quando estiver tudo pronto.
					if(Ajax.status == 200) {	
						while(cboObjeto.options.length>0)cboObjeto.options[0]=null
						//Transforma a lista de cidades em Javascript
						var aCidades = eval((Ajax.responseText));
						//popula o select com a lista de cidades obtida
						for(var i=0;i<aCidades.length;i++){
						    aCidades[i]=unescape(aCidades[i]);
						    cboObjeto.options[cboObjeto.options.length]=new Option(aCidades[i].substring(pTamanhoValor, aCidades[i].length), aCidades[i]);
						    if (aCidades[i].substring(0, pTamanhoValor)==pValorSelecionado)
								var intItemSelecionado = i;
						}
						if (pValorSelecionado!='')
							cboObjeto.selectedIndex = intItemSelecionado;
					}
				}
			}
	}

}
//////////////////////////////////////////////////////////////////////////
//VALIDA CPF
//////////////////////////////////////////////////////////////////////////
function validaCPF(nmTxt)
{
 if (nmTxt.value.length == 0)
 {
  return false;
 }
 
 if (nmTxt.value.length != 11) {sim=false}
 else {sim=true}

  if (sim )  // valida o primeiro digito
  {
  for (i=0;((i<=(nmTxt.value.length-1))&& sim); i++)
  {
   val = nmTxt.value.charAt(i)
   if

 ((val!="9")&&(val!="0")&&(val!="1")&&(val!="2")&&(val!="3")&&(val!="4")

 &&    (val!="5")&&(val!="6")&&(val!="7")&&(val!="8")) {sim=false}
   }

   if (sim)
  {
    soma = 0
    for (i=0;i<=8;i++)
    {
     val = eval(nmTxt.value.charAt(i))
     soma = soma + (val*(i+1))
    }

    resto = soma % 11
    if (resto>9) dig = resto -10
    else  dig = resto
    if (dig != eval(nmTxt.value.charAt(9))) { sim=false }
   else   // valida o segundo digito
    {

    soma = 0
    for (i=0;i<=7;i++)
     {
     val = eval(nmTxt.value.charAt(i+1))
      soma = soma + (val*(i+1))
    }

     soma = soma + (dig * 9)
    resto = soma % 11
     if (resto>9) dig = resto -10
     else  dig = resto
   if (dig != eval(nmTxt.value.charAt(10))) { sim = false }
    else sim = true
   }
   }
  }

   if (sim)
   {
	if (nmTxt.value == "11111111111" || nmTxt.value == "22222222222" || nmTxt.value == "33333333333" || nmTxt.value == "44444444444" || nmTxt.value == "55555555555" ||
		nmTxt.value == "66666666666" || nmTxt.value == "77777777777" || nmTxt.value == "88888888888" || nmTxt.value == "99999999999" || nmTxt.value == "00000000000")
	{
		sim=false;
	}
   }

  if (sim) 
  {	
	//alert("Valor Valido de CPF") 
	return true;
  }
  else
  { 
    alert("Valor inválido de CPF");
	nmTxt.value = '';
	return false;
  }
}
//FIM DA FUNÇÃO
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//FUNÇÃO INSERE MÁSCARA NO FORMATO DE DATA '##/##/####' - DD/MM/YYYY
//////////////////////////////////////////////////////////////////////////
function formataData(pObj, teclapres) {
  var tecla = teclapres.keyCode; 
  var vr = pObj.value;
  
  if ("0123456789".search(vr.substr(vr.length-1,1)) == -1) {
      vr = vr.substr(0, vr.length-1);
      pObj.value = vr;
  }
  else {   
    vr = vr.replace( ".", "" );
    vr = vr.replace( "/", "" );
    vr = vr.replace( "/", "" );
    tam = vr.length + 1;
    if ( tecla != 9 && tecla != 8 ) {
      if ( tam > 2 && tam < 5 ) {
        pObj.value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
      }
      if ( tam >= 5 && tam <= 10 ) {
        pObj.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 );
      }
    }
  }
}//FIM DA FUNÇÃO
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//VALIDAÇÃO DE DATA DD/MM/AAAA (EXPRESSÃO REGULAR)
//////////////////////////////////////////////////////////////////////////
//Os dias 1 a 29 ((0?[1-9]|[12]\d)) são aceitos em todos os meses (1 a 12): (0?[1-9]|1[0-2])
//Dia 30 é válido em todos os meses, exceto fevereiro (02): (0?[13-9]|1[0-2])
//Dia 31 é permitido em janeiro (01), março (03), maio (05), julho (07), agosto (08),
//outubro (10) e dezembro (12): (0?[13578]|1[02]).
//////////////////////////////////////////////////////////////////////////
function validaData(pStr, pTipo)
{
  //1 = DATA DE NASCIMENTO
  //2  OUTRAS DATAS
  if (pTipo==2)
  verificarRetroatividade(pStr);
    
  if (pStr.value.substring(6, 12) % 4 != 0 && pStr.value.substring(0, 5) == '29/02')
	{
		alert(pStr.value + " NÃO é uma data válida.");
		pStr.value = '';
		pStr.focus();
	}
  
  var reDate = /^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20)?\d{2}$/;
  if (reDate.test(pStr.value)) {
    //alert(pStr.value + " é uma data válida.");
  } else if (pStr.value != null && pStr.value != "") {
    alert(pStr.value + " NÃO é uma data válida.");
    pStr.value = '';
    pStr.focus();
    }
}
//////////////////////////////////////////////////////////////////////////
//VALIDAÇÃO DE E-MAIL
//////////////////////////////////////////////////////////////////////////
function validaEmail(pStr)
{
  var reEmail = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
  if (reEmail.test(pStr.value)) {
    //alert(pStr.value + " é um endereço de e-mail válido.");
    return true;
  } else if (pStr.value != null && pStr.value != "") {
    alert(pStr.value + " NÃO é um endereço de e-mail válido.");
    //pStr.value = '';
	pStr.select();
    pStr.focus();
    return false;
  }
}//FIM DA FUNÇÃO
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//VALIDA PASSO 1
//////////////////////////////////////////////////////////////////////////
function validaPasso1() {
		var strResultado = syncCall('funcoes_adote.asp', createParams('frmForm1')+'&pAcao=PASSO1');
		if (strResultado.substring(0, 2) == "OK") {
			document.frmForm1.submit();
			return true;
		}
		else {
			return false;
		}
}
//////////////////////////////////////////////////////////////////////////
//VALIDA PASSO 2
//////////////////////////////////////////////////////////////////////////
function validaPasso2()
{
		var strResultado = syncCall('funcoes_adote.asp', createParams('frmCadastro')+'&pAcao=PASSO2');
		if (strResultado.substring(0, 2) == "OK")
			return true;
		else {
			return false;
		}
}

