
// -----------------------------------------------------
// Funciones generales de la intranet de Circulo Gacela, 
// Creador: Isidro Casanovas Santos
// Fecha: 29 de Julio de 2008.
// Dependencias: navegador.js
// -----------------------------------------------------

   String.prototype.trim= function() {//Agrega la función trim al objeto String
   return this.replace(/(^\s*)|(\s*$)|(\.)|(\ )/g,""); //elimina espacios a izquierda y derecha
   }

idAbierto=0;
tControl=1;
tTotalLineas=0;
tLineasFactura=Array();
tLineaId=0;
tLineasVacio=0;
tProceso=false;

function fVerDatos(id,modo,objeto) {
	tModo=modo?modo:"tablaHtml";
	tCapa="capa8800"+id;
	if (document.getElementById(tCapa).style.display=="none") {
	        fCargarDatos(id,modo,objeto,'leer',tCapa);
		document.getElementById(tCapa).parentNode.style.borderWidth="1pt";
		document.getElementById(tCapa).parentNode.style.borderStyle="solid";
		document.getElementById(tCapa).parentNode.style.borderColor="#20619d"
		}
	else {
		document.getElementById(tCapa).innerHTML="";
		document.s.formularioAbierto.value=0;
                document.getElementById(tCapa).parentNode.style.borderWidth="0";
		}
        fExpandir("8800"+id);
        }

function fCargarDatos(id,modo,objeto,op,capa,opciones,cF) {

        tModo=modo?modo:"tablaHtml";
	tOpciones=opciones?opciones:1;
	tCapa=capa?capa:"capa800"+id;
	tCF=cF?cF:0;
        http=crearAjax();
        http.onreadystatechange=function() {
                if (http.readyState==4) {
                        document.getElementById(tCapa).innerHTML=http.responseText;
				evaluarScript(http.responseText);
                        }
                }

        x=0;
        tParametros="";
        while(document.s.elements[x]) {
		if (document.s.elements[x].tagName=="INPUT") {
	                t=document.s.elements[x].name.indexOf("dato");
       	         	if (t==0) { 
				if (document.s.elements[x].type=="checkbox") {
					if (document.s.elements[x].checked) { tParametros+="&"+document.s.elements[x].name+"="+escape(document.s.elements[x].value); }
					else { tParametros+="&"+document.s.elements[x].name+"=0"; }
					}
				else { tParametros+="&"+document.s.elements[x].name+"="+escape(document.s.elements[x].value); }
				}
		 	}
		x++;
                }
        http.open("POST","php/datosXml.php",true);
        http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        http.send("opciones="+tOpciones+"&capa="+tCapa+"&modo="+tModo+"&objeto="+objeto+"&op="+op+"&idObjeto="+id+tParametros);

        }


function fCargarDatosAdm(id,modo,objeto,op,capa,opciones,cF) {

        tModo=modo?modo:"tablaHtml";
	tOpciones=opciones?opciones:1;
	tCapa=capa?capa:"capa800"+id;
	tCF=cF?cF:0;
        http=crearAjax();
        http.onreadystatechange=function() {
                if (http.readyState==4) {
                        document.getElementById(tCapa).innerHTML=http.responseText;
				evaluarScript(http.responseText);
                        }
                }

        x=0;
        tParametros="";
        while(document.sAdm.elements[x]) {
		if (document.sAdm.elements[x].tagName=="INPUT") {
	                t=document.sAdm.elements[x].name.indexOf("dato");
       	         	if (t==0) { 
				if (document.sAdm.elements[x].type=="checkbox") {
					if (document.sAdm.elements[x].checked) { tParametros+="&"+document.sAdm.elements[x].name+"="+escape(document.sAdm.elements[x].value); }
					else { tParametros+="&"+document.sAdm.elements[x].name+"=0"; }
					}
				else { tParametros+="&"+document.sAdm.elements[x].name+"="+document.sAdm.elements[x].value; }
				}
			}
		else if (document.sAdm.elements[x].tagName=="SELECT") {
			tParametros+="&"+document.sAdm.elements[x].name+"="+document.sAdm.elements[x].options[document.sAdm.elements[x].selectedIndex].value;
		 	}
		x++;
                }
        http.open("POST","php/datosXml.php",true);
        http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        http.send("opciones="+tOpciones+"&capa="+tCapa+"&modo="+tModo+"&objeto="+objeto+"&op="+op+"&idObjeto="+id+tParametros);

        }

