// 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 no 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)
}

function mostraSub(url, dir, codigo, id, idcont){
	//alert(dir)
	nome = ""
	cont = document.getElementById(idcont).value;
	if (dir == "produto.asp"){
		nome = "imgCatProd";
	}else{
		nome = "imgCatUsd";
	}
	ajax = ajaxInit();
	if (cont == "0"){
		
		if(ajax) {
   		    document.getElementById(id).style.display= "";
			document.getElementById(id).innerHTML ="<div bgcolor='#F3F5F8' style='text-align:center;background-color:#F3F5F8;padding:4px;' class='menu_link' ><img src='imagens/ajax-loader2.gif' /></div>"
			ajax.open("GET",url + "?dir=" + dir + "&codigo=" + codigo, true);
        	
			ajax.onreadystatechange = function() {
				if(ajax.readyState == 4) {
					if(ajax.status == 200) {
						document.getElementById(id).innerHTML = ajax.responseText;
						document.getElementById(idcont).value="1";
						document.getElementById(nome).innerHTML = "- ";
					}else{
						alert(ajax.statusText);
					}
				}
			}
			ajax.send(null);
			
		}
		setTimeout("mostraCont('"+ dir +"',"+codigo+",'')",0);
	}else{
		document.getElementById(id).innerHTML = "";
		document.getElementById(id).style.display= "none";
		document.getElementById(idcont).value="0";
		document.getElementById(nome).innerHTML = "+ ";
	}
}
function mostraCont(url, cod_categ, cod_sub){
	ajax2 = ajaxInit();
	//alert(url)
	if(ajax2) {
		ajax2.open("GET", url + "?cod_categoria=" + cod_categ +"&cod_subcategoria="+ cod_sub, true);
		ajax2.onreadystatechange = function() {
			if(ajax2.readyState == 4) {
				if(ajax2.status == 200) {
					document.getElementById("conteudo").innerHTML = ajax2.responseText;
				}else{
					alert(ajax2.statusText);
				}
			}
   		}
		ajax2.send(null);

	}
}

function mostraCategUsada(url, dir){
	cont =  document.getElementById("contCategUsada").value;
	if(cont == "0"){
		ajax = ajaxInit();
		
		if(ajax) {
			document.getElementById("categoriaUsada").innerHTML ="<div bgcolor='#F3F5F8' style='text-align:center;background-color:#F3F5F8;padding:4px;' class='menu_link' ><img src='imagens/ajax-loader2.gif' /></div>"
			ajax.open("GET",url +"?dir=" + dir, true);
			ajax.onreadystatechange = function() {
				if(ajax.readyState == 4) {
					if(ajax.status == 200) {
						document.getElementById("categoriaUsada").innerHTML = ajax.responseText;
						document.getElementById("contCategUsada").value="1";
						document.getElementById("imgUsd").innerHTML = "- ";
					}else{
						alert(ajax.statusText);
					}
				}
			}
			ajax.send(null);
		}
	}else{
		document.getElementById("categoriaUsada").innerHTML = "";
		document.getElementById("contCategUsada").value = "0";
		document.getElementById("imgUsd").innerHTML = "+ ";
	}
}

