/*LAST MODIFIED <ASP/SMS/03/01> <3.3.1> <tionglk>    06/07/2005 show & hide */


//From "JavaScript Bible"
//Cookies
/*
//EXAMPLE:
var expdate = new Date ();
FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); // 24 hrs from now 
SetCookie ("ccpath", "http://www.hidaho.com/colorcenter/", expdate);
SetCookie ("ccname", "hIdaho Design ColorCenter", expdate);
SetCookie ("tempvar", "This is a temporary cookie.");
SetCookie ("ubiquitous", "This cookie will work anywhere in this domain",null,"/");
SetCookie ("paranoid", "This cookie requires secure communications",expdate,"/",null,true);
SetCookie ("goner", "This cookie must die!");
document.write (document.cookie + "<br>");
DeleteCookie ("goner");
document.write (document.cookie + "<br>");
document.write ("ccpath = " + GetCookie("ccpath") + "<br>");
document.write ("ccname = " + GetCookie("ccname") + "<br>");
document.write ("tempvar = " + GetCookie("tempvar") + "<br>");
*/
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
	i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
return null;
}

function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

//DHTML
function getObject(obj) {
	var theObj
	if (document.layers) {
		if (typeof obj == "string") {
			return document.layers[obj]
		} else {
			return obj
		}
	}
	else if (document.all) {
		if (typeof obj == "string") {
			return document.all(obj).style
		} else {
			return obj.style
		}
	}
	else if (document.getElementById) {
		if (typeof obj == "string") {
			return document.getElementById(obj).style
		} else {
			return obj.style
		}
	}
	return null
}

// position an object at a specific pixel coordinate
function shiftTo(obj, x, y) {
	var theObj = getObject(obj)
	if (theObj.moveTo) {
		theObj.moveTo(x,y)
	} else if (typeof theObj.left != "undefined") {
		theObj.left = x
		theObj.top = y
	}
}

// move an object by x and/or y pixels
function shiftBy(obj, deltaX, deltaY) {
	var theObj = getObject(obj)
	if (theObj.moveBy) {
		theObj.moveBy(deltaX, deltaY)
	} else if (typeof theObj.left != "undefined") {
		theObj.left = parseInt(theObj.left) + deltaX
		theObj.top = parseInt(theObj.top) + deltaY
	}
}

// set the z-order of an object
function setZIndex(obj, zOrder) {
	var theObj = getObject(obj)
	theObj.zIndex = zOrder
}

// set the background color of an object
function setBGColor(obj, color) {
	var theObj = getObject(obj)
	if (theObj.bgColor) {
		theObj.bgColor = color
	} else if (typeof theObj.backgroundColor != "undefined") {
		theObj.backgroundColor = color
	}
}

// set the visibility of an object to visible
function show(obj) {
	var theObj = getObject(obj)
	theObj.display = ""
	//theObj.visibility = "visible"
}

// set the visibility of an object to hidden
function hide(obj) {
	var theObj = getObject(obj)
	theObj.display = "none"
	//theObj.visibility = "hidden"
}

// retrieve the x coordinate of a positionable object
function getObjectLeft(obj)  {
	var theObj = getObject(obj)
	return parseInt(theObj.left)
}

// retrieve the y coordinate of a positionable object
function getObjectTop(obj)  {
	var theObj = getObject(obj)
	return parseInt(theObj.top)
}

function setImgDim(img,w,h,imgobj){
	var obj = imgobj? imgobj: document.getElementById(img);
	if(obj.width>w || obj.height>h){
		if(obj.width>=obj.height){
			//obj.width=w;
			//obj.height=parseInt((w*obj.height)/obj.width);
			//22/09/2003						
			obj.style.width=w;
			obj.style.height=parseInt((w*obj.height)/obj.width)
			/*
			obj.width=w;
			obj.height=parseInt((w*img.height)/img.width)
			*/
			}
		else{
			//obj.height=h;
			//obj.width=parseInt((h*obj.width)/obj.height);
			obj.style.height=h;
			obj.style.width=parseInt((h*obj.width)/obj.height)
			/*
			obj.height=h;
			obj.width=parseInt((h*img.width)/img.height)
			*/
			}
		}	
	//obj.style.display = '';
	}


