// JavaScript Document

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 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 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 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(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 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 verTecla(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 == 13 ) 
	{
    	document.getElementById("formBuscaEv").submit()
	}
 }
function carregaCombo(url, codigo, id) {
	//alert(url + "?codigo=" + codigo)
	ajax = ajaxInit();
	if(ajax) {
		ajax.open("GET",url + "?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 ocultaHist(numId)
{
	
	img = document.getElementById("img_" + numId);
	div_modulo = document.getElementById("h_" + numId);
	var aux = div_modulo.style.display;
	if (aux == "none")
	{
		div_modulo.style.display = "block";
		if (img)
		{
		    img.src = "imagens/-.gif";
		    img.alt = "Ocultar"
		}
	}
	if (aux == "block")
	{
		div_modulo.style.display = "none";
		if (img)
		{
		    img.src = "imagens/+.gif";
		    img.alt = "Mostrar"
		}
	}
}

function mostra_cad_banco(id)	
{
	
	obj = document.getElementById("banco")
	
	if(obj.value == 0)
	{
		div_banco = document.getElementById("div_bancos");
		div_banco.style.display = "block";	
		
	}
	if(obj.value > 0)
	{
		div_banco = document.getElementById("div_bancos");
		div_banco.style.display = "none";	
		
	}	

	
}

var retorno;								
var mpg_popup;								
window.name="loja";								
function fabrewin()

{

if(navigator.appName.indexOf("Netscape") != -1)

   mpg_popup = window.open("", "mpg_popup","toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,screenX=0,screenY=0,left=0,top=0,width=765,height=440");

else

   mpg_popup = window.open("", "mpg_popup","toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,screenX=0,screenY=0,left=0,top=0,width=765,height=440");

 

				window.location="aguarde.html";

				return true;

}

// código criado por : Leandro Lyria
								// por favor não copiar os códigos scripts
								// eu conheço meus códigos

function setcountdown(theyear,themonth,theday) {
	yr=theyear;mo=themonth;da=theday
}


var occasion=""
var message_on_occasion=""


var countdownwidth='300px'
var countdownheight='20px'
var countdownbgcolor=''
var opentags='<font face="Arial" >'
var closetags='</font>'



var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
var crosscount=''

function start_countdown(ano,mes,dia)
{
setcountdown(ano,mes,dia)
if (document.layers)
document.countdownnsmain.visibility="show"
else if (document.all||document.getElementById)
crosscount=document.getElementById&&!document.all?document.getElementById("countdownie") : countdownie
countdown()
}

if (document.all||document.getElementById)
document.write('<span id="countdownie" style="width:'+countdownwidth+'; background-color:'+countdownbgcolor+'"></span>')


function countdown(){
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getMonth()
var todayd=today.getDate()
var todayh=today.getHours()
var todaymin=today.getMinutes()
var todaysec=today.getSeconds()
var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec
futurestring=montharray[mo-1]+" "+da+", "+yr
dd=Date.parse(futurestring)-Date.parse(todaystring)
dday=Math.floor(dd/(60*60*1000*24)*1)
dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)
//if on day of occasion
if(dday<=0&&dhour<=0&&dmin<=0&&dsec<=1&&todayd==da){
if (document.layers){
document.countdownnsmain.document.countdownnssub.document.write(opentags+message_on_occasion+closetags)
document.countdownnsmain.document.countdownnssub.document.close()
}
else if (document.all||document.getElementById)
crosscount.innerHTML=opentags+message_on_occasion+closetags
return
}
//if passed day of occasion
else if (dday<=-1){
if (document.layers){
document.countdownnsmain.document.countdownnssub.document.write(opentags+"Occasion already passed! "+closetags)
document.countdownnsmain.document.countdownnssub.document.close()
}
else if (document.all||document.getElementById)
crosscount.innerHTML=opentags+"Occasion already passed! "+closetags
return
}
//else, if not yet
else{
if (document.layers){
document.countdownnsmain.document.countdownnssub.document.write(opentags+dday+ " days, "+dhour+" hours, "+dmin+" minutes, and "+dsec+" "+occasion+closetags)
document.countdownnsmain.document.countdownnssub.document.close()
}
else if (document.all||document.getElementById)
document.getElementById("contadoTempo").innerHTML="<strong> "+dday+ "</strong> dias, <strong> "+dhour+"</strong> horas, <strong> "+dmin+"</strong> minutos, e <strong> "+dsec+"</strong> seg."
}
setTimeout("countdown()",1000)
}

function selecaoQtd(id,valor)
{
	var num = id.split("_")
	
	
	idBoleto = "boleto_"+num[1]
	idCartao = "cartao_"+num[1]
	obj = document.getElementById("boleto").getElementsByTagName("div");	
	for(i=0;i<obj.length;i++)
	{
		
		if(obj[i].id == idBoleto)
		{
			obj[i].style.display='block'	
		}
		else
		{
			obj[i].style.display='none'	
		}
	}
	obj = document.getElementById("cartao").getElementsByTagName("div");	
	
	for(i=0;i<obj.length;i++)
	{
		
		if(obj[i].id == idCartao)
		{
			obj[i].style.display='block'	
		}
		else
		{
			obj[i].style.display='none'	
		}
	}
	
	if(document.getElementById("formaPagtoB").checked == true)
	{
		document.getElementById("formaPagtoB").click();	
	}
	else
	{
		obj = document.getElementById("cartao").getElementsByTagName("div");	
		var verif = false;
		for(i=0;i<obj.length;i++)
		{			
			if(obj[i].style.display == "block")
			{
				obj = obj[i].getElementsByTagName("input");	
				for(i=0;i<obj.length;i++)
				{			
					if(obj[i].type == "radio" && obj[i].checked == true)
					{
						obj[i].click();
						verif = true;
						break
					}
				}
			}
			
		}		
		if(verif == false)
		{
			document.getElementById("valorTotal").childNodes[0].nodeValue = valor	
		}
	}
	
	
}
function selecaoFormaPag(valor,tipo)
{
	if(tipo == "C")
	{
		document.getElementById("valorTotal").childNodes[0].nodeValue = valor
		valor = valor.replace("R$","")
		valor = valor.replace(" ","")
		document.getElementById("visatotal").value = valor
	}
	else
	{
		obj = document.getElementById("boleto").getElementsByTagName("div");	
		var num
		for(i=0;i<obj.length;i++)
		{
			
			if(obj[i].style.display == "block")
			{
				num = obj[i].id.split("_")
				break
			}
			
		}
		document.getElementById("valorTotal").childNodes[0].nodeValue = document.getElementById("vboleto_"+num[1]).value
	}
}

function verSelecaoQtd()
{
	obj = document.getElementsByTagName("input");	
	for(i=0;i<obj.length;i++)
	{
		if(obj[i].type == "radio" && obj[i].checked == true )
		{
			obj[i].click();
		}
	}
}

function validaFormPagamento()
{
	obj = document.getElementById("cestaCotasQtd").getElementsByTagName("input");	
	verif = false;
	for(i=0;i<obj.length;i++)
	{
		if(obj[i].type == "radio" && obj[i].checked == true )
		{
			obj[i].click();
			verif = true;
		}
	}	
	if( verif == true)
	{
		verif = false;	
		obj = document.getElementById("cestaFormaPag").getElementsByTagName("input");
		for(i=0;i<obj.length;i++)
		{
			if(obj[i].type == "radio" && obj[i].checked == true )
			{
				obj[i].click();
				verif = true;
			}
		}	
		if( verif == true)
		{
			document.getElementById("formPgto").submit();	
		}
		else
		{
			alert("Selecione a forma de pagamento desejada.")	
		}
	}
	else
	{
		alert("Selecione a quantidade de cotas desejada.")	
	}
}

function SubmetPopUpPag(url)
{
	if(validaFormGeral())
	{
		SubmetPopUp(url)	
	}
}

function SubmetPopUp(url){

with (document.formPagFinalizacao) {
method = "POST";
action=url;
var left = (parseInt(screen.width) - 1000) / 2
jan=window.open(url,'destino','left='+ left +',width=900px,height='+ (parseInt(screen.height)+200) +',scrollbars=1,status=0,toolbar=0,location=0,menubar=1,directories=0,resizable=1');
target='destino';
submit();
jan.focus();
}
return true;
}
function fecharJanela()
{
	window.close();	
}

function abrirMenu(wcontrole, wmenu, walturaIni, walturaMaxima, walfa, wopacity, wmOpacity){
controle = document.getElementById(wcontrole);
menu = document.getElementById(wmenu);
altura = document.getElementById(walturaIni);
alfa = document.getElementById(walfa);
opacity = document.getElementById(wopacity);
mOpacity = document.getElementById(wmOpacity);

if(controle.value == 0){
	menu.style.display = "";
	menu.style.height = altura.value + "px";
	
	if(parseInt(altura.value) != parseInt(walturaMaxima)){
		altura.value = parseInt(altura.value) + 19;
		alfa.value = parseInt(alfa.value) + 7.15;
		opacity.value = parseFloat(opacity.value) + 0.06;
		mOpacity.value = parseFloat(mOpacity.value) + 0.06;
		
		menu.style.filter = "alpha(opacity="+alfa.value+")";
		menu.style.MozOpacity = opacity.value;
		menu.style.opacity = mOpacity.value;
		setTimeout("abrirMenu('"+wcontrole+"', '"+wmenu+"', '"+walturaIni+"', '"+walturaMaxima+"', '"+walfa+"', '"+wopacity+"', '"+wmOpacity+"')",0);
	}else{
		controle.value = 1;
		menu.style.filter = "alpha(opacity=100)";
		menu.style.MozOpacity = "1";
		menu.style.opacity = "1";
	}
	
}else{
	menu.style.height = altura.value + "px";
	menu.style.filter = "alpha(opacity="+alfa.value+")";
	menu.style.MozOpacity = opacity.value;
	menu.style.opacity = mOpacity.value;
		
	if(parseInt(altura.value) == 19){
		controle.value = 0;
		alfa.value = 0;
		opacity.value = 0;
		mOpacity.value = 0;
		menu.style.display = "none";
	}else{
		altura.value = parseInt(altura.value) - 19;
		alfa.value = parseInt(alfa.value) - 7.15;
		opacity.value = parseFloat(opacity.value) - 0.04;
		mOpacity.value = parseFloat(mOpacity.value) - 0.04;
		setTimeout("abrirMenu('"+wcontrole+"', '"+wmenu+"', '"+walturaIni+"', '"+walturaMaxima+"', '"+walfa+"', '"+wopacity+"', '"+wmOpacity+"')",0);
	}
}
}

function ocultaBusca(id, formulario, titulo, tipo){
	div_canais = document.getElementById(id);
	var aux_class = div_canais.style.display;
	
	switch (tipo) {
	case "1":
		document.getElementById(formulario).action="eventos_lista.asp?ev=1&busca=ok";
		document.getElementById(titulo).innerHTML = "Busca em 'Casamentos'";
		break
	case "2":
		document.getElementById(formulario).action="eventos_lista.asp?ev=2&busca=ok";
		document.getElementById(titulo).innerHTML = "Busca em 'Chás Cozinha/Lingerie/Bar'";
		break
	case "3":
		document.getElementById(formulario).action="eventos_lista.asp?ev=3&busca=ok";
		document.getElementById(titulo).innerHTML = "Busca em 'Aniversários'";
		break
	case "4":
		document.getElementById(formulario).action="eventos_lista.asp?ev=4&busca=ok";
		document.getElementById(titulo).innerHTML = "Busca em 'Bodas'";
		break
	case "5":
		document.getElementById(formulario).action="eventos_lista.asp?ev=5&busca=ok";
		document.getElementById(titulo).innerHTML = "Busca em 'Formaturas'";
		break
	}
	
	if (aux_class == "none"){
		div_canais.style.display = "block";			
	}
}

function calcCota(id)
{
	idElement = id.replace("numero","")
	qtd = document.getElementById(id).value
	if(qtd == "")
	{
		qtd = 1	
		document.getElementById(id).value = 1
	}
	valor = document.getElementById("preco_total"+idElement).value	
	valor = valor.replace(".","")
	valor = valor.replace(",",".")
	if(parseFloat(valor) != 0 && valor !="" )
	{
		
		var total = parseFloat(valor) / parseInt(qtd)
		totalStr = new String(total) 		
		document.getElementById("cota"+idElement).value = totalStr.replace(".",",")
		formatarValor("cota"+idElement)
		
	}
	
}
function verValor()
{
	for(i=1;i< document.getElementById("totPresente").value; i++)
	{
		calcCota("numero"+i)	
	}
	if(validaFormGeral('presenteValor'))
	{
		if(document.getElementById("minimo1"))
		{
			for(i=1;i< document.getElementById("totPresente").value; i++)
			{
				idElement = i
				valor = document.getElementById("preco_total"+idElement).value
				valor = valor.replace(".","")
				valor = valor.replace(",",".")
				if(parseFloat(valor) >= parseFloat(document.getElementById("minimo"+idElement).value) )
				{
					if(parseFloat(valor) > parseFloat(document.getElementById("maximo"+idElement).value) )
					{
						alert("Atenção, " + document.getElementById("preco_total"+idElement).title + " com valor superior que o máximo estipulado")
						document.getElementById("maximo"+idElement).focus()	
						return false
						break
					}			
						
				}
				else
				{
					alert("Atenção, " + document.getElementById("preco_total"+idElement).title + " com valor inferior que o mínimo estipulado")
					document.getElementById("minimo"+idElement).focus()
					return false
					break
				}
			}
		}
		return true
	}
	else
	{
		return false
	}	
}
