/*
 *
 * @author     Jun Yamane <jun1969x[at]gmail.com>
 * @license    BSD http://www.opensource.org/licenses/bsd-license.html
 * @version    ver 0.14 (2011-01-27)
 * @link       http://sourceforge.jp/projects/xbrl-php/
 *
 */

/**********************************************************
XBRL load file
**********************************************************/
function changeJs(id1) {
	var id2 = (id1=="con") ? "non" : "con";
	$("div_" + id1).style.display = "block";
	$("a_" + id1).style.background = "yellow";
	$("a_" + id1).style.fontWeight = "bold";
	$("div_" + id2).style.display = "none";
	$("a_" + id2).style.background = "#ffffff";
	$("a_" + id2).style.fontWeight = "normal";
}
/**********************************************************
U-chart
**********************************************************/
function uchart(uid) {
	var ary_consol = $("consol_arys").value.split(",");
	if(ary_consol.length==2) {
		$("div_non").style.display = "none";
	}
	if(uid==1) {
		return;
	}
	for(var i=0; i<ary_consol.length; i++) {
		var arys = $("uchart_ary_" + ary_consol[i]).value.split("/");
		for(var i0=0, num=arys.length; i0<num; i0++) {
			var ary = arys[i0].split(",");
			var consol = ary[0];
			var state = ary[1];
			var radius = parseInt(ary[2]);
			var per = ary.slice(3);
			for(var i1=0, sum=0, len=per.length; i1<len; i1++) {
				sum += Math.abs(parseInt(per[i1]));
			}
			var id = "u_" + consol + "_" + i0;
//alert(arys[i0])
			if($(id).getContext && radius<=100 && radius>0 && (state=="BS" || state=="PL" || state=="CF" || state=="SS") && len && sum) {
				var colors = {
					BS:[
						"#f92500",	//c151
						"#890F65",	//c153
						"#fbe731",	//c161
						"#8c8c8c",	//c121
						"#47ea7e",	//c113
						"#28af78",	//c111
						"#2860a3",	//c109
						"#74a9d6"		//c101
					],
					PL:[
						"#f92500",	//c201
						"#fbe731",	//c210
						"#890F65",	//c231
						"#28af78",	//c228
						"#8c8c8c",	//c211
						"#2860a3"		//c202
					]
				};
				if(state=="CF") {
					colors = [
						[per[0], "#f92500", per[0]],	//c301
						[per[1], "#fbe731", per[1]],	//c311
						[per[2], "#2860a3", per[2]],	//c321
						[per[3], "#8c8c8c", per[3]],	//c330
						[per[4], "#74a9d6", -per[4]]	//c331
					].sort(function(a, b) { return b[2] - a[2];});
				}else if(state=="SS") {
					colors = [
						[per[0], "#f92500", per[0]],	//c401
						[per[1], "#2860a3", per[1]],	//c411
						[per[2], "#890F65", per[2]],	//c421
						[per[3], "#8c8c8c", per[3]],	//c431
						[per[4], "#47ea7e", per[4]],	//c441
						[per[5], "#28af78", per[5]],	//c481
						[per[6], "#fbe731", -per[6]]	//c491
					].sort(function(a, b) { return b[2] - a[2];});
				}
				$(id).width = $(id).height = 105;
				var obj = $(id).getContext("2d");
				obj.lineJoin = "bevel";
				obj.lineWidth = 1.6;
				for(var j=0, start_=close_=270; j<len; j++) {
					v = (state!="CF" && state!="SS") ? [per[j], colors[state][j]] : [colors[j][0], colors[j][1]];
					x = radius + 4;
					y = x + (50 - radius) * 2;
					close_ += 360 * Math.abs(parseInt(v[0]) / sum);
					obj.start = start_ * Math.PI / 180;
					obj.close = close_ * Math.PI / 180;
					start_ = close_;
					obj.beginPath();
// line
					obj.moveTo(radius * Math.cos(obj.start) + x, radius * Math.sin(obj.start) + y);
					obj.lineTo(radius * Math.cos(obj.close) + x, radius * Math.sin(obj.close) + y);
					obj.lineTo(x, y);
					obj.strokeStyle = (document.all && !window.opera) ? v[1] : "#ffffff";
					obj.stroke();
// circle
					obj.arc(x, y, radius, obj.start, obj.close, false);
					obj.fillStyle = v[1];
					obj.fill();
				}
			}
		}
	}
}
/**********************************************************
XBRL load file
**********************************************************/
function xbrlLoadFile(zip_, file_, id, num, name_) {
	var obj = createXMLHttpRequest(), text;
	if(obj) {
		xbrlShow("Now loading...");
		obj.open("POST", "libs/XBRL.php", true);
		obj.setRequestHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
		obj.send("zip=." + zip_ + "&file=" + file_ + "&name=" + name_);
		obj.onreadystatechange = function() { 
			if(obj.readyState==4) {
				if(text = obj.responseText) {
//alert(text);
					if(text = eval("(" + text + ")")) {
						return xbrlShow(print_r(text, "<br />", "&nbsp;&nbsp;"), id, num);
					}else {
						return xbrlShow("Error Data");
					}
				}else {
					return xbrlShow("No Data");
				}
 			}
		}
	}
}
/**********************************************************
XBRL load show
**********************************************************/
function xbrlShow(str, id, num) {
	$("code_zip").innerHTML = "<code>" + str + "</code>";
	if(id || num) {
		for(var i=0; i<num; i++) {
			$("li_" + i).style.background = "#fff";
		}
		$("li_" + id).style.background = "yellow";
	}
}
/**********************************************************
print_r http://ma-bank.com/item/1228
**********************************************************/
function print_r(arr, br, nbsp) {
	br = (br) ? br : "\\n";
	nbsp = (nbsp) ? nbsp : "  ";
	function dump(arr, br, nbsp, level) {
		var dumped_text = '';
		if(!level) {
			level = 0;
		}
		//The padding given at the beginning of the line.
		var level_padding = '';
		for(var j=0; j<level+1; j++) {
			level_padding += nbsp + nbsp;
		}
		if(typeof(arr)=='object') { //Array/Hashes/Objects
			for(var item in arr) {
				var value = arr[item];
				if(typeof(value)=='object') { //If it is an array,
					dumped_text += level_padding + '[' + item + '] => Array' + br;
					dumped_text += nbsp + level_padding + '(' + br + dump(value, br, nbsp, level+1) + nbsp + level_padding + ')'  + br;
				}else {
					dumped_text += level_padding + '[' + item + '] => \'' + value + '\'' + br;
				}
			}
		}else { //Stings/Chars/Numbers etc.
			dumped_text = '===>' + arr + '<===(' + typeof(arr) + ')';
		}
		return dumped_text;
	}
	return 'Array' + br + nbsp + '(' + br + dump(arr, br, nbsp) + nbsp + ')';
}
/**********************************************************
ajax http://allabout.co.jp/internet/javascript/closeup/CU20050515A/
**********************************************************/
function createXMLHttpRequest() {
	if(window.ActiveXObject) {
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e) {
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e2) {
				return null;
			}
		}
	}else if(window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}else {
		return null;
	}
}
/**********************************************************
document.getElementById
**********************************************************/
function $(id) {
	return document.getElementById(id);
}

