$(document).ready( function() {

	//MENUS INTERNOS
	$('.boton-interno').hover(function () {
	  $(this).stop().animate({
		marginLeft : 38
	  }, 250);
	}, function () {
	  $(this).stop().animate({
		marginLeft : 48
	  }, 250);
	});
			
	// 1 y 2 vuelta comparativa
	$('.showhide').live('click', function(){
		//var container = ($(this).attr('rev') != '') ? '#'+$(this).parents('.'+$(this).attr('rev')).attr('id')+' ' : '';
		//var container = '#'+$('.showhide').parents('#vueltas').attr('id');
		var selected = $(this).attr('rel');
		
		//alert("container = "+container);
		//alert("selected = "+selected);
		
		$('.showhide').removeClass('selected');
		
		
		
		$('.vueltas').addClass('hide');
		$('#showhide_'+selected).removeClass('hide');
		
		
		return false;
	});
	
	// tapa portada
	$('#info_top').click(function(){
		//
		$(this).hide();
		$.cookie('info_top','1', {expires: 3000});
		return false;
	});
	
	
	// pestañas
	$('.tab').live('click', function(){
		var container = ($(this).attr('rev') != '') ? '#'+$(this).parents('.'+$(this).attr('rev')).attr('id')+' ' : '';
		var selected = $(this).attr('rel');
		var type = $(this).attr('name');
		var url = $(this).attr('href');
		
		$(container+'.tab').removeClass('selected');
		$(this).addClass('selected');
		$(this).unbind('click')
		
		// cargo por ajax la url en la pestaña
		if(type == 'external'){
			$(container+'#tab_'+selected).load(url, function(){
				$('a.subtab.auto-load', $(this)).click();
			});
		} else {
			$(container+'.tab-content').hide();
			$(container+'#tab_' + selected).show();
		}
		return false;
	});



	// Manejo de sub-pestañas
	
	/* Versión antigua del evento que no tenía en cuenta que se puede querer cargar contenido
	 * en la misma subpestaña sin perder la selección del SUBTAB
	 *
	 * nota por Sebastián Veggiani, 20110620
	 */
	/* $('.subtab').live('click', function(){
		var container = ($(this).attr('rev') != '') ? '#'+$(this).parents('.'+$(this).attr('rev')).attr('id')+' ' : '';
		var selected = $(this).attr('rel');
		var type = $(this).attr('name');
		var url = $(this).attr('href');

		$(container+'.subtab').removeClass('selected');
		$(this).addClass('selected');

		// cargo por ajax la url en la pestaña
		if(type == 'external'){

			if(selected == 'clasification-clasification-content'){
				$('#clasification-details').hide();
			}

			createPreload(container+'#subtab_' + selected, 'preload_'+selected, '')

			$(container+'#subtab_' + selected).load(url, function(){
				destroyPreload('preload_'+selected)
				$(this).show()
			});

		} else {
			$(container+'.subtab-content').not(container+'#subtab_'+selected).hide();
			$(container+'#subtab_' + selected).show();
		}
		return false;
	}); */

	$('.subtab').live('click', function(){

		var container = ($(this).attr('rev') != '') ? '#'+$(this).parents('.'+$(this).attr('rev')).attr('id')+' ' : '';
		var selected = $(this).attr('rel');
		var type = $(this).attr('name');
		var url = $(this).attr('href');
		var noChangeTab = $(this).hasClass('no-change-tab');

		// si debe cambiarse el tab activo...
		if ( noChangeTab == false ) {
			$(container+'.subtab').removeClass('selected');
			$(this).addClass('selected');
		}

		// cargo por ajax la url en la pestaña
		if(type == 'external'){

			if(selected == 'clasification-clasification-content'){
				$('#clasification-details').hide();
			}

			createPreload(container+'#subtab_' + selected, 'preload_'+selected, '')

			$(container+'#subtab_' + selected).load(url, function(){
				destroyPreload('preload_'+selected)
				$(this).show()
			});

		} else {
			$(container+'.subtab-content').not(container+'#subtab_'+selected).hide();
			$(container+'#subtab_' + selected).show();
		}

		return false;

	});



	// Expandir contenidos
	
	/*$('a.expand').live('click',function(){
		var container_id = $(this).attr('rel');
		var type = $(this).attr('name');
		var url = $(this).attr('href');

		$(this).toggleClass('active');

		if(type == 'external'){
			// Con Preload
			createPreload('#'+container_id, 'preload_'+container_id, '')

			$('#'+container_id).load(url, function(){
				destroyPreload('preload_'+container_id);
				$(this).slideToggle('slow');
			});
			
			// OK Sin Preload
			$('#'+container_id).load(url, function(){
				$(this).slideToggle('slow');
			});
			
		} else {
			$('#'+container_id).slideToggle('slow');
		}
		return false;
	})*/

	$('a.expand').live('click',function(){

		var link = $(this);
		var container_id = $(this).attr('rel');
		var type = $(this).attr('name');
		var url = $(this).attr('href');
		var currentStatus = $(this).hasClass('active');
		var isLoaded = $(this).hasClass('loaded');

		// alterna la la clase para el estado
		$(this).toggleClass('active');

		if( type == 'external' ){

			// determina si se está mostrando u ocultando
			if (
				currentStatus != true
				&& isLoaded == false
			) {
				// crea el preloader
				createSmallPreload( link, 'preload_smallPreload', '');

				// agrega clase para saber si ya está cargado
				link.addClass('loaded');

				// carga el contenido
				$('#' + container_id).load( url, function(){
					$(this).slideToggle('slow');
					destroyPreload('preload_smallPreload');
				});

			} else {

				$('#' + container_id).slideToggle('slow');

			}

		} else {

			$('#'+container_id).slideToggle('slow');

		}

		return false;

	});


	// SIN expandir contenidos
	$('a.noexpand').live('click',function(){
		var container_id = $(this).attr('rel');
		var type = $(this).attr('name');
		var url = $(this).attr('href');

		//$(this).toggleClass('active');

		if(type == 'external'){
			// Con Preload
			createPreload('#'+container_id, 'preload_'+container_id, '')

			$('#'+container_id).load(url, function(){
				destroyPreload('preload_'+container_id);
				//$(this).slideToggle('slow');
			});
			/*
			// OK Sin Preload
			$('#'+container_id).load(url, function(){
				//$(this).slideToggle('slow');
			});
			*/
		} else {
			//$('#'+container_id).slideToggle('slow');
		}

		return false;
	})


	// detalle de filas
	$('a.btn-details').unbind('click');
	$('a.btn-details').live('click',function(){

		$('tr.match-row', $(this).parent().parent().parent().parent().parent().parent().parent()).removeClass('active');
		$(this).parent().parent().addClass('active');

		var container = $(this).attr('rel');
		var type = $(this).attr('name');
		var url = $(this).attr('href');
		
		if(container != '' && container != null){

			// cargo por ajax la url en el contenedor
			if(type == 'external'){

				createPreload('#'+container, 'preload_'+container, '')

				$('#'+container).load(url, function(){
					destroyPreload('preload_'+container)
					$(this).show();
				});
			} else {
				$('#'+container).show();
			}
		
		}

		return false;
	});
	
	// panel boletos: carga de estadisticas
	$('#ticket-panel tr.team-row').hover(function(){
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	})

	$('#ticket-panel tr.team-row td').not('#ticket-panel tr.team-row td.results-red').click(function(){
		$('.btn-statistics', $(this).parents('#ticket-panel tr.team-row')).click();
		return false;
	})
	
	$('.btn-statistics').click(function(){
		var element = $(this);
		var statistics_id = $(this).attr('rel');
		var statistics_class = $('#'+statistics_id).attr('class');
		var statistics_url = $(this).attr('href');

		// cierro panel abierto anteriormente
		$('.'+statistics_class).each(function(){
			if($(this).is(':visible') && $(this).not('#'+statistics_id)){
				$('#ticket-panel tr.team-row').removeClass('active');
				$(this).slideUp('slow');
			}
		})
		
		if($('#'+statistics_id).is(':visible')){
			element.parent().parent().removeClass('active');
			$('#'+statistics_id).slideUp('slow');
		} else {
			$('#'+statistics_id).load(statistics_url, function(){
				element.parent().parent().addClass('active');
				$('#'+statistics_id).slideDown('slow');
			});
		}
		return false
	});

	// panel boletos: cierra estadisticas	
	$('.statistics-main a.close').live('click',function(){
		$('#ticket-panel tr.team-row').removeClass('active');
		$(this).parents('.statistics-container').slideUp('slow');
		return false;
	});
	
	// panel boletos: combinaciones
	//$('.combinations-table tr.match-row').unbind('click');
	//$('.combinations-table tr.match-row').live('click',function(){
	//	$('a.btn-details', $(this)).click();
	//	return false;
	//});

	// panel ayuda: quiniela > detalle de pronostico
	$('a.lupa-details').unbind('click');
	$('.seasons-details-table tr.match-row').live('click',function(){
		$('a.lupa-details', $(this)).click();
		return false;
	});

	// panel ayuda: clasificacion > detalle de partidos
	$('.clasification-table tr.match-row').live('click',function(){
		$('a.btn-details', $(this)).click();
		return false;
	});

	

	/* Modificada en favor del evento de .help-ajax-link
	 * creo que este evento no estaba utilizado pero lo dejo por referencia
	 *
	 * nota por Sebastián Veggiani, 20110620
	 */
	/* $('.ajax-link').live('click',function(){

		var container = $(this).attr('rel');
		var type = $(this).attr('name');
		var url = $(this).attr('href');

		// cargo por ajax la url en el contenedor
		if(type == 'external'){

			createPreload('#'+container, 'preload_'+container, '')

			$('#'+container).load(url, function(){
				destroyPreload('preload_'+container);
				$(this).show();
			});
		} else {
			$('#'+container).show();
		}

		return false;
	});
	*/



	/* Carga de contenidos con AJAX mediante links
	 * Atributos en el enlace
	 * href: URL a cargar
	 * data-tab: ID del tab a activar en la carga. Todos los tabs deberán tener un ID único como atributo para poder marcalo como "activo".
	 * data-scroll: 1 si desea hacer scroll en la página hasta el contenedor de la carga
	 */
	$('.help-ajax-link').live('click',function(){

		var type = $(this).data('type'); //console.log( type );
		var url = $(this).attr('href'); //console.log( url );
		var selTab = $(this).data('tab');
		var holder = $( '#help-panel .tab-content' );

		// determina tab a activar
		if ( selTab != '' && selTab != undefined ) {
			$('#help-panel .tabs-classic a').removeClass('selected');
			selTabElem = $( '#' + selTab );
			selTabElem.addClass('selected');
		}

		// scroll to element
		if ( $(this).data('scroll') == '1' ) {
			$('html, body').animate({ scrollTop: holder.offset().top }, 500);
		}


		// crea el preload y carga el contenido
		createPreload( holder, 'preload_' + selTab, '')

		$(holder).load( url, function() {
			destroyPreload('preload_'+selTab);
			$(this).show();
		});

		return false;

	});




	$('.analysis-table a.btn-match-details').live('click',function(){

		$('.analysis-table a.btn-match-details').removeClass('active');
		$(this).addClass('active');

		var container = $(this).attr('rel');
		var type = $(this).attr('name');
		var url = $(this).attr('href');

		// cargo por ajax la url en el contenedor
		if(type == 'external'){

			createPreload('#'+container, 'preload_'+container, '')

			$('#'+container).load(url, function(){
				destroyPreload('preload_'+container);
				$(this).show();
			});
		} else {
			$('#'+container).show();
		}

		return false;
	});

	// envía los formularios por ajax y carga el contenido devuelto
	$('.ajax-form a.ajax-form-send').live('click',function(){

		var theForm = $(this).parents('form'); //console.log( theForm );
		var container = theForm.attr('rel');
		var url = $(this).attr('href')+"?"+theForm.serialize();
		//console.log( theForm, container, url );

		// solo para demostración, comentar esta linea!!
		//alert("Formulario enviado: esto cargará \n\nla respuesta de la URL: " + url + "\n\nen el <div> con id = subtab_" + container + ",\n\ny los parámetros del formulario \"" + theForm.attr('id') + "\"\n\nque llegarán por POST son: " + theForm.serialize() );


		// cargo por ajax la url en el contenedor
		createPreload('#subtab_' + container, 'preload_' + container, '');

		$('#subtab_' + container).load( url, function(){
			destroyPreload('preload_' + container);
			$(this).show();
		});

		return false;

	});
	
	// envía los formularios por ajax y carga el contenido devuelto
	$('.ajax-form a.ajax-form-send2').live('click',function(){

		var theForm = $(this).parents('form'); //console.log( theForm );
		var container = theForm.attr('rel');
		var url = $(this).attr('href')+"?"+theForm.serialize();
		//console.log( theForm, container, url );

		// solo para demostración, comentar esta linea!!
		//alert("Formulario enviado: esto cargará \n\nla respuesta de la URL: " + url + "\n\nen el <div> con id = " + container + ",\n\ny los parámetros del formulario \"" + theForm.attr('id') + "\"\n\nque llegarán por POST son: " + theForm.serialize() );


		// cargo por ajax la url en el contenedor
		createPreload('#' + container, 'preload_' + container, '');

		$('#' + container).load( url, function(){
			destroyPreload('preload_' + container);
			$(this).show();
		});

		return false;

	});
	
	// panel ayuda: otras quinielas > anteriores > carga detalles
	$('.other-previous-table tr.match-row').live('click',function(){

		$('a.btn-details', $(this)).unbind('click');
		$('a.btn-details', $(this)).live('click',function(){


			var details_id = $(this).attr('rel');
			var details_class = $('#'+details_id).attr('class');
			var details_url = $(this).attr('href');
	
			// cierro detalles abierto anteriormente
			$('.'+details_class).each(function(){
				if($(this).is(':visible') && $(this).not('#'+details_id)){
					$('tr.match-row', $(this).parent().parent().parent()).removeClass('active');
					$(this).slideUp('slow');
				}
			})

			$(this).parent().parent().addClass('active');
			
			if($('#'+details_id).is(':visible')){
					$('tr.match-row', $(this).parent().parent().parent()).removeClass('active');
				$('#'+details_id).slideUp('slow');
			} else {
				$('#'+details_id).load(details_url, function(){
					$('#'+details_id).slideDown('slow');
				});
			}
			return false
		});
		$('a.btn-details', $(this)).click();
		return false;
	});



	// panel ayuda: euromillones > anteriores > carga detalles
	$('.euromillones-previous-table tr.match-row').live('click',function(){

		$('a.btn-details', $(this)).unbind('click');
		$('a.btn-details', $(this)).live('click',function(){


			var details_id = $(this).attr('rel');
			var details_class = $('#'+details_id).attr('class');
			var details_url = $(this).attr('href');
	
			// cierro detalles abierto anteriormente
			$('.'+details_class).each(function(){
				if($(this).is(':visible') && $(this).not('#'+details_id)){
					$('tr.match-row', $(this).parent().parent().parent()).removeClass('active');
					$(this).slideUp('slow');
				}
			})

			$(this).parent().parent().addClass('active');
			
			if($('#'+details_id).is(':visible')){
					$('tr.match-row', $(this).parent().parent().parent()).removeClass('active');
				$('#'+details_id).slideUp('slow');
			} else {
				$('#'+details_id).load(details_url, function(){
					$('#'+details_id).slideDown('slow');
				});
			}
			return false
		});
		$('a.btn-details', $(this)).click();
		return false;
	});

	// panel ayuda: primitiva > anteriores > carga detalles
	$('.primitiva-previous-table tr.match-row').live('click',function(){

		$('a.btn-details', $(this)).unbind('click');
		$('a.btn-details', $(this)).live('click',function(){


			var details_id = $(this).attr('rel');
			var details_class = $('#'+details_id).attr('class');
			var details_url = $(this).attr('href');
	
			// cierro detalles abierto anteriormente
			$('.'+details_class).each(function(){
				if($(this).is(':visible') && $(this).not('#'+details_id)){
					$('tr.match-row', $(this).parent().parent().parent()).removeClass('active');
					$(this).slideUp('slow');
				}
			})

			$(this).parent().parent().addClass('active');
			
			if($('#'+details_id).is(':visible')){
					$('tr.match-row', $(this).parent().parent().parent()).removeClass('active');
				$('#'+details_id).slideUp('slow');
			} else {
				$('#'+details_id).load(details_url, function(){
					$('#'+details_id).slideDown('slow');
				});
			}
			return false
		});
		$('a.btn-details', $(this)).click();
		return false;
	});

	// tablón de premios
	function awards(){

		var list_width = 0;
		var slide_total = 0;
		var slide_counter = 1;

		$('#awards-panel .award-item').each(function(){
			list_width += $(this).width()+10;
			slide_total++;
		});
		
		$('#awards-panel .awards-list').css('width', list_width);
		
		$('#awards-panel .btn-show').click(function(){
			if(slide_counter < slide_total-3){
				$('#awards-panel .awards-list').animate({'left': '-=170'}, 750);
				slide_counter++;
			} else {
				$('#awards-panel .awards-list').animate({'left': '0'}, 750);
				slide_counter=1;
			}
			clearTimeout(awardsTimeout)
			awardsTimeout = setTimeout(function(){ $('#awards-panel .btn-show').click() }, 5000);
			/*
			if(slide_counter < list_width / 680){
				$('#awards-panel .awards-list').animate({'left': '-=680'});
				slide_counter++;
			} else {
				$('#awards-panel .awards-list').animate({'left': '0'});
				slide_counter = 1;
			}*/
			return false;
		});
		
		var awardsTimeout = setTimeout(function(){ $('#awards-panel .btn-show').click() }, 5000);

	}

	// paneles laterales	
	$('.sidebar-panel .panel-header.expand-collapse a').click(function(){
		$(this).toggleClass('collapse');
		var container = $(this).attr('rel');
		var cookie_name = $(this).attr('name');
		
		if(container != '' && container != null){
			var element = $('.'+container, $(this).parents('.sidebar-panel'));
		} else {
			var element = $('.panel-content', $(this).parents('.sidebar-panel'));
		}
		
		if (cookie_name != '' && cookie_name != null) {
			if (element.is(':visible')) {
				$.cookie(cookie_name,'0', {expires: 30});
			} else {
				$.cookie(cookie_name,'1', {expires: 30});
			}
		}
		element.slideToggle('slow')
		return false;
	})

	$('.sidebar-panel .subpanel-header.expand-collapse a').click(function(){
		$(this).toggleClass('collapse');
		var cookie_name = $(this).attr('name');

		if (cookie_name != '' && cookie_name != null) {
			if ($('.subpanel-content', $(this).parents('.subpanel')).is(':visible')) {
				$.cookie(cookie_name,'0', {expires: 30});
			} else {
				$.cookie(cookie_name,'1', {expires: 30});
			}
		}

		$('.subpanel-content', $(this).parents('.subpanel')).slideToggle('slow');
		/*
		if($('.subpanel-content', $(this).parents('.subpanel')).is(":visible")){
			 $('.subpanel-content', $(this).parents('.subpanel')).slideDown();
		}else{
			 $('.subpanel-content', $(this).parents('.subpanel')).slideUp();
			}
		*/
		return false;
	})



	/* Versión antigua del evento que fallaba al determinar el contexto
	 * de los paneles a ocultar o desplegar
	 *
	 * nota por Sebastián Veggiani, 20110620
	 */
	/*$('.sidebar-panel .minipanel-header.expand-collapse a').click(function(){

		// si se debe abrir solo uno oculto el que este abierto
		if($(this).attr('rel') == 'unique'){
			$('.sidebar-panel .minipanel-header.expand-collapse a').not($(this)).removeClass('collapse')
			$('.minipanel-content').not($('.minipanel-content', $(this).parents('.minipanel'))).each(function(){
				if($(this).is(':visible')){
					$(this).slideUp('slow');
				}
			})
		}
		
		$(this).toggleClass('collapse');
		$('.minipanel-content', $(this).parents('.minipanel')).slideToggle('slow');

		return false;
	}) */


	$('.minipanel-header.expand-collapse a').click( function(){

		// define el contexto en el que trabaja el panel expandi
		context = $(this).parents('.minipanels'); //console.log(context);
		current = $(this);
		panelParent = $(this).parents('.minipanel', context); //console.log(panelParent);


		// si se debe abrir solo uno oculto el que este abierto
		if( current.attr('rel') == 'unique' ){

			// quito la clase que mustra la flecha de colapsar en los paneles que no son el actual
			$('.expand-collapse a', context).not( current ).removeClass('collapse');

			// recorro todos los paneles y despliego u oculto según corresponda
			$('.minipanel-content', context).not( $('.minipanel-content', panelParent) ).each( function(){
				if( $(this).is(':visible') ) {
					$(this).slideUp('slow');
				}
			});

		}
		
		// cambia la clase para la flecha de colapsar
		current.toggleClass('collapse');

		// modifico el estado del contenido del panel
		$('.minipanel-content', panelParent).slideToggle('slow');

		return false;
	})



	$('#friend-sites li a').hover(function(){
		$(this).animate({marginTop:'-3'}, 250)
	},function(){
		$(this).animate({marginTop:'-10'}, 250)
	})
	
	$('.results-boxes a').live('click', function(){
		$('span.result-box', $(this)).toggleClass('active');
		return false
	})

	$('.results-boxes-home a').live('click', function(){
		$('span.result-box', $(this)).toggleClass('active');
		return true
	})



	function createPreload ( object, theID, theClass ) {
		
		/* CSS example
		.preloader-dark { position:absolute; z-index:9999; display:block; background:transparent url(../img/loader_medium.gif) no-repeat 50% 50%; }
		.preloader-light { position:absolute; z-index:9999; display:block; background:#transparent url(../img/loader_medium.gif) no-repeat 50% 50% ; }
		*/
		
		// busca el objeto base
		baseElement = $(object);
		
		if ( baseElement[0] ) {
			
			// toma las propiedades del objeto base
			overlayW = baseElement.outerWidth(); //console.log( overlayW );
			overlayH = baseElement.outerHeight(); //console.log( overlayH );
			overlayPos = baseElement.offset(); //console.log( overlayPos );
			
			// apaga el objeto base
			baseElement.css('opacity','.3');
			
			// crea el elemento y lo posiciona
			var overlay = $('<div id="' + theID + '" class="preloader' + theClass + '"></div>'); //console.log(overlay);
						
			overlay.css('top', overlayPos.top );
			overlay.css('left', overlayPos.left );
			overlay.css('width', overlayW );
			overlay.css('height', overlayH );
			
			overlay.data('linked-element', object );
	
			$(overlay).appendTo( 'body' );

		}
			
	}

	/* Nueva función para crear preloads y deshabilitar los botones tipo "Ver detalles""
	 *
	 * nota por Sebastián Veggiani, 20110620
	 */
	function createSmallPreload ( object, theID, theClass ) {
		
		// busca el objeto base
		baseElement = $(object);

		if ( baseElement[0] ) {

			// toma las propiedades del objeto base
			overlayW = baseElement.outerWidth() + 20; //console.log( overlayW );
			overlayH = baseElement.outerHeight() + 5; //console.log( overlayH );
			overlayPos = baseElement.offset(); //console.log( overlayPos );

			// apaga el objeto base
			baseElement.css('opacity','.5');

			// crea el elemento y lo posiciona
			var overlay = $('<div id="' + theID + '" class="small-preloader' + theClass + '"></div>'); //console.log(overlay);

			overlay.css('top', overlayPos.top );
			overlay.css('left', overlayPos.left );
			overlay.css('width', overlayW );
			overlay.css('height', overlayH );

			overlay.data('linked-element', object );
	
			$(overlay).appendTo( 'body' );

		}

	}

	function destroyPreload ( theID ) {
		
		// busca el objeto base
		element = $('#'+ theID);
		
		// restaura la transparencia
		baseElement = $(element.data('linked-element'));
		
		baseElement.css('opacity','1');
		
		element.remove();
			
	}

		// comentado
		//*
	fullTo15 = function(event, element){
		var input_result_1 = $('#full_result_1');
		var input_result_x = $('#full_result_x');
		var input_result_2 = $('#full_result_2');
		
		var box_result_1 = $('.results-boxes15 a.r1');
		var box_result_x = $('.results-boxes15 a.rx');
		var box_result_2 = $('.results-boxes15 a.r2');
		
		var input_limit = 100;

		var result_1 = 0;
		var result_x = 0;
		var result_2 = 0;

		if(event == 'keyPress'){
			
			result_1 = input_result_1.val()*1;
			result_x = input_result_x.val()*1;
			result_2 = input_result_2.val()*1;
			if(result_1 > 100){
				result_1 = 100;
			} else if(result_1 < 0){
				result_1 = 0;
			} else if(result_1 == ""){
				result_1 = 0;
			}
			if(result_x > 100){
				result_x = 100;
			} else if(result_x < 0){
				result_x = 0;
			} else if(result_x == ""){
				result_x = 0;
			}
			if(result_2 > 100){
				result_2 = 100;
			} else if(result_2 < 0){
				result_2 = 0;
			} else if(result_2 == ""){
				result_2 = 0;
			}
			
			
			
			
			switch(element){
				case input_result_1.attr('id'):
					if(result_x > 0 && result_2 == 0){
						result_x = Math.floor(input_limit - result_1);
						result_2 = 0;
					} else if(result_x == 0 && result_2 > 0){
						if((result_2 + result_1) >100){
							result_x = 0;
							result_2 = Math.floor(input_limit - result_1);
						}else{
							result_x = Math.floor(input_limit - (result_2 + result_1));
						}
					} else if(result_x > 0 && result_2 > 0){
						if((result_2 + result_1) >100){
							result_x = 0;
							result_2 = Math.floor(input_limit - result_1);
						}else{
							result_x = Math.floor(input_limit - (result_2 + result_1));
						}
					} else if(result_x == 0 && result_2 == 0){
						if(result_1 < 100){
							result_x = Math.floor(input_limit - result_1);
							result_2 = 0;
						}
					}
					deixem_cookie2();
					break;
				case input_result_x.attr('id'):
					if(result_2 > 0 && result_1 == 0){
						result_2 = Math.floor(input_limit - result_x);
						result_1 = 0;
					} else if(result_2 == 0 && result_1 > 0){
						if((result_1 + result_x) >100){
							result_2 = 0;
							result_1 = Math.floor(input_limit - result_x);
						}else{
							result_2 = Math.floor(input_limit - (result_1 + result_x));
						}
					} else if(result_2 > 0 && result_1 > 0){
						if((result_1 + result_x) >100){
							result_2 = 0;
							result_1 = Math.floor(input_limit - result_x);
						}else{
							result_2 = Math.floor(input_limit - (result_1 + result_x));
						}
					} else if(result_2 == 0 && result_1 == 0){
						if(result_x < 100){
							result_2 = Math.floor(input_limit - result_x);
							result_1 = 0;
						}
					}
					deixem_cookie2();
					break;
				case input_result_2.attr('id'):
					if(result_1 > 0 && result_x == 0){
						result_1 = Math.floor(input_limit - result_2);
						result_x = 0;
					} else if(result_1 == 0 && result_x > 0){
						if((result_x + result_2) >100){
							result_1 = 0;
							result_x = Math.floor(input_limit - result_2);
						}else{
							result_1 = Math.floor(input_limit - (result_x + result_2));
						}
					} else if(result_1 > 0 && result_x > 0){
						if((result_x + result_2) >100){
							result_1 = 0;
							result_x = Math.floor(input_limit - result_2);
						}else{
							result_1 = Math.floor(input_limit - (result_x + result_2));
						}
					} else if(result_1 == 0 && result_x == 0){
						if(result_2 < 100){
							result_1 = Math.floor(input_limit - result_2);
							result_x = 0;
						}
					}
					deixem_cookie2();
					break;
			}
			deixem_cookie2();
			/*
			if( (result_1 + result_x + result_2) > 100){
				result_1 = 34;
				result_x = 33;
				result_2 = 33;
			}*/

			box_result_1.data('active', 0);
			box_result_x.data('active', 0);
			box_result_2.data('active', 0);
			$('span', box_result_1).removeClass('active');
			$('span', box_result_x).removeClass('active');
			$('span', box_result_2).removeClass('active');
			restableix(15);

			if(result_1 > 0){
				box_result_1.data('active', 1);
				$('span', box_result_1).addClass('active');
				selecciona(15,'1');
			}
			if(result_x > 0){
				box_result_x.data('active', 1);
				$('span', box_result_x).addClass('active');
				selecciona(15,'X');
			}
			if(result_2 > 0){
				box_result_2.data('active', 1);
				$('span', box_result_2).addClass('active');
				selecciona(15,'2');
			}

		} else if(event == 'click'){

			if(box_result_1.data('active') == 1){
				result_1 = 100;
			}
			if(box_result_x.data('active') == 1){
				result_x = 100;
			}
			if(box_result_2.data('active') == 1){
				result_2 = 100;
			}

			if(result_1 == 100 && result_x == 100 && result_2 == 100){
				result_1 = 34;
				result_x = 33;
				result_2 = 33;
			} else if(result_1 == 100 && result_x == 100){
				result_1 = 50;
				result_x = 50;
			} else if(result_x == 100 && result_2 == 100){
				result_x = 50;
				result_2 = 50;
			} else if(result_1 == 100 && result_2 == 100){
				result_1 = 50;
				result_2 = 50;
			}

		}
		
		input_result_1.val( result_1 )
		input_result_x.val( result_x )
		input_result_2.val( result_2 )
		
		deixem_cookie2();
	}
	//*/
//*
	$("#full_result_1").keypress(function(e){
		var charCode = (e.which) ? e.which : e.keyCode;
		if (charCode > 31 && (charCode < 48 || charCode > 57)) {
			return false;
		} else {
			setTimeout("fullTo15('keyPress','"+$(this).attr('id')+"')",500);
		}
	});

	$("#full_result_x").keypress(function(e){
		var charCode = (e.which) ? e.which : e.keyCode;
		if (charCode > 31 && (charCode < 48 || charCode > 57)) {
			return false;
		} else {
			setTimeout("fullTo15('keyPress','"+$(this).attr('id')+"')",500);
		}
	});
	$("#full_result_2").keypress(function(e){
		var charCode = (e.which) ? e.which : e.keyCode;
		if (charCode > 31 && (charCode < 48 || charCode > 57)) {
			return false;
		} else {
			setTimeout("fullTo15('keyPress','"+$(this).attr('id')+"')",500);
		}
	});
	//*/
//comentado

	$('.results-boxes-home15 a').live('click', function(){
		// comentado
		$('span.result-box', $(this)).toggleClass('active');
		return true
	})

/*
	$(".results-boxes15 a").unbind('click')
	$(".results-boxes15 a").live('click',function(){
		if($(this).data('active') == undefined || $(this).data('active') <= 0){
			$(this).data('active',1)
			$('span', $(this)).addClass('active');
		} else {
			$(this).data('active',0)
			$('span', $(this)).removeClass('active');
		}
		
		fullTo15('click');
	});
//	*/
// comentado
//*
	$('.options-item a').click(function(){
		$('.options-item').removeClass('selected');
		$('.options-item .percents').hide();
		$(this).parents('.options-item').addClass('selected');
		$('.percents', $(this).parents('.options-item')).show();

		if ($(this).parent().attr('id') == "tipo15_1") {
			$('#tipo15').val('1')
		} else {
			$('#tipo15').val('2')
		}
		
		calcula_preu();
		return false
	})
//*/

	/* Maneja los selectboxes dependientes
	 * 
	 * Debe aplicarlse la clase 'dependentSelectBox' al select que se desea manejar y se debe
	 * definir el array con datos en el objeto quinielistaData.selectBoxOptions
	 */
$('.dependentSelectBox').live('change', function(){

		var theID = $(this).attr('id'); //console.log('selectbox cambiado: ' + theID );
		var selectData = quinielistaData.selectBoxOptions[theID]; //console.log( selectData );
		var dependentID = $(this).val(); //console.log( value );


		// recorre todos los selectboxes dependientes que haya definidos
		var counter = 0;

		$(selectData).each( function() {

			counter++;
			var dependentSBData = this; //console.log( dependentSBData );
			var dependentSB = $( '#' + dependentSBData.id ); //console.log(dependentSB);

			// verifica que exista el selectbox dependiente exista
			if ( dependentSB.length > 0 ) {

				// crea el preload y carga el contenido
				createPreload( dependentSB, 'preload_dependentSelectBox' + counter, '' );

				// recorre el array de datos y va creando las opciones
				var html = '';

				$( dependentSBData.data[dependentID] ).each( function( dataKey, dataItem ) {
					html += '<option value="' + dataItem.value + '">' + dataItem.text + '</option>'; //console.log(html);
				});

				// reemplaza el contenido del select dependiente y dispara el evento change para actualizar SB dependientes
				dependentSB.html( html );

				if ( dependentSB.hasClass('dependentSelectBox') ) {
					dependentSB.change();
				}

				// destruye el preloader
				destroyPreload( 'preload_dependentSelectBox' + counter );

			};

		});

	});

	awards();
	
	// carga automaticamente el contenido externo
	$('a.tab.auto-load').click();


});




