/*
 * Some useful JavaScript functions
 * (c) iSOCO Lab November 1999
 * Kurt Cabanes
 */
 
/*
 * Check if the given fields contains numbers and only numbers
 */

/*
 * Validacion para Campos del formulario de login2
 *
 */
function isReady1(form) {
 var kk= true;
 
 if (isFilled(form.email) == false) {
   alert("Por favor, se requiere que rellenes el campo Email");
   form.email.focus();
   return false;
 }
 if (isFilled(form.email2) == false) {
   alert("Por favor, se requiere que rellenes el campo Repite email");
   form.email2.focus();
   return false;
 }
 if (isFilled(form.password) == false) {
   alert("Por favor, se requiere que rellenes el campo Contraseña");
   form.password.focus();
   return false;
 }
 if (isFilled(form.password2) == false) {
   alert("Por favor, se requiere que rellenes el campo Repite contraseña");
   form.password2.focus();
   return false;
 }
 if ((isFilled(form.cif) == false) && (form.titulo.selectedIndex == "1")){
   alert("Por favor, se requiere que rellenes el campo NIF/CIF");
   form.cif.focus();
   return false;
 }
 if (isFilled(form.nombre) == false) {
   alert("Por favor, se requiere que rellenes el campo Nombre");
   form.nombre.focus();
   return false;
 }
 if (isFilled(form.apellidos) == false) {
   alert("Por favor, se requiere que rellenes el campo Apellidos");
   form.apellidos.focus();
   return false;
 }
 if (isFilled(form.direccion) == false) {
   alert("Por favor, se requiere que rellenes el campo Dirección");
   form.direccion.focus();
   return false;
 }
 if (isFilled(form.poblacion) == false) {
   alert("Por favor, se requiere que rellenes el campo Población");
   form.poblacion.focus();
   return false;
 }
 if (isFilled(form.codigopostal) == false) {
   alert("Por favor, se requiere que rellenes el campo Código postal");
   form.codigopostal.focus();
   return false;
 }
 var ceuta = form.codigopostal.value.substring(0,2);
 if ((ceuta.toString() == "51") || (ceuta.toString() == "52") ||
     (ceuta.toString() == "35") || (ceuta.toString() = "38")) {
	 if (isFilled(form.cif) == false) {
	   alert("Por favor, se requiere que rellenes el campo NIF/CIF");
	   form.cif.focus();
	   return false;
	 }
 }
 
 if (kk) kk= checkNumeric(form.telefono); 
 if (kk) kk= checkTextNumeric(form.codigopostal);
 if (kk) kk= checkEqual(form);
 return kk;

}

/*
 * Validacion para Campos del formulario de login2
 *
 */
function isReady4(form) {
 var kk= true;
 
 if (isFilled(form.password) == false) {
   alert("Por favor, se requiere que rellenes el campo Contraseña");
   form.password.focus();
   return false;
 }
 if (isFilled(form.password2) == false) {
   alert("Por favor, se requiere que rellenes el campo Repite contraseña");
   form.password2.focus();
   return false;
 }
 if ((isFilled(form.cif) == false) && (form.titulo.selectedIndex == "1")){
   alert("Por favor, se requiere que rellenes el campo NIF/CIF");
   form.cif.focus();
   return false;
 }
 if (isFilled(form.nombre) == false) {
   alert("Por favor, se requiere que rellenes el campo Nombre");
   form.nombre.focus();
   return false;
 }
 if (isFilled(form.direccion) == false) {
   alert("Por favor, se requiere que rellenes el campo Dirección");
   form.direccion.focus();
   return false;
 }
 if (isFilled(form.poblacion) == false) {
   alert("Por favor, se requiere que rellenes el campo Población");
   form.poblacion.focus();
   return false;
 }
 if (isFilled(form.codigopostal) == false) {
   alert("Por favor, se requiere que rellenes el campo Código postal");
   form.codigopostal.focus();
   return false;
 }
 var ceuta = form.codigopostal.value.substring(0,2);
 if ((ceuta.toString() == "51") || (ceuta.toString() == "52") ||
     (ceuta.toString() == "35") || (ceuta.toString() = "38")) {
		 if (isFilled(form.cif) == false) {
		   alert("Por favor, se requiere que rellenes el campo NIF/CIF");
		   form.cif.focus();
		   return false;
	 }
 } 
 
 if (kk) kk= checkNumeric(form.telefono); 
 if (kk) kk= checkTextNumeric(form.codigopostal);
 if (kk) kk= checkEqual(form);
 return kk;

}


// Dreamweaver  
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_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_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];}
}
 
/*
 * Check if the required fields of a form are filled. The required fields are
 * defined as an array list passed as argument.
 */  
