﻿// JScript 檔
/*

/* 
---------------- 凍結GridView 的 Header的JavaScriptCode-------------------
Step1:在HTML頁面放置這兩個DIV
<div id="FixedHeaderDIV"></div>
<div style="overflow-y: scroll; height: 400px" >
<!- GridView Put Hear -->
</div>

Step2:在程式碼中放入這兩行code

this.GridView1.Attributes.Add("style", "table-layout:fixed");
this.Page.ClientScript.RegisterStartupScript(typeof(string), "FixTableHeader", "<script language='javascript'>FixHeader('" + this.GridView1.ClientID + "','FixedHeaderDIV');</script>");


*/

//取得瀏覽器版本
function GetBrowserVersion() {
    if (window.XMLHttpRequest) { //Mozilla, Safari,...IE7
        var browserVersion = '';
        
        if (!window.ActiveXObject)// Mozilla, Safari,...      
        {
            browserVersion='Mozilla, Safari';
        }
        else {
            browserVersion='ie7';
        }


    }
    else {
        browserVersion='ie6';
    }

    return browserVersion;
}


function FixHeader(gridid,spanid)
{

 
 var t = document.getElementById(gridid);
 if(t!=null)
 {
     var t2 = t.cloneNode(true)
     for(i = t2.rows.length -1;i > 0;i--)
     t2.deleteRow(i)  
     t.deleteRow(0)  
     document.getElementById(spanid).appendChild(t2) ;
 }
}
//------------------------------------------------------------


//設定物件的隱藏或是顯示
function VisibleStateChange(objId)
{

 
 if(document.getElementById(objId).style.display=='none')
   document.getElementById(objId).style.display='block';
  else
  document.getElementById(objId).style.display='none';
}

function GridMouseOut(trObj,cssName)
            {
				if(trObj.title!=null && trObj.title=='NoUse')
					{
					trObj.className='DataGrid_NoUse';
					
					}
				else
                    {
                 if(trObj.className!='DataGrid_SelectedStyle')		
					trObj.className=cssName;
                   
                    }
			
			}
			function GridMouseMove(trObj,cssClassName)
			{
                if(trObj.className!='DataGrid_SelectedStyle')				
				        trObj.className=cssClassName;
			}

      function openModelWindow(url,width,height,top,left,StrongenablePostBack)
        { 
           var theForm = document.forms[0];
                       
               
           enablePostBack=        window.showModalDialog(url,'','dialogHeight:'+height+';dialogWidth:'+width+';dialogTop:'+top+';dialogLeft:'+left+';status:no;help:no;scroll:no;resizable:yes');
         
         if(StrongenablePostBack)
                enablePostBack=StrongenablePostBack;
                
                  if(enablePostBack) 
                  { 
                       theForm.submit();

                    }
                       }//Function
             


        
        //屬性:把字串的前後空白值去除
String.prototype.Trim=function(){return this.replace(/^\s+|\s+$/g,"");}
function Trim(str)
{
return str.replace(/^\s+|\s+$/g,"");
}
//只可輸入整數


function ___NumKeyInt()
{

  
	if (event.keyCode < 48 || window.event.keyCode > 57) 
	{
  
		event.returnValue = false;
		return false;
	}



}




//只可輸入小數
function ___NumKeyDouble()
{
	if (event.keyCode == 46) 
	{
		if (document.activeElement.value.indexOf('.') >= 0) 
		{
			event.returnValue = false;
			return false;
		}
    } else if (event.keyCode < 48 || event.keyCode > 57) 
    {
		event.returnValue = false;
		return false;
	}
}
//將數值格式化為有逗點分隔的數字
function ___FormatCurrency(number) {

//小數兩位

var num = number.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
//先乘100
num = Math.floor(num*100+0.50000000001);
//除100取餘數
cents = num%100;
num = Math.floor(num/100).toString();
if(cents>0)
{  
     if(cents<10)
      cents='0' + cents;       
     cents='.' + cents;      

    if(cents.substr(2,1)=='0')
       cents=cents.substr(0,2); 
   
}
else
cents='';

for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));


return  (((sign)?'':'-')  + num + cents);

}

