jQuery.noConflict();

function updateCategories() {
	_ajaxConfig = {'_cfscriptLocation':'/functions/category.cfc', '_jsscriptFolder':'/js'};
	
	// dealerID default should be 0
	dealerID = typeof(dealerID) != 'undefined' ? dealerID : 0;
	//locationID = typeof(locationID) != 'undefined' ? locationID : 0;
	locationID = DWRUtil.getValue('locationID');
	
	industryID = DWRUtil.getValue('industryID');
	
	if(industryID == '3'){
		document.getElementById('catDisplay').innerHTML = 'Trailer Type';
	} else {
		document.getElementById('catDisplay').innerHTML = 'Model';
	}
	
	
	
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getCategoriesByIndustry', industryID, dealerID, locationID, updateCategoriesResult);
	
}	

function updateCategoriesResult(categories) {

	DWRUtil.removeAllOptions("catID");
	
	var getValue = function (thisRow) {
		return thisRow.intcategoryid;
	}
	var getText = function (thisRow) {
		return thisRow.varcategory;
	}
	
	document.getElementById('catID').options[0] = new Option('All Categories', 0);
	DWRUtil.addOptions("catID", categories, [getValue, getText], null);	
	updateMakes();
}

function updateMakes() {
	_ajaxConfig = {'_cfscriptLocation':'/functions/make.cfc', '_jsscriptFolder':'/js'};
	
	// dealerID default should be 0
	dealerID = typeof(dealerID) != 'undefined' ? dealerID : 0;
	//locationID = typeof(locationID) != 'undefined' ? locationID : 0;
	locationID = DWRUtil.getValue('locationID');
	
	industryID = DWRUtil.getValue('industryID');
	catID = DWRUtil.getValue('catID');	

	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getMakesByCategory', catID, industryID, dealerID, locationID, updateMakesResult);
}

function updateMakesResult(makes) {
	DWRUtil.removeAllOptions("makeID");
	
	var getValue = function (thisRow) {
		return thisRow.intmakeid;
	}
	var getText = function (thisRow) {
		return thisRow.varmake;
	}

	document.getElementById('makeID').options[0] = new Option('All Makes', 0)	
	DWRUtil.addOptions("makeID", makes, [getValue, getText], null);
	
	// after all boxes are set, update the productlist query
	if (updateQuery == 1) {
		doQuery(dealerID,locationID);
	}
}

function unhide (unhideID) {
	row = document.getElementById(unhideID);
	row_header = document.getElementById(unhideID + '_header');
	
	row.style.display = '';
	row_header.style.display = '';
}

function hide (hideID) {
	row = document.getElementById(hideID);
	row_header = document.getElementById(hideID + '_header');
	
	row.style.display = 'none';
	row_header.style.display = 'none';
}

function validateSellStep2 (selectBoxesList, labelList) {
	selectBoxesArray = selectBoxesList.split(',');
	labelArray = labelList.split(',');
	
	for (i = 0; i < selectBoxesArray.length; i = i + 1) {
		if (document.getElementById(selectBoxesArray[i]).value == 0) {
			alert('You must select a value for ' + labelArray[i]);
			return false;
		}
	}
	/*
	if (document.getElementById(yearID).value == "") {
		year = 0;
	} else {
		year = parseInt(document.getElementById(yearID).value);
	}	

	if (year < 1900 || year > 2500) {
		alert('You must enter a valid year');
		return false;
	}
	*/
	
	return true;

}

