
	
	/**
	 * Attach the page redirect function to the "change" event of 
	 * the "Quick Search by Name" pulldown menu 
	 */
	function init_quick_search_by_name(pulldown_id) {
		
		$(pulldown_id).bind("change", function() {
					
			fid = $(pulldown_id + ' option:selected').val();
			if (fid == "") { return false; }
			
			if (fid == "all") { 
				window.location = "http://www.franchisevideos.com/all-franchises.php?sort=Name&order=ASC";
				return true;
			}
					
			window.location = "http://www.franchisevideos.com"
			                + "/franchise.php?fid=" + fid;
		});
	}
	
	
	
	/**
	 * Attach the page redirect function to the "change" event of 
	 * the "Quick Search by Category" pulldown menu 
	 */
	function init_quick_search_by_category(pulldown_id) {
		$(pulldown_id).bind("change", function() {
					
			cid = $(pulldown_id + ' option:selected').val();
			if (cid == "") { return false; }
			
			if (cid == "all") { 
				window.location = "http://www.franchisevideos.com/all-franchises.php?sort=Name&order=ASC";
				return true;
			}
					
			window.location = "http://www.franchisevideos.com"
			                + "/franchise-category.php?cid=" + cid;
		});
	}
	
	
	
	/**
	 * Attach the page redirect function to the "change" event of 
	 * the "Quick Search by Price" pulldown menu 
	 */
	function init_quick_search_by_price(pulldown_id) {
	
		$(pulldown_id).bind("change", function() {
				
				
			both_values = $(pulldown_id + ' option:selected').val();
			
			if (both_values == "") { 
				return false; 
			}
			
			if (both_values == "all") { 
				window.location = "http://www.franchisevideos.com/all-franchises.php?sort=CashRequiredLow&order=ASC";
				return true;
			}
				

				
			pieces = both_values.split("-");

			/* Create URL paramaeters string */
			if (pieces[0] == "under") {
				params = "?highPrice="+pieces[1];
			} else if (pieces[0] == "over") {
				params = "?lowPrice="+pieces[1];
			} else {
				params = "?lowPrice="+pieces[0]+"&highPrice="+pieces[1];
			}
			
			window.location = "http://www.franchisevideos.com/franchise-price.php"+params;			
		});
	}
		
	
		
	/**
	 * 
	 */
	function init_sort_by(sort_pulldown_id, order_pulldown_id) {
	
		// attach onchange sort to populate the order
		$(sort_pulldown_id).bind(
			"change", 
			function() { 
			
				var asc_selected = "";
				var desc_selected = "";
			
				if (gup('order') == 'ASC') {
					asc_selected = ' selected="selected" ';
				} else if (gup('order') == 'DESC') {
					desc_selected = ' selected="selected" ';
				}
			
				/* Populate "Order By" dropdown */
				if ($(this).val() == "Name") {
				
					$(order_pulldown_id).empty();
					$(order_pulldown_id).append("<option value='ASC'" + asc_selected + ">A-Z</option>");
					$(order_pulldown_id).append("<option value='DESC'" + desc_selected + ">Z-A</option>");
					
				} else if ($(this).val() == "CashRequiredLow" || $(this).val() == "InvestmentLow") {
				
					$(order_pulldown_id).empty();
					$(order_pulldown_id).append("<option value='ASC'" + asc_selected + ">Low-High</option>");
					$(order_pulldown_id).append("<option value='DESC'" + desc_selected + ">High-Low</option>");
					
				} else {
				
					$(order_pulldown_id).empty();
					$(order_pulldown_id).append("<option value='ASC'" + asc_selected + ">Ascending</option>");
					$(order_pulldown_id).append("<option value='DESC'" + desc_selected + ">Descending</option>");
					
				}
				
			} 
		);
		
		// trigger onchange
		$(sort_pulldown_id).trigger("change");
	
	}
		
		
		
	/**
	 * Record when someone clicks on a featured franchise
	 */
	function record_featured_franchise_click(id) {

		/* Make a synchronous GET request to track the click, then redirect to the page */
		$.ajax({
			async: false,
			type: "GET",
			url: "http://www.franchisevideos.com/php/api.php",
			data: "action=featured_franchises&fid="+id,
			success: function(msg){ 
				window.location = "http://www.franchisevideos.com/franchise.php?fid="+id; 
			},
			error: function(msg){ 
				window.location = "http://www.franchisevideos.com/franchise.php?fid="+id; 
			}
		});
		
			
		return false;
	}

	
	
	
	
	
	/**
	 * Get specified URL parameter
	 */
	function gup(name) {
	
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");

		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );

		if( results == null )
		return "";
		else
		return results[1];
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	