//////////////////////////////////////////////////////////////////////////////////////////
// GESTIÓN DE LA AYUDA
//////////////////////////////////////////////////////////////////////////////////////////
function clickHandler(oEvent,targetId) 
{
	oElem=document.getElementById(targetId);
	if (oElem!=null)
  {
	  if (oElem.style.display == "") oElem.style.display = "none";
	  else oElem.style.display = "";
	}
	if (NavegadorIE())
	{
    oEvent.cancelBubble = true;
	  oEvent.returnValue = false;
	}
	else
	{
		oEvent.stopPropagation();
		oEvent.preventDefault();
	}
}

function clickHandlerCheck(oEvent,targetId,checkId) 
{
	oElem=document.getElementById(targetId);
	oElemCheck=document.getElementById(checkId);
	if (oElem!=null)
  {
	  if (oElemCheck.checked == false) oElem.style.display = "none";
	  else oElem.style.display = "";
	}
	/*
	if (NavegadorIE())
	{
   oEvent.cancelBubble = true;
	  oEvent.returnValue = false;
	}
	else
	{
		oEvent.stopPropagation();
		oEvent.preventDefault();
	}
	*/
}

//////////////////////////////////////////////////////////////////////////////////////////
// VALIDACIONES DE LOS CAMPOS DE EDICIÓN
//////////////////////////////////////////////////////////////////////////////////////////
function quina_lletra (Dni){
	var lletra=new Array("T","R","W","A","G","M","Y","F","P","D","X","B","N","J","Z","S","Q","V","H","L","C","K","E");
	if (isNaN(Dni)) return false;
                else  return (lletra[Dni%23]); 
}

function CalculaNIF(nif) {
	var dni;
	var lletra;
	var lletra_correcta;
	nif=nif.toUpperCase( );
	if (isNaN(nif)) {
		if ((nif.length>2)&&(nif.length<10)) {	
			dni=nif.substring(0,nif.length-1); 
			lletra=nif.substring(nif.length-1,nif.length);
			if (isNaN(dni)) {
				alert ("El NIF no tiene un formato correcto: '99999999Q'. Si se escribe solo el DNI se calculará automáticament el NIF.");
			} else {	
				lletra_correcta=quina_lletra(dni);
				if (lletra_correcta==false){
					alert ("El NIF no tiene un formato correcto: ( 99999999Q ). Si se escribe solo el DNI se calculará automáticament el NIF.");
				}
				if (lletra!=lletra_correcta) {
					alert ("Se corregirá la letra del NIF. Le corresponde la ".concat(lletra_correcta)) ;
					return dni.concat(lletra_correcta);
				}
			}
		}
        } else  {
        		if(nif.length<10){
        			//return nif.concat(quina_lletra(nif)); 
        			return quina_lletra(nif);
        		}
        }
	return "";
}

function validaDecimal(decimal){	
	
	if(decimal.indexOf(',')>=0) return false;
	separador=decimal.indexOf('.');
	
	if (separador<1) {partEntera=decimal;partDecimal=0;
	}else{
		partEntera=decimal.substring(0,separador);
		partDecimal=decimal.substring(separador+1);
	}
	
	if (!(validaNumero(partDecimal)) || (partDecimal.length>2) ) return false;
	if (!(validaNumero(partEntera)) || (partEntera.length>3) ) return false;
	return true;
}
function validaNumero(num){
	
	for(i=0;i<num.length;i++){
		if (num.charAt(i)<'0' || num.charAt(i)>'9')return false;
	}
	return true;
}

function validaLetras(num){
	
	for(i=0;i<num.length;i++){
		if (num.charAt(i)<'a' || num.charAt(i)>'Z')return false;
	}
	return true;
}

function CampoObligatorio(valor){
//  if (valor.length>0){
//  } else{
//  	alert (strInformarCampo);
//  }
}

function ValidarDatos(){
	var coll = document.all;
	if (coll!=null) {
	    for (i=0; i<coll.length; i++) 
	        alert(coll.item(i).tagName);
	}
}

