/*-------------------------------------------------------------------
--- Liste der möglichen Platformen (sortiert nach nach Auftreten in
---	der Auswahlbox)
---	 <key>,<Bezeichnung>
-------------------------------------------------------------------*/

var platforms =	[
	['ooo', '--- OpenOffice.org ---'],
	['ext', '--- Extensions ---'],
	['win', 'Windows'],
	['lin', 'Linux (инсталация)'],
	['lin_32', 'Linux 32бит (RPM)'],
	['lin_32d', 'Linux 32бит (deb)'],
	['lin_64', 'Linux AMD 64бит (RPM)'],
	['lin_64d', 'Linux AMD 64бит (deb)'],
	['mac_intel', 'MacOS X Intel'],
	['mac_ppc', 'MacOS X PPC'],
	['freebsd_8_64', 'FreeBSD 8 AMD 64бит'],
	['freebsd_72_64', 'FreeBSD 7.2 AMD 64бит'],
	['freebsd_71_32', 'FreeBSD 7.1 32бит'],
	['freebsd_71_64', 'FreeBSD 7.1 AMD 64бит'],
	['sol_sparc', 'SolarisSparc'],
	['sol_x86', 'Solarisx86']
];

/*-------------------------------------------------------------------
--- Liste der möglichen Versionen nach Plattform
---	(sortiert nach nach Auftreten in der Auswahlbox)
---		<platform_key>, <key>, <Bezeichnung>, <pfad auf server>
-------------------------------------------------------------------*/
var versions =	[
	['ext', '3.1.1', '3.1.1', '3.1.1/extensions/'],
	['ext', '3.1', '3.1', '3.1.0/extensions/'],
	['ext', '3.0.1', '3.0.1', '3.0.1/extensions/'],

	['win', '3.1.1', '3.1.1', '3.1.1/windows/'],
	['win', '3.1', '3.1', '3.1.0/windows/'],
	['win', '3.0.1', '3.0.1', '3.0.1/windows/'],
	['win', '3.0', '3.0', '3.0.0/windows/'],
	['win', '2.4.1', '2.4.1', '2.4.1.1/windows/'],
	['win', '2.4', '2.4', '2.4/windows/'],
	['win', '1.1.4', '1.1.4', '1.1.x/OOo_1.1.4_Win32Intel_install_bg.zip'],

	['lin', '1.1.5', '1.1.5', '1.1.x/OOo_1.1.5_LinuxIntel_install_bg.tar.gz'],
	['lin', '1.1.4', '1.1.4', '1.1.x/OOo_1.1.4_LinuxIntel_install_bg.tar.gz'],

	['lin_32', '3.1.1', '3.1.1', '3.1.1/linux/x86-32/'],
	['lin_32', '3.1', '3.1', '3.1.0/linux/x86-32/'],
	['lin_32', '3.0.1', '3.0.1', '3.0.1/linux/'],
	['lin_32', '3.0', '3.0', '3.0.0/linux/'],
	['lin_32', '2.4.1', '2.4.1', '2.4.1.1/linux/'],
	['lin_32', '2.4', '2.4', '2.4/linux/'],

	['lin_32d', '3.1.1', '3.1.1', '3.1.1/linux/x86-32/'],
	['lin_32d', '3.1', '3.1', '3.1.0/linux/x86-32/'],
	['lin_32d', '3.0.1', '3.0.1', '3.0.1/linux/'],
	['lin_32d', '3.0', '3.0', '3.0.0/linux/'],
	['lin_32d', '2.4.1', '2.4.1', '2.4.1.1/linux/'],
	['lin_32d', '2.4', '2.4', '2.4/linux/'],

	['lin_64', '3.1.1', '3.1.1', '3.1.1/linux/x86-64/'],
	['lin_64', '3.1', '3.1', '3.1.0/linux/x86-64/'],
	['lin_64', '3.0.1', '3.0.1', '3.0.1/linux64/'],
	['lin_64', '3.0', '3.0', '3.0.0/linux64/'],
	['lin_64', '2.4.1', '2.4.1', '2.4.1.1/linux64/'],

	['lin_64d', '3.1.1', '3.1.1', '3.1.1/linux/x86-64/'],
	['lin_64d', '3.1', '3.1', '3.1.0/linux/x86-64/'],
	['lin_64d', '3.0.1', '3.0.1', '3.0.1/linux64/'],
	['lin_64d', '3.0', '3.0', '3.0.0/linux64/'],
	['lin_64d', '2.4.1', '2.4.1', '2.4.1.1/linux64/'],

	['freebsd_8_64', '3.1.1', '3.1.1', '3.1.1/freeBSD8/amd64/'],

	['freebsd_72_64', '3.1', '3.1', '3.1.0/freeBSD72/amd64/'],

	['freebsd_71_32', '3.0', '3.0', '3.0.0/freeBSD71/i386/'],
	['freebsd_71_64', '3.0', '3.0', '3.0.0/freeBSD71/amd64/'],

	['mac_ppc', '3.1', '3.1', '3.1.0/macOSX/PPC/'],
	['mac_ppc', '3.0', '3.0', '3.0.0/macOSX_ppc/'],

	['mac_intel', '3.1.1', '3.1.1', '3.1.1/macOSX/Intel/'],
	['mac_intel', '3.1', '3.1', '3.1.0/macOSX/Intel/'],
	['mac_intel', '3.0.1', '3.0.1', '3.0.1/macOSX_Intel/'],
	['mac_intel', '3.0', '3.0', '3.0.0/macOSX_Intel/'],
	['mac_intel', '2.4.1', '2.4.1', '2.4.1.1/macOSX_Intel/'],
	['mac_intel', '2.4', '2.4', '2.4/macOSX_Intel/'],

	['sol_sparc', '3.1.1', '3.1.1', '3.1.1/solaris/Sparc/'],
	['sol_sparc', '3.1', '3.1', '3.1.0/solaris/Sparc/'],

	['sol_x86', '3.1.1', '3.1.1', '3.1.1/solaris/x86/'],
	['sol_x86', '3.1', '3.1', '3.1.0/solaris/x86/']
];

