Ext.onReady(function() {

	//Hire Date
	var hire_date_field = new Ext.form.DateField({
		id:'hire_date_field',
		allowBlank: true
	});
	
	hire_date_field.render(Ext.get('calc_hire_date_div'));
	
	//Pay Type RadioGroup
	var pay_type_field = new Ext.form.RadioGroup({
		  hideLabel:true,
		  id:'payType',
	  	  columns:[60,60],
	  	  width:120,
	  	  items:[
	  	         {boxLabel: 'Salary', name: 'payType', inputValue: 'Slry', checked:true},
	  	         {boxLabel: 'Hourly', name: 'payType', inputValue: 'Hrly'}
	  	         ], 
	  	       listeners: {
	  	            'change': function(group, radio){ 	
						
						if(radio.inputValue == 'Slry'){
				
							emp_salary_label.setText( "Employee's Annual Salary:");
						}else if(radio.inputValue == 'Hrly'){
							emp_salary_label.setText("Employee's Hourly Rate:");
						}else if(radio.inputValue == 'Comm'){
							emp_salary_label.setText("Est. Monthly Commission:");
						}
	  	            }
				}
	    
	});
	pay_type_field.render(Ext.get('pay_type_div'));
	
	//Salary Description
	var emp_salary_label = new Ext.form.Label({
		text:"Employee's Annual Salary:",
		id:"emp_salary_label"
	});
	emp_salary_label.render(Ext.get('emp_salary_label_div'));
	
	//Salary Field
	var emp_salary = new Ext.form.NumberField({
		width:98,
		id:"emp_salary"
	});
	emp_salary.render(Ext.get('emp_salary_div'));
	
	//The Credit Value Field - Update this with the calculated value
	var credit_value = new Ext.form.Label({
		html:"",
		id:"credit_value"
	});
	
	credit_value.render(Ext.get('credit_val_div'));
	
	
	 //This function fires when the calculate Savings button is pressed
	var calcCredit = function(){
		val = 0;
		msg = '';
		wklyWages = 0;
		wages = 0;
		weeksInYear = 53;
		
		hire_date = hire_date_field.getValue();
		salary = emp_salary.getValue();
		if((hire_date ==  '') || (salary == '')){
			msg = 'Please complete all fields';
		}else{
		
			if(pay_type_field.getValue().inputValue == 'Slry'){
				wklyWages = salary / weeksInYear;
			}else{
				wklyWages = salary * 40;
			}
		
			if(hire_date.between(new Date('02/03/2010'), new Date('01/01/2011'))){
				//wages calculated between 03/18/20010 and 01/01/2011
				if(hire_date.between(new Date('03/18/2010'), new Date('01/01/2011'))){
					eligWeeks = weeksInYear - hire_date.getWeekOfYear();
				}else{
					eligWeeks = weeksInYear - (new Date('03/18/2010').getWeekOfYear());
				}
				
				//Calculate Wages
				wages = eligWeeks * wklyWages;
				
				//Account for the wage cap
				if(wages > 106800){
					wages = 106800
				}
				
				//Calculate the bonus
				bonus = wklyWages * 52 *.062;
				if(bonus > 1000){
				  bonus = 1000;	
				}
	
				//Calculate the credit value
				val = wages * .062 + bonus;
			
			
			}else{
				val = 0;
				msg = "The Employee was not hired during the eligibility time frame."
			}
		
		
		}
		
		//Format output
		
		val = Math.round(val).toString();
		if(val.length > 3){
		  val = val.substring(0,val.length -3)+","+val.substring(val.length -3)	;
		}
		credit_value.setText('$'+val);
		
		if(msg != ''){
		  Ext.Msg.alert('Error',msg);	
		}
	}
	
	//Register listener for calc_button 
	Ext.get('calc_button').addListener('click', calcCredit);

	
});

	