/*-------------------------GLOBALS-------------------------*/
var currentRecord = 0;
var pagingSize 	= 10;
var recordCount;
var previousPage;
var currentPage;
var nextPage;
var active = false;

/*---------------------------------------------------------------*/

Event.observe(window, 'load', init, false);

function init() {
	$('ResultDisplayType').value = 1;
	getRecordCount();
	getTableData();			
	setTimeout(
		function() {
			getNextData();
		}, 500
	);
}

/*---------------------------------------------------------------*/

var myrules = {
	'select' : function(element) {
		element.onkeyup = function(){
			getRecordCount();
			currentRecord = 0;
			getTableData();
			getNextData();		
			showNavigation();			
		},
		element.onchange = function(){
			getRecordCount();
			currentRecord = 0;
			getTableData();
			getNextData();			
			showNavigation();			
		}
	},
	'input' : function(element) {
		element.onkeyup = function(){
			getRecordCount();
			currentRecord = 0;
			getTableData();
			getNextData();
			showNavigation()
		};
	}
};
Behaviour.register(myrules);
/*---------------------------------------------------------------*/


function getRecordCount() {
	var myAjax = new Ajax.Request(
			'/cust/cust_inventory/scripts/ajaxInventorySearch.asp?action=count&Status_Id='+$('Status_Id').value+'&Type_Id='+$('Type_Id').value+'&Manufacturer_Id='+$('Manufacturer_Id').value+'&Model='+$('Model').value+'&Stock_Number='+$('Stock_Number').value+'&Location_Id='+$('Location_Id').value+'', 
			{
				method: 'get', 
				parameters: '', 
				onComplete: function(response) {
					recordCount = response.responseText;
					showNavigation();
					if (recordCount == 0 ) {
						table =  '<div class="InventorySearchResultList">Sorry, no results matched your search criteria.</div>';
						$('view').innerHTML = table;
					}
				}
			});
}

function getTableData() {
	//alert('/cust/cust_inventory/scripts/ajaxInventorySearch.asp?action=page&Status_Id='+$('Status_Id').value+'&Type_Id='+$('Type_Id').value+'&Fuel_Type='+$('Fuel_Type').value+'&Manufacturer_Id='+$('Manufacturer_Id').value+'&Model='+$('Model').value+'&Stock_Number='+$('Stock_Number').value+'&current='+parseInt($('currentRecord').value)+'&size='+pagingSize);
	// new: take into account the "view all" option.
	if ($('viewall').value == 1) {  
		current = 0;
		size = 1000;
	}
	else {
		current = parseInt($('currentRecord').value);
		size = pagingSize;
	}

		var myAjax = new Ajax.Request(
			//'/cust/cust_inventory/scripts/ajaxInventorySearch.asp?action=page&Status_Id='+$('Status_Id').value+'&Type_Id='+$('Type_Id').value+'&Manufacturer_Id='+$('Manufacturer_Id').value+'&Model='+$('Model').value+'&Stock_Number='+$('Stock_Number').value+'&current='+parseInt($('currentRecord').value)+'&size='+pagingSize, 
			'/cust/cust_inventory/scripts/ajaxInventorySearch.asp?action=page&Status_Id='+$('Status_Id').value+'&Type_Id='+$('Type_Id').value+'&Manufacturer_Id='+$('Manufacturer_Id').value+'&Model='+$('Model').value+'&Stock_Number='+$('Stock_Number').value+'&Location_Id='+$('Location_Id').value+'&current='+current+'&size='+size, 
			{
				method: 'get', 
				parameters: '', 
				onComplete: function(response) {
					currentPage = eval('(' + response.responseText + ')');
					drawTable(currentPage, $('view'));
					active = false;
				}
			});
	
}

function viewAll() {
	$('viewall').value = 1;
	getTableData();
}

function getNextData() {
	$('next').innerHTML = '';
	active = true;
	// only run this if we are not viewing all.
	if ($('viewall').value != 1) {  
		var myAjax = new Ajax.Request(
			'/cust/cust_inventory/scripts/ajaxInventorySearch.asp?action=page&Status_Id='+$('Status_Id').value+'&Type_Id='+$('Type_Id').value+'&Manufacturer_Id='+$('Manufacturer_Id').value+'&Model='+$('Model').value+'&Stock_Number='+$('Stock_Number').value+'&Location_Id='+$('Location_Id').value+'&current='+(parseInt($('currentRecord').value)+pagingSize)+'&size='+pagingSize, 
			{
				method: 'get', 
				parameters: '', 
				onComplete: function(response) {
					nextPage = eval('(' + response.responseText + ')');
					active = false;
					// drawTable(nextPage, $('next'));
				}
			});
	}
}

