var myWidth = 0;
var myHeight = 0;

function Random(inferior,superior){
  return parseInt(inferior) + Math.round( Math.random() * ( superior - inferior ) );
}


function Medidas(){
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  }else if( document.documentElement ){
    if ( document.documentElement.clientWidth || document.documentElement.clientHeight ){
      //IE 6+ in 'standards compliant mode'
      myWidth = document.documentElement.clientWidth;
      myHeight = document.documentElement.clientHeight;
    }else if ( document.body.clientWidth || document.body.clientHeight ){
      //IE 4 compatible
      myWidth = document.body.clientWidth;
      myHeight = document.body.clientHeight;
    }
  }else if( document.body ){
    if( document.body.clientWidth || document.body.clientHeight ){
      //IE 4 compatible
       myWidth = document.body.clientWidth;
       myHeight = document.body.clientHeight;
     }
  }
}


function Cargar( Sec ){
  if (Sec == "FormularioInscripcion"){
    window.open("http://privado.xn--programaad-29a.es/faces/inscripcion0.jsp");
  }else if(Sec == "ZonaEmpresa"){
    window.open("http://privado.xn--programaad-29a.es/faces/protected/empresa/index.jsp");
  }else{
    location.href="index.asp?Seccion=" + Sec;
  }
}

function ReEstructurar(){
  window.moveTo(0,0);
  window.resizeTo(screen.availWidth, screen.availHeight);

  Medidas();
  
  var Contenido = document.getElementById("Contenido");
  var Cuerpo = document.getElementById("Cuerpo");
  var Pie = document.getElementById("Pie").style;
  var Pie2 = document.getElementById("Pie2").style;
  var Pie0 = document.getElementById("Pie0").style;
  var Texto = document.getElementById("ContenidoTexto");
  var Scroll = 0;
  
  Contenido.style.visibility = "visible";
  Contenido.style.display = "block";

  if (Safari){
    if (myHeight == document.body.scrollHeight && myHeight<=1000){Scroll = myHeight-196-50-30-56;}
    else{
      var Aux = document.body.scrollHeight - myHeight;
      Scroll=document.body.scrollHeight;
      if (Aux < 120){
        Scroll += (120-Aux);
      }
    }
  }else{
    if (myHeight == document.documentElement.scrollHeight && myHeight<=1000){Scroll = myHeight-196-50-30-56;}
    else{
      var Aux = document.documentElement.scrollHeight - myHeight;
      Scroll=document.documentElement.scrollHeight;
      if (Aux < 120 && Aux>0){
        Scroll += (120-Aux);
      }
    }
  }
//  Cuerpo.style.height = (Texto.scrollHeight + 419 + (56 - (Texto.scrollHeight+419) % 56)) + "px";
  Cuerpo.style.height = (Scroll + (56 - (Scroll) % 56)) + "px";
  Pie.top = (30 + parseInt(Cuerpo.style.height,10)) + "px";
  Pie0.top = (parseInt(Pie.top,10) + 58) + "px";
  Pie2.top = (parseInt(Pie0.top,10) + 196) + "px";
}

function ActivarFondo(){
  var IE = navigator.appVersion.indexOf("MSIE") != -1;

  var Fondo = document.getElementById("Oscuro");

  Fondo.style.width = myWidth + "px";
  Fondo.style.height = myHeight + "px";
  if (IE){
    Fondo.style.top = document.documentElement.scrollTop + "px";
  }else{
    Fondo.style.top = window.pageYOffset + "px";
  }
  Fondo.style.left = "0px";
  Fondo.style.display = "block";
  Fondo.style.visibility = "visible";
    
  document.body.style.overflow = "hidden";
  
  Fondo.onclick = function(){
    DesActivarFondo();
  }
}
function DesActivarFondo(){
  document.body.style.overflow = "auto";
  var Fondo = document.getElementById("Oscuro");
  Fondo.style.display = "none";
  Fondo.style.visibility = "hidden";
  Fondo.onclick = function(){}
 
  var Rep = document.getElementById("RepVideo");
  Rep.innerHTML = "";
  Rep.style.display = "none";
  Rep.style.visibility = "hidden";
}

