﻿var ajaxResponse = "";
var httpRequest = null;
var ajaxCallBackFunction = null;

//取得瀏覽器語系
var userLanguage = (window.navigator.language==null)?window.navigator.systemLanguage.toLowerCase():window.navigator.language.toLowerCase();
//設定預設語系
var defaultLanguage = "zh-tw";

//將視窗開啟在正中央
function open_window(url,target,width,height,option){	
	var screen_top;
	var screen_left;
	screen_top = parseInt((window.screen.height-height)/2);
	screen_left = parseInt((window.screen.width-width)/2);
	if(option==null) option="";
	if(height>=600){
	    window.open(url,target,"width="+width+",height="+height+",left="+screen_left+",top="+screen_top+",scrollbars=yes"+option);
	}
	else{
	    window.open(url,target,"width="+width+",height="+height+",left="+screen_left+",top="+screen_top+option);
	}
}

//將視窗移到在正中央
function move_window(width,height){	
	var screen_top;
	var screen_left;
	screen_top = parseInt((window.screen.height-height)/2);
	screen_left = parseInt((window.screen.width-width)/2);
	window.resizeTo(width,height);
	window.moveTo(screen_left,screen_top);
}

//禁止使用者利用 Ctrl+N 開新視窗
function Check_CtrlN(){
	if(event.ctrlKey == true){
		if(event.keyCode == 78){
			Event_False();
		}
	}
}

function Event_False(){
	window.event.returnValue=false;
}
 	
function chk_num(mNum){
	if(isNaN(mNum)){
		return false;
	}
	else{
		return true;
	}
}

//限制使用者只能輸入數字
function lock_numkey(){
    var e = parseInt(event.keyCode);
    if(e!=9&&e!=37&&e!=39&&e!=46&&e!=8&&(e<48||e>57)&&(e<96||e>105)){
	    return false;
    }
}
 	
//檢查時間格式  type 0-小時 1-分鐘
function chk_time(obj,type){
    if(obj.value==""){
        alert(CommonMessage.DateColumnNull);
        obj.value="00";
        obj.select();
        return false;
    }
    switch(type){
        case 0:if(parseInt(obj.value) > 23){
         	        alert(CommonMessage.DateFormatError);
         	        obj.select();
                    return false;
                }	        
                break;
        case 1:if(parseInt(obj.value) > 59){
         	        alert(CommonMessage.DateFormatError);
         	        obj.select();
                    return false;
                }	        
                break;
    }
    return true;
}
 	
function get_checked_count(mNAME){
	var check_count = 0;
	if(document.getElementsByName(mNAME)==null){
		return 0;
	}
	for(i=0;i<document.getElementsByName(mNAME).length;i++){
		if(document.getElementsByName(mNAME)[i].checked){
			check_count += 1;
		}
	}
	return check_count;
}
		
//刪除下拉選單內的所有選項,cleartype 0:全刪 n:保留至第n項
function clearOption(selectID,cleartype){
    var obj = document.getElementById(selectID);
    var i;
    if(obj!=null){
        for(i=obj.options.length;i>cleartype;i--){
            obj.remove(i-1);
        }
        if(cleartype>0){
            obj.options.selectedIndex = 0;
        }
    }
}

//取得遠端資料
//formData 表單資料
//ajaxURL Server端URL
//isAsyn 同步/非同步
//funcName Server端回應後要執行的Script(非同步才有效)
function RemoteRequest(formData,ajaxURL,isAsyn,funcName){
  var responseString = "";
  var asyn = (isAsyn!=null)?isAsyn:false;
  ajaxCallBackFunction = (funcName!=null)?funcName:null;
  if(window.XMLHttpRequest){
      httpRequest = new XMLHttpRequest();
  }
  else if(window.ActiveXObject!=null){
      if(new ActiveXObject('Msxml2.XMLHTTP')!=null){
        httpRequest = new ActiveXObject('Msxml2.XMLHTTP');
      }
      else{
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }
  }
  if(httpRequest!=null){
      if(ajaxURL.indexOf("?") >= 0){
          ajaxURL = ajaxURL + "&timestamp=" + new Date();
      }
      else{
      	ajaxURL = ajaxURL + "?timestamp=" + new Date();
      }
      if(asyn){
          httpRequest.onreadystatechange = AjaxPrc;
      }
      if(formData==null){
        httpRequest.open("GET", ajaxURL, asyn);
      }
      else{
        httpRequest.open("POST", ajaxURL, asyn);
        httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
      }
  	httpRequest.send(formData);
    if(!asyn){
    		if(httpRequest.status == 200){
                responseString = httpRequest.responseText;
    		}
    		else{
                responseString = "";
    		}
    		httpRequest = null;
    		return responseString;
    }
  }
}		
		
