﻿var randomword = "";

function amazonSearch(GetKeywordsData,GetSearchType){
	randomword = Math.floor(Math.random() * 10000) + "ID" + Math.floor(Math.random() * 10000);
	document.write("<div id=\"" + randomword + "\"></div>");

	httpObj = createXMLHttpRequest(DisplayData);

	KeywordsData = encodeURIComponent(GetKeywordsData);
	SearchType = GetSearchType;

	if (SearchType == 0){
		SearchType = Math.floor(Math.random() * 3 + 1);
	}

	if(httpObj){
		httpObj.open("GET","http://www.mokorin.net/ama/rssget.php?SearchType=" + SearchType + "&Keywords=" + KeywordsData, true);
		httpObj.send(null);
	}
}

function DisplayData(){
	if((httpObj.readyState == 4) && (httpObj.status == 200)){
		document.all(randomword).innerHTML = bookList(httpObj.responseXML);
	}
}

function bookList(xml){
	itemList = xml.getElementsByTagName("Item");

	preid = "preid" + Math.floor(Math.random() * 1000);

	txt = "<div id=\"" + preid + "\"><br></div>"

	for (i=0; i<itemList.length; i++){
		ItemTitle = itemList[i].getElementsByTagName("Title")[0].childNodes[0].nodeValue;
		bookASIN = itemList[i].getElementsByTagName("ASIN")[0].childNodes[0].nodeValue;

		ItemURL =	"http://www.amazon.co.jp/exec/obidos/ASIN/" + bookASIN + "/sakamosyo-22/";
		ItemImage =	"http://rcm-images.amazon.com/images/P/" + bookASIN + ".09._SCTZZZZZZZ_";

		txt += "<a href=" + ItemURL + " target=_blank><img src=" + ItemImage + " border=\"0\" onMouseover=\"document.all." + preid + ".innerText='" + ItemTitle + "';\" style=\"margin:1px;\"></a>";
	}

	return txt;
}

// HTTP connection
function createXMLHttpRequest(cbFunc){
	var XMLhttpObject = null;

	try{
		XMLhttpObject = new XMLHttpRequest();
	}catch(e){
		try{
			XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				XMLhttpObject = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				return null;
			}
		}
	}

	if (XMLhttpObject){
		XMLhttpObject.onreadystatechange = cbFunc;
	}

	return XMLhttpObject;
}
