var InteriorCalculator = Class.create();
InteriorCalculator.prototype = {

txtRPft           : $( 'intCalc_txtRPft' ),
txtRPin           : $( 'intCalc_txtRPin' ),
txtWHft           : $( 'intCalc_txtWHft' ),
txtWHin           : $( 'intCalc_txtWHin' ),
txtSDoor          : $( 'intCalc_txtSDoor' ),
txtDDoor          : $( 'intCalc_txtDDoor' ),
txtSWindow        : $( 'intCalc_txtSWindow' ),
txtLWindow        : $( 'intCalc_txtLWindow' ),
rdoSurface_primer : $( 'intCalc_rdoSurface_primer' ),
rdoSurface_paint  : $( 'intCalc_rdoSurface_paint' ),
txtGallons        : $( 'intCalc_txtGallons' ),
btnTotal          : $( 'intCalc_btnTotal' ),

initialize : function() {

	Event.observe( this.btnTotal, 'click', this.validateForm.bind( this ), false );

}, // End initialize().

calculate : function() {

	var RPft = this.txtRPft.value;
	var RPin = this.txtRPin.value; 
	var WHft = this.txtWHft.value; 
	var WHin = this.txtWHin.value; 
	var SDoor = this.txtSDoor.value; 
	var DDoor = this.txtDDoor.value; 
	var SWindow = this.txtSWindow.value; 
	var LWindow = this.txtLWindow.value; 
	var Multiplier; 

	var InteriorTotal = (parseInt(RPft)*parseInt(WHft) + parseInt(RPin)*parseInt(WHin)/12) - (parseInt(SDoor)*20 + parseInt(DDoor)*40 + parseInt(SWindow)*10 + parseInt(LWindow)*25);

	if ( this.rdoSurface_primer.checked == true ) { 
	        Multiplier = 1; 
	} 
	else if ( this.rdoSurface_paint.checked == true ) { 
	        Multiplier = 2; 
	}       

	InteriorTotal = Math.ceil(parseInt(InteriorTotal)/375*parseInt(Multiplier)); 

	if(InteriorTotal < 0) InteriorTotal = 0;

	this.txtGallons.value = InteriorTotal; 

}, // End calculate().

allDigits : function( str ) {

	return this.inValidCharSet( str,"0123456789." );

}, // End allDigits().

inValidCharSet : function( str, charset ) {

	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++) {
		if (charset.indexOf(str.substr(i,1))<0) {
			result = false;
			break;
		}
	}	
	
	return result;

}, // End inValidCharSet().

validNum : function( formField, fieldLabel, required ) {

	var result = true;

	if (formField.value+"" == "" || formField.value == null) {
		formField.value = 0;
	}

 	if (result) {
 		if (!this.allDigits(formField.value)) {
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		} else {
			formField.value = Math.ceil(formField.value);
		}
	}

	return result;

}, // End validNum().

validInt : function( formField, fieldLabel, required ) {

	var result = true;

	if (required && !this.validRequired(formField,fieldLabel))
		result = false;
  
 	if (result) {
 		var num = parseInt(formField.value,10);
 		if (isNaN(num)) {
 			alert('Please enter a number for the "' + fieldLabel +'" field.');
			formField.focus();		
			result = false;
		}
	} 

	return result;

}, // End validInt().

validateForm : function() {

	// Track the submission by Google Analytics.
	pageTracker._trackPageview( '/products/tracking/interiorCalculatorSubmit' );

	// Track the submission by Floodlight Tag.
	new FloodlightTag( 'http://fls.doubleclick.net/activityi;src=2344780;type=landi542;cat=inter417;u1=Submitted;ord=' );

	if ( !this.validNum( this.txtRPft, "Room Perimeter (Feet)", false ) ) {
		return false;
	} else if ( !this.validNum( this.txtRPin, "Room Perimeter (Inches)", false ) ) {
		return false;
	} else if ( !this.validNum( this.txtWHft, "Wall Height (Feet)", false ) ) {
		return false;
	} else if ( !this.validNum( this.txtWHin, "Wall Height (Inches)", false ) ) {
		return false;
	} else if ( !this.validNum( this.txtSDoor, "Single Doors", false ) ) {
		return false;
	} else if ( !this.validNum( this.txtDDoor, "Double Doors", false ) ) {
		return false;
	} else if ( !this.validNum( this.txtSWindow, "Small Window", false ) ) {
		return false;
	} else if ( !this.validNum( this.txtLWindow, "Large Window", false ) ) {
		return false;
	} else {
		this.calculate();
		return true;
	}

} // End validateForm().

} // End class InteriorCalculator.

new InteriorCalculator();