function ___JsChkTimeObj(obj)//檢查時間格式
{	
/*
接受的時間格式:H(H):M(M):S(S)、H(H):M(M)、HHMMSS、HHMM
被括弧包起來代表可以省略
*/	
	
	str=obj.value.Trim();	
	if (str=="")
		return ;	
	var error;
	error="";
	var inthour=0,intminute=0,intsecond=0;
	var i,step=0,count=0;
	var flag=false;
	//檢查是否有分隔符號,若是則flag=true
	for (i=0;i<str.length;i++)
	{
		str=str.replace("０","0");
		str=str.replace("１","1");
		str=str.replace("２","2");
		str=str.replace("３","3");
		str=str.replace("４","4");
		str=str.replace("５","5");
		str=str.replace("６","6");
		str=str.replace("７","7");
		str=str.replace("８","8");
		str=str.replace("９","9");
		if (str.charCodeAt(i)<48 || str.charCodeAt(i)>57)
			flag=true;
	}
	//全部為數字組成,沒有分隔符號
	if (flag==false )
	{
		//最小長度為4,最大長度為6
		if (str.length<4 || str.length>6)
			error='請以「HH:MM:SS」格式輸入';
		else
		{
			for(i=0;i<str.length;i++)
			{
				if (i<2)
					inthour=inthour+parseInt(str.charAt(i))*Math.pow(10,(1-i));
				if (i>=2 && i<4)
					intminute=intminute+parseInt(str.charAt(i))*Math.pow(10,(3-i));
				if (i>=4)
					intsecond=intsecond+parseInt(str.charAt(i))*Math.pow(10,(5-i));
			}
		}
	}
	//用「:」分隔
	else
	{
		//最小長度為3,最大長度為8
		if (str.length<3 || str.length>8)
			error='請以「HH:MM:SS」格式輸入';
		else
		{
			for(i=0;i<str.length;i++)
			{
				if (str.charCodeAt(i)<48 || str.charCodeAt(i)>57)
				{
					count=0;
					step++;
				}
				else
				{
					switch(step)
					{
					case 0:
						inthour=inthour*Math.pow(10,count)+parseInt(str.charAt(i));
						count=1;
						break;
					case 1:
						intminute=intminute*Math.pow(10,count)+parseInt(str.charAt(i));
						count=1;
						break;
					case 2:
						intsecond=intsecond*Math.pow(10,count)+parseInt(str.charAt(i));
						count=1;
						break;
					default:
						error='請以「HH:MM:SS」格式輸入2';
					}
				}
			}
		}
	}

	if (inthour<0 || intminute<0 || intsecond<0)
		error='時間不正確!!';
	if (inthour>=24)
		error='小時不可超過23!!';
	if (intminute>=60)
		error='分鐘不可超過59!!';
	if (intsecond>=60)
		error='秒數不可超過59!!';
	//輸入時間
	if (error!="")
	{
		
		alert(error);
		obj.value="";
		obj.focus();
	}
	else
	{
		var str='';
		if (inthour<10)
			str=str+"0";
		str=str+inthour+":";
		if (intminute<10)
			str=str+"0";
		str=str+intminute+":"
		if (intsecond<10)
			str=str+"0";
		str=str+intsecond;
		
		obj.value=str;

	}
}

function ___ChkStrLength(obj,maxlen)//檢查中英文混合字串真實長度
{
	if(maxlen>0) //Alex Modify if maxlen don't have value then not check
	{	
	var len;
	var cword;
	var i;
	var str = new String;
	var newstr;
	
	
	str = obj.value;
	len=str.length;
	i=0;
	cword=0;
	for(i=0;i<len;i++)	{
		if(str.charCodeAt(i)>255)
			cword++;
		if((i+cword+1)>=maxlen)
			break;		
	}

	if(i<=len-1){
		if((i+cword+1)==maxlen)
			newstr = str.substring(0,i+1);
		else
			newstr = str.substring(0,i);
	
		if(newstr.length!=len)		{
			alert('此欄位最大長度為' + maxlen + ',請確認輸入值 ');
			obj.value = newstr;
		}
	}
	}
}