function toggleShowInfo() {
	var showInfo = document.getElementById('showInfo');
	var	chkToggleShowInfo = document.getElementById('include3D');
		
	//alert(chkToggleShowInfo.checked);
		
	if (typeof(showInfo) != "undefined" && chkToggleShowInfo.checked == true) {
		showInfo.style.display = "inline";
	} else {
		showInfo.style.display = "none";
	}
}

	
// 02.25.2008 stripe() function removed and placed in inventory.cfm and inventory-embed.cfm 
		
		
function doQuery(dealerID,locationID)	{
	_ajaxConfig = {'_cfscriptLocation':'/functions/product.cfc', '_jsscriptFolder':'/js'};
	
	var locationID = (locationID == null) ? "0" : locationID;
	
	// send data to CF
	DWRUtil.useLoadingMessage();
	industryID = parseInt(DWRUtil.getValue("industryID"));
	catID = parseInt(DWRUtil.getValue("catID"));
	makeID = parseInt (DWRUtil.getValue("makeID"));
	bitNew = parseInt (DWRUtil.getValue("bitNew"));
	//



//alert('makeID: ' + makeID);


	
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'inventoryDrillDown', dealerID, industryID, catID, makeID, bitNew, locationID, doQueryResults);
}
		
// call back function
function doQueryResults (r) {
	DWRUtil.removeAllRows("productTableBody");
	
	if (r.getRowCount()) {
		document.getElementById('productTable').style.display = '';
		document.getElementById('nomatch').style.display = 'none';
		
		industryID = parseInt(DWRUtil.getValue("industryID"));

		linkURL = initLinkURL + '&amp;intProductID=';
		productimagepath = 'productimages';
			
		var getPhoto = function (thisRow) {
			if (thisRow.varimage1) {
				thumbImage = thisRow.varimage1.substr(0, thisRow.varimage1.lastIndexOf('.')) + '_sm.jpg';
				imageSrc = '<img src="/' + productimagepath + '/' + thumbImage + '" border="0" />';
			} else {
				imageSrc = '<img src="/images/nophoto_sm.gif" border="0" />';
			}
			link = '<div style="text-align: center;"><a href="' + linkURL + thisRow.intproductid + '">' + imageSrc + '</a></div>';
			return link;
		}
		var getYear = function (thisRow) {
			
			var isnew = (thisRow.bitnew == true) ? 'New' : 'Used';
			var theyear = (thisRow.intyear != null && thisRow.intyear != 0) ? thisRow.intyear : '';
			
			link = '<a href="' + linkURL + thisRow.intproductid + '">' + theyear + ' (' + isnew + ') </a>';
			return link;
		}
		var getMake = function (thisRow) {
			link = '<a href="' + linkURL + thisRow.intproductid + '">' + thisRow.varmake + '</a>';
			return link;
		}
		var getModel = function (thisRow) {
			if(industryID == '3'){
				link = '<a href="' + linkURL + thisRow.intproductid + '">' + thisRow.varcategory + '</a>';
			} else {
				link = '<a href="' + linkURL + thisRow.intproductid + '">' + thisRow.varmodel + '</a>';				
			}
			return link;
		}
		var getPrice = function (thisRow) {
			if (thisRow.fltprice == 0) {
				thisRow.fltprice = 'Call';
			} else {
				thisRow.fltprice = '$' + CommaFormatted(String(parseFloat(thisRow.fltprice)));
			}
			link = '<div style="text-align: right;"><a href="' + linkURL + thisRow.intproductid + '">' + thisRow.fltprice + '</a></div>';
			return link;
		}
		var getQuantity = function (thisRow) {
			link = '<div style="text-align: center;"><a href="' + linkURL + thisRow.intproductid + '">' + thisRow.intquantity + '</a></div>';
			return link;				
		}
		var getLocation = function (thisRow) {
			link = '<a href="' + linkURL + thisRow.intproductid + '">' + thisRow.varprovince + '</a>';
			return link;
		}
		
		DWRUtil.addRows("productTableBody", r, [getPhoto, getYear, getMake, getModel, getPrice, getQuantity, getLocation], null);
		stripe("productTableBody");
	} else {
		DWRUtil.removeAllRows('productTableBody');
		document.getElementById('productTable').style.display = 'none';
		document.getElementById('nomatch').style.display = '';
	}
}
