/******************************************

Jquery table functions by Corné Hogerheijde
© 2010 Effectory B.V.

*******************************************/
$(function(){
	try{
		var minReq = 5; //Set the minimum number of option values before showing the selectbox with class datafill  (e.g.: branche)
		var numberOfVals = 10;// Set the minimum number of customers before showing a value as an option
		$("#searchoptions").setSelectBoxes("#myTable", minReq, numberOfVals);
	}catch(e){}

	try{
		$("#myTable").searchable().width(550);
	}
	catch(e){}
	
	try{
		$("#searchoptions").getMyreferrer();		
	}catch(e){}
});
//getMyreferrer - Function made by Corné Hogerheijde
//Reads url and based on regex selects options for customer experience.
//setSelectBoxes - Function made by Corné Hogerheijde
//Input options: targetTable = the table in which values are present. 
// Extra: All data needs to have a <li> item with class="[select options]". Document has to have a <select> with class="datafill" and attribute rel="[select options]" as well as the h6 node
jQuery.fn.extend({
	getMyreferrer: function(){
		if(document.referrer != "" && document.referrer != null)
		{
			var regexer = document.referrer
			$(this).find("select").each(function(){
				var val = null;
				$(this).find("option").each(function(){
					var Eval = $(this).val().replace("MO", "medewerkersonderzoek").replace("KOi", "intern-klantenonderzoek").replace("KO", "klantenonderzoek");
					if(Eval == "")
						Eval = "empty";
					var regex = new RegExp(Eval, "i");
					if(regex.test(regexer))
					{
						val = $(this).val();
						$(this).parents("select").val(val).change();
					}
				});	//Find option
			}); //$(this).find select
		}//if referrer
	}, 
	setSelectBoxes: function(targetTable, MinLength, numberOfVals){
		$(this).find("select").each(function() {
			var optionsArr = new Array(); //Fill with values
			var copyArr = new Array(); //Count occurrences of values
			var finalArr = new Array(); //If count higher than specified -> get value from optionsArr
			var Delete = false; // Check whether class="datafill present. If remains false do nothing.
			
			if($(this).hasClass("datafill"))
			{
				Delete = true;
				Value = $(this).attr("rel");

				$(this).find("option").each(function() {
					if($(this).val() != null && $(this).val() != "")
						$(this).remove();								 
				});
				
				$(targetTable).find("." + Value).each(function() {
					var present = false;
					for(x in optionsArr)
					{
						if($(this).text() == optionsArr[x])
						{
							present = true;
							copyArr[x] = copyArr[x] + 1;
						}
					}
					if(!present)
					{
						optionsArr.push($(this).text());
						copyArr.push(1);
					}
				});
				for(x in optionsArr)
				{
					if(copyArr[x] >= numberOfVals)
						finalArr.push(optionsArr[x]);
				}
			}// if hasclass datafill
			
			if(Delete && finalArr.length >= MinLength)
			{
				finalArr = finalArr.sort();
				var Final = "";
				for(x in finalArr)
				{
					Final = Final + "<option value='" + finalArr[x] + "'>" + finalArr[x] + "</option>";
				}
				$(this).find("option").after(Final);
			}
			else if (Delete)
			{
				//do not show!
				$(this).remove();
				$("h6").each(function() {
					if($(this).hasClass("datafill") && $(this).attr("rel") == Value)
						$(this).remove();
				});
			}//Delete && length
		}); //#searchoptions each
	}	//setSelectBoxes 
});  //extend
//Table searchable http://plugins.jquery.com - Edited by Corné
  (function($){
    $.fn.searchable = function(){
		return this.each(function(){
            var targetTable = this;
			$('#searchTable').keyup(function(event){
                event.preventDefault();
			
				var c = event.keyCode;
				// Check for conditions to trigger auto-search
				if ( (c == 8) || (c == 46) || (c == 109 || c == 189) || (c >= 65 && c <= 90) || (c >= 48 && c <= 57) ) 
				{
                    var keyword = new RegExp($(this).val(), "i");
                    $('tbody tr', targetTable).each(function(){
                        var $tr = $(this);
                        $('td', $tr).filter(function(){
                            return keyword.test($(this).html());
                        }).length ? $tr.show() : $tr.hide();
                	});
                }
            }).keydown(function(event){
				//prevent mainform from postback when enter is hit!
				var c = event.keyCode;	
				if(c==13)
				{   
					return false;
				}
			});
						
			//************************************************************************//
			$('#searchoptions').find("select").each(function() {
					$(this).change(function() {
							//Set vars:
							var val = $(this).val();
							var val_array = new Array();
							
				
							//Fill array with selected values.
							$("#searchoptions").find("select").each(function() {
								if($(this).val() != null && $(this).val() != "")
									val_array.push($(this).val());
							});

							//Make Regexes from selected values -> \\b (2x) search the exact word, .replace all - by nothing
                      						for (x in val_array) {
                      						    val_array[x] = new RegExp("\\b" + val_array[x].replace("-", "") + "\\b", "i");
                      						}
							
							//removepager if val_array not is empty.
							if(val_array.length > 0)
							{	
								//Find table, hide table and if fits user needs show.
								$('tbody tr', targetTable).each(function(){
									var $tr = $(this);
									//Hide all.
									$tr.hide();
									//declare new Array in which all values much be true to show item
									var visArr = new Array();
									
									for(x in val_array)
									{
										var $third = $(this).find("td:nth-child(3)").html().replace("-", "");
										var $fourth = $(this).find("td:nth-child(4)").html().replace("-", "");
										if(val_array[x].test($third) || val_array[x].test($fourth))
											visArr.push(true);
										else
											visArr.push(false);
									}
									//set Visible by default;
									var Visible = true;
									
									for(x in visArr)
									{
										if(visArr[x] == false)
											Visible = false;
									}
									
									if(Visible)
										$tr.show();
								});//tbody
							}
							else
							{
								//show whole table
								$('tbody tr', targetTable).each(function(){
									$(this).show();
								});
							}
						});//this.change
					});//searchoptions find each	
        	}); //this each
    	};//fn searchable
})(jQuery);
        
        
    