function fCargarSelect(id,modo,objeto,op,campoSelect,accion) {
	tModo=modo?modo:"tablaHtml";
	tObj=document.getElementById(campoSelect);
	tObj.length=0;
	http=crearAjax();
	http.onreadystatechange=function() {
		if (http.readyState==4) {
			var tRegistros=http.responseText.split("&");
			for (x=0;x<tRegistros.length;x++) {
				tResultado=tRegistros[x].split(";");
				if (tResultado[0] && tResultado[1]) tObj.options[x]=new Option(tResultado[0],tResultado[1]);
				if (accion) eval(accion);
				}
			}
		}

        x=0;
        tParametros="";
        while(document.s.elements[x]) {
                t=document.s.elements[x].name.indexOf("dato");
                if (t==0) {
                        if (document.s.elements[x].type=="checkbox") {
                                if (document.s.elements[x].checked) { tParametros+="&"+document.s.elements[x].name+"="+escape(document.s.elements[x].value); }
                                else { tParametros+="&"+document.s.elements[x].name+"=0"; }
                                }
                        else { tParametros+="&"+document.s.elements[x].name+"="+escape(document.s.elements[x].value); }
                        }
                x++;
                }

	http.open("POST","php/datosXml.php",true);
	http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	http.send("modo="+tModo+"&objeto="+objeto+"&op="+op+"&idObjeto="+id+tParametros);
	}

function evaluarScript(texto) {
        salir=false;
        tN2=0;
        n=texto.indexOf("<script language=javascript>",tN2);
        while (n!=-1) {
		tN=n+28;
		n2=texto.indexOf("</script>",tN2);
                tN2=n2+9;
                tNS=n2-tN;
                tS=texto.substring(tN,n2);
                eval(tS);
		n=texto.indexOf("<script language=javascript>",tN2);
                }
	}

function fComprobarCampo(campo,obj,idImagen,variableValidacion,opciones) {
	tImg=document.getElementById(idImagen);
	tV=document.getElementById(variableValidacion);
	opciones=opciones?opciones:"";
        http=crearAjax();
        http.onreadystatechange=function() {
                if (http.readyState==4) {
			tProceso=true;
			// alert(http.responseText);
                        if (http.responseText==0) { 
				tImg.src="imagenes/formulario/btn_ayuda_hoover.gif";
				tImg.alt="Valor de "+campo+" incorrecto";
				tImg.title="Valor de "+campo+" incorrecto";
				tV.value=0;
				obj.style.backgroundColor="#f17d00";
				tProceso=false;
				return false; 
				}
                        else {
                                tImg.src="imagenes/formulario/btn_ayuda.gif";
                                tImg.alt="Valor correcto";
                                tImg.title="Valor correcto";
				tV.value=1;
				obj.style.backgroundColor="#ffffff";
				tProceso=false;
				return true;
                                }
                        }
                }
	if (tProceso==false) {
	        http.open("POST","php/datosXml.php",true);
		http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		http.send("modo="+campo+"&objeto=usuario&op=comprobarCampo&idObjeto="+obj.value+"&opciones="+opciones);
		}
        }

function fComprobarCampoValorFecha(campo,valor,idImagen,variableValidacion,opciones) {
        tImg=document.getElementById(idImagen);
        tV=document.getElementById(variableValidacion);
        opciones=opciones?opciones:"";
        http=crearAjax();
        http.onreadystatechange=function() {
                if (http.readyState==4) {
                        tProceso=true;
                        // alert(http.responseText);
                        if (http.responseText==0) {
                                tImg.src="imagenes/formulario/btn_ayuda_hoover.gif";
                                tImg.alt="Valor de "+campo+" incorrecto";
                                tImg.title="Valor de "+campo+" incorrecto";
                                tV.value=0;
				document.sAdm.datoDia.style.backgroundColor="#f17d00";
				document.sAdm.datoMes.style.backgroundColor="#f17d00";
				document.sAdm.datoAno.style.backgroundColor="#f17d00";
                                tProceso=false;
                                return false;
                                }
                        else {
                                tImg.src="imagenes/formulario/btn_ayuda.gif";
                                tImg.alt="Valor de "+campo+" correcto";
                                tImg.title="Valor de "+campo+" correcto";
                                tV.value=1;
                                document.sAdm.datoDia.style.backgroundColor="#ffffff";
                                document.sAdm.datoMes.style.backgroundColor="#ffffff";
                                document.sAdm.datoAno.style.backgroundColor="#ffffff";
                                tProceso=false;
                                return true;
                                }
                        }
                }
        if (tProceso==false) {
                http.open("POST","php/datosXml.php",true);
                http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                http.send("modo="+campo+"&objeto=usuario&op=comprobarCampo&idObjeto="+valor+"&opciones="+opciones);
                }
        }

