var model_id_selected='';
var results;


function resetForm(){
	$('test_json').clear();
	$('test_json').enable();
	$('make_select').enable();
	$('make_select').clear();
	$("mobile_results").innerHTML='';
	$('mobile_id').hide();
	$('full_list_link').show();
	results=null;
	closeComment();
}



function start(){
	getModels();
	$('start').hide();
	$('startstage').hide();
	
	$('reset').show();
	$('reset').show();
}


/* 
Gets the data for question 1

*/ 
function getModels() {
	var loading = "<b>Loading phone models <\/b><img src=\"http://images2.moneysavingexpert.com/images/recycling/33_ball_two_24.gif\">";
	$('loadingphonedata').innerHTML=loading;
	var myAjax = new Ajax.Request(
			'http://www.moneysavingexpert.com/recycling/make/', 
			{
				method: 'get', 
				parameters: '', 
				onComplete: function(response) {
					 makes = eval('(' + response.responseText + ')');
					 buildInput(makes, $('model_form'));
					 $('loadingphonedata').hide();
					 $('make_data').show();
				}
			});
}
function setMake(id){
		model_id_selected = id;
		$("mobile_results").innerHTML='';
		$('mobile_id').show();
		

}

function sortBy(column){
	results.sortOutput(column);
}

function  hideColumn(columnname){
	results.showHideColumns(columnname);
}


function buildInput(make,contain){
	make_input="<select name=\"make_select\" id=\"make_select\" onchange=\"setMake(this.value)\"><option value=''><\/option>";
	for(i = 0; i < make['results'].length; i++) {
	
		 make_input += "<option value=\""+make['results'][i].id+"\">"+make['results'][i].name+"<\/option>";
	}
	make_input += "<\/select>";
	contain.innerHTML = make_input;
	
}






/*
This function generates the drop down list that appears on question 2
*/
function getMobilePhoneDropDown(){
	var options = {
		script:'http://www.moneysavingexpert.com/recycling/make-model/json=true&limit=6&',
		varname:'input',
		json:true,
		shownoresults:true,
		maxresults:16,
		callback: function (obj) { 
			getData(obj.id);
		}
		};
		var json=new AutoComplete('test_json',model_id_selected,options);

}

/* This function actually gets the result and displays them*/
function getData(id){
	$('test_json').disable();
	$('make_select').disable();
	$('full_list_link').hide();
	
	var options = {
			script:'http://www.moneysavingexpert.com/recycling/phone_data-results/json=true&limit=6&',
			varname:'id',
			json:true,
			shownoresults:true,
			maxresults:16,
			callback: function (obj) { },
			onComplete: function(response) {
			
			}
		};
		results=new MobileResults(id,options);
	

}





var openid=-1;
function loadTypeInfo(displayid){
	if(openid!=-1){
		document.getElementById('display_'+openid).style.display='none';
	}
	openid = displayid;
	document.getElementById('display_'+displayid).style.display='block';
	return false;
}
				
function closeComment(){
	if(openid!=-1){
	document.getElementById('display_'+openid).style.display='none';
	openid=-1;
	}
}

/*
Function to load the full list screen taking the make info from what is set as model_id
*/
function openScreen(){
	dialogArguments = $('test_json');
	window.open('http://www.moneysavingexpert.com/recycling/full-list/makeid='+model_id_selected,'name',"status=1, scrollbars=yes, width=650,height=500 ");
	return false;
}
	
