/**
 * Clase que contiene utilidades 
 * varias
 */
function JUtil() {}
JUtil.IE  = 1001;
JUtil.MOZ = 1002;
JUtil.getNav = function() {
    var isIE = navigator.userAgent.toLowerCase().indexOf("msie") > -1? true : false;
    var isMoz = document.implementation && document.implementation.createDocument;
    if ( isIE ) return this.IE;
    if ( isMoz) return this.MOZ;
}   
var isIE = JUtil.getNav()==JUtil.IE? true : false;

/**
 * Metodo que devuelve el objeto asociado al id
 * @return (Object) Objeto asociado al id
 */
JUtil.el = function(id) {
	return document.getElementById(id);
}

JUtil.isDefined = function(id) {
	return typeof(id) == 'undefined'? false : true;
}

JUtil.screenWidth= function() {
	return document.body.clientWidth;
}

JUtil.emailValido = function(email) {
	var s = email;
	var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_\-]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (s.length == 0 ) return true;
	return filter.test(s)? true : false;
}


JUtil.screenHeight= function() {
	return document.body.clientHeight;
}

JUtil.abrirPopup = function(href,width,height) {
	iLeft = (JUtil.screenWidth() - parseInt(width) ) /2;
	iTop  = (JUtil.screenHeight() - parseInt(height) ) /2;
	if ( width  > 0 || height>0 ) {
		window.open(href,"_blank","width="+width+", height="+height+", left="+iLeft+", top="+iTop);
	} else {
		window.open(href,"_blank");
	}	
}

/**
 * Redondea y formatea un número en base
 * a la precisión
 * @param   (double)    value        Número a formatear 
 * @param   (int)       precision    Número de decimales 
 */
Math.roundOFF = function (value, precision) {
    value = "" + value //convert value to string
    precision = parseInt(precision);
    var whole = "" + Math.round(value * Math.pow(10, precision));
    if ( precision == 0) return whole;
    var decPoint = whole.length - precision;

    if(decPoint != 0) {
        result = whole.substring(0, decPoint);
		result += ",";
        result += whole.substring(decPoint, whole.length);
		if (result==",0") result="0,00";
    } else {
        result = "0,"+whole;
    }
    return result;
}

/**
 * Objeto para controlar los selects
 */
function JSelect(input) {
	// Propiedades
	this.input = input;
	
	// Metodos
	this.add	= JSelectAdd;
	this.clear	= JSelectClear;
	this.hide	= JSelectHide;
	this.show	= JSelectShow;
	this.select = JSelectSelect;
}

function JSelectSelect(valor) {
	for(i=0;i<this.input.options.length;i++) {
		if ( this.input.options[i].value == valor ) 
		{
			this.input.options[i].selected = true;
		}
	}
}

function JSelectHide() {
	this.input.style.display = "none";	
}

function JSelectAdd(id,valor,selected) {
	this.input.options[this.input.options.length] = new Option(valor,id,false,selected);
}

function JSelectClear() {
	var i;
	for(i=this.input.options.length;i>=1;i--) {
		this.input.options[i] = null;
	}
}

function JSelectShow() {
	this.input.style.display = isIE? "inline" : "inline";	
}

