/**
 *	Created with MAX's HTML Beauty++ 2004
 *	validate.js
 *	@author Amani Ahmed
 *	@date July 8th 2004
*/

//--------FORMAT VALIDATORS--------------
/**
 * 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){
	if (isNaN(num)) return false;
	num=Math.abs(num);
	for(var i=0;i<num.length;i++){
		if(!isDigit(num.charAt(i))){
			return false;
		}
	}
	return true;
}

//------------FORM ELEMENT VALIDATORS---------------
/**
* Alerts if the value is not an integer
*/
function alertIfNotInteger(object){
	if (object==null || object.value==null || object.value=="")
		return true;
	if(!isInteger(object.value)){
		alert('Please enter an integer in this space');
		return false;
	}
	return true;
}

/**
* Alerts if the value is not a number
*/
function alertIfNotNumber(object){
	if (object==null || object.value==null || object.value=="")
		return true;
	if(isNaN(object.value)){
		alert('Please enter a number in this space');
		return false;
	}
	return true;
}

//------------ASSESSMENT VALIDATORS--------------
/**
 * 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 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;
}

//--------SIGNIFICANT FIGURES FUNCTIONS--------------
/**	
 *  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 strAbs=round_sig(Math.abs(parseFloat(num)),sig)+'';
	var strSign=""+((parseFloat(num)<0)?"-":"");
	var str=round_sig(parseFloat(num),sig)+'';
	var nDig=number_sig(str);
	while (nDig<sig) {
		if (nDig==strAbs.length) {
			strAbs+=".";
		}
		nDig++;
		strAbs+='0';
	}	
	return strSign+strAbs;
}

/**	
 *  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) {
	if(isNaN(num)) return 0;
	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)!="." 
				&& str.substring(i,i+1)!="-" && str.substring(i,i+1)!="+") isSignificant=true;
		if (isNaN(str.substring(i,i+1)) && str.substring(i,i+1)!="."
				&& 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);		
}
