﻿function richterplanet()
{
}

function $id(id)
{
	var element = document.getElementById(id);
	return element ? element : false;
}

function $tag(root, tagName)
{
    var tags = root.getElementsByTagName(tagName);
    var length = tags ? tags.length : 0;
    return length > 0 ? tags : false;
}

function getrandomval()
{
	return Math.random() * 1000000;
}

function searchplanetwindow(sender)
{
	var obj = sender;

	while(true)
	{
		obj = obj.parentNode;
		if (obj.id.indexOf("planet_window_") == 0)
		{
			return obj;
			break;
		}   
	}
}

function planetrequest(sender, xmlfilename, getdata, postdata, israndomquery, loadingcontainer, resultingcontainer)
{
	var randomid = getrandomval();
	sender.id = sender.id ? sender.id : ("planet_request_sender_" + randomid);
	loadingcontainer.id = loadingcontainer.id ? loadingcontainer.id : ("planet_request_loading_container_" + randomid);
	resultingcontainer.id = resultingcontainer.id ? resultingcontainer.id : ("planet_request_resulting_container_" + randomid);
	sender.resultingContainer = resultingcontainer;
	sender.retryfunc = "planetrequest($id('" + sender.id +"'), '" + xmlfilename +"', '" + getdata +"', '" + postdata +"', '" + (israndomquery ? "true" : "false") +"', $id('" + loadingcontainer.id +"'), $id('" + resultingcontainer.id +"'));"; 
	loadingcontainer.innerHTML = '<br /><center><label class="cn12_black">正在連接服務器，請稍候</label></center>';
	sender.xmlHttpRequest = null;
	sender.xmlHttpRequest = GetXmlHttpRequest();
	var url = location.href.substring(0, location.href.lastIndexOf("/")) + "/" + xmlfilename;

	if (israndomquery)
	{ 
		getdata += (getdata.length > 0 ? "&" : "") + "random=" + (Math.random() * 1000000);
	}
	url += getdata.length > 0 ? ("?" + getdata) : "";

	sender.xmlHttpRequest.onreadystatechange = new Function("getplanetresult('" + sender.id + "')");

	if (postdata.length > 0)
	{
		sender.xmlHttpRequest.open("POST", url, true); 
		sender.xmlHttpRequest.setRequestHeader("Content-Length", postdata.length);
		sender.xmlHttpRequest.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
		sender.xmlHttpRequest.send(postdata);
	}
	else
	{
		sender.xmlHttpRequest.open("GET", url, true);
		sender.xmlHttpRequest.send(null);
	}
	
}

