
dojo.require("dojox.fx");

function chooseTopic(topic){
	var topicId = topic.options[topic.selectedIndex].value;
	console.log(topicId);
	setTopic(topicId);
}

function setTopic(topicId){
	var topicArticles = articles[topicId];
	var articleSelect = document.getElementById('article');
	articleSelect.options.length=0;
	
	var o = document.createElement('option');
	o.text = "[select an article]";
	o.value = -1;
	try
	  {
	  articleSelect.add(o,null);
	  }
	catch(ex)
	  {
	  articleSelect.add(o);
	  }
	  
	for (key in topicArticles){
		var article = topicArticles[key];
		var o = document.createElement('option');
		o.text = article.title;
		o.value = article.id;
		try
		  {
		  articleSelect.add(o,null);
		  }
		catch(ex)
		  {
		  articleSelect.add(o);
		  }
	}
	articleSelect.onchange = function(){
		var id = articleSelect.options[articleSelect.selectedIndex].value;
		if(id>-1){
			window.location = "askpros.php?article="+id;
		}
	}
	
	var topicSelect = document.getElementById('topic');
	var topicOptions = topicSelect.options;
	for(var i=0; i < topicOptions.length; i++){
		if(topicOptions[i]['value'] == topicId){
			topicSelect.selectedIndex = i;
		}
	}
}

function setArticle(defaultArticle){
	var articleSelect = document.getElementById('article');
	var articleOptions = articleSelect.options;
	for(var i=0; i < articleOptions.length; i++){
		if(articleOptions[i]['value'] == defaultArticle){
			articleSelect.selectedIndex = i;
		}
	}
}

function appendToSelect(select1, value, content) {
	var opt;
	opt = document.createElement("option");
	opt.appendChild(document.createTextNode(content));
	opt['value'] = value;
	select1.appendChild(opt);
} 

dojo.addOnLoad( function() {
	setTopic(defaultTopic);
	setArticle(defaultArticle);
});