﻿/*
//直接用array的sort method處理
function listsort(AArray) {
    var i, j
    var tmpstr = ''
    //var compstr1, compstr2

    //Bubble Sort
    for (i=1; i<AArray.length; i++) {
        for (j=2;j<(AArray.length-i+1);j++) {
            //compstr1 = AArray[j-1][0].replace(/ /,'')
            //compstr2 = AArray[j][0].replace(/ /,'')
            if (AArray[j-1][0].localeCompare(AArray[j][0]) > 0) {
            //if(compstr1.localeCompare(compstr2)==1) {
                tmpstr = AArray[j-1]
                AArray[j-1] = AArray[j]
                AArray[j] = tmpstr
            }
        }
    }

    //List Sort
//    for (i = 1; i <= AArray.length - 2; i++) {

//        for (j=i+1; j<=AArray.length-i; j++) {

//            if (AArray[j][0] > AArray[i][0]) {
//                tmpItem = AArray[j]
//                AArray[j] = AArray[i]
//                AArray[i] = tmpItem
//            }
//        }
//    }

    return AArray
}
*/

function getJm(argIdx) {
    return 'jm' + PadL(argIdx, 6, '0'); 
}

function getCm(argIdx) { 
    if (argIdx == '') { return ''; }
    else { return 'cm' + PadL(argIdx, 6, '0'); }
}

//+-------------------------------------------------------------------------+
//| 取得歌詞array
//+-------------------------------------------------------------------------+
function getSngArray() {

    var Songs = new Array;

    Songs = getSA00100(1,    Songs);
    Songs = getSA00200(101,  Songs);
    Songs = getSA00300(201,  Songs);
    Songs = getSA00400(301,  Songs);
    Songs = getSA00500(401,  Songs);
    Songs = getSA00600(501,  Songs);
    Songs = getSA00700(601,  Songs);
    Songs = getSA00800(701,  Songs);
    Songs = getSA00900(801,  Songs);
    Songs = getSA01000(901,  Songs);
    Songs = getSA01100(1001, Songs);
    Songs = getSA01200(1101, Songs);
    Songs = getSA01300(1201, Songs);
    Songs = getSA01400(1301, Songs);

    return Songs;
}

/*
function searchSongs(argSngArray, argKeyword, argCond) {
    var tmpArray = new Array    
    var srhIdx = 0
    var idx = 1
    var item = 1
    
    if (argCond.toUppercase() == 'SONG') { srhIdx = 0 }
    else if (argCond.toUppercase() == 'SINGER') { srhIdx = 1 }
    else if (argCond.toUppercase() == 'KEYWORD') {srhIdx = 5 }

    for (idx = 1; idx < argSngArray.length;idx++) {
        if (argSngArray[idx][srhIdx] == argKeyword) {
            tmpArray[item] = argSngArray[idx]
            item ++
        }
    }
    //allen 2006.07.26
    //return listsort(tmpArray)
    //return tmpArray.sort()
    tmpArray.sort(function(a,b) { return a[0].localeCompare(b[0]); })
    return tmpArray
}
*/

//+-------------------------------------------------------------------------+
//| 從選單進入時展示歌詞列表的方式
//+-------------------------------------------------------------------------+
function outputSongList(ASngArray, AKeyword,  APage) {

    var tmpArray = new Array;
    var idx = 1;
    var item = 1;

    for (idx = 1; idx < ASngArray.length;idx++) {
        if (ASngArray[idx][5] == AKeyword) {
            tmpArray[item] = ASngArray[idx];
            item ++;
        }
    }
    //allen 2006.07.26
    //tmpArray = listsort(tmpArray)
    //tmpArray.sort()
    tmpArray.sort(function(a,b) { return a[0].localeCompare(b[0]); })

    APage = APage - 1;
    composeListTable(tmpArray, 'WRITE', APage, true);
}

//+-------------------------------------------------------------------------+
//| 取得目前中日文歌詞的收錄數量
//+-------------------------------------------------------------------------+
function getSongsNum(argSngArry) {
    var sngArray = argSngArry;
    var idx = 1;
    var sum = new Array;

    sum[0] = 0;
    sum[1] = 0;

    for (idx = 1; idx < sngArray.length;idx++) {
        if (Trim(sngArray[idx][2]) != '') { sum[0] ++; } // jp
        if (Trim(sngArray[idx][3]) != '') { sum[1] ++; } // ch
    }
    return sum;
}