function ___ChkEmail(obj)//檢查輸入是否為Email 
{
		str=obj.value;
		var lstr=str.length
		if(lstr>0)
				{
				var at="@"
				var dot="."
				var lat=str.indexOf(at)	
				
				var ldot=str.indexOf(dot)
				if (str.indexOf(at)==-1){
				alert("請確認Email格式的正確性");
				obj.focus();
				return false;
				}

				if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
				alert("請確認Email格式的正確性");
				obj.focus();
				return false;
				}

				if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
					alert("請確認Email格式的正確性");
				obj.focus();
				return false;
				}

				if (str.indexOf(at,(lat+1))!=-1){
					alert("請確認Email格式的正確性");
				obj.focus();
				return false;
				}

				if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
					alert("請確認Email格式的正確性");
				obj.focus();
				return false;
				}

				if (str.indexOf(dot,(lat+2))==-1){
					alert("請確認Email格式的正確性");
				obj.focus();
				return false;
				}
				
				if (str.indexOf(" ")!=-1){
					alert("請確認Email格式的正確性");
				obj.focus();
				return false;
				}
		}
 		 return true;
	}
	
function ___ChkTime(obj)
{	
/*
接受的時間格式:H(H):M(M):S(S)、H(H):M(M)、HHMMSS、HHMM
被括弧包起來代表可以省略
*/	
	str=obj.value.Trim();
	if (str=="")
		return ;	
	var error;
	error="";
	var inthour=0,intminute=0,intsecond=0;
	var i,step=0,count=0;
	var flag=false;
	//檢查是否有分隔符號,若是則flag=true
	for (i=0;i<str.length;i++)
	{
		str=str.replace("０","0");
		str=str.replace("１","1");
		str=str.replace("２","2");
		str=str.replace("３","3");
		str=str.replace("４","4");
		str=str.replace("５","5");
		str=str.replace("６","6");
		str=str.replace("７","7");
		str=str.replace("８","8");
		str=str.replace("９","9");
		if (str.charCodeAt(i)<48 || str.charCodeAt(i)>57)
			flag=true;
	}
	//全部為數字組成,沒有分隔符號
	if (flag==false )
	{
		//最小長度為4,最大長度為6
		if (str.length<4 || str.length>6)
			error='請以「HH:MM:SS」格式輸入';
		else
		{
			for(i=0;i<str.length;i++)
			{
				if (i<2)
					inthour=inthour+parseInt(str.charAt(i))*Math.pow(10,(1-i));
				if (i>=2 && i<4)
					intminute=intminute+parseInt(str.charAt(i))*Math.pow(10,(3-i));
				if (i>=4)
					intsecond=intsecond+parseInt(str.charAt(i))*Math.pow(10,(5-i));
			}
		}
	}
	//用「:」分隔
	else
	{
		//最小長度為3,最大長度為8
		if (str.length<3 || str.length>8)
			error='請以「HH:MM:SS」格式輸入';
		else
		{
			for(i=0;i<str.length;i++)
			{
				if (str.charCodeAt(i)<48 || str.charCodeAt(i)>57)
				{
					count=0;
					step++;
				}
				else
				{
					switch(step)
					{
					case 0:
						inthour=inthour*Math.pow(10,count)+parseInt(str.charAt(i));
						count=1;
						break;
					case 1:
						intminute=intminute*Math.pow(10,count)+parseInt(str.charAt(i));
						count=1;
						break;
					case 2:
						intsecond=intsecond*Math.pow(10,count)+parseInt(str.charAt(i));
						count=1;
						break;
					default:
						error='請以「HH:MM:SS」格式輸入2';
					}
				}
			}
		}
	}

	if (inthour<0 || intminute<0 || intsecond<0)
		error='時間不正確!!';
	if (inthour>=24)
		error='小時不可超過23!!';
	if (intminute>=60)
		error='分鐘不可超過59!!';
	if (intsecond>=60)
		error='秒數不可超過59!!';
	//輸入時間
	if (error!="")
	{
		
		alert(error);
		obj.value="";
		obj.focus();
	}
	else
	{
		var str='';
		if (inthour<10)
			str=str+"0";
		str=str+inthour+":";
		if (intminute<10)
			str=str+"0";
		str=str+intminute+":"
		if (intsecond<10)
			str=str+"0";
		str=str+intsecond;
		obj.value=str;
	}
}