/* Estimación */
var felix = 0;
var recaudacion;
var bote;
var precio;
var Columna = new Array(3);
var Signo = new Array(15);
var Porcentaje = new Array(3);

function InicializarJS(){
	recaudacion=document.quiniela.recaudacion.value;
	bote=document.quiniela.bote.value;
	precio=document.quiniela.precio.value;

	Columna[0] = new Array(15);
	Columna[1] = new Array(15);
	Columna[2] = new Array(15);

	Porcentaje[0] = new Array(15);
	Porcentaje[1] = new Array(15);
	Porcentaje[2] = new Array(15);

	Porcentaje[0][0] = Math.round(document.quiniela.PH00.value);
	Porcentaje[1][0] = Math.round(document.quiniela.PH10.value);
	Porcentaje[2][0] = Math.round(document.quiniela.PH20.value);
	Porcentaje[0][1] = Math.round(document.quiniela.PH01.value);
	Porcentaje[1][1] = Math.round(document.quiniela.PH11.value);
	Porcentaje[2][1] = Math.round(document.quiniela.PH21.value);
	Porcentaje[0][2] = Math.round(document.quiniela.PH02.value);
	Porcentaje[1][2] = Math.round(document.quiniela.PH12.value);
	Porcentaje[2][2] = Math.round(document.quiniela.PH22.value);
	Porcentaje[0][3] = Math.round(document.quiniela.PH03.value);
	Porcentaje[1][3] = Math.round(document.quiniela.PH13.value);
	Porcentaje[2][3] = Math.round(document.quiniela.PH23.value);
	Porcentaje[0][4] = Math.round(document.quiniela.PH04.value);
	Porcentaje[1][4] = Math.round(document.quiniela.PH14.value);
	Porcentaje[2][4] = Math.round(document.quiniela.PH24.value);
	Porcentaje[0][5] = Math.round(document.quiniela.PH05.value);
	Porcentaje[1][5] = Math.round(document.quiniela.PH15.value);
	Porcentaje[2][5] = Math.round(document.quiniela.PH25.value);
	Porcentaje[0][6] = Math.round(document.quiniela.PH06.value);
	Porcentaje[1][6] = Math.round(document.quiniela.PH16.value);
	Porcentaje[2][6] = Math.round(document.quiniela.PH26.value);
	Porcentaje[0][7] = Math.round(document.quiniela.PH07.value);
	Porcentaje[1][7] = Math.round(document.quiniela.PH17.value);
	Porcentaje[2][7] = Math.round(document.quiniela.PH27.value);
	Porcentaje[0][8] = Math.round(document.quiniela.PH08.value);
	Porcentaje[1][8] = Math.round(document.quiniela.PH18.value);
	Porcentaje[2][8] = Math.round(document.quiniela.PH28.value);
	Porcentaje[0][9] = Math.round(document.quiniela.PH09.value);
	Porcentaje[1][9] = Math.round(document.quiniela.PH19.value);
	Porcentaje[2][9] = Math.round(document.quiniela.PH29.value);
	Porcentaje[0][10] = Math.round(document.quiniela.PH010.value);
	Porcentaje[1][10] = Math.round(document.quiniela.PH110.value);
	Porcentaje[2][10] = Math.round(document.quiniela.PH210.value);
	Porcentaje[0][11] = Math.round(document.quiniela.PH011.value);
	Porcentaje[1][11] = Math.round(document.quiniela.PH111.value);
	Porcentaje[2][11] = Math.round(document.quiniela.PH211.value);
	Porcentaje[0][12] = Math.round(document.quiniela.PH012.value);
	Porcentaje[1][12] = Math.round(document.quiniela.PH112.value);
	Porcentaje[2][12] = Math.round(document.quiniela.PH212.value);
	Porcentaje[0][13] = Math.round(document.quiniela.PH013.value);
	Porcentaje[1][13] = Math.round(document.quiniela.PH113.value);
	Porcentaje[2][13] = Math.round(document.quiniela.PH213.value);
	Porcentaje[0][14] = Math.round(document.quiniela.PH014.value);
	Porcentaje[1][14] = Math.round(document.quiniela.PH114.value);
	Porcentaje[2][14] = Math.round(document.quiniela.PH214.value);
}
var img1_NO = new Image(); img1_NO.src = '../img/1.gif';
var imgX_NO = new Image(); imgX_NO.src = '../img/X.gif';
var img2_NO = new Image(); img2_NO.src = '../img/2.gif';
var img1_SI = new Image(); img1_SI.src = '../img/1X.gif';
var imgX_SI = new Image(); imgX_SI.src = '../img/XX.gif';
var img2_SI = new Image(); img2_SI.src = '../img/2X.gif';
var img1_GRIS_SI = new Image(); img1_GRIS_SI.src = '../img/1Xg.gif';
var imgX_GRIS_SI = new Image(); imgX_GRIS_SI.src = '../img/XXg.gif';
var img2_GRIS_SI = new Image(); img2_GRIS_SI.src = '../img/2Xg.gif';

