/* $Id$ */
/* Javascript file for SndfmtChange Page */
xmlhttp = null;

function Initialize()
{
    if (window.XMLHttpRequest) {
	try {
	    xmlhttp = new XMLHttpRequest();
	} catch (e) {}
    } else if (window.ActiveXObject) {
	try {
	    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e) {
	    try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (e) {}
	}
    }
    if (xmlhttp) {
	xmlhttp.onreadystatechange = ReceiveData;
    }
}

function YtDownloader()
{
    if (!xmlhttp) {
	return;
    }
    switch (xmlhttp.readyState) {
    case 1:
    case 2:
    case 3:
	xmlhttp.abort();
	break;
    default:
	break;
    }
	
    var ytid = null, eqmt = null, qlty = null, mlad = null, i;
    /* YouTube Video ID */
    ytid = escape(document.F1.youtubeid.value);
    /* PC, IPOD, PSP, DCM, AU, SBM, MP3, AAC, NIL */
    for (i = 0; i < 9; i++) {
	if (document.F1.equipment[i].checked) {
	    eqmt = escape(document.F1.equipment[i].value);
	    break;
	}
    }
    /* HIGH, NORMAL, LOW */
    for (i = 0; i < 3; i++) {
	if (document.F1.quality[i].checked) {
	    qlty = escape(document.F1.quality[i].value);
	    break;
	}
    }
    /* mail address to notify (optional) */
    mlad = escape(document.F1.mailaddress.value);
    if (!ytid || !eqmt || !qlty) {
	return;
    }
    var url = "cgi-bin/ytdwnlderi.cgi?youtubeid=" + ytid + "&equipment=" + eqmt + "&quality=" + qlty;
    if (mlad.length > 0) {
	url += "&mailaddress=" + mlad;
    }

    // Pstatus("YtDownloader: url = " + url);
    xmlhttp.open("GET", url, true);
    xmlhttp.send(null);
    var drst = document.getElementById("dld_result");
    if (drst) {
	drst.innerHTML = "<img src=\"waiting.gif\" alt=\"waiting\"><span class=\"strong\">ダウンロード + 変換しています。お待ち下さい!</span>";
	drst.style.display = "block";
    }
    return;
}

function ReceiveData()
{
    if (!xmlhttp || xmlhttp.readyState != 4) {
	// Pstatus("ReceiveData readyState: " + xmlhttp.readyState + ".");
	return;
    }
    // Pstatus("ReceiveData: status: " + xmlhttp.status);
    var drst = document.getElementById("dld_result");

    if (xmlhttp.status == 200) {
	drst.innerHTML = xmlhttp.responseText;
    } else {
	// Pstatus("ReceiveData status: " + xmlhttp.status + ".");
	drst.innerHTML = "<img src=\"error.png\" alt=\"error\"><span class=\"strong\">&nbsp;&nbsp;Server Error!: " + xmlhttp.status + "</span>";
    }
    if (drst.style.display != "block") {
	drst.style.display = "block";
    }
}

/* 
function Pstatus(str) 
{
    var dl = document.getElementById("DebugLog");
    if (dl) {
	if (dl.style.display == "none") {
	    dl.style.display = "block";
	}
	dl.innerHTML += str + "<br>";
    } else {
	alert("Pstatus: dl not found!");
    }
    // console.log(str + "<br>");
    return true;
}
*/
