// 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;
}

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 preecheInput(idCodigo,idRet,tabela,campo,campoRet) 
{
	ajax = ajaxInit();
	codLoc = document.getElementById(idCodigo);
	if( (ajax) && (codLoc) && (codLoc.value != "") )
	{
		wurl = "buscaCodigo.asp?tabela="+tabela+"&campo="+campo+"&codigo="+codLoc.value+"&campoPreenche="+campoRet;
		extra = document.getElementById("extra")
		if( (extra) && (extra.value != "") )
		{
			vet_extra = extra.value.split("|");
			campoExtra = vet_extra[0];
			valorCampo = document.getElementById(vet_extra[1]);
			if (valorCampo)
			{
				wurl = wurl + "&campoExtra=" + campoExtra + "&valorExtra=" + valorCampo.value
			}
		}	
		
		ajax.open("GET",wurl, true);
		ajax.onreadystatechange = function()
		{
			if(ajax.readyState == 4)
			{
				if(ajax.status == 200)
				{
					retornoBusca = ajax.responseText;
					if (retornoBusca == "n_encontrado")
					{
						alert("Registro não encontrado");
						codLoc.value = "";
						document.getElementById(idRet).value="";						
						
					}
					else
					{
						document.getElementById(idRet).value = retornoBusca;
						
						
					}								
				}else{	
					alert(ajax.statusText)
				}
			}
		}
		ajax.send(null);	
		
	}
	
}
function preencheValor(idCodigo,idRet,tabela,campo,campoRet) 
{
	ajax = ajaxInit();
	codLoc = document.getElementById(idCodigo);
	if( (ajax) && (codLoc) && (codLoc.value != "") )
	{
		wurl = "buscaCodigo.asp?tabela="+tabela+"&campo="+campo+"&codigo="+codLoc.value+"&campoPreenche="+campoRet;
		extra = document.getElementById("extra")
		if( (extra) && (extra.value != "") )
		{
			vet_extra = extra.value.split("|");
			campoExtra = vet_extra[0];
			valorCampo = document.getElementById(vet_extra[1]);
			if (valorCampo)
			{
				wurl = wurl + "&campoExtra=" + campoExtra + "&valorExtra=" + valorCampo.value
			}
		}	
		wurl = wurl + "&retValor=s";
		ajax.open("GET",wurl, true);
		ajax.onreadystatechange = function()
		{
			if(ajax.readyState == 4)
			{
				if(ajax.status == 200)
				{
					retornoBusca = ajax.responseText;
					if (retornoBusca == "n_encontrado")
					{
						codLoc.value = "";
						document.getElementById(idRet).value="";
					}
					else
					{
						var idAux = idRet.replace("v_unitario","qtd")
						var qtd = 0;
						document.getElementById(idRet).value = retornoBusca;
						formatarValor(idRet)
						retornoBusca = retornoBusca.replace(",",".")
						qtd = document.getElementById(idAux).value
						
						if( document.getElementById(idAux).value == "")
						{
							document.getElementById(idAux).value = "1"
							qtd = 1
						}
						retornoBusca = parseFloat(retornoBusca) * parseInt(qtd)
						idAux = idRet.replace("v_unitario","v_total")
						str = new String(retornoBusca)
						document.getElementById(idAux).value = str.replace(".",",")
						formatarValor(idAux)
						calcTotalVenda()
						
					}								
				}else{	
					alert(ajax.statusText)
				}
			}
		}
		ajax.send(null);	
		
	}
}
function alteraQtd(id)
{
	var idValor = id
	idValor = idValor.replace("qtd","v_unitario")	
	if( parseInt(document.getElementById(id).value) > 0 )
	{
		if(document.getElementById(idValor) && document.getElementById(idValor).value != "" && document.getElementById(id).value != "")
		{
			var valor = document.getElementById(idValor).value
			valor = valor.replace(".","")
			valor = valor.replace(",",".")
			total = parseInt(document.getElementById(id).value) * parseFloat(valor)
			total = parseFloat(total)
			strTotal = new String(total)
			var idTotal = id.replace("qtd","v_total")	
			document.getElementById(idTotal).value = strTotal.replace(".",",")
			formatarValor(idTotal)
			calcTotalVenda()
		}
	}
	else
	{
		alert("Atenção, quantidade deve ser superior a 0")	
		document.getElementById(id).value = "1"
	}
}


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";
	}
}


