//format functions

function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
 /**********************************************************************
  IN:
   NUM - the number to format
   decimalNum - the number of decimal places to format the number to
   bolLeadingZero - true / false - display a leading zero for
           numbers between -1 and 1
   bolParens - true / false - use parenthesis around negative numbers
   bolCommas - put commas as number separators.
  
  RETVAL:
   The formatted number!
  **********************************************************************/
 { 
  if (isNaN(parseInt(num))) return "";
 
  var tmpNum = num;
  var iSign = num < 0 ? -1 : 1;  // Get sign of number
  
  // Adjust number so only the specified number of numbers after
  // the decimal point are shown.
  tmpNum *= Math.pow(10,decimalNum);
  tmpNum = Math.round(Math.abs(tmpNum))
  tmpNum /= Math.pow(10,decimalNum);
  tmpNum *= iSign;     // Readjust for sign
  
  
  // Create a string object to do our formatting on
  var tmpNumStr = new String(tmpNum);
 
  // See if we need to strip out the leading zero or not.
  if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
   if (num > 0)
    tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
   else
    tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
   
  // See if we need to put in the commas
  if (bolCommas && (num >= 1000 || num <= -1000)) {
   var iStart = tmpNumStr.indexOf(".");
   if (iStart < 0)
    iStart = tmpNumStr.length;
 
   iStart -= 3;
   while (iStart >= 1) {
    tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
    iStart -= 3;
   }  
  }
 
  // See if we need to use parenthesis
  if (bolParens && num < 0)
   tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";
 
  //if current number of decimals is not enough, add zeros
  if (decimalNum > 0) {
  	var i;
	 var idxPoint = tmpNumStr.lastIndexOf(".");
	 //alert(idxPoint);
	 if (idxPoint > 0) {
	 	var decNow = tmpNumStr.length - (idxPoint+1);
		//alert(tmpNumStr.length);
	 	for(i=0; i<decimalNum-decNow; i++) {
   			tmpNumStr = tmpNumStr + "0"; 
		}
 	 } else {
	 	tmpNumStr = tmpNumStr + ".";
	 	for(i=0; i<decimalNum; i++) {
   			tmpNumStr = tmpNumStr + "0"; 
	 	}
 	 }  
  }
 
 
  return tmpNumStr;  // Return our formatted string!
 }
 
 function FormatNumber_ZE(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
 /**********************************************************************
  IN:
   NUM - the number to format
   decimalNum - the number of decimal places to format the number to
   bolLeadingZero - true / false - display a leading zero for
           numbers between -1 and 1
   bolParens - true / false - use parenthesis around negative numbers
   bolCommas - put commas as number separators.
  
  RETVAL:
   The formatted number!
   
   AANPASSING: ZE (ZeroEmpty)
   als de numerieke waarde 0 is, ook lege string teruggeven
   
  **********************************************************************/
 { 
  if (isNaN(parseInt(num))) return "";
  //if (parseInt(num) == 0) return "";
if (parseFloat(comma2point(num)) == 0 || num == 0.00 ) return "";
 
  var tmpNum = num;
  var iSign = num < 0 ? -1 : 1;  // Get sign of number
  
  // Adjust number so only the specified number of numbers after
  // the decimal point are shown.
  tmpNum *= Math.pow(10,decimalNum);
  tmpNum = Math.round(Math.abs(tmpNum))
  tmpNum /= Math.pow(10,decimalNum);
  tmpNum *= iSign;     // Readjust for sign
  
  
  // Create a string object to do our formatting on
  var tmpNumStr = new String(tmpNum);
 
  // See if we need to strip out the leading zero or not.
  if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
   if (num > 0)
    tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
   else
    tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
   
  // See if we need to put in the commas
  if (bolCommas && (num >= 1000 || num <= -1000)) {
   var iStart = tmpNumStr.indexOf(".");
   if (iStart < 0)
    iStart = tmpNumStr.length;
 
   iStart -= 3;
   while (iStart >= 1) {
    tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
    iStart -= 3;
   }  
  }
 
  // See if we need to use parenthesis
  if (bolParens && num < 0)
   tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";
 
  //if current number of decimals is not enough, add zeros
  if (decimalNum > 0) {
  	var i;
	 var idxPoint = tmpNumStr.lastIndexOf(".");
	 //alert(idxPoint);
	 if (idxPoint > 0) {
	 	var decNow = tmpNumStr.length - (idxPoint+1);
		//alert(tmpNumStr.length);
	 	for(i=0; i<decimalNum-decNow; i++) {
   			tmpNumStr = tmpNumStr + "0"; 
		}
 	 } else {
	 	tmpNumStr = tmpNumStr + ".";
	 	for(i=0; i<decimalNum; i++) {
   			tmpNumStr = tmpNumStr + "0"; 
	 	}
 	 }  
  }
 
 
  return tmpNumStr;  // Return our formatted string!
 }
 
 function FormatNumberMoreDec(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
 /**********************************************************************
  IN:
   NUM - the number to format
   decimalNum - the number of decimal places to format the number to
   bolLeadingZero - true / false - display a leading zero for
           numbers between -1 and 1
   bolParens - true / false - use parenthesis around negative numbers
   bolCommas - put commas as number separators.
  
  RETVAL:
   The formatted number!
   
   AANPASSING: MoreDec (MoreDecimals)
   tm 4 decimals teruggeven als deze siginificant zijn
   
  **********************************************************************/
 { 
  if (isNaN(parseInt(num))) return "";
 
  var tmpNum = num;
  var tmpNumMOD4 = num; 	// (tmpNum * 10000) % 10;
  var tmpNumMOD3 = num;		//(tmpNum * 1000) % 10;

  tmpNumMOD4 *= Math.pow(10,4); 
  tmpNumMOD4 = (Math.round(Math.abs(tmpNumMOD4))) % 10;
  tmpNumMOD3 *= Math.pow(10,3); 
  tmpNumMOD3 = (Math.round(Math.abs(tmpNumMOD3))) % 10;

  var iSign = num < 0 ? -1 : 1;  // Get sign of number
  
  // Adjust number so only the specified number of numbers after

	decimalNum = 2;
	if (tmpNumMOD4 > 0) 
	{
		decimalNum = 4;
	}
	else if (tmpNumMOD3 > 0) 
	{
		decimalNum = 3;
	}
  
  tmpNum *= Math.pow(10,decimalNum);
  tmpNum = Math.round(Math.abs(tmpNum));
  tmpNum /= Math.pow(10,decimalNum);
  tmpNum *= iSign;     // Readjust for sign
  
  // Create a string object to do our formatting on
  var tmpNumStr = new String(tmpNum);
 
   // See if we need to strip out the leading zero or not.
  if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
   if (num > 0)
    tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
   else
    tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
   
  // See if we need to put in the commas
  if (bolCommas && (num >= 1000 || num <= -1000)) {
   var iStart = tmpNumStr.indexOf(".");
   if (iStart < 0)
    iStart = tmpNumStr.length;
 
   iStart -= 3;
   while (iStart >= 1) {
    tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
    iStart -= 3;
   }  
  }
 
  // See if we need to use parenthesis
  if (bolParens && num < 0)
   tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";
 
  //if current number of decimals is not enough, add zeros
  if (decimalNum > 0) {
  	var i;
	 var idxPoint = tmpNumStr.lastIndexOf(".");
	 //alert(idxPoint);
	 if (idxPoint > 0) {
	 	var decNow = tmpNumStr.length - (idxPoint+1);
		//alert(tmpNumStr.length);
	 	for(i=0; i<decimalNum-decNow; i++) {
   			tmpNumStr = tmpNumStr + "0"; 
		}
 	 } else {
	 	tmpNumStr = tmpNumStr + ".";
	 	for(i=0; i<decimalNum; i++) {
   			tmpNumStr = tmpNumStr + "0"; 
	 	}
 	 }  
  }

  //return tmpNumMOD4 + "/" + tmpNumMOD3 + "/" + tmpNumStr;  // Return our formatted string!
  return tmpNumStr;  // Return our formatted string!
  
 }
 
 
/**********************************************************************
function comma2point
use this function if you have a decimal comma for serversettings in iis
and you need to calculate in javascript

example:
n_contrdisc = parseFloat(comma2punt(document.frmnewproditem.field7.value));

 IN:
  strNumber - the string containing a number with maybe a decimal comma
 
 RETVAL:
  the string containing a number with a decimal point
  
  nb: if the number is formatted with a groupdigit, this is removed first
  nb: als er alleen een punt staat en geen komma, ga er dan van uit dat dit al de decimaal is...
  dit is om invoer via numerieke gedeelte makkelijker te maken
 **********************************************************************/ 
function comma2point(strNumber)
{
    var myString;
    var rExp;
	var result;
	
	
	
	if (strNumber != '') {
		myString = new String(strNumber);
		
		var iPunt = myString.indexOf(".");
		var iComma = myString.indexOf(",");
		//alert ("iPunt=" + iPunt + "\niComma=" + iComma);
		
		if (iComma == -1) {
		// er is geen komma, dus niets doen, dan wordt de punt als decimaal geinterpreteerd
			result = myString;
		} else {
		// er is wel een komma
			
			//er is ook een punt (groepdigit), deze eerst vervangen door niets
			rExp = /\./gi;
    		result = myString.replace(rExp, '');
			//alert('eerst punt eruit: ' + result);
			
			//next replace , with point
			myString = new String(result);
	    	rExp = /,/gi;
	    	result = myString.replace(rExp, '.');
			//alert('nu comma vervangen door punt: ' + result);
		}
	} else {
		result = '';
	}
	return result;
}

/**********************************************************************
function point2comma
use this function if you want to replace the point with a comma

 IN:
  strNumber - the string containing a number with maybe a decimal point
 
 RETVAL:
  the string containing a number with a decimal comma
  
    nb: if the number is formatted with a groupdigit, this is removed first
	
	'1-10-2007 gewijzigd door karen: gaat fout als getal 0 erin gestopt wordt
	'wordt namelijk als == '' gezien dus komt lege string terug ipv '0'
	'dus je moet ervoor zorgen dat als een getalparameter er in gaat, je hier een formatstring
	'overheen gooit.
 **********************************************************************/ 
function point2comma(strNumber)
{
    var myString;
    var rExp;
	var result;
	
	if (strNumber != '') {
    	myString = new String(strNumber);
		//first replace comma with nothing
		rExp = /,/gi;
    	result = myString.replace(rExp, '');
		//next replace point with comma
		myString = new String(result);
    	rExp = /\./gi;
    	result = myString.replace(rExp, ',');
	} else {
		result = '';
	}
	return result;
}

/**********************************************************************
function switchpointcomma
use this function if you want to switch the point and comma in a string
e.g. if you have a decimal comma for serversettings in iis
and you use the format function above to return a string with a formatted number
and you want to display 1.345,67 instead of 1,345.67

 IN:
  strNumber - the string containing a number with maybe a decimal point and a comma as thousand separator
 
 RETVAL:
  the string containing a number with maybe a decimal comma and a point as thousand separator
 **********************************************************************/ 
function switchpointcomma(strNumber)
{
    var myString1, myString2, myString3;
    var rExp1, rExp2, rExp3;
	var result;
	
	//alert(strNumber);

    myString1 = new String(strNumber);
    rExp1 = /\./gi;
    result = myString1.replace(rExp1, '*');
	
	myString2 = new String(result);
	rExp2 = /,/gi;
    result = myString2.replace(rExp2, '.');
	
	myString3 = new String(result);
	rExp3 = /\*/gi;
	result = myString3.replace(rExp3, ',');
	//alert(result);
	return result;
}


/**********************************************************************
function trimString
use this function if you want to trim spaces from the beginning and ending of the string

 IN:
  strText - the string containing a number with maybe a decimal point and a comma as thousand separator
 
 RETVAL:
  the string without preceding or trailing spaces
 **********************************************************************/ 
function trimString(strText)
{
    var myString1, myString2, myString3;
    var rExp1, rExp2, rExp3;
	var result;
	
	//alert('['+ strText + ']');

    myString1 = new String(strText);
    rExp1 = /^\s*/gi;
    result = myString1.replace(rExp1, '');
	
	myString2 = new String(result);
	rExp2 = /\s*$/gi;
    result = myString2.replace(rExp2, '');

	//alert('['+ result + ']');
	return result;
}

/**********************************************************************
function encodePlus
javascript escape functie doet plus niet escapen, hier handmatig door %2B vervangen
 **********************************************************************/ 
function encodePlus(strTekst)
{
    var myString;
    var rExp;
	var result;
	
	
	
	if (strTekst != '') {
		myString = new String(strTekst);
		
		var iPlus = myString.indexOf("+");
		//alert ("iPlus=" + iPlus);
		
		if (iPlus == -1) {
		// er is geen plus, dus niets doen
			result = myString;
		} else {
		// er is wel een plus
			
			//replace + door %2B
			rExp = /\+/gi;
	    	result = myString.replace(rExp, '%2B');
			
		}
	} else {
		result = '';
	}
	return result;
}


