
/*----  Menu Start  ----*/
function MM_swapImgRestore()
{ //v3.0
  var i,x,a=document.MM_sr; 
  for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) 
    x.src=x.oSrc;
}

function MM_preloadImages() 
{ //v3.0
  var d=document; 
  if(d.images)
  { 
    if(!d.MM_p) 
        d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
    for(i=0; i<a.length; i++)
    {
        if (a[i].indexOf("#")!=0)
        { 
            d.MM_p[j]=new Image; 
            d.MM_p[j++].src=a[i];
        }
    }
  }
}

function MM_findObj(n, d) 
{ //v4.01
  var p,i,x;  
  if(!d) 
    d=document; 
  if((p=n.indexOf("?"))>0&&parent.frames.length) 
  {
    d=parent.frames[n.substring(p+1)].document; 
    n=n.substring(0,p);
  }
  if(!(x=d[n])&&d.all) 
    x=d.all[n]; 
  for (i=0;!x&&i<d.forms.length;i++) 
    x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
    x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) 
    x=d.getElementById(n); 
    
  return x;
}

function MM_swapImage() 
{ //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; 
  document.MM_sr=new Array; 
  
  for(i=0;i<(a.length-2);i+=3)
  {
   if ((x=MM_findObj(a[i]))!=null)
   {
    document.MM_sr[j++]=x; 
    if(!x.oSrc) 
        x.oSrc=x.src; 
    x.src=a[i+2];
   }
  }
}
/*----  Menu End  ----*/


/* Get Obj Position Start */
/* var pos=GetObjPos(ID); */
function CPos(x, y)
{
    this.x = x;
    this.y = y;
}

function GetObjPos(ATarget)
{
    var target = ATarget;
    var pos = new CPos(target.offsetLeft, target.offsetTop);    
    var target = target.offsetParent;
    while (target)
    {
         pos.x += target.offsetLeft;
         pos.y += target.offsetTop;
        
         target = target.offsetParent
     }
    
    return pos;
}
/* Get Obj Position End*/



/*--------------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------------*/


//功能:限制輸入框只能輸入A~Z、a~z、0~9,~!@#$%^&*()
function NormalInputLimit()
{
    var obj=event.srcElement;
    if (!((window.event.keyCode >= 44 && window.event.keyCode <= 57 ) || (window.event.keyCode >= 65 && window.event.keyCode <= 90 ) || (window.event.keyCode >= 97 && window.event.keyCode <= 122 ) || (window.event.keyCode == 13)))
    {  
        window.event.keyCode = 0 ; 
        obj.focus();
    }
}

//檢查有值時是否符合數字格式
function CheckIntEmp(src , val)
{         
    if (src.value != '' && isNaN(src.value))
    {                
        src.focus();        
        window.alert(document.getElementById("input_number").value);
        src.value = val;
    }
}
//檢查有值時是否符合日期格式
function CheckDateEmp(src , val)
{         
    if (src.value != '' )
    {               
        var t = Date.parse(src.value.replace(/\-/g , '/'));
        if(isNaN(t)) {            
            src.focus();
            window.alert(document.getElementById("input_date").value);
            src.value = val;
        }
    }
}

//檢查信箱格式
function CheckMailForm(obj)
{
    obj.value = trim(obj.value) ;
    if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(obj.value)))
    {
        alert("信箱格式錯誤請確實填寫!!");
        obj.focus();
    }
    else
        return false;
    
    return true ;
}

var MyNewsTimeout;
//var Timeout_MoveOut;
//移進主目錄，顯示子項目
function NewsSetTimeout(objNamem , objdivname )
{
    clearTimeout(MyNewsTimeout);   
    
    var xx = document.getElementById(objNamem);                               
    var curleft = findPosX(xx);
    var curtop = findPosY(xx);        
    curleft = curleft + 130;
    curtop = curtop ;
    
    MyNewsTimeout = setTimeout("ChangNews('"+objNamem+"','"+objdivname+"' , '"+ curleft +"' , '"+ curtop +"')" , 500);
}
//移出主目錄，等待後清空子項目
function NewsClearMenuTimeout()
{       
    //ChangNews('','','','')
    //clearTimeout(MyNewsTimeout);        
    MyNewsTimeout = setTimeout("ChangNews('','','','')" , 1000);              
}
//隱藏所有子項目
function ChangNews(objName , objdivname , tX , tY)
{                                    
    var Newsdiv = document.getElementsByTagName("div")        
    for(var i=0;i < Newsdiv.length;i++){    
        if(Newsdiv[i].name == "NewsDiv"){    
            Newsdiv[i].style.display = 'none'        
        }
    }    
    if(objdivname != '')
    {
        objdiv = document.getElementById(objdivname);                                       
        objdiv.style.left = tX + " px";
        objdiv.style.top = tY + " px"; 
        objdiv.style.display = '';  
    }
}

