/*
--------------------------------------------------------------------------------
 JavaScript: Tire Selector Entry Form.
 Requires: JQuery
--------------------------------------------------------------------------------
*/
var TireSelectorEntryForm = Class.extend( {

// URL to the application that generates the Tire Selector AJAX data.
//
AJAX_URL : '/cfmx/web/goodyeartiresel/components/tiresel.cfc',

// Text used for the first item in each drop down list.
//
LIST_LABEL : "Select\u2026",

// Page elements.
//
VEHICLE_FORM : null,
VEHICLE_YEAR_SELECT : null,
VEHICLE_YEAR_PLUS_CHECK : null,
VEHICLE_MAKE_SELECT : null,
VEHICLE_MODEL_SELECT : null,
VEHICLE_VERSION_CONTAINER : null,
VEHICLE_VERSION_SELECT : null,
VEHICLE_OPTION_CONTAINER : null,
VEHICLE_OPTION_SELECT : null,
VEHICLE_SUBMIT : null,
SIZE_FORM : null,
SIZE_WIDTH_SELECT : null,
SIZE_RATIO_SELECT : null,
SIZE_DIAMETER_SELECT : null,
SIZE_SUBMIT : null,
TIRE_NAME_SELECT : null,
TIRE_NAME_SUBMIT : null,

// Reference the the Tire Selector tabs.
//
tabs : null,

// Initialize the Tire Selector.
//
init : function( elements ) {

	this.VEHICLE_FORM =              $( '#' + elements.vehicleForm )[ 0 ];
	this.VEHICLE_YEAR_SELECT =       $( '#' + elements.vehicleYearSelect )[ 0 ];
	this.VEHICLE_YEAR_PLUS_CHECK =   $( '#' + elements.vehicleYearPlusCheck )[ 0 ];
	this.VEHICLE_MAKE_SELECT =       $( '#' + elements.vehicleMakeSelect )[ 0 ];
	this.VEHICLE_MODEL_SELECT =      $( '#' + elements.vehicleModelSelect )[ 0 ];
	this.VEHICLE_VERSION_CONTAINER = $( '#' + elements.vehicleVersionContainer )[ 0 ];
	this.VEHICLE_VERSION_SELECT =    $( '#' + elements.vehicleVersionSelect )[ 0 ];
	this.VEHICLE_OPTION_CONTAINER =  $( '#' + elements.vehicleOptionContainer )[ 0 ];
	this.VEHICLE_OPTION_SELECT =     $( '#' + elements.vehicleOptionSelect )[ 0 ];
	this.VEHICLE_SUBMIT =            $( '#' + elements.vehicleSubmit )[ 0 ];
	this.SIZE_FORM =                 $( '#' + elements.sizeForm )[ 0 ];
	this.SIZE_METRIC_SELECT =        $( '#' + elements.sizeMetricSelect )[ 0 ];
	this.SIZE_WIDTH_SELECT =         $( '#' + elements.sizeWidthSelect )[ 0 ];
	this.SIZE_RATIO_SELECT =         $( '#' + elements.sizeRatioSelect )[ 0 ];
	this.SIZE_DIAMETER_SELECT =      $( '#' + elements.sizeDiameterSelect )[ 0 ];
	this.SIZE_SUBMIT =               $( '#' + elements.sizeSubmit )[ 0 ];
	this.TIRE_NAME_SELECT =          $( '#' + elements.tireNameSelect )[ 0 ];
	this.TIRE_NAME_SUBMIT =          $( '#' + elements.tireNameSubmit )[ 0 ];

	var thisTSEObj = this;

	// Vehicle drop downs.
	if ( this.VEHICLE_YEAR_SELECT != null ) {
		$( this.VEHICLE_YEAR_SELECT ).change(
			function( e ) {
				thisTSEObj.onYearChange();
			}
		)
	} // End if.
	if ( this.VEHICLE_YEAR_PLUS_CHECK != null ) {
		$( this.VEHICLE_YEAR_PLUS_CHECK ).click(
			function( e ) {
				thisTSEObj.onYearChange();
			}
		)
	} // End if.
	if ( this.VEHICLE_MAKE_SELECT != null ) {
		$( this.VEHICLE_MAKE_SELECT ).change(
			function( e ) {
				thisTSEObj.onMakeChange();
			}
		)
	} // End if.
	if ( this.VEHICLE_MODEL_SELECT != null ) {
		$( this.VEHICLE_MODEL_SELECT ).change(
			function( e ) {
				thisTSEObj.onModelChange();
			}
		)
	} // End if.
	if ( this.VEHICLE_VERSION_SELECT != null ) {
		$( this.VEHICLE_VERSION_SELECT ).change(
			function( e ) {
				thisTSEObj.onVersionChange();
			}
		)
	} // End if.
	if ( this.VEHICLE_OPTION_SELECT != null ) {
		$( this.VEHICLE_OPTION_SELECT ).change(
			function( e ) {
				thisTSEObj.onOptionChange();
			}
		)
	} // End if.

	// Tire size drop downs.
	if ( this.SIZE_METRIC_SELECT != null ) {
		$( this.SIZE_METRIC_SELECT ).change(
			function( e ) {
				thisTSEObj.onSizeMetricChange();
			}
		)
	} // End if.
	if ( this.SIZE_WIDTH_SELECT != null ) {
		$( this.SIZE_WIDTH_SELECT ).change(
			function( e ) {
				thisTSEObj.onSizeWidthChange();
			}
		)
	} // End if.
	if ( this.SIZE_RATIO_SELECT != null ) {
		$( this.SIZE_RATIO_SELECT ).change(
			function( e ) {
				thisTSEObj.onSizeRatioChange();
			}
		)
	} // End if.
	if ( this.SIZE_DIAMETER_SELECT != null ) {
		$( this.SIZE_DIAMETER_SELECT ).change(
			function( e ) {
				thisTSEObj.onSizeDiameterChange();
			}
		)
	} // End if.

	// Tire name drop down.
	if ( this.TIRE_NAME_SELECT != null ) {
		$( this.TIRE_NAME_SELECT ).change(
			function( e ) {
				thisTSEObj.onTireNameChange();
			}
		)
	} // End if.

	this.initForms();

}, // End init().

initForms : function() {

	// Reset the forms.
	this.resetVehicleForm();
	this.resetSizeForm();

	var thisTSEObj = this;

	// Populate the years field.
	$.getJSON(
		this.AJAX_URL,
		{ method        : 'getYear',
		  returnformat  : "json"
		},
		function( json ) { thisTSEObj.populateYears( json ); }
	);

	// Populate the size metric field.
	$.getJSON(
		this.AJAX_URL,
		{ method        : 'getMetric',
		  returnformat  : "json"
		},
		function( json ) { thisTSEObj.populateSizeMetrics( json ); }
	);

	// Populate the tire name field.
	$.getJSON(
		this.AJAX_URL,
		{ method        : 'getTireList',
		  returnformat  : "json"
		},
		function( json ) { thisTSEObj.populateTireNames( json ); }
	);

}, // End initForms().

resetVehicleForm : function() {

	if ( this.VEHICLE_YEAR_PLUS_CHECK != null ) {
		this.VEHICLE_YEAR_PLUS_CHECK.disabled = true;
	} // End if.

	if ( this.VEHICLE_MAKE_SELECT != null ) {
		this.VEHICLE_MAKE_SELECT.disabled = true;
		this.clearOptions( this.VEHICLE_MAKE_SELECT );
		this.addOption( this.VEHICLE_MAKE_SELECT, this.LIST_LABEL, "" );
	} // End if.

	if ( this.VEHICLE_MODEL_SELECT != null ) {
		this.VEHICLE_MODEL_SELECT.disabled = true;
		this.clearOptions( this.VEHICLE_MODEL_SELECT );
		this.addOption( this.VEHICLE_MODEL_SELECT, this.LIST_LABEL, "" );
	} // End if.

	if ( this.VEHICLE_VERSION_SELECT != null ) {
		this.VEHICLE_VERSION_SELECT.disabled = true;
		this.clearOptions( this.VEHICLE_VERSION_SELECT );
		this.addOption( this.VEHICLE_VERSION_SELECT, this.LIST_LABEL, "" );
	} // End if.

	if ( this.VEHICLE_OPTION_SELECT != null ) {
		this.VEHICLE_OPTION_SELECT.disabled = true;
		this.clearOptions( this.VEHICLE_OPTION_SELECT );
		this.addOption( this.VEHICLE_OPTION_SELECT, this.LIST_LABEL, "" );
	} // End if.

	this.VEHICLE_VERSION_CONTAINER.className = 'tsm_containerOff';
	this.VEHICLE_OPTION_CONTAINER.className = 'tsm_containerOff';

	if ( this.VEHICLE_FORM != null ) this.VEHICLE_SUBMIT.disabled = true;

	if ( this.VEHICLE_FORM != null ) this.VEHICLE_FORM.reset();

}, // End resetVehicleForm().

resetSizeForm : function() {

	if ( this.SIZE_WIDTH_SELECT != null ) {
		this.SIZE_WIDTH_SELECT.disabled = true;
		this.clearOptions( this.SIZE_WIDTH_SELECT );
		this.addOption( this.SIZE_WIDTH_SELECT, this.LIST_LABEL, "" );
	} // End if.

	if ( this.SIZE_RATIO_SELECT != null ) {
		this.SIZE_RATIO_SELECT.disabled = true;
		this.clearOptions( this.SIZE_RATIO_SELECT );
		this.addOption( this.SIZE_RATIO_SELECT, this.LIST_LABEL, "" );
	} // End if.

	if ( this.SIZE_DIAMETER_SELECT != null ) {
		this.SIZE_DIAMETER_SELECT.disabled = true;
		this.clearOptions( this.SIZE_DIAMETER_SELECT );
		this.addOption( this.SIZE_DIAMETER_SELECT, this.LIST_LABEL, "" );
	} // End if.

	if ( this.SIZE_FORM != null ) this.SIZE_FORM.reset();

}, // End resetSizeForm().

onYearChange : function() {

	this.VEHICLE_MAKE_SELECT.disabled = true;
	this.clearOptions( this.VEHICLE_MAKE_SELECT );
	this.addOption( this.VEHICLE_MAKE_SELECT, this.LIST_LABEL, "" );

	this.VEHICLE_MODEL_SELECT.disabled = true;
	this.clearOptions( this.VEHICLE_MODEL_SELECT);
	this.addOption( this.VEHICLE_MODEL_SELECT, this.LIST_LABEL, "" );

	this.VEHICLE_VERSION_SELECT.disabled = true;
	this.clearOptions( this.VEHICLE_VERSION_SELECT);
	this.addOption( this.VEHICLE_VERSION_SELECT, this.LIST_LABEL, "" );

	this.VEHICLE_OPTION_SELECT.disabled = true;
	this.clearOptions( this.VEHICLE_OPTION_SELECT);
	this.addOption( this.VEHICLE_OPTION_SELECT, this.LIST_LABEL, "" );

	this.VEHICLE_VERSION_CONTAINER.className = 'tsm_containerOff';
	this.VEHICLE_OPTION_CONTAINER.className = 'tsm_containerOff';
	this.VEHICLE_SUBMIT.disabled = true;

	if ( this.VEHICLE_YEAR_SELECT.selectedIndex == 0 ) {
		return alert( "Please select a year." );
	} else {
		this.VEHICLE_YEAR_PLUS_CHECK.disabled = false;
	}

	var thisTSEObj = this;
	$.getJSON(
		this.AJAX_URL,
		{ method        : 'getMake',
		  year          : this.VEHICLE_YEAR_SELECT.value,
		  yearplusminus : ( this.VEHICLE_YEAR_PLUS_CHECK.checked ) ? '1' : '0',
		  returnformat  : "json"
		},
		function( json ) { thisTSEObj.populateMakes( json ); }
	);

}, // End onYearChange().

onMakeChange : function() {

	this.VEHICLE_MODEL_SELECT.disabled = true;
	this.clearOptions( this.VEHICLE_MODEL_SELECT);
	this.addOption( this.VEHICLE_MODEL_SELECT, this.LIST_LABEL, "" );

	this.VEHICLE_VERSION_SELECT.disabled = true;
	this.clearOptions( this.VEHICLE_VERSION_SELECT);
	this.addOption( this.VEHICLE_VERSION_SELECT, this.LIST_LABEL, "" );

	this.VEHICLE_OPTION_SELECT.disabled = true;
	this.clearOptions( this.VEHICLE_OPTION_SELECT);
	this.addOption( this.VEHICLE_OPTION_SELECT, this.LIST_LABEL, "" );

	this.VEHICLE_VERSION_CONTAINER.className = 'tsm_containerOff';
	this.VEHICLE_OPTION_CONTAINER.className = 'tsm_containerOff';
	this.VEHICLE_SUBMIT.disabled = true;

	if ( this.VEHICLE_MAKE_SELECT.selectedIndex == 0 ) {
		return alert( "Please select a make." );
	}

	var thisTSEObj = this;
	$.getJSON(
		this.AJAX_URL,
		{ method        : 'getModel',
		  year          : this.VEHICLE_YEAR_SELECT.value,
		  yearplusminus : ( this.VEHICLE_YEAR_PLUS_CHECK.checked ) ? '1' : '0',
		  make          : this.VEHICLE_MAKE_SELECT.value,
		  returnformat  : "json"
		},
		function( json ) { thisTSEObj.populateModels( json ); }
	);

}, // End onMakeChange().

onModelChange : function() {

	this.VEHICLE_VERSION_SELECT.disabled = true;
	this.clearOptions( this.VEHICLE_VERSION_SELECT);
	this.addOption( this.VEHICLE_VERSION_SELECT, this.LIST_LABEL, "" );

	this.VEHICLE_OPTION_SELECT.disabled = true;
	this.clearOptions( this.VEHICLE_OPTION_SELECT);
	this.addOption( this.VEHICLE_OPTION_SELECT, this.LIST_LABEL, "" );

	this.VEHICLE_VERSION_CONTAINER.className = 'tsm_containerOff';
	this.VEHICLE_OPTION_CONTAINER.className = 'tsm_containerOff';
	this.VEHICLE_SUBMIT.disabled = true;

	if ( this.VEHICLE_MODEL_SELECT.selectedIndex == 0 ) {
		return alert( "Please select a model." );
	} else {
		this.VEHICLE_SUBMIT.disabled = false;
	}

	var thisTSEObj = this;
	$.getJSON(
		this.AJAX_URL,
		{ method        : 'getVersion',
		  year          : this.VEHICLE_YEAR_SELECT.value,
		  yearplusminus : ( this.VEHICLE_YEAR_PLUS_CHECK.checked ) ? '1' : '0',
		  make          : this.VEHICLE_MAKE_SELECT.value,
		  model         : this.VEHICLE_MODEL_SELECT.value,
		  returnformat  : "json"
		},
		function( json ) { thisTSEObj.populateVersions( json ); }
	);

}, // End onModelChange().

onVersionChange : function() {

	this.VEHICLE_OPTION_SELECT.disabled = true;
	this.clearOptions( this.VEHICLE_OPTION_SELECT );
	this.addOption( this.VEHICLE_OPTION_SELECT, this.LIST_LABEL, "" );

	this.VEHICLE_OPTION_CONTAINER.className = 'tsm_containerOff';

	if ( this.VEHICLE_VERSION_SELECT.selectedIndex == 0 ) {
		return alert( "Please select a version." );
	}

	var thisTSEObj = this;
	$.getJSON(
		this.AJAX_URL,
		{ method        : 'getOption',
		  year          : this.VEHICLE_YEAR_SELECT.value,
		  yearplusminus : ( this.VEHICLE_YEAR_PLUS_CHECK.checked ) ? '1' : '0',
		  make          : this.VEHICLE_MAKE_SELECT.value,
		  model         : this.VEHICLE_MODEL_SELECT.value,
		  version       : this.VEHICLE_VERSION_SELECT.value,
		  returnformat  : "json"
		},
		function( json ) { thisTSEObj.populateOptions( json ); }
	);

}, // End onVersionChange().

onOptionChange : function() {

	if ( this.VEHICLE_OPTION_SELECT.selectedIndex == 0 ) {
		return alert( "Please select an option." );
	}

}, // End onOptionChange().

onSizeMetricChange : function() {

	this.SIZE_WIDTH_SELECT.disabled = true;
	this.clearOptions( this.SIZE_WIDTH_SELECT );
	this.addOption( this.SIZE_WIDTH_SELECT, this.LIST_LABEL, "" );

	this.SIZE_RATIO_SELECT.disabled = true;
	this.clearOptions( this.SIZE_RATIO_SELECT );
	this.addOption( this.SIZE_RATIO_SELECT, this.LIST_LABEL, "" );

	this.SIZE_DIAMETER_SELECT.disabled = true;
	this.clearOptions( this.SIZE_DIAMETER_SELECT );
	this.addOption( this.SIZE_DIAMETER_SELECT, this.LIST_LABEL, "" );

	this.SIZE_SUBMIT.disabled = true;
	
	if ( this.SIZE_METRIC_SELECT.selectedIndex == 0 ) {
		return alert( "Please select a metric." );
	}

	var thisTSEObj = this;
	$.getJSON(
		this.AJAX_URL,
		{ method        : 'getWidth',
		  metric        : this.SIZE_METRIC_SELECT.value,
		  returnformat  : "json"
		},
		function( json ) { thisTSEObj.populateSizeWidths( json ); }
	);

}, // End onSizeWidthChange().

onSizeWidthChange : function() {

	this.SIZE_RATIO_SELECT.disabled = true;
	this.clearOptions( this.SIZE_RATIO_SELECT );
	this.addOption( this.SIZE_RATIO_SELECT, this.LIST_LABEL, "" );

	this.SIZE_DIAMETER_SELECT.disabled = true;
	this.clearOptions( this.SIZE_DIAMETER_SELECT );
	this.addOption( this.SIZE_DIAMETER_SELECT, this.LIST_LABEL, "" );

	this.SIZE_SUBMIT.disabled = true;
	
	if ( this.SIZE_WIDTH_SELECT.selectedIndex == 0 ) {
		return alert( "Please select a width." );
	}

	var thisTSEObj = this;
	$.getJSON(
		this.AJAX_URL,
		{ method        : 'getAspectRatio',
		  metric        : this.SIZE_METRIC_SELECT.value,
		  sectionwidth  : this.SIZE_WIDTH_SELECT.value,
		  returnformat  : "json"
		},
		function( json ) { thisTSEObj.populateSizeRatios( json ); }
	);

}, // End onSizeWidthChange().

onSizeRatioChange : function() {

	this.SIZE_DIAMETER_SELECT.disabled = true;
	this.clearOptions( this.SIZE_DIAMETER_SELECT );
	this.addOption( this.SIZE_DIAMETER_SELECT, this.LIST_LABEL, "" );

	this.SIZE_SUBMIT.disabled = true;

	if ( this.SIZE_RATIO_SELECT.selectedIndex == 0 ) {
		return alert( "Please select an aspect ratio." );
	}

	var thisTSEObj = this;
	$.getJSON(
		this.AJAX_URL,
		{ method        : 'getRimDiameter',
		  metric        : this.SIZE_METRIC_SELECT.value,
		  sectionwidth  : this.SIZE_WIDTH_SELECT.value,
		  aspectratio   : this.SIZE_RATIO_SELECT.value,
		  returnformat  : "json"
		},
		function( json ) { thisTSEObj.populateSizeDiameters( json ); }
	);

}, // End onSizeRatioChange().

onSizeDiameterChange : function() {

	this.SIZE_SUBMIT.disabled = true;

	if ( this.SIZE_DIAMETER_SELECT.selectedIndex == 0 ) {
		return alert( "Please select a diameter." );
	}

	this.SIZE_SUBMIT.disabled = false;

}, // End onSizeDiameterChange().

onTireNameChange : function() {
	
	this.TIRE_NAME_SUBMIT.disabled = true;

	if ( this.TIRE_NAME_SELECT.selectedIndex == 0 ) {
		return alert( "Please select a tire name." );
	}

	this.TIRE_NAME_SUBMIT.disabled = false;
	
}, // End onTireNameChange().

populateMakes : function( response ) {

	this.VEHICLE_MAKE_SELECT.disabled = false;
	var makes = response.DATA;
	for (var i = 0; i < makes.length; i++ ) {
		this.addOption( this.VEHICLE_MAKE_SELECT, makes[ i ], makes[ i ] );
	}

}, // End populateMakes().

populateModels : function( response ) {

	this.VEHICLE_MODEL_SELECT.disabled = false;
	var models = response.DATA;
	for (var i=0; i<models.length; i++) {
		this.addOption( this.VEHICLE_MODEL_SELECT, models[ i ], models[ i ] );
	}

}, // End populateModels().

populateYears : function( response ) {	

	this.VEHICLE_YEAR_SELECT.disabled = false;
	var years = response.DATA;
	for (var i = 0; i < years.length; i++ ) {
		this.addOption( this.VEHICLE_YEAR_SELECT, years[ i ], years[ i ] );
	}

}, // End populateYears().

populateVersions : function( response ) {	

	var versions = response.DATA;
	if ( versions.length > 1 ) {
		this.VEHICLE_VERSION_CONTAINER.className = 'tsm_containerOn';
		this.VEHICLE_VERSION_SELECT.disabled = false;
		for (var i = 0; i < versions.length; i++ ) {
			this.addOption( this.VEHICLE_VERSION_SELECT, versions[ i ], versions[ i ] );
		}
	} else {
		return;
	}


}, // End populateVersions().

populateOptions : function( response ) {

	var options = response.DATA;
	if ( options.length > 1 ) {
		this.VEHICLE_OPTION_CONTAINER.className = 'tsm_containerOn';
		this.VEHICLE_OPTION_SELECT.disabled = false;
		for (var i=0; i < options.length; i++) {
			this.addOption( this.VEHICLE_OPTION_SELECT, options[ i ], options[ i ] );
		}
	} else {
		return;
	}

}, // End populateOptions().

populateSizeMetrics : function( response ){	

	if ( this.SIZE_METRIC_SELECT == null ) return;

	this.SIZE_METRIC_SELECT.disabled = false;	
	var sizeMetrics = response.DATA;
	for (var i=0; i<sizeMetrics.length; i++) {
		this.addOption( this.SIZE_METRIC_SELECT, sizeMetrics[ i ][ 0 ], sizeMetrics[ i ][ 0 ] );
	}

}, // End populateSizeMetrics().

populateSizeWidths : function( response ){	

	if ( this.SIZE_WIDTH_SELECT == null ) return;

	this.SIZE_WIDTH_SELECT.disabled = false;	
	var sizeWidths = response.DATA;
	for (var i=0; i<sizeWidths.length; i++) {
		this.addOption( this.SIZE_WIDTH_SELECT, sizeWidths[ i ], sizeWidths[ i ] );
	}

}, // End populateSizeWidths().

populateSizeRatios : function( response ) {

	this.SIZE_RATIO_SELECT.disabled = false;
	var sizeRatios = response.DATA;
	for (var i=0; i<sizeRatios.length; i++) {
		this.addOption( this.SIZE_RATIO_SELECT, sizeRatios[ i ], sizeRatios[ i ] );
	}

}, // End populateSizeRatios().

populateSizeDiameters : function( response ) {

	this.SIZE_DIAMETER_SELECT.disabled = false;
	var sizeDiameters = response.DATA;
	for (var i=0; i<sizeDiameters.length; i++) {
		this.addOption( this.SIZE_DIAMETER_SELECT, sizeDiameters[ i ], sizeDiameters[ i ] );
	}

}, // End populateSizeDiameters().

populateTireNames : function( response ) {	

	if ( this.TIRE_NAME_SELECT == null ) return;

	this.TIRE_NAME_SELECT.disabled = false;		
	var tireNames = response.DATA;
	for (var i=0; i<tireNames.length; i++) {
		this.addOption( this.TIRE_NAME_SELECT, tireNames[ i ][ 1 ], tireNames[ i ][ 0 ] );
	}

}, // End populateTireNames().

/**
 * Remove all of the options from the passed <select> object
 *
 * @param selectObject
 */
clearOptions : function(selectObject)
{
	if (!selectObject) {
		return;
	}
	
	while (selectObject.options.length>0) {
		selectObject.remove(0);
	}
}, // End this.clearOptions().

/**
 * Add a name/value option to the passed <select> object
 *
 * @param selectObject
 * @param name
 * @param value
 */
addOption : function(selectObject, name, value) {
	if (!selectObject) {
		return;
	}
	
	selectObject.options[selectObject.options.length] = 
		new Option(name, value);
}, // End this.addOption().

/**
 * Trim leading and trailing whitespace
 *
 * @param str
 */
trim : function(str) {
	if (!str) {
		return "";
	}
	return str.replace(/^\s+|\s+$/g,"");
} // End trim().

} ); // End class TireSelectorEntryForm.
