	
/*
	Search JavaScript - FlashGeeks 2010.
*/

function showOptions(divId) {
	if(!$('#'+divId).is(":visible")) {
		$('#'+divId).slideDown();
		
		/*$('#'+divId+'.show').fadeIn();
		$('#'+divId+'.hide').fadeOut();*/

		$('.'+divId).find(".show").show();
		$('.'+divId).find(".hide").hide();

	} else {
		$('#'+divId).slideUp();
		
		$('.'+divId).find(".show").hide();
		$('.'+divId).find(".hide").show();


	}
}


function getURLParameter(name) {
    return unescape(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}

function filterResults() {

}

function tickbox(id) {
	if($('input[name='+id+']').is(':checked')) {
		$('input[name='+id+']').attr('checked', false);
	} else {
		$('input[name='+id+']').attr('checked', true);
	}
	if($('input[name=winter]').is(':checked')) {
		
		winterSpeedChange(1);
		}
		
}
// paul added function for changing speed rating on winter box tick
function winterSpeedChange(num){
	if($('input[name=winter]').is(':checked')) {
		var selObj = document.getElementById('tyreSpeedId');
		selObj.selectedIndex = num;
		
//		if (selObj.disabled==false){
//			selObj.disabled = true;
//			}else{
//			selObj.disabled=false;
//			}
	}
	}
	

	
function checkWinterchecked(num){
	var selObj = document.getElementById('tyreSpeedId');
		selObj.selectedIndex = num;
		selObj.disabled = true;
			
	}
// end paul edit
var ratingsArray = Array("N", "P", "Q", "R", "S", "T", "U", "H", "V", "W", "Y", "Z");
var speedRating;



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];
}

function filterSpeedRatings(currentSpeedRating) 
{
	if(gup('tyreSpeedId')!=0) {
		slowerrating = false;
		fasterrating = false;

		speedRating = currentSpeedRating;
		$(".sr"+currentSpeedRating).show();
		
		//toggleFasterRatings(true);
		//toggleSlowerRatings(false);


		if(speedRating == "Y" || speedRating=="W") {
			//toggleExceptions(true);
		}

		if(!slowerrating && !fasterrating) {
			$("#differentRating").fadeOut();
		}
	} else {
		$(".hidden").show();
	}

	

}

function toggleFasterRatings(showBool)
{
	found = false;
	total = 0;
	sizes = "";
	
	for(i=0;i<ratingsArray.length;i++) {
		
		if(!found) {
			
			if(ratingsArray[i] == speedRating) {
				found=true;
			} 

		} else {
			
			if(showBool) {
				$(".sr"+ratingsArray[i]).show();
			} else {
				$(".sr"+ratingsArray[i]).hide();
			}

			total += $(".sr"+ratingsArray[i]).length;
			
			if($(".sr"+ratingsArray[i]).length>0) {
				sizes += ratingsArray[i] + ",";
			}
		} 
	}
	

	if(total>0) {

		fasterating = true;
		if(showBool) {
			$("#extraresults").html("Included in your results are "+total+ " tyres with a higher speed rating <span class='bluesize'>"+sizes+"</span> - <a href='javascript:toggleFasterRatings(false)'>Click here to hide</a>");
		} else {
			$("#extraresults").html("Also available are "+total+ " results with a higher speed rating <span class='bluesize'>"+sizes+"</span> - <a href='javascript:toggleFasterRatings(true)'>Click here to show</a>");
		}
	}

	updateImages(direction);
}



function showSlowConfirmation()
{
	$("#hiddenspeeds").html("");
	$("#speedconfirmation").fadeIn();
}

function toggleExceptions(showBool) 
{
	included = "";
	total = 0;

	if(showBool) {
		if(speedRating=="Y") {
			
			$(".srW").show();
			$(".srZ").show();
			
			total = $(".srW").length + $(".srZ").length;

			included = "W,Z";

		} else if(speedRating=="W") {
			$(".srZ").show();
			included = "Z";
			total = $(".srZ").length;
		}
		
		$("#exceptionspeeds").html("Also included in your results are "+total+" tyres with "+included+" speed rating. <a href='javascript:toggleExceptions(false)'>Click here to hide</a>");

	} else {
		
		if(speedRating=="Y") {
			$(".srW").hide();
			$(".srZ").hide();
		} else if(speedRating=="W") {
			$(".srZ").hide();
		}
		
		$("#exceptionspeeds").html("");

	}

}