function showElements(oEvent,theForm) {   
  var oCampo,value,name,sDescripcion,sTipoCampo,j,bError,sMensajeError;

	value=theForm.elements[0].value;
	for (i = 0; i < theForm.length; i++) 
	{
		if (name!=theForm.elements[i].name)
		{
      oCampo=theForm.elements[i];
			name=theForm.elements[i].name;
			value=theForm.elements[i].value;
			j=0;
		}
		else
		{
			if (j==1) sTipoCampo=theForm.elements[i].value;
			else
			{
				if (j==3) sDescripcion=theForm.elements[i].value;
				else 
				{
					if (j==5) 
					{
						bError=false;
						if ((theForm.elements[i].value=="not null") && (value.length==0))
						{
							// Campo obligatorio no informado
							bError=true;
							sMensajeError=strCampoObligatorio;
						}
						else
						{
							if ((sTipoCampo=='date') && (!Validar_Data(value)))
							{
								// Fecha incorrecta
								bError=true;
								sMensajeError=strCampoIncorrecto;
							}
						}

						// Si se ha producido un error, mostramos un mensaje y paramos la validación
						if (bError)
						{
							oCampo.focus();
							alert(sMensajeError);
							if (NavegadorIE())
							{
								oEvent.cancelBubble = true;
								oEvent.returnValue = false;
							}
							else
							{
	  						oEvent.stopPropagation();
								oEvent.preventDefault();
							}
							break;
						}
					}
				}
			}
		}
		j++;	
	}				
}


//--------------------------------------------------
//	Mascara per escriure dates
//	Només permet dígits i '/'.
//  Invocar desde 'onKeyPress'.
//--------------------------------------------------
function MascaraData(oEvent,oText){
var iNumero,iKeyCode,sData;

	// Obtenemos el código de la tecla
  if (oEvent.keyCode) iKeyCode=oEvent.keyCode;
  else iKeyCode=oEvent.which;

	sData = oText.value.split("/"); //0:Dia  1:Mes  2:Any					

	 //Tractament de la longitud
	//Caràcters no permesos
	if((iKeyCode<47)||(iKeyCode>57))
  {
		if (!TeclaFuncional(iKeyCode)) iKeyCode=-1;
  }
	else{
		//Barra
		if(iKeyCode==47){
			if((oText.value.length!=1)&&(oText.value.length!=2)&&
			   (oText.value.length!=4)&&(oText.value.length!=5))
					iKeyCode=-1;
			else{
				//Tractament d'un sol dígit.  Ex.: '3/' -> '03/'
				if(oText.value.length==1) 
					if(sData[0]==0) iKeyCode=-1;
					else oText.value="0"+oText.value;
				else if(oText.value.length==4)
					if(sData[1]==0) iKeyCode=-1;
					else oText.value=sData[0]+"/0"+sData[1];
			}
		}
		//Número
		else{ 
			//Barra automàtica
			if((oText.value.length==2)||(oText.value.length==5))
					oText.value=oText.value + "/";
			//Tractament de números
			iNumero=iKeyCode-48;
			switch (oText.value.length){
				case 0:  //Posició: 0 (Primer dígit de dia)
					if(iNumero>3) oText.value="0"+oText.value;		  //Ex.: '5' -> '05'
					break;
				case 1:  //Posició: 1 (Segon dígit de dia)
					if((sData[0]==0)&&(iNumero==0)) 
						iKeyCode=-1;
					else if((sData[0]==3)&&(iNumero>1))
						oText.value="0"+sData[0]+"/0";    //Ex.: '35'->'03/05'
					break;
				case 3:  //Posició: 3 (Primer dígit de mes)
					if(iNumero>1){ 
						//Restricció mesos per nº de dies (*)
						if((sData[0]==31)&&((iNumero==2)||(iNumero==4)||
						   (iNumero==6)||(iNumero==9)))
								iKeyCode=-1; 
						//Restricció mesos per nº de dies (*) 
						else if((sData[0]==30)&&(iNumero==2))
								iKeyCode=-1;
						else oText.value=sData[0]+"/0";   //Ex.:'14/5'->'14/05'
					}
					break;
				case 4:  //Posició: 4 (Segon dígit de mes)
					if((sData[1]==0)&&(iNumero==0)) 
						iKeyCode=-1;
					else if((sData[1]==1)&&(iNumero>2))
						iKeyCode=-1; 
					//Restricció mesos per nº de dies (*)
					else{
						if((sData[0]==31)&&(sData[1]==0)&&((iNumero==2)||
						   (iNumero==4)||(iNumero==6)||(iNumero==9)))
								iKeyCode=-1;
						else if((sData[0]==31)&&(sData[1]==1)&&(iNumero==1))
								iKeyCode=-1;
						else if((sData[0]==30)&&(sData[1]==0)&&(iNumero==2))
								iKeyCode=-1;
					}
					break;
				case 6:  //Posició:6 (Primer dígit d'any)
					if((iNumero==0)||((iNumero>2)&&(iNumero<6)))  //20**
						oText.value=sData[0]+"/"+sData[1]+"/20";
					else if(iNumero>=6)						      //19**
						oText.value=sData[0]+"/"+sData[1]+"/19";
					break;
				case 7:  //Posició: 7 (Segon dígit d'any)
					if((sData[2]==1)&&(iNumero<9)) iKeyCode=-1;
					break;
				//Restricció d'anys de traspàs (*)
				case 9:  //Posició: 9 (Quart dígit d'any)
					if((sData[0]==29)&&(sData[1]==2)&&
				       (!Traspas(sData[2]+iNumero))) iKeyCode=-1;
					break;
				default:
		  }
	  }
  }

  if (iKeyCode==-1)
  {
    if (NavegadorIE()) oEvent.keyCode=0;
    else oEvent.preventDefault();
  }
  
}