function Cambia_Signo(col,fila) {
	var LIntValor;

	eval("LIntValor=document.quiniela.EsDef"+(fila+1)+".value;");
	if (LIntValor==""){
		LIntValor=5;		
	}

	Signo[fila] = col;

	switch(col) {
		case 0:
			eval("document.quiniela.EsAlea"+(fila+1)+".value=0");
			if (LIntValor==0){
				Cambia_Signo_Definitivo(col,fila);
			}else{
				Columna[0][fila] = 1; document.quiniela['img1_'+fila].src = img1_GRIS_SI.src;
			}
			Columna[1][fila] = 0; document.quiniela['imgX_'+fila].src = imgX_NO.src;
			Columna[2][fila] = 0; document.quiniela['img2_'+fila].src = img2_NO.src;
			break;
		case 1:
			eval("document.quiniela.EsAlea"+(fila+1)+".value=1");
			Columna[0][fila] = 0; document.quiniela['img1_'+fila].src = img1_NO.src;
			if (LIntValor==1){
				Cambia_Signo_Definitivo(col,fila);
			}else{
				Columna[1][fila] = 1; document.quiniela['imgX_'+fila].src = imgX_GRIS_SI.src;
			}
			Columna[2][fila] = 0; document.quiniela['img2_'+fila].src = img2_NO.src;
			break;
		case 2:
			eval("document.quiniela.EsAlea"+(fila+1)+".value=2");
			Columna[0][fila] = 0; document.quiniela['img1_'+fila].src = img1_NO.src;
			Columna[1][fila] = 0; document.quiniela['imgX_'+fila].src = imgX_NO.src;
			if (LIntValor==2){
				Cambia_Signo_Definitivo(col,fila);
			}else{
				Columna[2][fila] = 1; document.quiniela['img2_'+fila].src = img2_GRIS_SI.src;
			}
			break;
	}

	if (felix==1) Estimar();
}