/*-------------------------------------------------------------------
--- Mirrorliste (sortiert nach nach Auftreten in der Auswahlbox)
---		<key>, <Bezeichnung>, <Basis-URL>
-------------------------------------------------------------------*/
var mirrors =	[
	// Reguläre Mirrors (für freigegebene OOo-Versionen und PrOOo-Box CD)
	['spectrum_h', 'Spectrum (HTTP)', 'http://ftp.spnet.net/ooo-bg/'],
	['spectrum_f', 'Spectrum (FTP)', 'ftp://ftp.spnet.net/ooo-bg/']
];

/*-------------------------------------------------------------------
--- Liste, welcher Mirror welche Plattformen vorhält ...
---	wenn wir das noch auf die Versionen runterbrechen, werden wir mit tippen nicht fertig
---		<mirror_key>, <plattform_key>
full: ['aach_f', ['lin_ppc', 'lin_sparc', 'lin_x86', 'lin_x86d', 'mac', 'macIntel', 'sol_sparc', 'sol_x86', 'win', 'prooobox' ]],
classA: ['aach_f', ['lin_ppc', 'lin_sparc', 'lin_x86', 'lin_x86d', 'mac', 'macIntel', 'sol_sparc', 'sol_x86', 'win' ]],

-------------------------------------------------------------------*/

var mirror_platforms =	[
	['spectrum_h', ['lin_32', 'lin_32d', 'lin_64', 'lin_64d', 'lin', 'freebsd_8_64', 'freebsd_72_64', 'freebsd_71_32', 'freebsd_71_64', 'mac_ppc', 'mac_intel', 'sol_sparc', 'sol_x86', 'win', 'ext' ]],
	['spectrum_f', ['lin_32', 'lin_32d', 'lin_64', 'lin_64d', 'lin', 'freebsd_8_64', 'freebsd_72_64', 'freebsd_71_32', 'freebsd_71_64', 'mac_ppc', 'mac_intel', 'sol_sparc', 'sol_x86', 'win', 'ext' ]]
];


/*-------------------------------------------------------------------
--- FormReset beim laden des Windows rufen
-------------------------------------------------------------------*/
if (window.attachEvent) {
	window.attachEvent('onload',resetForm);
} else if (window.addEventListener) {
	window.addEventListener('load',resetForm,false);
	document.addEventListener('load',resetForm,false);
}


/*-----------------------------------------------------------------*/
function getParam( par_name ) {
/* extrahiert den Wert für den übergebenen Parameter aus der URL */

	var dc = window.location.href;
	var prefix = par_name;
	var begin = dc.indexOf("?" + prefix);
	if (begin == -1) {
		begin = dc.indexOf("&" + prefix);
		if (begin == -1 )
			return null;
		else
			begin += 2;
	} else
		begin += 2;
	var end = dc.indexOf("&", begin);
	if (end == -1)
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}


/*-----------------------------------------------------------------*/
function resetForm() {
/*--- setzt wie Eingabe fuer Quickdownload auf Initialwerte
*/
	document.download.version.options.length = 0;
	document.download.version.disabled= true;
	document.download.mirror.options.length = 0;
	document.download.mirror.disabled= true;
	document.download.start.disabled= true;
	document.download.copybox.value = "";
	createPlatformList();

}

/*-------------------------------------------------------------------*/
function createPlatformList() {
/*--- baut die Liste verfuegbarer Plattformen im Select-Element
	'platform' auf */
var i;
var j = 0;
var k = 0;

	/* Liste leeren */
	document.download.platform.options.length = 0;
	//document.download.platform.options[0] = new Option('---','dummyplatform' );

	/* Array platforms durchlaufen und Key + Bezeichnug als Optionen einfuegen */
	for ( i=0; i<platforms.length ; i++) {
		document.download.platform.options[i] =
			new Option (platforms[i][1], platforms[i][0]);
			if ( platforms[i][0] == getParam( "platform") ) {
				/* wenn Eintrag der übergebenen platform entspricht -> Index setzen */
				document.download.platform.selectedIndex = i;
			}
		}
	if (document.download.platform.options.length > 0) {
		if (document.download.platform.selectedIndex < 0 )
			document.download.platform.selectedIndex = 0;
		makeOSVersion();
	}

}

