var Ventana2;

function openIT(theURL,W,H, wname,S) {
  var prop;
  
  if (S!='no') S="yes"
  
  prop = "width="+W+",height="+H+",scrollbars="+S;
  
  
if (Ventana2) {
    if (Ventana2.closed) {
      Ventana2=open(theURL,wname,prop+",location=no,status=no,directories=no,toolbar=no,resizable=no,menubar=no");
      Ventana2.focus();
    }
    else {
      Ventana2=open(theURL,wname,prop+",location=no,status=no,directories=no,toolbar=no,resizable=no,menubar=no");
      Ventana2.focus();
    }
  }
  else {
      Ventana2=open(theURL,wname,prop+",location=no,status=no,directories=no,toolbar=no,resizable=no,menubar=no");          
      Ventana2.focus();
  }
}

function lanzaMensaje(msg) {
  alert (msg);
}



function abrirVentana(URL,name,prop) {
  if (Ventana2) {
    if (Ventana2.closed) {
      Ventana2=open(URL,name,prop+",location=no,status=no,directories=no,toolbar=no,resizable=no,menubar=no");
      Ventana2.focus();
    }
    else {
      Ventana2=open(URL,name,prop+",location=no,status=no,directories=no,toolbar=no,resizable=no,menubar=no");
      Ventana2.focus();
    }
  }
  else {
      Ventana2=open(URL,name,prop+",location=no,status=no,directories=no,toolbar=no,resizable=no,menubar=no");          
      Ventana2.focus();
  }
}


// Copia el valor de un desplegable a un campo de texto de un mismo formulario.
function copia(f,ori,des) {
  var sel = 0;
  eval('sel=f.'+ori+'.options.selectedIndex');
  if (sel!=0) {
	  eval('f.'+des+'.value=f.'+ori+'.options[sel].text');
	}
}


function esVacio(s) 
{ 
    for(var i = 0; i < s.length; i++) { 
        var c = s.charAt(i); 
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false; 
    } 
    return true; 
} 
 
function tieneComilla(s) 
{ 
    for(var i = 0; i < s.length; i++) { 
        var c = s.charAt(i); 
        if (c == '\'') return true; 
    } 
    return false; 
}

function validaFecha(f) {
    dia=Number(f.substring(0,2)); 
    mes=Number(f.substring(3,5)); 
    ano=Number(f.substring(6,10));
    
    sep1=f.charAt(2); 
    sep2=f.charAt(5); 
 
    if ((isNaN(dia) || isNaN(mes) || isNaN(ano)) || 
        (sep1!='/' || sep2!='/') ||   
        !((dia>=1 && dia<=31) && (mes>=1 && mes<=12)) || (f.length!=10)) return false; 
 
    return true; 
}


function enviar(f) {
  errors = false;
  msg = 'Se han encontrado los siguientes\nerrores en el formulario:\n\n';
  for (i=0; i < f.length; i++) {
    e = f.elements[i];
    
    if ((e.type=="select-one") || (e.type=="select-multiple")) {
      if ((e.value=="-1" || e.value=="") && (e.optional=="false")) {
        msg += '-- '+e.label+' es un campo obligatorio.\n';
        errors = true;
        continue;
      }
    }
    
    if ((e.type=="text") || (e.type == "textarea")) {
      if (((e.value == null) ||(e.value == "") || esVacio(e.value)) && (e.optional=="false")) {
        msg += '-- '+e.label+' es un campo obligatorio.\n';
        errors = true;
        continue;
      }
      
      if (e.fecha == "true" && !validaFecha(e.value)) {
        if (e.value != "") {
          msg += '-- Formato de fecha erróneo.\n';
          errors = true;
          continue;
        }
      }       
      
      if (tieneComilla(e.value)) {
        msg += '-- '+e.label+' tiene un carácter no válido.\n';
        errors = true;
        continue;
      }
    }
  }
  
  if (!errors) {
    if (confirm('Se dispone a '+f.label+' registro/s.\n\n¿Desea continuar?')) f.submit();
  }
  else alert(msg);
    
}

