// SUPPORT FOR SERVICES TABLE SEARCH SCRIPT
// support-services-common.js must be included prior to this file

function highlightModels() {

	var maxResults = 5;
	var minChars = 3;
	
	$('#found').attr('id','');
	$('.found').removeClass('found');
	
	var m = $('#model').val().toLowerCase(); // Match is case sensitive so everything is done in lowercase letters
	m.replace('nokia',''); // ^-^
	
	if(m.length < minChars) {$('#model').focus();return false;}
	
	var found = false;
	var msg = {notfound: "The device model you're searching for may not exist, or may be available in another region." , toomany:'models were found. <a href="#found">Go to first result.</a>'};
	var fCount = 0;
	var $foundCell;

	$('tr').find('td:first').each(function() {
		var text = $(this).text().toLowerCase();
		if(text.indexOf(m) !== -1 ) {
			if(!found) {
				$(this).parent().attr('id','found');
				$foundCell = $(this);
			}
			$(this).parent().addClass('found');
			found = true;
			fCount++;
		} 
	});

	fCount < 1 ? $('#msg').html(msg.notfound) :
    fCount > 1 ? $('#msg').html(+fCount+' '+msg.toomany) : $('#msg').html('');

	if(fCount == 1) {
		$foundCell.click(); 
		top.location.hash='found';
	}
	$('#clear').show(); 
	return false;
}


$(function() {
	addForm();
	$('#finder').submit(function () {highlightModels(); return false; })
	$('#model').focus(function(){$(this).select();}).click(function(){this.select();}).focus();
	$('#findModel').click(highlightModels);
	$('#servicesTable td').click(highlightThis);
	// Rivitys
	$('#servicesTable tr:even').addClass('on');
});