function checkRequired(daForm, daRequiredList) {
    var pass=true;
    if (document.images) {
    	var i=0;
	var tempobj= daForm.elements[daRequiredList[0]];
	while (tempobj!="undefined") {
	    if (tempobj.type.toString()=="text") 
	    	tempobj.value= stripSpaces(tempobj.value);
	    if ((tempobj.value=='')|| (tempobj.type.toString().charAt(0)=="s" &&
                tempobj.selectedIndex==0)) {
                pass=false;
		tempobj.focus();
		tempobj.select();
		alert("Se requiere que rellenes el campo "+tempobj.name);
                break;
               
           }
  	   tempobj=daForm.elements[daRequiredList[++i]];
       }
   }
   return pass;
}


/* 
 * Checks if ALL fields of a given form are filled 
 */
function checkAllRequired(daForm) {
    var req= new Object;
    var i=0;
    while (daForm.elements[i]!=undefined) {
    	req[i]= daForm.elements[i].name;
	i++;
    }
    return checkRequired(daForm, req);
}

/*
 * Strips all non alphanumeric characters from a string
 */
function stripNonAlphaNum(field) {
    stripped="";
    alphabet=" -abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789????????????????????????????????????????????";
    for (var i=0; i<field.value.length; i++) {
    	ch= field.value.charAt(i);
	if (alphabet.indexOf(ch) != "-1")
	    stripped= stripped+ch;
    }
    field.value= stripped;
} 

function elimAccents(field) {
    stripped="";
    accented="????????????????????????????????????????";
    nonaccented="aeiouAEIOUaeiouAEIOIaeiouAEIOUaeiouAEIOU";
    for (var i=0; i<field.value.length; i++) {
    	ch= field.value.charAt(i);
	if (accented.indexOf(ch) != "-1") {
	    stripped= stripped+nonaccented.charAt(accented.indexOf(ch));
        } 
	else {
	    stripped= stripped+ch;
        }
    }
    field.value= stripped;
    
}

/*
 * Hides a layer
 */
function hide(name) {
    ns = (document.layers) ? 1:0
    ie = (document.all) ? 1:0
    if (ns) {
    	evalStr= 'parent.document.layers["'+name+'"]';
	laya= eval(evalStr);
	laya.visibility='hide';    
    } else {
        laya= eval("document.all."+name+".style");
	laya.visibility="hidden";    
    }
}

/*
 * Shows a layer
 */
function show(name) {
    ns = (document.layers) ? 1:0
    ie = (document.all) ? 1:0
    if (ns) {
	laya= eval('parent.document.layers["'+name+'"]');
	laya.visibility='show';  
    } else {
        laya= eval("document.all."+name+".style");
	laya.visibility="visible";    
    }
}

/*
 * Validacion para Campos del formulario getadress 
 */
function isReady2(form) {
  var kk= true;
  if (isFilled(form.nombre) == false) {
     alert("Por favor, se requiere que rellenes el campo Nombre");
     form.direccion.focus();
     return false;
  }
  if (isFilled(form.direccion) == false) {
     alert("Por favor, se requiere que rellenes el campo Dirección");
     form.direccion.focus();
     return false;
  }
  if (isFilled(form.poblacion) == false) {
     alert("Por favor, se requiere que rellenes el campo Población");
     form.poblacion.focus();
     return false;
  }
  if (isFilled(form.codigopostal) == false) {
     alert("Por favor, se requiere que rellenes el campo Código postal");
     form.codigopostal.focus();
     return false;
  }
  var ceuta = form.codigopostal.value.substring(0,2);
  if ((ceuta.toString() == "51") || (ceuta.toString() == "52") ||
     (ceuta.toString() == "35") || (ceuta.toString() = "38")) {
		 if (isFilled(form.cif) == false) {
		   alert("Por favor, se requiere que rellenes el campo NIF/CIF");
		   form.cif.focus();
		   return false;
	   }
  }   
  if (kk) kk= checkTextNumeric(form.codigopostal);
  if (kk) kk= checkNumeric(form.telefono);
  return kk;
} 
 
/*
 * Validacion para campos del formulario getvisa
 */
function isReady3(form) {
  var kk=true;
  if (isFilled(form.visa) == false) {
     alert("Por favor, se requiere que rellenes el campo del Número de tarjeta");
     form.visa.focus();
     return false;
  }

 if (isFilled(form.cvc) == false) {
    alert("Por favor, se requiere que rellenes el campo CVC");
    form.cvc.focus();
    return false;		
 }

 if (kk) kk= checkNumeric(form.cvc);
 if (kk) kk= checkNumeric(form.visa);
  
  if (form.visa.value.substring(0,2)=="37"){
 	if (kk) kk= amexlength(form);
	if (kk) kk= cvcamexlength(form);  	
  }else{
 	if (kk) kk= visalength(form);
	if (kk) kk= cvclength(form);  	
  }
 
 return kk; 
} 
 