function isInt(id)
{
    var valor_text = valor;
	var str = /^\d+$/;
	return str.test(valor_text);
}


function formatar(id, mask)
{
	src = document.getElementById(id);
	var i = src.value.length;
  	var saida = mask.substring(0,1);
  	var texto = mask.substring(i)
	if (texto.substring(0,1) != saida)
  	{
    	src.value += texto.substring(0,1);
  	}
}
function formatarFone(id)
{
	src = document.getElementById(id);
	var i = src.value.length;
	if(i==1)
	{
		src.value = "(" + src.value;
	}
	if(i==3)
	{
		src.value = src.value + ") " 
	}
	if(i==9)
	{
		src.value = src.value + "-" 
	}
	
	
}
function formatarValor(id)
{
	obj = document.getElementById(id)
	valor_aux = obj.value;
	if( valor_aux != "")
	{
		if(isFloat(valor_aux) == true)
		{
			pos = valor_aux.search(",");
			if(pos != -1)
			{
				vet_valor = valor_aux.split(",");
				valor = vet_valor[0];
				valor= valor.replace(".","")
				valor= valor.replace(".","")
				valor= valor.replace(".","")
				valor= valor.replace(".","")
				valor= valor.replace(".","")
				valor = valor * 100;
				valor = valor / 100;
				valor = new String(valor);	
				if(valor != "")
				{
					if (vet_valor[1].length == 2)
					{
						valor = formataFloatPonto(valor) + "," + vet_valor[1];
						document.getElementById(id).value = valor;
					}
					else
					{
						if (vet_valor[1].length > 2)
						{
							vet_valor[1] = vet_valor[1].substring(0,2);	
							valor = formataFloatPonto(valor) + "," + vet_valor[1]
							document.getElementById(id).value = valor;
						}
						else
						{
							valor = formataFloatPonto(valor) + "," + vet_valor[1] + "0"	
							document.getElementById(id).value = valor;
						}
					}
				}
				else
				{
					valor = formataFloatPonto(valor) + ",00"; 
					document.getElementById(id).value = valor;
				}
			}
			else
			{
				valor_aux= valor_aux.replace(".","")
				valor_aux= valor_aux.replace(".","")
				valor_aux= valor_aux.replace(".","")
				valor_aux= valor_aux.replace(".","")
				valor_aux= valor_aux.replace(".","")
				obj = document.getElementById(id);
				obj.value = formataFloatPonto(valor_aux) + ",00"
				
			}
		}
	}
	
}
function formataFloatPonto(valorParam)
{
	valor = valorParam;	
	tam_vet = parseInt(valor.length / 3)
	var vet_aux = new Array();				
	cont = 0;
	contVet = 0
	if (valor != "")
	{
		if (valor.length > 3)
		{
				
			for(i=valor.length;i>0;i--)
			{
				if( ( cont % 3 == 0)  && (cont > 0) )
				{
					vet_aux[contVet] = i;
					contVet = contVet + 1;
				}	
				cont = cont + 1
			}
			for(i=0;i<vet_aux.length;i++)
			{
				valor = valor.substring(0,vet_aux[i]) + "." + valor.substring(vet_aux[i],valor.length);				
			}
			
		}
	}
	else
	{
		valor = "0";
	}
	return valor
}

function insNumeric(obj,event)
{
    var tecla = event.charCode;
    var ie = event.keyCode;
    if (!event) event = window.event;
    var code;
    if (event.keyCode) code = event.keyCode;
    else if (event.which) code = event.which; // Netscape 4.?
//se nao for número nem parentesis ou espaço
    if ((code < 48 || code > 59) && (code != 8 ) ){
      event.returnValue = false;
      if (event.which){
        event.preventDefault();
      }
      return false;
    }else{
      event.returnValue = true;
      return true;
    }
 }