//------------------------------------------------------------------
/*說明:檢查是否正確身分證號
*/  		
	function ___ChkPersonalID(rsid_i){
   rsid_i.value = rsid_i.value.toUpperCase()
   if (rsid_i.value != ''){
      check="ERROR"
      if (rsid_i.value.length==10){
         if (((rsid_i.value.charAt(0)=="A") && (rsid_i.value.charAt(1)=="A"))||((rsid_i.value.charAt(0)=="A") && (rsid_i.value.charAt(1)=="Z"))){
            check="OK"
		 }else{
            checknum1=((rsid_i.value.charAt(0)>="A") && (rsid_i.value.charAt(0)<="Z")) 
            checknum2=((rsid_i.value.charAt(1)==1) || (rsid_i.value.charAt(1)==2))
            if (checknum2 && checknum1){
               id1 = rsid_i.value.charAt(0)
               if (id1 == 'A') id0=1
               else if (id1 == 'B') { id0=10 ;}
               else if (id1 == 'C') { id0=19 ;}
               else if (id1 == 'D') { id0=28 ;}
               else if (id1 == 'E') { id0=37 ;}
               else if (id1 == 'F') { id0=46 ;}
               else if (id1 == 'G') { id0=55 ;}
               else if (id1 == 'H') { id0=64 ;}
               else if (id1 == 'I') { id0=39 ;}
               else if (id1 == 'J') { id0=73 ;}
               else if (id1 == 'K') { id0=82 ;}
               else if (id1 == 'L') { id0=2 ;}
               else if (id1 == 'M') { id0=11 ;}
               else if (id1 == 'N') { id0=20 ;}
               else if (id1 == 'O') { id0=48 ;}
               else if (id1 == 'P') { id0=29 ;}
               else if (id1 == 'Q') { id0=38 ;} 
               else if (id1 == 'R') { id0=47 ;}
               else if (id1 == 'S') { id0=56 ;}
               else if (id1 == 'T') { id0=65 ;}
               else if (id1 == 'U') { id0=74 ;}
               else if (id1 == 'V') { id0=83 ;}
               else if (id1 == 'W') { id0=21 ;}
               else if (id1 == 'X') { id0=3 ;}
               else if (id1 == 'Y') { id0=12 ;}
               else if (id1 == 'Z') { id0=30 ;}
               id2 = id0 + rsid_i.value.charAt(1)*8 + rsid_i.value.charAt(2)*7 + rsid_i.value.charAt(3)*6 + rsid_i.value.charAt(4)*5 + rsid_i.value.charAt(5)*4 + rsid_i.value.charAt(6)*3 + rsid_i.value.charAt(7)*2 + rsid_i.value.charAt(8)*1 + rsid_i.value.charAt(9)*1
               if (id2 % 10 == 0){
                  check="OK"
			   }else{ 
                  check="ERROR"
			   }
            }
         }
      }
      if (check != "OK"){
						alert("身份證號碼錯誤! 請重新輸入!");         
						rsid_i.focus();
						}
		else if(check=="OK")
		{
		return true;
		}
   }//rsid_i!='';
}
function chkFloat(obj) {

 

   if (isNaN(obj.value)) {
       alert('is not number!');
       obj.focus();
   }


}


function chkInt(obj){
		if(obj.value == "" ){
			alert('請確認輸入值為正整數!');
			obj.focus();
			return false;
		} 

		var i;
		for(i=0;i<obj.value.length;i++)	{
		
			if(obj.value.charAt(i) > "9" || obj.value.charAt(i) < "0"){
				alert('請確認輸入值為正整數!');
				obj.focus();
				return false;
			}
		}
		return true;

}


