jQuery.noConflict();
jQuery(document).ready(function($) 
{
	////////////////////////////////////////////////
	//
	// Load filter table
	//
	////////////////////////////////////////////////
	$.ajax({
		type: "POST",
		    url: "includes/scripts/js/selectionGuide/selection_guide_table.php",
		    success: function(msg){ 
		    $("#filterTable").html(msg);
		    $(".select").change(function () {
			    var str = "";
			    var name = "";
			    var filterValue = new Array();
			    var filter = new Array();
			    filter[0] = "LCDemulation";
			    filter[1] = "LCDfootprint";
			    filter[2] = "Outline";
			    filter[3] = "ViewingArea";
			    filter[4] = "DisplayFormat";
			    filter[5] = "DisplayGraphics";
			    filter[6] = "Power";
			    filter[7] = "CharacterHeight";
			    filter[8] = "NumberofDots";
			    filter[9] = "Interface";
			    filter[10] = "Series";
			    filter[11] = "OperatingTemp";
			    filter[12] = "Features";
	
			    ////////////////////////////////////////////////
			    //
			    // Two methods to "post"to php
			    // 1. Long string with delimiter dividing filter
			    //    name and values. I.E. "|" & ";"
			    // 2. Array
			    //
			    //////////////////////////////////////////////// 
					
			    //Method 01
			    for(var i in filter) 
				{
				    str = "";
				    $("#"+filter[i]+" option:selected").each(function (index) {
					    str += $(this).val() + "|";
					});	
				    filterValue[i] = str;
				}
			    var query = "LCDemulation="+filterValue[0]+"&"+"LCDfootprint="+filterValue[1]+"&"+"Outline="+filterValue[2]+"&"+"ViewingArea="+filterValue[3]+"&"+"DisplayFormat="+filterValue[4]+"&"+"DisplayGraphics="+filterValue[5]+"&"+"Power="+filterValue[6]+"&"+"CharacterHeight="+filterValue[7]+"&"+"NumberofDots="+filterValue[8]+"&"+"Interface="+filterValue[9]+"&"+"Series="+filterValue[10]+"&"+"OperatingTemp="+filterValue[11]+"&"+"Features="+filterValue[12];
			    $.ajax({
				    type: "POST",
					url: "includes/scripts/js/selectionGuide/selection_guide_results.php",
					data: query,
					beforeSend: function(){
					$("#results").html("<img src='images/spinner.gif'> Loading...");  
				    },
					success: function(results){
					$("#results").html(results);
			 		$.tablesorter.addParser({
						// set a unique id
						id: 'number',
						    is: function(s) {
						    // return false so this parser is not auto detected
						    return false;
						},
						    format: function(s) {
						    // format your data for normalization
						    var d = s.substr(0,6);
						    return d.replace("x", "");
						},
						    // set type, either numeric or text
						    type: 'numeric'
						    });
					$("#specTable").tablesorter({
						headers: {
						    0: { sorter: false },
							3: { sorter:'number' },
							4: { sorter:'number' },
							5: { sorter:'number' },
							6: { sorter:'number' },
							//8: { sorter:'number' },
							10: { sorter: false }
						}
					    });
				
				    }
				});
			}).change();						
		}
	    });

	$("#Compare").click(function () {	
		var checkbox = "";
		$("input:checkbox:checked").each(function (index) {
			checkbox += $(this).val() + ",";
		});	
		
		if( checkbox != "") {
			var query = "ID="+checkbox;
			
			$.ajax({
				type: "POST",
				url: "includes/scripts/js/selectionGuide/selection_guide_compare.php",
				data: query,
				beforeSend: function(){
					$("#results").html("<img src='images/spinner.gif'> Loading...");  
		  		},
			  	success: function(compared) {
					$('#results').html(compared);
					var backLink = "<div class='floatLeft marginL10px'><p>| </p></div>"+
									"<div class='floatLeft marginL10px'><p><a href='#' id='Back'>View List</a></p> </div>"+
									"<div class='floatFix'></div>";
					$('#backContent').html(backLink);
					$("#Back").click(function () {
						$(".select").change();	
						$('#backContent').html("");
					});	
			  	}
			});
		}
	});	
					  
	$("#CSV").click(function () {
		var str = "";
		var name = "";
		var filterValue = new Array();
		var filter = new Array();
		filter[0] = "LCDemulation";
		filter[1] = "LCDfootprint";
		filter[2] = "Outline";
		filter[3] = "ViewingArea";
		filter[4] = "DisplayFormat";
		filter[5] = "DisplayGraphics";
		filter[6] = "Power";
		filter[7] = "CharacterHeight";
		filter[8] = "NumberofDots";
		filter[9] = "Interface";
		filter[10] = "Series";
		filter[11] = "OperatingTemp";
		filter[12] = "Features";
	
		////////////////////////////////////////////////
		//
		// Two methods to "post"to php
		// 1. Long string with delimiter dividing filter
		//    name and values. I.E. "|" & ";"
		// 2. Array
		//
		//////////////////////////////////////////////// 
					
		//Method 01
		for(var i in filter) 
		    {
			str = "";
			$("#"+filter[i]+" option:selected").each(function (index) 
								 {
								     str += $(this).val() + "|";
								 });	
			filterValue[i] = str;
		    }
		var query = "CSV=1"+"&"+"LCDemulation="+filterValue[0]+"&"+"LCDfootprint="+filterValue[1]+"&"+"Outline="+filterValue[2]+"&"+"ViewingArea="+filterValue[3]+"&"+"DisplayFormat="+filterValue[4]+"&"+"DisplayGraphics="+filterValue[5]+"&"+"Power="+filterValue[6]+"&"+"CharacterHeight="+filterValue[7]+"&"+"NumberofDots="+filterValue[8]+"&"+"Interface="+filterValue[9]+"&"+"Series="+filterValue[10]+"&"+"OperatingTemp="+filterValue[11]+"&"+"Features="+filterValue[12];
		$.ajax({
			type: "POST",
			    url: "includes/scripts/js/selectionGuide/selection_guide_results.php",
			    data: query,
			    beforeSend: function(){
			    $("#downloadData").html("<img src='images/spinner.gif'> Preparing data...");  
			},
			    success: function(EXCEL)
			    {
				var CSVlink = "<br /><a href='includes/scripts/js/selectionGuide/selection_guide_csv.php?CSV="+EXCEL+"' id='downloadCSV' class='CSVdownloadLink link'>Download CSV</a>";
				$("#downloadData").html(CSVlink);
				$("#downloadCSV").click(function () 
							{
							    $("#downloadData").html("");
							});	
			    }
		    });
	    });
});