//Ajax非同步處理
function AjaxPrc(){
  if(httpRequest!=null){
      if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
            ajaxResponse = httpRequest.responseText;
        } 
        else {
            ajaxResponse = "";
        }
        if(ajaxCallBackFunction!=null){
    	    eval(ajaxCallBackFunction);
        }	
        httpRequest = null;
      }
  }
}		
		
//字串替換
function charReplace(mStr,mChar1,mChar2){
	var tmpStr = mStr;
	if(tmpStr.indexOf(mChar1)<0){
		return tmpStr;
	}
	else{
		return charReplace(tmpStr.replace(mChar1,mChar2),mChar1,mChar2);
	}
}

//取得物件在頁面上的X座標
function FindPosX(obj){   
    var left = 0;   
    if (obj.offsetParent){         
        if(obj==null){return [0,0];}    
        left=obj.offsetLeft;    
        obj=obj.offsetParent;    
        while(obj){    
          if(obj.scrollLeft)//判斷是否有拉scroll   
           left-=obj.scrollLeft;      
           left+=obj.offsetLeft;    
           obj=obj.offsetParent;    
        }    
    }   
    else if (obj.x) {   
        left += obj.x;   
    }   
    return left;   
}

//取得物件在頁面上的Y座標
 function FindPosY(obj){
    var top = 0;   
    if (obj.offsetParent){
        if(obj==null){return [0,0];}    
        top=obj.offsetTop;    
        obj=obj.offsetParent;    
        while(obj){    
              if(obj.scrollTop)//判斷是否有拉scroll   
               top-=obj.scrollTop;      
               top+=obj.offsetTop;    
               obj=obj.offsetParent;    
        }    
     }   
     else if (obj.y){
         top += obj.y; 
    } 
     return top;   
}
 
//刪除 Table 中的 ROW
function DelTableRow(tbID,objTr){
    var oTable = document.getElementById(tbID);
    if(oTable!=null){
        oTable.deleteRow(objTr.rowIndex);
    }
}
 
