var Search = {

	handleSuccess:function(o)
	{
		document.getElementById('search_results_number').innerHTML = o.responseText;
	},

	handleFailure:function(o)
	{
		// Failure handler
	},
	
	start:function(int_class, int_subclass, str_text,str_volts1, str_volts2, str_amps, str_teeth, str_mounting_type, str_rotation)
	{
		document.getElementById('search_results_number').innerHTML = "<img src=\"images/loading.gif\" align=\"baseline\">";
		
		YAHOO.util.Connect.asyncRequest('POST', "ajax.php?do=search", callback, "class="+int_class+"&subclass="+int_subclass+"&text="+str_text+"&volts1="+str_volts1+"&volts2="+str_volts2+"&amps="+str_amps+"&teeth="+str_teeth+"&mounting_type="+str_mounting_type+"&rotation="+str_rotation);
	}
};

var callback =
{
	success:Search.handleSuccess,
	failure:Search.handleFailure,
	scope: Search
};

var OemSearch = {

	handleSuccess:function(o)
	{
		//document.getElementById('search_results').innerHTML = o.responseText;
		
		if (o.responseText == "0")
		{
			document.getElementById('search_results').innerHTML = "<strong>OEM number not found</strong>";
		}
		else
		{
			//document.getElementById('search_results').innerHTML = o.responseText;
			var array = o.responseText.split(",");
			top.location.href = "index.php?page=oem_details&type=" + array[0] + "&id=" + array[1];
		}
	},

	handleFailure:function(o)
	{
		// Failure handler
	},
	
	start:function(str_text)
	{
		YAHOO.util.Connect.asyncRequest('POST', "ajax.php?do=oem_search", oem_callback, "text="+str_text);
	}
};

var oem_callback =
{
	success:OemSearch.handleSuccess,
	failure:OemSearch.handleFailure,
	scope: OemSearch
};


var timer = null;

function delaySearch(delay)
{
	if (timer != null) clearTimeout(timer);
	timer = setTimeout("search()",delay);
}

function search()
{
	var item_class = null;
	var subclass = null;
	var volts1 = null;
	var volts2 = null;
	var amps = null;
	var teeth = null;
	var mounting_type = null;
	var rotation = null;	
	var text = null;
	
	var form = document.forms['product_search'];
	
	if (form.class_list.value != "")
	{
		item_class = document.forms['product_search'].class_list.value;
	}
	
	if (form.subclass_list.value != "")
	{
		subclass = form.subclass_list.value;
	}
	
	if (form.volts1.value != "")
	{
		volts1 = form.volts1.value;
	}
	
	if (form.volts2.value != "")
	{
		volts2 = form.volts2.value;
	}
	
	if (form.amps.value != "")
	{
		amps = form.amps.value;
	}
	
	if (form.teeth.value != "")
	{
		teeth = form.teeth.value;
	}
	
	if (form.mount_type.value != "")
	{
		mounting_type = form.mount_type.value;
	}
	
	if (form.rotation.value != "")
	{
		rotation = form.rotation.value;
	}
	
	if (form.search_text.value != "")
	{
		text = form.search_text.value;
	}
	
	if (item_class != null)
	{
		document.getElementById('loader').style.display = "block";
		Search.start(item_class,subclass,text,volts1,volts2,amps,teeth,mounting_type,rotation);
	}
	else
	{
		document.getElementById('loader').style.display = "none";
	}
}

function oem_search()
{
	var form = document.forms['oem_search'];
	var text = null;
	
	if (form.number.value != "")
	{
		text = form.number.value;
	}
	
	if (text != null)
	{
		OemSearch.start(text);
	}
}

