/* 
------------------------------------------
    Flipbox written by CrappoMan
    simonpatterson@dsl.pipex.com
------------------------------------------
*/
function flipBox(who) {
    var tmp; 
    if (document.images['b_' + who].src.indexOf('_on') == -1) { 
        tmp = document.images['b_' + who].src.replace('_off', '_on');
        document.getElementById('box_' + who).style.display = 'none';
        document.images['b_' + who].src = tmp;
    } else { 
        tmp = document.images['b_' + who].src.replace('_on', '_off');
        document.getElementById('box_' + who).style.display = 'block';
        document.images['b_' + who].src = tmp;
    } 
}

function addText(elname, wrap1, wrap2) {
    if (document.selection) { // for IE 
        var str = document.selection.createRange().text;
        document.forms['inputform'].elements[elname].focus();
        var sel = document.selection.createRange();
        sel.text = wrap1 + str + wrap2;
        return;
    } else if ((typeof document.forms['inputform'].elements[elname].selectionStart) != 'undefined') { // for Mozilla
        var txtarea = document.forms['inputform'].elements[elname];
        var selLength = txtarea.textLength;
        var selStart = txtarea.selectionStart;
        var selEnd = txtarea.selectionEnd;
        var oldScrollTop = txtarea.scrollTop;
        //if (selEnd == 1 || selEnd == 2)
        //selEnd = selLength;
        var s1 = (txtarea.value).substring(0,selStart);
        var s2 = (txtarea.value).substring(selStart, selEnd)
        var s3 = (txtarea.value).substring(selEnd, selLength);
        txtarea.value = s1 + wrap1 + s2 + wrap2 + s3;
        txtarea.selectionStart = s1.length;
        txtarea.selectionEnd = s1.length + s2.length + wrap1.length + wrap2.length;
        txtarea.scrollTop = oldScrollTop;
        txtarea.focus();
        return;
    } else {
        insertText(elname, wrap1 + wrap2);
    }
}

function insertText(elname, what) {
    if (document.forms['inputform'].elements[elname].createTextRange) {
        document.forms['inputform'].elements[elname].focus();
        document.selection.createRange().duplicate().text = what;
    } else if ((typeof document.forms['inputform'].elements[elname].selectionStart) != 'undefined') { // for Mozilla
        var tarea = document.forms['inputform'].elements[elname];
        var selEnd = tarea.selectionEnd;
        var txtLen = tarea.value.length;
        var txtbefore = tarea.value.substring(0,selEnd);
        var txtafter =  tarea.value.substring(selEnd, txtLen);
        var oldScrollTop = tarea.scrollTop;
        tarea.value = txtbefore + what + txtafter;
        tarea.selectionStart = txtbefore.length + what.length;
        tarea.selectionEnd = txtbefore.length + what.length;
        tarea.scrollTop = oldScrollTop;
        tarea.focus();
    } else {
        document.forms['inputform'].elements[elname].value += what;
        document.forms['inputform'].elements[elname].focus();
    }
}

