$(document).ready(function(){	
	if($("input#formButton").val() == 'update')
	{
		var make = $("select#make").val();
		var options = '';
		var model_list = models[make].split(';');
		for(var i = 0; i < model_list.length; i++){
			var value_option = model_list[i].split(':');
			options += '<option value="' + value_option[0] + '">' + value_option[1] + '</option>';
		}
		$("select#model").html(options);
		$("select#model").val($("input#model_edit").val());
	}
	$("select#make").change(function(){
								var make = $(this).val();
								var options = '<option value="any">Any</option>';
								if(make == ''){
								}
								else{
									var model_list = models[make].split(';');
									for(var i = 0; i < model_list.length; i++){
										var value_option = model_list[i].split(':');
										options += '<option value="' + value_option[0] + '">' + value_option[1] + '</option>';
									}
								}
								$("select#selected_models").html(options);
								$("select#model").html(options);
							  });  

// sortby redirect code
        $("select#sortedby").change(function(){
                                                if($(this).val() == ''){
                                                }
                                                else{
                                                       	document.location.href = 'http://' + document.location.host + '/cars-for-sale/results.cgi?' + $("#sortby_url_string").val() + '&sortby=' + $(this).val();
                                                }
                                });

// end sortby redirect code

//select_count code
	$("select#select_count").ready(function(){
							$("#select_count_text").hide();
							var start = 1;
							var page_number = 1;
							var count = $("#count").val();
							var options = '';
							if(count < 11){
								$("select#select_count").hide();
								$("#select_count_text").show();
							}
							else{
								while(((start + 9) < count) && ((start + 9) < 5000)){
									options += '<option value="' + page_number + '">' + start + '-' + (start + 9) + '</option>';
									start += 10;
									page_number++;
								}
								if((start + 9) == count){
								}
								else if(count > 5000){
									options += '<option value="' + page_number + '">' + start + '-5000</option>';
								}
								else{
                                                                        options += '<option value="' + page_number + '">' + start + '-' + count + '</option>';
                                                                }
							}
							$("select#select_count").html(options).change(function(){
                                                document.location.href = 'http://' + document.location.host + '/cars-for-sale/results.cgi?' + $("#sortby_url_string").val() + '&page=' + $(this).val() + '&sortby=' + $("select#sortedby").val();
                                });

//set feeds to selected values if it exists
        var param_list = ($("#param_list").val() == undefined)? $("#query_string").val() : $("#param_list").val();
        if(param_list == undefined){
        }else{
                var param = param_list.split('&');
                for(var i = 0; i < param.length; i++){
                        var option_value = param[i].split('=');
                        var option = option_value[0];
                        if(option == 'make'){
                                $("#make").val(unescape(option_value[1]));
                                var options = '<option value="any">Any</option>';
                                if(option_value[1] == ''){
                                }
                                else{
                                        var model_list = models[unescape(option_value[1])].split(';');
                                        for(var j = 0; j < model_list.length; j++){
                                                var value_option = model_list[j].split(':');
                                                options += '<option value="' + value_option[0] + '">' + value_option[1] + '</option>';
                                        }
                                }
                                $("select#selected_models").html(options);
				$("select#model").html(options);
                        }
                        if(option == 'selected_models'){
                                $("#model").val(option_value[1]);
                        }
                        if(option == 'price_min'){
                                $("#pricefrom").val(option_value[1]);
                        }
                        if(option == 'price_max'){
                                $("#priceto").val(option_value[1]);
                        }
                        if(option == 'within'){
                                $("#within").val(option_value[1]);
								
								if ( $(".postcodeHelper").elementExists() )
								{
									var withinValue = $(".postcodeHelper #within").selectedValue();
									
									if ( ($(".postcodeHelper #within").selectedValue() == "scotland") || ($(".postcodeHelper #within").selectedValue() == "uk") )
									{
										//cause of a bug, its supposed to do nothing rather than return false
										//return false;
									}
									else
									{
										$("#postcode2").removeAttr('disabled', 'disabled');
									}
									
								}
								
                                if((option_value[1] != 'scotland') && (option_value[1] != 'uk')){
                                        $("#thePostcode").show()
                                }
                        }
                        if(option == 'cashbackCheckbox'){
                                if(option_value[1] == 'on'){
                                        $("#cashbackCheckbox").attr('checked', true);
                                }
                                else{
                                        $("#cashbackCheckbox").attr('checked', false);
                                }
                        }
                        if(option == 'images_only'){
                                if(option_value[1] == 'on'){
                                        $("#images_only").attr('checked', true);
                                }
                                else{
                                        $("#images_only").attr('checked', false);
                                }
                        }
                        if(option == 'postcode'){
                                $("#postcode2").val(unescape(option_value[1]));
                        }
                        if(option == 'keywords'){
                                $("#keywords").val(unescape(option_value[1]));
                        }
                         if(option == 'sortby'){
                                $("#sortedby").val(option_value[1]);
                        }
                         if(option == 'page'){
                                $("#select_count").val(option_value[1]);
                        }
                }
        }
});

});
