// JavaScript Document

function selection(obj, set_text_default){   
    /* @author Bernardo Rufino  
     * @license Creative Commons Developing Nations: http://creativecommons.org/licenses/devnations/2.0/  
     * @description Function to manipulate the actual selection  
     * @returns It returns an object with an attribute *text* with the current selection text and  
     *        a method *setText()* that receives one argument, the text to be set in the selection.  
     * @parameters The first is the object where you want the selection (DOM or String with id), and  
     *           the second (optional), a boolean, if is true then the *setText()* method will  
     *           append the text if there's no way to replace the selection.  
     *  
     *     !Nao retire essas informacoes!  
     *     !Do not drop this information!  
    */  
    if(obj.constructor == String){
	
		obj = document.getElementById(obj);
	
	}   
	
    var set_text = (set_text_default) ? function(text){obj.value += text;} : function(){return false;};   
    var selection = {text: null, setText: set_text};   
    if(document.selection){   
        
		var range = document.selection.createRange();   
		if (range.text !=""){
		
		selection.text = range.text;   
		selection.setText = function(text){   
		range.text = text.replace(/\\r?\\n/g, "\\r\\n");   
			
		}
		}
    } else if(typeof(obj.selectionStart) != "undefined"){   
        
		selection.text = obj.value.substring(obj.selectionStart, obj.selectionEnd);   
        selection.setText = function(text){   
			if(text != null && text != ""){
            obj.value = obj.value.substring(0, obj.selectionStart) + text + obj.value.substring(obj.selectionEnd);   
			}
        }   
    } else if(window.getSelection){   
        selection.text = window.getSelection().toString();   
		 
    }   
	
    return selection;   
}  

function handleEnter (field, event) {
        var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
        if (keyCode == 13) {
            field.value += "<br>"
        } 
    }      


function SetBold(txt){
	if (txt != ""){
		selected = selection(txt, true);
		selected.setText("<b>" + selected.text + "</b>");  
	}
}

function SetItalic(txt){
	if (txt != ""){
		selected = selection(txt, true);
		selected.setText("<i>" + selected.text + "</i>");  
	}
}

function SetUnderline(txt){
	if (txt != ""){
		selected = selection(txt, true);
		selected.setText("<u>" + selected.text + "</u>");  
	}
}
function SetCenter(txt){
	if (txt != ""){
		selected = selection(txt, true);
		selected.setText("<center>" + selected.text + "</center>");  
	}
}
function SetRight(txt){
	if (txt != ""){
		selected = selection(txt, true);
		selected.setText("<div align='right'>" + selected.text + "</div>");  
	}
}
function SetLeft(txt){
	if (txt != ""){
		selected = selection(txt, true);
		selected.setText("<Div align='left'>" + selected.text + "</div>");  
	}
}




function ajaxInit() {
	var req;
	try {
		 req = new ActiveXObject("Microsoft.XMLHTTP");
	} catch(e) {
		 try {
			  req = new ActiveXObject("Msxml2.XMLHTTP");
		 } catch(ex) {
		 try {
		   req = new XMLHttpRequest();
	     } catch(exc) {
	 		  alert("Esse browser não tem recursos para uso do Ajax");
	  		 req = null;
	     }
	 }
}
return req;
}

function PreviewPanel(texto) {
	ajax = ajaxInit();
	
	if(ajax) {
			ajax.open("GET","PreviewPanel.asp?texto=" + texto, true);
			ajax.onreadystatechange = function() {
				if(ajax.readyState == 4) {
					if(ajax.status == 200) {
						
						document.getElementById("PreviewPanel").innerHTML = ajax.responseText;
						
					}else{	
						alert(ajax.statusText)
					}
				}
			}
			ajax.send(null);
		
	}
}