function VerVideo( Video ){
  var IE = navigator.appVersion.indexOf("MSIE") != -1;

  if ( myHeight == 0 ){ Medidas(); }
  
  ActivarFondo();
  var Desplazamiento = 0;
  if (IE){
    Desplazamiento = parseInt(document.documentElement.scrollTop,10);
  }else{
    Desplazamiento = parseInt(window.pageYOffset,10);
  }
   
  var Rep = document.getElementById("RepVideo");
  Rep.style.top = parseInt(Desplazamiento + myHeight/2 - 412/2,10) + "px";
  Rep.style.left = parseInt(myWidth/2 - 550/2,10) + "px";
  Rep.style.width = "550px";
  Rep.style.height = "412px";
  Rep.style.display = "block";
  Rep.style.visibility = "visible";

  var so = new SWFObject("Reproductor.swf", "ReoproductorFlash", "550px", "412px", "6", "#FFFFFF",false, "high");
  so.addParam("menu", "false");
  so.addParam("allowScriptAccess", "sameDomain");
  so.addVariable("srcVideo", Video);
  so.write("RepVideo");
}

function EnviarAmigo(){
  var f = document.getElementById("EnviarAmigo").style;
  var t = document.getElementById("Contenedor1").style;
  
  t.visibility = "hidden";
  t.display = "none";
  f.visibility = "visible";
  f.display = "block";
}

function REnviarAmigo(){
  var f = document.getElementById("EnviarAmigo").style;
  var r = document.getElementById("Respuesta").style;
  var fea = document.getElementById("FormEnviarAmigo").style;

  f.visibility = "visible";
  f.display = "block";
  fea.visibility = "visible";
  fea.display = "block";
  r.visibility = "hidden";
  r.display = "none";
}

function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();
    try{
      if (sMethod == "GET"){
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }else{
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete){
          bComplete = true;
          fnDone(xmlhttp);
        }
      };
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}


function EnviarFormularioAmigo(){
  var Nombre = document.getElementById("Nombre");
  var EMail = document.getElementById("EMail");
  var Apellidos = document.getElementById("Apellidos");
  var Comunidad = document.getElementById("Comunidad");
  var Err = false;
  var MsgErr = "";
  var Filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
  
  if (!Filter.test(EMail.value)){
    Err = true;
    MsgErr = "El correo electrónico no es válido"
  }  
  if ( EMail.value == "" ){
    Err = true;
    MsgErr = "Rellene el campo de Correo electrónico";
  }
  if ( Comunidad.value == "" ){
    Err = true;
    MsgErr = "Rellene el campo de Comunidad";
  }
  if ( Apellidos.value == "" ){
    Err = true;
    MsgErr = "Rellene el campo de Apellidos";
  }
  if ( Nombre.value == "" ){
    Err = true;
    MsgErr = "Rellene el campo de Nombre";
  }

  
  if (Err){
    alert(MsgErr);
  }else{
    var ajaxConn = new XHConn();
    var URLForm = "FormularioAmigo.asp";
    var Variables = "";
    var Metodo = "POST"; //"GET"
  
    Variables = "Nombre=" + Nombre.value;
    Variables = Variables + "&Apellidos=" + Apellidos.value;
    Variables = Variables + "&Comunidad=" + Comunidad.value;
    Variables = Variables + "&EMail=" + EMail.value;

    Nombre.value = "";
    EMail.value = "";
    Apellidos.value = "";
    Comunidad.value = "";
    ajaxConn.connect(URLForm,
                    Metodo,
                    Variables,
                    function(XML){
                      //alert(XML.responseText);
                      var f = document.getElementById("FormEnviarAmigo").style;
                      var r = document.getElementById("Respuesta").style;
  
                      f.visibility = "hidden";
                      f.display = "none";
                      r.visibility = "visible";
                      r.display = "block";
                    }
                    );
  }
}

function RestaurarFormularioAmigo(){
  document.getElementById("Nombre").value = "";
  document.getElementById("EMail").value = "";
  document.getElementById("Apellidos").value = "";
  document.getElementById("Comunidad").value = "";
}

function VerNotaLegal(){
  window.open("NotaLegal.html","Nota_Legal","width=400,height=647,left=0,top=0,toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no");
}