/**************************************************************************
	Copyright (c) 2001-2003 Geir Landr(drop@destroydrop.com)
	JavaScript Tree - www.destroydrop.com/hjavascripts/mypage/
	Version 0.96	

	This script can be used freely as long as all copyright messages are
	intact.
**************************************************************************/

// Arrays for nodes and icons
var nodes			= new Array();;
var openNodes	= new Array();
var icons			= new Array(6);
var selCtrl;
var selParam 		= "";

// Loads all icons that are used in the tree
function preloadIcons() {
	var root = "../../img/kb/mypage/";
	icons[0] = new Image();
	icons[0].src = root + "plus.gif";
	icons[1] = new Image();
	icons[1].src = root + "plusbottom.gif";
	icons[2] = new Image();
	icons[2].src = root + "minus.gif";
	icons[3] = new Image();
	icons[3].src = root + "minusbottom.gif";
	icons[4] = new Image();
	icons[4].src = root + "folder.gif";
	icons[5] = new Image();
	icons[5].src = root + "folderopen.gif";
}

/*
nodeValues
[0] = ノードID
[1] = 親ノードID
[2] = ノード名
[3] = ノードタイプ(個人・グループ等)
[4] = ノードパラメータ(タイプ:フォルダID)
[5] = ノード備考
*/
// Create the tree
function createTree(arrName, startNode, openNode) {

	nodes = arrName;
	if (nodes.length > 0) {
		//各種アイコンを取得
		preloadIcons();

		//ノードのオープン状態をセット
		if (startNode == null) startNode = 0;
		if (openNode != 0 || openNode != null) setOpenNodes(openNode);
		if (startNode !=0) {
			var nodeValues = nodes[getArrayId(startNode)].split("|");
			document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\"><img src=\"../../img/kb/mypage/folderopen.gif\" align=\"absbottom\" alt=\"\" />" + nodeValues[2] + "</a><br />");
		} else document.write("<a href=\"javascript: oc(0, 0);\" onClick=\"SetSelFolder(0,0,this,true);SelectCtrl(this);requestMypageListFile();\"><img src=\"../../img/kb/mypage/folder_icon.gif\" align=\"absbottom\" alt=\"\" />マイページリスト</a><br />");

		var recursedNodes = new Array();
		addNode(startNode, recursedNodes);
	}
}
// Returns the position of a node in the array
function getArrayId(node) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==node) return i;
	}
}
// Puts in array nodes that will be open
function setOpenNodes(openNode) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==openNode) {
			openNodes.push(nodeValues[0]);
			setOpenNodes(nodeValues[1]);
		}
	} 
}
// Checks if a node is open
function isNodeOpen(node) {
	for (i=0; i<openNodes.length; i++)
		if (openNodes[i]==node) return true;
	return false;
}
// Checks if a node has any children
function hasChildNode(parentNode) {
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) return true;
	}
	return false;
}
// Checks if a node is the last sibling
function lastSibling (node, parentNode) {
	var lastChild = 0;
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode)
			lastChild = nodeValues[0];
	}
	if (lastChild==node) return true;
	return false;
}
// Adds a new node to the tree
function addNode(parentNode, recursedNodes) {
	for (var i = 0; i < nodes.length; i++) {

		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) {
			
			var ls	= lastSibling(nodeValues[0], nodeValues[1]);
			var hcn	= hasChildNode(nodeValues[0]);
			var ino = isNodeOpen(nodeValues[0]);

			// Write out line & empty icons
			for (g=0; g<recursedNodes.length; g++) {
				if (recursedNodes[g] == 1) document.write("<img src=\"../../img/kb/mypage/line.gif\" align=\"absbottom\" alt=\"\" />");
				else  document.write("<img src=\"../../img/kb/mypage/empty.gif\" align=\"absbottom\" alt=\"\" />");
			}

			// put in array line & empty icons
			if (ls) recursedNodes.push(0);
			else recursedNodes.push(1);

			// Write out join icons
			if (hcn) {
				if (ls) {
					document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 1);\"><img id=\"join" + nodeValues[0] + "\" src=\"../../img/kb/mypage/");
					 	if (ino) document.write("minus");
						else document.write("plus");
					document.write("bottom.gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>");
				} else {
					document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 0);\"><img id=\"join" + nodeValues[0] + "\" src=\"../../img/kb/mypage/");
						if (ino) document.write("minus");
						else document.write("plus");
					document.write(".gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>");
				}
			} else {
				if (ls) document.write("<img src=\"../../img/kb/mypage/joinbottom.gif\" align=\"absbottom\" alt=\"\" />");
				else document.write("<img src=\"../../img/kb/mypage/join.gif\" align=\"absbottom\" alt=\"\" />");
			}

			// Start link
			//document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
			
			// Write out folder & page icons
