var LastName;
function setRowBold(n)
{
  if(n == "" || n == undefined) return;

  var f = ['cn','ym','b1','a1','mp','dp','df','mq','tq','op','hi','lo','rp','oi','bd','mt','sp','tp','td','iv','tv','nv','de','th','ga','ve','bt','pd','bw','dd'];
  if(LastName != undefined)
  {
    for(var i=0,len=f.length ; i<len ; i++)
    {
      var aCell = $(LastName +f[i]);
      if(aCell != undefined)
      {
        aCell.style.textDecoration = "none";
        aCell.style.fontWeight = "normal";
        //aCell.style.borderWidth = "0px";
        //aCell.style.fontStyle = "normal";
        //if(f[i]=='cn'){
          aCell.style.backgroundColor='';
          aCell.style.color='';
        //}
      }  
    }    
  }  

  for(var i=0,len=f.length ; i<len ; i++)
  {
    var aCell = $(n +f[i]);
    if(aCell != undefined)
    {
      aCell.style.textDecoration = "underline";
      aCell.style.fontWeight = "bold";
      //aCell.style.fontStyle = "italic";
      
      /*
      aCell.style.borderWidth = "1px";
      aCell.style.borderStyle = "solid";
      aCell.style.borderColor = "#ff0000";
      */
      
      //  aCell.style.backgroundColor='#ffff33';
      
      /*
      if(f[i]=='cn'){
        aCell.style.backgroundColor='#999999'
        aCell.style.color='#FFFFFF'
        aCell.style.fontStyle = "normal";
      }
      */

    }  
  }    
  LastName = n;
}  

function setCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}


function delCookie(name) {
  setCookie(name,"",-1);
}



function effect(oc1,oc2,oc3)
{
  var c1 = oco(oc1);
  var c2 = oco(oc2);
  var c3 = oco(oc3);
  
  if(c1 == null || c2 == null) return null;
  if (c1.r==c2.r && c1.g==c2.g && c1.b==c2.b) return null;
  if (c1.r==c3.r && c1.g==c3.g && c1.b==c3.b) return null;

  if ((c2.r-c1.r) > 4 )
    c1.r += 4;
  else if ((c2.r-c1.r) < -4 )
    c1.r -= 4;
  else
    c1.r = c2.r;

  if (c2.g-c1.g > 4 )
    c1.g += 4;
  else if (c2.g-c1.g < -4 )
    c1.g -= 4;
  else
    c1.g = c2.g;

  if (c2.b-c1.b > 4 )
    c1.b += 4;
  else if (c2.b-c1.b < -4 )
    c1.b -= 4;
  else
    c1.b = c2.b;

  var bg = '#' + c1.r.toColorPart() + c1.g.toColorPart() + c1.b.toColorPart() ;
  return bg;
}

function oco(c)
{
  if(c == undefined || c== '') return null;
  var r,g,b;
  c = c.toLowerCase();
  if ( c.indexOf("rgb(") == 0 )
  {
    var colors = c.substring(4, c.length - 1 );
    var colorArray = colors.split(",");
    r = parseInt( colorArray[0] );
    g = parseInt( colorArray[1] );
    b = parseInt( colorArray[2] );
  }
  else
  {
    if ( c.indexOf('#') == 0 ) c = c.substring(1);
    r = parseInt(c.substring(0,2),16);
    g = parseInt(c.substring(2,4),16);
    b = parseInt(c.substring(4,6),16);
  }
  
  return {r:r,g:g,b:b}
}


//-----------------------------------------------------------------------------
// sortTable(id, col, rev)
//
//  id  - ID of the TABLE, TBODY, THEAD or TFOOT element to be sorted.
//  col - Index of the column to sort, 0 = first column, 1 = second column,
//        etc.
//  rev - If true, the column is sorted in reverse (descending) order
//        initially.
//
// Note: the team name column (index 1) is used as a secondary sort column and
// always sorted in ascending order.
//-----------------------------------------------------------------------------