//檢查日期格式
function DateFormat(obj)
{
   var formatCheck = /((^((1[8-9]\d{2})|([2-9]\d{3}))(\/)(10|12|0?[13578])(\/)(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(\/)(11|0?[469])(\/)(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(\/)(0?2)(\/)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)(\/)(0?2)(\/)(29)$)|(^([3579][26]00)(\/)(0?2)(\/)(29)$)|(^([1][89][0][48])(\/)(0?2)(\/)(29)$)|(^([2-9][0-9][0][48])(\/)(0?2)(\/)(29)$)|(^([1][89][2468][048])(\/)(0?2)(\/)(29)$)|(^([2-9][0-9][2468][048])(\/)(0?2)(\/)(29)$)|(^([1][89][13579][26])(\/)(0?2)(\/)(29)$)|(^([2-9][0-9][13579][26])(\/)(0?2)(\/)(29)$))/;
   if(!formatCheck.test(obj))
   {
       return false;
   }
   return true;
}

//移除ViewState 
function RemoveViewState(){
    try{
        var oDiv = document.getElementById("aspnetForm").getElementsByTagName("div")[0];
        oDiv.removeChild(oDiv.children[0]);
        oDiv = document.getElementById("aspnetForm").getElementsByTagName("div")[document.getElementById("aspnetForm").getElementsByTagName("div").length-1];
        oDiv.removeChild(oDiv.children[0]);
    }
    catch(e){

    }
}

//轉移資料  mode:0-單筆 1-全部
//arrow 0-左到右  1-右到左
function transModelData(source,target,mode){
    var dataSource = document.getElementById(source);
    var dataTarget = document.getElementById(target);
    var value;
    var text;
    if(mode==0){
        value = dataSource.value;
        if(dataSource.selectedIndex>-1){
            text = dataSource.options[dataSource.selectedIndex].text;
            dataTarget.add(new Option(text,value));
            dataSource.remove(dataSource.selectedIndex);
        }
    }
    else{
        var i;
        for(i=dataSource.length-1;i>=0;i--){
            dataSource.selectedIndex = i;
            value = dataSource.value;
            text = dataSource.options[dataSource.selectedIndex].text;
            dataTarget.add(new Option(text,value),0);
            dataSource.remove(dataSource.selectedIndex);
        }
    }
    dataSource.selectedIndex = -1;
    dataTarget.selectedIndex = -1;
}

//轉移資料  mode:0-add 1-del
function addMutliSelection(target,parent,mode,valuerank,chr){
    var parentList = parent.split(",")
    var dataSource = document.getElementById(parentList[parentList.length-1]);
    var dataTarget = document.getElementById(target);
    var value="";
    var text="";
    var i;
    var parentNode;
    if(mode==0){
        if(dataSource.selectedIndex>-1){
            for(i=0;i<parentList.length;i++){
                parentNode = document.getElementById(parentList[i]);
                text += parentNode.options[parentNode.selectedIndex].text + chr;
            }
            for(i=parentList.length-1;i>=parentList.length-valuerank;i--){
                parentNode = document.getElementById(parentList[i]);
                value = chr + parentNode.options[parentNode.selectedIndex].value + value;
            }
            text = text.substr(0,text.length-1);
            value = value.substr(1,value.length);
            
            var existFlag=false;
            for(var j=0;j<dataTarget.options.length;j++)
            {
                if(value==dataTarget.options[j].value)
                {
                    existFlag=true;
                    break;
                }
            }
            if(!existFlag)
                dataTarget.add(new Option(text,value));
        }
    }
    else{
        if(dataTarget.selectedIndex>-1){
            dataTarget.remove(dataTarget.selectedIndex);
        }
    }
}

//將文字欄位的值加到下拉選單中
function AddTextToSelection(source,target,blankMessage,errorMessage){
    var objSource = document.getElementById(source);
    var objTarget = document.getElementById(target);
    var i;
    if(objSource.value==""){
        alert(blankMessage);
        return false;
    }
    
    for(i=0;i<objTarget.options.length;i++){
        if(objSource.value == objTarget.options[i].value){
            alert(errorMessage);
            return false;
        }
    }
    objTarget.add(new Option(objSource.value,objSource.value));
    objSource.value = "";
}

//檢查Server端的檔案是否存在
function CheckRemoteFile(filepath){
    var status = false;
    if(window.XMLHttpRequest){
        httpRequest = new XMLHttpRequest();
    }
    else if(window.ActiveXObject!=null){
        if(new ActiveXObject('Msxml2.XMLHTTP')!=null){
            httpRequest = new ActiveXObject('Msxml2.XMLHTTP');
        }
        else{
            httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    if(httpRequest!=null){
        httpRequest.open("GET", filepath, false);
        httpRequest.send(null);
        status = httpRequest.status;
    }
    httpRequest = null;
    return (status >=200 && status <300);
}

//載入JS 語言檔
function LoadJsLangFile(){
        var JsLangSrc = "js/lang/" + userLanguage + "/common.js";
        if(!CheckRemoteFile(JsLangSrc)){
            JsLangSrc = "js/lang/" + defaultLanguage + "/common.js";
        }
        var oScript = document.createElement("script");
        oScript.type = "text/javascript";
        oScript.language = "javascript";
        oScript.src = JsLangSrc;
        var htmlHead = document.getElementsByTagName("head")[0];
        htmlHead.appendChild(oScript);
}

//新增 JS 語言檔
function AddJsLangFile(scriptName){
        var JsLangSrc = "js/lang/" + userLanguage + "/" + scriptName;
        if(!CheckRemoteFile(JsLangSrc)){
            JsLangSrc = "js/lang/" + defaultLanguage + "/" + scriptName;
        }
        var oScript = document.createElement("script");
        oScript.type = "text/javascript";
        oScript.language = "javascript";
        oScript.src = JsLangSrc;
        var htmlHead = document.getElementsByTagName("head")[0];
        htmlHead.appendChild(oScript);
}

//格式化數字(3位加逗點)
function numFormat(str){
    var j=str.length;
    var tempStr="";
    if(j==0){
        return str;
    }
    while(j-3>0){
        tempStr = "," + str.substring(j-3,j) + tempStr;
        j -= 3;
    }
    if(j!=0){
        tempStr = str.substring(0,j) + tempStr;
    }
    return tempStr;
}

LoadJsLangFile();

function AddLog(actionmode){
    var pageurl = this.location.toString();
    pageurl = pageurl.substring(pageurl.indexOf("/",10),pageurl.length);
    var flag = pageurl.indexOf("?");
    if(flag > -1){
        pageurl = pageurl.substring(0,flag);
    }
    RemoteRequest("actionMode="+actionmode+"&pageurl="+pageurl,"/Handler/AddPageLog.aspx",true);
}

function CheckBrowserStatus(){
    if(document.readyState!='complete'){
        setTimeout("CheckBrowserStatus()",1);
    }
    else{
        AddLog(4);
    }
}
