/*
 * NCMC utilities.
 *
 * By Jason Wang
 */


//**********************************************************
// Example:
// onMouseOver="toolTip('tool tip text here')";
// onMouseOut="toolTip()";
// -or-
// onMouseOver="toolTip('more good stuff', '#FFFF00', 'orange')";
// onMouseOut="toolTip()";
//**********************************************************
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;
var ie4 = document.all;
offsetX = 0;
offsetY = 20;
var toolTipSTYLE="";
function initToolTips()
{
  if(ns4||ns6||ie4)
  {
    if(ns4) toolTipSTYLE = document.toolTipLayer;
    else if(ns6) toolTipSTYLE = document.getElementById("toolTipLayer").style;
    else if(ie4) toolTipSTYLE = document.all.toolTipLayer.style;
    if(ns4) document.captureEvents(Event.MOUSEMOVE);
    else
    {
      toolTipSTYLE.visibility = "visible";
      toolTipSTYLE.display = "none";
    }
    document.onmousemove = moveToMouseLoc;
  }
}
function toolTip(msg, fg, bg)
{
  if(toolTip.arguments.length < 1) // hide
  {
    if(ns4) toolTipSTYLE.visibility = "hidden";
    else toolTipSTYLE.display = "none";
  }
  else // show
  {
    if(!fg) fg = "#777777";
    if(!bg) bg = "#FFFFFF";
    var content =
    '<table border="0" cellspacing="0" cellpadding="1" bgcolor="' + fg + '"><td>' +
    '<table border="0" cellspacing="0" cellpadding="1" bgcolor="' + bg +
    '"><td align="center"><font face="sans-serif" color="' + fg +
    '" size="-2">&nbsp\;' + msg +
    '&nbsp\;</font></td></table></td></table>';
    if(ns4)
    {
      toolTipSTYLE.document.write(content);
      toolTipSTYLE.document.close();
      toolTipSTYLE.visibility = "visible";
    }
    if(ns6)
    {
      document.getElementById("toolTipLayer").innerHTML = content;
      toolTipSTYLE.display='block'
    }
    if(ie4)
    {
      document.all("toolTipLayer").innerHTML=content;
      toolTipSTYLE.display='block'
    }
  }
}
function moveToMouseLoc(e)
{
  if(ns4||ns6)
  {
    x = e.pageX;
    y = e.pageY;
  }
  else
  {
    x = event.x + document.body.scrollLeft;
    y = event.y + document.body.scrollTop;
  }
  toolTipSTYLE.left = x + offsetX;
  toolTipSTYLE.top = y + offsetY;
  return true;
}
//**********************************************************


//**********************************************************
//* onMouseOver event for menu bar.
//**********************************************************
function highlightOn(obj)
{
  obj.style.backgroundColor="#ffcc66";
}

//**********************************************************
//* onMouseOut event for menu bar.
//**********************************************************
function highlightOff(obj)
{
  obj.style.backgroundColor="#e6e7e8";
}


//**********************************************************
//* onMouseOver event for list row.
//**********************************************************
function rowOn(obj, tips)
{
  obj.style.backgroundColor="#ffcc66";
}

//**********************************************************
//* onMouseOut event for list row.
//**********************************************************
function rowOff(obj)
{
  obj.style.backgroundColor="#FFFFFF";
}

//**********************************************************
//* Put a navigation tab button in blue
//**********************************************************
function writeTopNavOnTab(hyperLink, caption)
{
  document.write("<td><img height=19 alt='' src='../images/bluetableft.gif' width=15 border=0></td>");
  document.write("<td valign=center align=middle>");
  document.write("<table height=19 cellspacing=0 cellpadding=0 background=../images/tabfade.gif border=0>");
  document.write("<tr>");
  document.write("<td nowrap> <div align=center><a class=OnTab href='" + hyperLink + "' target=_top>" + caption + "</a> </div></td>");
  document.write("</tr>");
  document.write("</table>");
  document.write("</td>");
  document.write("<td><img height=19 alt='' src='../images/bluetabright.gif' width=12 border=0></td>");
}

//**********************************************************
//* Put a navigation tab button in gray
//**********************************************************
function writeTopNavOffTab(hyperLink, caption)
{
  document.write("<td><img height=19 alt='' src='../images/graytableft.gif' width=20 border=0></td>");
  document.write("<td valign=center align=middle>");
  document.write("<table height=19 cellspacing=0 cellpadding=0 background=../images/graytabbody.gif border=0>");
  document.write("<tr>");
  document.write("<td nowrap> <div align=center><a class=OffTab href='" + hyperLink + "' target=_top>" + caption + "</a> </div></td>");
  document.write("</tr>");
  document.write("</table>");
  document.write("</td>");
  document.write("<td><img height=19 alt='' src='../images/graytabright.gif' width=12 border=0></td>");
}

//**********************************************************
//* Open window from a Select object.
//**********************************************************
function dropdown(mySel)
{
  var myWin, myVal;
  myVal = mySel.options[mySel.selectedIndex].value;
  if(myVal)
  {
    if(mySel.form.target)myWin = parent[mySel.form.target];
    else myWin = window;
    if (! myWin) return true;
    myWin.location = myVal;
  }
  return false;
}

//**********************************************************
//* Example: obj = findObj("image1");
//**********************************************************
function findObj(theObj, theDoc)
{
  var p, i, foundObj;

  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++)
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);

  return foundObj;
}

