var nasistentes = 1;
var precio = 800;

function encontrar_elemento (item) {
   if( window.mmIsOpera ) return(document.getElementById(item));
   if (document.all) return document.all[item];
   if (document.getElementById) return(document.getElementById(item));
   return(false);
}

function actualizar_numero_asistentes (a) {
   asis = encontrar_elemento ("campo_numero_asistentes");
   tot  = encontrar_elemento ("campo_total_a_pagar");
   persona = encontrar_elemento ("campo_tipo_persona");
   asis_final = encontrar_elemento ("campo_numero_asistentes_final");
   tot_final  = encontrar_elemento ("campo_total_a_pagar_final");
   tarifa  = encontrar_elemento ("tarifa");
   f_pago = encontrar_elemento ("campo_forma_de_pago");
   tot_adicionales = encontrar_elemento("campo_total_adicionales");
   
   precio = 900;
   descuento_fpago = 0;

   if(tarifa.value == 1)
      {
      if (datos.campo_tipo_persona[0].checked)
         precio = 800;
      if (datos.campo_tipo_persona[1].checked)
         precio = 900;
      if (datos.campo_tipo_persona[2].checked)
         precio = 300;
      }
   else
      {
      if (datos.campo_tipo_persona[0].checked)
         precio = 900;
      if (datos.campo_tipo_persona[1].checked)
         precio = 1000;
      if (datos.campo_tipo_persona[2].checked)
         precio = 300;
      }

   precio_total = precio*nasistentes;

   if (datos.campo_tipo_persona[0].checked)
      {
	if(tarifa.value == 1)
		{
		  if(nasistentes == 1)
			 precio_total = 800;
		  if(nasistentes == 2)
			 precio_total = 800+800;
		  if(nasistentes == 3)
			 precio_total = 800+800+800;
		  if(nasistentes > 3)
			 precio_total = 800+800+800+(nasistentes-3)*800;
		}
	else
		{
		  if(nasistentes == 1)
			 precio_total = 900;
		  if(nasistentes == 2)
			 precio_total = 900+900;
		  if(nasistentes == 3)
			 precio_total = 900+900+900;
		  if(nasistentes > 3)
			 precio_total = 900+900+900+(nasistentes-3)*900;
		}
      }

   if (datos.campo_forma_de_pago[1].checked)
      {
      descuento_fpago = parseInt(precio_total*0.00);
      }

   precio_final = precio_total-descuento_fpago;

   // Campos en numero de asistentes
   tot.value = precio_final;
   tot.value = "US$ " + formatNmb(tot.value);

   // Calcula precio de adicionales
	adicionales_final = 0;

	add_tour_1   = encontrar_elemento ("add_tour_1");
	tour_1   = encontrar_elemento ("tour_1");
	precio_tour_1 = 300;

	add_tour_2   = encontrar_elemento ("add_tour_2");
	tour_2   = encontrar_elemento ("tour_2");
	precio_tour_2 = 300;

	add_ebook    = encontrar_elemento ("add_ebook");
	ebook    = encontrar_elemento ("ebook");
	precio_ebook = 50;

	add_book     = encontrar_elemento ("add_book");
	book     = encontrar_elemento ("book");
	precio_book = 200;

	add_dinner   = encontrar_elemento ("add_dinner");
	dinner   = encontrar_elemento ("dinner");
	precio_dinner = 150;

	add_luncheon = encontrar_elemento ("add_luncheon");
	luncheon = encontrar_elemento ("luncheon");
	precio_luncheon = 150;

	add_tour_3   = encontrar_elemento ("add_tour_3");
	tour_3   = encontrar_elemento ("tour_3");
	precio_tour_3 = 60;

	add_tour_4   = encontrar_elemento ("add_tour_4");
	tour_4   = encontrar_elemento ("tour_4");
	precio_tour_4 = 120;

	if(add_tour_1.checked)
		adicionales_final += tour_1.value * precio_tour_1;
	if(add_tour_2.checked)		
		adicionales_final += tour_2.value * precio_tour_2;
	if(add_ebook.checked)
		adicionales_final += ebook.value * precio_ebook;
	if(add_book.checked)
		adicionales_final += book.value * precio_book;
	if(add_dinner.checked)
		adicionales_final += dinner.value * precio_dinner;
	if(add_luncheon.checked)
		adicionales_final += luncheon.value * precio_luncheon;
	if(add_tour_3.checked)
		adicionales_final += tour_3.value * precio_tour_3;
	if(add_tour_4.checked)
		adicionales_final += tour_4.value * precio_tour_4;
   // Total de adicionales
   tot_adicionales.value = adicionales_final;
   tot_adicionales.value = "US$ " + formatNmb(tot_adicionales.value);

   // Campos al final del formulario
   asis_final.value = nasistentes;
   tot_final.value = precio_final + adicionales_final;
   tot_final.value = "US$ " + formatNmb(tot_final.value);

//   alert("Asisentes: "+nasistentes+"\nPrecio unitario: "+precio+"\nPrecio total: "+precio_total+"\nDescuento tarjeta credito: "+descuento_fpago+"\nPrecio a pagar: "+precio_final);

}