function show_hide(msg_id) {
    msg_id.style.display = msg_id.style.display == 'none' ? 'block' : 'none';
}



  var isIE = document.all?true:false;
  var isNS = document.layers?true:false;

        function openPopup(url) {
      var ancho = 580;
          var alto = 332;
      var left = (screen.width - ancho) / 2;
          var top =  (screen.height - alto) / 2;
    window.open(url,"_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, left="+left+", top="+top+", width="+ancho+", height="+alto);
  }


  function onlyDigits(e, decReq)
  {
    var key = (isIE) ? window.event.keyCode : e.which;
    var obj = (isIE) ? event.srcElement : e.target;

    var isNum = (key > 47 && key < 58) ? true:false;

    var dotOK = (key==46 && decReq=='decOK' && (obj.value.indexOf(".")<0 || obj.value.length==0)) ? true:false;

    window.event.keyCode = (!isNum && !dotOK && isIE) ? 0:key;

    e.which = (!isNum && !dotOK && isNS) ? 0:key;

    return (isNum || dotOK);
  }


  function getAnio(incremento)
  {
    fechaActual = new Date();

    return fechaActual.getYear() + incremento;
  }

  function submitform()
  {
    document.formulario.idFiscal.value = document.formulario.idFiscal.value.toUpperCase();

    if (document.formulario.tipoId.selectedIndex == 0)
    {
      if (document.formulario.idFiscal.value == "" )
      {
        alert("Por favor, introduce tu " + document.formulario.tipoId.options[document.formulario.tipoId.selectedIndex].text + " para poder registrarte en Canal Cliente.");
        document.formulario.idFiscal.focus();
        return;
      }
      else if (validarNIF(document.formulario.idFiscal) == false)
      {
        alert("El formato de NIF es incorrecto. Por favor, introdúcelo de nuevo.");
        document.formulario.idFiscal.focus();
        return;
      }

    }
    else if (document.formulario.tipoId.selectedIndex != 0)
    {
      if (document.formulario.idFiscal.value == "")
      {
        alert("Por favor, introduce tu " + document.formulario.tipoId.options[document.formulario.tipoId.selectedIndex].text + " para poder registrarte en Canal Cliente.");
        document.formulario.idFiscal.focus();
        return;
      }
    }

    if (document.formulario.numeroMovil.value == "")
    {
      alert("Por favor, introduce el número de móvil para enviarte la clave.");
      document.formulario.numeroMovil.focus();
      return;
    }
    else if (document.formulario.numeroMovil.value.length < 9 || isNaN(document.formulario.numeroMovil.value))
    {
      alert("El formato del telefono no es correcto. Por favor, inténtalo de nuevo.");
      document.formulario.numeroMovil.focus();
      return;
    }

    if (document.formulario.formasPago.selectedIndex == 0)
    {
      alert("Por favor, selecciona la forma de pago asociada a la factura.");
      document.formulario.formasPago.focus();
      return;
    }
    else if (document.formulario.formasPago.selectedIndex == 1)
    {
      if (document.formulario.entidad.value == ""
            || document.formulario.sucursal.value == ""
            || document.formulario.digito.value == ""
            || document.formulario.cuenta.value == "")
      {
        alert("Por favor, introduce tus datos bancarios para poder registrarte en el Canal Cliente.");
        document.formulario.entidad.focus();
        return;
      }
      else if (document.formulario.entidad.value.length < 4 || isNaN(document.formulario.entidad.value)
            || document.formulario.sucursal.value.length < 4 || isNaN(document.formulario.sucursal.value)
            || document.formulario.digito.value.length < 2
            || document.formulario.cuenta.value.length < 10 || isNaN(document.formulario.cuenta.value))
      {
        alert("Los datos bancarios son incorrectos. Por favor, introdúcelos de nuevo.");
        document.formulario.entidad.focus();
        return;
      }
    }
    else if (document.formulario.formasPago.selectedIndex == 2)
    {
      if (document.formulario.tarjeta1.value == ""
            || document.formulario.tarjeta2.value == ""
            || document.formulario.tarjeta3.value == ""
            || document.formulario.tarjeta4.value == "")
      {
        alert("Por favor, introduce los números de tu tarjeta para poder registrarte en el Canal Cliente.");
        document.formulario.tarjeta1.focus();
        return;
      }
      else if (document.formulario.tarjeta1.value.length < 4 || isNaN(document.formulario.tarjeta1.value)
            || document.formulario.tarjeta2.value.length < 4 || isNaN(document.formulario.tarjeta2.value)
            || document.formulario.tarjeta3.value.length < 4 || isNaN(document.formulario.tarjeta3.value)
            || document.formulario.tarjeta4.value.length < 4 || isNaN(document.formulario.tarjeta4.value))
      {
        alert("El número de tarjeta es incorrecto. Por favor, introdúcelo de nuevo.");
        document.formulario.tarjeta1.focus();
        return;
      }

      if (document.formulario.mes.selectedIndex == 0 || document.formulario.anio.selectedIndex == 0)
      {
        alert("Por favor, introduce la fecha de caducidad de tu tarjeta para poder registrarte en el Canal Cliente.");
        document.formulario.mes.focus();
        return;
      }
      else
      {
        fechaActual = new Date();

        if ((document.formulario.anio.options[document.formulario.anio.selectedIndex].value == fechaActual.getYear()
              && document.formulario.mes.options[document.formulario.mes.selectedIndex].value <= (fechaActual.getMonth() + 1))
            || document.formulario.anio.options[document.formulario.anio.selectedIndex].value < fechaActual.getYear())
        {
          alert("La fecha de caducidad de tu tarjeta de crédito es incorrecta. Por favor, introdúcela de nuevo.");
          document.formulario.mes.focus();
          return;
        }
      }
    }
    else
    {
      if (document.formulario.factura1.value == ""
            || document.formulario.factura2.value == ""
            || document.formulario.factura3.value == "")
      {
        alert("Por favor introduce el número de la última factura para poder registrarte en Canal Cliente.");
        document.formulario.factura1.focus();
        return;
      }
      else if (document.formulario.factura1.value.length < 2 || isNaN(document.formulario.factura1.value)
                || document.formulario.factura2.value.length < 4
                || document.formulario.factura3.value.length < 6 || isNaN(document.formulario.factura3.value))
      {
        alert("El formato de número de factura es incorrecto. Por favor, introdúcelo de nuevo.");
        document.formulario.factura1.focus();
        return;
      }
    }

    document.formulario.groupId.value = '1349';
    MM_openBrWindow('/fwk/cda/controller/CCLI_CW_publico/0,2214,259__2425__,00.html','legal','scrollbars=yes,resizable=yes','580','332','true');

    // El submit es llamado desde el popup de lopd.
  }

  function completaNif(cNif)
  {
    var longitud = cNif.value.length;
    var ceros = "000000000"

    if (longitud < 9)
    {
      cNif.value = ceros.substring(0, 9 - longitud) + cNif.value;
    }

    return cNif.value;
  }

  function validarNIF(cNif)
  {
    var nif = completaNif(cNif);
    cNif.value = nif.toUpperCase();

    dni = nif.substring(0, nif.length - 1);
    letraNif = nif.charAt(nif.length - 1);

    if (!isNaN(letraNif))
     {
      return false;
     }
    else
     {
      cadena = "TRWAGMYFPDXBNJZSQVHLCKET";
      posicion = dni % 23;
      letra = cadena.substring(posicion, posicion + 1);

      if (letra != letraNif.toUpperCase())
       {
        return false;
       }
     }

    return true;
  }

  function limpiaFormaPago()
  {
    document.formulario.entidad.value = "";
    document.formulario.sucursal.value = "";
    document.formulario.digito.value = "";
    document.formulario.cuenta.value = "";

    document.formulario.tarjeta1.value = "";
    document.formulario.tarjeta2.value = "";
    document.formulario.tarjeta3.value = "";
    document.formulario.tarjeta4.value = "";

    document.formulario.mes.selectedIndex = 0;
    document.formulario.anio.selectedIndex = 0;

    document.formulario.factura1.value = "";
    document.formulario.factura2.value = "";
    document.formulario.factura3.value = "";
  }


  function comboChg2(selecc,limpiar)
  {
    if (selecc==0)
    {
    document.getElementById("metodopago1").style.display="none";
      document.getElementById("metodopago11").style.display="none";
      document.getElementById("metodopago111").style.display="none";
      document.getElementById("metodopago1111").style.display="none";
      document.getElementById("metodopago11111").style.display="none";

      return;
  }
  else
  {
    if(limpiar == 0){
      limpiaFormaPago();
    }
  }

  if(selecc>0)
  {
    selecc--;
  }

    for (ii=0;ii<3;ii++)
    {
      vista = (ii==selecc) ? "inline" : "none";
      document.getElementById("metodopago"+(ii+1)).style.display=vista;
      document.getElementById("metodopago"+(ii+1)+(ii+1)).style.display=vista;

      if(ii==0)
      {
          document.getElementById("metodopago"+(ii+1)+(ii+1)+(ii+1)).style.display=vista;
        document.getElementById("metodopago"+(ii+1)+(ii+1)+(ii+1)+(ii+1)).style.display=vista;
        document.getElementById("metodopago"+(ii+1)+(ii+1)+(ii+1)+(ii+1)+(ii+1)).style.display=vista;
      }
    }

    foco_a= (selecc==0) ? 1 : (selecc==1) ? 5 : 9;
  }
function openCalendar(params, form, field, type) {
    window.open("./calendar.php?" + params, "calendar", "width=400,height=200,status=yes");
    dateField = eval("document." + form + "." + field);
    dateType = type;
}


function menu(obj, estilo){obj.style.cursor = 'hand'; obj.className=estilo;}
function menuentra(src) {src.bgColor='#FFFFFF';src.style.cursor='hand';}
function menuentra2(src) {src.bgColor='#cfdeef';src.style.cursor='hand';}
function menuentra3(src) {src.bgColor='#c0d3e9';src.style.cursor='hand';}
function menuentra4(src) {src.bgColor='#f0f5fb';src.style.cursor='hand';}
function menusale(src)  {src.bgColor='';}