function getPreviousData() {
	if(parseInt(($('currentRecord').value) - pagingSize) >= 0) {
		$('previous').innerHTML = '';
		active = true;
		var myAjax = new Ajax.Request(
				'/cust/cust_inventory/scripts/ajaxInventorySearch.asp?blah=previous&action=page&Status_Id='+$('Status_Id').value+'&Type_Id='+$('Type_Id').value+'&Manufacturer_Id='+$('Manufacturer_Id').value+'&Model='+$('Model').value+'&Stock_Number='+$('Stock_Number').value+'&Location_Id='+$('Location_Id').value+'&current='+(parseInt($('currentRecord').value)-pagingSize)+'&size='+pagingSize, 
				{
					method: 'get', 
					parameters: '', 
					onComplete: function(response) {
						previousPage = eval('(' + response.responseText + ')');
						active = false;
						// drawTable(previousPage,$('previous'));
					}
				});
	}
	else {
		// alert('currentRecord: ' + currentRecord + ' | pagingSize: ' + pagingSize)
		$('previous').innerHTML = '';
	};
	getNextData();
}
/*---------------------------------------------------------------*/
function trim(str, chars) {
	return spacetodash(ltrim(rtrim(str, chars), chars));
}

function spacetodash(str) {
	return str.replace(/\s/g,"-");
}
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
/*---------------------------------------------------------------*/

function drawTable(page, contain) {
	alt = '';
	if (page == undefined ){
			//alert('undefined');
			getTableData();
		}else{
		
	if ($F('ResultDisplayType') == 1){
		table = 	'<div class="InventorySearchResultList" id="results">';
		for(i = 0; i < page['inventory'].length; i++) {
			//table +=		'<div class="result" onmouseover="pviiClassNew(this, \'resultOver\')" onmouseout="pviiClassNew(this, \'result\')" onclick="MM_goToURL(\'this\',\'/UsedMotorhomes/Usedmotorhome-' + page['inventory'][i].Inventory_Id + '.html\')">'
			table +=		'<div class="result" onmouseover="pviiClassNew(this, \'resultOver\')" onmouseout="pviiClassNew(this, \'result\')" onclick="MM_goToURL(\'this\',\'/'+trim(page['inventory'][i].Status)+'Motorhomes/' +
									trim(page['inventory'][i].Year) + '-' +
									trim(page['inventory'][i].Manufacturer) + '-' +
									trim(page['inventory'][i].Model) + '-' +
									trim(page['inventory'][i].Inventory_Id) +
									'.html\')">'
					if (page['inventory'][i].image1){
								table += '<div class="thumb"><img src="' + page['inventory'][i].image1 + '" height=65 /></div>'
							}
							table += '<div class="title">' + page['inventory'][i].Year + ' ' + 
								page['inventory'][i].Manufacturer + ' '  + 
								page['inventory'][i].Model +
							'</div>' +
							'<div><span class="label">Vintage:</span> ' + page['inventory'][i].Status + '</div>' +
							'<div><span class="label">Stock #:</span> ' + page['inventory'][i].Stock_Number + '</div>' + 
							'<div><span class="label">'
							if (new Date(page['inventory'] [i].Featured_Start) <= new Date() && new Date(page['inventory'] [i].Featured_End) >= new Date()){
								if (page['inventory'][i].Price == 0){
									// Item is featured, but no normal price us sprcified.  Just show the sales price.
									table += ' Sale Price:</span> $' + page['inventory'][i].Sale_Price + ''
								}else{
									// Item is featured AND the normal price is specified.  Show price and sales price.
									table += 'Price:</span> $' + page['inventory'][i].Price + ''
									table += ' | <span class="label">Sale Price:</span>$' + page['inventory'][i].Sale_Price + ''
								}
							}else{
								if (page['inventory'][i].Price == 0){
									// Item is featured, but no normal price us sprcified.  Just show the sales price.
									//table += 'Price: $</span>Current market value.'
									table += '</span>'
								}else{
									// Item is featured AND the normal price is specified.  Show price and sales price.
									
									table += ' Price:</span> $' + page['inventory'][i].Price + ''
								}
								// Item is not featured, don't show the price.
								
							}
							
							
							/*
							if (page['inventory'][i].Price == 0){
								table += 'Price:</span>Current market value.'
							}else{
								table += 'Price:</span>$' + page['inventory'][i].Price + ''
							}	
							if (page['inventory'] [i].Sale_Price == 0){
								table += '&nbsp'
							}else{
								if (new Date(page['inventory'] [i].Featured_Start) <= new Date() && new Date(page['inventory'] [i].Featured_End) >= new Date()){
									table += ' | <span class="label">Sale Price:</span>$' + page['inventory'][i].Sale_Price + ''
								}
							}
							*/
						table += '</div>';	
						table += '</div>';
		};
		table += '</div>';
	}else{
		table = 	'<table class="InventorySearchResultList" id="results">' +
					'<tr>' +
						'<td  class="label">Year</td>' +
						'<td class="label">Manufacturer</td>' +
						'<td class="label">Model</td>' +
						'<td class="label">Price</td>' +
						'<td class="label"></td>' +
					'</tr>';
		for(i = 0; i < page['inventory'].length; i++) {			
			table +=		'<tr>' +
							'<td>' + page['inventory'][i].Year + '</td>' +
							'<td>' + page['inventory'][i].Manufacturer + '</td>' +
							'<td>' + page['inventory'][i].Model + '</td>'
							
							if (page['inventory'][i].Price == 0){
								table += '<td>Current market value.</td>'
							}else{
								table += '<td>$' + page['inventory'][i].Price + '</td>'
							}
							
							table += '<td><a href="\Usedmotorhome-'+page['inventory'][i].Inventory_Id+'.html">More Details</td>' +
						'</tr>';
		};
		table += '</table>';	
	};
	};	
	contain.innerHTML = table;	
}

