// And the browser goes to...
ie4 = (document.all)? true:false;
ie6 = ((document.all) && (document.getElementById))? true:false;
ns6 = ((!document.all) && (document.getElementById))? true:false;

passport = ((ie4) || (ns6) || (ie6))? true:false;

// Excepciones
if(navigator.appVersion.indexOf("Konqueror") != -1) passport = false;
else if(navigator.appVersion.indexOf("Opera") != -1) passport = false;
function CorrigeApuesta(valor){
	valor = valor.toUpperCase();
	var combinacionesPosibles = ["1","X","2","1X","12","X2","1X2"];
	for(var i=0;i<combinacionesPosibles.length;i++){
		if(valor==combinacionesPosibles[i]){
			return valor;
		}
	}
	return '';
}
function ValidaAM(){
	if(document.QUINIELA.elements['importe'].value == '') {
		alert('ERROR\n\nEl importe no puede estar vacío!');
		return false;
	}

	if(""+Number(document.QUINIELA.elements['importe'].value) == 'NaN') {
		alert('ERROR\n\nEl importe no es un número válido!');
		return false;
	}
	if(Number(document.QUINIELA.elements['importe'].value) < 1) {
		alert('ERROR\n\nEl importe mínimo es de un euro!');
		return false;
	}
	var nPartidosSimples = 0;
	for(var i=9;i<14;i++){
		var value = document.QUINIELA.elements['apuesta_'+String(i+1)].value;
		if(value != '') nPartidosSimples ++;
	}
	if(nPartidosSimples>4) {
		alert('ERROR\n\nDebes seleccionar solamente 4 partidos con apuestas simples!');
		return false;
	}

	return true;
}
function ImprimeBoleto(numero){
	var win = window.open("about:blank","imprimeboleto","width=550,height=400,scrollbars=1,status=1,toolbar=0,resizable=0,location=0");
	win.focus();
	document.forms['BOLETO'+numero].target='imprimeboleto';
	document.forms['BOLETO'+numero].submit();
}
function CorrigeNumero(valor){
	var a = Number(valor);
	if(String(a) == 'NaN'||a>15||a<0){
		return '';
	}
	return valor;
}
function AltaCobros(){
	var win = window.open("/jsp/esperealtacobros.jsp","altaquinicobros","width=400,height=300,scrollbars=1,status=1,toolbar=0,resizable=0,location=0");
	win.focus();
}
function Sugerencia(){
	var win = window.open("/jsp/sugerencia.jsp","sugerencia","width=500,height=400,scrollbars=1,status=1,toolbar=0,resizable=0,location=0");
	win.focus();
}
function AltaUsuario(){
	var win = window.open("/jsp/altausuario.jsp","nuevousuario","width=640,height=480,scrollbars=1,status=1,toolbar=0,resizable=0,location=0");
	win.focus();
}
function CobrarPremio(premio,usuario){
	var win = window.open("/jsp/esperecobrarpremio.jsp?premio="+premio+"&usuario="+usuario,"quinicobros","width=400,height=300,scrollbars=1,status=1,toolbar=0,resizable=0,location=0");
	win.focus();
}
function Estadisticas(local,visitante){
	var win = window.open("/jsp/historico.jsp?local="+escape(local)+"&visitante="+escape(visitante),"historico","width=500,height=400,scrollbars=1,status=1,toolbar=0,resizable=0,location=0");
	win.focus();
}
function Evolucion(local,visitante){
	var win = window.open("/jsp/historicoev.jsp?local="+escape(local)+"&visitante="+escape(visitante),"evolucion","width=600,height=500,scrollbars=1,status=1,toolbar=0,resizable=0,location=0");
	win.focus();
}
function ValidaLogin(){
	document.login.elements['j_username'].value = document.login.elements['j_username'].value.toUpperCase();
	return true;
}
function ValidaQuini(){
	for(var i=0;i<15;i++){
		var value = document.QUINIELA.elements['apuesta_'+String(i+1)].value;
		if(value == '') break;
	}
	if(i!=15) {
		alert('ERROR\n\nDebe rellenar todas las apuestas!');
		return false;
	}
	return true;
}
function ValidaMultiple(){
	for(var i=0;i<15;i++){
		var value = document.QUINIELA.elements['apuesta_'+String(i+1)].value;
		if(value == '') break;
	}
	if(i!=15) {
		alert('ERROR\n\nDebe rellenar todas las apuestas!');
		return false;
	}
	
	if(document.QUINIELA.elements['max_variantes'].value=='') {
		alert('ERROR\n\nEl número máximo de variantes no puede\nestar vacío!');
		return false;
	}
	if(document.QUINIELA.elements['max_equis'].value=='') {
		alert('ERROR\n\nEl número máximo de equis no puede\nestar vacío!');
		return false;
	}
	if(document.QUINIELA.elements['max_doses'].value=='') {
		alert('ERROR\n\nEl número máximo de doses no puede\nestar vacío!');
		return false;
	}
	if(document.QUINIELA.elements['min_variantes'].value=='') {
		alert('ERROR\n\nEl número mínimo de variantes no puede\nestar vacío!');
		return false;
	}
	if(document.QUINIELA.elements['min_equis'].value=='') {
		alert('ERROR\n\nEl número mínimo de equis no puede\nestar vacío!');
		return false;
	}
	if(document.QUINIELA.elements['min_doses'].value=='') {
		alert('ERROR\n\nEl número mínimo de doses no puede\nestar vacío!');
		return false;
	}
	if(Number(document.QUINIELA.elements['min_doses'].value)>Number(document.QUINIELA.elements['max_doses'].value)) {
		alert('ERROR\n\nEl número mínimo de doses no puede superar\nal número máximo de doses!');
		return false;
	}
	if(Number(document.QUINIELA.elements['min_equis'].value)>Number(document.QUINIELA.elements['max_equis'].value)) {
		alert('ERROR\n\nEl número mínimo de equis no puede superar\nal número máximo de equis!');
		return false;
	}
	if(Number(document.QUINIELA.elements['min_variantes'].value)>Number(document.QUINIELA.elements['max_variantes'].value)) {
		alert('ERROR\n\nEl número mínimo de variantes no puede superar\nal número máximo de variantes!');
		return false;
	}
	return true;
}
function ValidaSuplantar(){
	var ok = ValidaQuini();
	if(ok){
		ok = confirm("Estás seguro q deseas suplantar a "+document.QUINIELA.elements['USUARIO'].value+"\nen la JORNADA "+document.QUINIELA.elements['JORNADA'].value);
	}
	return ok;
}
function ValidaJornada(){
	if( document.QUINIELA.elements['JORNADA'].value == '' ) {
		alert('ERROR\n\nIntroduce una jornada!');
		return false;
	}
	return true;
}
function ValidaAltaUsuario(){
	if( document.PREFS.elements['PASSWORD'].value!=document.PREFS.elements['R_PASSWORD'].value){
		alert('ERROR\n\nLos passwords no coinciden!');
		return false;
	}
	if( document.PREFS.elements['NOMBRE'].value=='' ){
		alert('ERROR\n\nEl nombre no puede estar en blanco!');
		return false;
	}
	if( document.PREFS.elements['EMAIL'].value=='' ){
		alert('ERROR\n\nEl e-mail no puede estar en blanco!');
		return false;
	}
	if( !IsEmail(document.PREFS.elements['EMAIL'].value) ){
		alert('ERROR\n\nEl e-mail introducido no es correcto!');
		return false;
	}
	return true;
}
function ValidaPreferencias(){
	if(document.PREFS.elements['AUTO_APOSTAR'].value == '') {
		alert('ERROR\n\nEl importe no puede estar vacío!');
		return false;
	}
	if(""+Number(document.PREFS.elements['AUTO_APOSTAR'].value) == 'NaN') {
		alert('ERROR\n\nEl importe no es un número válido!');
		return false;
	}

	if( document.PREFS.elements['PASSWORD'].value!=document.PREFS.elements['R_PASSWORD'].value){
		alert('ERROR\n\nLos passwords no coinciden!');
		return false;
	}
	if( document.PREFS.elements['MOVIL'].value!= '' && String(Number(document.PREFS.elements['MOVIL'].value))=='NaN'){
		alert('ERROR\n\nEl móvil introducido no es correcto!');
		return false;
	}
	if( document.PREFS.elements['EMAIL'].value!= '' && (document.PREFS.elements['EMAIL'].value.indexOf(" ")!=-1 || document.PREFS.elements['EMAIL'].value.indexOf("@")==-1 || document.PREFS.elements['EMAIL'].value.indexOf(".")==-1)){
		alert('ERROR\n\nEl e-mail introducido no es correcto!');
		return false;
	}
	return true;
}
function ValidaNuevoUsuario(){
	return ValidaPreferencias();
}
function ValidaSMS(){
	if( document.SMS.elements['MOVIL'].value== '' && document.SMS.elements['USUARIO'].value== '' && !document.SMS.elements['TODOS'].checked){
		alert('ERROR\n\nTienes que introducir el móvil o el usuario!');
		return false;
	}
	if( document.SMS.elements['MOVIL'].value!= '' && String(Number(document.SMS.elements['MOVIL'].value))=='NaN'){
		alert('ERROR\n\nEl móvil introducido no es correcto!');
		return false;
	}
	return true;
}
function ValidaEMAIL(){
	if( document.EMAIL.elements['EMAIL'].value== '' && document.EMAIL.elements['USUARIO'].value== '' && !document.EMAIL.elements['TODOS'].checked){
		alert('ERROR\n\nTienes que introducir el e-mail o el usuario!');
		return false;
	}
	if( document.EMAIL.elements['EMAIL'].value!= '' && !IsEmail(document.EMAIL.elements['EMAIL'].value)){
		alert('ERROR\n\nEl e-mail introducido no es correcto!');
		return false;
	}
	return true;
}
function ValidaAvisar(){
	if( document.AVISAR.elements['USUARIOS'].value== ''){
		alert('ERROR\n\nNo hay usuarios que no han enviado la quiniela!');
		return false;
	}
	return true;
}
function ValidaPremios(){
	var elements = document.PREMIOS.elements;
	for(var i=0;i<elements.length;i++){
		if( elements[i].name.indexOf("IMPORTE_")==0 && String(Number(elements[i].value))=='NaN'){
			alert('ERROR\n\nAlgunos de los premios introducidos no son números válidos!');
			return false;
		}
	}
	return true;
}
function VerUsuario(nick){
	var win = window.open("/jsp/showuser.jsp?user="+nick,"user","width=250,height=320,scrollbars=1,status=1,toolbar=0,resizable=0,location=0");
	win.focus();
}
function ValidaTodasJornadas(){
	if(document.NUEVO.USUARIO.value == ''){
		alert('ERROR\n\nEl usuario no puede estar en blanco!');
		return false;
	}
	document.NUEVO.USUARIO.value = document.NUEVO.USUARIO.value.toUpperCase();
	return true;
}
function IsEmail(value) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(value)){
		return true;
	}
	return false;
}
function ValidaSugerencia(){
	if( document.forms[0].elements['MSG'].value=='' ){
		alert('ERROR\n\nEl texto del mensaje no puede\nestar vacío!');
		return false;
	}
	if( document.forms[0].elements['FROM'].value=='' ){
		alert('ERROR\n\nEl e-mail no puede estar vacío!');
		return false;
	}
	if( !IsEmail(document.forms[0].elements['FROM'].value) ){
		alert('ERROR\n\nEl e-mail introducido no es correcto!');
		return false;
	}
	return true;
}
