/**
 * General purpose utilities that relates to jquery
 * 
 * @author jwest
 */

/**
 * Clears a select field of its options
 * 
 * @param {Object} el
 * @param {Object} message
 */
resetAndClearSelect = function(el, message){
	el.options.length = 0;
    el.options[0] = new Option(message, '');
}

/**
 * Populates select list from array of items given as 
 * objects: { name: 'text', value: 'value' }
 * 
 * @param {Object} el
 * @param {Object} items
 */
populateSelect = function(el, items, message) {
    el.options.length = 0;
    if (items.length > 0) {
		el.options[0] = new Option(message, '');
	}
    $.each(items, function () {
        el.options[el.options.length] = new Option(this.NAME, this.VALUE);
    });
}
$(document).ready(function(){
	
	resetSearchForm();
	
	$("#searchSubmitButton").bind(
		"click",
		function(){
			if ( $("#type").val() == "used" ){
				$("#searchForm")
					.attr("action","used-vehicles.da")
					.attr("method","POST")
					.submit();
				
			}
			if ( $("#type").val() == "new" ){
				$("#searchForm")
					.attr("action","new-vehicles.da")
					.attr("method","POST")
					.submit();
			}
		}
	);
});

/**
 *
 */
getSelectDataByType = function(){
	var type = $("#type").val();
	if ( type != "" ){
		$.get(
			"./api/com/jbc/cfc/FormDataHelper.cfc",
			{
				method: "GetVehicleMakes",
				type: type
			},
			function(data){
				if (data.DATA.length > 0){
					populateSelect(document.getElementById("make"),data.DATA,"Select Make");
				}
			},
			"json"
		);
		$.get(
			"./api/com/jbc/cfc/FormDataHelper.cfc",
			{
				method: "GetVehicleModels",
				type: type
			},
			function(data){
				if (data.DATA.length > 0){
					populateSelect(document.getElementById("model"),data.DATA,"Select Model");
				}
			},
			"json"
		);
		processSelectDataByType();
	} else {
		resetSearchForm();
	}
}

/**
 *
 */
getSelectDataByMake = function(){
	var type = $("#type").val();
	var make = $("#make").val();
	
	if ( type != "" && make != "" ){
		$.get(
			"./api/com/jbc/cfc/FormDataHelper.cfc",
			{
				method: "GetVehicleModels",
				type: type,
				make: make
			},
			function(data){
				if (data.DATA.length > 0){
					populateSelect(document.getElementById("model"),data.DATA,"Select Model");
				}
			},
			"json"
		);
	}
}

/**
 *
 */
processSelectDataByType = function(data){
	$("#make").removeAttr("disabled");
	$("#model").removeAttr("disabled");
	$("#year_from").removeAttr("disabled");
	$("#year_to").removeAttr("disabled");
	$("#internet_price_from").removeAttr("disabled");
	$("#internet_price_to").removeAttr("disabled");
	$("#searchResetButton").removeAttr("disabled");
	$("#searchSubmitButton").removeAttr("disabled");
}

/**
 *
 */
resetSearchForm = function(){
	// clear the options out of these fields and reset them
	resetAndClearSelect($("#make").get(0), 'Select Make');
	resetAndClearSelect($("#model").get(0), 'Select Model');

	// reset the selected value in the select boxes
	$("#type").selectOptions("",true);
	$("#year_from").selectOptions("",true);
	$("#year_to").selectOptions("",true);
	$("#internet_price_from").selectOptions("",true);
	$("#internet_price_to").selectOptions("",true);
	
	// disable the fields again
	$("#make").attr("disabled",true);
	$("#model").attr("disabled",true);
	$("#year_from").attr("disabled",true);
	$("#year_to").attr("disabled",true);
	$("#internet_price_from").attr("disabled",true);
	$("#internet_price_to").attr("disabled",true);
	$("#searchResetButton").attr("disabled",true);
	$("#searchSubmitButton").attr("disabled",true);
}