//========================================================================== 
// SuperMap IS 客户端程序,版权所有，北京超图地理信息技术有限公司，1999-2004。 
// 本程序只能在有效的授权许可下使用。未经许可，不得以任何手段擅自使用或传播。 
// 作者：			李玮顾，胡中南 
// 修改：	 
// 文件名：			Page.js 
// 功能：			自定义事件，与页面相关的一些函数 
// 版本：			5.0.2.6225 
// 最后修改时间：	2004-09-03 
//========================================================================== 

var former_index = 0;
var MAX_Btn_Number = 33;

//切换按钮 //
function SwitchBtn(index, action)
{
//	status = typeof(index);
	// when argument index is ignored
	if(typeof(index) == "undefined" || typeof(index) == "string") 
	{
		action = index;
		index = Eve().id.replace("img0","").replace("img","");
	}
	
	var prefix="btn";
	var current = "";
	var rest = "";
	if (index==-1) //reset all buttons to off status
	{
				for(var i=1; i<10; i++)
				{
					if(i!=index && getImg("img0"+i))
						getImg("img0"+i).src="images/"+prefix+"_0"+i+"_off.gif";
				}
				for(var i=10; i<=MAX_Btn_Number; i++)
				{
					if(i!=index && getImg("img"+i))
						getImg("img"+i).src="images/"+prefix+"_"+i+"_off.gif";
				}
				former_index = 0;
	}
	switch(action)
	{
		case "Over":
			current = "on";
			rest = "off";
			SwitchBtn_ChangeImg(current,rest,prefix,index,former_index);
			break;
		case "Out":
			current = "off";
			rest = "off";
			SwitchBtn_ChangeImg(current,rest,prefix,index,former_index);
			break;
		case "Down":
			current = "down";
			rest = "off";
			//判断鼠标操作类型 
			if(index==4 || index==5 || index==6 || index==10 || (index>=13 && index<=17) || index==22 || index==26 || (index>=28 && index<=32))
			{
				for(var i=1; i<10; i++)
				{
					if(i!=index && getImg("img0"+i))
						getImg("img0"+i).src="images/"+prefix+"_0"+i+"_off.gif";
				}
				for(var i=10; i<=MAX_Btn_Number; i++)
				{
					if(i!=index && getImg("img"+i))
						getImg("img"+i).src="images/"+prefix+"_"+i+"_off.gif";
				}
				SwitchBtn_ChangeImg(current,rest,prefix,index,former_index);
				former_index = index;
			}
			else
			{
				SwitchBtn_ChangeImg(current,rest,prefix,index,former_index);
				//former_index = index;
			}
			break;
	}
}

function SwitchBtn_ChangeImg(current,rest,prefix,index,former_index)
{
	if(former_index != index)
	{
		if(getImg("img"+index))
		{
			getImg("img"+index).src="images/"+prefix+"_"+index+"_"+current+".gif";
		}
		if(getImg("img0"+index))
		{
			getImg("img0"+index).src="images/"+prefix+"_0"+index+"_"+current+".gif";
		}
	}
	else
	{
		if(current != "off")
		{
			if(getImg("img"+index))
			{
				getImg("img"+index).src="images/"+prefix+"_"+index+"_"+current+".gif";
			}
			if(getImg("img0"+index))
			{
				getImg("img0"+index).src="images/"+prefix+"_0"+index+"_"+current+".gif";
			}
		}
		else
		{
			if(getImg("img"+index))
			{
				getImg("img"+index).src="images/"+prefix+"_"+index+"_"+"down"+".gif";
			}
			if(getImg("img0"+index))
			{
				getImg("img0"+index).src="images/"+prefix+"_0"+index+"_"+"down"+".gif";
			}
		}
	}
}

function getImg(img)
{
	if (document.all)
	{
		return document.all(img);
	}
	else if (document.getElementById)
	{
		return document.getElementById(img);
	}
}

function Eve(e)
{
   var trigger='';
   if(!e && event.srcElement) trigger= event.srcElement;
   else if(typeof(e) != "undefined" && e.target) trigger= e.target;
   if (trigger.nodeType && trigger.nodeType== 3)
      trigger= trigger.parentNode;
   return trigger;
}

function SqlQuery()
{
	var strSQL;
	if(document.all)
	{
		strSQL = document.all.sql.value;
		document.all.mapForm.QuerySQL(strSQL);
	}
	else if (document.getElementById)
	{
		strSQL = document.getElementById('sql').value
		document.getElementById('mapForm').QuerySQL(strSQL);
	}

}

