/////////////
// GLOBALS //
/////////////

// Index constants
var INDEX_TYPE = 0;
var INDEX_WIDTH = 1;
var INDEX_RATIO = 2;
var INDEX_RIM = 3;
var INDEX_BRAND = 4;


// Tyres array
var $tyres = new Array;

////////////////////
// FILL FUNCTIONS //
////////////////////

function tyre_fillWidth(){
	$dropdown=document.getElementById("width");
	autofill_setDropdownLoading($dropdown);
	if(document.getElementById("type")!=null){$widths=tyre_getWidths(general_getElementValue("type"));}
	else{$widths=tyre_getWidths();}
	if($widths.length>0){
		for($i=0;$i<$widths.length;$i++){
			$curWidth=$widths[$i];
			$dropdown.options[$dropdown.options.length]=new Option($curWidth,$curWidth);
		}
		autofill_setDropdownLoaded($dropdown);
		sortListNumeric('width');
	}else{
		autofill_noData($dropdown);
	}
}	

function tyre_fillRatio(){
	tyre_setFields("width");
	$dropdown=document.getElementById("ratio");
	autofill_setDropdownLoading($dropdown);
	if(document.getElementById("type")!=null){$ratios=tyre_getRatios(general_getElementValue("type"),general_getElementValue("width"));}
	else{$ratios=tyre_getRatios('',general_getElementValue("width"));}
	if($ratios.length>0){
		for($i=0;$i<$ratios.length;$i++){
			$curRatio=$ratios[$i];
			$dropdown.options[$dropdown.options.length]=new Option($curRatio,$curRatio);
		}
		autofill_setDropdownLoaded($dropdown);
		sortListNumeric('ratio');
	}else{
		autofill_noData($dropdown);
	}
}
function tyre_fillRim(){
	tyre_setFields("ratio");
	$dropdown=document.getElementById("rim");
	autofill_setDropdownLoading($dropdown);
	if(document.getElementById("type")!=null){$rims=tyre_getRims(general_getElementValue("type"),general_getElementValue("width"),general_getElementValue("ratio"));}
	else{$rims=tyre_getRims('',general_getElementValue("width"),general_getElementValue("ratio"));}
	if($rims.length>0){
		for($i=0;$i<$rims.length;$i++){
			$curRim=$rims[$i];
			$dropdown.options[$dropdown.options.length]=new Option($curRim,$curRim);
		}
		autofill_setDropdownLoaded($dropdown);
		sortListNumeric('rim');
	}else{
		autofill_noData($dropdown);
	}
}
function tyre_fillBrand(){
	tyre_setFields("rim");
	$dropdown=document.getElementById("brand");
	autofill_setDropdownLoading($dropdown);
	if(document.getElementById("type")!=null){$brands=tyre_getBrands(general_getElementValue("type"),general_getElementValue("width"),general_getElementValue("ratio"),general_getElementValue("rim"));}
	else{$brands=tyre_getBrands('',general_getElementValue("width"),general_getElementValue("ratio"),general_getElementValue("rim"));}
	if($brands.length>0){
		for($i=0;$i<$brands.length;$i++){
			$curBrand=$brands[$i];
			if($curBrand[0]!='Other') {
				$dropdown.options[$dropdown.options.length]=new Option($curBrand[1],$curBrand[0]);
			}
		}
		autofill_setDropdownLoaded($dropdown,'Any');
		sortList('brand');
	}else{
		autofill_noData($dropdown,'Any');
	}
}


////////////////////////
// GET DATA FUNCTIONS //
////////////////////////

// Get the available types
function tyre_getWidths($selType) {
	var $widths = new Array;
	for ($i=0; $i<$tyres.length; $i++) {
		if (document.getElementById("type") != null) { 
			$curType = $tyres[$i][INDEX_TYPE][0];
		} else { $curType = $selType; }		
		$curWidth = $tyres[$i][INDEX_WIDTH];
		if ($curType == $selType) {
			if ($widths.inArray($curWidth) == false) {
				$widths[$widths.length] = $curWidth;
			}
		}
	}
	
	return $widths;
}

