﻿//固定頁面為此頁
//window.onunload = function(){ location.replace(document.location); };

//如果是showModelessDialog,showModalDialog 作相關設定

if(typeof(window.dialogArguments) == "object")
{
	document.write("<base target=_self>");
//	document.onkeyup = new Function("if(event.keyCode==116) location.reload();");
}
else
{
//	window.onbeforeunload= new Function("window.location.replace('');"); //禁止回到前一頁
}

function GetFunctionTraceInfo()
{
	var infoString = "";
	var functionNodeArgumentsInfo;
	var functionNodeArgumentsCount;
	var functionObj = GetFunctionTraceInfo;
	var i;
	while((functionObj = functionObj.caller) != null)
	{
		functionNodeArgumentsCount = functionObj.arguments.length;
		functionNodeArgumentsInfo = ""
		for(i = 0;i < functionNodeArgumentsCount;i++)
			functionNodeArgumentsInfo += "," + functionObj.arguments[i];
		if(functionNodeArgumentsInfo != "") functionNodeArgumentsInfo = functionNodeArgumentsInfo.substr(1);
		functionNodeArgumentsInfo = " (" + functionNodeArgumentsInfo + ")"
		infoString = GetFunctionName(functionObj.toString()) + functionNodeArgumentsInfo + "\r\n" + infoString ;
	}
	return infoString;
}

function GetFunctionName(functionString)
{
	var arr = (/^function ([^\{\}\(\)]*)/ig).exec(functionString);
	return (arr == null ? "" : RegExp.$1);
}

// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
//	Ex: addEvent(window, "load", prepareTextBoxes);
// Ex: addEvent(window, "load", function(){rebuildInputCSS("text","text");});
function addEvent(elm, evType, fn, useCapture)
{  
	if (elm.addEventListener){
		elm.addEventListener(evType, fn, useCapture);    
		return true;  
	} else if (elm.attachEvent){
		var r = elm.attachEvent("on"+evType, fn);    
		return r;  
	} else {    
		alert("Handler could not be removed");
	}
} 

function GetAppPath()
{
	var scriptCur;
	var scriptSrc;
	for(scriptCur = 0 ; scriptCur < window.document.scripts.length ; scriptCur++)
	{
		scriptSrc = window.document.scripts[scriptCur].src;
		if(/Scripts[\\\/]Global.js$/ig.test(scriptSrc))
		{
			return scriptSrc.substr(0,scriptSrc.length - "Scripts/Global.js".length);
		}
	}
	return null;
}

function WriteExceptionToServer(msg,url,line) {
	try {
		var valEnvironmentInfo = "";
		valEnvironmentInfo += "UserAgent:" + window.navigator.userAgent + "\r\n";
		valEnvironmentInfo += "HTML Content:\r\n" + window.document.documentElement.outerHTML;
		var PostValue = "";
		PostValue += "FormValues=" + escape("Line Number:" + line + "\r\n" + msg + "\r\n" + GetFunctionTraceInfo());
		PostValue += "&Url=" + escape("Url:" + url);
		PostValue += "&ErrDescript=" + escape(valEnvironmentInfo).replace(/\+/g,"%2B");
		var xmlhttp= new ActiveXObject("MSXML2.XMLHTTP");
		xmlhttp.open("POST",GetAppPath() + "Miscellaneous/ExceptionHandle.aspx",true);
		xmlhttp.setRequestHeader("Content-Length", PostValue.length);
		xmlhttp.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded");
		xmlhttp.send(PostValue);
	}
	catch(e) {
		//alert(e.message);
	}
}

addEvent(window, "error", function(msg,url,line){WriteExceptionToServer(msg,url,line);});

function SetReadonly(obj,flagReadonly) {
	if (obj == null) return;
	var isCreateSpanObj = obj.tagName == "SELECT" || (obj.tagName == "INPUT" && $.inArray(obj.type,["checkbox","radio"]) >= 0);
	var targetObj = $(obj);
	if (isCreateSpanObj) {
	    var spanObj = targetObj.parent("span.readonlySpan");
	    if (spanObj.length == 0) {
	        //IE6的AppendChildren render有bug,wrapAll會有問題
	        spanObj = targetObj.wrapAll("<span class='readonlySpan'></span>").parent();
	        targetObj.attr("checked",targetObj.attr("checked"));
	        targetObj.bind("beforeactivate", function() { return !this.readOnly; });
	        targetObj.focus(function() { if (this.readOnly) { this.blur(); } });
	        spanObj.mouseover(function() { if (targetObj.attr("readOnly")) { this.setCapture(); } });
	        spanObj.mouseout(function() { if (targetObj.attr("readOnly")) { this.releaseCapture(); } });
	    }
	}
	targetObj.attr("readOnly", flagReadonly);
}