function SubMenuMouseOver()
{    
    clearTimeout(MyNewsTimeout);    
}

function trim(stringToTrim)
{ 
    return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function CheckEmpty(obj)
{
    obj.value = trim(obj.value) ;
    if (obj.value == '')
    {
        alert("請輸入" + obj.title + " !") ;
        obj.focus() ;        
    }
    else
        return false ;
        
    return true ;
}

function ClearText(obj)
{
    //Use For MainSearchForm.ascx
    obj.value = "";
}

//紀錄連結點閱次數
function LogClickLinkTime(sItemID , sDataID , sLinkID)
{            
    var fun = Ajax.xLogClickLinkTime(sItemID,sDataID,sLinkID).value;                                  
}

//記錄檔案點閱次數
function LogDownloadTime(sItemID , sDataID , sFileID)
{
    var fun = Ajax.xLogDownloadTime(sItemID,sDataID,sFileID).value;    
} 

//設定調整圖片大小
function ReSetSize(oPic , xsize)
{    
    var BaseSize = xsize;
        
    if(oPic.width > oPic.height)
    {
        if(oPic.width > BaseSize){
            oPic.width = BaseSize;
        }
    }
    else
    {             
        if(oPic.height > BaseSize){
            oPic.height = BaseSize;
        }
    } 
}  

function DateClick(src)
{           
    SelectDate(src,"yyyy-MM-dd");                  
}

//網頁接值
//var queryString = window.top.location.search.substring(1);
function getParameter ( queryString, parameterName ) {
// Add "=" to the parameter name (i.e. parameterName=value)
var parameterName = parameterName + "=";

    if ( queryString.length > 0 ) {
        // Find the beginning of the string
        begin = queryString.indexOf ( parameterName );
        // If the parameter name is not found, skip it, otherwise return the value
        if ( begin != -1 ) {
        // Add the length (integer) to the beginning
            begin += parameterName.length;
            // Multiple parameters are separated by the "&" sign
            end = queryString.indexOf ( "&" , begin );
            if ( end == -1 ) {
                end = queryString.length
            }
            // Return the string
            return unescape ( queryString.substring ( begin, end ) );
        }
        // Return "null" if no parameter has been found
        return "null";
    }
} 

function ShowHideObj(objname)
{   
    obj = document.getElementById(objname) ;
    if (obj)
    {
        if (obj.style.display == '')
            obj.style.display = 'none' ;
        else
            obj.style.display = '' ;
    }   
}

function OpenWindow(url,wID,hh,ww)
{
	var xLeft = (screen.width - hh)/2;
	var yTop = (screen.height - ww)/2;
	popupWin = window.open(url,wID,'width='+hh+',height='+ww+',scrollbars=no,left='+xLeft+',top='+yTop) ;
	popupWin.focus() ;
}

function ChangFocusNews(objName , objdivname)
{                                    
    var Thisdiv = document.getElementsByTagName("div")        
    for(var i=0;i < Thisdiv.length;i++){    
        if(Thisdiv[i].name == objName){    
            Thisdiv[i].style.display = 'none'        
        }
    }    
    if(objdivname != '')
    {
        objdiv = document.getElementById(objdivname);                               
        objdiv.style.display = '';                
    }
}

function pos(obj)
{
   var curleft = findPosX(obj);
   var curtop = findPosY(obj);
   alert(curleft + "," + curtop);
}
function findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
    {
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    }
    else if(obj.x)
        curleft += obj.x;
        
    return curleft;
}
function findPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
    {
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    }
    else if(obj.y)
        curtop += obj.y;
        
    return curtop;
}