function viewObj(obj)
{
	var str;
	var aWin = window.open("","","");
	str = getAttributes(obj);
	aWin.document.write(str);
	aWin.document.close();
}
function getAttributes(o)
{
	var s = "";
	var i;

	// undefined or null
	if(typeof(o)=="undefined" || o==null)
		return "<unknown>null</unknown>";

	// array
	if(typeof(o[0]) != "undefined"){
		for(i = 0; i < o.length; i++)
			s += "<item" + i + ">" + getAttributes(o[i]) + "</item" + i + ">";
		return s;
	}

	if(typeof(o.xml) != "undefined")
		return "<xml>" + o.xml + "</xml>";

	// object(not array)
	try{
		if(typeof(o) != "object"){
			s = "<" + typeof(o) + ">" + o + "</" + typeof(o) + ">";
		}else{
			var oo;	// oo 是对象 o 的属性 
			for(oo in o){
				if(o[oo]==null){
					s += "<" + oo + ">null</" + oo + ">";					
					continue;
				}
				// array
				if(typeof(o[oo]) != "undefined" && typeof(o[oo][0]) != "undefined"){
					var strTemp = "";
					for(i = 0; i < o[oo].length; i++){		
						strTemp += "<item" + i + ">" + getAttributes(o[oo][i]) + "</item" + i + ">";
					}
					strTemp = "<" + oo + ">" + strTemp + "</" + oo + ">";
					s += strTemp;
				}
				// not array
				else if(typeof(o[oo])=="object"){
					var str2;
					str2 = getAttributes(o[oo]);
					s += "<" + oo + ">" + str2 + "</" + oo + ">";
				}else{
					s += "<" + oo + ">" + o[oo] + "</" + oo + ">";
				}
			}
		}
	}catch(e){
		s = "<error>" + e.name + ":" + e.message + "</error>";
	}

	return s;
}

// SQL查询 
var isQuerying = false;  //避免重复查询 
function QuerySQL(strSQL)
{
	// 锁定 
	if(isQuerying)
	{
		alert("正在查询，请稍后。 Querying, please wait for a moment. "); // ?
		return;
	}

	isQuerying = true;

	// 替换“<”和“>”符号 
	var re;
	re = /</g;
	strSQL = strSQL.replace(re,"&lt;");
	re = />/g;
	strSQL = strSQL.replace(re,"&gt;");
	window.status = strSQL;
	
	mapAction.SetAction('SQLQUERY');
	document.forms[0].SQLQUERY.value = strSQL;

	OnSubmitToServer();
	isQuerying = false;
}


function PrintMap(mapControlId, mapWidth, mapHeight, mapUrl)
{
	var printWin;
	
	if ( typeof(mapUrl)=="undefined" || mapUrl=="" )
	{
		mapUrl = document.getElementById(mapControlId+"_MVIMAGE").src;
	}
//	printUrl = "print.html?mapWidth="+mapWidth+"&mapHeight="+mapHeight+"&mapUrl="+mapUrl;
	var addr = document.getElementById(mapControlId+"_MapServer_Addr").value;
	var port = document.getElementById(mapControlId+"_MapServer_Port").value;

	printUrl = "print.aspx?Addr=" + addr + "&Port=" + port;
	printWin = window.open(printUrl, "", "width=" + (eval(mapWidth)+200) + ",height=" + (eval(mapHeight)+200) + ",resizable,toolbar,menubar,scrollbars,status" );
	//window.status = "width=" + (eval(mapWidth)+100) + ";height=" + (eval(mapHeight)+0 +";mapUrl="+mapUrl );
}


function SwitchToolboxTab(boxIndex, tabIndex)
{
	var COUNT = 4;
	var tempObject;
	for (var i=1; i<=COUNT; i++)
	{
		tempObject = document.getElementById("tbt_" + i);
		if( typeof(tempObject) != "undefined" && tempObject != null)
		{
			tempObject.src = tempObject.src.replace("_on", "_off");
		}
		tempObject = document.getElementById("Toolbox_" + i);
		if( typeof(tempObject) != "undefined" && tempObject != null)
		{
			tempObject.style.visibility = "hidden";
			tempObject.style.display = "none";
		}
	}
	
	// 可能只有 boxIndex 参数，而没有 tabIndex 参数。  //
	if(tabIndex == null || typeof(tabIndex) == "undefined") 
	{
		tabIndex = boxIndex;
	}
	
	tempObject = document.getElementById("tbt_" + tabIndex);
	if( typeof(tempObject) != "undefined" && tempObject != null)
	{
		tempObject.src = tempObject.src.replace("_off", "_on");
	}
	tempObject = document.getElementById("Toolbox_" + boxIndex);
	if( typeof(tempObject) != "undefined" && tempObject != null)
	{
		tempObject.style.visibility = "visible";
		tempObject.style.display = "block";
	}
}