function insFloat(obj,event)
{
    var tecla = event.charCode;
    var ie = event.keyCode;
    if (!event) event = window.event;
    var code;
    if (event.keyCode) code = event.keyCode;
    else if (event.which) code = event.which; // Netscape 4.?
//se nao for número nem parentesis ou espaço
    if ((code < 48 || code > 59) && (code != 8 ) & (code != 44)){
      event.returnValue = false;
      if (event.which){
        event.preventDefault();
      }
      return false;
    }else{
      event.returnValue = true;
      return true;
    }
 } 

function isFloat(valor)
{
	var valor_text = valor;
	var str = /^[+-]?((\d+|\d{1,3}(\.\d{3})+)(\,\d*)?|\,\d+)$/;
	return str.test(valor_text);
}

function pegarHora(campo){
	hora = new Date;
	wHora = hora.getHours();
	wMin = hora.getMinutes();
	if(wMin < 10){
		wMin = "0" + wMin;
	}
	wSec = hora.getSeconds();
	if(wSec < 10){
		wSec = "0" + wSec;
	}
	
	//wHoraF = wHora +":"+ wMin +":"+ wSec;
	wHoraF = wHora +":"+ wMin;
	
	document.getElementById(campo).innerHTML = wHoraF;
	
	setTimeout("pegarHora('"+campo+"')",1);
}
function pegarData(){
	var nomeDia;
	data = new Date;
	dia  = data.getDay();
	mes = data.getMonth();
	ano = data.getFullYear();
	
	switch(dia){
		case 0:
			nomeDia = "Domingo";
		case 1:
			nomeDia = "Segunda-feira";
		case 2:
			nomeDia = "Terça-feira";
		case 3:
			nomeDia = "Quarta-feira";
		case 4:
			nomeDia = "Quinta-feira";
		case 5:
			nomeDia = "Sexta-feira";
		case 6:
			nomeDia = "Sábado";
	}
	
	switch(mes){
		case 0:
			nomeMes = "janeiro";
		case 1:
			nomeMes = "fevereiro";
		case 2:
			nomeMes = "março";
		case 3:
			nomeMes = "abril";
		case 4:
			nomeMes = "maio";
		case 5:
			nomeMes = "junho";
		case 6:
			nomeMes = "julho";
		case 7:
			nomeMes = "agosto";
		case 8:
			nomeMes = "setembro";
		case 9:
			nomeMes = "outubro";
		case 10:
			nomeMes = "novembro";
		case 11:
			nomeMes = "dezembro";
	}
	
	campoData = document.getElementById("data_semana");
	campoData.innerHTML = nomeDia +", "+ dia +" de "+ nomeMes +" de "+ ano +".";	
}

var urlImagem = "http://www.fortalezaguapore.com.br/intranet/imagens/";
function liberarCampos()
{
	obj_input = document.getElementsByTagName('input');	
	ob_select = document.getElementsByTagName('select');
	ob_area = document.getElementsByTagName('textarea');
	for (i=0;i<obj_input.length;i++)
	{
		if (obj_input[i].name != "codigo")
		{
			obj_input[i].disabled = "";	
		}
	}
	for (i=0;i<ob_select.length;i++)
	{
		ob_select[i].disabled = "";	
	}
	for (i=0;i<ob_area.length;i++)
	{
		ob_area[i].disabled = "";	
	}
}
function bloqCampos()
{
	obj_input = document.getElementsByTagName('input');	
	ob_select = document.getElementsByTagName('select');
	ob_area = document.getElementsByTagName('textarea');
	for (i=0;i<obj_input.length;i++)
	{
		if (obj_input[i].name != "codigo")
		{
			obj_input[i].disabled = "disabled";	
		}
	}
	for (i=0;i<ob_select.length;i++)
	{
		ob_select[i].disabled = "disabled";	
	}
	for (i=0;i<ob_area.length;i++)
	{
		ob_area[i].disabled = "disabled";	
	}
}

function janelaBusca(url,id,idIns,IdDel,largura,altura)
{
	element = document.getElementById(id)
	extra = document.getElementById("extra")
	wurl = url + "&digitado="+ element.value + "&camposIns=" + idIns + "&camposApagar=" + IdDel	
	
	if( (extra) && (extra.value != "") )
	{
		vet_extra = extra.value.split("|");
		campoExtra = vet_extra[0];
		valorCampo = document.getElementById(vet_extra[1]);
		if (valorCampo)
		{
			wurl = wurl + "&campoExtra=" + campoExtra + "&valorExtra=" + valorCampo.value
		}
	}
	
	winDt = window.open(wurl,'','top=300,left=600,width='+ largura +',height='+ altura +',scrollbars=1,status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0');
	winDt.focus();
}