function getNextPage() {
	if(!active) {
		$('currentRecord').value = parseInt($('currentRecord').value) + pagingSize
		showNavigation();
		previousPage = currentPage;
		currentPage = nextPage;
		drawTable(currentPage, $('view'));
		// drawTable(previousPage, $('previous'));
		getNextData();
	}
}



function getPreviousPage() {
	//if(!active) {
		$('currentRecord').value = parseInt($('currentRecord').value) - pagingSize
		showNavigation();
		nextPage = currentPage;
		currentPage = previousPage;
		//getTableData();
		drawTable(currentPage, $('view'));
		// drawTable(nextPage, $('next'));
		getPreviousData();
	//}
}
function setResultDisplayType() {
	if ($F('ResultDisplayType') == 1){
		$('displayType').innerHTML = '<a href="javascript:setResultDisplayType();"">View list with <strong>Thumbnails</strong></a>';
		$('ResultDisplayType').value = 2;
		drawTable(currentPage, $('view'));
	}else{
		$('displayType').innerHTML = '<a href="javascript:setResultDisplayType();"">View <strong>Text Only</strong> List</a>';
		$('ResultDisplayType').value = 1;
		drawTable(currentPage, $('view'));
	};
}
function showNavigation() {
	// new: take into account the "view all" option.
	if ($('viewall').value == 1) {  
		$('currentRec').innerHTML = "Viewing All";
		$('currentRecBot').innerHTML = "Viewing All"
	}
	else {
		var x = parseInt($('currentRecord').value);
		var y = pagingSize;
		var z = x+y;
		var vv = parseInt($('currentRecord').value)+1;
		// Need to change that to have 'y' be dynamic
		if (z > recordCount){
			z = recordCount;
			//ofRecord = '?';
		}
		if (x == 0){
			$('currentRec').innerHTML = "Record 1 - " + z  + " of " + recordCount; 
		}else{
			$('currentRec').innerHTML = "Record " + vv + " - " + z  + " of " + recordCount;
		}
		$('currentRecBot').innerHTML = "Record " + vv + " - " + z  + " of " + recordCount; 
		if (parseInt($('currentRecord').value) == 0){
				$('previousLink').style.display = 'none';
				$('PagingSep').style.display = 'none';
				$('previousLinkBot').style.display = 'none';
				$('PagingSepBot').style.display = 'none';
			}else{
				$('previousLink').style.display = 'inline'
				$('PagingSep').style.display = 'inline';
				$('previousLinkBot').style.display = 'inline'
				$('PagingSepBot').style.display = 'inline';
			}
		if ((parseInt($('currentRecord').value) + pagingSize) >= recordCount){
			$('nextLink').style.display = 'none';
			$('nextLinkBot').style.display = 'none';
		}else{
			$('nextLink').style.display = 'inline';
			$('nextLinkBot').style.display = 'inline';
		}
	}
}

