function grid(campo_x){
	this.reg = new Array()
	this.grid_campos = ""
	this.grid_titulos = ""
	this.m_grid = new Array()
	this.control = control
	this.grid = grid
	this.campo = campo_x
	this.form = form
	this.add_reg = add_reg
	this.ctl_select = "checkbox"
	this.orden = 1
	this.eliminar = eliminar
	this.bajar = bajar
	this.subir = subir
	this.seleccionar = seleccionar
	this.ult_ord = ""
	this.ult_sgn = false
	this.titulo = "Configuración de datos"
	this.border = "2"
	this.max_reg = 100
	this.min_reg = 0
	this.nro_reg = 0
	this.guardar = guardar
	this.estilo = ""
	this.ordenar = ordenar
	function ordenar(n_campo){
		n_campo = this.m_col[this.m_grid[n_campo]]
		maux = new Array()
		for(i=1;i<this.reg.length;i++){
			aux2 = this.reg[i].split("|")
			maux[i]=aux2[n_campo]
		}// next
		
		if (this.ult_ord != n_campo){
			this.ult_ord = n_campo
			this.sgn = true
		}else{
			this.sgn = !this.sgn
		}// end if
		if(this.sgn)
			cond = "maux[i]>maux[j]"
		else
			cond = "maux[i]<maux[j]"
		for(i=1;i<this.reg.length-1;i++){
			for(j=i+1;j<this.reg.length;j++){
				if(eval(cond)){
					aux1 = maux[i]
					maux[i] = maux[j]
					maux[j] = aux1
					aux = this.reg[i]
					this.reg[i] = this.reg[j]
					this.reg[j] = aux				
				}// edn fi
			}// next
		}// next
		this.control(this.campo)
	}// end fucntion
	function form(form_x){
		if (typeof(form_x)=="object")
			if (form_x.type=="form"){
				return form_x
			}else{
				return form_x.form
			}// end if
		else
			if (form_x=="" || form_x == null)
				return document.forms[0]
			else
				return document.forms[form_x]
			// end if
		// end if
	}// end function	
	function seleccionar(campo_x,titulo){
		t = document.getElementById(campo_x+"_tablaX")
		opcion = t.rows[titulo].cells[0].firstChild.checked
		for(x=titulo+1;x<t.rows.length;x++){
			t.rows[x].cells[0].firstChild.checked = opcion
		}// next
	}// end function	
	function bajar(f){
		t = document.getElementById(campo_x+"_tablaX")
		checked_x = new Array()
		for(j=t.rows.length-1;j>=0;j--){
			ele_x = t.rows[j].cells[0].firstChild
			checked_x[j] = ele_x.checked
			if(ele_x.checked && j>0 && j<(t.rows.length-1)){
				aux = this.reg[j]
				this.reg[j] = this.reg[j+1]
				this.reg[j+1] = aux
				aux2 = checked_x[j]
				checked_x[j]=checked_x[j+1]
				checked_x[j+1] = aux2
			}// end if
		}// next		
		this.control(this.campo)
		for(i=0;i<t.rows.length;i++){
			t.rows[i].cells[0].firstChild.checked = checked_x[i]
		}//next
	}// end if
	
	function subir(f){
		t = document.getElementById(campo_x+"_tablaX")
		checked_x = new Array()
		for(j=0;j<t.rows.length;j++){
			ele_x = t.rows[j].cells[0].firstChild
			checked_x[j] = ele_x.checked
			if(ele_x.checked && j>1){
				aux = this.reg[j]
				this.reg[j] = this.reg[j-1]
				this.reg[j-1] = aux
				aux2 = checked_x[j]
				checked_x[j]=checked_x[j-1]
				checked_x[j-1] = aux2
			}// end if
		}// next		
		this.control(this.campo)
		for(i=0;i<t.rows.length;i++){
			t.rows[i].cells[0].firstChild.checked = checked_x[i]
		}//next
	}// end if
	
	function eliminar(f){
		m_aux = new Array()
		t = document.getElementById(campo_x+"_tablaX")
		i=0
		for(j=0;j<t.rows.length;j++){
			ele_x = t.rows[j].cells[0].firstChild
			if(!ele_x.checked || j==0){
				m_aux[i]=this.reg[j]
				i++
			}// end if
		}// next	
		this.reg = m_aux
		this.control(this.campo)	
	}// end if	
	function control(campo_x){
		if(campo_x!="" && campo_x!=null)
			this.campo = campo_x
		campo_x = this.campo
		var ie4 = document.all;
		var ns4 = document.layers;
		var ns6 = document.getElementById && !document.all; 
		
		this.m_col = new Array()	
		col = this.reg[0].split("|")
		for(i=0;i<col.length;i++){
			this.m_col[col[i]]=i
		}// next		
		this.m_grid = this.grid_campos.split("|")
		m_titulos = this.grid_titulos.split("|")
		t = document.createElement("table")
		t.id=campo_x+"_tablaX"
		t.border = this.border
		t.className = this.estilo
		if(this.titulo!=""){
			caption = document.createElement("caption")
			caption.innerHTML = this.titulo
			t.appendChild(caption)
		}//end if
		document.getElementById(campo_x+"_divX").innerHTML = ""
		n = this.reg.length
		m = this.m_grid.length
		for(i=0;i<n;i++){
			m_valor = this.reg[i].split("|")
			fila = t.insertRow(-1)
			if(i>0){
				tipo_celda = "td"
			}else{
				tipo_celda = "th"
			}// end if	
			celda = document.createElement(tipo_celda)
			celda.className = this.estilo
			if(this.ctl_select=="checkbox"){
				ch = document.createElement("input")
				ch.type=this.ctl_select
				ch.value = i
				ch.name = this.campo+"_chkX_"+i
				ch.id = ch.name
			}else{
				if (i<=0){
					ch = document.createElement("span")
					ch.innerHTML = "&nbsp;"
				}else if(ie4){
					ch = document.createElement("<input type='radio' name='"+campo_x+"_chkX"+"'>")
					ch.id = ch.name
				}else{
					ch = document.createElement("input")
					ch.type=this.ctl_select
					ch.name = campo_x+"_chkX"
					ch.id = ch.name
				}// end if
			}// end if	
			if(i==0 && this.ctl_select=="checkbox"){
				n_titulo = this.n_titulo
				ch.onclick = function(){
					seleccionar(campo_x,0)
				}// end function
			}// end if			
			celda.appendChild(ch) 
			fila.appendChild(celda)			
			for(j=0;j<m;j++){
				campo = this.m_col[this.m_grid[j]]
				celda = document.createElement(tipo_celda)	
				celda.className = this.estilo
				if(i==0){
					celda.innerHTML = m_titulos[j]
					celda.onclick = function(){
						ele_x = document.getElementById(campo_x+"_ordX")
						ele_x.value = this.cellIndex-1
						ele_x.onclick()
					}// end fucntion
					celda.style.cursor = "pointer"
				}else{
					patron = /(.+)~(.+)/
					if (matchx = patron.exec(m_valor[campo])){
						m_valor[campo] = matchx[2]
					}// end if	
					if(m_valor[campo]=="" || m_valor[campo]==null)
						celda.innerHTML = "&nbsp;"
					else
						celda.innerHTML = m_valor[campo]
				}// end if
				fila.appendChild(celda)
			
			}//next
		}// next
		capa = document.getElementById(campo_x+"_divX")
		capa.appendChild(t)
		this.nro_reg = this.reg.length
		this.guardar()
		
		//alert(campo_x+"_divX")		
	}// end function
	function add_reg(form_q){
		n = this.reg.length
		if(n>this.max_reg){
			alert("Sobrepaso el máximo de registros permitidos")
			return false
		}// end if
		nn=form_q.elements.length
		aux=""
		
		m=this.m_grid.length
		
		for(i=0;i<m;i++){
			aux += ((aux!="")?"|":"")+form_q.elements[this.m_grid[m]].value
			
		}
		
		this.reg[n] = aux 
		this.control(this.campo)
		
	}// end if
	function guardar(){
		
		//alert(document.getElementById(this.campo).value)
		n= this.reg.length
		aux = ""
		for(i=0;i<n;i++){
			aux +=((aux!="")?"||":"")+this.reg[i] 
		
		}// next
		
		document.getElementById(this.campo).value = aux
		document.getElementById(this.campo+"_nregX").value = n-1
	
	}
}// end fucntion