function excluirReg(url)
{
	if (confirm("Deseja realmente excluir o registro"))
	{
		location.href=url;	
	}
}
function fechar()
{
	close();
}
function salvar(id)
{
	document.getElementById(id).submit();	
}

function abrirMenu(wcontrole)
{
	$("#"+wcontrole).slideToggle("normal")
}

function janelaRelatorio(wurl)
{
	var winDt = window.open(wurl,'','scrollbars=1,status=1,toolbar=1,location=0,menubar=1,directories=1,resizable=1');
	winDt.focus();
}

function horaImpressao()
{
	data_aux = new Date;
	whora = data_aux.getHours()
	wminuto = data_aux.getMinutes()
	
	dia = data_aux.getDate();
	mes = data_aux.getMonth();
	mes = parseInt(mes) + 1;
	ano = data_aux.getFullYear();
	if (dia < 10)
	{
		dia = "0" + dia;
	}
	if (mes < 10)
	{
		mes = "0" + mes;
	}
	if (whora < 10)
	{
		whora = "0" + whora;
	}
	if (wminuto < 10)
	{
		wminuto = "0" +wminuto;
	}
	
	obj = document.getElementsByTagName("span");
	
	for (i=0;i<obj.length;i++)
	{
		
		if (obj[i])
		{
			if (obj[i].className == "hora")
			{
				obj[i].innerHTML = dia + "/" + mes + "/" + ano + " " + whora + ":" + wminuto;
			}
		}
	}		
	
}



var index = 1
objIntervalo = new Object();
function troca(pos)
{
	index = pos
	
	for(i=1;i<8;i++)
	{
		if(i!= index)
		{
			document.getElementById("end_" + i).style.display = "none"	
			document.getElementById("label_" + i).className = "numEnd"	
		}
		else
		{
			document.getElementById("end_" + index).style.display = "block"
			document.getElementById("label_" + index).className = "numEndFundo"	
		}
	}
	
	index = parseInt(index)+ 1;
	
	if( index == 8 )
	{
		index = 1						
	}
	clearInterval(objIntervalo)
	trocaEndAuto(index)
}
function trocaEndAuto(pos)
{
 	objIntervalo = setInterval("troca('"+ pos +"')", 5000);
}
function mostraPag(id,idImg)
{
	if( $("#"+id).is(":hidden") ) 
	{
       $("#"+id).slideDown("slow");
	    $("#"+idImg).attr("src","imagens/seta2.jpg")
    } 
	else 
	{
        $("#"+id).slideUp("slow");
		$("#"+idImg).attr("src","imagens/seta.jpg")
    }
				
}


function ocultaCanais(id)
{
	id_par = id;
	vet_id = id_par.split("_");
	id_element = vet_id[1];
	img = document.getElementById(id);
	div_canais = document.getElementById(id_element);
	var aux_class = div_canais.style.display;
	if (id_element == "equipe")
	{
		if (aux_class == "block")
		{
			div_canais.style.display = "none";
			img.src = "imagens/icon_mais.jpg";
			img.alt = "Mostrar"
		}
		if (aux_class == "none")
		{
			div_canais.style.display = "block";
			img.src = "imagens/icon_menos.jpg";
			img.alt = "Esconder"
			document.getElementById("colaboradores").style.display = "none";
			
		}	
	}
	else
	{
		if (aux_class == "block")
		{
			div_canais.style.display = "none";
			img.src = "imagens/lat_fecha.jpg";
			img.alt = "Mostrar"
		}
		if (aux_class == "none")
		{
			div_canais.style.display = "block";
			img.src = "imagens/lat_abre.jpg";
			img.alt = "Esconder"			
		}
	}
}
/*
function validaCont(id)
	{			
		if(validaFrmGeral2(id) == false)
		{
			return false;				
		}
						
		if(document.getElementById("mensagem").value == "")
		{
			alert("Preencha o campo Mensagem");
			document.getElementById("mensagem").focus();
			return false;
		}
		if(document.getElementById("modalidade").value == "")
		{
			alert("Preencha o campo Modalidade");
			document.getElementById("modalidade").focus();
			return false;
		}
		if(document.getElementById("estado").value == "")
		{
			alert("Preencha o campo Estado");
			document.getElementById("estado").focus();
			return false;
		}
		if(document.getElementById("estado_u").value == "")
		{
			alert("Preencha o campo Estado - Universidade");
			document.getElementById("estado_u").focus();
			return false;
		}
		if(document.getElementById("comentario").value == "")
		{
			alert("Preencha o campo Comentário");
			document.getElementById("comentario").focus();
			return false;
		}
		
	}
	*/
