/* Written, mostly by Michael Niblett (borft@openoffice.org)
 * written for OpenOffice.org mirrors project
 * This js may be used under GPL
 */
function initForm(){
   // set different versions
	addSelectOption("platform", "Choose platform", "", false);

	for (var key in ooo){
		addSelectOption("platform", key, key, false);
	}
}

function updateVersion(){
	var platform = document.getElementById("platform").value;
	var versions = ooo[platform];

	document.download.version.length = 0;
	document.download.lang.length = 0;

	addSelectOption("version", "Choose version", "", false);

	for ( var key in versions ){
		addSelectOption("version", "OpenOffice.org " + key, key, true);
	}
}


function updateLang(){
	// set langs available      
	var version =  document.getElementById("version").value;
	var platform =  document.getElementById("platform").value;
	var langs = ooo[platform][version];

	// reset list
	document.download.lang.length=0;
	addSelectOption("lang", "Choose language", "", false);
	
	// a sourcefile doesn't have a language
	// so select one, and send torrent
	if ( platform == "source" ){
		addSelectOption("lang", "en", "en", false);
		document.download.lang.disabled=true;
		sendTorrent();
	}
	else{
		document.download.lang.disabled=false;
		// search platforms for selected version
		for ( var key in langs ){
			addSelectOption("lang", key, key, true);
		}
	}
}

function sendTorrent(){
   var version =  document.getElementById('version').value;
   var platform =  document.getElementById('platform').value;
   var lang = document.getElementById('lang').value;
   var torrent = ooo[platform][version][lang];
   if ( torrent != undefined ){
      document.location=torrent;
   }
}


/* kindly donated by Tom Jansen */
function addSelectOption(selectParentId, optionText, optionValue, ordered){
	/** Find the SELECT we are going to add to */
	var spid = document.getElementById(selectParentId);
	/** Create the new option */
	var opt = document.createElement('OPTION');
	
	opt.setAttribute('value', optionValue);
	opt.appendChild(document.createTextNode(optionText));
	
	if (spid == null || opt == null) {
		alert('addSelectOption: parent DOM element (id: ' + selectParentId + ') not found or child node creating failed.');
	}
	
	var child = null;
	
	if (ordered != 'undefined' && ordered == true) {
		for (child = spid.firstChild; 
			child != null; 
			child = child.nextSibling) {
	
			if (child.value < optionValue) {
				continue;
			}
			else {
				break;
			}
		}
	}
	/** If child == null, this is the last or the only element
	*  in the SELECT */
	if (child == null) {
		spid.appendChild(opt);
	}
	else {
		spid.insertBefore(opt, child);
	}
	
	return opt;
} 