/*
			if (hcn) {	
			
				document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"../../img/kb/mypage/folder")
				if (ino) document.write("open");
				document.write(".gif\" align=\"absbottom\" alt=\"Folder\" />");
			} else document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"../../img/kb/mypage/page.gif\" align=\"absbottom\" alt=\"Page\" />");
*/
			//★画像表示
			SetImage(nodeValues,ino);			

			// End link
			document.write("<br />");
			
			// If node has children write out divs and go deeper
			if (hcn) {
				document.write("<div id=\"div" + nodeValues[0] + "\"");
					if (!ino) document.write(" style=\"display: none;\"");
				document.write(">");
				addNode(nodeValues[0], recursedNodes);
				document.write("</div>");
			}
			
			// remove last line or empty icon 
			recursedNodes.pop();
		}
	}
}

// 表示する画像を決定する
function SetImage(nodeValues, ino)
{
	var img = "";
	switch (nodeValues[3])
	{
		case "1":
			img = "folder_kojin_icon";
			break;
		case "2":
			img = "folder_group_icon";
			break;
		case "3":
			img = "folder_icon";
			break;
		case "4":
			img = "folder_koukai_icon";
			break;
		case "5":
			img = "folder_icon";
			break;
		case "6":
			img = "folder_icon";
			break;
		case "7":
			img = "folder_receive_icon";
			break;
		case "8":
			img = "folder_kyouyu_icon";
			break;
		case "9":
			img = "user_icon";
			break;
		case "10":
			img = "folder_icon";
			break;
	}

	var ls	= lastSibling(nodeValues[0], nodeValues[1]);
	document.write("<a title=\"" + nodeValues[5] + "\" id=\"atree_" + nodeValues[0] + "\" style=\"cursor:hand;\"");
	if (ls) 
	{
		document.write(" href=\"javascript: oc(" + nodeValues[0] + ", 1);\"");
	}
	else
	{
		document.write(" href=\"javascript: oc(" + nodeValues[0] + ", 0);\"");		
	}

	//フォルダIDが設定されているノードであれば
	var strClick = "";
	if (eval(nodeValues[4].split(":")[1]) > 0)
	{
		//選択されたらタイプを登録しAjaxを実行
		strClick = " onClick=\"SetSelFolder(" + nodeValues[3] + "," + nodeValues[4].split(":")[1] + ",this,true,'" + nodeValues[2] + "','" + nodeValues[1] + "');SelectCtrl(this);requestMypageListFile('" + nodeValues[4].split(":")[0] + "','" + nodeValues[4].split(":")[1] + "');\""
	}
	else
	{
		//選択されたらタイプを登録
		strClick = " onClick=\"SetSelFolder(" + nodeValues[3] + "," + nodeValues[4].split(":")[1] + ",this,true,'" + nodeValues[2] + "','" + nodeValues[1] + "');SelectCtrl(this);requestMypageListFile();\""
	}

	document.write(strClick);

	document.write(">");
	document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"../../img/kb/mypage/" + img);
	//if (ino) document.write("open");
	document.write(".gif\" align=\"absbottom\" alt=\"" + nodeValues[5] + "\"");

	document.write("/>");
	document.write("" +  nodeValues[2] + " " + "<font color=red>" + nodeValues[6] + "</font>");
	document.write("</a>");
}

//ファイルにアクセスし受信内容を確認します
function requestMypageListFile(type, folder_id, obj, isJoin)
{
	//同一のフォルダを選択していたら処理無し
	if (selParam == type + ":" + folder_id)
	{
		return;
	}
	selParam = type + ":" + folder_id;

	if (type == null)
	{
		if ($("mypagebox2"))
		{
			 $("mypagebox2").innerHTML = "";
		}
		else
		{
			if ($("mypagebox")) $("mypagebox").innerHTML = "";
		}
		return;
	}

	var url = GetMypageListPath(type, folder_id, obj, isJoin);
	GetMypageListValue(url,"mypagebox");
}

//アクセスパスを取得
function GetMypageListPath(type, folder_id, obj, isJoin2)
{
	var url = "";
	var isJoin = "";
	if (isJoin2 != null) isJoin = isJoin2;
	var name = "";
	var isNN = "";
	if ($("selName"))
	{
		name = $("selName").Value;
	}
	var parent = "";
	if (obj == null)
	{
		if ($("selParentName"))
		{
			parent = $("selParentName").Value;
		}
	}
	else
	{
		if (obj.textContent == "undefined" || obj.textContent == null)
		{
			parent = obj.innerText;
		}
		else
		{
			parent = obj.textContent;
		}
	}

	//ブラウザ判定(Netscapeで全角文字を通信させないため)
	if (userSystem.browserLongName.indexOf("Netscape") >= 0)
	{
		name = "";
		parent = "";
		isNN = "true";
	}

	switch (eval(type))
	{
		case 1:
			url = "person.aspx";
			break;
		case 2:
			url = "group.aspx";
			break;
		case 3:
			url = "group_list.aspx";
			break;
		case 4:
			return "";
			break;
		case 5:
			url = "public.aspx"
			break;
		case 6:
			url = "public.aspx";
			break;
		case 7:
			url = "receive.aspx";
			break;
		case 8:
			return "";
			break;
		case 9:
			url = "group.aspx";
			isJoin = "true";
			break;
		case 10:
			url = "group_list.aspx";
			isJoin = "true";
			break;
	}

	return url + "?group_id=" + folder_id + "&folder_id=" + folder_id + "&type=" + type + "&isajax=true" + "&name=" + name + "&parent=" + parent + "&isNN=" + isNN + "&isJoin=" + isJoin;
}