function TeclaFuncional(iKeyCode)
{
	if ((iKeyCode==8) || (iKeyCode==9) || (iKeyCode==46) || (iKeyCode==37) || (iKeyCode==39) || (iKeyCode==36) || (iKeyCode==35))
		return true;
  else return false;
}


//--------------------------------------------------
//	Funció que canvia el color del texte depenent
//	de si té o no un format de data correcte.
//  Invocar desde 'onBlur'.
//--------------------------------------------------
function SortirData(oText,ColorCorrecte,ColorErroni){

	if ((oText.value.length==0) || (Validar_Data(oText.value)))
	{ 
		oText.style.color=ColorCorrecte; 
		return(true); 
	}
	else
	{ 
		oText.style.color=ColorErroni; 
		return(false); 
	}
}


//--------------------------------------------------
//	Mascara per comprovar que una data és correcte.
//	Només permet dígits i '/'.
//--------------------------------------------------
function Validar_Data(sData){
var bTraspas;

	sData = sData.split("/"); //0:Dia  1:Mes  2:Any
	//Comprovació del nombre de camps
	if(sData.length!=3) return(false); 
	else{ 
		//Comprovació de la longitud dels camps
		if((sData[0].length!=2)||(sData[1].length!=2)||(sData[2].length!=4)) 
						return(false);
		//Tractament d'any
		if((sData[2]<1900)||(sData[2]>2100)) return(false); 
		else{ 
			//Tractament de mes(amb 31 dies)
			if((sData[1]==1)||(sData[1]==3)||(sData[1]==5)||(sData[1]==7)||
			   (sData[1]==8)||(sData[1]==10)||(sData[1]==12)){
					if((sData[0]<1)||(sData[0]> 31)) return(false);
			} 
			//Tractament de mes (amb 30	dies) 
			else if((sData[1]==4)||(sData[1]==6)||(sData[1]==9)||(sData[1]==11)){
					if((sData[0]<1)||(sData[0]>30)) return(false);
			}
			//Tractament de Febrer (28 o 29 dies)
			else if(sData[1]==2){
					bTraspas=Traspas(sData[2]);
					if((bTraspas)&&((sData[0]<1)||(sData[0]>29))) 
							return(false);
					else if((!bTraspas)&&((sData[0]<1)||(sData[0]>28))) 
							return(false);
			}
		}
	}
	return(true);
}


