/**
 * Ajax補助ユーティリティ.
 *
 * @author TAKEMURA mitsuo<takemura@ark-web.jp>
 */


/**
 * XMLドキュメントから指定したタグ名のエレメントを返す.
 * (Safari対策)
 *
 * @param  XMLDocument XMLドキュメントオブジェクト
 * @param  String      取得するタグ名
 * @return Element     OK=Element / NG=null
 */
function myGetElementByTagName(oXmlDoc, sTargetTagName) {
    for (var i = 0 ; i < oXmlDoc.childNodes.length ; i++) {
       var oElement = oXmlDoc.childNodes[i];
       if (oElement.nodeName == sTargetTagName) {
           return oElement;
       }
   }
   return null;
}
function getTagData(oEntry, sTagName) {
    var oElement = myGetElementByTagName(oEntry, sTagName);
    return oElement.firstChild.data;
}
function getTagAttribute(oEntry, sTagName, sAttributeName) {
    var oElement = myGetElementByTagName(oEntry, sTagName);
    return oElement.getAttribute(sAttributeName);
}


function rewrite(sIdName, sClassName, sString) {
    var oElement = getClassElement(document.getElementById(sIdName), sClassName);
    if (oElement == null) { return; }
    oElement.innerHTML = sString;
}
function rewrite_href(sIdName, sClassName, sString) {
    var oElement = getClassElement(document.getElementById(sIdName), sClassName);
    if (oElement == null) { return; }
    oElement.href = sString;
}

function getClassElement(oElements, sClassName) {
	for (var i = 0 ; i < oElements.childNodes.length ; i++) {
		// 子ノードがある場合は再起させて、子ノードで見つけたらそれを返す
		if (oElements.childNodes[i].childNodes.length > 0) {
			var oResult = getClassElement(oElements.childNodes[i], sClassName);
			if (oResult != null) { return oResult; }
		}
		// クラス名が該当するものと同じならそれを返す
		if (oElements.childNodes[i].className == sClassName) {
			return oElements.childNodes[i];
		}
	}
    return null;
}


function divOpen(iDivID) {
    if (document.all) {
        document.all(iDivID).style.display = 'inline';
    }
    else if (document.getElementById(iDivID)) {
        document.getElementById(iDivID).style.display = 'inline';
    }
}
function divClose(iDivID) {
    if (document.all) {
        document.all(iDivID).style.display = 'none';
    }
    else if (document.getElementById(iDivID)) {
        document.getElementById(iDivID).style.display = 'none';
    }
}


/*  Function Equivalent to URLEncoder.encode(String, "UTF-8")
    Copyright (C) 2002 Cresc Corp.
    Version: 1.0
*/
function encodeURL(str){
    var s0, i, s, u;
    s0 = "";                // encoded str
    for (i = 0; i < str.length; i++){   // scan the source
        s = str.charAt(i);
        u = str.charCodeAt(i);          // get unicode of the char
        if (s == " "){s0 += "+";}       // SP should be converted to "+"
        else {
            if ( u == 0x2a || u == 0x2d || u == 0x2e || u == 0x5f || ((u >= 0x30) && (u <= 0x39)) || ((u >= 0x41) && (u <= 0x5a)) || ((u >= 0x61) && (u <= 0x7a))){     // check for escape
                s0 = s0 + s;           // don't escape
            }
            else {                      // escape
                if ((u >= 0x0) && (u <= 0x7f)){     // single byte format
                    s = "0"+u.toString(16);
                    s0 += "%"+ s.substr(s.length-2);
                }
                else if (u > 0x1fffff){     // quaternary byte format (extended)
                    s0 += "%" + (oxf0 + ((u & 0x1c0000) >> 18)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0x3f000) >> 12)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
                else if (u > 0x7ff){        // triple byte format
                    s0 += "%" + (0xe0 + ((u & 0xf000) >> 12)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
                else {                      // double byte format
                    s0 += "%" + (0xc0 + ((u & 0x7c0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
            }
        }
    }
    return s0;
}

