/**
 *	Created with MAX's HTML Beauty++ 2004
 *	validate.js
 *	@author Amani Ahmed
 *	@date July 8th 2004
*/

/**
 * Checks if the given argument is a number
 *
 * @param  num  the object to be validated
 * @return      a boolean value indicating if num is a number
*/
function isNotaNumber(num){
	if(!isNaN(num)){
		alert('Please do not enter only numbers in this space');
		return false;
	}else return true;
}

/**
 * Checks if the given argument is a number
 *
 * @param  num  the object to be validated
 * @return      a boolean value indicating if num is a number
*/
function isANumber(num){
	if(isNaN(num)){
		alert('Please only enter a number in this space');
		return false;
	}else return true;
}


/**
 * Checks if the given argument is a digit
 *
 * @param  num  the object to be validated
 * @return      a boolean value indicating if num is a digit
*/
function isDigit(num) {
	if (num.length>1){return false;}
		var string="0123456789";
	if (string.indexOf(num)!=-1){return true;}
		return false;
}

/**
 * Checks if the given argument is a natural number
 *
 * @param  num  the object to be validated
 * @return      a boolean value indicating if num is a natural number
*/
function isNaturalNumber(num){
	for(var i=0;i<num.length;i++){
		if(!isDigit(num.charAt(i))){
			alert('Please enter a natural number in this space');			
			return false;
		}
	}
	return true;
}

/**
 * Checks if the given argument is an integer
 *
 * @param  num  the object to be validated
 * @return      a boolean value indicating if num is an integer
*/
function isInteger(num){
	for(var i=0;i<num.length;i++){
		if(!isDigit(Math.abs(num.charAt(i)))){
			alert('Please enter an integer in this space');
			return false;
		}
	}
	return true;
}

/**
 * Checks if the first argument is within the given range
 *
 * @param  num  the object to be validated
 * 		   low  the minimum of the range
 *		   high the maximum of the range
 * @return      a boolean value indicating if num is in the given range
*/
function isNumberInRange(num, low, high){
	if((low <= num) && (num <= high))
		return true;
	else return false;
}

/**
 * Checks if the first argument is within within the error range 
 * given of the correct answer (second argument)
 *
 * @param  	user	the value provided for comparasion
 * 		   	correct the correct value
 *		   	error		the error which the user answer has to be in	
 * @return      	a boolean value indicating user is within the percentage range
*/
function relativeError(user, correct, error) {
	if (Math.abs((user - correct)/ correct) <= error)
		return true;
	else return false;
}

/**
 * Checks if the first argument is within within the error range 
 * given of the correct answer (second argument)
 *
 * @param  	user	the value provided for comparasion
 * 		   	correct the correct value
 *		   	diff	the diff which the user answer has to be in	
 * @return      	a boolean value indicating user is within the percentage range
*/
function absError(user, correct, diff) {
	if (Math.abs(user - correct) <= diff)
		return true;
	else return false;
}

/**	
 *  Rounds a number to the given significant digits
 *
 * @param num			double/integer to be rounded
 *		  sig			integer defining the number of significant digits to keep
 * @return 				the number to the given significant digits
*/
function round_sig(num, sig){
    var factor = Math.pow(10,sig-Math.ceil(Math.log(Math.abs(num))/Math.LN10));
    return Math.round(num*factor)/factor;
}

/**	
 *  Formats a number to the given significant digits
 *
 * @param num			double/integer to be rounded
 *		  sig			integer defining the number of significant digits to keep
 * @return 				the number to the given significant digits
*/
function format_sig(num, sig){
    var str=round_sig(parseFloat(num),sig)+'';
	var nDig=number_sig(str);
	while (nDig<sig) {
		if (nDig==str.length) {
			str+=".";
		}
		nDig++;
		str+='0';
	}	
	return str;
}

/**	
 *  Counts the number of significant digits
 *
 * @param num			double/integer to know its number of significant figures
 * @return 				the number of significant digits
*/
function number_sig(num) {
	var str=num+'';
    var nDig=0;
    var isSignificant=false;
	for(var i=0;i<str.length;i++) {
		if (str.substring(i,i+1)!="0" && str.substring(i,i+1)!=".") isSignificant=true;
		if (isNaN(str.substring(i,i+1)) && str.substring(i,i+1)!=".") break;
		if (isSignificant) nDig+=(isDigit(str.substring(i,i+1)))?1:0;
	}
	return parseInt(nDig);		
}