function Cambia_Signo_Definitivo(col,fila) {
	Signo[fila] = col;
	switch(col) {
		case 0:
			Columna[0][fila] = 1; document.quiniela['img1_'+fila].src = img1_SI.src;
			Columna[1][fila] = 0; document.quiniela['imgX_'+fila].src = imgX_NO.src;
			Columna[2][fila] = 0; document.quiniela['img2_'+fila].src = img2_NO.src;
			break;
		case 1:
			Columna[0][fila] = 0; document.quiniela['img1_'+fila].src = img1_NO.src;
			Columna[1][fila] = 1; document.quiniela['imgX_'+fila].src = imgX_SI.src;
			Columna[2][fila] = 0; document.quiniela['img2_'+fila].src = img2_NO.src;
			break;
		case 2:
			Columna[0][fila] = 0; document.quiniela['img1_'+fila].src = img1_NO.src;
			Columna[1][fila] = 0; document.quiniela['imgX_'+fila].src = imgX_NO.src;
			Columna[2][fila] = 1; document.quiniela['img2_'+fila].src = img2_SI.src;
			break;
	}
	if (felix==1) Estimar();
}

function Cambia_Signo_YaSeleccionadoAzar(col,fila){
	Signo[fila] = col;
	switch(col) {
		case 0:
			Columna[0][fila] = 1; document.quiniela['img1_'+fila].src = img1_GRIS_SI.src;
			Columna[1][fila] = 0; document.quiniela['imgX_'+fila].src = imgX_NO.src;
			Columna[2][fila] = 0; document.quiniela['img2_'+fila].src = img2_NO.src;
			break;
		case 1:
			Columna[0][fila] = 0; document.quiniela['img1_'+fila].src = img1_NO.src;
			Columna[1][fila] = 1; document.quiniela['imgX_'+fila].src = imgX_GRIS_SI.src;
			Columna[2][fila] = 0; document.quiniela['img2_'+fila].src = img2_NO.src;
			break;
		case 2:
			Columna[0][fila] = 0; document.quiniela['img1_'+fila].src = img1_NO.src;
			Columna[1][fila] = 0; document.quiniela['imgX_'+fila].src = imgX_NO.src;
			Columna[2][fila] = 1; document.quiniela['img2_'+fila].src = img2_GRIS_SI.src;
			break;
	}
	if (felix==1) Estimar(); 
}