// Get the available ratios
function tyre_getRatios($selType, $selWidth) {
	var $ratios = new Array;
	
	for ($i=0; $i<$tyres.length; $i++) {
		if (document.getElementById("type") != null) { 
			$curType = $tyres[$i][INDEX_TYPE][0];
		} else { $curType = $selType; }		
		$curWidth = $tyres[$i][INDEX_WIDTH];
		$curRatio = $tyres[$i][INDEX_RATIO];
		if (($curType == $selType) && ($curWidth == $selWidth)) {
			if ($ratios.inArray($curRatio) == false) {
				$ratios[$ratios.length] = $curRatio;
			}
		}
	}
	
	return $ratios;
}


// Get the available rims
function tyre_getRims($selType, $selWidth, $selRatio) {
	var $rims = new Array;
	
	for ($i=0; $i<$tyres.length; $i++) {
		if (document.getElementById("type") != null) { 
			$curType = $tyres[$i][INDEX_TYPE][0];
		} else { $curType = $selType; }		
		$curWidth = $tyres[$i][INDEX_WIDTH];
		$curRatio = $tyres[$i][INDEX_RATIO];
		$curRim = $tyres[$i][INDEX_RIM];
		if (($curType == $selType) && ($curWidth == $selWidth) && ($curRatio == $selRatio)) {
			if ($rims.inArray($curRim) == false) {
				$rims[$rims.length] = $curRim;
			}
		}
	}
	
	return $rims;
}

// Get the available brands
function tyre_getBrands($selType, $selWidth, $selRatio, $selRim) {
	var $brands = new Array;
	var $addedBrands = new Array;
	
	for ($i=0; $i<$tyres.length; $i++) {
		if (document.getElementById("type") != null) { 
			$curType = $tyres[$i][INDEX_TYPE][0];
		} else { $curType = $selType; }		
		$curWidth = $tyres[$i][INDEX_WIDTH];
		$curRatio = $tyres[$i][INDEX_RATIO];
		$curRim = $tyres[$i][INDEX_RIM];
		$curBrand = $tyres[$i][INDEX_BRAND];
		if (($curType == $selType) && ($curWidth == $selWidth) && ($curRatio == $selRatio) && ($curRim == $selRim)) {
			if ($brands.inArray($curBrand) == false) {
				$brands[$brands.length] = $curBrand;
				$addedBrands[$addedBrands.length] = $curBrand[0];
			}
		}
	}
	
	return $brands;
}


/////////////////////////////
// MISCELLANEOUS FUNCTIONS //
/////////////////////////////
// This functions adds 'inArray' to the standard array class.  This comes from:
Array.prototype.inArray=function(search_term){var i=this.length;if(i>0){do{if(this[i]===search_term){return true;}}while(i--);}return false;}

// This function returns the value of a given element.  Elements should be specified by their element ID.
function general_getElementValue($elementID){$value=document.getElementById($elementID).value;return $value;}

// Register a tyre
function tyre_register($data) {
	$tyre = $data.split("|");
	$tyre[INDEX_TYPE] = $tyre[INDEX_TYPE].split(":");
	$tyre[INDEX_BRAND] = $tyre[INDEX_BRAND].split(":");
	$tyres[$tyres.length] = $tyre;
}


// This function enables/disables etc. fields for the tyre search.
function tyre_setFields($lastField) {
	// Initialise vars
	var $fieldList = new Array;
	var $curField = "";
	var $lastFieldFound = false;
	var $dropdown = "";

	// Setup the list of fields
	$fieldList[0] = "type";
	$fieldList[1] = "width";
	$fieldList[2] = "ratio";
	$fieldList[3] = "rim";
	$fieldList[4] = "brand";
	
	// Loop through all fields
	for ($i=1; $i<$fieldList.length; $i++) {
		// Get the current field
		$curField = $fieldList[$i];

		// If the last field has already been found...
		if ($lastFieldFound) {
			// Get a handle to the current field
			$dropdown = document.getElementById($curField)
			// Set as no data
			if ($curField == 'brand') {
				autofill_noData($dropdown, 'Any');
			} else {
				autofill_noData($dropdown);
			}
		// If the last field has NOT already been found...
		} else {
			// If the current field is the last field...
			if ($curField == $lastField) {
				// Record the last field as found
				$lastFieldFound = true;
			}
		}
	}
}