//**********************************************************
// * Dependencies *
// this function requires the following snippets:
// JavaScript/readable_MM_functions/findObj
//
// Accepts a variable number of arguments, in triplets as follows:
// arg 1: simple name of a layer object, such as "Layer1"
// arg 2: ignored (for backward compatibility)
// arg 3: 'hide' or 'show'
// repeat...
//
// Example: showHideLayers(Layer1,'','show',Layer2,'','hide');
//**********************************************************
function showHideLayers()
{
  var i, visStr, obj, args = showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3)
  {
    if ((obj = findObj(args[i])) != null)
    {
      visStr = args[i+2];
      if (obj.style)
      {
        obj = obj.style;
        if(visStr == 'show') visStr = 'visible';
        else if(visStr == 'hide') visStr = 'hidden';
      }
      obj.visibility = visStr;
    }
  }
}

function moveSubMenu(subMenu)
{
  var obj;
  if ((obj = findObj(subMenu)) != null)
    obj.style.top = document.body.scrollTop + event.y - 10;
}


//**********************************************************
//Example: preloadImages('file.gif', 'http://www.x.com/y.gif');
//**********************************************************
function preloadImages()
{
  if(document.images)
  {
    if(!document.imageArray) document.imageArray = new Array();
    var i,j = document.imageArray.length, args = preloadImages.arguments;

    for(i=0; i<args.length; i++)
    {
      if (args[i].indexOf("#")!=0)
      {
        document.imageArray[j] = new Image;
        document.imageArray[j++].src = args[i];
      }
    }
  }
}


//**********************************************************
// New round function with decimal.
//**********************************************************
function round(num, dec)
{
  if (round.arguments.length < 2) dec = 0;
  var base = Math.pow(10, dec);
  return (Math.round(num * base)/base);
}

function formatNumber(num, dec) {
  if (formatNumber.arguments.length < 2) dec = 0;
  var base = Math.pow(10, dec);

  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
  num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*base+0.50000000001);
  cents = num%base;
  num = Math.floor(num/base).toString();
  if (dec > 1) {
    zeroNum = dec - cents.toString().length;
    for (i = 0; i < zeroNum; i++) {
      cents = "0" + 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));

  var ret = ((sign)?'':'-') + num;
  if (dec > 0) {
    ret += '.' + cents;
  }
  return (ret);
}

function unFormatNumber(num) {
  num = num.toString().replace(/\$|\,/g,'');
  return num;
}

function formatBoolean(obj) {
  if (!obj) return;
  if (obj.value == "y" || obj.value == "Y" || obj.value == "1" || obj.value == "t" || obj.value == "T") {
    obj.value = "Y";
  } else {
    obj.value = "N";
  }
}

//**********************************************************
// Convert a string (in form of YYYY-MM-DD) into a Date object.
//**********************************************************
function toDate(dateStr)
{
  var year = dateStr.substring(0, 4);
  var month = dateStr.substring(5,7) - 1;
  var day = dateStr.substring(8);
  return (new Date(year,month, day));
}

//***********************************************************
// Check counts of textArea 单字节
//***********************************************************
function textCounter(field, countfield, maxlimit)
{
  // 定义函数，传入3个参数，分别为表单区的名字，表单域元素名，字符限制；
  if (field.value.length > maxlimit)
    //如果元素区字符数大于最大字符数，按照最大字符数截断；
    field.value = field.value.substring(0, maxlimit);
  else
  //在记数区文本框内显示剩余的字符数；
  countfield.value = maxlimit - field.value.length;
}
//***********************************************************
// Count text length cosidering 1 Chinese char as 3 bytes.
//***********************************************************
function textLength(text)
{
  var length = 0;
  for (i=0; i < text.length; i++)
  {
    if (text.charCodeAt(i) < 256)
    {
      length += 1;
    } else {
      length += 3;
    }
  }
  return length;
}
//***********************************************************
// Count text length cosidering 1 Chinese char as 2 bytes.
//***********************************************************
function textLength1(text)
{
  var length = 0;
  for (i=0; i < text.length; i++)
  {
    if (text.charCodeAt(i) < 256)
    {
      length += 1;
    } else {
      length += 2;
    }
  }
  return length;
}
//***********************************************************
// 一个汉字相当3个字符来计算
//***********************************************************
function textCounter1(field, countfield, maxlimit){
  	// 定义函数，传入3个参数，分别为表单区的名字，表单域元素名，字符限制；
  	var len = textLength(field.value);
  	if (len > maxlimit){
		alert("不能超过" + maxlimit + "个字符");
    	//如果元素区字符数大于最大字符数，按照最大字符数截断；
    	field.value = field.value.substring(0, maxlimit);
  	}else
  	//在记数区文本框内显示剩余的字符数；
  	countfield.value = maxlimit - len;
}
//***********************************************************
// 一个汉字相当2个字符来计算
//***********************************************************
function textCounter2(field, countfield, maxlimit){
  	// 定义函数，传入3个参数，分别为表单区的名字，表单域元素名，字符限制；
  	var len = textLength1(field.value);
	if (len > maxlimit){
		alert("不能超过" + maxlimit + "个字符");
    	//如果元素区字符数大于最大字符数，按照最大字符数截断；
    	field.value = field.value.substring(0, maxlimit);
	}else
  		//在记数区文本框内显示剩余的字符数；
  		countfield.value = maxlimit - len;
}