function oNumero(numero) {
	//Propiedades 
	this.valor = numero || 0
	this.dec = -1;
	//Métodos 
	this.formato = numFormat;
	this.ponValor = ponValor;
	//Definición de los métodos 
	function ponValor(cad) {
		if (cad =='-' || cad=='+') return
		if (cad.length ==0) return
		if (cad.indexOf('.') >=0)
			this.valor = parseFloat(cad);
		else 
			this.valor = parseInt(cad);
	} 
	function numFormat(dec, miles) {
		var num = this.valor, signo=3, expr;
		var cad = ""+this.valor;
		var ceros = "", pos, pdec, i;
		for (i=0; i < dec; i++)
			ceros += '0';
		pos = cad.indexOf('.')
		if (pos < 0)
			cad = cad+"."+ceros;
		else {
			pdec = cad.length - pos -1;
			if (pdec <= dec) {
				for (i=0; i< (dec-pdec); i++)
					cad += '0';
			} else {
				num = num*Math.pow(10, dec);
				num = Math.round(num);
				num = num/Math.pow(10, dec);
				cad = new String(num);
			}
		}
		pos = cad.indexOf('.')
		if (pos < 0) pos = cad.lentgh
		if (cad.substr(0,1)=='-' || cad.substr(0,1) == '+') 
			signo = 4;
		if (miles && pos > signo)
		do{
			expr = /([+-]?\d)(\d{3}[\.\,]\d*)/
			cad.match(expr)
			cad=cad.replace(expr, RegExp.$1+','+RegExp.$2)
		}
		while (cad.indexOf(',') > signo)
		if (dec<0) cad = cad.replace(/\./,'')
			return cad;
	}
}//Fin del objeto oNumero