//--------------------------------------------------
//	Funció que donada una data en format incorrecte
//	la transforma correctament.
//--------------------------------------------------
function Transformar_Data(sData){
var iDia;
var iMes;
var iAny;
	
	sData = sData.split("/"); //0:Dia  1:Mes  2:Any
	//Comprovació del nombre de camps
	if(sData.length!=3) return "";
	else{ 
		//Tractament de dia
		if(sData[0].length==1) iDia="0" + sData[0];
		else iDia=sData[0];
		
		//Tractament de mes
		if(sData[1].length==1) iMes="0" + sData[1];
		else iMes=sData[1];
		
		//Tractament d'any
		if(sData[2].length==2){
			if(sData[2][0]>=6) iAny="19" + sData[2];
			else iAny="20" + sData[2];
		}
		else iAny=sData[2];
	}
	return(iDia + "/" + iMes + "/" + iAny);
}


//--------------------------------------------------
//	Mascara per saber si un any és de traspàs.
//	Només permet un enter de fins a 4 dígits.
//--------------------------------------------------
function Traspas(iAny){
	if((iAny % 4)==0){
			if((iAny % 100)==0){
					if((iAny % 400)==0) return(true);
					else return(false);
			}
			else return(true);
    }
	else return(false);
}


//--------------------------------------------------
//  Funció que retorna la data actual
//  Invocar desde 'onFocusIn'
//--------------------------------------------------
function FechaActual(oText)
{
  var d,s;
   
  if (oText.value.length==0)
  {
    d=new Date();
    s = d.getDate() +  "/" +  (d.getMonth()+1) +  "/" +  d.getFullYear();  
    oText.value=Transformar_Data(s);
  }
}


//--------------------------------------------------
//	Mascara per escriure números [decimals] [negatius].
//	Només permet dígits, -..
//  Invocar desde 'onKeyPress'.
//--------------------------------------------------
function Mascara_Numero(oEvent,oText,intMaxDecimals,blnNegatiu){
var intNumero,strNumero,intPosicionDecimal,bCancel;

	if (oEvent.keyCode) intNumero=oEvent.keyCode;
	else intNumero=oEvent.which;
	strNumero=oText.value;

	//Caràcters no permesos
  bCancel=false;
	if((intNumero<44)||(intNumero>57))
	{
		if (!TeclaFuncional(intNumero)) bCancel=true;
	}
	else{
		//Comprovem on està el separador decimal
		intPosicioDecimal=strNumero.search(/[,]|[.]/);		
		//Signe menys (-)
		if(intNumero==45){ 
				if((!(blnNegatiu))||(strNumero.search(/-/)>=0))
						bCancel=true;
		} 
		//Separador decimal (,.) 
		else if((intNumero==44)||(intNumero==46)){ 
				if(intMaxDecimals>0){
					if(intPosicioDecimal>=0) bCancel=true;
				}
				else bCancel=true;
		}
		//Número
		else{ 
				if((intPosicioDecimal>=0)&&(intMaxDecimals>0)){
						//Màxim número de decimals cobert
						if(strNumero.length-intPosicioDecimal-1-intMaxDecimals>=0)	
							bCancel=true;
				}
		}
	}

  if (bCancel) 
	{
		if (NavegadorIE()) oEvent.returnValue=0;
		else oEvent.preventDefault();
	}
}


//--------------------------------------------------
//	Función para llenar el contenido de una lista
//	asociada a otra lista
//--------------------------------------------------
function LlenarListasIE(oSelect,oElemXML,sLista,sCodElem)
{
  var oListas,oElemento,oListaElementos,oOption,sXPath;

  oListas=oElemXML.XMLDocument;
  if (oListas!=null)
  {
	  sXPath="listas/f[@i='" + sLista + "']/e[@i='" + sCodElem + "']";
	  oElemento=oListas.selectSingleNode(sXPath);
    if (oElemento!=null)
    {
			if (oElemento.getAttribute("s")!=null)
      {
				//Añadimos los elementos
				sXPath="listas/f[@i='" + oElemento.getAttribute("s") + "']/e";
        oListaElementos=oListas.selectNodes(sXPath);
        for (i=0;i<oListaElementos.length;i++)
        {
          oElemento=oListaElementos.item(i);
					oOption = document.createElement("OPTION");
					oOption.text=oElemento.nodeTypedValue;
				  oOption.value=oElemento.getAttribute("i") + "-" + oElemento.nodeTypedValue;
				  oSelect.add(oOption);
			  }
      }
    }
  }
}