/*-------------------------------------------------------------------*/
function makeOSVersion() {
/*- baut die Liste verfuegbarer Versionen in Abhägigkeit der gewälten
	 Plattform auf */
var i;
var j=0;

	/* gewälte Plattform holen */
	var platform = document.download.platform.options[document.download.platform.selectedIndex].value;
	/* Liste leeren */
	document.download.version.options.length = 0;
	/* array versions durchlaufen */
	for (i=0; i < versions.length ; i++) {
		if ( versions[i][0] == platform) {
			/* wenn <platform_key> in Array versions gleich der gewälten Plattform ist,
				neuen Eintrag erstellen */
			j++;
			document.download.version.options[document.download.version.options.length] =
					new Option(versions[i][2], versions[i][1] )
			if ( versions[i][1] == getParam( "version") ) {
				/* wenn Eintrag der übergebenen Version entspricht -> Index merken
					und später setzen */
				document.download.version.selectedIndex = j-1;
			}
		}
	}
	if ( document.download.version.options.length > 0 ){
		if (document.download.version.selectedIndex  < 0 )
			document.download.version.selectedIndex  = 0;
		document.download.version.disabled = false;
		makeMirrorList ();
	} else {
		document.download.version.disabled= true;
		document.download.mirror.options.length = 0;
		document.download.mirror.disabled= true;
		document.download.copybox.value = "";
		document.download.start.disabled= true;
	}

}

/*-------------------------------------------------------------------*/
function makeMirrorList() {
/*- Baut die Liste der Mirrors anhand der gewälten Platform / Version auf
 	und w?lt einen zuf?ligen Mirror aus*/
var i;
var j;
var k;

	/* gew?lte Plattform*/
	var platform = document.download.platform.value;

	/* Liste leeren */
	document.download.mirror.options.length = 0;
	if ( ! ( (platform == 'dummyplatform') ||
			 (document.download.version.value == '')
		   ) ) {
		/* array versions durchlaufen */
		for (i=0; i < mirror_platforms.length ; i++) {
			for (j=0; j < mirror_platforms[i][1].length ; j++) {
				if ( mirror_platforms[i][1][j] == platform) {
					/* Mirror ist gueltig - in mirrors array suchen */
					for (k=0; k < mirrors.length ; k++) {
						if ( mirror_platforms[i][0] == mirrors[k][0]) {
							document.download.mirror.options[document.download.mirror.options.length] =
								new Option(mirrors[k][1], mirrors[k][0] )
						}
					}
				}
			}
		}
	}
	if ( document.download.mirror.options.length > 0 ) {
		document.download.mirror.disabled = false;
		document.download.mirror.selectedIndex = Math.round ( Math.random()*
												(document.download.mirror.options.length - 1) );
		selectMirror();
	} else {
		document.download.mirror.disabled= true;
		document.download.copybox.value = "";
		document.download.start.disabled= true;
	}

}

/*-------------------------------------------------------------------*/
function selectMirror() {
/*- Mirror wurde ausgewhält - URL aus Plattform, Version und Mirror
	ermitteln und in die Copybox eintragen -*/
var i; var j; var k;

	/* gew?lte Plattform, Version und Mirror holen holen */
	var platform = document.download.platform.value;
	var vers = document.download.version.value;
	var mirror = document.download.mirror.value;

	if ( platform != '' & vers != '' & mirror != '' ) {
		/* mirror suchen und Basis-URL holen*/
		for (i=0; i < mirrors.length ; i++) {
			if ( mirrors[i][0] == mirror) {
				myURL = mirrors[i][2];
				/* jetzt noch Pfad entsprechend Platform / Version holen */
				for (j=0; j < versions.length ; j++) {
					if (versions[j][0] == platform & versions[j][1]  == vers ) {
						myURL = myURL + versions[j][3];
					}
				}
			}
		}
		if (myURL != '') {
			document.download.copybox.value = myURL;
			document.download.start.disabled = false;
		} else {
			alert ('Грешка в скрипта, не може да бъде открит URL адресът!');
		}
	}
}

/*-------------------------------------------------------------------*/
function startDownload() {
/*- Downloadknopf wurde gedrckt - URL aus Copybox lesen und
	Download starten-*/

	var myURL = document.download.copybox.value;
	if (myURL != '') {
		if ( document.download.platform.value == 'win' ) {
                    	//alert("Моля да обмислите следната препоръка\n\nНякои вирусни скенери блокират определени файлови операции\n- тогава правилната инсталация на OpenOffice.org може да бъде възпрепятствана.\n\nЗатова препоръчваме да спрете вирус агента преди започване на иснталацията\nза да не възникне проблем.");
                        window.location.href = myURL;
		} else {
			//window.location.href = myURL;
			window.location.href = myURL;
		}
	} else {
		alert ('Fehler im Script, es konnte keine URL ermittelt werden!');
	}
}