//TIONGLK, 25/07/2003 V1.0
//echo "return chkNumeric($objName,$p,$d,"true",'$allow','$j_err.$label','$j_xamt','$j_xrange','$j_xdec');";
function chkNumeric(objName,p,d,xZero,allow,j_err,j_xamt,j_xrange,j_xdec){
var checkOK = "0123456789" + allow; //comma + period + hyphen....
var allValid = true;
var allNum = "";
var chkVal = "";

//validate numeric
for (var i=0; i<objName.value.length; i++){
	ch=objName.value.charAt(i);
	for(j=0; j<checkOK.length; j++) if(ch==checkOK.charAt(j)) break;
	if (j==checkOK.length){
		allValid = false; break;
		}
	if (ch != ",") allNum += ch;
	}

if (!allValid){	
	objName.select();
	alert(j_err + ' - ' + j_xamt)
	return false;
	}

//validate decimal
if (allNum.indexOf('.') != -1){
	chkVal= allNum.substring(allNum.indexOf('.')+1, allNum.length);
	if (chkVal.length > d || chkVal.length==0) {
		objName.select();
		alert(j_err + ' - ' + j_xdec)
		return false;
		}
	}
//validate precision
chkVal =  allNum.indexOf('.') != -1? allNum.substring(0,allNum.indexOf('.')): allNum;
if(chkVal.length > p || chkVal.length==0) {
	objName.select();
	alert(j_err + ' - ' + j_xrange)
	return false;
	}

if(xZero && parseFloat(allNum)==0){
	objName.select();
	alert(j_err + ' - ' + j_xrange)
	return false;
	}
}


function format(value){
  var NDIGITS = 16;
  var entered = true;
  var fixed = 0;
  var valStr = "" + value;
  if (valStr.indexOf("N")>=0 || (value == 2*value && value == 1+value))
   return "Error ";
  var i = valStr.indexOf("e")
  if (i>=0)
  {
   var expStr = valStr.substring(i+1,valStr.length);
   if (i>11) i=11;  // max 11 digits
   valStr = valStr.substring(0,i);
   if (valStr.indexOf(".")<0) valStr += ".";
   valStr += " " + expStr;
  }
  else
  {
   var valNeg = false;
   if (value < 0)
    { value = -value; valNeg = true; }
   var valInt = Math.floor(value);
   var valFrac = value - valInt;
   var prec = NDIGITS - (""+valInt).length - 1; // how many digits available after period
   if (! entered && fixed>0)
    prec = fixed;
   var mult = " 1000000000000000000".substring(1,prec+2);
   var frac = Math.floor(valFrac * mult + 0.5);
   valInt = Math.floor(Math.floor(value * mult + .5) / mult);
   if (valNeg)
    valStr = "-" + valInt;
   else
    valStr = "" + valInt;
   var fracStr = "00000000000000"+frac;
   fracStr = fracStr.substring(fracStr.length-prec, fracStr.length);
   i = fracStr.length-1;
   if (entered || fixed==0)
   {
    // remove trailing zeros unless fixed during entry.
    while (i>=0 && fracStr.charAt(i)=="0")
     --i;
    fracStr = fracStr.substring(0,i+1);
   }
   if (i>=0) valStr += "." + fracStr;
  }
  return valStr;
}

function number_format(val, decimal, cur){
	var newVal, bgDigit, decDigit;
	var IsDot=false;
	
	//round to decimal
	n = Math.pow(10, !decimal ? 2 : decimal);
	val=format(Math.round(format(val*n)) / n);

	newVal=val.split(".");
	if (newVal[1]){
		//dot exist
		newVal[1]=newVal[1].substring(0,decimal);
		bgDigit=newVal[1].length+1;
		decDigit=newVal[1];
	}else{
		//not dot
		bgDigit=1;
		decDigit='';
	}
	for (var i=bgDigit; i<=decimal; i++){
		decDigit=decDigit+'0';
	}
	val=newVal[0]+'.'+decDigit;

	if  (cur==true){
		//currency format
		var len, firstDigit, result='';
		newVal=val.split(".");
		len=newVal[0].length;

		if (len>3){
			firstDigit=len%3;
			for(var i=0; i<len; i++){
				if (i%3==firstDigit && i!=len && i!=0){
					result=result + ",";
				}//end if 
				result=result + val.substring(i,i+1);
			}//end for
			if (newVal[1]) result=result+'.'+newVal[1];			
			return result;
		}else{//end if len>3
			return val;
		}
	}else{
		return val;
	}
}

//frances - to remove the ',' for monetary value
function remove_comma(num){
	var result, finals='';
	result=num.split(",");

	for(var i=0; i<result.length; i++){
		finals=finals+result[i];
	}
	return finals;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