function fComprobarCampoValorCodigoPostal(campo,valor,idImagen,variableValidacion,opciones) {
        tImg=document.getElementById(idImagen);
        tV=document.getElementById(variableValidacion);
        opciones=opciones?opciones:"";
        http=crearAjax();
        http.onreadystatechange=function() {
                if (http.readyState==4) {
                        tProceso=true;
                        // alert(http.responseText);
                        if (http.responseText==0) {
                                tImg.src="imagenes/formulario/btn_ayuda_hoover.gif";
                                tImg.alt="Valor de "+campo+" incorrecto";
                                tImg.title="Valor de "+campo+" incorrecto";
                                tV.value=0;
				document.sAdm.datoIdProvincia.style.backgroundColor="#f17d00";
				document.sAdm.datoCodigoPostal.style.backgroundColor="#f17d00";
                                tProceso=false;
                                return false;
                                }
                        else {
                                tImg.src="imagenes/formulario/btn_ayuda.gif";
                                tImg.alt="Valor de "+campo+" correcto";
                                tImg.title="Valor de "+campo+" correcto";
                                tV.value=1;
                                document.sAdm.datoIdProvincia.style.backgroundColor="#ffffff";
                                document.sAdm.datoCodigoPostal.style.backgroundColor="#ffffff";
                                tProceso=false;
                                return true;
                                }
                        }
                }
        if (tProceso==false) {
                http.open("POST","php/datosXml.php",true);
                http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                http.send("modo="+campo+"&objeto=usuario&op=comprobarCampo&idObjeto="+valor+"&opciones="+opciones);
                }
        }


function fVaciarCampo(obj,valor) { if (obj.value==valor) obj.value=""; }
function fRellenarCampo(obj,valor) { if (obj.value=="") obj.value=valor; }

function fComprobarFecha() {
	if (document.sAdm.datoDia.options[document.sAdm.datoDia.selectedIndex].value && document.sAdm.datoMes.options[document.sAdm.datoMes.selectedIndex].value) {
		fComprobarCampoValorFecha("fecha",document.sAdm.datoDia.options[document.sAdm.datoDia.selectedIndex].value+"/"+document.sAdm.datoMes.options[document.sAdm.datoMes.selectedIndex].value+"/"+document.sAdm.datoAno.options[document.sAdm.datoAno.selectedIndex].value,"imgDatoFecha","datoFechaValida");
		}
	}

function fComprobarCodigoPostal() {
	if (document.sAdm.datoIdProvincia.options[document.sAdm.datoIdProvincia.selectedIndex].value && document.sAdm.datoCodigoPostal.value && document.sAdm.datoCodigoPostal.value!="C.P.") {
		fComprobarCampoValorCodigoPostal("codigoPostal",document.sAdm.datoCodigoPostal.value,"imgDatoCodigoPostal","datoCodigoPostalValido",document.sAdm.datoIdProvincia.value);
		}
	}
function fComprobarPasswords() {
	tImg=document.getElementById("imgDatoPassword");
	tV=document.getElementById("datoPasswordValido");
	if (document.sAdm.datoPassword.value && document.sAdm.datoPassword2.value && document.sAdm.datoPassword.value != "clave") {
		if ( document.sAdm.datoPassword.value.length<6 || document.sAdm.datoPassword.value.length>12 || document.sAdm.datoPassword.value!=document.sAdm.datoPassword2.value) {
			tImg.src="imagenes/formulario/btn_ayuda_hoover.gif";
			document.sAdm.datoPassword.style.backgroundColor="#f17d00";
			document.sAdm.datoPassword2.style.backgroundColor="#f17d00";
			tV.value=0;
			}
		else {
			tImg.src="imagenes/formulario/btn_ayuda.gif";
			document.sAdm.datoPassword.style.backgroundColor="#ffffff";
			document.sAdm.datoPassword2.style.backgroundColor="#ffffff";
			tV.value=1;
			}
		}
	}

