var last_query = '';
function urlencode (str) {
    str = (str + '').toString();
     // Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
    // PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
    replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}

function update_handsets(hval){
	$('.handset_list').load("get_handsets.php?handset_manufacturer_id="+hval+"");
}

function do_search(){
		$('.search_arg:disabled').removeAttr('disabled');
		if ($('.search_arg[name=tariff_type]').val() == 'payg'){
			$('.search_arg[name=min_total_cost_contract]').attr('disabled','disabled');
			$('.search_arg[name=max_total_cost_contract]').attr('disabled','disabled');
			$('.search_arg[name=min_line_cost]').attr('disabled','disabled');
			$('.search_arg[name=max_line_cost]').attr('disabled','disabled');
			$('.search_arg[name=min_inclusive_mins]').attr('disabled','disabled');
			$('.search_arg[name=min_inclusive_txts]').attr('disabled','disabled');
		}

		var query_str = '';
		var amp = '';
		$('.search_arg').each(function(){
			if ($(this).is(':disabled')){

			}else{
				if ($(this).attr('type') != 'radio' || $(this).is(":checked")){
					query_str = query_str + amp + $(this).attr('name') + '=' + urlencode($(this).val());
					amp = '&';
				}
			}
		});
		if (query_str == last_query){
			//alert("no changes detected.");
		}else{
			$("#search_results").load("do_search.php?"+query_str+"");
			last_query = query_str;
		}
	}


$(document).ready(function(){


	$('.search_arg').change(function(){
		do_search();
		
	});

	$('.search_arg').click(function(){
		do_search();

	});


	$('.trigger_search').click(function(){
		do_search();
	});


	$('.update_handset').change(function(){
		update_handsets($(this).val());
	});

	
	do_search();

	setTimeout("do_scroll();","5000");

	$(".slide_left").click(function(){



		if(scroll_count > 11){
			scroll_count += 1;
		}else{
			scroll_count -= 1;
		}

		$('.slider').animate({scrollLeft: '-=160'},'slow');



	});

	$(".slide_right").click(function(){
		if(scroll_count > 11){
			scroll_count -= 1;
		}else{
			scroll_count += 1;
		}

		$('.slider').animate({scrollLeft: '+=160'},'slow');



	});


});


var scroll_count = 0;
var direction = '+';
function do_scroll(){
	//alert ("word");
	scroll_count += 1;

	if(scroll_count > 11){
		direction = '-';
	}

	if (scroll_count > 22){
		direction = '+';
		scroll_count = 1;
	}

	$('.slider').animate({scrollLeft: ''+direction+'=160'},'slow');

	

	setTimeout("do_scroll();","5000");
}