function openplanetwindow(eventArg, sender, xmlfilename, getdata, postdata, israndomquery, width, height, title, left, top)
{
	var randomid = getrandomval();
	
	var planetwindow;
	var orix;
	var oriy;
	if (sender.planetWindow)
	{
		orix = sender.planetWindow.style.left;
		oriy = sender.planetWindow.style.top;
		try
		{  
			 RemoveTag(sender.planetWindow);
		}
		catch(e)
		{
		}
	}
   
	planetwindow = document.createElement("div");
	
	planetwindow.innerHTML = '<table cellpadding="0" cellspacing="0"><tr><td class="boxupleft_gray"></td><td class="boxup_gray"></td><td class="boxupright_gray"></td></tr><tr><td class="boxleft_gray"></td><td width="' + width + '" valign="top"><div style="height:' + height + 'px;overflow-y:auto;"></div></td><td class="boxright_gray"></td></tr><tr><td class="boxdownleft_gray"></td><td class="boxdown_gray"></td><td class="boxdownright_gray"></td></tr></table>'; 
	planetwindow.id = "planet_window_" + randomid; 
	planetwindow.style.position = "absolute";

	var planetcontent = planetwindow.getElementsByTagName('table')[0].rows[1].cells[1].getElementsByTagName('div')[0];
   
	if (eventArg)
	{
	
		if ((document.body.scrollLeft + width + eventArg.clientX + 30) > document.body.scrollWidth)
		{
		    if (document.body.scrollWidth - width - 100 > 0)
		    {
			    planetwindow.style.left = (document.body.scrollWidth - width - 100) + "px";
			}
			else
			{
			    planetwindow.style.left = (document.body.scrollLeft + 30) + "px";
			}
		}
		else
		{
			planetwindow.style.left = (document.body.scrollLeft + eventArg.clientX) + "px";
		}
		
		if ((document.body.scrollTop + height + eventArg.clientY + 30) > document.body.scrollHeight)
		{
            if (document.body.scrollHeight - 100 - height > 0)
            {
                planetwindow.style.top = (document.body.scrollHeight - 100 - height) + "px";
            }
            else
            {
                planetwindow.style.top = (document.body.scrollTop + 30) + "px";
            }
		}
		else
		{
			planetwindow.style.top = (document.body.scrollTop + eventArg.clientY) + "px";
		}
	}
	else
	{
		planetwindow.style.left = orix;
		planetwindow.style.top = oriy;
	}

	if (left != "")
	{
		planetwindow.style.left = left;
	}
	if (top != "")
	{
		planetwindow.style.top = top;
	}
	
	planetwindow.style.backgroundColor = "#ffffff"; 
	if (planetwindow.style.opacity)
	{
		planetwindow.style.opacity = 0.3; 
	}
	else
	{
		planetwindow.style.filter = "alpha(opacity=30)"; 
	}
	planetwindow.fadeopacity = 30; 
   
	var table = document.createElement("table");
	table.cellPadding = 0;
	table.cellSpacing = 0;
	table.style.borderCollapse = "collapse";
	var tr1 = table.insertRow(0);
	tr1.id = "planet_window_title_" + randomid;
	var td1 = tr1.insertCell(0);
	td1.align = "right";
	td1.className = "black12";
	td1.innerHTML = title + '&nbsp;&nbsp;[ <a href="javascript:richterplanet();" class="black12" onclick="Fade($id(\'' + planetwindow.id + '\'), 100, 0, 50);">關閉</a> ]';
	var tr2 = table.insertRow(1);
	var td2 = tr2.insertCell(0); 
	td2.height = height-15;
	td2.width = width - 15;
	 
	/*if (tr1.addEventListener)
	{
		planetwindow.addEventListener("mousedown", new Function("getMoving(event, '" + planetwindow.id +"', '" + planetwindow.id +"')"), false);
		planetwindow.addEventListener("mousemove", new Function("goMoving(event, '" + planetwindow.id +"', 0)"), false);
		planetwindow.addEventListener("mouseup", new Function("stopMoving(event, '" + planetwindow.id +"')"), false);
	}    
	else
	{
		planetwindow.onmousedown = new Function("getMoving(event, '" + planetwindow.id +"', '" + planetwindow.id +"')");
		planetwindow.onmousemove = new Function("goMoving(event, '" + planetwindow.id +"', 0)");
		planetwindow.onmouseup = new Function("stopMoving('" + planetwindow.id +"')");
	}*/
	  
	planetcontent.appendChild(table);  
	sender.planetWindow = planetwindow;
   
	document.body.appendChild(sender.planetWindow); 
	 
	Fade(sender.planetWindow, 30, 100, 50);
	
	planetrequest(sender, xmlfilename, getdata, postdata, israndomquery, td2, td2);
}

function getplanetresult(senderID)
{	
	var sender = $id(senderID);
	var xmlhttp = sender.xmlHttpRequest;
	var readystate = checkReadyState(xmlhttp);
	if (readystate == 200)
	{
		 var beginIndex = xmlhttp.responseText.indexOf("<body>") + 6;
		 var endIndex = xmlhttp.responseText.indexOf("</body>");
		 var responsevalue = xmlhttp.responseText.substring(beginIndex, endIndex);
		 sender.resultingContainer.innerHTML = responsevalue;
		 beginIndex = responsevalue.indexOf("<script>");
		 if (beginIndex > -1)
		 {
			beginIndex += 8;
			endIndex = responsevalue.indexOf("</script>");
			eval(responsevalue.substring(beginIndex, endIndex));
		 }
	}
	else if(readystate == -1)
	{
		 sender.resultingContainer.innerHTML = '<center class="black12"><label>無法和連接服務器，請</label><a href="javascript:richterplanet();" onclick="' + sender.retryfunc +'">重試</a></center>';
	}
}

var movingObj = 0;
var x = 0;
var y = 0;
function getMoving(event, senderid,moveid){
	senderObj = $id(senderid);
	movingObj = $id(moveid);
	x = event.clientX - parseInt(movingObj.style.left.replace("px", ""));
	y = event.clientY - parseInt(movingObj.style.top.replace("px", ""));
	//senderObj.setCapture();
}
function goMoving(event, moveid,ud1lr2){
	if (movingObj !=0){
		if (event.clientY < 0 || event.clientX < 0){
			stopMoving(movingObj);
		}else{
			if (ud1lr2 == 1){
				movingObj.style.top = (event.clientY - y) + "px";
			}else if(ud1lr2 == 2){
				movingObj.style.left = (event.clientX - x) + "px";
			}else{
				movingObj.style.top = (event.clientY - y) + "px";
				movingObj.style.left = (event.clientX - x) + "px";
			}
		}
	}
}
function stopMoving(senderid){
	movingObj = 0;
	x = 0;
	y = 0;
}
