$(document).ready(function(){
	$('#studRefineBtn').bind('click', function(e){runStudSearch();})
});

function runStudSearch(e){
	/*$('studResults').html('Loading...');*/
	$.ajax({
		type: "GET",
		url: "/_commongoal/cg_content/StudProducts_ajax.cfc",
		dataType: "json",
		data: {
		   method: 'searchStuds',
		   style: $('#style').val(),
		   thread: $('#thread').val(),
		   diameter: $('#diameter').val()/*,
		   compression: $('#compression').val()*/
		},
		beforeSend: fadeLoadIn,
		success: processStudRes
	});
}

var fadeLoadIn = function(){
	$('studResults').html('Loading...');
}

var processStudRes = function(res){
	if(res.length < 101){
		if(res.length == 0){
			$('#studResults').html('<p>No results found matching your criteria.</p>');	
		}
		else{
			$('#studResults').html('');
			var resultsHtml = $('#studResults').html();
			resultsHtml += '<table id="studResultsTable" cellpadding="0" cellspacing="0"><tr><th style="padding-left:8px;">Part #</th><th>Style</th><th>Diameter (D)</th><th>Thickness (M)</th><th>Thread Size</th><th>Stud Length (S)</th><th>Max. Shear Load (lbs)</th><th>Max. Compression Load (lbs)</th><th>Material</th></tr>'
			$.each(res, function(i, studProds){			
				resultsHtml +='<tr><td style="padding-left:8px; width:50px;">' + studProds.PARTNUM + '</td><td>'+ studProds.STYLE +'</td><td>'+ studProds.DIAMETER +'</td><td>'+ studProds.THICKNESS +'</td><td>'+ studProds.THREADSIZE +'</td><td>'+ studProds.LENGTH +'</td><td>'+ studProds.SHEARLOAD +'</td><td>'+ studProds.COMPRESSION +'</td><td>'+ studProds.MATERIAL +'</td></tr>';
			});
			resultsHtml +='</table>';
			$('#studResults').html(resultsHtml);
			
			$('#studResults tr:odd').addClass('searchAlt');
			$('#studResults td').css({'padding-top':'5px', 'padding-bottom':'5px'});
		}
	}
	else{
		$('#studResults').html('<p>Your search has returned more than 100 results. <br />Please narrow your search or <a href="/documents/file/Stud%20Mounts.pdf" target="_blank">view the datasheet</a> to view all products.</p>');
	}
}

function studResetSearch(){
	 $("#style option[value='']").attr('selected', 'selected');
	 $("#thread option[value='']").attr('selected', 'selected');
	 $("#diameter option[value='']").attr('selected', 'selected');
	 $('#studResults').html('');
}