function ConfirmarBorrado(direccion,titular){
	if (confirm(titular)){
		window.location = direccion;
	}	
}

function abrir(theURL,winName,features) { 
  window.open(theURL,winName,features);
}

function cambioestado(ide,direccion){
	aux_estado=eval("document.form1.activo_"+ide+".checked");
	valor="0";
	if(aux_estado==true){
		valor="1";
	}
	document.location.href=direccion+'&id='+ide+"&estado="+valor;
}

function envioform(opcion,formulario){
	valoranyo=eval("document."+formulario+".anyo.value");
	//valornombre=eval("document."+formulario+".nombre.value");
	//if((valoranyo==false)||(valornombre==false)){
	if(valoranyo==false){
		alert("Es imperscindible seleccionar un año");
	}else{
		eval("document."+formulario+".opcion.value='"+opcion+"'");
		eval("document."+formulario+".submit()");
	}
}
function salto(campo,formul){
	//"salto('temporada','form3')"
	aux_valor=eval("document."+formul+"."+campo+".value");
	//if(aux_valor!="-1"){
		eval("document."+formul+".submit()");
	//}
}

function reordenar(valor,formul){
	eval("document."+formul+".idorden.value='"+valor+"'");
	eval("document."+formul+".submit()");
}

function enviartema(formul,tipo){
	//para poder detectar si sube algo o no y q no se ralle el xelupload
	if(tipo!=2){
		eval('document.'+formul+'.nombre_ip.value=document.'+formul+'.imagenp.value');
	}
	if((tipo==1)||(tipo==2)){
		eval('document.'+formul+'.nombre_doc.value=document.'+formul+'.documento.value');
	}
	eval('document.'+formul+'.submit()');
}

function asignarlinea(id,valor){
	alert(id+" - "+valor);
}

function irurl(direccion){
	document.location.href=direccion;
}

function envioconvalor(formulario,campo,valor){
	eval("document."+formulario+"."+campo+".value='"+valor+"'");
	eval("document."+formulario+".submit()");
}

function saltoConForm(form,dir,tampag,pagabs){
	eval("document."+form+".tamanopagina.value='"+tampag+"'");
	eval("document."+form+".paginaabsoluta.value='"+pagabs+"'");
	//alert(tampag+" - "+pagabs);
	eval("document."+form+".submit()");	
}

//***************************************************************
//VALIDACION DE FORMULARIOS
//***************************************************************
msje="Por favor, revise los errores en los siguientes campos:\n\n";
equivoc=0;

function emilio(contcorreo,nombrecampo){
        //compruebo el emilio
                var correo=contcorreo;
                var fallo_correo = 0;
                var pos = correo.indexOf("@");
                
                if (pos == -1){
                        equivoc=1;
                        fallo_correo = 1;
                }
        
        //compruebo q tenga al menos 1 carácter antes y después de le arroba y el punto
                if ((pos == 0)||(pos == correo.length-1)) {
                        equivoc=1;
                        fallo_correo = 1;
                }
                
                var pos2 = correo.lastIndexOf(".");
                if((pos2 < pos+1)||(pos2 == correo.length-1)){
                        equivoc=1;
                        fallo_correo = 1;        
                }
/*
                for (i=0; i<correo.length; i++){
                        if (correo.charCodeAt(i)==" ") {
                        equivoc=1;
                        fallo_correo = 1;
                        }
                }*/
                
                if (fallo_correo==1){
                        msje+="· El campo '"+nombrecampo+"' requiere una dirección de correo válida.\n";
                }
}

function validar_fecha(field){
	//compruebo la fecha mientras la mete el usuario
	var checkstr = "0123456789";
	var DateField = field;
	var Datevalue = "";
	var DateTemp = "";
	var seperator = "/";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;
   err = 0;
   DateValue = DateField.value;
   /* borra todos los caracteres excepto 0..9 */
   for (i = 0; i < DateValue.length; i++) {
          if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
             DateTemp = DateTemp + DateValue.substr(i,1);
          }
   }
   DateValue = DateTemp;
   /* Siempre cambio la fecha a 8 dígitos - cadena */
   /* si el año tiene 2 digitos/ se asume 20xx */
   if (DateValue.length == 6) {
      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   /* año incorrecto si es = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* valido mes*/
   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* valido día*/
   day = DateValue.substr(0,2);
   if (day < 1) {
     err = 22;
   }
   /* validación de año bisiesto */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* validacion de otros meses */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* Si se mete 00, directamente borro */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* si no hay error, lo meto en el campo */
   if (err == 0) {
      DateField.value = day + seperator + month + seperator + year;
   }
   /* Si el error es != 0 , mensaje */
   else {
      alert("La fecha es incorrecta");
      DateField.select();
          DateField.focus();
   }
}