function cvclength(form){
 if (form.cvc.value.length!=3) {
 alert("El código CVC introducido es incorrecto, debe contener 3 cifras");
 return false;
 }
 return true;
}

function amexlength(form){
 if (form.visa.value.length!=15) {
 alert("El número de tarjeta introducido es incorrecto, debe contener 15 cifras");
 return false;
 }
 return true;
}

function cvcamexlength(form){
 if (form.cvc.value.length!=4) {
 alert("El código CVC introducido es incorrecto, debe contener 4 cifras");
 return false;
 }
 return true;
} 
 
/*
 * Esta relleno 
 *
 */
function isFilled(elm) {
 if (elm.value =="" || elm.value ==null)
   return false;
 else
   return true;
}
/*
 * Campos de login2 de password y emails iguales.
 */
function checkEqual(form) {
 if (form.password.value!=form.password2.value) {
   alert("La clave introducida en los campos de contraseñas no se corresponden");
   return false;
 }
 if (form.email.value!=form.email2.value) {
   alert("El email introducido en los campos de email no se corresponden");
   return false;
 }
 return true; 
}

function visalength(form){
 if (form.visa.value.length!=16) {
 alert("El número de tarjeta introducido es incorrecto, debe contener 16 cifras");
 return false;
 }
 return true;
} 
 
/*
 * Basic checks to ensure that an email address is formed in the right way
 */
function checkEmail(field) {
    if (field.value.indexOf("@")<2) {
    	alert("La dirección de correo introducida en el campo "+field.name+
	      "parece estar incorrectamente formada. ¿Lleva la arroba?");	
	field.focus();
	field.select();
	return false;
    }
    if (field.value.indexOf(".")<0) {
    	alert("La dirección de correo introducida en el campo "+field.name+
	      "parece estar formada incorrectamente. Compruebe el dominio del email, por favor");
	field.focus();
	field.select();
	return false;
    }
   return true;
}

function stripSpaces(daString) {
    i=0;
    while (daString.charAt(i)==' ')
    	i++;
    j=daString.length-1;
    while (daString.charAt(j)==' ')
    	j--;
    if (i>j)
    	return '';
    return daString.substring(i,j+1);
} 
 
/*
 * Checks if the given field contains letters and numbers only 
 */
function checkTextNumeric(field) {
    if(checkFld(field, " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789????????????????????????????????????????????")==false) {
    	alert("Sólo se permiten letras y números en el campo "+field.name);
	return false;
    }
    return true;
}
/*
 * Checks if the given field contains letters and letters only
 */
function checkTextual(field) {
   if(checkFld(field, "abcdefghijklmnopqrstuvwxyz")==false) {
        alert("Sólo se permiten letras en el campo "+field.name);
        return false;
    }
    return true;
}

/*
 * Checks if the given field contains letters and letters only
 */
function checkNumeric(field) {
   if(checkFld(field, "0123456789")==false) {
        alert("Sólo se permiten números en el campo "+field.name);
        return false;
    }
    return true;
}

/*
 * Checks if the given field is formed only by the characters contained in 
 * the string of valid characters
 */
function checkFld(field, valid) {
    var ok = true;
    var temp;
    for (var i=0; i<field.value.length; i++) {
        temp = "" + field.value.substring(i, i+1);
        if (valid.indexOf(temp) == "-1") ok =false;
    }
    if (ok == false) {
        field.focus();
        field.select();
    }
    return ok;
}

/*
 * Obtener la fecha actual
 */
function getDate() {
    var Days = new Array('Domingo','Lunes','Martes','Miércoles', 'Jueves','Viernes','Sábado');
    var Months = new Array('enero','febrero','marzo','abril','mayo','junio','julio','agosto','septiembre','octubre','noviembre','diciembre');
    var today = new Date();           
    var tsem = Days[today.getDay()];
    var tyear = today.getFullYear();
    var tday = today.getDate();
    var tmonth = Months[today.getMonth()];
    document.writeln(tsem+', '+tday+' de '+tmonth+' de '+tyear);        
}

function switchMenu(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != "none" ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

function switchBetweenMenu(obj1,obj2) {
	var el = document.getElementById(obj1);
	var el2 = document.getElementById(obj2);
	if ( el.style.display != "none" ) {
		el.style.display = 'none';
		el2.style.display = '';
	}
	else {
		el.style.display = '';
		el2.style.display = 'none';
	}
}


function resizeImage(i,percent) {
 var f =0;
 f = i.height*percent;
 i.height=Math.round(f);
}

var iportadaspercent = 100;

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