function LlenarListasNoIE(oSelect,oElemXML,sLista,sCodElem)
{
  var i,j,sSubLista,oListas,oLista,oElemento,oListaElementos,oOption,sXPath;

  oListas=oElemXML.childNodes[1];
  if (oListas!=null)
  {
    //Primero buscamos la lista
		sSubLista='';
		for (i=0;i<oListas.childNodes.length;i++)
		{
			oLista=oListas.childNodes[i];
			if(oLista.nodeType != 3)
			{ 
				if (oLista.getAttribute('i')==sLista)
				{
					//Lista encontrada, buscamos el elemento
					for (j=0;j<oLista.childNodes.length;j++)
					{
						oElemento=oLista.childNodes[j];
						if (oElemento.nodeType !=3 )
						{
							if (oElemento.getAttribute('i')==sCodElem)
							{
								//Hemos encontrado la sublista que hay que cargar
								sSubLista=oElemento.getAttribute('s');
							break;
							}
						}
					}
					break;	
				}
			}
		}

		// Buscamos la sublista y cargamos todos sus elementos
		if (sSubLista.length>0)
		{
			for (i=0;i<oListas.childNodes.length;i++)
			{
				oLista=oListas.childNodes[i];
				if(oLista.nodeType != 3)
				{ 
					if (oLista.getAttribute('i')==sSubLista)
					{
						//Lista encontrada, cargamos todos sus elementos
						for (j=0;j<oLista.childNodes.length;j++)
						{
							oElemento=oLista.childNodes[j];
							oOption = document.createElement("OPTION");
							oOption.text=oElemento.firstChild.nodeValue;
						  oOption.value=oElemento.getAttribute("i") + "-" + oElemento.firstChild.nodeValue;
						  oSelect.appendChild(oOption);
						}
						break;
					}
				}
			}
		}
  }
}

function LlenarListaAsociada(oSelect,sLista,sElemento)
{
  var i,oElemXML;

	//Primero eliminamos todos los elementos menos el primero que es un elemento en blanco
 	while (oSelect.options.length>1) oSelect.removeChild(oSelect.options[1]);

  //Buscamos la sublista asociada al elemento de la lista indicada
  sCodElem="";
  for(i=0;i<sElemento.length;i++)
  {
    if ('-'==sElemento.substr(i,1)) break;
    else sCodElem=sCodElem.concat(sElemento.substr(i,1));
  }
  oElemXML=document.getElementById("oListasXml");
  if (oElemXML!=null)
  {
    if (NavegadorIE())
    {
      LlenarListasIE(oSelect,oElemXML,sLista,sCodElem);
    }
    else
		{
      LlenarListasNoIE(oSelect,oElemXML,sLista,sCodElem);
    }
  }
}

function ActualizarListas()
{
	var i,aElementos,aElemAux,aListas,sName;

	//Actualizamos todas las combos y los options
  aElementos=document.getElementsByTagName("SELECT");
  for(i=0;i<aElementos.length;i++)
  {
		sName=aElementos.item(i).name;
		sName=sName.substr(0,sName.length-1) + '1';
		if (sName!=aElementos.item(i).name)
		{
			aListas=document.getElementsByName(sName);
			if (aListas.length>=10)
			{
				aElemAux=document.getElementsByName(aElementos.item(i).name);
				LlenarListaAsociada(aListas.item(0),aElemAux.item(8).value,aElementos.item(i).value);
			}
		}
  }
  aElementos=document.getElementsByTagName("INPUT");
  for(i=0;i<aElementos.length;i++)
  {
	  if (aElementos.item(i).type=="radio")
    {
      if (aElementos.item(i).checked)
			{
				sName=aElementos.item(i).name;
				sName=sName.substr(0,sName.length-1) + '1';
				if (sName!=aElementos.item(i).name)
				{
					aListas=document.getElementsByName(sName);
					if (aListas.length>=10)
					{
						aElemAux=document.getElementsByName(aElementos.item(i).name);
						LlenarListaAsociada(aListas.item(0),aElemAux.item(aElemAux.length-1).value,aElementos.item(i).value);
					}
				}
			}
    }
  }
}