function SwitchEditDiv(divIndex)
{
	var COUNT = 5;
	var tempObject;
	for (var i=1; i<=COUNT; i++)
	{
		tempObject = document.getElementById("Toolbox_4_" + i);
		if( tempObject )
		{
			tempObject.style.visibility = "hidden";
			tempObject.style.display = "none";
		}
	}
	
	tempObject = document.getElementById("Toolbox_4_" + divIndex);
	if( tempObject )
	{
		tempObject.style.visibility = "visible";
		tempObject.style.display = "block";
	}
}

//executed when body loaded 
function Body_Load()
{
	;
}

function ShowList(listId)
{
	var list;
	if(listId)
	{
		list = document.getElementById(listId);
		if(!list)
		{
			return;
		}
	}
	else
	{
		list = this;
	}
	
//	list.onmousemove = function(){this.bStarted = true; this.onmouseout = HideList();};
//	list.onmouseout = HideList();
	
	var curPosition = SMISGetMouseAbsolutePosition();
	with(list.style)
	{
		visibility	= "visible";
		display		= "block";
		position	= "absolute";
		left		= curPosition.x + 3;
		top			= curPosition.y + 3;
	}
}

function HideList(listId)
{
//	window.status = "test";
	var list;
	if(listId)
	{
		list = document.getElementById(listId);
		if(!list)
		{
			return;
		}
	}
	else
	{
		//alert(show_props(event));
		list = event.srcElement;
	}

/*	
	if(!list.bStarted)
	{
		return;
	}
*/
	
	if(list.style.visibility != "hidden")
	{
		with(list.style)
		{
			visibility	= "hidden";
			display		= "none";
		}
		
		list.bStarted = false;
	}

}

//CustomAction

function MoveEntity(mapId, entityType)
{
	if(!entityType)
	{
		entityType = document.getElementById("entityType").value;
	}
	var points = document.getElementById('pixs').value;
	switch(entityType)
	{
		case "Point":
			SMISCreateLine(mapId, points);
			SMISCreatePoint(mapId, points);
			break;
		case "Line":
			SMISCreateLine(mapId, points);
			break;
		case "Polygon":
			SMISCreatePolygon(mapId, points);
			break;
		default:
			break;
	}
	
}

function OpenLegendWindow(legendControlId)
{
	var legend = document.getElementById(legendControlId + "_LegendUrl");
	if(!legend)
	{
		return;
	}
	
	var win = window.open("legendWindow");
	win.document.write("<img src='" + legend.value + "'>");
	win.focus();
}
 
 
function ShowBusSolutionSubmit(solutionId)
{
	var inputShowBusSolution = document.getElementById("ShowBusSolution");
	if(!inputShowBusSolution)
	{
		SMISCreateHiddenField(document.forms[0], "ShowBusSolution", "ShowBusSolution", "true");
	}
	else
	{
		inputShowBusSolution.Value = "true";
	}
	
	var inputSolutionId = document.getElementById("SolutionId");
	if(!inputSolutionId)
	{
		SMISCreateHiddenField(document.forms[0], "SolutionId", "SolutionId", solutionId);
	}
	else
	{
		inputSolutionId.Value = solutionId;
	}
	
	__doPostBack("","");
} 

 
function ShowBusStopSubmit(stopId, solutionId, locationX, locationY, stopName)
{
	var inputShowBusStop = document.getElementById("ShowBusStop");
	if(!inputShowBusStop)
	{
		SMISCreateHiddenField(document.forms[0], "ShowBusStop", "ShowBusStop", "true");
	}
	else
	{
		inputShowBusStop.Value = "true";
	}
	
	var inputStopId = document.getElementById("StopId");
	if(!inputStopId)
	{
		SMISCreateHiddenField(document.forms[0], "StopId", "StopId", stopId);
	}
	else
	{
		inputStopId.Value = stopId;
	}
	
	var inputSolutionId = document.getElementById("SolutionId");
	if(!inputSolutionId)
	{
		SMISCreateHiddenField(document.forms[0], "SolutionId", "SolutionId", solutionId);
	}
	else
	{
		inputSolutionId.Value = solutionId;
	}
	
	SMISCreateHiddenField(document.forms[0], "LocationX", "LocationX", locationX);
	SMISCreateHiddenField(document.forms[0], "locationY", "LocationY", locationY);
	SMISCreateHiddenField(document.forms[0], "StopName", "StopName", stopName);
	
	__doPostBack("","");
}
