﻿/*
//直接用array的sort method處理
function ArraySort(argArray) {
    var i, j
    var tmpstr = ''
    //Bubble Sort
    for (i=0; i<argArray.length; i++) {    
//if (argArray[i]=='福山雅治') { alert(i) }
        for (j=1;j<(argArray.length-i);j++) {
            if (argArray[j-1].localeCompare(argArray[j]) > 0) {
                tmpstr = argArray[j-1]
                argArray[j-1] = argArray[j]
                argArray[j] = tmpstr
            }
        }
    }
    return argArray
}
*/

/*
document.cookie = 'name=Myweb;path=/;domain=.myweb.hinet.net';
var windowOpen = window.open;
window.open = function(URL, name, features) {
    if (URL == 'http://hiad.myweb.hinet.net/280x300.html') {
        return window
    }
    else {
        windowOpen(URL, name, features)
    }
}
*/

//+-------------------------------------------------------------------------+
//| 檢查是否在Frame框架中，若否則直接跳到frmaes.htm
//+-------------------------------------------------------------------------+
function chkFrame(argLv) {
    var strPath = '';
    if (Trim(argLv) == '') { argLv = 0; }
    if (top.location.href.indexOf('frames.htm') == -1) {
        for (var i=1; i<=argLv; i++) {
            strPath = '../' + strPath;
        }
    top.location.href = strPath + 'frames.htm';
    }
}

//+-------------------------------------------------------------------------+
//| 按Logo圖時重新載入歌曲資料
//+-------------------------------------------------------------------------+
function DataReload() {
    top.frData.document.location.reload();
}

//+-------------------------------------------------------------------------+
//| 傳入中日文歌曲的網址（程式自動組出），主要用於composeListTable
//+-------------------------------------------------------------------------+
function DispalySong(argJpUrl, argChUrl) {
    top.frData.nJpUrl = argJpUrl;
    top.frData.nChUrl = argChUrl;
    top.frTop.location.href = "display.htm";
}

//+-------------------------------------------------------------------------+
//| 取得網址參數，並丟入Array
//+-------------------------------------------------------------------------+
function getArgs() {
    var args = new Object();
    var query = location.search.substring(1);
    var pairs = query.split('&');
    for (var idx = 0; idx < pairs.length; idx++) {
        var pos = pairs[idx].indexOf('=');
        if (pos == -1) { continue; }
        var argname = pairs[idx].substring(0, pos);
        var value = pairs[idx].substring(pos + 1);
        args[argname] = unescape(value);
    }
    return args;
}

//+-------------------------------------------------------------------------+
//| 根據檔名，自動取得歌曲所在的目錄位置
//+-------------------------------------------------------------------------+
function getDirectory(ANum) {
    ANum = ANum + 99;
    ANum = ANum + '';
    ANum = ANum.substring(0,ANum.length-2) + '00';
    return PadL(ANum, 5, '0');
}

//+-------------------------------------------------------------------------+
//| 取字串長度，主要供PadL及PadR使用
//+-------------------------------------------------------------------------+
function getLength(argStr) {
    var cnt = 0;
    for (var i = 0; i < argStr.length; i++) {
        if (escape(argStr.charAt(i)).length >= 4) { cnt += 2; }
        else { cnt++; }
    }
    return cnt;
}

//+-------------------------------------------------------------------------+
//| 檢查傳入值是否位於陣列中，主要用於conv.htm
//+-------------------------------------------------------------------------+
function InArray(aArray, aItem) {
    for (var idx=0;idx<aArray.length;idx++) {
        if (aItem==aArray[idx]) { return true; }
    }
    return false;
}

//+-------------------------------------------------------------------------+
//| 傳回現在日期時間 yyyy/m/d h:m:s 型態
//+-------------------------------------------------------------------------+
function now() {
    var now = new Date();
    var nowStr;
    nowStr = now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate() + ' ';
    nowStr = nowStr + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
    return nowStr;
}

//+-------------------------------------------------------------------------+
//| 去除字串左邊所有半形空白
//+-------------------------------------------------------------------------+
function PadL(sAString, bSize, cChar) { 
    var trimStr = sAString + '';
    var sAStringLen = getLength(trimStr);
    var LeftStrLen = bSize - sAStringLen;
    var LeftStr = '';

    for (var i = 0; i < LeftStrLen; i++) { 
        LeftStr = LeftStr + cChar;
    }
    return LeftStr + trimStr;
}

//+-------------------------------------------------------------------------+
//| 去除字串右邊所有半形空白
//+-------------------------------------------------------------------------+
function PadR(sAString, bSize, cChar) {
    var trimStr = sAString + '';
    var bSrcLength = getLength(trimStr);
    var RightStrLen = bSize - bSrcLength;
    var RightStr = '';

    for (var i = 0; i < RightStrLen; i++) { 
        RightStr = RightStr + cChar;
    }
    return trimStr + RightStr;
}

//+-------------------------------------------------------------------------+
//| Cookie 相關 functions: Del, Read, Save
//+-------------------------------------------------------------------------+
function DelCookie(argName) {
    document.cookie = argName + '=;expires=Thu,01-Jan-70 00:00:01 GMT';
}

function ReadCookie(argName) {
    var namex = argName + "=";

    if (document.cookie.length == 0) { return null; }
    nameat = document.cookie.indexOf(namex);
    if (nameat == -1) { return null; }
    ValueAt = nameat + namex.length;
    endPos = document.cookie.indexOf(';', ValueAt);
    if (endPos == -1) { return document.cookie.substring(ValueAt); }
    else { return document.cookie.substring(ValueAt, endPos); }
}