function enviar2(f) {
  errors = false;
  msg = 'Se han encontrado los siguientes\nerrores en el formulario:\n\n';
  for (i=0; i < f.length; i++) {
    e = f.elements[i];
    
    if ((e.type=="select-one") || (e.type=="select-multiple")) {
      if ((e.value=="-1" || e.value=="") && (e.optional=="false")) {
        msg += '-- '+e.label+' es un campo obligatorio.\n';
        errors = true;
        continue;
      }
    }
    
    if ((e.type=="text") || (e.type == "textarea")) {
      if (((e.value == null) ||(e.value == "") || esVacio(e.value)) && (e.optional=="false")) {
        msg += '-- '+e.label+' es un campo obligatorio.\n';
        errors = true;
        continue;
      }
      
      if (e.fecha == "true" && !validaFecha(e.value)) {
        if (e.value != "") {
          msg += '-- Formato de fecha erróneo.\n';
          errors = true;
          continue;
        }
      }       
      
      if (tieneComilla(e.value)) {
        msg += '-- '+e.label+' tiene un carácter no válido.\n';
        errors = true;
        continue;
      }
    }
  }
  
  if (!errors) {
    f.submit();
  }
  else alert(msg);
    
}

function busca(f) {
  errors = false;
  msg = 'Se han encontrado los siguientes\nerrores en el formulario:\n\n';
  for (i=0; i < f.length; i++) {
    e = f.elements[i];
    
    if ((e.type=="select-one") || (e.type=="select-multiple")) {
      if ((e.value=="-1" || e.value=="") && (e.optional=="false")) {
        msg += '-- '+e.label+' es un campo obligatorio.\n';
        errors = true;
        continue;
      }
    }
    
    if ((e.type=="text") || (e.type == "textarea")) {
      if (((e.value == null) ||(e.value == "") || esVacio(e.value)) && (e.optional=="false")) {
        msg += '-- '+e.label+' es un campo obligatorio.\n';
        errors = true;
        continue;
      }
      
      if (e.fecha == "true" && !validaFecha(e.value)) {
        if (e.value != "") {
          msg += '-- Formato de fecha erróneo.\n';
          errors = true;
          continue;
        }
      }
      
      if (tieneComilla(e.value)) {
        msg += '-- '+e.label+' tiene un carácter no válido.\n';
        errors = true;
        continue;
      }
    }
  }
  
  if (!errors) {
    f.submit();
  }
  else alert(msg);
    
}

function confirma(URL,op) {
  if (confirm('Se dispone a '+op+' registro/s.\n\n¿Desea continuar?')) document.location=URL;
}

function recarga(f,URL) {
  f.action = URL;
  f.submit();
}

function recarga2(f,URL) {
  f.action = URL;
  enviar(f);
}


function checkall(f) {
  for (i=0; i < f.length; i++) {
    e = f.elements[i];
    e.checked = true;
  }
}

function convert(what) {
  val = eval('document.busc.'+what+'.value');
  if (val!="") {
    val = Math.ceil(val * 166.386);
    if (isNaN(val)) 
      val = '  Error en formato';
    else   
      val = '   '+val+' ptas';
    document.forms[what].texto.value = val;
    return true;
  }
  return false;
  
}


function MM_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); return x;
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

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_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 printit(){  
if (NS) {
    window.print() ;  
} else {
    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    WebBrowser1.ExecWB(6, 2);
WebBrowser1.outerHTML = "";  
}
}

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];}
}

function emutation_ToObject(id) {
		if(document.layers){
			return (document.layers[id])?eval(document.layers[id]):null;
		}
		else if(document.all && !document.getElementById){
			return (eval("window."+id))?eval("window."+id):null;
		}
		else if(document.getElementById && document.body.style) {
			return (document.getElementById(id))?eval(document.getElementById(id)):null;
		}
	}
  