function checkeacampo(tipo,campo,form){
        /*
        validacion de campos; "tipo" nos dice si es cadena, numero, fecha, dni,... Con "campo" le paso dos
        cosas: el nombre del campo y  si es o no obligatorio. por eso lo primero que hago es esplitear cada
        valor en la matriz "obligatorio"; la cadena que llega es así: "campo_n!1". 1 es obligatorio y 0, no. 
        Y finalmente, "form" es el nombre del formulario.
        */
        var obligatorio=new Array();
        obligatorio=campo.split("!");
        switch (tipo) {
                case 1:
                //numero
                        if (isNaN(eval("document."+form+"."+obligatorio[0]+".value"))) {
                                equivoc=1;
                                msje+="· El campo '"+obligatorio[0]+"' debe ser un número.\n";
                        }
                        if ( ((eval("document."+form+"."+obligatorio[0]+".value"))=="") && (obligatorio[1]==1) ) {
                                equivoc=1;
                                msje+="· El campo '"+obligatorio[0]+"' es obligatorio.\n";
                        }
                break;
                case 21:
                //numero DNI; quito la validacion de numero
                        /*if (isNaN(eval("document."+form+"."+obligatorio[0]+".value"))) {
                                equivoc=1;
                                msje+="· El campo '"+obligatorio[0]+"' debe ser un número.\n";
                        }*/
                        if ( ((eval("document."+form+"."+obligatorio[0]+".value"))=="") && (obligatorio[1]==1) ) {
                                equivoc=1;
                                msje+="· El campo '"+obligatorio[0]+"' es obligatorio.\n";
                        }
                break;
                case 4:
                //fecha corta
                        if ( ((eval("document."+form+"."+obligatorio[0]+".value"))=="") && (obligatorio[1]==1) ) {
                                equivoc=1;
                                msje+="· El campo '"+obligatorio[0]+"' es obligatorio.\n";
                        }
                break;
                case 20:
                //correo
                if (obligatorio[1]==1) {
                        emilio("'"+(eval("document."+form+"."+obligatorio[0]+".value"))+"'","'"+obligatorio[0]+"'");
                }else if ((eval("document."+form+"."+obligatorio[0]+".value"))!=""){
                        emilio("'"+(eval("document."+form+"."+obligatorio[0]+".value"))+"'","'"+obligatorio[0]+"'");
                }
                break;
                case 12:
                //editor
                break;
                default:
                //texto
					if ( ((eval("document."+form+"."+obligatorio[0]+".value"))=="") && (obligatorio[1]==1) ) {
							equivoc=1;
							msje+="· El campo '"+obligatorio[0]+"' es obligatorio.\n";
					}
        }
}
/*ejempo de cadena:
validar('Idestado_civil!0|10#numero_hijos!0|1#f_nac!1|4#email!0|20#email2!0|20#Idlugar_nacimiento!0|10#','formulario')
*/
function validar(valor,nombreform){

        var matriz_obligatorios=new Array();
        matriz_obligatorios=valor.split("#");
        var longitud=(matriz_obligatorios.length)-1;
        var matriztipo=new Array(2);
        var matrizcampos=new Array();
        
        for (i=0;i<longitud;i++){
                matriztipo=matriz_obligatorios[i].split("|");
                matrizcampos[i]=matriztipo;
        }
        
        for (i=0;i<longitud;i++){
                eval("checkeacampo("+matrizcampos[i][1]+",'"+matrizcampos[i][0]+"','"+nombreform+"')");
        }
        
        if (equivoc==0){
                eval("document."+nombreform+".submit()");
        }else{
                alert(msje);
                equivoc=0;
                msje="Por favor, revise los errores en los siguientes campos:\n\n";
        }
}


function hasFlash() {
	var hasFlash = false;
	try {
		var fo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
		if (fo) hasFlash = true;
	} catch (e) {
		if (navigator.mimeTypes["application/x-shockwave-flash"] != undefined) hasFlash = true;
	}
	return hasFlash;

}
//***************************************************************
//FINAL DE VALIDACION DE FORMULARIOS
//***************************************************************

//***************************************************************
//funciones varias del tejedor
//***************************************************************
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];}
}