function InicializarFormulario()
{
	var i,aElementos,aElemAux,aListas,sName;

  //Ponemos valores en los campos fijos del formulario
  if (sNumDoc.length>0)
  {
    aElementos=document.getElementsByName("WTGNUMDOC");
    if (aElementos.length>0) aElementos.item(0).value=sNumDoc;
  }
  if (sCarCon.length>0)
  {
	  aElementos=document.getElementsByName("WTGCARCON");
	  if (aElementos.length>0) aElementos.item(0).value=sCarCon;
  }
  if (sNombre.length>0)
  {
	  aElementos=document.getElementsByName("WTGNOMBRE");
	  if (aElementos.length>0) aElementos.item(0).value=sNombre;
  }
  if (sApell1.length>0)
  {
	  aElementos=document.getElementsByName("WTGAPELL1");
	  if (aElementos.length>0) aElementos.item(0).value=sApell1;
  }
  if (sApell2.length>0)
  {
	  aElementos=document.getElementsByName("WTGAPELL2");
	  if (aElementos.length>0) aElementos.item(0).value=sApell2;
  }
  if (sDirMai.length>0)
  {
	  aElementos=document.getElementsByName("WTGDIRMAI");
	  if (aElementos.length>0) aElementos.item(0).value=sDirMai;
  }

  if (sTelef1.length>0)
  {
	  aElementos=document.getElementsByName("WTGTELEF1");
	  if (aElementos.length>0) aElementos.item(0).value=sTelef1;
  }
  
  if (sTelMov.length>0)
  {
	  aElementos=document.getElementsByName("WTGTELMOV");
	  if (aElementos.length>0) aElementos.item(0).value=sTelMov;
  }
  if (sMailNot.length>0)
  {
	  aElementos=document.getElementsByName("WTGMAINOT");
	  if (aElementos.length>0) aElementos.item(0).value=sMailNot;
  }
  
  aElementos=document.getElementsByName("WTGENVMAI");
   if (aElementos.length>0 ) 
   {
   		if (aElementos.item(0).type=='checkbox')
		{
			if (sEnvMailNot=='S')
				aElementos.item(0).checked=true;
			else
				aElementos.item(0).checked=false;
		}
		else{
			if (sEnvMailNot=='S')
				aElementos.item(0).value='S';
			else
				aElementos.item(0).value='N';
		}
	}
   
   
   aElementos=document.getElementsByName("WTGENVSMS");
   if (aElementos.length>0)
   {	
   
   		if (aElementos.item(0).type=='checkbox')
		{
   			if (sEnvSMSNot=='S')
   				aElementos.item(0).checked=true;
			else
				aElementos.item(0).checked=false;
		}
		else{
			if (sEnvSMSNot=='S')
				aElementos.item(0).value='S';
			else
				aElementos.item(0).value='N';
		}
   } 
   
   

  //Actualizamos las listas
  ActualizarListas();
  
}


//--------------------------------------------------
//	Función para verificar los dígitos de control
//	de una cuenta bancaria
//--------------------------------------------------
function ValidarDigitoControlCueBan(sEntidad,sOficina,sDC,sCuenta)
{
  if ((sEntidad.length>0) && (sOficina.length>0) && (sCuenta.length>0))
  {
    sDC=FormatearCadena(sDC,2,'0');
    if (sDC!=CalcularDigitosControlCueBan(sEntidad,sOficina,sCuenta))
    {
      alert(strDCCuentaBancaria);
    }
  }
}