function Estimar(){
	var apuestas,apuestas1;
	var i,j,k,l,m;
	var de14,de13,de12,de11,de10;
	var im14,im13,im12,im11,im10;
	var P15,P14,P13,P12,P11,P10;

	//Percentatges dels premis a partir de la recaudación
	P15=10;
	P14=12;
	P13=8;
	P12=8;
	P11=8;
	P10=9;

	var numacert14,porcentaje15,numero14,numa15;

	apuestas = recaudacion/precio;
	for (i = 0; i < 14; i++)
		apuestas = apuestas * Porcentaje[Signo[i]][i] / 100;
	de14 = Math.round(apuestas);
	if (de14 < 1) im14 = Math.round(recaudacion * P14) / 100;
	else im14 = Math.round(recaudacion * P14 / de14 ) / 100;
	var numero = new oNumero(de14);     
	document.quiniela.a14.value = PonerFormatoCorrectoSinDecimales(numero.formato(0, true));
	numero14=PonerFormatoCorrectoSinDecimales(numero.formato(0, true)).replace(".","");
	var numero = new oNumero(im14); 
	document.quiniela.i14.value = PonerFormatoCorrectoMiles(numero.formato(2, true));
	apuestas1 = 0;
	for (j = 0; j < 14; j++){
		Porcentaje[Signo[j]][j] = 100 - Porcentaje[Signo[j]][j];
		apuestas = recaudacion / precio;
		for (i = 0; i < 14; i++)
			apuestas = apuestas * Porcentaje[Signo[i]][i] / 100;
		apuestas1 = apuestas1 + apuestas;
		Porcentaje[Signo[j]][j] = 100 - Porcentaje[Signo[j]][j];
	}

	//Calcul del 15 (independent a la resta de càlculs)
	numacert14=numero14;
	//alert("numacert14:  "+numacert14);
	porcentaje15=Porcentaje[Signo[14]][14];
	//alert("porcentaje15:  "+porcentaje15);
	numa15= Math.round((numacert14*porcentaje15)/100);
	//alert("numa15:  "+numa15);

	document.quiniela.a15.value = numa15;
	if (numa15!=0){
		if (bote!=0){
			numa15=((recaudacion * (P15/100))+parseFloat(bote)) / numa15;
			numa15=Math.round(numa15*100) / 100;
		} else {
			numa15=Math.round(recaudacion * P15 / numa15 ) / 100;
		}
		var numero = new oNumero(numa15);
		document.quiniela.i15.value = PonerFormatoCorrectoMiles(numero.formato(2,true));
	}else{
		document.quiniela.i15.value =0;
	}
	//Fi càlcul del 15

	de13 = Math.round(apuestas1); if (de13 < 1) de13 = 1;
	im13 = Math.round((recaudacion * P13) / de13) / 100;

	var numero = new oNumero(de13);
	document.quiniela.a13.value = PonerFormatoCorrectoSinDecimales(numero.formato(0, true));
	var numero = new oNumero(im13);
	document.quiniela.i13.value = PonerFormatoCorrectoMiles(numero.formato(2, true));

	apuestas1 = 0;
	for (k = 0; k < 13; k++){
		Porcentaje[Signo[k]][k] = 100 - Porcentaje[Signo[k]][k];
		for (j = k+1; j < 14; j++) {
			Porcentaje[Signo[j]][j] = 100 - Porcentaje[Signo[j]][j];
			apuestas = recaudacion / precio;
			for (i = 0; i < 14; i++)
				apuestas = apuestas * Porcentaje[Signo[i]][i] / 100;
			apuestas1 = apuestas1 + apuestas;
			Porcentaje[Signo[j]][j] = 100 - Porcentaje[Signo[j]][j];
		}
		Porcentaje[Signo[k]][k] = 100 - Porcentaje[Signo[k]][k];
	}
	de12 = Math.round(apuestas1); if (de12 < 1) de12 = 1;
	im12 = Math.round((recaudacion * P12) / de12) / 100;

	var numero = new oNumero(de12);
	document.quiniela.a12.value = PonerFormatoCorrectoSinDecimales(numero.formato(0, true));
	var numero = new oNumero(im12);
	document.quiniela.i12.value = PonerFormatoCorrectoMiles(numero.formato(2, true));

	apuestas1 = 0;
	for (l = 0; l < 12; l++){
		Porcentaje[Signo[l]][l] = 100 - Porcentaje[Signo[l]][l];
		for (k = l+1; k < 13; k++){
			Porcentaje[Signo[k]][k] = 100 - Porcentaje[Signo[k]][k];
			for (j = k+1; j < 14; j++){
				Porcentaje[Signo[j]][j] = 100 - Porcentaje[Signo[j]][j];
				apuestas = recaudacion / precio;
				for (i = 0; i < 14; i++)
					apuestas = apuestas * Porcentaje[Signo[i]][i] / 100;
				apuestas1 = apuestas1 + apuestas;
				Porcentaje[Signo[j]][j] = 100 - Porcentaje[Signo[j]][j];
			}
			Porcentaje[Signo[k]][k] = 100 - Porcentaje[Signo[k]][k];
		}
		Porcentaje[Signo[l]][l] = 100 - Porcentaje[Signo[l]][l];
	}
	de11 = Math.round(apuestas1); if (de11 < 1) de11 = 1;
	im11 = Math.round((recaudacion * P11) / de11) / 100;

	var numero = new oNumero(de11);
	document.quiniela.a11.value = PonerFormatoCorrectoSinDecimales(numero.formato(0, true));
	var numero = new oNumero(im11);
	document.quiniela.i11.value = PonerFormatoCorrectoMiles(numero.formato(2, true));

	apuestas1 = 0;
	for (m = 0; m < 11; m++){
		Porcentaje[Signo[m]][m] = 100 - Porcentaje[Signo[m]][m];
		for (l = 0; l < 12; l++){
			Porcentaje[Signo[l]][l] = 100 - Porcentaje[Signo[l]][l];
			for (k = l+1; k < 13; k++){
				Porcentaje[Signo[k]][k] = 100 - Porcentaje[Signo[k]][k];
				for (j = k+1; j < 14; j++){
					Porcentaje[Signo[j]][j] = 100 - Porcentaje[Signo[j]][j];
					apuestas = recaudacion / precio;
					for (i = 0; i < 14; i++)
						apuestas = apuestas * Porcentaje[Signo[i]][i] / 100;
					apuestas1 = apuestas1 + apuestas;
					Porcentaje[Signo[j]][j] = 100 - Porcentaje[Signo[j]][j];
				}
				Porcentaje[Signo[k]][k] = 100 - Porcentaje[Signo[k]][k];
			}
			Porcentaje[Signo[l]][l] = 100 - Porcentaje[Signo[l]][l];
		}
		Porcentaje[Signo[m]][m] = 100 - Porcentaje[Signo[m]][m];
	}
	de10 = Math.round(apuestas1/3.5); if (de10 < 1) de10 = 1;
	im10 = Math.round((recaudacion * P10) / de10) / 100;
	var numero = new oNumero(de10);
	document.quiniela.a10.value = PonerFormatoCorrectoSinDecimales(numero.formato(0, true));  
	var numero = new oNumero(im10);  
	document.quiniela.i10.value = PonerFormatoCorrectoMiles(numero.formato(2, true));
}

function PonerFormatoCorrectoMiles(PIntValor){
	var LIntValor;
	
	LIntValor=PIntValor;
	LIntValor=LIntValor.replace(".","*");
	LIntValor=LIntValor.replace(".","*");
	LIntValor=LIntValor.replace(".","*");
	
	LIntValor=LIntValor.replace(",",".");
	LIntValor=LIntValor.replace(",",".");
	LIntValor=LIntValor.replace(",",".");
	
	LIntValor=LIntValor.replace("*",",");
	LIntValor=LIntValor.replace("*",",");
	LIntValor=LIntValor.replace("*",",");
	
	return LIntValor;
}

function PonerFormatoCorrectoSinDecimales(PIntValor){
	var LIntValor;
	LIntValor=PIntValor;
	LIntValor=LIntValor.replace(".","");
	LIntValor=LIntValor.replace(",",".");
	LIntValor=LIntValor.replace(",",".");
	LIntValor=LIntValor.replace(",",".");
	return LIntValor;
}

function Crea_Columna_DeLaFila(PIntFila,PIntValor){
	//MGA 28/10/2004
	//Crea el valor que se le indica en la columna que tb se le indica
	//Primero se comprueba si se ha seleccionado un valor distinto al definitivo
	var LIntValor;
	var LIntValorDef;
	
	eval("LIntValor=document.quiniela.EsAlea"+(PIntFila+1)+".value;");
	eval("LIntValorDef=document.quiniela.EsDef"+(PIntFila+1)+".value;");

	if (LIntValor!=""){
		if (LIntValorDef!=""){
			LIntValor=parseInt(LIntValor);
			LIntValorDef=parseInt(LIntValorDef);
			if (LIntValor==LIntValorDef){
				//marcamos el signo como definitivo
				LIntValorDef=parseInt(LIntValorDef);
				Cambia_Signo_Definitivo(LIntValorDef,PIntFila);
			}else{
				//marcamos como No definitivo
				LIntValor=parseInt(LIntValor);
				Cambia_Signo_YaSeleccionadoAzar(LIntValor,PIntFila);
			}
		}else{
			//marcamos el signo como NO definitivo
			LIntValor=parseInt(LIntValor);
			Cambia_Signo_YaSeleccionadoAzar(LIntValor,PIntFila);
		}
	}else{
		//marcamos el signo como definitivo
		eval("document.quiniela.EsDef"+(PIntFila+1)+".value="+PIntValor+"");
		Cambia_Signo_Definitivo(PIntValor,PIntFila);
	}
}