//讀取,設定,刪除Cookie
/*
http://techpatterns.com/downloads/javascript_cookies.php
sample
// remember, these are the possible parameters for Set_Cookie:
// name, value, expires, path, domain, secure
Set_Cookie( 'test', 'it works', '', '/', '', '' );
if ( ExistCookie( 'test' ) ) alert( Get_Cookie('test'));
// and these are the parameters for Delete_Cookie:
// name, path, domain
// make sure you use the same parameters in Set and Delete Cookie.
Delete_Cookie('test', '/', '');
( ExistCookie( 'test' ) ) ? alert( Get_Cookie('test')) :
alert( 'it is gone');
------------
Set_Cookie( 'test', 'it works', '', '/', '', '' );
if (   Get_Cookie( 'test' ) ) 
alert( Get_Cookie('test'));
Delete_Cookie('test', '/', '');
*/
    function Set_Cookie(name, value, expires, path, domain, secure) {
        // set time, it's in milliseconds
        var today = new Date();
        today.setTime(today.getTime());

        /*
        if the expires variable is set, make the correct
        expires time, the current script below will set
        it for x number of days, to make it for hours,
        delete * 24, for minutes, delete * 60 * 24
        */
        if (expires) {
            expires = expires * 1000 * 60 * 60 * 24;
        }
        var expires_date = new Date(today.getTime() + (expires));

        document.cookie = name + "=" + escape(value) +
    ((expires) ? ";expires=" + expires_date.toGMTString() : "") +
    ((path) ? ";path=" + path : "") +
    ((domain) ? ";domain=" + domain : "") +
    ((secure) ? ";secure" : "");
    }


    // with this test document.cookie.indexOf( name + "=" );
    function ExistCookie(check_name) {
        // first we'll split this cookie up into name/value pairs
        // note: document.cookie only returns name=value, not the other components
        var a_all_cookies = document.cookie.split(';');
        var a_temp_cookie = '';
        var cookie_name = '';
        var cookie_value = '';
        var b_cookie_found = false; // set boolean t/f default f

        for (i = 0; i < a_all_cookies.length; i++) {
            // now we'll split apart each name=value pair
            a_temp_cookie = a_all_cookies[i].split('=');


            // and trim left/right whitespace while we're at it
            cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

            // if the extracted name matches passed check_name
            if (cookie_name == check_name) {
                b_cookie_found = true;
                // we need to handle case where cookie has no value but exists (no = sign, that is):
                if (a_temp_cookie.length > 1) {
                    cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
                }
                // note that in cases where cookie is initialized but no value, null is returned
                return cookie_value;
                break;
            }
            a_temp_cookie = null;
            cookie_name = '';
        }
        if (!b_cookie_found) {
            return null;
        }
    }

    // this function gets the cookie, if it exists
    // don't use this, it's weak and does not handle some cases
    // correctly, this is just to maintain legacy information
        function Get_Cookie(name) {

            var start = document.cookie.indexOf(name + "=");
            var len = start + name.length + 1;
            if ((!start) &&
               (name != document.cookie.substring(0, name.length))) {
                return null;
            }
            if (start == -1) return null;
            var end = document.cookie.indexOf(";", len);
            if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(len, end));
        }


        // this deletes the cookie when called
        function Delete_Cookie(name, path, domain) {
                    if (Get_Cookie(name)) document.cookie = name + "=" +
            ((path) ? ";path=" + path : "") +
            ((domain) ? ";domain=" + domain : "") +
            ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
                }



                function SetImageSize(obj, displayWdith, displayHeight) {


                    setTimeout("SetImageSizeTimeOut('" + obj.id + "', " + displayWdith + ", " + displayHeight + ");", 1);

                }

                //設定圖片大小
                function SetImageSizeTimeOut(objID, displayWdith, displayHeight) {


                    var obj = document.getElementById(objID);
                   
                  
                   
                    obj.style.display = 'block';
                    //var obj=  document.getElementById(ImageID);
                    var heightRate = parseFloat(displayHeight) / parseFloat(obj.height);
                    var widthRate = parseFloat(displayWdith) / parseFloat(obj.width);
                    var height=obj.height;
                     var width=obj.width;
                   
                   
                    var baseRate;
                    if (widthRate > heightRate)
                        baseRate = heightRate;
                    else
                        baseRate = widthRate;

                 
                      if (baseRate < 1) {

                        width = parseInt(baseRate * obj.width);
                        height = parseInt(baseRate * obj.height);

                    }


                    //javascript 的bug 必須要將寬度放在這重設
                    obj.width = width;
                    obj.height = height;
                
                    
                  

                }