function SaveCookie(argName, argValue, argYears) {
    var today = new Date();
    var yr = today.getFullYear();
    var mn = today.getMonth();
    var dy = today.getDate();

    if (argYears != null && argYears != '') { yr = yr + argYears; }
    //預設到該日的23時59分59秒0毫秒
    var expday = new Date(yr, mn, dy, 23, 59, 59, 0);
    expday = expday.toGMTString();
    document.cookie = argName + '=' + escape(argValue) + ';expires=' + expday;
}

//+-------------------------------------------------------------------------+
//| 顯示各網頁的 title 區域（div物件），名稱固定為 dvTitle
//+-------------------------------------------------------------------------+
function showtitle(aTitle, aShowBack) {
    document.write('<div id="dvTitle" style="position: absolute; z-index: 1; left: 0px; top: 0px">');
    document.write('<table id=control width="100%" border="0" cellspacing="0" cellpadding="0" class="borderon">');
    document.write('    <tr>');
    document.write('        <td width="10%"><img src="icon/mnopen.gif" width="16" height="15" class="button" onclick="javascript:window.top.fsMain.cols = \'0%,15%,85%,0%\';" vspace="2" hspace="1" alt="Open"></td>');
    document.write('        <td height="20" style="padding-top:3px"><nobr><font color="Black" size="2">&nbsp;' + aTitle + '</font></nobr></td>');
    if (eval(aShowBack)) {
        document.write('<td width="10%" align="right">');
        document.write('<input type="button" id="btBack" class="GuideButton" value="前へ" style="height:20px;margin:0px;padding:0px" onClick="top.history.go(-3)">&nbsp;');
        document.write('</td>');
    }
    document.write('    </tr>');
    document.write('</table>');
    document.write('</div>');
    document.write('<br>');
}

//+-------------------------------------------------------------------------+
//| 讓中日文歌詞網頁兩者的scroll動作同步
//+-------------------------------------------------------------------------+
function SongScroll(pagetype) {
    if (pagetype == 'ch') {
        parent.frJp.document.body.scrollTop = document.body.scrollTop;
    }
    else {
        parent.frCh.document.body.scrollTop = document.body.scrollTop;
    }
}

//+-------------------------------------------------------------------------+
//| 開啟提示子視窗
//+-------------------------------------------------------------------------+
function subwin(argUrl,argWinnm, argWidth, argHeight) {
    var screen_height = window.screen.height;
    var screen_width  = window.screen.width;
    var subwin_width  = argWidth;
    var subwin_height = argHeight;
    var subwin_top    = (screen_height - subwin_height) / 2;
    var subwin_left   = (screen_width  - subwin_width ) / 2;

    //  OPTIONS //
    var pDir    = "directories=no";
    var pLcn    = "location=no";
    var pMenu   = "menubar=no";
    var pStatus = "status=no";
    var pTool   = "toolbar=no";
    var pScroll = "scrollbars=yes";
    var pResize = "resizable=no";
    var pHeight = "height=" + subwin_height;
    var pWidth  = "width=" + subwin_width;
    var pTop    = "top=" + subwin_top;
    var pLeft   = "left=" + subwin_left;

    var pUrl    = argUrl;
    var pOption = pDir+','+pHeight+','+pLcn+','+pMenu+','+pStatus+','+pTool+','+pScroll+','+pResize+','+pWidth+','+pTop+','+pLeft+',z-lock=yes';

    var calWin = window.open(pUrl,argWinnm,pOption);
    calWin.onblur = subwinBlur;
}

function subwinBlur() {
    this.focus();
}

//+-------------------------------------------------------------------------+
//| 讓Title區域（div物件）永遠保持在網頁最上方，目前名稱固定為dvTitle
//+-------------------------------------------------------------------------+
function TitleKeepTop() {
    var winTop  = parseInt(document.body.scrollTop);
    var srcTop  = 0 + winTop;
    if (document.getElementById("dvTitle") != null) {
        document.getElementById("dvTitle").style.top = srcTop;
	}
}

//+-------------------------------------------------------------------------+
//| 十進位轉十六進位
//+-------------------------------------------------------------------------+
function ToHex(num){
   var st16 = "";
   var a16 = new Array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
   //取得商數;
   var quot = (num - (num % 16)) / 16;
   //將餘數加入十六進位
   st16 = a16[(num % 16)] + st16;

   while(quot > 15) {
        quot = (quot - (quot % 16)) / 16;
        st16 = a16[(quot%16)] + st16;
   }
   //如果最後的商數不為0則需補入
   if (quot != 0) { st16 = a16[quot] + st16; }
   return st16;
}

//+-------------------------------------------------------------------------+
//| 去除字串前後所的半形空白
//+-------------------------------------------------------------------------+
function Trim(argStr) { 
    argStr = argStr +'';
    var StrLen = argStr.length;
    var trimStr = '';
    var returnStr = '';

    for (var i = 0; i < StrLen ; i++) { 
        argStr.substring(i,i+1);
        trimStr = argStr.substring(i,i+1);
        if ((trimStr == ' ') && (i != StrLen )) { trimStr = ''; }
        returnStr = returnStr + trimStr;
    }
    return  returnStr;
}

//+-------------------------------------------------------------------------+
//| 將Array的內容填入combobox的value與text屬性中
//+-------------------------------------------------------------------------+
function ArrayToCombo(argArray, argCombo) {
    for (var idx = 0; idx < argArray.length; idx++) {
        argCombo.length = argCombo.length+1;
        argCombo.options[argCombo.length-1].value = argArray[idx];
        argCombo.options[argCombo.length-1].text  = argArray[idx];
    }
}