/* BookList用支援 JavaScript
   Copyright 2007 Kenshi Muto <kmuto@debian.org>
 */
YAHOO.namespace("popup.container");

function keyPressFilter(evt, type) {
    var keyCode;
    if (evt) {
	keyCode = evt.keyCode;
    } else {
	keyCode = event.keyCode;
    }
    if (keyCode != 10 && keyCode != 13) {
	return true;
    } else {
	switch (type) {
	case "findByTitle":
	    findByTitle();
	    break;
	case "findByWords":
	    findByWords();
	    break;
	case "modify":
	    submitForm('modify');
	    break;
	case "makeSureRemove":
	    if (makeSureRemove()) {
		submitForm('remove');
	    }
	    break;
	}
	return false;
    }
}

function submitForm(val) {
    document.mainBookForm.mode.value = val;
    document.mainBookForm.submit();
}

function createXMLHttpRequest(callback) {
    xmlHttpObject = null;
    if (window.XMLHttpRequest) { // Safari, Firefox
        xmlHttpObject = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            xmlHttpObject = new ActiveXObject("Msxml2.XMLHTTP"); // IE6
        } catch (e) {
            try {
                xmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP"); // IE5
            } catch (e) {
                return null;
            }
        }
    }

    if (xmlHttpObject) xmlHttpObject.onreadystatechange = callback;
    return xmlHttpObject;
}

function findByTitle() {
    title = document.mainBookForm.title.value;
    if (title == "") {
	alert("書名を先に入れてください。");
	return false;
    }
    findAmazon(title);
}

function findByWords() {
    words = document.mainBookForm.assearch.value;
    if (words == "") {
	alert("検索単語 (書名や著者名など) を左のボックスに入力してください。複数の検索単語をスペースで区切って指定できます。");
	return false;
    }
    findAmazon(words);
}

function findAmazon(keyword) {
    httpObj = null;
    httpObj = createXMLHttpRequest(viewAmazon);
    if (httpObj) {
	httpObj.open("GET", "parts_amazon.rhtml?k=" + encodeURI(keyword), true);
	httpObj.send(null);
    }
}

function viewAmazon() {
    callbackFunction("viewAmazon");
}

function replaceBookList() {
    httpObj = null;
    httpObj = createXMLHttpRequest(viewBookList);
    if (httpObj) {
	httpObj.open("GET", "parts_list.rhtml?w=" + document.mainBookForm.w.value + "&g=" + document.mainBookForm.g.value + "&p=" + document.mainBookForm.g.value, true);
	httpObj.send(null);
    }
}

function viewBookList() {
    callbackFunction("viewBookList");
}

function replaceBookList2() {
    httpObj = null;
    httpObj = createXMLHttpRequest(viewBookList);
    if (httpObj) {
	if (document.mainBookForm.a == null) {
	    a = "";
	} else {
	    a = "&a=" + (document.mainBookForm.a.checked ? 1 : 0)
	}
	httpObj.open("GET", "parts_list2.rhtml?w=" + document.mainBookForm.w.value + "&g=" + document.mainBookForm.g.value + a, true);
	httpObj.send(null);
    }
}

function callbackFunction(type) {
    if ((httpObj.readyState == 4) && (httpObj.status == 200)) {
	switch (type) {
	case "viewAmazon":
	    document.getElementById("amazonresult").innerHTML =
		xmlHttpObject.responseText;
	    break;
	case "viewBookList":
	    document.getElementById("booklistTable").innerHTML =
		xmlHttpObject.responseText;
	    /* innerHTMLではJavaScriptが解釈されないため、再度引き渡す */
	    for (i = 1; i < document.mainBookForm.v.value; i++) {
		YAHOO.popup.container.tt1 = new YAHOO.widget.Tooltip("tt1", { context: "link" + i});
		if (i > 2000) break; /* failsafe */
	    }
	    break;
	}
    }
}

function replacePublisher() {
    publisher = document.mainBookForm.publishers.value;
    if (publisher != "") {
	document.mainBookForm.publisher.value = publisher;
    }
}

function replaceAsin(asin) {
    if (asin != "") {
	document.mainBookForm.asin.value = asin;
    }
}

function makeSureRemove() {
    return confirm("本当に『" + document.mainBookForm.title.value + "』を削除してもよいですか?");
}