function agregar_asistente () {
   asis       = encontrar_elemento ("asistentes");
   formulario = encontrar_elemento ("datos");

	// reconocer IE por setAttribute "className"
	if(navigator.appVersion.indexOf("MSIE") != -1) var IE = true;

   if (asis && formulario) {
      nasistentes++;
      // Se agrega el div
      nuevo_asistente = document.createElement('div');
      asis.appendChild (nuevo_asistente);
      nuevo_asistente.id = "asistente_"+nasistentes;

//      tabla = "<table><tr><td colspan='2' class='texto_4'><strong>ASISTENTE "+nasistentes+"</strong></td></tr><tr class='texto'><td>Nombre</td><td>Apellido</td></tr><tr><td id='td_nombre_"+nasistentes+"'></td><td id='td_apellido_"+nasistentes+"'></td></tr><tr><td class='texto'>Cargo</td><td>e-mail</td></tr><tr><td id='td_cargo_"+nasistentes+"'></td><td id='td_email_"+nasistentes+"'></td></tr><tr><td>Empresa / institución</td><td id='td_empresa_"+nasistentes+"'></td></tr><tr><td>Teléfono</td><td id='td_telefono_"+nasistentes+"'></td></tr></table>";
	  tabla = "<table width='100%' border='0' cellpadding='0' cellspacing='0' class='texto_formulario'><tr><td height='30' colspan='2'></td></tr><tr><td height='30' colspan='2' class='texto_formulario'><table border='0' align='left' cellpadding='0' cellspacing='0'><tr><td width='20' align='left' valign='middle'><img src='imagenes/icono_programa.gif' width='20' height='14'></td><td><div align='left' class='texto_formulario'><strong>ATTENDEE "+nasistentes+"</strong></div></td></tr></table></td></tr><tr><td width='50%' height='20'>First Name</td><td width='50%' height='20'>Last Name</td></tr><tr><td height='20' id='td_nombre_"+nasistentes+"'></td><td height='20' id='td_apellido_"+nasistentes+"'></td></tr><tr><td height='20'>Position</td><td height='20'>E-mail</td></tr><tr><td height='20' id='td_cargo_"+nasistentes+"'></td><td height='20' id='td_email_"+nasistentes+"'></td></tr><tr><td height='20'>Company or institution</td><td height='20'>Telephone</td></tr><tr><td height='20' id='td_empresa_"+nasistentes+"'></td><td height='20' id='td_Telefono_"+nasistentes+"'></td></tr></table>";

      nuevo_asistente.innerHTML = tabla;

      // Se agregan los campos del formulario. Primero se debe agregar al formulario y después a la celda, sino
      // aparece el campo al final de la tabla y no dentro de la celda
      texto = document.createElement("input");
      texto.setAttribute('type', 'text');
      texto.setAttribute('name', 'campo_requeridoNombre_asistente_'+nasistentes);
      texto.setAttribute('id', 'campo_requeridoNombre_asistente_'+nasistentes);
      if(IE) texto.setAttribute('className', 'campo');
      else texto.setAttribute('class', 'campo');
      celda = encontrar_elemento ('td_nombre_'+nasistentes);
      formulario.appendChild(texto);
      celda.appendChild(texto);

      texto = document.createElement("input");
      texto.setAttribute('type', 'text');
      texto.setAttribute('name', 'campo_requeridoApellido_asistente_'+nasistentes);
      texto.setAttribute('id',   'campo_requeridoApellido_asistente_'+nasistentes);
	  if(IE) texto.setAttribute('className', 'campo');
	  else texto.setAttribute('class', 'campo');
      celda = encontrar_elemento ('td_apellido_'+nasistentes);
      formulario.appendChild(texto);
      celda.appendChild(texto);

      texto = document.createElement("input");
      texto.setAttribute('type', 'text');
      texto.setAttribute('name', 'campo_requeridoCargo_asistente_'+nasistentes);
      texto.setAttribute('id',   'campo_requeridoCargo_asistente_'+nasistentes);
	  if(IE) texto.setAttribute('className', 'campo');
	  else texto.setAttribute('class', 'campo');
      celda = encontrar_elemento ('td_cargo_'+nasistentes);
      formulario.appendChild(texto);
      celda.appendChild(texto);

      texto = document.createElement("input");
      texto.setAttribute('type', 'text');
      texto.setAttribute('name', 'campo_requeridoEmpresa_institucion_asistente_'+nasistentes);
      texto.setAttribute('id',   'campo_requeridoEmpresa_institucion_asistente_'+nasistentes);
	  if(IE) texto.setAttribute('className', 'campo');
	  else texto.setAttribute('class', 'campo');
	  celda = encontrar_elemento ('td_empresa_'+nasistentes);
      formulario.appendChild(texto);
      celda.appendChild(texto);

      texto = document.createElement("input");
      texto.setAttribute('type', 'text');
      texto.setAttribute('name', 'campo_requeridoEmail_asistente_'+nasistentes);
      texto.setAttribute('id',   'campo_requeridoEmail_asistente_'+nasistentes);
	  if(IE) texto.setAttribute('className', 'campo');
	  else texto.setAttribute('class', 'campo');
	  celda = encontrar_elemento ('td_email_'+nasistentes);
      formulario.appendChild(texto);
      celda.appendChild(texto);
	  
	        texto = document.createElement("input");
      texto.setAttribute('type', 'text');
      texto.setAttribute('name', 'campo_requeridoTelefono_asistente_'+nasistentes);
      texto.setAttribute('id',   'campo_requeridoTelefono_asistente_'+nasistentes);
	  if(IE) texto.setAttribute('className', 'campo');
	  else texto.setAttribute('class', 'campo');
	  celda = encontrar_elemento ('td_Telefono_'+nasistentes);
      formulario.appendChild(texto);
      celda.appendChild(texto);

//      texto = document.createElement("input");
//      texto.setAttribute('type', 'text');
//      texto.setAttribute('name', 'campo_requeridoTelefono_asistente_'+nasistentes);
//      texto.setAttribute('id',   'campo_requeridoTelefono_asistente_'+nasistentes);
//	  if(IE) texto.setAttribute('className', 'campo');
//	  else texto.setAttribute('class', 'campo');
//      celda = encontrar_elemento ('td_telefono_'+nasistentes);
//      formulario.appendChild(texto);
//      celda.appendChild(texto);

      actualizar_numero_asistentes();
   }
}

