function elemento(obj){
  return document.getElementById(obj);
}

var videoSel=1;
function selVideo(num){
  var num= num || 1;
  if (num != videoSel){
    elemento('disco'+num).src= elemento('disco'+num).src.replace('off.gif','on.gif');
    elemento('disco'+videoSel).src= elemento('disco'+videoSel).src.replace('on.gif','off.gif');
    elemento('info_video'+videoSel).style.display= 'none';
    elemento('info_video'+num).style.display= 'block';
    var video= 'SJeqtP6hoaQ';
    var tipo= 1;
    switch (num){
      case 2: video= 'NbAnRbk5-IA';
              tipo= 1;
              break;
      case 3: video= 'EHkQjDthR5A';
              tipo= 1;
              break;
      case 4: video= '31564709';
              tipo= 2;
              break;
      case 5: video= 'KNup8g5EKU4';
              tipo= 1;
              break;
      default:video= 'SJeqtP6hoaQ';
              tipo= 1;
    }
    mostrarVideoConcurso(video,tipo)
    videoSel= num;
  }
}

function mostrarVideoConcurso(vid,tipo){
	if (tipo==1){
		elemento('pantalla').innerHTML= '<object width="425" height="344">'
																	+ '	<param name="movie" value="http://es.youtube.com/v/'+vid+'"></param>'
																	+ '	<param name="wmode" value="transparent"></param>'
																	+ '	<embed src="http://es.youtube.com/v/'+vid+'&autoplay=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="344"></embed>'
																	+ '</object><br />';
	} else {
		elemento('pantalla').innerHTML= '<embed src="http://lads.myspace.com/videos/vplayer.swf" flashvars="m='+vid+'&v=2&type=video&autoplay=1" type="application/x-shockwave-flash" width="425" height="344"></embed><br />';	
	}
}

function pantalla(num){
  var num= num || 0;
  switch (num){
    case 1: elemento('instrucciones').style.display= 'none';
            elemento('television').style.display= 'block';
            elemento('formulario').style.display= 'none';
            elemento('seleccion').style.display= 'block';
            elemento('botones2').style.display= 'none';
            elemento('botones1').style.display= 'block';
            break;
    case 2: elemento('instrucciones').style.display= 'none';
            elemento('television').style.display= 'block';
            elemento('formulario').style.display= 'block';
            elemento('seleccion').style.display= 'none';
            elemento('botones2').style.display= 'block';
            elemento('botones1').style.display= 'none';
            break;
    case 3: elemento('instrucciones').style.display= 'none';
            elemento('television').style.display= 'block';
            elemento('formulario').style.display= 'none';
            elemento('seleccion').style.display= 'none';
            elemento('botones2').style.display= 'none';
            elemento('botones1').style.display= 'none';
            elemento('enviado_OK').style.display= 'block';
            break;
    case 4: elemento('instrucciones').style.display= 'none';
            elemento('television').style.display= 'block';
            elemento('formulario').style.display= 'none';
            elemento('seleccion').style.display= 'none';
            elemento('botones2').style.display= 'none';
            elemento('botones1').style.display= 'none';
            elemento('enviado_YA').style.display= 'block';
            break;
    default:elemento('television').style.display= 'none';
            elemento('formulario').style.display= 'none';
            elemento('seleccion').style.display= 'none';
            elemento('botones2').style.display= 'none';
            elemento('botones1').style.display= 'none';
            elemento('instrucciones').style.display= 'block';
  }
}

function comprobarEmail (nombre_input) {
	var valido= true;
	if (elemento(nombre_input)){
		var valor= elemento(nombre_input).value;
		if (valor == ''){
			valido= false;
		} else {
			var partes_mail= valor.split('@');
			if (partes_mail.length != 2){
				valido= false;
			} else {
				var mail_server = partes_mail[1].split('.');
				if (mail_server.length < 2){
					valido= false;
				} else {
					if ((partes_mail[0].length < 2) || (mail_server[mail_server.length - 1].length < 2) || (mail_server[mail_server.length - 1].length > 3)){
						valido= false;
					} else {
						for (var i=0; i < (mail_server.length - 1); i++){
							if (mail_server[i].length < 2){
								valido= false;
							}
						}
					}
				}
			}
		}
	} else {
		valido= false;
	}
	return valido;
}

function validarForm() {
	var color_error= '#fc9';
	elemento('nombre').style.background= '#fff';
	elemento('email').style.background= '#fff';
	var fallo= 0;
	if (elemento('nombre').value == ''){
		fallo= 1;
		elemento('nombre').style.background= color_error;
	}
	if (elemento('clave').value == ''){
		fallo= 1;
		elemento('clave').style.background= color_error;
	}
	var respuesta= elemento('respuesta').value.replace('"','').replace("'","").replace('&','');
	if (respuesta == ''){
		fallo= 1;
		elemento('respuesta').style.background= color_error;
	}
	
	if (! comprobarEmail('email')){
		fallo= 1;
		elemento('email').style.background= color_error;
	}
	
	if (elemento('aceptar_bases').checked){
		var aceptar= 0;
		elemento('aceptar_bases').style.background= 'transparent';
	} else {
		var aceptar= 1;
		elemento('aceptar_bases').style.background= color_error;
	}
	
	if (aceptar){
		alert('Debe aceptar las bases del concurso');
	}
	if (fallo){
		alert('Debe rellenar correctamente todos los campos del formulario');
	} else {
		if (! aceptar){
			sendDatosConcurso();
		}
	}
}

function sendDatosConcurso(){
	var sel= elemento('respuesta').value.replace('"','').replace("'","").replace('&','');
	var url_envio = 'concurso/enviar_datos.php?id=70&tipo=3&sel='+sel;
	var nombre= elemento('nombre').value;
	var email= elemento('email').value;
	var clave= elemento('clave').value;
	http.open('post',url_envio, true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.setRequestHeader('Accept-Charset', 'UTF-8');
	http.onreadystatechange = handleSendDatosConcurso;
	http.send('nombre='+nombre+'&email='+email+'&clave='+clave);
}

function handleSendDatosConcurso () {
	if(http.readyState == 4) {
		var txt = http.responseText;
		if (txt == 'ERROR'){
			alert('Se produjo un error al enviar sus datos');
		} else {
		  //limpiamos los datos del formulario
		  elemento('nombre').value= '';
		  elemento('email').value= '';
		  elemento('clave').value= '';
		  elemento('respuesta').value= '';
		  elemento('aceptar_bases').checked= false;
		  
		  if (txt == 'YA'){
		    pantalla(4);
		  } else {
		    pantalla(3);
		  }
		}
	}
}