//Clase de utilidades
function Util() {};
//Variables de clase
Util.botonColorOver = '#D0D0E0';
Util.botonColorOverX = '#F5D0D0';
Util.botonColorOverY = '#D0F5D0';
Util.botonColorOut = '';
try {
	Util.jsr = new HTML_AJAX_Serialize_PHP();
} catch(e) {};
//Metodos de clase

Util.isIE = function() {
	var agt = navigator.userAgent.toLowerCase();
	return ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
}

Util.escape = function(arr) {
	for (var i in arr) {
		arr[i] = escape(arr[i]);
	}
	return Util.jsr.serialize(arr);
}

Util.unescape = function(arr) {
	for (var i in arr) {
		arr[i] = unescape(arr[i]);
	}
	return arr;
}

Util.serialize = function(arr) {
	return Util.jsr.serialize(arr);
}

Util.unserialize = function(arr) {
	return Util.jsr.unserialize(arr);
}

Util.habilitarForm = function(id, hab) {
	var count = document.getElementById(id).elements.length;
	for (i=0; i<count; i++) {
		document.getElementById(id).elements[i].disabled = !hab;
	}
}

Util.resaltarBotonesForm = function(id) {
//decora botones	
	var count = document.getElementById(id).elements.length;
	var obj, c;
	for (i=0; i<count; i++) {
		obj = document.getElementById(id).elements[i];
		if (obj.type == "button") {
			if ( (obj.value == 'Cerrar') || (obj.value == 'Anular') ) {
				obj.onmouseover = function() {
					this.style.backgroundColor = Util.botonColorOverX;
				}
			} else if (obj.value == 'Confirmar') {
				obj.onmouseover = function() {
					this.style.backgroundColor = Util.botonColorOverY;
				}
			} else {
				obj.onmouseover = function() {
					this.style.backgroundColor = Util.botonColorOver;
				}
			}
			obj.onmouseout = function() {
				this.style.backgroundColor = Util.botonColorOut;
			}
		}
	}	
}

Util.select = function(id, k) {
//selecciona una opcion por su valor en un control select	
	var obj = document.getElementById(id);
	for (var i = 0; i < obj.options.length; i++) {
		if (obj.options[i].value == k) {
			obj.options[i].selected = true;
			return true;
		}
	}
	return false;
}

Util.selectAddOption = function(id, k, v) {
//agrega una opcion a un select
	var sel_obj = Util.get(id);
	var opt_obj = document.createElement("OPTION");
	opt_obj.text = v;
	opt_obj.value = k;
	if (Util.isIE()) {
		sel_obj.add(opt_obj);
	} else {
		sel_obj.add(opt_obj, null);
	}
	return opt_obj;
}

Util.get = function(id) {
//devuelve un control segun su id	
	return document.getElementById(id);
}

Util.clearEvt = function(evt) {
	evt.returnValue = false;
}

Util.getKeyCode = function(evt) {
	return evt.keyCode;
}


Util.ctrlSec = function(id1, id2, f) {
//coloca dos controles de formulario en secuencia mediante [ENTER]	
	Util.get(id1).onkeypress = function(evt) {
		if (!evt) evt = event;
		if (Util.getKeyCode(evt) == 13) {
			Util.clearEvt(evt);
			if (!f) {
				Util.focus(id2, true);
			} else {
				Util.focus(id2, false);
			}
		}
	}
}

Util.setPos = function(id, x, y) {
	if (Util.isIE()) {
		Util.get(id).style.posLeft = x;
		Util.get(id).style.posTop = y;	
	} else {
		Util.get(id).style.left = x + "px";
		Util.get(id).style.top = y + "px";
	}	
}

Util.setHeight = function(id, h) {
//setea alto
	if (Util.isIE()) {
		Util.get(id).style.height = h + "px";	
	} else {
		Util.get(id).style.height = h + "px";	
	}	
}
Util.setWidth = function(id, w) {
//setea ancho
	if (Util.isIE()) {
		Util.get(id).style.width = w + "px";
	} else {
		Util.get(id).style.width = w + "px";
	}	
}

Util.setTam = function(id, w, h) {
//setea tamano
	Util.setWidth(id, w);
	Util.setHeight(id, h);
}

Util.centerDiv = function(id, w, h, ax, ay) {
//centra un div	
	if (!w) var w = 500;
	if (!h) var h = 150;
	if (!ax) var ax = 0;
	if (!ay) var ay = 0;
	var posx = (screen.availWidth - w) / 2 + ax;
	var posy = (screen.availHeight - h) / 2 - 100 + ay;
	Util.setPos(id, posx, posy);
}

Util.show = function(id, sh) {
//muestra u oculta un control	
	if (sh) {
		Util.get(id).style.visibility = "visible";
		Util.get(id).style.display = "inline";
	} else {
		Util.get(id).style.visibility = "hidden";
		Util.get(id).style.display = "none";
	}
}

Util.scrollToBegin = function(id) {
//mueve un control scrollable hasta el principio
	Util.get(id).scrollTop = 0;
}

Util.scrollToEnd = function(id) {
//mueve un control scrollable hasta el final
	Util.get(id).scrollTop = Util.get(id).scrollHeight;
}

Util.focus = function(id, sel) {
//foco sobre un control	(o seleccion)
	try {
		Util.get(id).value = Util.get(id).value + "";
	} catch(e) {};
	try {
		if (!sel) {
			Util.get(id).focus();
		} else {
			Util.get(id).select();
		}
	} catch(e) {};
}

Util.ffecha = function(f) {
	//fecha de dmY => d/m/Y
	if (f.length != 8) return f;
	return f.substr(0,2)+"/"+f.substr(2,2)+"/"+f.substr(4,4);
}

Util.delTable = function(id_t, ini) {
	//borra las filas de una tabla a partir de ini
	var t = Util.get(id_t);
	var tot = t.rows.length, j;
	for (var i = 0; i < (tot - ini); i++) {
		j = t.rows.length - 1;
		t.deleteRow(j);
	}
}

Util.delSelect = function(id_s, ini) {
	//bora los elementos de un select a partir de ini
	var s = Util.get(id_s);
	var tot = s.options.length, j;
	for (var i = 0; i < (tot - ini); i++) {
		j = s.options.length - 1;
		if (Util.isIE()) {
			s.options.remove(j);
		} else {
			s.remove(j);
		}
	}
}

Util.setValue = function(id_obj, val) {
	//setea value en un objeto
	if (Util.get(id_obj)) {
		Util.get(id_obj).value = val;
	}
}

Util.setInnerHTML = function(id_obj, html) {
	//setea innerHTML en un objeto
	if (Util.get(id_obj)) {
		Util.get(id_obj).innerHTML = html;
	}
}

Util.isReady = function(obj) {
	//devuelve true si el objeto esta listo (carga completa)
	if (obj) {
		if (Util.isIE()) {
			return (obj.readyState == "complete");
		} else {
			return obj.imReady;
		}
	} else {
		return false;
	}
}

//*** This code is copyright 2002-2003 by Gavin Kistner and Refinery Inc.; www.refinery.com
//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt
Util.findXY = function(id) {
	//devuelve la posicion absoluta de un objeto
	var obj = Util.get(id);
	var x=0,y=0;
	while (obj!=null){
		x+=obj.offsetLeft-obj.scrollLeft;
		y+=obj.offsetTop-obj.scrollTop;
		obj=obj.offsetParent;
	}
	return {x:x,y:y};
}