﻿function scrollToBottom(elm_id){
	var elm = document.getElementById(elm_id);
	try
		{
		elm.scrollTop = elm.scrollHeight;
		}
	catch(e)
		{
		var f = document.createElement("input");
		if (f.setAttribute) f.setAttribute("type","text")
		if (elm.appendChild) elm.appendChild(f);
		f.style.width = "0px";
		f.style.height = "0px";
		if (f.focus) f.focus();
		if (elm.removeChild) elm.removeChild(f);
		}
}
	
Ext.override(Ext.form.ComboBox, {
    setValue : function(v){
        if(this.mode == 'remote' && !Ext.isDefined(this.store.totalLength)){
            this.store.on('load', this.setValue.createDelegate(this, arguments), null, {single: true});
            if(this.store.lastOptions === null){
                this.store.load();
            }
            return;
        }
        var text = v;
        if(this.valueField){
            var r = this.findRecord(this.valueField, v);
            if(r){
                text = r.data[this.displayField];
            }else if(this.valueNotFoundText !== undefined){
                text = this.valueNotFoundText;
            }
        }
        this.lastSelectionText = text;
        if(this.hiddenField){
            this.hiddenField.value = v;
        }
        Ext.form.ComboBox.superclass.setValue.call(this, text);
        this.value = v;
    }
});

Ext.override(Ext.form.Field,{
	enableKeyEvents: true,
	listeners:{
		keydown: function(field, e){
			if(e.getKey() == Ext.EventObject.ENTER){
				if (field.next){
					var nextfield = Ext.get(field.next);
				    nextfield.focus.defer(100, nextfield );
			    }
			}
		}
	}
})

Eps = {
    version : '2.0',
    path: ""
};

Eps.general = {
	getAppPath: function(){
		appPath = location.protocol + "//" + location.host ;
		return appPath;
	},
	getSession: function(name){
		path = this.getAppPath() + '/interfaz/asp/session/ajax_getSession.asp';
		response = Ext.Ajax.request({
	        url: path,
	        method: 'POST',
	        params: {
	            session: name
	        },
	        success: function(res){
	        	if (response){
        			return response.conn.responseText;
        		}
	        },
	        failure: function(res){
	        	return response.conn.responseText;
	        }
	    });
	},
	getGridData: function(grid){
	   /* var columns = grid.getColumnModel().config;
	    var data = [];
	    var gridData = ""
	    grid.store.data.each(function(item) {
	      //apply renderers from column model
	      for (var key in item.data) {
	        var value = item.data[key];
	        gridData  = gridData  + value + ","
	      }
   	      gridData  = gridData + "["
	    });
	    return(gridData)*/
	    
	    var columns = grid.getColumnModel().config;
    
	    //build a useable array of store data for the XTemplate
	    var data = [];
	    grid.store.data.each(function(item) {
	      var convertedData = {};
	
	      //apply renderers from column model
	      for (var key in item.data) {
	        var value = item.data[key];
	        
	        Ext.each(columns, function(column) {
	          if (column.dataIndex == key) {
	           	 convertedData[key] = column.renderer ? column.renderer(value) : value;
	          }
	        }, this);
	      }
	      data.push(Ext.util.JSON.epsEncode(convertedData,1));
	    });
		return data ;
	},
	contextmenu: function(){
		return false;	
	},
	getVar: function(name){
		get_string = document.location.search;         
        return_value = '';
         do { //This loop is made to catch all instances of any get variable;
             name_index = get_string.indexOf(name + '=');

             if (name_index != -1) {
                 get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);

                 end_of_value = get_string.indexOf('&');
                 if (end_of_value != -1)
                     value = get_string.substr(0, end_of_value);
                 else
                     value = get_string;

                 if (return_value == '' || value == '')
                     return_value += value;
                 else
                     return_value += ', ' + value;
             }
         } while (name_index != -1)
            
         //Restores all the blank spaces;
         space = return_value.indexOf('+');
         while (space != -1) {
             return_value = return_value.substr(0, space) + ' ' +
              return_value.substr(space + 1, return_value.length);

             space = return_value.indexOf('+');
         }
         return(return_value); 
	}	
}

document.oncontextmenu = Eps.general.contextmenu;