function GetOptionObject(selectObject,findValue)
{
	if(selectObject == null) return null;
	if(selectObject.tagName != "SELECT") return null;
	var i;
	for(i=0;i<selectObject.options.length;i++)
	{
		if(selectObject.options[i].value == findValue) return selectObject.options[i];
	}
	return null;
}

function GetRadioCheckedObject(radioObject)
{
	if(radioObject == null) return null;
	if(typeof(radioObject.length) == "number")
	{
		var i;
		for(i=0;i<radioObject.length;i++)
		{
			if(radioObject[i].checked) return radioObject[i];
		}
		return null;
	}
	else
	{
		return (radioObject.checked ? radioObject : null);
	}
}

function SetZipHtmlOption()
{	
	var zipElementName = (arguments.length >= 1 ? arguments[0] : null );
	var srcElement = (arguments.length >= 2 ? arguments[1] : null );
	var ZipAreaElement = document.all["_" + zipElementName + "ZipArea"];
	var ZipAreaElementOptionsCount = ZipAreaElement.options.length;
	var ZipAreaElementOptionsCur = ZipAreaElement.selectedIndex;
	var ZipUnitElement;
	if((srcElement == null) || (srcElement.name == "_" + zipElementName + "ZipArea"))
	{
		if(srcElement == null)
		{
			var ZipCode = document.all[zipElementName].value;
			if(ZipCode == "")
			{
				ZipAreaElement.value = "";
				SetZipHtmlOption(zipElementName,ZipAreaElement);
				return;
			}
			var ZipUnitElementOptionsCount;
			var j;
			var aa;
			for(i = 0 ; i < ZipAreaElementOptionsCount ; i++)
			{
				if(ZipAreaElement.options[i].value != "")
				{
					ZipUnitElement = document.all["_" + zipElementName + "ZipUnit_" + ZipAreaElement.options[i].value];
					aa += ZipUnitElement + "\r\n";
					ZipUnitElementOptionsCount = ZipUnitElement.options.length;
					for(j = 0 ; j < ZipUnitElementOptionsCount ; j++)
					{
						if(ZipUnitElement.options[j].value == ZipCode)
						{
							ZipUnitElement.selectedIndex = j;
							ZipAreaElement.selectedIndex = i;
							SetZipHtmlOption(zipElementName,ZipAreaElement);
							return;
						}
					}
				}
			}
			alert(aa);
		}
	
		var ZipSpanElement;
		var i;
		for(i = 0 ; i < ZipAreaElementOptionsCount ; i++)
		{
			if(ZipAreaElement.options[i].value != "")
			{
				ZipSpanElement = document.all["_" + zipElementName + "ZipSpan_" + ZipAreaElement.options[i].value];
				ZipSpanElement.style.display = (i == ZipAreaElementOptionsCur ? "inline" : "none" );
			}
		}			
	}
	if(srcElement != null)
	{
		var ZipElement = document.all[zipElementName];
		if(ZipAreaElement.value == "") 
		{
			ZipElement.value = "";
			return;
		}
		ZipUnitElement = document.all["_" + zipElementName + "ZipUnit_" + ZipAreaElement.value];
		ZipElement.value = ZipUnitElement.value;
	}
}

function HtmlEncode(str) {
	var tmpStr = str;
	tmpStr = tmpStr.replace(/</g,"&lt;");
	tmpStr = tmpStr.replace(/&/g,"&amp;");
	tmpStr = tmpStr.replace(/ /g,"&nbsp;");
	tmpStr = tmpStr.replace(/\t/g,"&#09;");
	tmpStr = tmpStr.replace(/\r\n/g,"<br>");
	tmpStr = tmpStr.replace(/\r/g,"<br>");
	tmpStr = tmpStr.replace(/\n/g,"<br>");
	return tmpStr;
}

function GetNowDateTimeString() {
	var d = new Date();
	return GetDateString(d) + " " + GetTimeString(d);
}

function GetDateString(theDate) {
	var return_value = theDate.getYear() + "/";
	if((theDate.getMonth()+1) < 10) return_value += "0";
	return_value += (theDate.getMonth()+1) + "/";
	if(theDate.getDate() < 10) return_value += "0";
	return_value += theDate.getDate();
	return return_value;
}

function GetTimeString(theDate)
{
	var return_value = theDate.getHours() + ":";
	if(theDate.getMinutes() < 10) return_value += "0";
	return_value += theDate.getMinutes() + ":";
	if(theDate.getSeconds() < 10) return_value += "0";
	return_value += theDate.getSeconds();
	return return_value;
}