function carregaCombo(id,codigo) {
	ajax = ajaxInit();
	if(ajax) {
		ajax.open("GET","combo_subcategoria.asp?codigo=" + codigo, 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 mudaProd(codigo){
	
	ajax2 = ajaxInit();
	if(ajax2) {
		ajax2.open("GET","produtos.asp?produto=" + codigo, true);
		ajax2.onreadystatechange = function() {
			if(ajax2.readyState == 4) {
				if(ajax2.status == 200) {
					document.getElementById("conteudo").innerHTML = ajax2.responseText;
				}else{
					alert(ajax2.statusText);
				}
			}
   		}
		ajax2.send(null);

	}
}
function selTab(tab){
	tabs = "tabdesc,tabcarac,tabespec,tabopc,tabaplic";
	tabs = tabs.split(",");
	//aqui cai no que eu j tinha dito: voc poder ter quantas abas quiser. Aqui simplesmente contar quantas abas voc tem.
	
	for (n=0;n < tabs.length ;n++)
	{
	
			   //aqui vai comparar se o nome do id do seu div  igual a aba que voc selecionou. Se for, ele vai mostrar o contedo da aba selecionada. Caso contrrio, no exibe nada.
			   
			   if (tabs[n] == tab)
			   {
				   document.getElementById(tabs[n]).style.display = "inline";
				   document.getElementById('col' + tab.substring(3,tab.length)).style.borderBottom = "none";
				   document.getElementById('col' + tab.substring(3,tab.length)).style.backgroundColor = "#e0eeef";
				}
			   else
			   {
				   document.getElementById(tabs[n]).style.display = "none";
				   document.getElementById('col' + tabs[n].substring(3,tabs[n].length)).style.borderBottom = "#CCCCCC 1px solid";
				   document.getElementById('col' + tabs[n].substring(3,tabs[n].length)).style.backgroundColor = "";
			   }
	}
}
function mostraProd(codigo){
	ajax2 = ajaxInit();
	
	if(ajax2) {
		ajax2.open("GET","produtos.asp?produto=" + codigo, true);
		ajax2.onreadystatechange = function() {
			if(ajax2.readyState == 4) {
				if(ajax2.status == 200) {
					document.getElementById("conteudo").innerHTML = ajax2.responseText;
				}else{
					alert(ajax2.statusText);
				}
			}
   		}
		ajax2.send(null);

	}
}
function mostraUsado(codigo){
	ajax2 = ajaxInit();
	
	if(ajax2) {
		ajax2.open("GET","maq_usadas.asp?produto=" + codigo, true);
		ajax2.onreadystatechange = function() {
			if(ajax2.readyState == 4) {
				if(ajax2.status == 200) {
					document.getElementById("conteudo").innerHTML = ajax2.responseText;
				}else{
					alert(ajax2.statusText);
				}
			}
   		}
		ajax2.send(null);

	}
}
function abrirVideo(url, nome_pag, tamanho){//v2.0
	//alert("teste")
	window.open(url, nome_pag, tamanho);
}

function verHora(){
	var h = new Date();
	var hora = h.getHours();
	if((hora >="0") && (hora <"12")){
		document.getElementById("hora").innerHTML = "Bom dia,";
	}
	if((hora >="12") && (hora <"18")){
		document.getElementById("hora").innerHTML = "Boa tarde,";
	}
	if((hora >="18") && (hora <"0")){
		document.getElementById("hora").innerHTML = "Boa noite,";
	}
}

function ajaxGet(url,id) {
	ajax = ajaxInit();
	if(ajax) {
		
		document.getElementById(id).innerHTML = "<div align='center'><div align='center' style=' padding:10px;height:80px;width:200px;border:1px solid #979ABD;background-color:#EDEEF3;font-size:12px;font-family:Verdana, Arial, Helvetica, sans-serif;color:#8694B9;' align='center'><div><img src='imagens_adm/ajax-loader.gif' /></div><strong>Carregando</strong></div></div><br />";
		ajax.open("GET", url, true);
		ajax.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");    	       
		ajax.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");    	        
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");	   	ajax.setRequestHeader("Pragma", "no-cache");
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 4) {
				
				if(ajax.status == 200) {
					document.getElementById(id).innerHTML = '<div style="padding:5px;height:24px;background-color:#EEF2F7;"  align="left"><a style="color:black;font-family:Tahoma;text-decoration:none;font-size:11px;" href="logout.asp"> <img border="0" style="position:relative;top:4px;" src="imagens_adm/logout.gif" /> Sair</a> <a style="color:black;font-family:Tahoma;text-decoration:none;font-size:11px;" href="default.asp"> <img border="0" style="position:relative;top:4px;"  src="imagens_adm/home.gif" /> Home</a></div>'+ajax.responseText;
					
				}else{
					alert(ajax.statusText);
				}
			}
		}
		ajax.send(null);
	}
}


function abrirDiv(divClicada, controleClicado, divGeral, controleGeral, totalReg){
	cont = parseInt(document.getElementById(totalReg).value) + 1;
	
	//alert(divClicada+','+ controleClicado+','+ divGeral+','+ controleGeral+','+ cont);
	
	for (i=1; i < cont; i++){
		if(divClicada != divGeral+i){
			document.getElementById(divGeral+i).style.display="none";
			document.getElementById(controleGeral+i).value="0";
		}
	}
	
	if(document.getElementById(controleClicado).value == "0"){
		document.getElementById(divClicada).style.display="";
		document.getElementById(controleClicado).value="1";
	}else{
		document.getElementById(divClicada).style.display="none";
		document.getElementById(controleClicado).value="0";
	}

}

function subLine(id){
	document.getElementById(id).style.textDecoration="underline";
}

function noLine(id){
	document.getElementById(id).style.textDecoration="none";
}

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 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 checado(name, cod, id){
	
	novoCod="";
	if(document.getElementById(name).checked){
		document.getElementById(id).value += cod+"|"; 
	}
	if(document.getElementById(name).checked==false){
		valores = document.getElementById(id).value;
		document.getElementById(id).value="";
		codigos = valores.split("|");
		
		for (i=0;i<=codigos.length-2;i++){
			
			if(codigos[i] != cod){
				novoCod = novoCod+codigos[i]+"|";
				document.getElementById(id).value=novoCod;
			}
		}
	}
}

function tipoPessoa()
{
	if (document.getElementById("pfisica").checked == true)
	{
		document.getElementById("cadastro_pfisica").style.display = '';
		document.getElementById("cadasto_pjuridica").style.display = 'none';
	}
	if (document.getElementById("pjuridica").checked == true)
	{
		document.getElementById("cadastro_pfisica").style.display = 'none';
		document.getElementById("cadasto_pjuridica").style.display = '';
	}
}
function tipoEndereco()
{
	if (document.getElementById("cadastrado").checked == true)
	{
		document.getElementById("endereco_cadastrado").style.display = '';
		document.getElementById("endereco_n_cadastrado").style.display = 'none';
	}
	if (document.getElementById("ncadastrado").checked == true)
	{
		document.getElementById("endereco_cadastrado").style.display = 'none';
		document.getElementById("endereco_n_cadastrado").style.display = '';
	}
}

function selecionaFrete(tipo,valor_frete,valor_total){
	document.getElementById('totalmostra').value='R$ ' + valor_total;
	document.getElementById('totalenvia').value=valor_total;
	document.getElementById('freteenvia').value=valor_frete;
	document.getElementById('freteescolhido').value='R$ ' + valor_frete;
	document.getElementById('tipo_frete').value=tipo;
}