// JavaScript Document

var ajx;
var ajx_url;
var ajx_form;
var post_form ;
var postar_logo; 


function preloader(ar_img) 

	{
	
	Imagem = new Array();
	
			for(x=0;x<ar_img.length;x++){
				imagem_carrega = ar_img[x];
				Imagem[x] = new Image(); 
				Imagem[x].src = imagem_carrega;
				
			
			}
	
	}

function init(){
	
	
}



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




function ajax()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Seu browser não tem acesso ao  AJAX!");
      return false;
      }
    }
  }
  
  
  return  xmlHttp;
}


function coleta_form(form){
	
		formId=document.getElementById(form)
	var postStr= '';
	for (i = 0; i < formId.elements.length; i++) {
		formElem = formId.elements[i];
		switch (formElem.type) {
			case 'text':
			case 'select-one':
			case 'hidden':
			case 'password':
			case 'textarea':
			case 'checkbox':
			case 'radio':
			if(formElem.type == 'checkbox' || formElem.type == 'radio'){
			   
			   if(formElem.checked == true){
			
			postStr += formElem.name + '=' + escape(formElem.value) + '&';
			   }
			
			}else{
				
				postStr += formElem.name + '=' + escape(formElem.value) + '&'
				
			}
		break;
		}
	}
	
	return postStr;
	
	
}

function loadestado(uf){
	ajx = ajax();
	//mostra_p = setInterval('mostra_processo()',0555);
 	ajx.onreadystatechange = abreEstado;
	tipo_texto = 'Carregando'
	document.getElementById('lista_estados').innerHTML = '<img src=\'/images/loader.gif\'>';

	u = 'abre_estado.php?uf='+uf;
	ajx.open("GET", u, true);
		ajx.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajx.setRequestHeader("Connection", "close"); 
        ajx.send(null);

}

function consulta_paciente(id_dentista){
	ajx = ajax();
	//mostra_p = setInterval('mostra_processo()',0555);
 	ajx.onreadystatechange = resposta_paciente;
	tipo_texto = 'Carregando'
	

	coddoc =document.getElementById('pacientes').doc_user.value;
	senha =document.getElementById('pacientes').senha_user.value;
	//document.getElementById(ajx_form).dominio.disabled = true;

	u = '/consulta_paciente.php?coddoc='+coddoc+'&senha='+senha+"&id_dentista="+id_dentista;
	ajx.open("GET", u, true);
		ajx.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajx.setRequestHeader("Connection", "close"); 
        ajx.send(null);
		
		return false;

}

function resposta_paciente(){
	if(ajx.readyState == 4){
				if(ajx.status == 200){
					if(ajx.responseText == 'naoencontrado'){
						alert('Documentação não encontrada!');	
					}else{
						
						document.pacientes.action='http://'+ajx.responseText+'/paciente.php';
						document.pacientes.submit();
						
					}
					
					
				}
				
	}
	
}



function consulta_dominio(){
	ajx = ajax();
	post_form = true;
 	ajx.onreadystatechange = resposta_dominio;
	tipo_texto = 'Carregando'
	document.getElementById('resposta_dominio').innerHTML = '';
	document.getElementById('espera_dominio').innerHTML = '<img src=\'/images/loader.gif\'>';
	dominio =document.getElementById(ajx_form).dominio.value;
	document.getElementById(ajx_form).dominio.disabled = true;

	u = 'consulta_dominio.php?d='+dominio;
	ajx.open("GET", u, true);
		ajx.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajx.setRequestHeader("Connection", "close"); 
        ajx.send(null);
		
		return false;

}





function resposta_dominio(){
	if(ajx.readyState == 4){
				if(ajx.status == 200){
					
					if(ajx.responseText == 'dominio_pronto'){
						document.getElementById('resposta_dominio').innerHTML = "<label>    <input type=\"checkbox\" name=\"com_dominio\" id=\"com_dominio\" value=\"sim\" checked=\"checked\" onchange=\"enable_dominio()\"/>  Publicar dom&iacute;nio:<span class=\"azuldestaque\"> docviewer.com.br/"+ document.getElementById(ajx_form).dominio.value+" </span> </label> ";
						document.getElementById('espera_dominio').innerHTML = '';
						document.getElementById(ajx_form).realdominio.value = document.getElementById(ajx_form).dominio.value;
					if(post_form){
						post_form = false;
						
						if(postar_logo){
							postar_logo = false;
						verifica_dados();
						}else{
							
							post_logo = false;
						}
						
					}
					
					
					}else{
						post_form
						document.getElementById(ajx_form).dominio.disabled = false;
						document.getElementById('resposta_dominio').innerHTML = ajx.responseText;
						document.getElementById('espera_dominio').innerHTML = '';
					}
				}
	}
	
	
}

function enable_dominio(){
	
	if(document.getElementById(ajx_form).dominio.disabled){
	document.getElementById(ajx_form).dominio.disabled = false;
	}else{
	document.getElementById(ajx_form).dominio.disabled = true;
	}
}


function abreEstado(){
	if(ajx.readyState == 4){
				if(ajx.status == 200){
					
					document.getElementById('lista_estados').innerHTML = ajx.responseText;
					
				}
	}
	
	
}



