/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function cambia_video(i)
   {
    c ='<p class="descrip_video"><span class="titulo_video">';
    c+=videos_tit[i]+'</span><br/>';
    c+=videos_descrip[i]+'</p>';
    document.getElementById('texto_video').innerHTML=c;

    var s = new SWFObject('/resources/swf/mediaplayer.swf','single','270','212','7');
    s.addParam('allowfullscreen','true');
    s.addVariable('file',videos_flv[i]);
    s.addVariable('image',videos_foto[i]);
    s.addVariable('height','212');
    s.addVariable('width','270');
    s.addVariable('overstretch','true');
    s.addVariable('showicons','false');
    s.addVariable('javascriptid','v_player');
    s.addVariable('allowscriptaccess','always');
    s.addParam('wmode','transparent');
    s.write('v1');
   }

function cambia_noti(i,n)
{
 for (j=1;j<=n;j++)
     {
         if (j==i) document.getElementById('noti'+j).style.display='';
         else document.getElementById('noti'+j).style.display='none';
     }
}

function cambia_noti_all(i,n)
{
 for (j=1;j<=n;j++)
     {
         if (j==i) document.getElementById('noti'+j).style.display='';
         else document.getElementById('noti'+j).style.display='none';
     }
}

//FUNCIONES JAVASCRIPT COMUNES

function checkMail(correo)
{
	var x = correo;
    if (correo.length==0) return 0;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return 0;
	else return -1;
}

function check_cp(cp)
{
    if (cp.length!=5) return -1;
    if (!IsInt(cp,false)) return -1;
}

function IsInt( numstr, allowNegatives ) {
	// Return immediately if an invalid value was passed in
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")
		return false;

	// Default allowNegatives to true when undefined or null
	if (allowNegatives+"" == "undefined" || allowNegatives+"" == "null")
		allowNegatives = true;

	var isValid = true;

	// convert to a string for performing string comparisons.
	numstr += "";

	// Loop through string and test each character. If any
	// character is not a number, return a false result.
 	// Include special case for negative numbers (first char == '-').
	for (i = 0; i < numstr.length; i++) {
    	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || (numstr.charAt(i) == "-"))) {
       	isValid = false;
       	break;
		} else if ((numstr.charAt(i) == "-" && i != 0) ||
				(numstr.charAt(i) == "-" && !allowNegatives)) {
       	isValid = false;
       	break;
      }

   } // END for

   	return isValid;
}  // end IsInt


function is_numeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;


   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;

   }

   function NumberFormat(num, numDec, decSep, thousandSep){
    var arg;
    var Dec;
    Dec = Math.pow(10, numDec);
    if (typeof(num) == 'undefined') return;
    if (typeof(decSep) == 'undefined') decSep = ',';
    if (typeof(thousandSep) == 'undefined') thousandSep = '.';
    if (thousandSep == '.')
     arg=/./g;
    else
     if (thousandSep == ',') arg=/,/g;
    if (typeof(arg) != 'undefined') num = num.toString().replace(arg,'');
    num = num.toString().replace(/,/g, '.');
    if (isNaN(num)) num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * Dec + 0.50000000001);
    cents = num % Dec;
    num = Math.floor(num/Dec).toString();
    if (cents < (Dec / 10)) cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
     num = num.substring(0, num.length - (4 * i + 3)) + thousandSep + num.substring(num.length - (4 * i + 3));
    if (Dec == 1)
     return (((sign)? '': '-') + num);
    else
     return (((sign)? '': '-') + num + decSep + cents);
   }


function check_nif(nif) {
   if (nif.length==0) return -1;
   if (nif.length!=9) return -1;

   numero=nif.substring(0,8);
   aux=parseInt(numero);
   if (isNaN(aux)) {return -1;}
   letra=nif.substring(8,9);
   hashletras="TRWAGMYFPDXBNJZSQVHLCKE";
   var fin=numero % 23;
   var resul=hashletras.substring(fin,fin +1 );
   if (resul!=letra)
      {
       return -1;
      }
   return 0;
}

function check_nie(nif) {
   if (nif.length==0) return -2;
   if ((nif.substring(0,1)!="X") && (nif.substring(0,1)!="Y") && (nif.substring(0,1)!="Z")) return -1;
   if (nif.length!=9) return -1;

   if (nif.substring(0,1)=="X") numero="0"+nif.substring(1,8);
   if (nif.substring(0,1)=="Y") numero="1"+nif.substring(1,8);
   if (nif.substring(0,1)=="Z") numero="2"+nif.substring(1,8);
   aux=parseInt(numero);
   if (isNaN(aux)) {return -1;}
   letra=nif.substring(8,9);
   hashletras="TRWAGMYFPDXBNJZSQVHLCKE";

   var fin=numero % 23;
   var resul=hashletras.substring(fin,fin +1 );
   if (resul!=letra)
      {
       return -1;
      }
   return 0;
}

