/**
 *	Waits until conditionFunction returns true, then runs callbackFunction			 
 *
 *	@param	Function that returns whether the condition has been met or not
 *	@param	Function that is called upon meeting condition
 *	@param	(Optional) timeout interval, default 500			 
 *	@return	null upon success, error message otherwise
 */			 			 						
function async_loop (conditionFunction, callbackFunction, timeout){
	//	Checks to see if variables are expected types			
	if (typeof(conditionFunction) != 'function')
		return 'Condition is not a function';
	else if (typeof(callbackFunction) != 'function')
		return 'Callback is not a function';
	else if (typeof(timeout) == 'undefined')
		timeout = 500; 
	else if (typeof(timeout) != 'number')
		return 'Timeout is not an integer';

	//	Initializes functions												
	try{	
		var tempFinal = function(){
			if (conditionFunction())
				callbackFunction();
			else
				setTimeout(tempFinal,timeout);																	
		};
	}catch(err){
		return 'Could not initialize functions: ' + err;
	}

	//	Runs initialized function				
	try{
		tempFinal();
	}catch(err){
		return 'Could not run function: ' + err;
	}

	return null;
}


/**
 *	Runs repeatedly while conditionFunction returns true, then runs callbackFunction			 
 *
 *	@param	Function that returns whether the condition has been met or not
 *	@param	Function that is called upon meeting condition
 *	@param	(Optional) timeout interval, default 500			 
 *	@return	null upon success, error message otherwise
 */			 			 						
function async_while (conditionFunction, callbackFunction, timeout){
	//	Checks to see if variables are expected types			
	if (typeof(conditionFunction) != 'function')
		return 'Condition is not a function';
	else if (typeof(callbackFunction) != 'function')
		return 'Callback is not a function';
	else if (typeof(timeout) == 'undefined')
		timeout = 500; 
	else if (typeof(timeout) != 'number')
		return 'Timeout is not an integer';

	//	Initializes functions												
	try{	
		var tempFinal = function(){
			if (conditionFunction()){
				callbackFunction();			
				setTimeout(tempFinal, timeout);
			}																	
		};
	}catch(err){
		return 'Could not initialize functions: ' + err;
	}

	//	Runs initialized function				
	try{
		tempFinal();
	}catch(err){
		return 'Could not run function: ' + err;
	}

	return null;
}