function verifica_dados(){
	
	if(post_form){
		postar_logo = true;	
		return false;
	}
	ajx = ajax();
	
	dados = coleta_form(ajx_form);
	ajx.onreadystatechange = verificado;
	tipo_texto = 'Carregando';
	
	
	mostra_p = setInterval('mostra_processo()',0555);
        ajx.open("POST", ajx_url, true);
		ajx.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajx.setRequestHeader("Content-length", dados.length);
		ajx.setRequestHeader("Connection", "close"); 
        ajx.send(dados);
		
		return false;
}
function verifica_login(){
	
	ajx = ajax();
	
	login = escape(document.login.login.value);
	senha = escape(document.login.senha.value);
	dados = 'email='+login+'&senha='+senha;
	url = 'verifica_login.php';
	tipodeverificacao = 'login';
	
	ajx.onreadystatechange = verificado;
	tipo_texto = 'Carregando';
	mostra_p = setInterval('mostra_processo()',0555);
        ajx.open("POST", url, true);
		ajx.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajx.setRequestHeader("Content-length", dados.length);
		ajx.setRequestHeader("Connection", "close") 
        ajx.send(dados);
		
		return false;
}



function verificado(){
	mostra_processo(ajx.readyState)
	
	
	
if(ajx.readyState == 4){
				if(ajx.status == 200){
					clearInterval(mostra_p);
					tipo_texto = 'erro';
					eval(ajx.responseText);
				}
	}
	
	
	
}
var tipo_texto;
var mostra_p;
var tipodeverificacao;

function mostra_processo(tipo){
	if(tipo_texto != 'erro'){
	if(!div_msg){
	

	
	
		
		
	
	
	var ht = "<div align='center'><img src='/carregando.gif' style='margin-top:10%'></div>";
	}else{
		var ht = "<div align='center' class='simbous' style='margin:20px'>"+ '<img src="../icones/alerta.gif" alt="" width="33" height="34" align="left" />'+div_msg+"</div>";
	}
		
		
	if(tipodeverificacao != 'login'){
	//document[ajx_form].enviar.disabled = true;
	
	}
	document.getElementById('carrega').style.visibility= 'visible';

	var top = document.body.scrollTop;
	
	//var baixo = document.body.clientHeight;
	var baixo = document.body.clientHeight;
	var horizontal = document.body.clientWidth/ 2 - 200;
	
	posicao = Math.round(baixo + top) - 100;
	
	
	document.getElementById('carrega').style.left = horizontal+'px';
	document.getElementById('carrega').style.top = posicao+"px";
	document.getElementById('carrega').innerHTML = ht;
	
	}
}

var mostra_msg;
var div_msg;
function add_msg($msg){

	
}

function send_mensagem($msg){
	tipo_texto = false;
	div_msg = $msg;
	document.getElementById('carrega').style.visibility = 'visible';
	document.getElementById('carrega').style.width = '300px';
	mostra_p = setInterval('mostra_processo()',0555);
	setTimeout('close_carrega()',4000);
}
function send_focus(f){
	document[ajx_form][f].focus();
}

function close_carrega(){
	document.getElementById('carrega').style.visibility = 'hidden';
	document.getElementById('carrega').style.width = '100px';
	clearInterval(mostra_p);
	tipo_texto = 'erro';
	div_msg = false;
}



function redirect(url){
	
	location.href = url;	
}

function set_position_iframe(){
	
temp = document.body.cumulativeScrollOffset();
w = temp[1]+50;
//$('div_frame').setStyle({top:w+'px'});
//$('div_frame').morph('top:'+w+'px')

new Effect.Morph('div_frame', {
	style: {
     top:w+'px'
  }
 , // CSS class name
  duration: 0.9 // Core Effect properties
});
/*
$('div_frame').morph('top:'+w+'px');
*/


}

function recarregar(){
	
	window.history.go(0);
}

function troca_senha(){
	cria_sombra('troca_senha.php');
	
}

function open_maps(id_clinica){
	
	cria_sombra('abre_mapa.php?id_clinica='+id_clinica);

	set_dimencion_frame()
	
}

function set_dimencion_frame(){
	
new Effect.Morph('frame', {
	style: {
     height:470+'px',
	 width:880+'px'
  }
 , 
  duration: 0.9 
});

temporario = document.body.clientWidth;
left = (temporario/2) -440;


new Effect.Morph('div_frame', {
	style: {
     left:left +'px',
	 width:880+'px'
  }
 , 
  duration: 0.9 
});

	
}


function extraiScript(texto){
	
    // inicializa o inicio ><
    var ini = 0;
    // loop enquanto achar um script
    while (ini!=-1){
        // procura uma tag de script
        ini = texto.indexOf('<script', ini);
        // se encontrar
        if (ini >=0){
            // define o inicio para depois do fechamento dessa tag
            ini = texto.indexOf('>', ini) + 1;
            // procura o final do script
            var fim = texto.indexOf('</script>', ini);
            // extrai apenas o script
            codigo = texto.substring(ini,fim);
            // executa o script
            eval(codigo);
        }
    }
}

document.write('<script src="/inc/fichas.js"></script>');
	
	
	
function FormataData(Campo,teclapres) {
		var tecla = teclapres.keyCode;
		vr = document.form_paciente[Campo].value;
		vr = vr.replace( ".", "" );
		vr = vr.replace( ".", "" );
		vr = vr.replace( "/", "" );
		vr = vr.replace( "/", "" );
		tam = vr.length + 1;
		if ( tecla != 9 && tecla != 8 ){
			if ( tam > 2 && tam < 5 )
				document.form_paciente[Campo].value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
			if ( tam >= 5 && tam <= 10 )
				document.form_paciente[Campo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); 
		}
}		

window.onload = init;