function check_pasaporte(nif) {
   if (nif.length==0) return -2;
   if (nif.substring(0,1)!="P") return -1;

   return 0;
}


function check_cif(numero) {
   var letrasposibles;
   var digitos;


   letrasposibles="ABCDEFGHKLMPQSXJUVRW";
   digitos="0123456789";
   abecedario="ABCDEFGHIJKLMNOPQRSTUWXYZ";
   pos0=numero.substr(0,1);
   if (digitos.indexOf(pos0) != -1) /* si la primera es un digito pasamos a comprobar el NIF*/
      {
      return check_nif(numero);
      }

   if (numero.substr(0,1)=="X") /* si la primera es una X comprobamos el NIE*/
      {
      return check_nie(numero);
      }

   if (numero.length != 9)
      {
      alert('Longitud de CIF incorrecta ');
      return -1;
      }
   if (letrasposibles.indexOf(pos0) == -1)
      {
      alert('La primera letra del CIF no pertenece a ningun tipo de empresa');
      return -1;
      }

   if (pos0=='X') /* si la primera letra es X, se quita y se valida NIF con el resto*/
      {
      return check_nif(numero);
      }
   pos1=numero.substr(1,1)*1;
   pos2=numero.substr(2,1)*1;
   pos3=numero.substr(3,1)*1;
   pos4=numero.substr(4,1)*1;
   pos5=numero.substr(5,1)*1;
   pos6=numero.substr(6,1)*1;
   pos7=numero.substr(7,1)*1;
   pos8=numero.substr(8,1);

   A=pos2+pos4+pos6;
   B1=pos1*2;
   if (B1 > 9) /*si son dos digitos se deben sumar*/
      B1=B1-9;
   B2=pos3*2;
   if (B2 > 9)
      B2=B2-9;
   B3=pos5*2;
   if (B3 > 9)
      B3=B3-9;
   B4=pos7*2;
   if (B4 > 9)
      B4=B4-9;

   B=B1+B2+B3+B4;
   aux=A+B;
   while (aux > 10) aux=aux-10;
   control=10-aux;

   if (digitos.indexOf(pos8) == -1) /*Si al final hay letra */
   {
      //parche extraño, si la letra debiese ser una A y es una J, tambien es correcto

      if ((abecedario.substr(control-1,1)=="A") && (pos8=="J"))
      {
       return 0;
      }

      if (abecedario.substr(control-1,1) != pos8)
      {
         alert('CIF INCORRECTO');
         return -1;
      }
   }
   else /* Si al final hay un numero*/
   {
      pos8=pos8*1; /*convertimos pos8 a numero entero*/
      if (control != pos8)
      {
        alert('CIF INCORRECTO');
        return -1;
      }
   }
   return 0
}

function check_nif_nie_pass(nif) {
   if (nif.length==0) return 0;
   if (nif.substring(0,1)=="P") return 0;
   if  (((nif.substring(0,1)=="X") || (nif.substring(0,1)=="Y") || (nif.substring(0,1)=="Z")) && (check_nie(nif)==-1))
     {
       alert("NIE incorrecto: "+nif);
//       document.f.nif.focus();
       return -1;
     }
   if  ((nif.substring(0,1)!="X") && (nif.substring(0,1)!="Y") && (nif.substring(0,1)!="Z") && (nif.substring(0,1)!="P"))
     if (check_nif(nif)==-1)
        {
          alert("NIF incorrecto: "+nif);
//          document.f.nif.focus();
          return -1;
        }
        return 0;
}

function check_nif_nie(nif) {
   if (nif.length==0) return 0;
   if  (((nif.substring(0,1)=="X") || (nif.substring(0,1)=="Y") || (nif.substring(0,1)=="Z")) && (check_nie(nif)==-1))
     {
       alert("NIE Incorrecto: "+nif);
//       document.f.nif.focus();
       return -1;
     }
   if  ((nif.substring(0,1)!="X") && (nif.substring(0,1)!="Y") && (nif.substring(0,1)!="Z") && (nif.substring(0,1)!="P"))
     if (check_nif(nif)==-1)
        {
          alert("NIF incorrecto: "+nif);
//          document.f.nif.focus();
          return -1;
        }
        return 0;
}


