
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function MM_validateForm() { //v4.0

  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
	
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; 
	 
	  	if (nm=="empresa") nmtext="Empresa";
		else if (nm=="nif") nmtext="NIF/NIE";
		else if (nm=="nom") nmtext="Nombre y apellidos";
		else if (nm=="carrer") nmtext="Calle";
		else if (nm=="num") nmtext="Número";
		else if (nm=="poblacio") nmtext="Población";
		else if (nm=="cp") nmtext="Código postal";
		else if (nm=="provincia") nmtext="Provincia";
		else if (nm=="pais") nmtext="Pais";
		else if (nm=="telefon") nmtext="Teléfono";
		else if (nm=="fax") nmtext="Fax";
		else if (nm=="email") nmtext="Email";
		else if (nm=="controlCB1") nmtext="Actividad";
		//else if (nm=="V2") nmtext="Tipus d’entitat";

	
				
		
		
		else{ nmtext=nm.replace(/_db/gi, " ");nmtext=nmtext.replace(/_/gi, " ");}
		
		disa=val.disabled;
		len = document.getElementsByName(nm).length;
		chck=val.checked; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nmtext+' debe contener una dirección de correo electrónico válida.\n';
        } 
		else if (test.indexOf('isNif')!=-1) { 
			cnif=valida_nif_cif_nie(val) ;
			if (cnif != 1 && cnif != 2 && cnif != 3) errors+='- '+nmtext+' debe contenir un valor válido.\n';
        } 		
		else if (test.indexOf('checked') != -1){
		
	}else if (test.indexOf('radio') != -1){

		var radio_choice = false;
		var radios = document.getElementsByName(nm);
		

		// Loop from zero to the one minus the number of radio button selections
		for (counter = 0; counter < document.getElementsByName(nm).length; counter++)
		{
			
		// If a radio button has been selected it will return true
		// (If not it will return false)
		if (radios[counter].checked)
		radio_choice = true; 
		}

		
		if (!radio_choice){ if (disa == false){ errors += '- '+nmtext+' es obligatorio.\n'; }}


		//if (valButton(nm)){ if (disa == false){ errors += '- '+nm+' es obligatorio.\n'; }} 
		
	}else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nmtext+' ha de contenir un número.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nmtext+' must contain a number between '+min+' and '+max+'.\n';
      }else if (test.indexOf('morethan0') != -1){
		  	if (num>0){}
			else errors+='- Seleccione un valor para el campo '+nmtext+'.\n';
	  }
	  
	  } } else if (test.charAt(0) == 'R') if (disa == false){ errors += '- '+nmtext+' es obligatorio.\n'; }}
    } 
	
	

	if (errors) alert('Se han encontrado los siguientes errores:\n'+errors);
    document.MM_returnValue = (errors == '');
} }

function ferVisible(me,estat)
	{
			aux=document.getElementById(me);
			
			if(estat==1){
				aux.style.visibility="hidden";
				aux.style.display="none";	
			}else{
				aux.style.visibility="visible";
				aux.style.display="inline";
			}
	}



//Retorna: 1 = NIF ok, 2 = CIF ok, 3 = NIE ok, -1 = NIF error, -2 = CIF error, -3 = NIE error, 0 = ??? error
function valida_nif_cif_nie(a) 
{
	
	var temp=a.toUpperCase();
	var cadenadni="TRWAGMYFPDXBNJZSQVHLCKE";
 
	if (temp!==''){
		//si no tiene un formato valido devuelve error
		if ((!/^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$/.test(temp) && !/^[T]{1}[A-Z0-9]{8}$/.test(temp)) && !/^[0-9]{8}[A-Z]{1}$/.test(temp))
		{
			
			return 0;
		}
 
		//comprobacion de NIFs estandar
		if (/^[0-9]{8}[A-Z]{1}$/.test(temp))
		{
			posicion = a.substring(8,0) % 23;
			letra = cadenadni.charAt(posicion);
			var letradni=temp.charAt(8);
			if (letra == letradni)
			{
			   	return 1;
			}
			else
			{
				
				return -1;
			}
		}
 
		//algoritmo para comprobacion de codigos tipo CIF
		suma = parseInt(a[2])+parseInt(a[4])+parseInt(a[6]);
		for (i = 1; i < 8; i += 2)
		{
			temp1 = 2 * parseInt(a[i]);
			temp1 += '';
			temp1 = temp1.substring(0,1);
			temp2 = 2 * parseInt(a[i]);
			temp2 += '';
			temp2 = temp2.substring(1,2);
			if (temp2 == '')
			{
				temp2 = '0';
			}
 
			suma += (parseInt(temp1) + parseInt(temp2));
		}
		suma += '';
		n = 10 - parseInt(suma.substring(suma.length-1, suma.length));
 
		//comprobacion de NIFs especiales (se calculan como CIFs)
		if (/^[KLM]{1}/.test(temp))
		{
			if (a[8] == String.fromCharCode(64 + n))
			{
				return 1;
			}
			else
			{
				
				return -1;
			}
		}
 
		//comprobacion de CIFs
		if (/^[ABCDEFGHJNPQRSUVW]{1}/.test(temp))
		{
			temp = n + '';
			if (a[8] == String.fromCharCode(64 + n) || a[8] == parseInt(temp.substring(temp.length-1, temp.length)))
			{
				return 2;
			}
			else
			{
				
				return -2;
			}
		}
 
		//comprobacion de NIEs
		//T
		if (/^[T]{1}/.test(temp))
		{
			if (a[8] == /^[T]{1}[A-Z0-9]{8}$/.test(temp))
			{
				return 3;
			}
			else
			{
				
				return -3;
			}
		}
 
		//XYZ
		if (/^[XYZ]{1}/.test(temp))
		{
			pos = str_replace(['X', 'Y', 'Z'], ['0','1','2'], temp).substring(0, 8) % 23;
			if (a[8] == cadenadni.substring(pos, pos + 1))
			{
				return 3;
			}
			else
			{
				
				return -3;
			}
		}
	}
 
	return 0;
}

function fr(val,varc,form){
	
	checked = eval("document."+form+"."+val+".checked");
	value = parseInt(eval("document."+form+"."+varc+".value"));
	if (checked == true) value=value+1
	else value=value-1;
	eval("document."+form+"."+varc+".value="+value);

}  


function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
 
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
 
 
 function MM_openBrWindow2(theURL,winName,w,h) { //v2.0
 
   var availHeight = screen.availHeight;
   var availWidth = screen.availWidth;
 
   var x = 0, y = 0; 
   if (document.all) {
        x = window.screenTop; 
        y = window.screenLeft;
   }
   else if (document.layers) {  
        x = window.screenX;
        y = window.screenY;
   }
   
 
   t = (screen.height / 2) - (h / 2);
   l = (screen.width /2) - (w / 2);
 
 
   var arguments = 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,copyhistory=0,width='+w+',height='+h+',left='+l+',top='+t;
   var newWindow = window.open(theURL,'popup','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,copyhistory=0,width='+w+',height='+h+',left='+l+',top='+t);
 
   newWindow.focus();
}
