function createXMLHttpRequest2(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;
}

// document.getElementById
function $(tagId)
{
	return document.getElementById(tagId);
}
function searchNAID()
{
  textData = document.edit_metadata.dc_citation_naid.value;
  if (textData == "")
  {
    alert("NAIDが未入力です");
    $("result_naid").innerHTML = "";
    return false;
  }

  var isPublished = false;
  for (i=0; i< document.edit_metadata.length; i++) {
	if ( document.edit_metadata.elements[i].name == "dc_date_issued_year" ) {
		isPublished = true;
	}
  }
  if (!isPublished) {
    alert("前画面で「出版済」にチェックしてからやり直して下さい");
    $("result_naid").innerHTML = "";
    return false;
  }
	


  httpObj = createXMLHttpRequest2(displayData2);
  if (httpObj)
  {
    httpObj.open("POST","/cgi-bin/naid.cgi",true);
    httpObj.send("naid="+encodeURI(textData));
  }

}
function displayData2()
{
  if ((httpObj.readyState == 4) && (httpObj.status == 200))
  {

    textData = httpObj.responseText;

   if (textData.match(".*File Not Found.*")) {
        alert("no naid found");
   }
   else
   {

    xmlData = httpObj.responseXML;

    var titleTags = xmlData.getElementsByTagName("dc:title");
    var journal_titleTags = xmlData.getElementsByTagName("prism:publicationName");
    var yearTags  = xmlData.getElementsByTagName("prism:publicationDate");
    var volumeTags  = xmlData.getElementsByTagName("prism:volume");
    var issueTags  = xmlData.getElementsByTagName("prism:number");
    var first_pageTags  = xmlData.getElementsByTagName("prism:startingPage");
    var end_pageTags  = xmlData.getElementsByTagName("prism:endingPage");
    var issnTags  = xmlData.getElementsByTagName("prism:issn");
    var abstractTags  = xmlData.getElementsByTagName("dc:description");
    //var creatorTags  = xmlData.getElementsByTagName("dc:creator");
    var publisherTags  = xmlData.getElementsByTagName("dc:publisher");

    if (titleTags.length > 0) {
      document.edit_metadata.dc_title.value = titleTags.item(0).childNodes[0].nodeValue;
    }
    /*
    if (yearTags.length > 0) {
         document.edit_metadata.dc_date_issued_year.value = yearTags.item(0).childNodes[0].nodeValue;
    }
    */
    if (journal_titleTags.length > 0) {
          document.edit_metadata.dc_citation_jtitle.value = journal_titleTags.item(0).childNodes[0].nodeValue;
    }
    if (volumeTags.length > 0) {
       document.edit_metadata.dc_citation_volume.value = volumeTags.item(0).childNodes[0].nodeValue;
    }
    if (issueTags.length > 0) {
	tmpissue = issueTags.item(0).childNodes[0].nodeValue;
	if(tmpissue!="0"){
		document.edit_metadata.dc_citation_issue.value = tmpissue;
		//document.edit_metadata.dc_citation_issue.value = "123";
	}
       //document.edit_metadata.dc_citation_issue.value = issueTags.item(0).childNodes[0].nodeValue;
    }
    if (first_pageTags.length > 0) {
       document.edit_metadata.dc_citation_spage.value = first_pageTags.item(0).childNodes[0].nodeValue;
    }
    if (end_pageTags.length > 0) {
       document.edit_metadata.dc_citation_epage.value = end_pageTags.item(0).childNodes[0].nodeValue;
    }
    /*
    if (issnTags.length > 0) {
       document.edit_metadata.dc_citation_issn.value = issnTags.item(0).childNodes[0].nodeValue;
    }
    */

    /*
    if (abstractTags.length > 0) {
       document.edit_metadata.internal_comment.value = abstractTags.item(0).childNodes[0].nodeValue;
    }
    */

    if (publisherTags.length > 0) {
       document.edit_metadata.dc_publisher.value = publisherTags.item(0).childNodes[0].nodeValue;
    }

    if (issnTags.length > 0) {
	issn_tmp = issnTags.item(0).childNodes[0].nodeValue;
	if (issn_tmp.indexOf("-") == -1) {
		issn = issn_tmp.substring(0,4) + "-" + issn_tmp.substring(4,8);
	}
	document.edit_metadata.dc_citation_issn.value = issn;
    }

    if (yearTags.length > 0) {
	tmpdate = yearTags.item(0).childNodes[0].nodeValue;
	tmpdate0 = tmpdate.split("-");
        if(tmpdate0[0]!=null){
        	document.edit_metadata.dc_date_issued_year.value = tmpdate0[0];
	}
        if(tmpdate0[1]!=null){
        	document.edit_metadata.dc_date_issued_month.value = tmpdate0[1];
	}
	if(tmpdate0[2]!=null){
        	document.edit_metadata.dc_date_issued_day.value = tmpdate0[2];
	}
    }


    creatorListTag = xmlData.getElementsByTagName("dc:creator");

    for (i=0; i<creatorListTag.length; i++) {
    	if (i == 0) {
		tmpauthor0 = creatorListTag.item(i).childNodes[0].nodeValue;
		tmpauthor02 = tmpauthor0.split(",");
       		//document.edit_metadata.dc_creator_last_0.value = creatorListTag.item(i).childNodes[0].nodeValue;
       		document.edit_metadata.dc_creator_last_0.value = tmpauthor02[0];
       		document.edit_metadata.dc_creator_first_0.value = tmpauthor02[1];
    	}
        if (i == 1) {
                tmpauthor1 = creatorListTag.item(i).childNodes[0].nodeValue;
                tmpauthor12 = tmpauthor1.split(",");
                document.edit_metadata.dc_creator_last_1.value = tmpauthor12[0];
                document.edit_metadata.dc_creator_first_1.value = tmpauthor12[1];
        }
        if (i == 2) {
                tmpauthor2 = creatorListTag.item(i).childNodes[0].nodeValue;
                tmpauthor22 = tmpauthor2.split(",");
                document.edit_metadata.dc_creator_last_2.value = tmpauthor22[0];
                document.edit_metadata.dc_creator_first_2.value = tmpauthor22[1];
        }
        if (i == 3) {
                tmpauthor3 = creatorListTag.item(i).childNodes[0].nodeValue;
                tmpauthor32 = tmpauthor3.split(",");
                document.edit_metadata.dc_creator_last_3.value = tmpauthor32[0];
                document.edit_metadata.dc_creator_first_3.value = tmpauthor32[1];
        }

    }

    /*
    issnListTag = xmlData.getElementsByTagName("issn");

    for (i=0; i<issnListTag.length; i++) {
	if (issnListTag.item(i).getAttributeNode("type").nodeValue == "print") {
	   issn_tmp = issnListTag.item(i).childNodes[0].nodeValue;
	   if (issn_tmp.indexOf("-") == -1) {
	      issn = issn_tmp.substring(0,4) + "-" + issn_tmp.substring(4,8);
	   }
	   document.edit_metadata.dc_citation_issn.value = issn;
	} else if (issnListTag.item(i).getAttributeNode("type").nodeValue == "electronic") {
	   eissn_tmp = issnListTag.item(i).childNodes[0].nodeValue;
	   if (eissn_tmp != "00000000") {
	      if (eissn_tmp.indexOf("-") == -1) {
	         eissn = eissn_tmp.substring(0,4) + "-" + eissn_tmp.substring(4,8);
	      }
	      document.edit_metadata.dc_citation_eissn.value = eissn;
           }
	}	   
    }
    */

   }
   $("result_naid").innerHTML = "";
  }else{
    $("result_naid").innerHTML = "<b>Wait...</b>";
  }
}