function check_cif_nie_pass(nif) {
   if (nif.length==0) return 0;
   if (nif.substring(0,1)=="P") return 0;
   if  (((nif.substring(0,1)=="X") || (nif.substring(0,1)=="Y") || (nif.substring(0,1)=="Z")) && (check_nie(nif)==-1))
     {
       alert("NIE incorrecto: "+nif);
       //document.f.nif.focus();
       return -1;
     }
   if  ((nif.substring(0,1)!="X") && (nif.substring(0,1)!="Y") && (nif.substring(0,1)!="Z") && (nif.substring(0,1)!="P"))
     if (check_cif(nif)==-1)
        {
          alert("CIF/NIF incorrecto");
          //document.f.nif.focus();
          return -1;
        }
   return 0;
}



function check_nss(numero)
   {
    if (numero.length==0) return -2;
    if ((numero.length!=13) && (numero.length!=14))
       {
        alert("Longitud de num S.S. Incorrecta");
//        document.f.nss.focus();
        return -1;
       }
    var aux_num=numero.replace("/","");
    /* excepcion de S.S. de personas si tercer digito es 0 */
    if ((numero.length=14) && (numero.substr(3,1)=="0"))
       aux_num=aux_num.substr(0,2)+aux_num.substr(3,10);

    cod=aux_num.substring(aux_num.length-2,aux_num.length);
    n=aux_num.substring(0,aux_num.length-3);

    res=n % 97;
    if (cod!=res)
      {
       alert("Num S.S. Incorrecto");
//       f.nss.focus();
       return -1;
      }
    return res;
   }

function check_ncc_v2(numero) {
   var entidad, sucursal, dc, cuenta;

   if (numero.length==0) return 0;
   if (numero.length!=23)
      {
       alert("Longitud incorrecta de cuenta");
       return -1;
      }

   if ((numero.substr(4,1)!="-") || (numero.substr(9,1)!="-") || (numero.substr(12,1)!="-") )
      {
       alert("Formato incorrecto de cuenta");
       return -1;
      }

    entidad=numero.substr(0,4);
    sucursal=numero.substr(5,4);
    dc=numero.substr(10,2);
    cuenta=numero.substr(13,10);

    return check_ncc(entidad, sucursal, dc, cuenta);
}

function check_ncc(entidad, sucursal, dc, cuenta) {
    var e1,e2,e3,e4,s1,s2,s3,s4,suma,resto,d1,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10;
    /*Validacion del primer digito de control*/
    s1=sucursal.substr(0,1)*1;
    s2=sucursal.substr(1,1)*1;
    s3=sucursal.substr(2,1)*1;
    s4=sucursal.substr(3,1)*1;
    e1=entidad.substr(0,1)*1;
    e2=entidad.substr(1,1)*1;
    e3=entidad.substr(2,1)*1;
    e4=entidad.substr(3,1)*1;
    dc1=dc.substr(0,1)*1;
    dc2=dc.substr(1,1)*1;

    suma=(s4*6)+(s3*3)+(s2*7)+(s1*9);
    suma=suma+(e4*10)+(e3*5)+(e2*8)+(e1*4);
    resto=suma % 11;
    d1=11-resto;
    if (d1==11) d1=0;
    if (d1==10) d1=1;
    if (d1!= dc1)
       {
        alert("Nº Entidad/Sucursal incorrecto ");
        return -1;
       }
    /*Validacion del segundo digito de control*/
    c1=cuenta.substr(0,1)*1;
    c2=cuenta.substr(1,1)*1;
    c3=cuenta.substr(2,1)*1;
    c4=cuenta.substr(3,1)*1;
    c5=cuenta.substr(4,1)*1;
    c6=cuenta.substr(5,1)*1;
    c7=cuenta.substr(6,1)*1;
    c8=cuenta.substr(7,1)*1;
    c9=cuenta.substr(8,1)*1;
    c10=cuenta.substr(9,1)*1;
    suma=c10*6+c9*3+c8*7+c7*9+c6*10+c5*5+c4*8+c3*4+c2*2+c1*1;
    resto=suma % 11;
    d2=11-resto;
    if (d2==11) d2=0;
    if (d2==10) d2=1;
    if (d2!= dc2)
       {
        alert("Nº de Cuenta incorrecto");
        return -1;
       }

}

function check_fecha(fecha)
{


        if (fecha.length !=10) return -1;

        var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

	var day = parseInt(fecha.substr(0,2));
	var month = parseInt(fecha.substr(3,2));
	var year = parseInt(fecha.substr(6,4));

	if (!day || !month || !year)
		return -1;

        if (day < 1) return -1;
        if ((month > 12) || (month < 1)) return -1;

	if (year/4 == parseInt(year/4))
		monthLength[1] = 29;

	if (day > monthLength[month-1])
		return -1;



	return 0;
}




