﻿var xmlHttp=getXmlHttpRequest();
//取得當前瀏覽器支持的XMLHTTP對象   
function getXmlHttpRequest() 
{  
    var request=null;   
    if(window.XMLHttpRequest)
    {
        request = new XMLHttpRequest();
    } 
    else if (window.ActiveXObject)
    {
        request=new ActiveXObject("Msxml2.XMLHTTP");
        if (! request)
        {
            request=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return request;
}

//目的：提供開啟視窗的畫面
//參數：strUrl-->欲開啟畫面的網址，width-->畫面的寬度，height-->畫面的高度
//      WinName-->開啟的視窗名稱
//  xx. YYYY/MM/DD   VER     AUTHOR      COMMENTS　　
//   1. 2009/04/29    1.00     Lisa        New Create
function OpenWin(strUrl,width,height,WinName)
{
   var top=0;
   var left=0;
   if (height =='' && width==''){
        width=screen.availWidth; 
        height=screen.availHeight;
    }else if (height >screen.availHeight && width>screen.availWidth){
        width=screen.availWidth; 
        height=screen.availHeight;
    }else{
        top=(screen.availHeight-height)/2;
        left=(screen.availWidth-width)/2;
    }
   
    window.open(strUrl,WinName,'width='+width+'px,height='+height+'px,dependent,left='+left+',top='+top+',status=no,toolbar=false,menubar=no,scrollbars=yes,resizable=yes',true);
}  

function OpenPdf(strUrl,width,height,WinName) 
{
    OpenWin(strUrl,width,height,WinName);
//    window.setTimeout("DeletePDF('"+strUrl+"')",5000)
    DeletePDF(strUrl);
}

function OpenAttachment(strPath,strFileName,width,height,WinName,Title,strUsr)
{
    var SPath = strPath;
    var SName = strFileName;
    var TPath = SPath+"Attachment";
    
    ToURL="../US00/US0000ATT.aspx?Ajax=Ajax&SPath="+strPath+"&SName="+SName+"&TPath="+TPath+"&nocachernd="+Math.random();
    xmlHttp.open("POST",ToURL,false);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    xmlHttp.send("");
    var result = xmlHttp.responseText;
    if(result!= "")
    {	   
        var top=0;
        var left=0;
        if (height =='' && width==''){
            width=screen.availWidth; 
            height=screen.availHeight;
        }else if (height >screen.availHeight && width>screen.availWidth){
            width=screen.availWidth; 
            height=screen.availHeight;
        }else{
            top=(screen.availHeight-height)/2;
            left=(screen.availWidth-width)/2;
        }
        
        var strUrl = "../Temp/"+result;
        var oWin=window.open(strUrl,WinName,'width='+width+'px,height='+height+'px,dependent,left='+left+',top='+top+',status=no,toolbar=false,menubar=no,scrollbars=yes,resizable=yes',true);
//        if(Title != null && Title !='')
//            oWin.document.title=Title; 
        
        try{
             window.setTimeout("Delete('"+result+"')",5000)
        }
        catch(e){;}
//        ToURL="../US00/US0000ATT.aspx?Delete=Del&DelName="+result;
//        xmlHttp.open("POST",ToURL,false);
//        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
//        xmlHttp.send("");
    }
} 

function Delete(DelName)    
{
    ToURL="../US00/US0000ATT.aspx?Delete=DelAtt&DelName="+DelName+"&nocachernd="+Math.random();
    xmlHttp.open("POST",ToURL,true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    xmlHttp.send("");
}

function DeletePDF(DelName)    
{
    ToURL="../US00/US0000ATT.aspx?Delete=DelPdf&DelName="+DelName+"&nocachernd="+Math.random();
    xmlHttp.open("POST",ToURL,true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    xmlHttp.send("");
}
//目的：提供開啟showModalDialog的畫面
//參數：strUrl-->欲開啟畫面的網址，width-->畫面的寬度，height-->畫面的高度
//  xx. YYYY/MM/DD   VER     AUTHOR      COMMENTS　　
//   1. 2009/04/29    1.00     Lisa        New Create
function OpenDialog(strUrl,width,height)
{
    var top=0;
    var left=0;
    if (height =='' && width==''){
        width=screen.availWidth; 
        height=screen.availHeight;
    }else if (height >screen.availHeight && width>screen.availWidth){
        width=screen.availWidth; 
        height=screen.availHeight;
    }else{
        top=(screen.availHeight-height)/2;
        left=(screen.availWidth-width)/2;
    }
    var returnValue=false;
    var dlgwin=null;
       	//returnValue=window.showModalDialog(strUrl,{opener:window},'dialogLeft:'+left+';dialogTop:'+top+';dialogHeight:'+height+'px;dialogWidth:'+width+'px;scroll:yes;status:no;help:no');
    try{
        returnValue=window.showModalDialog(strUrl,window,'dialogLeft:'+left+';dialogTop:'+top+';dialogHeight:'+height+'px;dialogWidth:'+width+'px;status:no;help:no');
    }catch(e){returnValue=false;}
    if (returnValue==null && returnValue==undefined){ 
        returnValue=false;
    }
    dlgwin=null;
   	return returnValue;
} 


//*******************************************
//目的：透過XMLDOM去取得另一個Page的資料，傳回取回的Data
//參數：
//  xx. YYYY/MM/DD   VER     AUTHOR      COMMENTS　　
//   1. 2009/04/29    1.00     Lisa        New Create
function __ATLoadXml(vstrReq){
	var root, source;		
	var sT;
	
	status = 'searching, please wait....';
	source = new ActiveXObject('Microsoft.XMLDOM');
	source.async = false;
	source.load(vstrReq);
	//Check to see if there was an error parsing the response from the server
	if (source.parseError != 0){
		sT = 'XML Error...<br>reason:' + source.parseError.reason + '<br>';
		sT += 'errorCode:' + source.parseError.errorCode + '<br>';
		sT += 'filepos:' + source.parseError.filepos + '<br>';
		sT += 'line:' + source.parseError.line + '<br>';
		sT += 'linepos:' + source.parseError.linepos + '<br>';
		sT += 'reason:' + source.parseError.reason + '<br>';
		sT += 'srcText:' + source.parseError.srcText + '<br>';
		sT += '<pre>' + source.xml + '</pre><br>';
		alert(sT);
		status = 'XML Error!';}
	else {
		root = source.documentElement;		//Get a reference to root XML object
		status = 'Done';
		return root;}
}


//功能：設定二個控制項的絕對位置
//參數：obj1－傳入第一個控制項物件,objDiv--傳入對應位置的控制項
//  xx. YYYY/MM/DD   VER     AUTHOR      COMMENTS　　
//   1. 2009/04/29    1.00     Lisa        New Create
function SetDynamicPosition(obj1,objDiv){     
    var oBndRct =obj1.getBoundingClientRect();
    var xel, yel;
	xel = oBndRct.left;
	yel = oBndRct.bottom;
    objDiv.style.left=xel;
    objDiv.style.top=yel+document.body.scrollTop;
    objDiv.style.display="";
}

//功能：閱覽條件設定資訊
//參數：obj1－傳入欲顯示條件的控制項物件
//  xx. YYYY/MM/DD   VER     AUTHOR      COMMENTS　　
//   1. 2009/04/29    1.00     Lisa        New Create
function ShowQueryString(obj1,objQryStr){  
    var oBndRct =obj1.getBoundingClientRect();
    var xel, yel;
	   xel = oBndRct.left;
	   yel = oBndRct.bottom;
	   if (document.all(objQryStr)!=null){
	    var objDiv=document.all(objQryStr);
        objDiv.style.left=xel;
        objDiv.style.top=yel;
        objDiv.style.display="";
    }
}

//功能：關閉條件設定資訊
//參數：
//  xx. YYYY/MM/DD   VER     AUTHOR      COMMENTS　　
//   1. 2009/04/29    1.00     Lisa        New Create
function HideQueryString(objQryStr){  
	if (document.all(objQryStr)!=null){
        var objDiv=document.all(objQryStr);
        objDiv.style.display="none";
     }   
}



//功能：設定選取功能的按鈕
//參數：
//  xx. YYYY/MM/DD   VER     AUTHOR      COMMENTS　　
//   1. 2009/04/29    1.00     Lisa        New Create
function SetSelGrid(objChecked,obj1,obj2){   
     document.all(objChecked).checked=true;
     document.all(obj1).checked=false;
     document.all(obj2).checked=false;  
}


function __ATGetParentElementByTagName(vobj,vstrTagName) {
	while (vobj.tagName!=vstrTagName.toUpperCase()) {
		vobj=vobj.parentElement;
		if(vobj==null) return;
	}		
	return vobj
}

//功能：驗証Email的格式
//參數：field-->欲驗証的欄位 
//  xx. YYYY/MM/DD   VER     AUTHOR      COMMENTS　　
//   1. 2009/04/29    1.00     Lisa        New Create
function validate_email(objField)
{
    with (objField)
    {
        
        apos=value.indexOf("@");
        dotpos=value.lastIndexOf(".");
        if(value==""){
            return false;
        }
        
        re = /^[^\s]+@[^\s]+\.[^\s]{2,3}$/;
        if(re.test(value)==true)
        {
            return true;
        }
        else //if (apos<1||dotpos-apos<2)
        {
           // alert("Email error");
            ShowError('J000039');
            objField.value="";
            objField.focus();
            return false;
        }
      
    }
}

 /*
'''</summary>
'''提供Clinet呼叫Waiting畫面的Event，避免Client到按鈕
''' <remarks></remarks>
//  xx. YYYY/MM/DD   VER     AUTHOR      COMMENTS　　
//   1. 2009/04/29    1.00     Lisa        New Create
*/
function OnClientPageBlock(vblnDisplay)
{//alert(vblnDisplay);
    if (vblnDisplay==true){
        //Modify by Michael 2007/12/07 先讓滑鼠不能點
        document.body.style.cursor='wait';
        document.getElementById("ctl00_divFrmLoad").style.display="";
        document.getElementById("tdFrmLoad").innerHTML="<img src='../images/waiting.gif' alt='Data Is Loading, Please Wait…'/><br>Data Is Loading, Please Wait…";
    }else{
        document.body.style.cursor='';
        document.getElementById("ctl00_divFrmLoad").style.display="none";
    }
}

 /*
'''</summary>
'''提供Clinet偵測到有捲軸就切換視窗大小
''' <remarks></remarks>
//  xx. YYYY/MM/DD   VER     AUTHOR      COMMENTS　　
//   1. 2009/04/29    1.00     Lisa        New Create
*/
function fnSetWidowSize(){
    if (window.dialogHeight<parseInt(document.body.scrollHeight+50)){
        window.dialogHeight = document.body.scrollHeight+50+"px";
    }
}   


function getTop(e){
	var offset=e.offsetTop;
	if(e.offsetParent!=null) offset+=getTop(e.offsetParent);
	return offset;
}
function getLeft(e){
	var offset=e.offsetLeft;
	if(e.offsetParent!=null) offset+=getLeft(e.offsetParent);
	return offset;
}


// 列印
//在列印的報表畫面加入：
//<OBJECT  id="WebBrowser"  classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"  height="0"  width="0" ></OBJECT>
function fnPrint() {
    try    {
        // 儲存原本頁首頁尾的設定，然後設定空白
        var ret = saveAndClearSetting();
        // 列印
        try{
            document.all.WebBrowser.ExecWB(6,1);
            return true;
        }catch(e)
        {
            window.print();
        }
        // 回存原本頁首頁尾的設定
        if ( ret ) restoreSetting();
    } catch (e) { alert("err="+e.description); }
    return false;
}
var hkey_path = "HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
var hkey_key_header = hkey_path+"header"; // 頁首
var hkey_key_footer = hkey_path+"footer"; // 頁尾
var hkey_key_margin_bottom = hkey_path+"margin_bottom"; // 邊界（下）
var hkey_key_margin_left = hkey_path+"margin_left"; // 邊界（左）
var hkey_key_margin_right = hkey_path+"margin_right"; // 邊界（右）
var hkey_key_margin_top = hkey_path+"margin_top"; // 邊界（上）
var old_header = "&w&b第 &p 頁，共 &P 頁";
var old_footer = "&u&b&d";
var old_left = "";
var old_right = "";
var old_bottom = "";
var old_top = "";
// 儲存原本頁首頁尾的設定，然後設定空白
function saveAndClearSetting() {
  try {
    var RegWsh = new ActiveXObject("WScript.Shell");
    old_header = RegWsh.RegRead(hkey_key_header);
    old_footer = RegWsh.RegRead(hkey_key_footer);
    old_left = RegWsh.RegRead(hkey_key_margin_left);
    old_right = RegWsh.RegRead(hkey_key_margin_right);
    old_bottom = RegWsh.RegRead(hkey_key_margin_bottom);
    old_top = RegWsh.RegRead(hkey_key_margin_top);
    RegWsh.RegWrite(hkey_key_header,"");
    RegWsh.RegWrite(hkey_key_footer,"");
//    RegWsh.RegWrite(hkey_key_margin_left,"0.05");
//    RegWsh.RegWrite(hkey_key_margin_right,"0.05");
//    RegWsh.RegWrite(hkey_key_margin_bottom,"0.2");
//    RegWsh.RegWrite(hkey_key_margin_top,"0.2");
    return true;
  } catch (e) { 
    if ( e.description.indexOf("伺服程式無法產生物件") != -1 ) {//alert("ERR="+e.description);
        ;//alert("請調整IE瀏覽器的安全性\n網際網路選項＼安全性＼自訂層級\n「起始不標示為安全的ActiveX控制項」設定為啟用或提示。"); 
    } // if
    else {
        alert("ERR="+e.description); 
    } // else
  } // catch
    return false;
}
// 回存原本頁首頁尾的設定
function restoreSetting() {
  try {
    var RegWsh = new ActiveXObject("WScript.Shell");
    RegWsh.RegWrite(hkey_key_header,old_header);
    RegWsh.RegWrite(hkey_key_footer,old_footer);
    RegWsh.RegWrite(hkey_key_margin_left,old_left);
    RegWsh.RegWrite(hkey_key_margin_right,old_right);
    RegWsh.RegWrite(hkey_key_margin_bottom,old_bottom);
    RegWsh.RegWrite(hkey_key_margin_top,old_top);
  } catch (e) {
    if ( e.description.indexOf("伺服程式無法產生物件") != -1 ) {
        ;//alert("請調整IE瀏覽器的安全性\n網際網路選項＼安全性＼自訂層級\n「起始不標示為安全的ActiveX控制項」設定為啟用或提示。"); 
    } // if
    else {
        ;//alert("ERR="+e.description); 
    } // else
  } // catch
}

//獲得文件大小    
function getFileSize (fileName)
{   
    if(document.all)   
    {   
        window.oldOnError = window.onerror;           
        window.onerror = function (err) {   
            if(err.indexOf('utomation') != -1)   
            {   
                alert('The command has been forbidden!');                   
                return   true;               
            }             
            else     
                return   false;           
        };   
        var fso = new ActiveXObject('Scripting.FileSystemObject');   
        var file = fso.GetFile(fileName);           
        window.onerror = window.oldOnError;   
        return file.Size; 
   }
}

//獲得文件類型
function getFileType(files)
{
	var ary=files.value.split(".");
	return ary[ary.length-1];
}   

//限制上傳文件的大小
function ShowSize(files,i_byte)       
{       
  try{
      var fso,f,s;    
      fso=new ActiveXObject("Scripting.FileSystemObject");       
      f=fso.GetFile(files.value);       
      if((f.size/(1024*1024))>i_byte*1){   
      //alert("Sorry,the file size should not exceed "+ i_byte +"M");   
      files.value="";
      return false;   
      }   
      else   
      { OnClientPageBlock(true);
        return true;   
      }
  }  
  catch(e)
  {
    OnClientPageBlock(true);
    return true;
  }
  
}

//功能: 當按Enter時就移到下一個欄位//參數：//  xx. YYYY/MM/DD   VER     AUTHOR      COMMENTS　　
//   1. 2009/04/29    1.00     Lisa        New Create
function EnterKey()
{		
    try{
 
    if (window.event.keyCode==13)
    {			
        var type=document.activeElement.type;       
        if (!(type=="button" || type=="submit" || type=="textarea"))
        {						
			        var SName=document.activeElement.name;		
			        var Idx=ElementIdx(SName);					
			        if (Idx==-1)
			        {
				        window.event.returnValue=false;
			        }
			        else
			        {						
				        //注意:連結按鈕不能算				
				        var maxItem=document.forms[0].length;
				        while  (Idx<=maxItem-2)
				        {							
                var typeNext=document.forms[0].elements[Idx+1].type;																						
                    if (!(typeNext=="button" || typeNext=="submit" || typeNext=="hidden" || typeNext=="option" || typeNext=="select-one" || typeNext=="checkbox" || typeNext=="radio" || document.forms[0].elements[Idx+1].style.display=="none" ))
                    {								
                    if(document.forms[0].elements[Idx+1].readOnly==true || document.forms[0].elements[Idx+1].disabled==true || document.forms[0].elements[Idx+1].parentNode.style.display=="none" )
                    {
                        Idx++;
                        continue;
                    }
                    else
                    {										
                        document.forms[0].elements[Idx+1].focus();		
                        window.event.returnValue=false;	//由上一行替代Focus動作								
                        break;										
                    }
                }
					        else
					        {
						        if(Idx==maxItem-1)
						        {
							        document.forms[0].elements[Idx].focus();	
							        window.event.returnValue=false;
							        break;
						        }else{
						        Idx++;					
						        continue;	}					
					        }
				        }
				        window.event.returnValue=false;	//由上一行替代Focus動作	
			        }
        }
    }
    }catch(e){}
}

//功能: 回傳 Control 索引值 in <Form> tag
//參數：sSName傳入要Focus的欄位名稱
//  xx. YYYY/MM/DD   VER     AUTHOR      COMMENTS　　
//   1. 2009/04/29    1.00     Lisa        New Create
function ElementIdx(SName){
	var IsFound=false;		
	if (SName!=""){					
		for (var i=0;i<=document.forms[0].length-1;i++){
			var Name=document.forms[0].elements[i].name;					
			if (SName==Name){
				IsFound=true;
				break;
			}
		}
	}			
	if (IsFound) return i;  else  return -1;		
}

//*****************************************************
//* 函數名稱: GetCulture
//* 目    的: 獲取當前語言
//* 參    數: GetCulture()
//* 範    例: alert(GetCulture());
//*****************************************************
//add by Lisa 2009/04/29
function GetCulture()
{
    var o_Culture = document.getElementById('hid_CULTURE');
    if(o_Culture==null)
    {
        return 1;
    }
    else
    {
        if(o_Culture.value=="")
        {
            return 1;
        }
        else
        {
            if(o_Culture.value.toUpperCase()=="ZH-TW")return 1;
            if(o_Culture.value.toUpperCase()=="EN-US")return 2;
        }
    }
}
//*****************************************
//add by Lisa 2009/04/29
function showMsg(s_MsgCode)
{
    var k = GetCulture();
    var oDoc = new ActiveXObject("MSXML2.DOMDocument");
    var result ="../WEBSITE/Message.xml";
    oDoc.async=false;
    oDoc.load(result);
    var root=oDoc.documentElement;
    if(root==null)
    {
        var result ="../WEBSITE/Message.xml";
        oDoc.async=false;
        oDoc.load(result);
        root=oDoc.documentElement;
    }
    if (root==null)
    {
        alert("Error");
        return;
    } 
    var i; 
    for(i=0;i<root.childNodes.length;i++)
    {     
        if(root.childNodes[i].childNodes[0].text==s_MsgCode)
        {
            alert(root.childNodes[i].childNodes[k].text);
            return;
        }
    }
    alert("this Message not find");
    return;
}

//*****************************************
//下載
//add by Johnson 2009/06/11
function Fn_Download(strFilePath ,strFlag )
{   

      if(strFlag=="0")
      {
         alert(strFilePath);
         return false;
      }
      if(strFlag=="1")
      {      
         // window.open(strFilePath);  
          window.location.href= strFilePath;
      }
      if(strFlag=="2")
      {      
           //alert('2222='+strFilePath) ; 
//          window.open(strFilePath,"_blank","");  
          window.location.href= strFilePath;    
      }
  
    return false;

//    //alert("strFilePath"+strFilePath);
//    window.location.href=strFilePath;
//    return false;
}
