function get_radio_value(id)
{
	var value;
	var index;
	var radiogroup = document.getElementById(id);
	var radio = radiogroup.getElementsByTagName('input');

	for (index=0; index < radio.length; index++)
	{
		if(radio[index].checked == true)
		{
			value = radio[index].value;
		}
	}

	return	value;
}

function dl_iso(e)
{
	var http = new Array(
		'http://ftp.jaist.ac.jp/pub/Linux/Vine/',
		'http://ftp.kddilabs.jp/pub/Linux/packages/Vine/',
		'http://www.ring.gr.jp/pub/linux/Vine/',
		'http://ftp.vinelinux.org/pub/Vine/'
		);
	var ftp = new Array(
		'ftp://ftp.jaist.ac.jp/pub/Linux/Vine/',
		'ftp://ftp.kddilabs.jp/pub/Linux/packages/Vine/',
		'ftp://ftp.ring.gr.jp/pub/linux/Vine/',
		'ftp://ftp.vinelinux.org/pub/Vine/'
		);
	var imgprefix = 'Vine-5.1/IMAGES/Vine51-';
	var protocol;
	var arch;
	var media;
	var uri;

	protocol = get_radio_value ('protocol');
	arch = get_radio_value ('arch');
	media = get_radio_value ('media');

	var select = document.getElementById('mirror');
	switch (protocol)
	{
	case 'http':
		uri = http[select.value];
		break;
	case 'ftp':	
		uri = ftp[select.value];
	}

	uri = uri + imgprefix + arch + "-" + media + ".iso";
	closewin();
	window.location = 'thanks.html?' + uri;
}

function closewin()
{
	var closebtn = document.getElementById("close");
	removeListener(closebtn, 'click', dl_cancel, false);
	var download = document.getElementById("downloads");
	removeListener(download, 'click', dl_iso, false);
	var dlwin = document.getElementById("dlwin");
	document.getElementsByTagName("body")[0].removeChild(dlwin);
}

function openwin(e)
{
	if(!document.getElementById("dlwin"))
	{
		var dlwin = document.createElement("div");
		dlwin.id = "dlwin";
		dlwin.innerHTML =	'<h2>Vine Linux 5.1 CD/DVD/USB ISOイメージのダウンロード</h2>\n' +
					'<form><p id="protocol">プロトコル：\n' +
					'<input type="radio" name="protocol" value="http" checked="checked" />HTTP\n' +
					'<input type="radio" name="protocol" value="ftp" />FTP</p>\n' +
					'<p><label>ミラーサーバ：<select id="mirror">\n' +
					'<option value="0">北陸先端科学技術大学院大学</option>\n' +
					'<option value="1">KDDI 研究所</option>\n' +
					'<option value="2">Ring Server Project</option>\n' +
					'<option value="3">Vine Linux Master Server</option></select></label></p>\n' +
					'<p id="arch">対象：\n' +
					'<label><input type="radio" name="arch" value="i386" checked="checked" />PC/AT互換機(32bit)</label>\n' +
					'<label><input type="radio" name="arch" value="x86_64" />PC/AT互換機(64bit)</label>\n' +
					'<label><input type="radio" name="arch" value="ppc" />Macintosh(Power PC)</label></p>\n' +
					'<p id="media">メディア：\n' +
					'<label><input type="radio" name="media" value="DVD" checked="checked" />DVD/USB (1GB)</label>\n' +
					'<label><input type="radio" name="media" value="CD" />CD (700MB)</label><br />\n' +
					'<p class="btngroup"><input type="button" id="downloads" value="ダウンロード" />\n' +
					'<input type="button" id="close" value="キャンセル" /></p></form>\n';
		document.getElementsByTagName("body")[0].appendChild(dlwin);

		var closebtn = document.getElementById("close");
		addListener(closebtn, 'click', dl_cancel, false);
		var download = document.getElementById("downloads");
		addListener(download, 'click', dl_iso, false);
	}
	
	if(e.preventDefault)
	{
		e.preventDefault();
	}
	
	/*
	if(window.event)
	{
		window.event.returnValue = false;
	}
	*/

	return false;
}

function dl_cancel(e)
{
	closewin();
}

function setListener(e)
{
	var dlbutton = document.getElementById("dlbutton");
	addListener(dlbutton, 'click', openwin, false);
}

function addListener(elem, eventType, func, cap)
{
	if(elem.addEventListener)
	{
		elem.addEventListener(eventType, func, cap);
	}
	/*
	else if(elem.attachEvent)
	{
		elem.attachEvent ('on'+eventType, func);
	}
	*/
}

function removeListener(elem, eventType, func, cap)
{
	if(elem.removeEventListener)
	{
		elem.removeEventListener(eventType, func, cap);
	}
}


addListener(window, 'load', setListener, false);