// sorts items in a dropdown list
function sortList(id) {	
  var obj = document.getElementById(id);
  var values = new Array();
  for(var i = 0; i < obj.options.length; i++) {
	values.push(obj.options[i].innerHTML + "--xx--" + obj.options[i].value);	
  }
  values = values.sort();
  for(var i = 0; i < values.length; i++) {
	valueArray = values[i].split('--xx--');
	obj.options[i].innerHTML = valueArray[0];
	obj.options[i].value = valueArray[1];
  }
}	


function sortNumeric(m,n)
{  
	if (parseInt(m)>parseInt(n)) return 1;
	if (parseInt(m)<parseInt(n)) return -1;
	return 0;
}

function sortListNumeric(id) {
  var obj = document.getElementById(id);
  var values = new Array();

  for(var i = 1; i < obj.options.length; i++) {
	if(obj.options[i].text == 'None') { var num = i; } 
	else { values.push(obj.options[i].text); }
  }
// sort numeric ASC
  values = values.sort(sortNumeric);
// add - select -
  values.unshift(obj.options[0].text);
// add None if there is one
	if(num) {
 	 values.push(obj.options[num].text); 
	}

for(var i = 0; i < values.length; i++) {
    valueArray = values[i];
    obj.options[i].innerHTML = valueArray;
    obj.options[i].value = valueArray;        
  }
}
// AutoFill

// This function disables a dropdown, clears all existing entries, and displays 'Loading...'.
function autofill_setDropdownLoading($dropdown) {
	$dropdown.disabled = true;
	$dropdown.options.length = 0;
	$dropdown.options[0] = new Option("Loading...", "");
}

// This function enables a dropdown and sets the first option as '- Select -'.
function autofill_setDropdownLoaded($dropdown, $firstOption) {
	if ($firstOption) {
		$dropdown.options[0] = new Option($firstOption, "");
	} else {
		$dropdown.options[0] = new Option("- Select -", "");
	}
	$dropdown.selectedIndex = 0;
	$dropdown.disabled = false;
}

// This function disables a dropdown, clears all existing entries, and displays '- Select -'.
function autofill_noData($dropdown, $firstOption) {
	$dropdown.disabled = true;
	$dropdown.options.length = 0;
	if ($firstOption) {
		$dropdown.options[0] = new Option($firstOption, "");
	} else {
		$dropdown.options[0] = new Option("- Select -", "");
	}
}

// ---- //



function displayCategory(eId) {
	var eId = eId.split('#')[1];
	
	var oEl = document.getElementById('menuCat');
	var oLi = oEl.getElementsByTagName("li");
	for (i=0; i < oLi.length; i++) {
		if (oLi[i].className.split(' ')[0] == 'first') {
			oLi[i].className = 'first';
		} else { oLi[i].className = ''; }
		var oA = oLi[i].getElementsByTagName("a");
		if (((oA[0].href).split('#')[1]) == eId) { 
			if (oLi[i].className == 'first') { oLi[i].className += ' active'; } else { oLi[i].className = 'active'; }
		}
	}
	
	var oAnchor = oEl.getElementsByTagName("a");
	var oDiv = document.getElementsByTagName("div");
	for (i=0; i < oDiv.length; i++) {
		if (oDiv[i].className.split(' ')[0] == 'resultCategory') {
			oDiv[i].className = 'resultCategory';
			if ((oDiv[i].id) == eId) { oDiv[i].className += ' active'; }
		}
	}
}

function displayDiv(eId) {
	var eId = eId.split('#')[1];
	var el = document.getElementById(eId);
	if (el.style.display == 'none') {
		el.style.display = 'block';
	} else {
		el.style.display = 'none';
	} 
}