function fValidarRegistro() {
	if (!document.sAdm.datoNombre.value || document.sAdm.datoNombre.value=="nombre") { alert("Tienes que introducir el nombre"); document.sAdm.datoNombre.focus(); return; }
	if (!document.sAdm.datoApellidos.value || document.sAdm.datoApellidos.value=="apellidos") { alert("Tienes que introducir los apellidos"); document.sAdm.datoApellidos.focus(); return; }
	if (!document.sAdm.datoSexo.options[document.sAdm.datoSexo.selectedIndex].value) { alert("Tienes que seleccionar el sexo"); document.sAdm.datoSexo.focus(); return; } 
	if (!document.sAdm.datoDia.options[document.sAdm.datoDia.selectedIndex].value || !document.sAdm.datoMes.options[document.sAdm.datoMes.selectedIndex].value || !document.sAdm.datoAno.options[document.sAdm.datoAno.selectedIndex].value || document.sAdm.datoFechaValida.value==0) { alert("Fecha incorrecta"); document.sAdm.datoDia.focus(); return; }
	if (!document.sAdm.datoDni.value || document.sAdm.datoDni.value=="dni" || document.sAdm.datoDniValido.value==0) { alert("Dni incorrecto"); document.sAdm.datoDni.focus(); return; }
	if (!document.sAdm.datoDireccion.value || document.sAdm.datoDireccion.value=="direccion (calle, piso, puerta)") { alert("Tienes que introducir la direccion"); document.sAdm.datoDireccion.focus(); return; }
	if (!document.sAdm.datoCiudad.value || document.sAdm.datoCiudad.value=="ciudad") { alert("Tienes que introducir la ciudad"); document.sAdm.datoCiudad.focus(); return; }
	if (!document.sAdm.datoIdProvincia.options[document.sAdm.datoIdProvincia.selectedIndex].value) { alert("Tienes que seleccionar la provincia"); document.sAdm.datoProvincia.focus(); return; }
	if (!document.sAdm.datoCodigoPostal.value || document.sAdm.datoCodigoPostal.value=="C.P.") { alert("Tienes que introducir el codigo postal"); document.sAdm.datoCodigoPostal.focus(); return; }
	if ((!document.sAdm.datoTelefonoMovil.value || document.sAdm.datoTelefonoMovil.value=="movil") && (!document.sAdm.datoTelefono.value || document.sAdm.datoTelefono.value=="telefono")) { alert("Tienes que introducir el telefono fijo o el telefono movil"); document.sAdm.datoTelefonoMovil.focus(); return; }
	if (!document.sAdm.datoEmail.value || document.sAdm.datoEmail.value=="correo electronico" || document.sAdm.datoEmailValido.value==0) { alert("Correo electronico incorrecto"); document.sAdm.datoEmail.focus(); return; }
	if (!document.sAdm.datoIdProvincia.options[document.sAdm.datoIdProvincia.selectedIndex].value || !document.sAdm.datoCodigoPostal.value || document.sAdm.datoCodigoPostal.value=="C.P."|| document.sAdm.datoCodigoPostalValido.value==0) { alert("Codigo postal incorrecto"); document.sAdm.datoCodigoPostal.focus(); return; }
	if (!document.sAdm.datoPassword.value || document.sAdm.datoPassword.value=="password") { alert("Tienes que introducir la clave"); document.sAdm.datoPassword.focus(); return; }
	if (document.sAdm.datoPassword.value.length<6) { alert("Clave demasiado corta"); document.s.datoPassword.focus(); return; }
	if (document.sAdm.datoPassword.value.length>12) { alert("Clave demasiado larga"); document.s.datoPassword.focus(); return; }
	fComprobarPasswords();
	if (document.sAdm.datoPasswordValido.value==0) { alert("Clave incorrecta"); document.sAdm.datoPassword.focua(); return; }
	if (document.sAdm.datoConfirmacion && !document.sAdm.datoConfirmacion.checked) { alert("Tienes que aceptar las bases"); document.sAdm.datoConfirmacion.focus(); return; }
	document.sAdm.op.value=1;
	document.sAdm.action='registro.php';
	document.sAdm.submit();
	}

