function roundNumber(n,dp) {

	var s = new String(n);
	var length = s.length;
	var wholeNumber = new String();
	var decimals = new String();
	var maxDecimalNumber = Math.pow(10,dp)-1; // this is the maximum value of the decimal portion - eg if dp is 2 then this will be 99
	
	var pointPos = -1;
	for (i=0;i<length;i++) {
		if (s.charAt(i) == ".") {
			pointPos = i;
			break;
		} 
		wholeNumber += s.charAt(i);		
	}
	wholeNumberInt = parseInt(wholeNumber);
	
	if (pointPos != -1) {
		for(i=pointPos+1;i<pointPos+dp+1;i++) {
			decimals += s.charAt(i);
		}
	}
	decimalsInt = parseInt(decimals);
	
	var startPos = pointPos+dp+1;
	var fullNumberInt = 0; // this is the whole decimal number ie. if n = 12.3567 fullNumberInt will be 3567
	var midNumberInt = 0; // if fullNumberInt is bigger than this then round up ie. if n=12.3567 and dp=2 then this will be 3550
	
	if (startPos < length) {
		midNumber = decimals+"5";
		for (i=startPos+1;i<length;i++) {
			midNumber += "0";
		}
		midNumberInt = parseInt(midNumber);
		fullNumber = s.substr(pointPos+1,length-pointPos-1);
		fullNumberInt = parseInt(fullNumber);
		
		// ie if n = 9.9969 and dp = 2 : round up if 9969 >= 9950
		if (fullNumberInt >= midNumberInt) {
			decimalsInt++;
			if (decimalsInt > maxDecimalNumber) {
				wholeNumberInt++;
				decimalsInt=0;
			}
		}
	}
	
	// pad with zeros
	decimals = new String(decimalsInt);
	if (decimals.length < dp) {
		extraZeros = dp - decimals.length; 
		for(i=0;i<extraZeros;i++) {
			decimals += "0";
		}
	}
	numberStr = wholeNumberInt;
	if (dp > 0) {
		numberStr += "." +decimals;
	}
	
//	alert(n+":"+numberStr+":max-"+maxDecimalNumber+":dec-"+decimalsInt+":full-"+fullNumberInt+":mid-"+midNumberInt);

	return(numberStr);
}

function validateEmail(addr,man,db) {

	if (addr == '' && man) {
	   if (db) alert('email address is mandatory');
	   return false;
	}
	if (addr == '') return true;
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
		  if (db) alert('email address contains invalid characters');
		  return false;
	   }
	}
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
		  if (db) alert("email address contains non ascii characters.");
		  return false;
	   }
	}

	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   if (db) alert('email address must contain an @');
	   return false;
	}
	if (atPos == 0) {
	   if (db) alert('email address must not start with @');
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   if (db) alert('email address must contain only one @');
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	   if (db) alert('email address must contain a period in the domain name');
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	   if (db) alert('period must not immediately follow @ in email address');
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
	   if (db) alert('period must not immediately precede @ in email address');
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
	   if (db) alert('two periods must not be adjacent in email address');
	   return false;
	}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
	   if (db) alert('invalid primary domain in email address');
	   return false;
	}
	return true;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}