/************************************************************************
 globalUtilities.js
 jpl 2/01/08
 
	Suite of global JavaScript functions specific to the Coach Accountable
	system.
	
	void			cconfirm(message)
	void			calert(message)
	void			jumpToScollerSection(theScroller, theSection)
	void			swapSections(oldSection, newSection)
	boolean			isValidUsername(username)
	boolean			isValidPassword(password)
	void			submitLogin()
	
************************************************************************/
	
	CAData = {};
	
	function cconfirm(message) {
		// regular confirmation popups, enhanced!  ...later.
		return confirm(message);
	}
	
	function calert(message) {
		// regular alert popups, enhanced!  ...later.
		alert(message);
	}
	
	
	function jumpToScollerSection(theScroller, theSection) {
		
		// how tall should our container be to show this section?
		var newHeight = theSection.getSize()['size']['y'];
		
		// resize...then scroll to!
		theScroller.element.effect('height').start(newHeight);
		
		function myLateScroll() {
			theScroller.toElement(theSection);
		}
		myLateScroll.delay(500, this);
	}
	
	function swapSections(oldSection, newSection) {
		
		oldSection = $(oldSection);
		newSection = $(newSection);
		
		oldSection.effect('opacity').start(1,0).chain(function() {
			newSection.style.visibility = 'hidden';
			oldSection.style.display = 'none';
			newSection.style.display = '';
			newSection.effect('opacity').start(0, 1);
		});
	}
	
	
	// boolean isValidUsername(username)
	//	Returns true if username conforms to all rules of valid usernames.
	//	If false, sets value of CAData.errorMessage and CAData.errorReason
	//	for processing later.
	function isValidUsername(username) {
		
		if (!username  ||  username == null) {
			CAData.errorMessage = 'Please enter a username.';
			CAData.errorReason = 'usernames cannot be blank';
			return false;
		}
		
		username = trim(username);
		
		if (username.length < 2) {
			CAData.errorMessage = 
				'Please enter a longer username (2 characters minimum).';
			CAData.errorReason = 'usernames must be at least 2 characters long';
			return false;
		}
		
		if (username.indexOf(' ') >= 0) {
			CAData.errorMessage = 'Please enter a username without spaces.';
			CAData.errorReason = 'username cannot contain spaces';
			return false;
		}
		
	//	if (!/^\w{2, 50}$/.test(username)) {
	//		CAData.errorMessage = 'Please enter a username with only alphanumeric characters';
	//		CAData.errorReason = 'username cannot contain non-alphanumeric characters';
	//		return false;
	//	}
		return true;
	}
	
	
	// boolean isValidPassword(password)
	//	Returns true if password conforms to all rules of valid password.
	//	If false, sets value of CAData.errorMessage and CAData.errorReason
	//	for processing later.
	function isValidPassword(password) {
		
		if (!password  ||  password == null  || password == '') {
			CAData.errorMessage = 'Please enter a password.';
			CAData.errorReason = 'passwords cannot be blank';
			return false;
		}
		
		if (password.length < 6) {
			CAData.errorMessage = 
				'Please enter a longer password (6 characters minimum).';
			CAData.errorReason = 'passwords must be at least 6 characters long';
			return false;
		}
		
		return true;
	}
	
	