function fValidarModificarDatos() {
	if (!document.sAdm.datoNombre.value || document.sAdm.datoNombre.value=="nombre") { alert("Tienes que introducir el nombre"); document.sAdm.datoNombre.focus(); return; }
	if (!document.sAdm.datoApellidos.value || document.sAdm.datoApellidos.value=="apellidos") { alert("Tienes que introducir los apellidos"); document.sAdm.datoApellidos.focus(); return; }
	if (!document.sAdm.datoSexo.options[document.sAdm.datoSexo.selectedIndex].value) { alert("Tienes que seleccionar el sexo"); document.sAdm.datoSexo.focus(); return; } 
	if (!document.sAdm.datoDia.options[document.sAdm.datoDia.selectedIndex].value || !document.sAdm.datoMes.options[document.sAdm.datoMes.selectedIndex].value || !document.sAdm.datoAno.options[document.sAdm.datoAno.selectedIndex].value || document.sAdm.datoFechaValida.value==0) { alert("Fecha incorrecta"); document.sAdm.datoDia.focus(); return; }
	if (!document.sAdm.datoDni.value || document.sAdm.datoDni.value=="dni" || document.sAdm.datoDniValido.value==0) { alert("Dni incorrecto"); document.sAdm.datoDni.focus(); return; }
	if (!document.sAdm.datoDireccion.value || document.sAdm.datoDireccion.value=="direccion (calle, piso, puerta)") { alert("Tienes que introducir la direccion"); document.sAdm.datoDireccion.focus(); return; }
	if (!document.sAdm.datoCiudad.value || document.sAdm.datoCiudad.value=="ciudad") { alert("Tienes que introducir la ciudad"); document.sAdm.datoCiudad.focus(); return; }
	if (!document.sAdm.datoIdProvincia.options[document.sAdm.datoIdProvincia.selectedIndex].value) { alert("Tienes que seleccionar la provincia"); document.sAdm.datoProvincia.focus(); return; }
	if (!document.sAdm.datoCodigoPostal.value || document.sAdm.datoCodigoPostal.value=="C.P.") { alert("Tienes que introducir el codigo postal"); document.sAdm.datoCodigoPostal.focus(); return; }
	if ((!document.sAdm.datoTelefonoMovil.value || document.sAdm.datoTelefonoMovil.value=="movil") && (!document.sAdm.datoTelefono.value || document.sAdm.datoTelefono.value=="telefono")) { alert("Tienes que introducir el telefono fijo o el telefono movil"); document.sAdm.datoTelefonoMovil.focus(); return; }
	if (!document.sAdm.datoIdProvincia.options[document.sAdm.datoIdProvincia.selectedIndex].value || !document.sAdm.datoCodigoPostal.value || document.sAdm.datoCodigoPostal.value=="C.P."|| document.sAdm.datoCodigoPostalValido.value==0) { alert("Codigo postal incorrecto"); document.sAdm.datoCodigoPostal.focus(); return; }
	if (!document.sAdm.datoPassword.value || document.sAdm.datoPassword.value=="password") { alert("Tienes que introducir la clave"); document.sAdm.datoPassword.focus(); return; }
	if (document.sAdm.datoPassword.value.length<6) { alert("Clave demasiado corta"); document.s.datoPassword.focus(); return; }
	if (document.sAdm.datoPassword.value.length>12) { alert("Clave demasiado larga"); document.s.datoPassword.focus(); return; }
	fComprobarPasswords();
	if (document.sAdm.datoPasswordValido.value==0) { alert("Clave incorrecta"); document.sAdm.datoPassword.focua(); return; }
	if (document.sAdm.datoConfirmacion && !document.sAdm.datoConfirmacion.checked) { alert("Tienes que aceptar las bases"); document.sAdm.datoConfirmacion.focus(); return; }
	// fCargarDatosAdm(0,"final","usuario","modificarDatosFinal","contenido");
	document.sAdm.op.value=1;
	document.sAdm.action='modificarDatos.php';
	document.sAdm.submit();
	}

function fRecordarPassword() {
	if (!document.sRP.datoEmail.value || document.sRP.datoEmail.value=="correo electr&oacute;nico") { alert("Tienes que introducir el correo electr&oacute;nico"); document.sRP.datoEmail.focus(); return false; }
	tb_show("Recordar contrase&ntilde;a","php/datosXml.php?width=400&height=342&op=recordarPassword&objeto=usuario&datoEmail="+document.sRP.datoEmail.value);
	}
