// JavaScript Document

var valor_total = 0
var totalParcelas = 07

function formata_valor(valor)
{
	s = new String()
	s.value = valor.toString()
	posicao = s.value.indexOf(".")
	if (posicao == -1)
	  s.value += ",00"
	else
	{
		s.value = s.value + "0";
		s.value = s.value.substr(0, posicao + 3)
		s.value = replaceString(".", ",", s.value)
	}
	return s.value
}

//Função abaixo não é de minha autoria

function replaceString(oldS, newS, fullS) {
   var indice;
  
   indice = fullS.indexOf(oldS);
      
   if (indice < 0)
     return fullS;

   nfullS = "";
   oldindice = 0;
  
   while(indice < fullS.length && indice != -1) {
     if (indice >= 0)
	nfullS = nfullS + fullS.substring(oldindice, indice) + newS;
     oldindice = indice+1;
     indice = fullS.indexOf(oldS, indice + 1);
   }

 if (oldindice < fullS.length)
   nfullS = nfullS + fullS.substring(oldindice, fullS.length);

   return nfullS; 
}

function altera_valor(qtd_parcela)
{
	f = document.form1
	s = new String()
	switch (qtd_parcela)
	{
		case '1':   s.value =  f.UMx.value.toString()
					s.value = replaceString(",", ".", s.value)
					s.value = s.value * 1
					s.value = formata_valor(s.value)
					f.total.value = s.value; break
		 case '2':   s.value =  f.DOISx.value.toString()
					s.value = replaceString(",", ".", s.value)
					s.value = s.value * 2
					s.value = formata_valor(s.value)
					f.total.value = s.value; break
       case '3':   s.value =  f.TRESx.value.toString()
					s.value = replaceString(",", ".", s.value)
					s.value = s.value * 3
					s.value = formata_valor(s.value)
					f.total.value = s.value; break
  		case '4':   s.value =  f.QUATROx.value.toString()
					s.value = replaceString(",", ".", s.value)
					s.value = s.value * 4
					s.value = formata_valor(s.value)
					f.total.value = s.value; break
		case '5':   s.value =  f.CINCOx.value.toString()
					s.value = replaceString(",", ".", s.value)
					s.value = s.value * 5
					s.value = formata_valor(s.value)
					f.total.value = s.value; break
        case '6':   s.value =  f.SEISx.value.toString()
					s.value = replaceString(",", ".", s.value)
					s.value = s.value * 6
					s.value = formata_valor(s.value)
					f.total.value = s.value; break
   		case '7':   s.value =  f.SETEx.value.toString()
					s.value = replaceString(",", ".", s.value)
					s.value = s.value * 7
					s.value = formata_valor(s.value)
					f.total.value = s.value; break
    	case '8':   s.value =  f.OITOx.value.toString()
					s.value = replaceString(",", ".", s.value)
					s.value = s.value * 8
					s.value = formata_valor(s.value)
					f.total.value = s.value; break
        case '9':   s.value =  f.NOVEx.value.toString()
					s.value = replaceString(",", ".", s.value)
					s.value = s.value * 9
					s.value = formata_valor(s.value)
					f.total.value = s.value; break
        case '10':   s.value =  f.DEZx.value.toString()
					s.value = replaceString(",", ".", s.value)
					s.value = s.value * 10
					s.value = formata_valor(s.value)
					f.total.value = s.value; break
//		case '11':   s.value =  f.ONZEx.value.toString()
//					s.value = replaceString(",", ".", s.value)
//					s.value = s.value * 11
//					s.value = formata_valor(s.value)
//					f.total.value = s.value; break

 	}

}

//para diminuir a parcela tem de tirar em cima e embaixo...

function ajusta_valores()
{
		f = document.form1
		f.UMx.value = formata_valor((valor_total+3)/1)
		f.DOISx.value = formata_valor((valor_total+6)/2)
		f.TRESx.value = formata_valor((valor_total+9)/3)
		f.QUATROx.value = formata_valor((valor_total+12)/4)
		f.CINCOx.value = formata_valor((valor_total+15)/5)
		f.SEISx.value = formata_valor((valor_total+18)/6)
//		f.SETEx.value = formata_valor((valor_total+21)/7)
//		f.OITOx.value = formata_valor((valor_total+24)/8)
//		f.NOVEx.value = formata_valor((valor_total+27)/9)
//		f.DEZx.value = formata_valor((valor_total+30)/10)
//		f.ONZEx.value = formata_valor((valor_total+33)/11) //
		f.total.value = formata_valor(valor_total)
}

function comprar(valor, dia, bloco)
{
	f = document.form1
	texto1 = dia + " - " + bloco + " - " + valor
	valor = parseFloat(valor)
	valor_total =  valor_total + valor
	aux = new Option(texto1, valor);
	f.dias_escolhidos.options[f.dias_escolhidos.length] = aux;
	ajusta_valores()
}

function excluir()
{
	lista=document.form1.dias_escolhidos;
	var valor
	if(lista.length == '1')
	{
		valor = lista.options[0].value
		lista.options[0] = null
		valor = parseFloat(valor)
		valor_total = valor_total - valor
		f.total.value = '0,00'
	}else
	{
		for( i = lista.length - 1;  i >= 0 ; i--)
		{
			if(lista.options[i].selected)
			{
				valor = lista.options[i].value
				valor = parseFloat(valor)
				lista.options[i] = null
				valor_total = valor_total - valor
				ajusta_valores()
			}
		}
	}
	
}


function validacompra(){
d = document.form1;

//validar total
if (d.total.value == 0){
alert("Escolha pelo menos 1 dia de Bloco ou de Camarote!");
d.total.focus();
return false;
}
//validar dias escolhidos
escolhas = f.dias_escolhidos.length;
	if (escolhas == 0)	{
	  alert("Escolha pelo menos 1 dia de Bloco ou de Camarote!");
	  return false;
}
//validar parcela
if (!d.parcela[0].checked && !d.parcela[1].checked && !d.parcela[2].checked && !d.parcela[3].checked && !d.parcela[4].checked && !d.Entrega[5].checked && !d.Entrega[6].checked && !d.Entrega[7].checked && !d.Entrega[8].checked && !d.Entrega[9].checked && !d.Entrega[10].checked) {
alert("Escolha em quantas parcelas deseja pagar!")
return false;
}
return true;
}

function EnviarEmail(){
	f = document.form1;
	f.submit();
}

function preencheEmail()
{
f = document.form1
lista=f.dias_escolhidos;

for( i = lista.length - 1;  i >= 0 ; i--)
		{
		f.todos_dias.value=f.todos_dias.value+lista.options[i].text+'<br>';
		}
	if (f.todos_dias.value=='')
	{
		return false;
	}
	else {
		return true;
	}
}