//検索用Ajax
function requestMypageSearch(priceform)
{
	//alert($("seltype").Value);
	var type = $("seltype").Value;
	//if (type == "2") type = "3";
	//if (type == "9") type = "10";
	var folder_id = $("selfolder").Value;
	//alert(folder_id);
	var list_id = $("sellist").Value;
	var searchtxt = $("searchtxt").value;
	var searchtype = "";
	var arrRadio = document.getElementsByName("searchtype");
	for (i = 0;i < arrRadio.length; i++)
	{
		if (arrRadio[i].checked == true)
		{
			searchtype = arrRadio[i].value;
		}
	}

	//ブラウザ判定(Netscapeで全角文字を通信させないため)
	if (userSystem.browserLongName.indexOf("Netscape") >= 0)
	{
		if (CheckLength(searchtxt,1) == true)
		{
			alert("ネットスケープでは全角文字は使用できません");
			return;
		}
	}

	var url = "";
	var target = "";

	if (priceform == null)
	{
		url = "search.aspx?type=" + type + "&folder_id=" + folder_id + "&list_id=" + list_id + "&searchtxt=" + searchtxt + "&searchtype=" + searchtype + "&formname=" + document.forms[0].name;
		target = "mypagebox";
	}
	else
	{
		url = "search.aspx?type=0&searchtxt=" + searchtxt + "&searchtype=" + searchtype + "&ispricelist=true";
		target = "mypagebox2";
	}
	GetMypageListValue(url, target, "true");

//	$("seltype").Value = "";
//	$("selfolder").Value = "";
//	$("sellist").Value = "";
}


//値を取得
function GetMypageListValue(url, target, issearch)
{
	//XMLHttpRequestオブジェクト生成
	var httpoj = createHttpRequest();
    //http通信開始
    httpoj.open( 'POST' , url , true );
   	//受信時に起動するイベント
		
    httpoj.onreadystatechange = function() 
    {	
 		//readyState値は4で受信完了
      	if (httpoj.readyState==4)  
      	{ 
       		//httpレスポンスにより戻ってきた文字列をhtmlに反映させる
        	on_MypageListloaded(httpoj, target, issearch);
	    }
    }
    
	//send メソッド
    httpoj.send("");
}

//コールバック関数 ( 受信時に実行されます )
function on_MypageListloaded(oj, target, issearch)
{
    //httpレスポンスを取得
    res  = oj.responseText //[6]
	//対象要素のhtmlを書き換え
	$(target).innerHTML = res;

	var tbl = $("listTable");
	if (tbl == null) return;
	if (tbl.rows.length <= 1) return;
	if (issearch == "true") return;
	tbl.rows[1].onclick();

	
//	$("groupname").innerHTML = $("selName").Value;

}

//選択中のコントロールを目立たせる
function SelectCtrl(obj)
{
	if (selCtrl != null)
	{
		//コントロールが同じなら処理無し
		if (selCtrl == obj) 
		{
			return;
		}
		selCtrl.style.fontWeight = "normal";
		selCtrl.style.textDecoration  = "none";
	}
	obj.style.fontWeight = "bold";				//太字
	obj.style.textDecoration  = "underline";	//下線
	selCtrl = obj;
}


// Opens or closes a node
function oc(node, bottom) {
	var theDiv = document.getElementById("div" + node);
	var theJoin	= document.getElementById("join" + node);
	var theIcon = document.getElementById("icon" + node);
	
	if (theDiv != null)
	{
		if (theDiv.style.display == 'none') {
			if (bottom==1) theJoin.src = icons[3].src;
			else theJoin.src = icons[2].src;
			//theIcon.src = icons[5].src;
			theDiv.style.display = '';
		} else {
			if (bottom==1) theJoin.src = icons[1].src;
			else theJoin.src = icons[0].src;
			//theIcon.src = icons[4].src;
			theDiv.style.display = 'none';
		}
	}
}
// Push and pop not implemented in IE
if(!Array.prototype.push) {
	function array_push() {
		for(var i=0;i<arguments.length;i++)
			this[this.length]=arguments[i];
		return this.length;
	}
	Array.prototype.push = array_push;
}
if(!Array.prototype.pop) {
	function array_pop(){
		lastElement = this[this.length-1];
		this.length = Math.max(this.length-1,0);
		return lastElement;
	}
	Array.prototype.pop = array_pop;
}