/*----  跑馬燈控制項  ----*/
function slideLine(box,stf,delay,speed,h)
{
  //取得id
  var slideBox = document.getElementById(box);
  //預設值 delay:幾毫秒滾動一次(1000毫秒=1秒)
  //       speed:數字越小越快，h:高度
  var delay = delay||1000,speed = speed||20,h = h||20;
  var tid = null,pause = false;
  //setInterval跟setTimeout的用法可以咕狗研究一下~
  var s = function(){tid=setInterval(slide, speed);}
  //主要動作的地方
  var slide = function(){
  //當滑鼠移到上面的時候就會暫停
    if(pause) return;
  //滾動條往下滾動 數字越大會越快但是看起來越不連貫，所以這邊用1
    slideBox.scrollTop += 1;
  //滾動到一個高度(h)的時候就停止
    if(slideBox.scrollTop%h == 0){
  //跟setInterval搭配使用的
      clearInterval(tid);
  //將剛剛滾動上去的前一項加回到整列的最後一項
      slideBox.appendChild(slideBox.getElementsByTagName(stf)[0]);
  //再重設滾動條到最上面
      slideBox.scrollTop = 0;
  //延遲多久再執行一次
      setTimeout(s, delay);
    }
  }
  //滑鼠移上去會暫停 移走會繼續動
  slideBox.onmouseover=function(){pause=true;}
  slideBox.onmouseout=function(){pause=false;}
  //起始的地方，沒有這個就不會動囉
  setTimeout(s, delay);
}

function slideLine2(box,stf,delay,speed,h,h2)//多了第6個參數h2 是裡面各元素的高度
{
  var slideBox = document.getElementById(box);
  var delay=delay||1000,speed=speed||20,h=h||20,h2=h2||20;//h2也加個預設值
  var tid = null,pause = false;
  var s = function(){tid=setInterval(slide, speed);}
  var slide = function(){
    if(pause) return;
    slideBox.scrollTop += 1;
    if(slideBox.scrollTop%h == 0){
      clearInterval(tid);
      for(var j=0;j<h;j+=h2){//依據外面包住的div高度 跟裡面各元素的高度 判斷一次要移幾個到後面
	      slideBox.appendChild(slideBox.getElementsByTagName(stf)[0]);
	    }
      slideBox.scrollTop = 0;
      setTimeout(s, delay);
    }
  }
  slideBox.onmouseover=function(){pause=true;}
  slideBox.onmouseout=function(){pause=false;}
  setTimeout(s, delay);
}
/*----  跑馬燈控制項 End  ----*/


/*----  換頁控制項  ----*/
function FirstPage()//第一頁
{
    PageControlUrl(1);
}
function previousPage(nowPage)//上一頁
{
    PageControlUrl(nowPage);       
}
function nextPage(nowPage)//下一頁
{
    PageControlUrl(nowPage);
}
function LastPage(page)//最末頁
{
    PageControlUrl(page);
}
function PageControlUrl(page)
{
    var queryString = window.top.location.search.substring(1);    
    var mid = getParameter ( queryString, 'mid' )
    var cid = getParameter ( queryString, 'cid' )
    var cattype = getParameter ( queryString, 'cattype' )
    var stype = getParameter ( queryString, 'stype' )
    //if(cattype = "null") cattype = "";
    //if(stype = "null") stype = "";
    var Url = location.protocol + "//" + location.host + location.pathname + "?mid=" + mid + "&cid=" + cid + "&stype=" + stype + "&cattype=" + cattype + "&Page=" + page;
    location.href = Url;   
}
/*----  換頁控制項 End  ----*/


/*----  改變字型大小  ----*/
function ChangeFontSize(size)
{
    if(size == "")    
        size = MyAjax.GetNowFontSize().value;    
    
    var px
    if(size == "1")
        px = "10pt"
    else if(size == "2")
        px = "12pt"
    else if(size == "3")
        px = "15pt"
    else if(size == "4")
        px = "18pt"  
    
    var div_font = document.getElementsByTagName("div")        
    for(var i=0;i < div_font.length;i++){    
        if(div_font[i].name == "ChangeFont"){    
            div_font[i].style.fontSize = px        
        }
    }  
    FontImage(size)
    
    MyAjax.RememberFontSize(size);
}

function FontImage(id)
{
    document.getElementById("Font1").src = "images/font_01.jpg";
    document.getElementById("Font2").src = "images/font_02.jpg";
    document.getElementById("Font3").src = "images/font_03.jpg";
    document.getElementById("Font4").src = "images/font_04.jpg";
    
    var simg = new Image ()
    simg = document.getElementById("Font"+id);
    simg.src = "images/font-on_0"+ id +".jpg";

}
/*----  改變字型大小 End ----*/


