// 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 verificaCheck (elemento, elementoOculto){
	
	if (document.getElementById(elemento).checked == true){
		document.getElementById(elementoOculto).style.display = '';
	}else{
		document.getElementById(elementoOculto).style.display = 'none';
	}
}

function foco(){
	document.forms[0].elements[0].focus();
}

function numeros(e){
    var whichCode = (window.Event)? e.which : e.keyCode;
    if(whichCode>=48 && whichCode<=57 || whichCode==0 || whichCode==32) return true;
    return false;
}

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);
	}
	
}