function emutation_FlashWrite(miEMswf, miEMcreativewidth, miEMcreativeheight, miEMwmode, miEMbgcolor, miID){
	var emswf = miEMswf; 
	var emcreativewidth = miEMcreativewidth;
	var emcreativeheight = miEMcreativeheight;
	var emwmode = miEMwmode;
	var embgcolor = miEMbgcolor;
	
	ntcode = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width='"+ emcreativewidth +"' height='"+ emcreativeheight +"' id='"+ miID +"'>";
	ntcode +="<param name='movie' value='" + emswf + "'>";
	ntcode +="<param name='quality' value='high'>";
	ntcode +="<param name='bgcolor' value='#" + embgcolor +"'>";
	ntcode +="<param name='wmode' value='"+ emwmode +"'>";
	ntcode +="<param name='menu' value='false'>";
	ntcode +="<embed src='" + emswf + "' quality='high' wmode='" + emwmode + "' width='" + emcreativewidth + "' height='" + emcreativeheight + "' bgcolor='#" + embgcolor +"' type='application/x-shockwave-flash' menu='false' name='"+ miID +"' swLiveConnect='true'></embed></object>";
  	document.write(ntcode);
}

function emutation_FlashInnerHTML(htmlElementId,code){
  var x=emutation_ToObject(htmlElementId);
  if(x){
    if(document.getElementById||document.all){
      x.innerHTML='';
      x.innerHTML=code;
      }
    else if(document.layers){
      x.document.open();
      x.document.write(code);
      x.document.close();
      }
    }
  }

// Para alta/modificacion de usuario
function activaPaisUser (f,s) {
  var vSel = s.options[s.selectedIndex].value;
  var iSel = s.selectedIndex;
  
  if (iSel == s.options.length - 1) {
    f.txtPais.style.visibility="visible";  
    f.txtProvincia.style.visibility="visible";  
    f.txtProvincia.value = "";
    f.txtPais.value = "";
    f.selProvincia.disabled=true;
    f.selProvincia.selectedIndex = f.selProvincia.options.length - 1;
  } else {
    if ((vSel != "España") && (iSel!=0)) {
      f.txtPais.style.visibility="hidden";
      f.txtPais.value = "";
      f.txtProvincia.style.visibility="visible";  
      f.txtProvincia.value = ""
      f.selProvincia.disabled=true;
      f.selProvincia.selectedIndex = f.selProvincia.options.length - 1;
    } else {
      f.txtPais.style.visibility="hidden";  
      f.txtProvincia.style.visibility="hidden";  
      f.txtProvincia.value = "";
      f.txtPais.value = "";
      f.selProvincia.disabled=false;
      f.selProvincia.selectedIndex = 0;
    }
  }
    
}

function activaPais (selPais,selProv,txtPais,txtProv) {
  var vSel = selPais.options[selPais.selectedIndex].value;
  var iSel = selPais.selectedIndex;
  
  /// otro pais
  if (iSel == selPais.options.length - 1) {
    txtPais.style.visibility="visible";  
    txtProv.style.visibility="visible";  
    txtProv.value = "";
    txtPais.value = "";
    selProv.disabled=true;
    selProv.selectedIndex = selProv.options.length - 1;
    return 1;
  } else {
  	// europa
    if ((vSel != "España") && (iSel!=0)) {
      txtPais.style.visibility="hidden";
      txtPais.value = vSel;
      txtProv.style.visibility="visible";  
      txtProv.value = "";
      selProv.disabled=true;
      selProv.selectedIndex = selProv.options.length - 1;
      return 2;
    } else {
      //españa
      txtPais.style.visibility="hidden";  
      txtProv.style.visibility="hidden";  
      txtProv.value = "";
      txtPais.value = "";
      selProv.disabled=false;
      selProv.selectedIndex = 0;
      return 3;
    }
  }
  
}