//+-------------------------------------------------------------------------+
//| 換頁，主要供「移動」按鈕用
//+-------------------------------------------------------------------------+
function changePage(aObj, aMaxPage, aThisPage) {
    if (Trim(aObj.value) != '') {
        if (!isNaN(aObj.value)) {
            if ((parseFloat(aObj.value) >= 1) && (parseFloat(aObj.value) <= aMaxPage) && (parseFloat(aObj.value)!=parseFloat(aThisPage))) {
                document.location.href = getURLWithoutSearch(document.location.href) + '?Page=' + aObj.value
            }
        }
    }
}

//+-------------------------------------------------------------------------+
//| 組成歌曲List Table
//+-------------------------------------------------------------------------+
function composeListTable(argSngArray, argOutputType, argPage, argWithBr) {
    var idx = 1;

    var jap = '';
    var chn = '';
    var dc = '';
    var japflag = '';
    var chnflag = '';
    var linkinfo = '';
    var dcflag = '';
    var output = '';
    var PageCount = 10;
    var ColCount = 6;
    var TotalPage = Math.ceil((argSngArray.length - 1) / PageCount);
    var RowCount = 1;

    if (argWithBr == true) { output = output + '<br>'; }
    output = output + '<table align="center" class="songlist">';

    // From Search
    output = output + '<tr>';
    output = output + '<td class="title" width="5%">番号</td>';
    output = output + '<td class="title">曲名</td>';
    output = output + '<td class="title">歌手名</td>';
    output = output + '<td class="title" colspan="4">檢索結果:' + (argSngArray.length-1) + '件(' + TotalPage + 'ページ)</td>';
    output = output + '</tr>';

    //20041024
    for (idx = 0; idx < argSngArray.length; idx++) {
    //for (idx = 1; idx < argSngArray.length;idx++) {

        if (!argSngArray[idx]) { continue; }

        if ((idx <=  (argPage * 10 - 1)) || (idx > (argPage * 10 + PageCount - 1))) { continue; }

        if (Trim(argSngArray[idx][2]) == '') {
            jap = '\'song_jp/jm000000.htm\'';
            japflag = '&nbsp;';
        }
        else {
            jap = '\'song_jp/' + getDirectory(argSngArray[idx][6]) + '/jm' + PadL(argSngArray[idx][6], 6, '0') + '.htm\'';
            japflag = '日';
        }

        if (Trim(argSngArray[idx][3]) == '') {
            chn = '\'song_ch/cm000000.htm\'';
            chnflag = '&nbsp;';
        }
        else { 
            chn = '\'song_ch/' + getDirectory(argSngArray[idx][6]) + '/cm' + PadL(argSngArray[idx][6], 6, '0') + '.htm\'';
            chnflag = '中';
        }  

        linkinfo = '';
        linkinfo = linkinfo  + '<a href="JavaScript:void DispalySong(' + jap +','+ chn +')">';
        linkinfo = linkinfo  + '<img src="icon/lyric.jpg" alt="歌詞" border="0" width="30" height="30">';
        linkinfo = linkinfo  + '</a>';

        if (Trim(argSngArray[idx][4]) == 'n') { 
            dc = '&nbsp;';
        }
        else { 
            dc = '';
            dc = dc + '<a href="javascript:void subwin(\'desc/dc' + PadL(argSngArray[idx][6], 6, '0') + '.htm\',\'descwin\',600,320)">';
            dc = dc + '<img src="icon/desc.jpg" border="0" alt="紹介" width="30" height="30">';
            dc = dc + '</a>';
        }

        output = output + '<tr>';
        output = output + '<td nowrap class="seq">' + (idx+1) + '</td>'; //番号
        output = output + '<td nowrap class="name">'; //曲名
        output = output + argSngArray[idx][0];
        output = output + '</td>';

        output = output + '<td nowrap class="singer">'; //歌手名
        output = output + argSngArray[idx][1];
        output = output + '</td>';

        output = output + '<td class="japflag">'; //日本語あり
        output = output + japflag;
        output = output + '</td>';

        output = output + '<td class="chnflag">'; //中国語あり
        output = output + chnflag;
        output = output + '</td>';

        output = output + '<td class="chnflag">'; //Link Page Info
        output = output + linkinfo;
        output = output + '</td>';
        ColCount= 7

        output = output + '<td class="desp">'; //説明あり
        output = output + dc;
        output = output + '</td>';
        output = output + '</tr>';
        RowCount++;
    }

    //20080405
    for (idx = RowCount; idx <= 10; idx++) {
        output = output + '<tr>';
        output = output + '<td class="desp">&nbsp;</td>';
        output = output + '<td class="desp">&nbsp;</td>';
        output = output + '<td class="desp">&nbsp;</td>';
        output = output + '<td class="desp">&nbsp;</td>';
        output = output + '<td class="desp">&nbsp;</td>';
        output = output + '<td class="desp"><img src="icon/lyric_disabled.jpg" border="0" width="30" height="30"></td>';
        output = output + '<td class="desp">&nbsp;</td>';
        output = output + '</tr>';
    }
    //output = output + '<tr><td colspan="' + ColCount +'">&nbsp;</td></tr>';
    //output = output + '<tr><td colspan="' + ColCount +'" align="right"><font size="2" color="White">ページ選択:</font><select id="selPage"></select></td></tr>';

    output = output + '<tr><td colspan="' + ColCount +'" align="right">';
    output = output + '<font size="2" color="White">ページ:&nbsp;';
    output = output + '<input type="text" id="Page" size="3" maxlength="3">&nbsp;';
    output = output + '<input type="button" id="btGoto" value="移動" class="btnGreen" onClick="changePage(Page, ' + TotalPage + ', ' + (argPage+1) + ')">';
    output = output + '</font></td></tr>';
    
    output = output + '<tr class="controlbar" aligh="middle">';
    output = output + '<td colspan="' + ColCount + '" align="center">';

    // First, Perior
    if (argPage > 0) {
        output = output + '<a href="' + getURLWithoutSearch(document.location.href) + '?Page=1" style="text-decoration: none"><font color="white" face="Webdings" size="4">9</font></a>' + '&nbsp;&nbsp;&nbsp;' 
        output = output + '<a href="' + getURLWithoutSearch(document.location.href) + '?Page=' + parseInt(argPage) + '" style="text-decoration: none"><font color="white" face="Webdings" size="4">3</font></a>' + '&nbsp;&nbsp;&nbsp;'
    }
    else { 
        output = output + '<font face="Webdings" size="4" color="Black">9</font>&nbsp;&nbsp;&nbsp;'
        output = output + '<font face="Webdings" size="4" color="Black">3</font>&nbsp;&nbsp;&nbsp;'
    }

    // Every Page
    for (idx = 1; idx <= TotalPage;idx++) {
        if (idx != argPage + 1) {
            //總頁數與目前頁數相差10頁
            if ((TotalPage - argPage + 1)  <= 11) {
                //總頁數小於10頁或計算頁數大於總頁數減10
                if ((TotalPage <= 10) || (idx > TotalPage - 10)) {
                    output = output + '<a href="' + getURLWithoutSearch(document.location.href) + '?Page=' + parseInt(idx) + '"><font color="white" size="4">' + idx + '</font></a>' + '&nbsp;&nbsp;&nbsp;';
                }
            }
            else {
                //計算頁數大於目前頁數且計算頁數小於等於目前頁數加10
                if ((idx > argPage) && (idx <= argPage+10))  {
                    output = output + '<a href="' + getURLWithoutSearch(document.location.href) + '?Page=' + parseInt(idx) + '"><font color="white" size="4">' + idx + '</font></a>' + '&nbsp;&nbsp;&nbsp;';
                }
            }
        }
        else{
            output = output + '<font color="red" size="4"><b>' + idx + '</b></font>' + '&nbsp;&nbsp;&nbsp;';
        }
    }

    // Next, Last
    if (argPage < (Math.ceil((argSngArray.length - 1) / PageCount) - 1)) {    
        output = output + '<a href="' + getURLWithoutSearch(document.location.href) + '?Page=' + parseInt(argPage + 2) + '" style="text-decoration: none"><font color="white" face="Webdings" size="4">4</font></a>' + '&nbsp;&nbsp;&nbsp;'
        output = output + '<a href="' + getURLWithoutSearch(document.location.href) + '?Page=' + Math.ceil((argSngArray.length - 1) / PageCount) + '" style="text-decoration: none"><font color="white" face="Webdings" size="4">:</font></a>'
    }    
    else { 
        output = output + '<font face="Webdings" size="4" color="Black">4</font>&nbsp;&nbsp;&nbsp;'
        output = output + '<font face="Webdings" size="4" color="Black">:</font>'
    }
    output = output + '</td>';
    output = output + '</tr>';

    output = output + '</table>';
    
    argOutputType = argOutputType.toUpperCase();
    if (argOutputType =='WRITE') { document.write(output); }
    else if (argOutputType =='HTML') { return output; }
}

function getURLWithoutSearch(argURL) {
    var pos = argURL.indexOf('?');
    if (pos == -1) { return argURL; }
    else { return argURL.substring(0,pos); }
}