function verificaEmail(email) {
	ajax = ajaxInit();
	if(ajax) {
		ajax.open("GET","verifica_email.asp?email=" + email, true);
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					
					document.getElementById("alerta").value = ajax.responseText;
					if(document.getElementById("alerta").value == 'N'){
						document.getElementById("textoAlerta").style.display = "";
					}
					
				}else{	
					alert(ajax.statusText)
				}
			}
		}
		ajax.send(null);		
	}
}

function Confirm(text, url) {
	var answer = confirm(text)
	if (answer){
		location.href = url;
		
	}
	else{
		
	}
}

function ConfirmForm(text) {
	var answer = confirm(text)
	if (answer){
		return true;
		
	}
	else{
		return false;
	}
}

var win = null;
function NewWindowPanel(valor){
	
	LeftPosition = (screen.width) ? (screen.width-400)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-300)/2 : 0;
	settings =
	'height=300,width=400,top='+TopPosition+',left='+LeftPosition+',scrollbars=Yes,resizable'
	win = window.open("PreviewPanel.asp?texto="+valor,"",settings)
}



var win = null;
function NewWindow(mypage,myname,w,h,scroll){
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings =
	'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
	win = window.open(mypage,myname,settings)
}

function foco(){
	document.forms[0].elements[0].focus();
}
function formatar(mascara, documento, e){
	var unicode = e.charCode? e.charCode : e.keyCode
	var i = documento.value.length;
	var saida = mascara.substring(0,1);
	var texto = mascara.substring(i)
	
	if (unicode==8) { 
		return true;
	}
	
	if (texto.substring(0,1) != saida){ 
		documento.value += texto.substring(0,1);
	}
	
}

function carregaCombo(id,url,codigo) {
	ajax = ajaxInit();
	url_combo = url + "codigo=" + codigo
	if(ajax) {
		ajax.open("GET",url_combo, true);
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					document.getElementById(id).innerHTML = ajax.responseText;					
				}else{	
					alert(ajax.statusText)
				}
			}
		}
		ajax.send(null);		
	}
}

function addFav(){
    var url      = "http://www.clinicagrafguimaraes.com.br";
    var title    = "Clínica Grafguimarães - Venha ser bem tratato aqui!";
    if (window.sidebar) window.sidebar.addPanel(title, url,"");
    else if(window.opera && window.print){
        var mbm = document.createElement('a');
        mbm.setAttribute('rel','sidebar');
        mbm.setAttribute('href',url);
        mbm.setAttribute('title',title);
        mbm.click();
    }
    else if(document.all){window.external.AddFavorite(url, title);}
}

function trocaPosition(idDiv)
{
	obj = document.getElementsByTagName("div");
	
	for(i=0;i<obj.length;i++)
	{
		aux = obj[i].title;
		if( (aux != idDiv) && (obj[i].title != "") )
		{
			obj[i].className = "listStatic";						
			
		}
		if( (aux == idDiv) && (obj[i].title != "") )
		{
			obj[i].className = "listRelative";
		}
		
	}	
	
}

//funções para menu suspenso
function pegaDados(codigo,controle,submenu,menu){
	conter = document.getElementById(controle).value;
	
	if (conter=="1"){
		h=100;
		document.getElementById(controle).value="0";
		document.getElementById(submenu).padding="0px";
		document.getElementById(submenu).style.lineHeight="0px";
		document.getElementById(submenu).style.display="none";
		
	}else{
		h=0;
		document.getElementById(controle).value="1";
		document.getElementById(submenu).style.lineHeight="normal";
		document.getElementById(submenu).style.display="";
		document.getElementById(controle).value="1";
	}
}
// JavaScript Document

function ajaxInit() {
	var req;
	try {
		 req = new ActiveXObject("Microsoft.XMLHTTP");
	} catch(e) {
		 try {
			  req = new ActiveXObject("Msxml2.XMLHTTP");
		 } catch(ex) {
		 try {
		   req = new XMLHttpRequest();
	     } catch(exc) {
	 		  alert("Esse browser não tem recursos para uso do Ajax");
	  		 req = null;
	     }
	 }
}
return req;
}