function FindRadioElementByValue(radioName,findValue)
{
	radioElement = document.forms[0].elements[radioName];
	if(radioElement == null) return null;
	if(typeof(radioElement.length) == "number")
	{
		for(i=0;i<radioElement.length;i++)
		{
			if(radioElement[i].value == findValue) return radioElement[i]
		}
	}
	else
		return (radioElement.value == findValue ? radioElement : null);
	return null;
}

function SetElementValue(obj,val)
{
	if(obj == null) return;
	if(obj.tagName == "SELECT")
	{
		if(GetOptionObject(obj,val) != null) obj.value = val;
	}
	else
		obj.value = val;
}

function FindParentElementByTag(obj,tagName)
{
	var cur_obj = obj;
	while(cur_obj != null)
	{
		cur_obj = cur_obj.parentElement;
		if(cur_obj == null) return null;
		if(cur_obj.tagName == tagName) return cur_obj;
	}
	return null;
}

function OpenerWindow()
{
	var windowObj = (arguments.length >= 1 ? arguments[0] : window);
	try
	{
		if(typeof(windowObj.dialogArguments) != "object") return windowObj.opener;
		if(typeof(windowObj.dialogArguments.document) != "object") return windowObj.opener;
		return windowObj.dialogArguments;
	}
	catch(e)
	{
		return null;
	}
}

function ReloadAllOpenerWindow()
{
	var OpenerWindowObj = OpenerWindow();
	try
	{
		while(OpenerWindowObj != null)
		{
			SubmitOpenerWindow(OpenerWindowObj);
			OpenerWindowObj = OpenerWindow(OpenerWindowObj);
		}
	}
	catch(e)
	{}
}

function ReloadOpenerWindow()
{
	SubmitOpenerWindow(OpenerWindow());
}

function SubmitOpenerWindow(OpenerWindowObj)
{
	try{
		OpenerWindowObj.document.forms[0].submit();
	}
	catch(e)
	{}
}

function CloseWindow()
{
	window.close();
}

function RebuildDateString(dateStr)
{
	if(dateStr == "") return dateStr;
	if(dateStr.length == 10) return dateStr;
	dateStrArray = dateStr.split("/");
	tmpStr = dateStrArray[0] + "/";
	if(dateStrArray[1].length <2) tmpStr += "0";
	tmpStr += dateStrArray[1] + "/";
	if(dateStrArray[2].length <2) tmpStr += "0";
	tmpStr += dateStrArray[2];
	return tmpStr;
}

function JumpUrl()
{
	url = (arguments.length >= 1 ? arguments[0] : location.href );
	target = (arguments.length >= 2 ? arguments[1] : window );
	target.location = url;
}

KeyCode_Enter = 13;
function checkKeyCode(KeyCode,EvalStr)
{
	if(event.keyCode == KeyCode)
		eval(EvalStr);
}

function OpenWindow()
{
	/*
		arguments[0] = url ;
		arguments[1] = OpenMethod
		Ex:
			showModelessDialog("myDialog.htm",window,"status:false;dialogWidth:300px;dialogHeight:300px");
			showModalDialog("myDialog.htm",window,"status:false;dialogWidth:300px;dialogHeight:300px");
			open("sample.htm","WindowName","height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
		
		arguments[2] = name or vArguments
		arguments[3] = features
	*/
	
	url = (arguments.length >= 1 ? arguments[0] : "" ) ;
	OpenMethod = (arguments.length >= 2 ? arguments[1].toLowerCase() : "open" ) ;
	vArguments = (arguments.length >= 3 ? arguments[2] : null );
	features = (arguments.length >= 4 ? arguments[3] : "" );
	
	//OpenMethod ="open";
	//vArguments ="NewWindow";
	
	if((features != "") && (OpenMethod == "showmodaldialog" || OpenMethod == "showmodelessdialog"))
	{
			features = features.toLowerCase();
			features = features.replace("width=","dialogwidth=");
			features = features.replace("height=","dialogheight=");
			features = features.replace("top=","dialogtop=");
			features = features.replace("left=","dialogleft=");
	}
	if (url == "") return;
	switch(OpenMethod)
	{
		case "open":
			window.open(url,vArguments,features + ",resizable=yes,scrollbars=yes,status=yes");
			break;
		case "showmodaldialog":
			window.showModalDialog(url,vArguments,features);
			break;
		case "showmodelessdialog":
			window.showModelessDialog(url,vArguments,features);
			break;
	}
}

