/**
 * Requirements:
 *     - Form needs to have an id 'inv_search'
 *     - Condition Select field needs an id 'inv_condition'
 *     - Makes Select field needs an id 'inv_make'
 *     - Models Select field needs an id 'inv_model'
 *     - Bodies select field needs an id 'inv_body'
 * 
 *     - The names (name attribute in HTML), needs to be the
 *     same as id without the 'inv_' prefix. So Condition select
 *     field would need to look like:
 *         '<select id="inv_condition" name="condition"...>'
 */


/*
 * The following line is the only configurable option here.
 * This line defines the communication URL for current page.
 * It is extremely important that this variable be declared
 * properly, and that it points to the correct inventory URL
 * of currently viewed Pfaff site.
 * 
 * 
 * You can:
 *     - Include the same .js file for all Pfaff Sites, and copy
 *       paste this variable declaration *before* loading this
 *       file. Obviously, the URL needs to be adjusted for every
 *       Pfaff site.
 *     - Uncomment this line, adjust it, and copy paste all of
 *       the JavaScript in this file.
 *     - Create mutiple javascript files, one for each Pfaff
 *       site, uncomment this line and adjust it for every single
 *       javascript file.
 */
//var InventoryURL = 'http://pfaffaudivaughan.devinventory.smartdealer.ca';
var InventoryURL = '';

$(document).ready(function() {
	$('select#inv_condition').change(function () {
		if ($('select#inv_condition').val() == 'Used') {
			$('form#inv_search').attr('action', InventoryURL +'/used');
		} else if ($('select#inv_condition').val() == 'New') {
			$('form#inv_search').attr('action', InventoryURL +'/new');
		}
		getMakes();
	});

	$('select#inv_make').change(function () {
		getModels();
	});

	$('select#inv_model').change(function () {
		getBodies();
	});
	getMakes();
});

function getMakes () {
	var data = {
		ajax 		: 'true',
		condition 	: $('select#inv_condition').val()
	};
	$.getJSON( InventoryURL+'/ajax/get-makes?callback=?', data, function (Response) {
		var options = '<option></option>';
		for (var i = 0; i < Response.length; i++) {
			options += '<option value="' + Response[i] + '">' + Response[i] + '</option>';
		}
		$('select#inv_make').html(options);
	});
}

function getModels () {
	var data = {
		ajax 		: 'true',
		condition 	: $('select#inv_condition').val(),
		make		: $('select#inv_make').val()
	};
	$.getJSON( InventoryURL+'/ajax/get-models?callback=?', data, function (Response) {
		var options = '<option></option>';
		for (var i = 0; i < Response.length; i++) {
			options += '<option value="' + Response[i] + '">' + Response[i] + '</option>';
		}
		$('select#inv_model').html(options);
	});
}

function getBodies () {
	var data = {
		ajax 		: 'true',
		condition 	: $('select#inv_condition').val(),
		make		: $('select#inv_make').val(),
		model		: $('select#inv_model').val()
	};
	$.getJSON( InventoryURL+'/ajax/get-bodies?callback=?', data, function (Response) {
		var options = '<option></option>';
		for (var i = 0; i < Response.length; i++) {
			options += '<option value="' + Response[i] + '">' + Response[i] + '</option>';
		}
		$('select#inv_body').html(options);
	});
}