function OpenPersonellDIV(Pop,Masked)
{ 
    var Result = MyAjax.CheckLogin().value;
    
    if(Result == "1")
    {
        location.href = "ContentDetail.aspx?mid=Personnel&cid=0";
    }
    else
    {
        OpenChoiceDIV(Pop,Masked);
        document.getElementById (Pop).style.top= Math.round ((document.documentElement.clientHeight/2)+ document.documentElement.scrollTop) + 'px';
    }
}

function PersonnelCheck()
{
    var PW = document.getElementById("PersonnelPW").value;
    
    var Result = MyAjax.PersonnelCheckLogin(PW).value;   
    
    if(Result == "1")
    {
        location.href = "ContentDetail.aspx?mid=Personnel&cid=0";
    }
    else
    {
        alert("密碼不正確！");
    }   
}

/*----  寄給朋友  ----*/
function OpenChoiceDIV(Pop,Masked)
{ 
    document.getElementById (Pop).style.visibility='visible';
    document.getElementById (Pop).style.display='';
    document.getElementById (Pop).style.top= Math.round ((document.documentElement.clientHeight/2)+ document.documentElement.scrollTop)-180 + 'px';
    document.getElementById (Pop).style.left='400px';
        
    document.getElementById (Masked).style.display='';
    document.getElementById (Masked).style.visibility='visible';
    document.getElementById (Masked).style.top='0px';
    document.getElementById (Masked).style.left='0px';
    document.getElementById (Masked).style.width=  document.documentElement.clientWidth + 'px';
    document.getElementById (Masked).style.height= document.documentElement.clientHeight + 1000 + 'px';
}

function CloseChoiceDIV(Pop ,Masked){
    document.getElementById (Masked).style.display='none';
    document.getElementById (Pop).style.display='none';
}

function SendContent(module,id,subject)
{
    //檢測 Mail   
    var obj1 = document.getElementById("tmail"); //朋友Email
    if(CheckEmpty(obj1))
        return;
    if(CheckMailForm(obj1))
        return;
    //檢測空白
    var obj2 = document.getElementById("fname"); //你的姓名
    if(CheckEmpty(obj2))
        return;
    //檢測 Mail   
    var obj3 = document.getElementById("fmail"); //你的Email
    if(CheckEmpty(obj3))
        return;
    if(CheckMailForm(obj3))
        return;
    var obj4 = document.getElementById("pmesg"); //給朋友的留言
    
    var Result = MyAjax.SendInformation(module , id , subject , obj1.value , obj2.value , obj3.value , obj4.value)
    if(Result)
    {
        alert("寄送成功！")
        CloseChoiceDIV();
    }
    else
    {
        alert("寄送失敗，請稍候再試，或與我們連絡。");
    }
}
/*----  寄給朋友 End ----*/


function SearchForm()
{   
    xObj = document.getElementById("keyword")         
    objContent = trim(xObj.value);       
    
    if(objContent != '請輸入關鍵字' && objContent != 'Please enter a keyword' && objContent != '')
    {
        if(objContent.indexOf("'") != -1 || objContent.indexOf("--") != -1)
        {
            alert("請勿輸入含有單引號的查詢字串");                
        }            
        else if(objContent.length < 2)
        {
            alert("請輸入至少兩個字的查詢字串");                
        }
        else
        {                     
            //紀錄查詢字樣
            MyAjax.Remember("SearchText" , objContent);
            window.location.href = "ContentSearch.aspx";
        }
    }
    else
    {        
        alert("請輸入關鍵字");
        event.returnValue = false
    }
}

function STYPEChange()
{
    var ObjCatType = document.getElementById("CatType");
    var Objstype = document.getElementById("STYPE");        
    var queryString = window.top.location.search.substring(1);    
    var mid = getParameter ( queryString, 'mid' );
    var cid = getParameter ( queryString, 'cid' );   
    var Page = getParameter ( queryString, 'Page' );
    var Url = location.protocol + "//" + location.host + location.pathname + "?mid=" + mid + "&cid=" + cid + "&stype=" + Objstype.value + "&cattype=" + ObjCatType.value ;//+ "&Page=" + Page;
    location.href = Url;   
}

