// このファイルではブログで単語のチェックを行った結果の処理として以下のXML-RPCの処理を行っている。
// 　mt.setPostCategories：カテゴリ設定
// 　mt.publishPost：指定したエントリの再構築
//
// ※charsetがEUC-JPでないとIEでエラーになる
// ※エントリーポイント、ユーザ名、パスワードがハードコーディングされている※ @@@

	var EntryPoint = "http://www.wiredgadget.com/mt/mt-xmlrpc.cgi";
	var UserName = "wiredgadget";
	var Password = "kakudate";
	var CategoryIdAr = [2, 1, 3];
	
	var PostID = null;


	// ※categoryのIDを固定でハードコーディングしてしまっています。※
	function resultPost(postId, result) {
		//alert("resultPost("+postId+", "+result+")");
		switch(result) {
		case 0:
			setPostCategory(postId, CategoryIdAr[2]);	// 覚えていない単語
			break;
		case 1:
			setPostCategory(postId, CategoryIdAr[1]);	// もう一歩の単語
			break;
		case 2:
			setPostCategory(postId, CategoryIdAr[0]);	// 覚えた単語
			break;
		}
	}



	// mt.setPostCategories
	// カテゴリに　○、△、×を設定するXML-RPC
	var setPostCatXML0 = '<?xml version="1.0" encoding="utf-8"?>';
	setPostCatXML0 += "<methodCall><methodName>mt.setPostCategories</methodName><params><param><value><int>";	//postId
	var setPostCatXML1 = "</int></value></param><param><value><string>" + UserName + "</string></value></param><param><value><string>" + Password + "</string>";
	setPostCatXML1 += "</value></param><param><value><array><data><value><struct><member><name>categoryId</name><value><int>";	//categoryId
	var setPostCatXML2 = "</int></value></member></struct></value></data></array></value></param></params></methodCall>";



	function setPostCategory(postId, categoryId) {
	    var xmlhttp = createHttpRequest();
	    if (xmlhttp == null) {
	        return null;
	    }
	    
		var setPostCatXML = setPostCatXML0 + postId + setPostCatXML1 + categoryId + setPostCatXML2;
		//alert(setPostCatXML);
		PostId = postId;	// mt.publishPostで必要とされるので、グローバル変数に保存しておく
	    sendRequest(xmlhttp, "POST", EntryPoint, true, setPostCatXML, onLoadSetPostCategoriesResponse, "text/xml");
	}





	// mt.publishPost
	var publishPostXML0 = '<?xml version="1.0" encoding="utf-8"?>';
	publishPostXML0 += "<methodCall><methodName>mt.publishPost</methodName><params><param><value><int>";	//postId
	var publishPostXML1 = "</int></value></param><param><value><string>" + UserName + "</string></value></param><param><value><string>" + Password + "</string>";
	publishPostXML1 += "</value></param></params></methodCall>";


	function onLoadSetPostCategoriesResponse(xmlhttp) {
	    var resXML = xmlhttp.responseXML;
	    if (resXML.getElementsByTagName("fault").length == 0) {
			window.status = "結果の書き込み完了";
			return;

		    var xmlhttp = createHttpRequest();
		    if (xmlhttp == null) {
		        return null;
		    }
		    
			var setPostCatXML = publishPostXML0 + PostId + publishPostXML1;
			//alert(setPostCatXML);
		    sendRequest(xmlhttp, "POST", EntryPoint, true, setPostCatXML, onLoadPublishPostResponse, "text/xml");
		}
		else {
			alert(xmlhttp.responseText);
			window.status = "結果の書き込みエラー";
		}
	}





	function onLoadPublishPostResponse(xmlhttp) {
	    var resXML = xmlhttp.responseXML;
	    if (resXML.getElementsByTagName("fault").length == 0) {
			window.status = "再構築完了";
		}
		else {
			alert(xmlhttp.responseText);
			window.status = "再構築エラー";
		}
	}
