////////////////////////////////////////////////////////////////////////////////
// Funciones varias
// Requiere: prototype.js
////////////////////////////////////////////////////////////////////////////////
Object.extend(String.prototype, {
  trim: function() {
	    return this.replace(/^\s+|\s+$/, '');
	}   
	
});

function estaDefinida( variable) {
    return (typeof(window[variable]) == "undefined")?  false: true;
}

function depura(obj) {
    var props = $H(obj);
    var resultado = "";
    var maxItemsLinea = 6;
    var itemsLinea = 0;    
    props.each(function(item) {
        resultado += "["+ item.key + "]=" + item.value + " ";
        itemsLinea++;
        if(itemsLinea == maxItemsLinea) {
            itemsLinea = 0;
            resultado += "\n"; //Nueva linea
        }
    });
    mensaje(resultado);
}

function errorAjax(peticion) {
    //mensaje("Error AJAX: "+peticion);
    depura(peticion);
}

function opcionLista(idLista) {
    var opciones = $(idLista);
    return opciones.options[opciones.selectedIndex];                
}

function seleccionaLista(idLista,opcion) {
    var opciones = $(idLista);
    if(opciones != undefined) {        
        opciones.selected = false;
        for(var i = 0; i < opciones.length ; i++) {
            if(opciones.options[i].value == opcion) {
                opciones.selectedIndex = i;    
                opciones.selected = false;
            }
        }
    }
}

//var Validacion = Class.create();

function validaEmail(valor) {
    reg = new RegExp("^(([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}){0,1}$");
    return reg.test(valor);
}

function recortaCampos(formulario) {    
    var campos = Form.getInputs(formulario,"text","");    
    campos.each(function(item, index) {
	item.value = item.value.trim();
    });
}

function mensaje(msg) {
    if(estaDefinida("Dialog")) {
        Dialog.alert(
                msg, 
                {
                    windowParameters: {width:300, height:100,hideEffect:  Element.hide, showEffect:  Element.show}, 
                    okLabel: "cerrar"
                    
                }
        );        
    } else {
        alert(msg);
    }
}