function Crea_Columna_Azar_DeLaFila(PIntFila){
//MGA 28/10/2004
	//Crea el valor al azar solo de la fila que se le indica
	var azar;
	var LIntValor;
	eval("LIntValor=document.quiniela.EsAlea"+(PIntFila+1)+".value;");
	if (LIntValor!=""){
		// Si ya se ha seleccionado un valor aleatorio, ponemos el mismo
		LIntValor=parseInt(LIntValor);
		Cambia_Signo_YaSeleccionadoAzar(LIntValor,PIntFila);
	}else{
		//Si no se ha marcado ningun numero todavia, se crea uno al azar
		azar = Math.round(Math.random() * 10);
		if (azar < 5){
			eval("document.quiniela.EsAlea"+(PIntFila+1)+".value=0");
			Cambia_Signo(0,PIntFila);
		}else if (azar < 8){
			eval("document.quiniela.EsAlea"+(PIntFila+1)+".value=1");
			Cambia_Signo(1,PIntFila);
		}else{
			eval("document.quiniela.EsAlea"+(PIntFila+1)+".value=2");
			Cambia_Signo(2,PIntFila);
		}
	}
}


function Asignar() {
	document.quiniela.P00.selectedIndex = Math.round(Porcentaje[0][0]);
	document.quiniela.P10.selectedIndex = Math.round(Porcentaje[1][0]);
	document.quiniela.P20.selectedIndex = Math.round(Porcentaje[2][0]);
	document.quiniela.P01.selectedIndex = Math.round(Porcentaje[0][1]);
	document.quiniela.P11.selectedIndex = Math.round(Porcentaje[1][1]);
	document.quiniela.P21.selectedIndex = Math.round(Porcentaje[2][1]);
	document.quiniela.P02.selectedIndex = Math.round(Porcentaje[0][2]);
	document.quiniela.P12.selectedIndex = Math.round(Porcentaje[1][2]);
	document.quiniela.P22.selectedIndex = Math.round(Porcentaje[2][2]);
	document.quiniela.P03.selectedIndex = Math.round(Porcentaje[0][3]);
	document.quiniela.P13.selectedIndex = Math.round(Porcentaje[1][3]);
	document.quiniela.P23.selectedIndex = Math.round(Porcentaje[2][3]);
	document.quiniela.P04.selectedIndex = Math.round(Porcentaje[0][4]);
	document.quiniela.P14.selectedIndex = Math.round(Porcentaje[1][4]);
	document.quiniela.P24.selectedIndex = Math.round(Porcentaje[2][4]);
	document.quiniela.P05.selectedIndex = Math.round(Porcentaje[0][5]);
	document.quiniela.P15.selectedIndex = Math.round(Porcentaje[1][5]);
	document.quiniela.P25.selectedIndex = Math.round(Porcentaje[2][5]);
	document.quiniela.P06.selectedIndex = Math.round(Porcentaje[0][6]);
	document.quiniela.P16.selectedIndex = Math.round(Porcentaje[1][6]);
	document.quiniela.P26.selectedIndex = Math.round(Porcentaje[2][6]);
	document.quiniela.P07.selectedIndex = Math.round(Porcentaje[0][7]);
	document.quiniela.P17.selectedIndex = Math.round(Porcentaje[1][7]);
	document.quiniela.P27.selectedIndex = Math.round(Porcentaje[2][7]);
	document.quiniela.P08.selectedIndex = Math.round(Porcentaje[0][8]);
	document.quiniela.P18.selectedIndex = Math.round(Porcentaje[1][8]);
	document.quiniela.P28.selectedIndex = Math.round(Porcentaje[2][8]);
	document.quiniela.P09.selectedIndex = Math.round(Porcentaje[0][9]);
	document.quiniela.P19.selectedIndex = Math.round(Porcentaje[1][9]);
	document.quiniela.P29.selectedIndex = Math.round(Porcentaje[2][9]);
	document.quiniela.P010.selectedIndex = Math.round(Porcentaje[0][10]);
	document.quiniela.P110.selectedIndex = Math.round(Porcentaje[1][10]);
	document.quiniela.P210.selectedIndex = Math.round(Porcentaje[2][10]);
	document.quiniela.P011.selectedIndex = Math.round(Porcentaje[0][11]);
	document.quiniela.P111.selectedIndex = Math.round(Porcentaje[1][11]);
	document.quiniela.P211.selectedIndex = Math.round(Porcentaje[2][11]);
	document.quiniela.P012.selectedIndex = Math.round(Porcentaje[0][12]);
	document.quiniela.P112.selectedIndex = Math.round(Porcentaje[1][12]);
	document.quiniela.P212.selectedIndex = Math.round(Porcentaje[2][12]);
	document.quiniela.P013.selectedIndex = Math.round(Porcentaje[0][13]);
	document.quiniela.P113.selectedIndex = Math.round(Porcentaje[1][13]);
	document.quiniela.P213.selectedIndex = Math.round(Porcentaje[2][13]);
	document.quiniela.P014.selectedIndex = Math.round(Porcentaje[0][14]);
	document.quiniela.P114.selectedIndex = Math.round(Porcentaje[1][14]);
	document.quiniela.P214.selectedIndex = Math.round(Porcentaje[2][14]);
}
function Asignar1() {
	document.quiniela.P00.value = Math.round(Porcentaje[0][0]);
	document.quiniela.P10.value = Math.round(Porcentaje[1][0]);
	document.quiniela.P20.value = Math.round(Porcentaje[2][0]);
	document.quiniela.P01.value = Math.round(Porcentaje[0][1]);
	document.quiniela.P11.value = Math.round(Porcentaje[1][1]);
	document.quiniela.P21.value = Math.round(Porcentaje[2][1]);
	document.quiniela.P02.value = Math.round(Porcentaje[0][2]);
	document.quiniela.P12.value = Math.round(Porcentaje[1][2]);
	document.quiniela.P22.value = Math.round(Porcentaje[2][2]);
	document.quiniela.P03.value = Math.round(Porcentaje[0][3]);
	document.quiniela.P13.value = Math.round(Porcentaje[1][3]);
	document.quiniela.P23.value = Math.round(Porcentaje[2][3]);
	document.quiniela.P04.value = Math.round(Porcentaje[0][4]);
	document.quiniela.P14.value = Math.round(Porcentaje[1][4]);
	document.quiniela.P24.value = Math.round(Porcentaje[2][4]);
	document.quiniela.P05.value = Math.round(Porcentaje[0][5]);
	document.quiniela.P15.value = Math.round(Porcentaje[1][5]);
	document.quiniela.P25.value = Math.round(Porcentaje[2][5]);
	document.quiniela.P06.value = Math.round(Porcentaje[0][6]);
	document.quiniela.P16.value = Math.round(Porcentaje[1][6]);
	document.quiniela.P26.value = Math.round(Porcentaje[2][6]);
	document.quiniela.P07.value = Math.round(Porcentaje[0][7]);
	document.quiniela.P17.value = Math.round(Porcentaje[1][7]);
	document.quiniela.P27.value = Math.round(Porcentaje[2][7]);
	document.quiniela.P08.value = Math.round(Porcentaje[0][8]);
	document.quiniela.P18.value = Math.round(Porcentaje[1][8]);
	document.quiniela.P28.value = Math.round(Porcentaje[2][8]);
	document.quiniela.P09.value = Math.round(Porcentaje[0][9]);
	document.quiniela.P19.value = Math.round(Porcentaje[1][9]);
	document.quiniela.P29.value = Math.round(Porcentaje[2][9]);
	document.quiniela.P010.value = Math.round(Porcentaje[0][10]);
	document.quiniela.P110.value = Math.round(Porcentaje[1][10]);
	document.quiniela.P210.value = Math.round(Porcentaje[2][10]);
	document.quiniela.P011.value = Math.round(Porcentaje[0][11]);
	document.quiniela.P111.value = Math.round(Porcentaje[1][11]);
	document.quiniela.P211.value = Math.round(Porcentaje[2][11]);
	document.quiniela.P012.value = Math.round(Porcentaje[0][12]);
	document.quiniela.P112.value = Math.round(Porcentaje[1][12]);
	document.quiniela.P212.value = Math.round(Porcentaje[2][12]);
	document.quiniela.P013.value = Math.round(Porcentaje[0][13]);
	document.quiniela.P113.value = Math.round(Porcentaje[1][13]);
	document.quiniela.P213.value = Math.round(Porcentaje[2][13]);
	document.quiniela.P014.value = Math.round(Porcentaje[0][14]);
	document.quiniela.P114.value = Math.round(Porcentaje[1][14]);
	document.quiniela.P214.value = Math.round(Porcentaje[2][14]);
}

