	Ext.namespace('Utilities');
	
	Utilities.showAllKeys = function(obj)
	{
		var kz = "";
		for (var j in obj) kz+=" "+j;
		alert(kz);
	}
	
	Utilities.findNearestParentWithTag = function(el,tag) {
		var result = el.parentNode;
		if (result.tagName.toLowerCase() == "body") return null;
		if (result.tagName.toLowerCase() == tag.toLowerCase()) return result;
		else return Utilities.findNearestParentWithTag(result,tag);
	}
	
	Utilities.findNearestParentWithTagAndClass = function(el,tag,className) {
		var result = el.parentNode;
		if (result.tagName.toLowerCase() == "body") return null;
		if (result.tagName.toLowerCase() == tag.toLowerCase() 
			&& result.className.indexOf(className)>0) return result;
		else return Utilities.findNearestParentWithTagAndClass(result,tag,className);
	}
	
	Utilities.getMoneyUnformatted = function(cashValue)
	{
		if (cashValue)
		{
			cashValue=cashValue.toString();
			var dI = cashValue.indexOf("$");
			if (dI>0)
			{
				cashValue = cashValue.substring(dI);
				cashValue= parseFloat(cashValue).toFixed(2);
			}
		}
		return cashValue;
	}
	Utilities.findKeys = function (obj) {
		var kz = "";
		for (var x in obj) kz+=x+"|";
		return kz;
	}
	Utilities.findRelativePos = function (from,to) {
		var fromPos = Utilities.findPos(from);
		var toPos = Utilities.findPos(to);
		return [fromPos[0]-toPos[0],fromPos[1]-toPos[1]];
	}
	Utilities.findPos = function (obj) {
		var curleft = curtop = 0;
	
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
			while (obj = obj.offsetParent);
		}
		return [curleft,curtop];
	}
	Utilities.findSize = function(el)
	{

		var temp = {};

		if (el.getHeight && el.getWidth)
		{
			temp.height=el.getHeight();
			temp.width=el.getWidth();
		}
		else //if (!Ext.isIE)
		{
			temp.height = el.offsetHeight;
			temp.width = el.offsetWidth;
		}
		if (el.currentStyle && el.currentStyle.height) {
			var h = Utilities.trimPx(el.currentStyle.height);
			if (!isNaN(h)) temp.height = h;
		}
		if (el.currentStyle && el.currentStyle.width) {
			var w = Utilities.trimPx(el.currentStyle.width);
			if (!isNaN(w)) temp.width = w;
		}
		//this.addFeedback(td.cellIndex+' '+temp.height);
		
		if (!temp.width) temp.width=0;
		if (!temp.height) temp.height=0;
		
		//alert(temp.height+' '+temp.width);
		return temp;
	}
	
	Utilities.isOverlapping = function(a,b, widthAdjustment,heightAdjustment) {
		var sa = Utilities.findSize(a);
		var sb = Utilities.findSize(b);
		var pa = Utilities.findPos(a);
		var pb = Utilities.findPos(b);
		
		if (widthAdjustment) pa[0]+=widthAdjustment;
		if (heightAdjustment) pa[1]+=heightAdjustment;
		
	//		var ac = {topleft:[pa[0],pa[1]],topright:[pa[0]+sa.width,pa[1]],bottomleft:[pa[0],pa[1]+sa.height,bottomright:[pa[0]+sa.width,pa[1]+sa.height]};
	//	var bc = {topleft:[pb[0],pb[1]],topright:[pb[0]+sb.width,pa[1]],bottomleft:[pb[0],pb[1]+sb.height,bottomright:[pb[0]+sb.width,pb[1]+sb.height]};
	
																																		 
		var edge_a = {left:pa[0],right:pa[0]+sa.width,top:pa[1],bottom:pa[1]+sa.height};
		var edge_b = {left:pb[0],right:pb[0]+sb.width,top:pb[1],bottom:pb[1]+sb.height};
		
		
																																		 
		var verticallyAligned = (edge_a.top > edge_b.bottom && edge_a.top < edge_b.top)
			|| (edge_a.bottom > edge_b.bottom && edge_a.bottom < edge_b.top);
		var horizontallyAligned = (edge_a.right > edge_b.left && edge_a.right < edge_b.right)
			|| (edge_a.left > edge_b.right && edge_a.left < edge_b.right);
		
		//	alert(a.id+" "+verticallyAligned+" "+horizontallyAligned);
		return verticallyAligned && horizontallyAligned;
	}
	
	Utilities.trimPx= function (s)
	{
		var res=s;
		if (s.indexOf('px')>0) res = s.substring(0,s.indexOf('px'));
		try{res = parseInt(res);}catch (e){}
		return res;													 
	}
	
	Utilities.arrayContains = function (arr,val) {
		for (var c=0;c<arr.length;c++) {
			//alert("ac "+c+" "+arr[c]+" "+val+" "+(arr[c]==val));
			if (arr[c]==val) return true;	
		}
		return false;
	}
	
	
	Utilities.getCurrentStylePadding=function (obj)
	{
		var padding={};
		if (obj.currentStyle) {
			var styleObj = obj.currentStyle; 
			padding.top=styleObj['paddingTop'];
			padding.right=styleObj['paddingRight'];
			padding.bottom=styleObj['paddingBottom'];
			padding.left=styleObj['paddingLeft'];
			
		} else if (Ext.isGecko) {
			var styleObj = document.defaultView.getComputedStyle(obj, null); 

			padding.top=styleObj['paddingTop'];
			padding.right=styleObj['paddingRight'];
			padding.bottom=styleObj['paddingBottom'];
			padding.left=styleObj['paddingLeft'];

		} else { //if (Ext.isSafari) 
			var styleObj = document.defaultView.getComputedStyle(obj, null); 
			padding.top=styleObj['padding-top'];
			padding.right=styleObj['padding-right'];
			padding.bottom=styleObj['padding-bottom'];
			padding.left=styleObj['padding-left'];
			
		}
		padding.all=styleObj['padding'];
		padding.isUniform = padding.all == padding.top
			&&  padding.all == padding.right
			&&  padding.all == padding.bottom
			&&  padding.all == padding.left;
		return padding;
	}
	Utilities.getIntegerPixelValue=function (s)
	{
		return parseInt(s.replace("px",""));
	}
	Utilities.getThickness=function (s)
	{
		return s.replace("medium","1").replace("thin","1").replace("thick","3").replace("px","");
	}
	Utilities.setDisplayValue=function(el,val)
	{
		//alert(val);
		if (!val) {
		
			val='';
		}
		if (el) {
			if (!el.style) {
				el.style={};
			}
			el.style.display=val;
		}
	}
	
	Utilities.replaceClass = function(dom,toReplace,value) {
		var inp = dom.className;
		var arr = inp.split(" ");
		var res="";
		//alert(arr.length);
		for (var i=0;i<arr.length;i++) {
			var r = arr[i];
			if (r==toReplace) r=value;
			res+= (i>0 ? " ":"") + r;
		}
		dom.className = res;
	}
	
	
	Utilities.endsWith=function(s,v) {
	
		return s.substring(s.length-v.length)==v;
	}
	
	Utilities.setOpacity=function (obj,value)
	{
	
		obj.style.opacity = value/10;
		obj.style.filter = 'alpha(opacity=' + value*10 + ')';
	}
	Utilities.getCurrentStyle =function (obj)
	{
		if (obj.currentStyle) {
			var styleObj = obj.currentStyle; 
		} else if (Ext.isGecko) {
			var styleObj = document.defaultView.getComputedStyle(obj, null);
		} else { // if (Ext.isSafari) 
			var styleObj = document.defaultView.getComputedStyle(obj, null); 
		} 
		return styleObj;
	}

	Utilities.getCurrentStyleBorders=function (obj)
	{
		var border={};
		border.top={};
		border.right={};
		border.bottom={};
		border.left={};
		
		
		if (obj.currentStyle) {
			var styleObj = obj.currentStyle; 
			border.top.value=styleObj['borderTopWidth']+" "+styleObj['borderTopStyle']+" "+styleObj['borderTopColor'];
			border.right.value=styleObj['borderRightWidth']+" "+styleObj['borderRightStyle']+" "+styleObj['borderRightColor'];
			border.bottom.value=styleObj['borderBottomWidth']+" "+styleObj['borderBottomStyle']+" "+styleObj['borderBottomColor'];
			border.left.value=styleObj['borderLeftWidth']+" "+styleObj['borderLeftStyle']+" "+styleObj['borderLeftColor'];
			
			
			
			border.top.width=parseInt(Utilities.getThickness(styleObj['borderTopWidth']));
			border.right.width=parseInt(Utilities.getThickness(styleObj['borderRightWidth']));
			border.bottom.width=parseInt(Utilities.getThickness(styleObj['borderBottomWidth']));
			border.left.width=parseInt(Utilities.getThickness(styleObj['borderLeftWidth']));
			
		} else if (Ext.isGecko) {
			var styleObj = document.defaultView.getComputedStyle(obj, null); 

			border.top.value=styleObj['borderTopWidth']+" "+styleObj['borderTopStyle']+" "+styleObj['borderTopColor'];
			border.right.value=styleObj['borderRightWidth']+" "+styleObj['borderRightStyle']+" "+styleObj['borderRightColor'];
			border.bottom.value=styleObj['borderBottomWidth']+" "+styleObj['borderBottomStyle']+" "+styleObj['borderBottomColor'];
			border.left.value=styleObj['borderLeftWidth']+" "+styleObj['borderLeftStyle']+" "+styleObj['borderLeftColor'];

			border.top.width=parseInt(styleObj['borderTopWidth'].replace("px",""));
			border.right.width=parseInt(styleObj['borderRightWidth'].replace("px",""));
			border.bottom.width=parseInt(styleObj['borderBottomWidth'].replace("px",""));
			border.left.width=parseInt(styleObj['borderLeftWidth'].replace("px",""));
		}	else { // if (Ext.isSafari)
			var styleObj = document.defaultView.getComputedStyle(obj, null); 
			border.top.value=styleObj['border-top-width']+" "+styleObj['border-top-style']+" "+styleObj['border-top-color'];
			border.right.value=styleObj['border-right-width']+" "+styleObj['border-right-style']+" "+styleObj['border-right-color'];
			border.bottom.value=styleObj['border-bottom-width']+" "+styleObj['border-bottom-style']+" "+styleObj['border-bottom-color'];
			border.left.value=styleObj['border-left-width']+" "+styleObj['border-left-style']+" "+styleObj['border-left-color'];
			
			border.top.width=parseInt(styleObj['border-top-width'].replace("px",""));
			border.right.width=parseInt(styleObj['border-right-width'].replace("px",""));
			border.bottom.width=parseInt(styleObj['border-bottom-width'].replace("px",""));
			border.left.width=parseInt(styleObj['border-left-width'].replace("px",""));
			
		} 
		border.all=styleObj['border'];
		border.isUniform = border.all == border.top
			&&  border.all == border.right
			&&  border.all == border.bottom
			&&  border.all == border.left;
		return border;
	}
	Utilities.getBackgroundColor = function(obj)
	{
		var result='';
		if (obj.currentStyle) {
			var styleObj = obj.currentStyle; 
			result=styleObj['backgroundColor'];
			
			
		} else if (Ext.isSafari) {
			var styleObj = document.defaultView.getComputedStyle(obj, null); 
			result=styleObj['background-color'];
			
			
		} else if (Ext.isGecko) {
			var styleObj = document.defaultView.getComputedStyle(obj, null); 
			result=styleObj['backgroundColor'];
		}
		return result;
	}
	
	Utilities.getQueryParameter =function( parameterName ) {
		  var queryString = window.top.location.search.substring(1);
		  var parameterName = parameterName + "=";
		  if ( queryString.length > 0 ) {
		    begin = queryString.indexOf ( parameterName );
		    if ( begin != -1 ) {
		      begin += parameterName.length;
		      end = queryString.indexOf ( "&" , begin );
		        if ( end == -1 ) {
		        end = queryString.length
		      }
		      return unescape ( queryString.substring ( begin, end ) );
		    }
	  }
  return "null";
} 

	Utilities.replaceButtons = function(selector,params) {
		var btns = Ext.query(selector);
		var res = [];
		for (var j=0;j<btns.length;j++) {
			var x = Utilities.replaceButton(btns[j], params);
			res.push(x);
		}
		return res;
	}
	Utilities.replaceButton = function(button, params) {
		var b = Ext.get(button);
		if(b){
			var iconCls = 'btnIcon-rightArrow';
			var cls = 'customExtButton';
			var text = b.dom.innerHTML;
			var scale = 'custom';
			var tooltip = b.dom.getAttribute('title');
			if (params)
			{
				if (params.text) text = params.text;
				if (params.cls) cls = params.cls;
				if (params.iconCls) iconCls = params.iconCls;
				if (params.scale) scale  = params.scale;
				if (params.tooltip) tooltip = params.tooltip;
				
			}
        	var href = b.dom? b.dom.href : null;

        	b.dom.parentNode.oldButton = b.dom;
        		
	        var btn = new Ext.Button({
	        	text:text,
				tooltip:tooltip,
	            scale:scale,
	            iconCls:iconCls,
	            cls:cls,
	            renderTo:b.parent(),
	            href:href,
	            handler:b.dom.onclick});
	        b.dom.parentNode.newButton = btn;
	     	btn.el.replace(b);
	     	
	     	
	     	
	     	return btn;
	    } else return null;
	    
	}
	
	
	Ext.ImageButton = function(renderTo, config){
    Ext.ImageButton.superclass.constructor.call(this, renderTo, config);
	};

Ext.extend(Ext.ImageButton, Ext.Button, {
    render : function(renderTo){
    		this.disabledImgPath = this.disabledImgPath || this.imgPath;
    		var tplHTML = '<div><img src={imgPath}/></div>';
    		var tpl = new Ext.Template(tplHTML);
        var btn = tpl.append(renderTo, {imgPath: this.disabled ? this.disabledImgPath : this.imgPath, imgWidth: this.imgWidth || "", imgHeight: this.imgHeight || "", imgText: this.text || "", tooltip: this.tooltip || ""}, true);
        btn.on("click", this.onClick, this);        
        if(this.cls){
            btn.addClass(this.cls);
        }
        this.el = btn;
        if(this.hidden){
            this.hide();
        }
    },
    disable : function(newImgPath){
    	var replaceImgPath = newImgPath || this.disabledImgPath;
    	if (replaceImgPath)
    		this.el.dom.firstChild.src = replaceImgPath;
    	this.disabled = true;
    },
    enable : function(newImgPath){
    	var replaceImgPath = newImgPath || this.imgPath;
    	if (replaceImgPath)
    		this.el.dom.firstChild.src = replaceImgPath;
    	this.disabled = false;
    }
});