/*
AL - for the js file to work as included as part of the <script src=...> tag 
you need the following added to the web server 
AddType application/x-javascript .js
*/

var xmlhttp

function contentTypeChange(type,explorerType){
	editionscombo = (explorerType == "CHANCONTENT")? document.getElementById('feditions') : document.getElementById('ceditions');
  	//get the current edition for reference
	editionCurSelected = (editionscombo.options.selectedIndex != -1)? editionscombo.options[editionscombo.options.selectedIndex].value : "";
	editionscombo.options.length = 0;  // remove all editions

	var i = 0;
	if(explorerType == "CHANINFO"){
		editionscombo.options[i] = new Option("*", "");
		i++;
	}
		
  	//build the editions for this content type
  	for (var edition in feedExplorer[((type == "")? "channelarticles" : type)]){
  		editionscombo.options[i] = new Option(edition, edition);
  		//set selected edition to this one if it match the previous one
  	 	if(edition == editionCurSelected){
  	    	editionscombo.options[i].selected = true;
  	 	}
  	 	i++;
  	}
	 
  	// if no editions were selected, set to top edition.
  	if(editionscombo.options.selectedIndex == -1){
    	editionscombo.options[0].selected = true;
  	}
	  
  	//fire the editions change handler
  	editionChange(editionscombo.options[editionscombo.options.selectedIndex].value, explorerType);
  	buildURL(explorerType);  // build the URL
}

function editionChange(edition, explorerType){
	contentscombo = (explorerType == "CHANCONTENT")? document.getElementById('fcontenttypes') : document.getElementById('ccontenttypes');
	channelscombo = (explorerType == "CHANCONTENT")? document.getElementById('fchannels') : document.getElementById('cchannels');
	type = contentscombo.options[contentscombo.options.selectedIndex].value;
	channelCurSelected = (channelscombo.options.selectedIndex != -1)? channelscombo.options[channelscombo.options.selectedIndex].value : "";
	channelscombo.options.length = 0;
	
	var i = 0;
	if(explorerType == "CHANINFO"){
		channelscombo.options[0] = new Option("*", "");
		channelscombo.options[0].selected = true;
		i++;
	}
	
	if( (type != "") && (edition != "" ) ){
		for (var channel in feedExplorer[type][edition]){
			channelscombo.options[i] = new Option(channel, feedExplorer[type][edition][channel]);
		  	if(channel == channelCurSelected || channelscombo.options.selectedIndex == -1){
		  		channelscombo.options[i].selected = true;
		  	    channelCurSelected = channel;
		  	}
		  	i++;
		 }
	 }
	 
	 buildURL(explorerType);
  	//temp.options[0].selected=true;
}

function buildURL(explorerType){
  
	contentscombo = (explorerType == "CHANCONTENT")? document.getElementById('fcontenttypes') : document.getElementById('ccontenttypes');
	channelscombo = (explorerType == "CHANCONTENT")? document.getElementById('fchannels') : document.getElementById('cchannels');
	editionscombo = (explorerType == "CHANCONTENT")? document.getElementById('feditions') : document.getElementById('ceditions');
	formatscombo = (explorerType == "CHANCONTENT")? document.getElementById('fformats') : document.getElementById('cformats');
	feedtypecombo = document.getElementById('feedtype');
	callback = (explorerType == "CHANCONTENT")? document.getElementById('fcallback') : document.getElementById('ccallback');
	version = (explorerType == "CHANCONTENT")? document.getElementById('fversion') : document.getElementById('cversion');
	count = document.getElementById('fcount');
	url = (explorerType == "CHANCONTENT")? document.getElementById('furl') : document.getElementById('curl');
	semantic = document.getElementById('fsemantic');
	      
	edition = editionscombo.options[(editionscombo.options.selectedIndex!=0)? editionscombo.options.selectedIndex : 0].value;
	contenttype = contentscombo.options[contentscombo.options.selectedIndex].value;
	channel = channelscombo.options[channelscombo.options.selectedIndex].value;
	format = formatscombo.options[formatscombo.options.selectedIndex].value;
	feedtype = feedtypecombo.options[feedtypecombo.options.selectedIndex].value;
	  
	if(explorerType == "CHANCONTENT"){
		url.value = contentUrlHelper(edition, contenttype, channel, format);

	}else if(explorerType == "CHANINFO"){
		url.value = basechannelurl + "?apikey=" + apikey;
		url.value = url.value + paramUrlHelper(edition, contenttype, channel, format);
	}  
	
	if(feedtype != ""){
		url.value = url.value + "&feedtype=" + feedtype;
	} 
	  
	if( (callback.disabled == false) && (explorerType == "CHANCONTENT") ){
		if(callback.value != ""){
	    	url.value = url.value + "&callback=" + callback.value;
	    }
	}
	     
	if( (count.value != "") && (explorerType == "CHANCONTENT") ){
		url.value = url.value + "&count=" + count.value;
	}
	     
	if( (semantic.value != "") && (explorerType == "CHANCONTENT") ){
		url.value = url.value + "&semantic=" + semantic.value;
	}
		
	if(version.value != ""){
		url.value = url.value + "&version=" + version.value;    
	}
}

function formatChange(explorerType){
	callback = (explorerType == "CHANCONTENT")? document.getElementById('fcallback') : document.getElementById('ccallback');
	formatscombo = (explorerType == "CHANCONTENT")? document.getElementById('fformats') : document.getElementById('cformats');
  	format = formatscombo.options[formatscombo.options.selectedIndex].value;

  	if(format == "json"){
    	callback.disabled = false;
  	}else{
    	callback.disabled = true;
  	}
    
  	buildURL(explorerType);
} 

function insertOption(combo, text, value){
  	var newOption = document.createElement('option');
  	newOption.text = text;
  	newOption.value = value;
  	try{
    	combo.add(newOption,null); // standards compliant
  	}catch(ex){
    	combo.add(newOption); // IE only
  	}
}

function go(explorerType){
	url = (explorerType == "CHANCONTENT")? document.getElementById('furl') : document.getElementById('curl');
  	location = url.value;
}

function contentUrlHelper(edition, contenttype, channel, format){
	if( (edition != "") && (contenttype != "") && (channel != "") && (format != "") ){
		return basefeedurl + "/" + edition + "/" + contenttype + "/" + channel + "/" + format + "?apikey=" + apikey;
	}
	
	return paramUrlHelper(edition, contenttype, channel, format);

}

function paramUrlHelper(edition, contenttype, channel, format){
    var params = "";
	if(edition != ""){
		params = params + "&edition=" + edition;
	} 
	   
	if(contenttype != ""){
		params = params + "&content=" + contenttype;
	}  
	    
	if(format != ""){
		params = params + "&format=" + format;
	}  
	
	if(channel != ""){
		params = params + "&channel=" + channel;
	}  

	return params;
}



