sglite.cmp.densityCalc = function(config) {

    /**
     * @cfg {String} config Config with the following properties
     * id, cls, html, el
     */
	//---------------------------------------------------------------------------------------
	// private
	//---------------------------------------------------------------------------------------
	var that = this;
	var config = config || {};
	var container;
	var dh = Ext.DomHelper;
	var validateEvent = "blur";
	var calFrm;
	var dnstyBod = {
		tag: 'div',
		id: 'bthFrms',
		cn: [{
			id: 'calcWrap',
		    tag: 'form',
		    cn: [    
			        {
					tag: 'div',
					id: 'pack',
					cn: [{
						tag: 'label',
						id: 'pLbl',
						html: 'Packaging: '
					},{
						tag: 'select',
						id: 'pInp',
						name: 'packaging ',
						cn:[{
							tag: 'option',
							value: 'Pallet',
							html: 'Pallet'
						},{
							tag: 'option',
							value: 'Box',
							html: 'Box'
						}]
						
					}]
					}, {
					tag: 'div',
					id: 'quant',
					cn: [{
						tag: 'label',
						id: 'qLbl',
						html: 'Pallet Quantity: '
					},{
						tag: 'input',
						id: 'qInp',
						name: 'quantity'	
					}]
					}, {
					tag: 'div',
					id: 'weight',
					cn: [{
						tag: 'label',
						id: 'wLbl',
						html: 'Weight: '
					},{
						tag: 'input',
						id: 'wInp',
						name: 'weight'
					}]
					}, {
					tag: 'div',
					id: 'leng',
					cn: [{
						tag: 'label',
						id: 'lLbl',
						html: 'Length(in): '
					},{
						tag: 'input',
						id: 'lInp',
						name: 'length'
					}]
					}, {
					tag: 'div',
					id: 'width',
					cn: [{
						tag: 'label',
						id: 'wiLbl',
						html: 'Width(in): '
					},{
						tag: 'input',
						id: 'wiInp',
						name: 'width'
					}]
					}, {
					tag: 'div',
					id: 'height',
					cn: [{
						tag: 'label',
						id: 'hLbl',
						html: 'Height(in)'
					},{
						tag: 'input',
						id: 'hInp',
						name: 'height'
					}]
					}]	
				}, {
				id: 'resultsWrap',
    			tag: 'form',
    			cn: [{
					tag: 'div',
					id: 'qFeet',
					cn: [{
						tag: 'label',
						html: 'Cubic Feet: '
					},{
						tag: 'input',
						id: 'qFtInp',
						readonly: 'true',
						name: 'cubicFeet'	
					}]
					}, {
					tag: 'div',
					id: 'density',
					cn: [{
						tag: 'label',
						html: 'Density: '
					},{
						tag: 'input',
						id: 'denInp',
						readonly: 'true',
						name: 'density'
					}]
					}, {
					tag: 'div',
					id: 'estClass',
					cn: [{
						tag: 'label',
						html: 'Freight Class: '
					},{
						tag: 'input',
						id: 'estClassInp',
						readonly: 'true',
						name: 'estimatedClass'
					}]
					}]
						
					}]
	};
	var densVals = {};
	var freightCls = {
	fields: [{
		name: "class",
		type: "float",
		sortType: "asFloat"
	},{
		name: "minDensity",
		type: "float",
		sortType: "asFloat"
	},{
		name: "maxDensity",
		type: "float",
		sortType: "asFloat"
	}],
	data: [
       [50,50,999999999999999],        
       [55,35,50],       
       [60,30,35],       
       [65,22.5,30],       
       [70,15,22.5],       
       [77.5,13,15],       
       [85,12,13],       
       [92.5,10.5,12],       
       [100,9,10.5],       
       [110,8,9],       
       [125,7,8],       
       [150,6,7],       
       [175,5,6],       
       [200,4,5],       
       [250,3,4],       
       [300,2,3],       
       [400,1,2],       
       [500,0,1] 
	 ]
};
	
//<option value="ABW">Aruba</option>
//Ext.get('pInp').dom.options[Ext.get('test').dom.selectedIndex].text
	//---------------------------------------------------------------------------------------
	// private methods
	//---------------------------------------------------------------------------------------
		function construct(){
			
			container = Ext.get(config.el);
			var bdy = dh.append(
				container,
				dnstyBod
			);
		/*	calFrm = new sglite.cmp.form({
				el: Ext.get("calcWrap"),
				id: "calcWrap",
				success: function(){},
				failure: function(){}
			}); */
			addEvts();
		};
		function addEvts(){
			
			var wrap = Ext.get('calcWrap');
			var vals = wrap.dom.childNodes;
			var sel, valId, denV;
			Ext.each(vals, function(itm, ix){
				sel = itm.childNodes[1];
				switch (sel.type) {
					case 'select-one':
						Ext.get(sel).on(validateEvent, function(evt, dom, opts){
							 that.getValues();
							});
					break;
					default:
						Ext.get(sel).on('keydown', function(evt, dom, opts){
							var key = evt.getCharCode();
							var regexp = /\b0*(4[7-9]|5[0-8]|[89]|9[5-9]|10[0-6])\b/;
							if(regexp.test(key)){
							}
							else{
								evt.stopEvent();
							}
							
														
						});
						Ext.get(sel).on(validateEvent, function(evt, dom, opts){
							if(Ext.isIE){
								that.getValuesIe();
							}
							else{
								that.getValues();
							}							
						});
				}
			});
			

		};
		
		 
	//---------------------------------------------------------------------------------------
	// public methods
	//---------------------------------------------------------------------------------------

	this.getValues = function(){
		var snout = Ext.select("form")
		var densCalcCheck = true;
		var frm = Ext.get('calcWrap')
        var fs = Ext.Ajax.serializeForm(frm.dom);
        var spit = Ext.urlDecode(fs);
		for (var key in spit){
			if(spit[key] == ''){
				densCalcCheck = false;
			}
		}
			if(densCalcCheck == true){
				that.resCalc(spit)
			}
	};
	this.getValuesIe = function(){
		var lugi = {};
		var densCalcCheck = true;
		lugi.quantity = Ext.get('qInp').dom.value;
		lugi.weight = Ext.get('wInp').dom.value;
		lugi.length = Ext.get('lInp').dom.value;
		lugi.width = Ext.get('wiInp').dom.value;
		lugi.height = Ext.get('hInp').dom.value;
		for (var key in lugi){
			if(lugi[key] == ''){
				densCalcCheck = false;
			};
		};
		if(densCalcCheck == true){
			that.resCalc(lugi)
		};
	};

	this.resCalc = function(vObj){
		var densityVars = vObj;
		var dnstyFormula = {
			cubicFeet: (parseInt(densityVars.length, 10) * parseInt(densityVars.width, 10) * parseInt(densityVars.height, 10)) / 1728,
			weight: parseInt(densityVars.quantity, 10) * parseInt(densityVars.weight, 10)
		};
		dnstyFormula.density = dnstyFormula.weight/dnstyFormula.cubicFeet;
		Ext.each(freightCls.data, function(itm, ix){
			if(dnstyFormula.density > itm[1] && dnstyFormula.density < itm[2]){
				dnstyFormula.estClass = itm[0]; 
			}
		});
		that.showTotals(dnstyFormula);
	};
	
	this.showTotals = function(totals){
		var rslts = totals;
		var cuFt = Ext.get('qFtInp');
		var dnsty = Ext.get('denInp');
		var estClass = Ext.get('estClassInp');
		cuFt.dom.value = sglite.util.number.rnd(rslts.cubicFeet, 1);
		dnsty.dom.value = sglite.util.number.rnd(rslts.density, 1);
		estClass.dom.value = rslts.estClass;
		var warDh = Ext.DomHelper;
		var estNoticeNode = Ext.get('resultsWrap');
		estNoticeNode.fadeOut({ endOpacity: .25, duration: 10}).fadeIn({
    		stopFx: true,
			endOpacity: 1, //can be any value between 0 and 1 (e.g. .5)
			easing: 'easeOut',
    		duration: .5
			});
		var snoopy = {
			tag: 'div',
			cls: 'estWarn',
			id: 'estimationW',
			html: 'Results are Estimates'
		};
		if(Ext.get('estimationW')){
			
		}
		else{
			var bdy = warDh.insertBefore(
				estNoticeNode,
				snoopy
			);
			var estNode = Ext.get('estimationW');
			estNode.fadeOut({ endOpacity: .25, duration: 2}).defer(2);		
		}


		
		
		
	};

	//---------------------------------------------------------------------------------------
	// public properties
	//---------------------------------------------------------------------------------------
	//---------------------------------------------------------------------------------------
	// constructor work
	//---------------------------------------------------------------------------------------
	// before constructor, add element to dom
	
	sglite.cmp.densityCalc.superclass.constructor.call(this, config);
	construct();
};// end object
Ext.extend(sglite.cmp.densityCalc, Ext.Element);