function CalcularDigitosControlCueBan(sEntidad,sOficina,sCuenta)
{
  var sDC,sCode,iFirstDigit,iSecondDigit;

	//Formateamos las diferentes partes de la cuenta bancaria
	sEntidad=FormatearCadena(sEntidad,4,'0');
	sOficina=FormatearCadena(sOficina,4,'0');
	sCuenta=FormatearCadena(sCuenta,10,'0');
  sCode=FormatearCadena(sEntidad+sOficina,10,'0');

	//Calculamos el primer dígito
  iFirstDigit=CalcularDigitoControlCueBan(sCode);

	//Calculamos el segundo dígito
	iSecondDigit=CalcularDigitoControlCueBan(sCuenta);

	//Devolvemos los dígitos de control
  sDC=iFirstDigit.toString() + iSecondDigit.toString();
  return(sDC);
}

function CalcularDigitoControlCueBan(sCadena)
{
  var k,iTemp,iDigito;
	var aCoef = new Array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);

	//Calculamos el segundo dígito
  iTemp=0;
  for(k=0;k<10;k++)
    iTemp=iTemp+(sCadena.substr(k,1)*aCoef[k]);
  iTemp=11-(iTemp%11);
  
  switch (iTemp)
  {
    case 10: 
			iDigito=1;
			break;
		case 11:
			iDigito=0;
			break;
		default:
			iDigito=iTemp;
  }

  return(iDigito);
}

function FormatearCadena(sCadena,iCaracteres,sCaracter)
{
  var i;

  sResultado=sCadena;
  if (sCadena.length<iCaracteres)
  {
    for(i=0;i<(iCaracteres-sCadena.length);i++) 
		{
			sResultado=sCaracter+sResultado;
		}
  }
  return(sResultado);
}

function ComprobarLongitudTexto(oTexto,lMaximo)
{
  if (lMaximo-oTexto.value.length < 0)
    oTexto.value = oTexto.value.substring(0,lMaximo);
}

function LimitarTexto(oEvent,oTexto,lMaximo)
{
  var iKeyCode;

  if (oEvent.keyCode) iKeyCode=oEvent.keyCode;
  else iKeyCode=oEvent.which;

  if ((oTexto.value.length >= lMaximo) && (!TeclaFuncional(iKeyCode)))
  {
	  if (NavegadorIE())
	  {
      oEvent.cancelBubble=true;
	    oEvent.returnValue=false;
	  }
	  else
	  {
		  oEvent.stopPropagation();
		  oEvent.preventDefault();
	  }
  }
}


//----------------------------------------------------------
//	Funciones para la gestión del botón de modificar
//  datos que tiene el paso de confirmación de un formulario
//----------------------------------------------------------
function ModificarDatos(oFormulario)
{
	var surl=window.document.referrer;
	var aUrl;
	aUrl=surl.split('?');
	if (aUrl.lenght>1)
		surl=aUrl[0] + '?procedencia=Confirmacion&' + aUrl[1];
	else
		surl=aUrl[0] + '?procedencia=Confirmacion';
		
	oFormulario.action=surl;
	oFormulario.submit();
}

function EstablecerValorCampo(theForm,nombreCampo,valorCampo) 
{     	
  var oCampo;
  var sName,aElemAux,aListas;
	
	oCampo=theForm.getElementById(nombreCampo);
	
	if (oCampo!=null) 
	{
		if (oCampo.type=='checkbox'||oCampo.type=='radio')
		{
			if (valorCampo=='S')
				oCampo.checked=true;
			else
				oCampo.checked=false;
			oCampo.value=valorCampo;
		}
		else
		{
			if(oCampo.type=='select-one')
			{
				oCampo.value=valorCampo;
				sName=nombreCampo;
				sName=sName.substr(0,sName.length-1) + '1';
				if (sName!=nombreCampo)
				{
					aListas=document.getElementsByName(sName);
					if (aListas.length>=10)
					{
						aElemAux=document.getElementsByName(nombreCampo);
						LlenarListaAsociada(aListas.item(0),aElemAux.item(8).value,valorCampo);
					}
				}
			}
			else
			{
				oCampo.value=valorCampo;
			}
		}
	}	
}
