	Ext.override(Ext.form.Field, {
	    fireKey : function(e) {
	        if(((Ext.isIE && e.type == 'keydown') || e.type == 'keypress') && e.isSpecialKey()) {
	            this.fireEvent('specialkey', this, e);
	        }
	        else {
	            this.fireEvent(e.type, this, e);
	        }
	    }
	  , initEvents : function() {
	        this.el.on("keyup", this.fireKey, this);
	
	        // reference to original value for reset
	        this.originalValue = this.getValue();
	    }
	});
	Ext.form.NumberField.prototype.getAllowed=function(){
		if(this.allowed===undefined){
			var allowed = this.baseChars+'';
	        if(this.allowDecimals){
	            allowed += this.decimalSeparator;
	        }
	        if(this.allowNegative){
	            allowed += "-";
	        }
			this.allowed = allowed;
		}
		return this.allowed;
	}
	/***** SETUP VALUES */
	var Calculation = {
		xValue:0,
		yValue:0,
		lCost:0,
		finishValue:0,
		blindValue:0,
		laserValue:0,
		mechanicalValue:0,
		viaValue:0,
		lowValue:null,
		highValue:null,
		prodValue:null,
		getLowValue:function(){
			if(this.lowValue===null){
				this.lowValue = document.getElementById('low');
			}
			return this.lowValue;
		},
		getHighValue:function(){
			if(this.highValue===null){
				this.highValue = document.getElementById('high');
			}
			return this.highValue;
		},
		getProdValue:function(){
			if(this.prodValue===null){
				this.prodValue = document.getElementById('prod');
			}
			return this.prodValue;
		},
		setXValue:function(i){
			this.xValue = i;
			this.calculate();
		},
		setYValue:function(i){
			this.yValue = i;
			this.calculate();
		},
		setLValue:function(i){
			this.lCost = i;
			this.calculate();
		},
		setFinishValue:function(i){
			this.finishValue = i;
			this.calculate();
		},
		setBlindValue:function(i){
			this.blindValue = i;
			this.calculate();
		},
		setLaserValue:function(i){
			this.laserValue = i;
			this.calculate();
		},
		setMechanicalValue:function(i){
			this.mechanicalValue = i;
			this.calculate();
		},
		setViaValue:function(i){
			this.viaValue = i;
			this.calculate();
		},
		getOffset:function(){
			var r=Math.floor(Math.random()*7);
			var z=(97+r)/100;
			return z;
		},
		calculate:function(){
			var o = this.getOffset();
			var len = this.yValue*this.xValue;
			var totalLen = (160/(len))*25;
			var t = this.yValue*this.xValue*this.lCost*this.finishValue*this.blindValue*this.laserValue*this.mechanicalValue*this.viaValue;
			var x = t*o;
			var tLow = Ext.util.Format.usMoney(x*.75);
			var tHigh = Ext.util.Format.usMoney(x*1.25);
			this.getLowValue().innerHTML = tLow;
			this.getHighValue().innerHTML = tHigh;
			this.getProdValue().innerHTML = (totalLen == 'Infinity') ? 0 : Math.round(totalLen) ;
		}
	}
	var xDimension = new Ext.form.NumberField({
		allowBlank:false,
		allowNegative:false,
		fieldLabel:'Your Part\'s X Dimension',
		name:'xDimension',
		value:0,
		maxValue:22,
		validator:function(val){
			if(val>16&&yDimension.getValue()>16){
				return false;
			}
			return true;
		},
		listeners:{
			keyup:function(f,e){
				var k = e.getKey();
				var c = e.getCharCode();
				if(this.getAllowed().indexOf(String.fromCharCode(c))>-1 || k == e.DELETE || k == e.BACKSPACE){
					Calculation.setXValue(this.getValue());
				}
			},
			valid:function(){
					if (yDimension.isValid(false)) {
						errorField.el.setVisible(false, true);
					}
			},
			invalid:function(){
				if (!errorField.el.visible) {
					var h = resultField.el.getHeight() + 8;
					errorField.setPosition(resultField.el.getX(), resultField.el.getY() + h);
					errorField.el.setVisible(true, true);
				}
			}
		}
	});
	var yDimension = new Ext.form.NumberField({
		allowBlank:false,
		allowNegative:false,
		fieldLabel:'Your Part\'s Y Dimension',
		name:'yDimension',
		value:0,
		maxValue:22,
		validator:function(val){
			if(val>16&&xDimension.getValue()>16){
				return false;
			}
			return true;
		},
		listeners:{
			keyup:function(f,e){
				var k = e.getKey();
				var c = e.getCharCode();
				if(this.getAllowed().indexOf(String.fromCharCode(c))>-1 || k == e.DELETE || k == e.BACKSPACE){
					Calculation.setYValue(this.getValue());
				}
			},
			valid:function(){
				if (xDimension.isValid(false)) {
					errorField.el.setVisible(false, true);
				}
			},
			invalid:function(){
				if (!errorField.el.visible) {
					var h = resultField.el.getHeight() + 8;
					errorField.setPosition(resultField.el.getX(), resultField.el.getY() + h);
					errorField.el.setVisible(true, true);
				}
			}
		}
	});
	var layerCount = new Ext.form.NumberField({
		allowBlank:false,
		fieldLabel:'Your Part\'s Layer Count',
		name:'layerCount',
		value:0,
		listeners:{
			keyup:function(f,e){
				var k = e.getKey();
				var c = e.getCharCode();
				if(this.getAllowed().indexOf(String.fromCharCode(c))>-1 || k == e.DELETE || k == e.BACKSPACE){
					var i = this.getValue();
					var cost = 0;
					if(i==1){
						cost = 1.25;
					} else if (i==2){
						cost = 0.62;
					} else if (i==3){
						cost = 0.57;
					} else if (i==4){
						cost = 0.52;
					} else if (i>4&&i<11){
						cost = 0.42;
					} else if (i>10) {
						cost = 0.41;
					}
					Calculation.setLValue(cost*i);
				}
			}
		}
	});
	var total = new Ext.form.TextField({
		fieldLabel:'Total',
		name:'total',
		disabled:true,
		value:'0'
	});
	var finalFinish = new Ext.form.FieldSet({
		hideBorders:true,
		style: 'border:none',
		labelWidth:200,
		autoHeight:true,
		bodyStyle:'margin:-10px',
		defaults:{
			xtype:'radio',
			labelSeparator:'',
			name:'finalFinish',
			listeners:{
				check:function(radio,checked){
					if (checked) {
						switch(this.value){
							case 'Electroplate Nickel/Gold':
								Calculation.setFinishValue(1.05);
								break;
							case 'Soft Wire Bondable Gold':
								Calculation.setFinishValue(1.1);
								break;
							default:
								Calculation.setFinishValue(1);
						}
					}
				}
			}
		},
		items:[
			{
				value:'ENIG',
				boxLabel:'ENIG',
				labelSeparator:':',
				fieldLabel:'Final Finish',
				inputValue:'ENIG',
				checked:true
			},
			{
				value:'OSP',
				inputValue:'OSP',
				boxLabel:'OSP'
			},
			{
				value:'HASL',
				inputValue:'HASL',
				boxLabel:'HASL'
			},
			{
				value:'Immersion Silver',
				inputValue:'Immersion Silver',
				boxLabel:'Immersion Silver'
			},
			{
				value:'Electroplate Nickel/Gold',
				inputValue:'Electroplate Nickel/Gold',
				boxLabel:'Electroplate Nickel/Gold'
			},
			{
				value:'Soft Wire Bondable Gold',
				inputValue:'Soft Wire Bondable Gold',
				boxLabel:'Soft Wire Bondable Gold'
			},
			{
				value:'Other Finish',
				inputValue:'Other Finish',
				boxLabel:'Other Finish'
			}
			
		]
	});
	var blindField = new Ext.form.FieldSet({
		hideBorders:true,
		style:'border:none;',
		labelWidth:200,
		autoHeight:true,
		bodyStyle:'margin:-10px;',
		defaults:{
			xtype:'radio',
			labelSeparator:'',
			name:'blind',
			listeners:{
				check:function(radio,checked){
					if(checked){
						var i = 1;
						if(radio.value==='Yes'){
							i = 1.05;
						}
						Calculation.setBlindValue(i);
					}
				}
			}
		},
		items:[
			{
				value:'Yes',
				boxLabel:'Yes',
				labelSeparator:':',
				fieldLabel:'Blind or Buried Vias with Conventional Drilling?',
				inputValue:'Yes',
				labelStyle:'position:absolute;width:200px'
			},
			{
				value:'No',
				boxLabel:'No',
				inputValue:'No',
				checked:true
			}
		]
	});
	var laserField = new Ext.form.FieldSet({
		hideBorders:true,
		style:'border:none;',
		labelWidth:200,
		autoHeight:true,
		bodyStyle:'margin:-10px;',
		defaults:{
			xtype:'radio',
			labelSeparator:'',
			name:'laser',
			listeners:{
				check:function(radio,checked){
					if(checked){
						var i = 1;
						if(radio.value==='Yes'){
							i = 1.05;
						}
						Calculation.setLaserValue(i);
					}
				}
			}
		},
		items:[
			{
				value:'Yes',
				boxLabel:'Yes',
				labelSeparator:':',
				fieldLabel:'Laser Vias?',
				inputValue:'Yes'
			},
			{
				value:'No',
				boxLabel:'No',
				inputValue:'No',
				checked:true
			}
		]
	});
	var mechanicalField = new Ext.form.FieldSet({
		hideBorders:true,
		style:'border:none;',
		labelWidth:200,
		autoHeight:true,
		bodyStyle:'margin:-10px;',
		defaults:{
			xtype:'radio',
			labelSeparator:'',
			name:'mechan',
			listeners:{
				check:function(radio,checked){
					if(checked){
						var i = 1;
						if(radio.value==='Yes'){
							i = 1.05;
						}
						Calculation.setMechanicalValue(i);
					}
				}
			}
		},
		items:[
			{
				value:'Yes',
				boxLabel:'Yes',
				labelSeparator:':',
				fieldLabel:'Mechanical Dimensioning Less Than +/- 0.004"?',
				inputValue:'Yes',
				labelStyle:'position:absolute;width:200px'
			},
			{
				value:'No',
				boxLabel:'No',
				inputValue:'No',
				checked:true
			}
		]
	});
	var viaField = new Ext.form.FieldSet({
		hideBorders:true,
		style:'border:none;',
		labelWidth:200,
		autoHeight:true,
		bodyStyle:'margin:-10px;',
		defaults:{
			xtype:'radio',
			labelSeparator:'',
			name:'via',
			listeners:{
				check:function(radio,checked){
					if(checked){
						var i = 1;
						if(radio.value==='Yes'){
							i = 1.05;
						}
						Calculation.setViaValue(i);
					}
				}
			}
		},
		items:[
			{
				value:'Yes',
				boxLabel:'Yes',
				labelSeparator:':',
				fieldLabel:'Via Filling?',
				inputValue:'Yes'
			},
			{
				value:'No',
				boxLabel:'No',
				inputValue:'No',
				checked:true
			}
		]
	});
	var resultField;
	var errorField;
	Ext.onReady(function(){
		resultField = new Ext.Panel({
			frame:true,
			border:true,
			floating:true,
			title:'Result',
			width:300,
			renderTo:'result',
			html:'Your part in the configuration above should run between <span id="low" class="result">0</span> and <span id="high" class="result">0</span> for low level production, which for your part would be around <span id="prod" class="result">0</span> pieces.',
			listeners:{
				render:function(){
					var target = Ext.get('form');
					var r = this;
					var x = target.getX()+420;
					var y = target.getY();
					r.setPosition(x,y);
					var el = Ext.isIE ? Ext.getBody() : window ;
					Ext.EventManager.addListener(el,'scroll',function(evt){
						if ((y - document.documentElement.scrollTop) < 0) {
							r.setPosition( x, document.documentElement.scrollTop+10 );
						} else {
							r.setPosition(x,y);
						}
					});
				}
			}
		});
		errorField = new Ext.Panel({
			frame:true,
			floating:true,
			border:true,
			hidden:true,
			title:'<span style="color:#CC624C">Error</span>',
			renderTo:'error',
			iconCls:'errorIcon',
			width:300,
			html:'Our largest panel size is 18" by 24" giving us a maximum usable area of 16" by 22" please call us for further assistance at 952-888-7900.',
			listeners:{
				render: function(){
					var r = this;
					var target = Ext.get('form');
					var x = target.getX() + 420;
					var y = target.getY();
					var el = Ext.isIE ? Ext.getBody() : window;
					var h = resultField.el.getHeight() + 18;
					r.setPosition(x, y + h - 8);
					Ext.EventManager.addListener(el, 'scroll', function(evt){
						if ((y - document.documentElement.scrollTop) < 0) {
							r.setPosition(x, document.documentElement.scrollTop + h);
						}
						else {
							r.setPosition(x, y + h - 8);
						}
					});
				}
			}
		});
		var frm = new Ext.form.FormPanel({
			onSubmit:Ext.emptyFn,
			submit:function(){
				this.getForm().getEl().dom.action = "formmail.php";
				this.getForm().getEl().dom.submit();
			},
			renderTo:'form',
			monitorValid:true,
			frame:true,
			width:400,
			title:'Cost Estimator',
			defaults:{
				labelStyle:'width:200px'
			},
			items:[
				{
					html:'We put together a quick and easy tool to help you get an idea of what your design might cost as a rigid flex PWB.  It is meant to provide you with a budgetary number that will help you know whether or not rigid flex pricing will work within your project\'s scope.<br /><br /><span style="font-size:10px;">This tool from PCi is meant only as a cost estimator, and cannot replicate the engineering discipline used to quote a board.  If you would like an actual quotation, please e-mail your fab package with print and gerber files to info@PrintedCircuits.com.<br /><br /></span>'
				},
				{
					xtype:'hidden',
					name:'TO',
					value:'bburns@printedcircuits.coms'
				},
				{
					xtype:'hidden',
					name:'SUBJECT',
					value:'Cost Estimator'
				},
				{
					xtype:'hidden',
					name:'REDIRECT',
					value:'thankyou.htm'
				},
				{
					xtype:'hidden',
					name:'TEMPLATE',
					value:'pci/estimator.template'
				},
				{
					xtype:'textfield',
					allowBlank:false,
					fieldLabel:'Your Name',
					name:'yourName'
				},
				{
					xtype:'textfield',
					fieldLabel:'Your Company Name',
					name:'companyName'
				},
				{
					xtype:'textfield',
					fieldLabel:'Your Phone Number',
					name:'phoneNumber'
				},
				{
					xtype:'textfield',
					allowBlank:false,
					fieldLabel:'Your Email Address',
					name:'emailAddress'
				},
				xDimension,
				yDimension,
				layerCount,
				finalFinish,
				blindField,
				laserField,
				mechanicalField,
				viaField
			],
			buttons:[
				{
					xtype:'button',
					text:'Submit',
					formBind:true,
					handler:function(e){
						frm.submit();
					}
				}
			]
		});
	});