function borrar_asistente () {
   if (nasistentes > 0) {
      asiss = encontrar_elemento ("asistentes");
      asis  = encontrar_elemento ("asistente_"+nasistentes);
      if (asiss && asis) {
         asiss.removeChild (asis);
         nasistentes--;
         actualizar_numero_asistentes();
         return true;
      }
      else {
         return false;
      }
   }
   else
      return false;
}

function control_asistentes (total_asistentes) {
   if (total_asistentes.value <= 0 || total_asistentes.value == nasistentes) {
      total_asistentes.value = nasistentes;
      actualizar_numero_asistentes();
   }
   else {
      if (total_asistentes.value < nasistentes) {
         diferencia = nasistentes - total_asistentes.value;
         for (i = 0; i < diferencia; i++)
            borrar_asistente();
      }
      else {
         diferencia = total_asistentes.value - nasistentes;
         for (i = 0; i < diferencia; i++)
            agregar_asistente();
      }
   }
}

function formatNmb(nNmb){ 
    var sRes = ""; 
    for (var j, i = nNmb.length - 1, j = 0; i >= 0; i--, j++) 
     sRes = nNmb.charAt(i) + ((j > 0) && (j % 3 == 0)? ",": "") + sRes; 
    return sRes; 
}

function activar(accion, campo) {
	selec = encontrar_elemento (campo);
	selec.disabled = !accion;
    actualizar_numero_asistentes();
}