function checadoTodos(qtd,name,namehidden)
{
	
	quantidade  = qtd;
	valor = 0;
	if (document.getElementById(name).checked == true)
	{
		document.getElementById(namehidden).value= "";
		for(i=1; i<=quantidade; i++)
		{
			document.getElementById("c_"+i).checked = true;
			valor = document.getElementById("c_"+i).value;
			document.getElementById(namehidden).value += valor+"|";
		}
	}
	else
	{
		for(i=1; i<=quantidade; i++)
		{
			document.getElementById("c_"+i).checked = false;
			document.getElementById(namehidden).value = "";
		}
	}
}

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 SubmetPopUp()
{
	if(navigator.appName.indexOf('Internet Explorer')>0)
	{
	 	document.getElementById('formPagSeguro').target = "_blank"		
		document.getElementById('formPagSeguro').submit();

	}
	else
	{
	 	with (document.formPagSeguro) {
		method = "POST";
		
		action=document.getElementById('formPagSeguro').action;
		jan=window.open(action,'destino','width=800','height=600,scrollbars=1,status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1');
		target='destino';
		submit();
		jan.focus();
	}

}
return true;
}
function contador(field,MaxLength) 
{
      
  //obj = document.all(field);
  document.getElementById(field)
  if (MaxLength !=0) {
	 if (  document.getElementById(field).value.length > MaxLength)  {
		  document.getElementById(field).value =   document.getElementById(field).value.substring(0, MaxLength);
		}
  }
  document.getElementById('contador').innerHTML =   document.getElementById(field).value.length + '/' + MaxLength;
}
function janelaForm(largura,altura,url)
{
	var winDt = window.open(url,'','top=300,left=150,width='+ largura +',height='+ altura +',scrollbars=1,status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1');
	winDt.focus();
}
function janelaExterna(url)
{
	var largura = window.width 
	var altura =  window.height
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var wleft = parseInt( ( parseInt(xScroll)-1100 ) /2 )
	largura = parseInt(xScroll)-1000
	altura = yScroll
	//alert(xScroll + " - " + yScroll)
	var winDt = window.open(url,'','left='+ wleft +',width=1050,height='+ altura +',scrollbars=1,status=1,toolbar=1,location=1,menubar=1,directories=1,resizable=1');
	winDt.focus();
}

function verifCheck(campo1, campo2){
	if(document.getElementById(campo1).checked == true){
		document.getElementById(campo2).style.display = "";
	}else{
		document.getElementById(campo2).style.display = "none";
	}
}
function carregaUrlExterna()
{
	$(".novaJanela").click(function(){
		wurl = $(this).attr("href")
		janelaExterna(wurl)
		return false
	})
	$(".banners").click(function(){
		wurl = $(this).attr("href")
		codigo = $(this).attr("id").split("_")[1]
		ajax = ajaxInit();
		url_combo = "contador_clique.asp?codigo=" + codigo
		
		if(ajax) {
			
			ajax.open("GET",url_combo, true);			
			ajax.send(null);		
		}
		setTimeout(function(){
			//janelaExterna(wurl)
			location.href=wurl
		},700)
		return false
	})
	
}
function incluiBanner(url,codigo,id,banner)
{
	$("#"+id).append("<a href='http://"+ url + "' id='b_" + codigo + "' class='banners'><img src='http://www.pubvet.com.br/imagens/banners/maxi/" + banner + "' alt=''></a>")	
}