function Modifica(col) {
	Porcentaje[0][0] = Math.round(document.quiniela.P00.options[document.quiniela.P00.selectedIndex].value);
	Porcentaje[1][0] = Math.round(document.quiniela.P10.options[document.quiniela.P10.selectedIndex].value);
	Porcentaje[2][0] = Math.round(document.quiniela.P20.options[document.quiniela.P20.selectedIndex].value);
	Porcentaje[0][1] = Math.round(document.quiniela.P01.options[document.quiniela.P01.selectedIndex].value);
	Porcentaje[1][1] = Math.round(document.quiniela.P11.options[document.quiniela.P11.selectedIndex].value);
	Porcentaje[2][1] = Math.round(document.quiniela.P21.options[document.quiniela.P21.selectedIndex].value);
	Porcentaje[0][2] = Math.round(document.quiniela.P02.options[document.quiniela.P02.selectedIndex].value);
	Porcentaje[1][2] = Math.round(document.quiniela.P12.options[document.quiniela.P12.selectedIndex].value);
	Porcentaje[2][2] = Math.round(document.quiniela.P22.options[document.quiniela.P22.selectedIndex].value);
	Porcentaje[0][3] = Math.round(document.quiniela.P03.options[document.quiniela.P03.selectedIndex].value);
	Porcentaje[1][3] = Math.round(document.quiniela.P13.options[document.quiniela.P13.selectedIndex].value);
	Porcentaje[2][3] = Math.round(document.quiniela.P23.options[document.quiniela.P23.selectedIndex].value);
	Porcentaje[0][4] = Math.round(document.quiniela.P04.options[document.quiniela.P04.selectedIndex].value);
	Porcentaje[1][4] = Math.round(document.quiniela.P14.options[document.quiniela.P14.selectedIndex].value);
	Porcentaje[2][4] = Math.round(document.quiniela.P24.options[document.quiniela.P24.selectedIndex].value);
	Porcentaje[0][5] = Math.round(document.quiniela.P05.options[document.quiniela.P05.selectedIndex].value);
	Porcentaje[1][5] = Math.round(document.quiniela.P15.options[document.quiniela.P15.selectedIndex].value);
	Porcentaje[2][5] = Math.round(document.quiniela.P25.options[document.quiniela.P25.selectedIndex].value);
	Porcentaje[0][6] = Math.round(document.quiniela.P06.options[document.quiniela.P06.selectedIndex].value);
	Porcentaje[1][6] = Math.round(document.quiniela.P16.options[document.quiniela.P16.selectedIndex].value);
	Porcentaje[2][6] = Math.round(document.quiniela.P26.options[document.quiniela.P26.selectedIndex].value);
	Porcentaje[0][7] = Math.round(document.quiniela.P07.options[document.quiniela.P07.selectedIndex].value);
	Porcentaje[1][7] = Math.round(document.quiniela.P17.options[document.quiniela.P17.selectedIndex].value);
	Porcentaje[2][7] = Math.round(document.quiniela.P27.options[document.quiniela.P27.selectedIndex].value);
	Porcentaje[0][8] = Math.round(document.quiniela.P08.options[document.quiniela.P08.selectedIndex].value);
	Porcentaje[1][8] = Math.round(document.quiniela.P18.options[document.quiniela.P18.selectedIndex].value);
	Porcentaje[2][8] = Math.round(document.quiniela.P28.options[document.quiniela.P28.selectedIndex].value);
	Porcentaje[0][9] = Math.round(document.quiniela.P09.options[document.quiniela.P09.selectedIndex].value);
	Porcentaje[1][9] = Math.round(document.quiniela.P19.options[document.quiniela.P19.selectedIndex].value);
	Porcentaje[2][9] = Math.round(document.quiniela.P29.options[document.quiniela.P29.selectedIndex].value);
	Porcentaje[0][10] = Math.round(document.quiniela.P010.options[document.quiniela.P010.selectedIndex].value);
	Porcentaje[1][10] = Math.round(document.quiniela.P110.options[document.quiniela.P110.selectedIndex].value);
	Porcentaje[2][10] = Math.round(document.quiniela.P210.options[document.quiniela.P210.selectedIndex].value);
	Porcentaje[0][11] = Math.round(document.quiniela.P011.options[document.quiniela.P011.selectedIndex].value);
	Porcentaje[1][11] = Math.round(document.quiniela.P111.options[document.quiniela.P111.selectedIndex].value);
	Porcentaje[2][11] = Math.round(document.quiniela.P211.options[document.quiniela.P211.selectedIndex].value);
	Porcentaje[0][12] = Math.round(document.quiniela.P012.options[document.quiniela.P012.selectedIndex].value);
	Porcentaje[1][12] = Math.round(document.quiniela.P112.options[document.quiniela.P112.selectedIndex].value);
	Porcentaje[2][12] = Math.round(document.quiniela.P212.options[document.quiniela.P212.selectedIndex].value);
	Porcentaje[0][13] = Math.round(document.quiniela.P013.options[document.quiniela.P013.selectedIndex].value);
	Porcentaje[1][13] = Math.round(document.quiniela.P113.options[document.quiniela.P113.selectedIndex].value);
	Porcentaje[2][13] = Math.round(document.quiniela.P213.options[document.quiniela.P213.selectedIndex].value);
	Porcentaje[0][14] = Math.round(document.quiniela.P014.options[document.quiniela.P014.selectedIndex].value);
	Porcentaje[1][14] = Math.round(document.quiniela.P114.options[document.quiniela.P114.selectedIndex].value);
	Porcentaje[2][14] = Math.round(document.quiniela.P214.options[document.quiniela.P214.selectedIndex].value);

	for (var i=0;i<15;i++) {
		var v0=eval(Porcentaje[0][i]);
		var v1=eval(Porcentaje[1][i]);
		var v2=eval(Porcentaje[2][i]);
		var suma=v0+v1+v2;
		switch(col) {
			case 0:
				if ((v0+v1)>100) {
					Porcentaje[1][i]=100-v0;
					Porcentaje[2][i]=0;
				} else {
					Porcentaje[2][i]=100-v0-v1;
				}
				break;
				case 1:
					if ((v1+v0)>100) {
						Porcentaje[0][i]=100-v1;
						Porcentaje[2][i]=0;
					} else {
						Porcentaje[2][i]=100-v1-v0;
					}
					break;
				case 2:
					if ((v0+v2)>100) {
						Porcentaje[0][i]=100-v2;
						Porcentaje[1][i]=0;
					} else {
						Porcentaje[1][i]=100-v0-v2;
					}
				break;
		}
	}
	Asignar1();
	Estimar();
}

function Pon100(col,fila) {
	document.write ("<select name=P"+col+fila+" onChange='javascript:Modifica("+col+")' size=1>");
	for (var i = 0; i < 101; i++) document.write ('<option value='+i+'>'+i+'</option>');
	document.write ('</select>');
}

function MostrarPorcentajes(){
	var LIntOpcionCombo;
	var LStrPag;
	var LStrIdioma;
	var LIntSubmit;

	LIntSubmit=0;
	LStrIdioma=document.quiniela.idioma.value;
	LStrPag="escrutinios_e.asp";

	LIntOpcionCombo=document.quiniela.SelectPorcentajes.options[document.quiniela.SelectPorcentajes.selectedIndex].value;

	if (LIntOpcionCombo==0){
		//Porcentajes de analisis
		document.quiniela.porcentajes.value=0;
		LIntSubmit=1;
	}
	if (LIntOpcionCombo==1){
		//Porcentajes de analisis
		document.quiniela.porcentajes.value=1;
		LIntSubmit=1;
	}
	if (LIntOpcionCombo==2){
		//Porcentajes de concurso
		document.quiniela.porcentajes.value=2;
		LIntSubmit=1;
	}
	
	if (LIntSubmit==1){
		document.quiniela.action=LStrPag;
		document.quiniela.submit();
	}
}

//-->
function fp_estimador() {
	v_estimador = window.open('/estimador.asp', 'v_estimador', 'width=580,height=500,scrollbars=no');
	v_estimador.focus();
}
/* FIN Estimación */

// POP UP PARTIDOS BOLETOS
function pop_info(partido, jornada, temporada){
	uri = "/ayudas/module_ticket_statistics_popup.asp?num="+partido+"&jornada="+jornada+"&temporada="+temporada;

	v_pop = window.open(uri, 'v_pop', 'width=740,height=600,scrollbars=YES');
	v_pop.focus();
}
/* fin POP UP PARTIDOS BOLETOS */
// POP UP PARTIDOS BOLETOS QUINIGOL
function pop_info_qg(partido, jornada, temporada){
	uri = "/ayudas/module_ticket_statistics_quinigol_popup.asp?num="+partido+"&jornada="+jornada+"&temporada="+temporada;
	
	v_pop = window.open(uri, 'v_pop', 'width=740,height=600,scrollbars=YES');
	v_pop.focus();
}
/* fin POP UP PARTIDOS BOLETOS QUINIGOL */


// number formatting function
// copyright Stephen Chapman 24th March 2006, 22nd August 2008
// permission to use this function is granted provided
// that this copyright notice is retained intact

function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {var x = Math.round(num * Math.pow(10,dec));if (x >= 0) n1=n2='';var y = (''+Math.abs(x)).split('');var z = y.length - dec; if (z<0) z--; for(var i = z; i < 0; i++) y.unshift('0'); if (z<0) z = 1; y.splice(z, 0, pnt); if(y[0] == pnt) y.unshift('0'); while (z > 3) {z-=3; y.splice(z,0,thou);}var r = curr1+n1+y.join('')+n2+curr2;return r;}

/////////