function sortTable(id, col, rev) {

  // Get the table or table section to sort.
  var aTable = document.getElementById(id);

  // The first time this function is called for a given table, set up an
  // array of reverse sort flags.
  if (aTable.reverseSort == null) {
    aTable.reverseSort = new Array();
    // Also, assume the team name column is initially sorted.
    aTable.lastColumn = 1;
  }

  // If this column has not been sorted before, set the initial sort direction.
  if (aTable.reverseSort[col] == null)
    aTable.reverseSort[col] = rev;

  // If this column was the last one sorted, reverse its sort direction.
  if (col == aTable.lastColumn)
    aTable.reverseSort[col] = !aTable.reverseSort[col];

  // Remember this column as the last one sorted.
  aTable.lastColumn = col;

  // Set the table display style to "none" - necessary for Netscape 6 
  // browsers.
  var oldDsply = aTable.style.display;
  aTable.style.display = "none";


  // Sort the rows based on the content of the specified column using a
  // selection sort.
  var tmpEl;
  var i, j;
  var minVal, minIdx;
  var testVal;
  var cmp;

  for (i = 0,len1=aTable.rows.length-1 ; i<len1 ; i++) {

    // Assume the current row has the minimum value.
    minIdx = i;
    minVal = getTextValue(aTable.rows[i].cells[col]);

    // Search the rows that follow the current one for a smaller value.
    for (j=i+1,len2=aTable.rows.length ; j<len2; j++) {
      testVal = getTextValue(aTable.rows[j].cells[col]);
      cmp = compareValues(minVal, testVal);
      // Negate the comparison result if the reverse sort flag is set.
      if (aTable.reverseSort[col])
        cmp = -cmp;
      // Sort by the second column (team name) if those values are equal.
      if (cmp == 0 && col != 1)
        cmp = compareValues(getTextValue(aTable.rows[minIdx].cells[1]),
                            getTextValue(aTable.rows[j].cells[1]));
      // If this row has a smaller value than the current minimum, remember its
      // position and update the current minimum value.
      if (cmp > 0) {
        minIdx = j;
        minVal = testVal;
      }
    }

    // By now, we have the row with the smallest value. Remove it from the
    // table and insert it before the current row.
    if (minIdx > i) {
      tmpEl = aTable.removeChild(aTable.rows[minIdx]);
      aTable.insertBefore(tmpEl, aTable.rows[i]);
    }
  }
  
  // Make it look pretty.
  makePretty(aTable, col);

  // Set team rankings.
  //setRanks(aTable, col, rev);

  // Restore the table's display style.
  aTable.style.display = oldDsply;

  return false;
}

//-----------------------------------------------------------------------------
// Functions to get and compare values during a sort.
//-----------------------------------------------------------------------------

// This code is necessary for browsers that don't reflect the DOM constants
// (like IE).
if (document.ELEMENT_NODE == null) {
  document.ELEMENT_NODE = 1;
  document.TEXT_NODE = 3;
}

function getTextValue(el) {
  return normalizeString(el.innerHTML);
}

function compareValues(v1, v2) {
  if (!isNaN(v1) || !isNaN(v2)) {
    if (!isNaN(v1) && !isNaN(v2)) {
      v1 = parseFloat(v1);
      v2 = parseFloat(v2);
    }
    else if (!isNaN(v1))
    {
      v1 = 1;
      v2 = 0;
    }
    else if (!isNaN(v2))
    {
      v1 = 0;
      v2 = 1;
    }
  }

  // Compare the two values.
  if (v1 == v2)
    return 0;
  if (v1 > v2)
    return 1
  return -1;
}

// Regular expressions for normalizing white space.
var whtSpEnds = new RegExp("^\\s*|\\s*$", "g");
var whtSpMult = new RegExp("\\s\\s+", "g");

function normalizeString(s) {
  s = s.replace(whtSpMult," ");  // Collapse any multiple whites space.
  s = s.replace(whtSpEnds,"");   // Remove leading or trailing white space.
  s = s.replace("%","");
  s = s.replace("▲","+");
  s = s.replace("﹢","+");
  s = s.replace("▼","-");
  s = s.replace("﹣","-");
  s = s.replace("元","");
  s = s.replace("倍","");
  s = s.replace("以上","");
  s = s.replace("以下","");
  return s;
}

//-----------------------------------------------------------------------------
// Functions to update the table appearance after a sort.
//-----------------------------------------------------------------------------

function makePretty(aTable, col) {
  var aRow = $('quote_head').rows[0];
  for (var i = 0; i < aRow.cells.length; i++) 
  {
    var aCell = aRow.cells[i];
    aCell.style.background = "#d0d0d0";
    if(i == col)
    {
      if(aTable.reverseSort[col])
        aCell.style.background = "#d0d0d0 url('images/dw4.gif') no-repeat 99% 99% ";
      else  
        aCell.style.background = "#d0d0d0 url('images/up4.gif') no-repeat 99% 99% ";
    }  
  }
  
  for (var i=0; i<aTable.rows.length ; i++)
  {
    var aRow = aTable.rows[i];
    for (var j=0; j<aRow.cells.length; j++)
    {
      var aCell = aRow.cells[j];
      
      if (i % 2 == 0 )
        aCell.style.backgroundColor = '#f0f0f0';
      else
        aCell.style.backgroundColor = '#e0e0e0';  

      //if(aCell.style.fontWeight == 'bold')
      //{
      //  aCell.style.backgroundColor = '#ffff33';  
      //}
    }
  }
  
  if(hash_cell != undefined)
  {
    var keys = hash_cell.keys();
    for(var i=0,len = keys.length ; i<len ; i++)
    {
        //hash_cell.remove(keys[i]);
        hash_cell.unset(keys[i]);
    }    
  }  
}
 
//若str的長度小於length，則在其左邊補sign   
function padLeft(str,length,sign){   
    if(str.length>=length)  return str;   
    else    return padLeft(sign+str,length,sign);   
}   
//若str的長度小於length，則在其右邊補sign   
function padRight(str,length,sign){   
    if(str.length>=length)  return str;   
     else    return padRight(str+sign,length,sign);   
}