function populateSpecial(id)
{
	switch (id)
	{
		case 10:
			document.getElementById('alternatorOptions1').className = 'search_option';
			document.getElementById('alternatorOptions2').className = 'search_option';
			document.getElementById('alternatorOptions3').className = 'search_option';
			document.getElementById('alternatorOptions1_1').className = '';
			document.getElementById('alternatorOptions2_2').className = 'text';
			document.getElementById('alternatorOptions3_3').className = '';
			document.getElementById('alternatorOptions1_1').disabled = false;
			document.getElementById('alternatorOptions2_2').readOnly = false;
			document.getElementById('alternatorOptions3_3').disabled = false;
			document.getElementById('alternatorOptions1_1').selectedIndex = -1;
			document.getElementById('alternatorOptions2_2').value = "";
			document.getElementById('alternatorOptions3_3').selectedIndex = -1;
		
			document.getElementById('alternatorOptions1').style.display = '';
			document.getElementById('alternatorOptions1_1').style.display = '';
			document.getElementById('alternatorOptions2').style.display = '';
			document.getElementById('alternatorOptions2_2').style.display = '';
			document.getElementById('alternatorOptions3').style.display = '';
			document.getElementById('alternatorOptions3_3').style.display = '';
			
			document.getElementById('starterOptions1').style.display = 'none';
			document.getElementById('starterOptions1_1').style.display = 'none';
			document.getElementById('starterOptions2').style.display = 'none';
			document.getElementById('starterOptions2_2').style.display = 'none';
			document.getElementById('starterOptions3').style.display = 'none';
			document.getElementById('starterOptions3_3').style.display = 'none';
			break;
		case 20:
			document.getElementById('starterOptions1').className = 'search_option';
			document.getElementById('starterOptions2').className = 'search_option';
			document.getElementById('starterOptions3').className = 'search_option';
			document.getElementById('starterOptions1_1').className = '';
			document.getElementById('starterOptions2_2').className = 'text';
			document.getElementById('starterOptions3_3').className = '';
			document.getElementById('starterOptions1_1').disabled = false;
			document.getElementById('starterOptions2_2').readOnly = false;
			document.getElementById('starterOptions3_3').disabled = false;
			document.getElementById('starterOptions1_1').selectedIndex = -1;
			document.getElementById('starterOptions2_2').value = "";
			document.getElementById('starterOptions3_3').selectedIndex = -1;
			
			document.getElementById('starterOptions1').style.display = '';
			document.getElementById('starterOptions1_1').style.display = '';
			document.getElementById('starterOptions2').style.display = '';
			document.getElementById('starterOptions2_2').style.display = '';
			document.getElementById('starterOptions3').style.display = '';
			document.getElementById('starterOptions3_3').style.display = '';
			
			document.getElementById('alternatorOptions1').style.display = 'none';
			document.getElementById('alternatorOptions1_1').style.display = 'none';
			document.getElementById('alternatorOptions2').style.display = 'none';
			document.getElementById('alternatorOptions2_2').style.display = 'none';
			document.getElementById('alternatorOptions3').style.display = 'none';
			document.getElementById('alternatorOptions3_3').style.display = 'none';
			break;
		default:
			document.getElementById('alternatorOptions1').className = 'search_option_disabled';
			document.getElementById('alternatorOptions2').className = 'search_option_disabled';
			document.getElementById('alternatorOptions3').className = 'search_option_disabled';
			document.getElementById('alternatorOptions1_1').className = 'disabled';
			document.getElementById('alternatorOptions2_2').className = 'text_disabled';
			document.getElementById('alternatorOptions3_3').className = 'disabled';
			document.getElementById('alternatorOptions1_1').disabled = true;
			document.getElementById('alternatorOptions2_2').readOnly = true;
			document.getElementById('alternatorOptions3_3').disabled = true;
			document.getElementById('alternatorOptions1_1').selectedIndex = -1;
			document.getElementById('alternatorOptions2_2').value = "";
			document.getElementById('alternatorOptions3_3').selectedIndex = -1;
			
			document.getElementById('alternatorOptions1').style.display = '';
			document.getElementById('alternatorOptions1_1').style.display = '';
			document.getElementById('alternatorOptions2').style.display = '';
			document.getElementById('alternatorOptions2_2').style.display = '';
			document.getElementById('alternatorOptions3').style.display = '';
			document.getElementById('alternatorOptions3_3').style.display = '';
			
			document.getElementById('starterOptions1').style.display = 'none';
			document.getElementById('starterOptions1_1').style.display = 'none';
			document.getElementById('starterOptions2').style.display = 'none';
			document.getElementById('starterOptions2_2').style.display = 'none';
			document.getElementById('starterOptions3').style.display = 'none';
			document.getElementById('starterOptions3_3').style.display = 'none';
			break;
	}
}

function selectCategory(value)
{
	var select = document.getElementById('class_list');
	
	for (var i = 0; i < select.options.length; i++)
	{
		if (select.options[i]['value'].indexOf(value) == 0)
		{
			select.selectedIndex = i;
			break;
		}
	}
	
	select.onchange();
}

function validateProdSearch(eform)
{
	if (eform.class_list.value == '0')
	{
		alert('Please choose a product category to start your search');
		return false;
	}
	
	return true;
}

function validateNumberSearch(eform)
{
	if (eform.number.value == "")
	{
		alert('Please enter the full part number or a part of the number');
		return false;
	}
	
	if (eform.number.value.length < 3)
	{
		alert('You need to enter at least 3 characters');
		return false;
	}
	
	return true;
}

function validateTextSearch(eform)
{
	if (eform.search.value.length < 3)
	{
		alert('You need to enter at least 3 characters');
		return false;
	}
	
	return true;
}