/*function : format_number()  
version: 1.0.0  
This function formats a numeric value passed in to it with specified number of  
decimal values. numeric value will not be rounded.  
pnumber : numeric value to be formatted.  
decimals : number of decimal points desired.  

Author: Buddhike de Silva  
Date: 21-Nov-2002 11:16 AM*/  

/*  
revision: 1.1.0  
Author: M. Cassim Farook  
Date: 21-Nov-2002 10:16 PM  
Notes: No offense buddhike...but i had to rewrite the code  
works for ADT (any dam thing)  
usage: x = format_number(123.999, 2)  
*/  

/*  
revision: 1.2.0  
Authors: Buddhike de Silva  
Date: 22-Nov-2002 12:07 PM  
Notes: Optimized for best performence.  
usage: x = format_number(123.999, 2)  
*/  

/* 
 * Revision: 1.3 
 * Author: Mike Robb (JS-X.com) 
 * Date: May 26, 2003 
 * Notes:  Changed to deal with negative numbers. 
 *         Fixed length of final answer. 
 *         Work-around for javascript internal math problem with rounding negative numbers. 
 */ 

/*
 * Revision 1.4
 * Author: LeAnn Roberts
 * Date: September, 2003
 * Note: Modified the if logic: Math.pow()
 */

/*
 * Revision 1.5
 * Author: Robert Heggdal
 * Date: February, 2004
 * Note: Modified check for negative number by replacing parseInt with parseFloat so that negative numbers between zero and minus one are recognized as such.
 */ 

/*
 * Revision 1.6
 * Author: Naveen
 * Date: February, 2004
 * Note: Rewrote format_number to correct a logic problem.
 */

/*
 * Revision 1.7
 * Author: JS-X.com
 * Date: February, 2004
 * Note: Added wrapper around format_number as negative values were dropped from
 *       the logic.
 */


function format_number(p,d) 
{
  var r;
  if(p<0){p=-p;r=format_number2(p,d);r="-"+r;}
  else   {r=format_number2(p,d);}
  return r;
}
function format_number2(pnumber,decimals) 
{
  var strNumber = new String(pnumber);
  var arrParts = strNumber.split('.');
  var intWholePart = parseInt(arrParts[0],10);
  var strResult = '';
  if (isNaN(intWholePart))
    intWholePart = '0';
  if(arrParts.length > 1)
  {
    var decDecimalPart = new String(arrParts[1]);
    var i = 0;
    var intZeroCount = 0;
     while ( i < String(arrParts[1]).length )
     {
       if( parseInt(String(arrParts[1]).charAt(i),10) == 0 )
       {
         intZeroCount += 1;
         i += 1;
       }
       else
         break;
    }
    decDecimalPart = parseInt(decDecimalPart,10)/Math.pow(10,parseInt(decDecimalPart.length-decimals-1)); 
    Math.round(decDecimalPart); 
    decDecimalPart = parseInt(decDecimalPart)/10; 
    decDecimalPart = Math.round(decDecimalPart); 

    //If the number was rounded up from 9 to 10, and it was for 1 'decimal' 
    //then we need to add 1 to the 'intWholePart' and set the decDecimalPart to 0. 

    if(decDecimalPart==Math.pow(10, parseInt(decimals)))
    { 
      intWholePart+=1; 
      decDecimalPart="0"; 
    } 
    var stringOfZeros = new String('');
    i=0;
    if( decDecimalPart > 0 )
    {
      while( i < intZeroCount)
      {
        stringOfZeros += '0';
        i += 1;
      }
    }
    decDecimalPart = String(intWholePart) + "." + stringOfZeros + String(decDecimalPart); 
    var dot = decDecimalPart.indexOf('.');
    if(dot == -1)
    {
      decDecimalPart += '.'; 
      dot = decDecimalPart.indexOf('.'); 
    } 
    var l=parseInt(dot)+parseInt(decimals); 
    while(decDecimalPart.length <= l) 
    {
      decDecimalPart += '0'; 
    }
    strResult = decDecimalPart;
  }
  else
  {
    var dot; 
    var decDecimalPart = new String(intWholePart); 

    decDecimalPart += '.'; 
    dot = decDecimalPart.indexOf('.'); 
    var l=parseInt(dot)+parseInt(decimals); 
    while(decDecimalPart.length <= l) 
    {
      decDecimalPart += '0'; 
    }
    strResult = decDecimalPart;

  }
  return strResult;
}


function number_format(number, decimals, point, separator)
		{
			if(!isNaN(number))
			{
				point = point ? point : '.';
				number = number.toString().split('.');
				if(separator)
				{
					var tmp_number = new Array();
					for(var i = number[0].length, j = 0; i > 0; i -= 3)
					{
						var pos = i > 0 ? i - 3 : i;
						tmp_number[j++] = number[0].substring(i, pos);
					}
					number[0] = tmp_number.reverse().join(separator);
				}
				if(decimals && number[1]) 
					number[1] = Math.round(parseFloat(number[1].substr(0, decimals) + '.' + number[1].substr(decimals, number[1].length), 10));
				return(number.join(point));
			}
			else return(null);
		}
		
function emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	//alert("Email адресата е грешна (проверете дали има @, .)")
	//return false
	return "1"
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    //alert("Корисничкото име во адресата е грешно.")
    //return false
	return "2"
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        //alert("Дестинациската IP адреса е грешна!")
			//return false
			return "3"
	    }
    }
    //return true
	return "0"
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	//alert("Името на доменот е грешно.")
    //return false
	return "4"
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   //alert("Адресата мора да завршува со 3 букви домен или 2 букви од земјата.")
   //return false
   return "5"
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="Адресата нема домен!"
   //alert(errStr)
   //return false
   return "6"
}

// If we've gotten this far, everything's valid!
//return true;
return "0"
}
//  End -->
		