function toggleSlowerRatings(showBool)
{
	found = false;
	total = 0;
	sizes = "";
	
	
	for(i=ratingsArray.length;i>0;i--) {
		if(!found) {
			if(ratingsArray[i] == speedRating) {
				found=true;
			} 
		} else {
			if(showBool) {
				$(".sr"+ratingsArray[i]).show();
			} else {
				$(".sr"+ratingsArray[i]).hide();
			}
			
			total += $(".sr"+ratingsArray[i]).length;

			if($(".sr"+ratingsArray[i]).length>0) {
				sizes += ratingsArray[i] + ",";
			}
		} 
	}
	if(total>0) {
		
		slowerrating = true;

		if(showBool) {
			$("#hiddenspeeds").html("Included in your results are "+total+ " results with a lower speed rating <span class='bluesize'>"+sizes+"</span> - <a href='javascript:toggleSlowerRatings(false)'>Click here to hide</a>");
		} else {
			$("#hiddenspeeds").html("Also available are "+total+ " tyres with a lower speed rating <span class='bluesize'>"+sizes+"</span> - <a href='javascript:toggleSlowerRatings(true)'>Click here to show</a>");
		}
	}
}


function showAllSpeedRatings() 
{	
	$("#hiddenspeeds").html("Included in your results are "+hiddenTotal +" results with lower speed ratings <span class='bluesize'>"+hiddenSpeeds +"</span> - <a href='javascript:filterSpeedRatings()'>Click here to hide</a>");
	ratingsArray = Array("N", "P", "Q", "R", "S", "T", "U", "H", "V", "Z", "W", "Y");

	for(i=0;i<ratingsArray.length;i++) {
		$(".sr"+ratingsArray[i]).show();
	}
}

/*
function filterResultsExtra() 
{
	
	if(getURLParameter("bridgestone")=="on" || getURLParameter("continental")=="on" || getURLParameter("dunlop")=="on" || getURLParameter("firestone")=="on" || getURLParameter("goodyear")=="on" || getURLParameter("michelin")=="on" || getURLParameter("pirelli")=="on" || getURLParameter("economy")=="on" || getURLParameter("midrange")=="on") {
		showOptions('advSearch1');
		showOptions('advSearch2');
		filter=true;	
	}

	if(!filter) {
		if(getURLParameter("runflat")=="on" || getURLParameter("caravan")=="on" || getURLParameter("winter")=="on") {
			showOptions('advSearch1');
			filter=true;
		}
	}

	if(getURLParameter("runflat")=="on") {
		$(".runflat").show();

	}

	if(getURLParameter("winter")=="on") {
		$(".winter").show();
	}

	if(getURLParameter("caravan")=="on") {
		$(".caravan").show();
	}
	

	if(getURLParameter("midrange")=="on") {
		$(".mid").show();
	}

	if(getURLParameter("economy")=="on") {
		$(".economy").show();
	}

	if(getURLParameter("bridgestone")=="on") {
		$(".bridgestone").show();
	}

	if(getURLParameter("continental")=="on") {
		$(".continental").show();
	}

	if(getURLParameter("runflat")=="on") {
		$(".runflat").show();
	}

	if(getURLParameter("dunlop")=="on") {
		$(".dunlop").show();
	}

	if(getURLParameter("firestone")=="on") {
		$(".firestone").show();
	}

	if(getURLParameter("goodyear")=="on") {
		$(".goodyear").show();
	}


	if(getURLParameter("michelin")=="on") {
		$(".michelin").show();
	}

	if(getURLParameter("pirelli")=="on") {
		$(".pirelli").show();
	}		

}
*/

function changedWinter(id) {
	if($('#'+id).val() < 4) {
		window.alert("We recommend you buy these tyres in a set of 4.");	
	}
}

