
/************************************************************************
 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)
	boolean			isValidUsername(username)
	boolean			isValidPassword(password)
	
************************************************************************/
	
	
	
	function cconfirm(message) {
		// regular confirmation popups, enhanced!  ...later.
		return confirm(message);
	}
	
	function calert(message) {
	//	if(window.CA  &&  CA.confirmModal)
	//		CA.confirmModal.alert(message);
	//	else
			alert(message);
	}
	
	
	function prepFileInputStyling(theInput, width, isOneLine) {
	//	if(Browser.Engine.trident) {
	//		$(theInput).setStyle('width', width + 'px');
	//		return;
	//	}
		dbug.nog(theInput);
		theInput = $(theInput);
		if(!theInput) return;
		
		dbug.nog(theInput);
		dbug.nog(theInput.parentNode);
		
		var fakeFileUpload = new Element('div');
		fakeFileUpload.className = 'fakeFile verticalMiddle';
		var newInput = (new Element('input')).addClass('verticalMiddle');
		newInput.type = 'text';
		if(properInt(width, 0) > 0) {
			newInput.setStyle('width', width);
			fakeFileUpload.setStyle('width', (width + 80) + 'px');
		}
		
		fakeFileUpload.appendChild(newInput);
		var image = (new Element('img')).addClass('verticalMiddle');
		image.src='images/buttons/buttonBrowse.gif';
		fakeFileUpload.appendChild(image);
		
		var parentDiv = $(theInput.parentNode);
		theInput.className = 'stealthFile verticalMiddle';
		parentDiv.appendChild(fakeFileUpload);
		parentDiv.addClass('verticalMiddle');
		theInput.onchange = theInput.onmouseout = function () {
			newInput.value = this.value;
		}
	}
	
	function getHalfHourOptions() {
		var theTime = Date.parse('1/1/2000 12:00 am');
		var result = [];
		for (var i=0; i <48; i++) {
			result.push(theTime.format('%i:%N %p'));
			theTime.increment('minute', 30);
		}
		return result;
	}
	
	
	function showAllTimeZones(theMoreLink) {
		theMoreLink = $(theMoreLink);
		var select1 = theMoreLink.getPrevious().getPrevious();
		var select2 = theMoreLink.getPrevious();
		
		// Do the ol' switcheroo:
		select2.name = select1.name;
		select1.name = select1.name + '_alt';
		select2.id = select1.id;
		select1.id = select1.id + '_alt';
		select2.value = select1.value;
		theMoreLink.setStyle('display', 'none');
		select1.setStyle('display', 'none');
		select2.setStyle('display', 'block');
	}
	
	
	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);
	}
	
	
	
	// boolean isValidUsername(username)
	//	Returns true if username conforms to all rules of valid usernames.
	//	If false, sets value of CA.errorMessage and CA.errorReason
	//	for processing later.
	function isValidUsername(username) {
		
		if (!username  ||  username == null) {
			CA.errorMessage = 'Please enter a username.';
			CA.errorReason = 'usernames cannot be blank';
			return false;
		}
		
		if (username.length < 2) {
			CA.errorMessage = 
				'Please enter a longer username (2 characters minimum).';
			CA.errorReason = 'usernames must be at least 2 characters long';
			return false;
		}
		
		if (username.indexOf(' ') >= 0) {
			CA.errorMessage = 'Please enter a username without spaces.';
			CA.errorReason = 'username cannot contain spaces';
			return false;
		}
		
		if (!username.test(/^\w{2,50}$/)) {
			CA.errorMessage = 'Please enter a username with only alphanumeric characters';
			CA.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 CA.errorMessage and CA.errorReason
	//	for processing later.
	function isValidPassword(password) {
		
		if (!password  ||  password == null  || password == '') {
			CA.errorMessage = 'Please enter a password.';
			CA.errorReason = 'passwords cannot be blank';
			return false;
		}
		
		if (password.length < 6) {
			CA.errorMessage = 
				'Please enter a longer password (6 characters minimum).';
			CA.errorReason = 'passwords must be at least 6 characters long';
			return false;
		}
		
		return true;
	}
	
	
	function toggleDisplay(theElement) {
		theElement.style.display = (theElement.style.display == 'none') ? '' : 'none';
	}
	
	
	function toggleModuleBody(theArrow) {
		var theElement = theArrow.parentNode.getNext().getNext()
		toggleDisplay(theElement);
		
		if(theElement.style.display == 'none')
			theArrow.src = theArrow.src.replace(/Down/, 'Left');
		else
			theArrow.src = theArrow.src.replace(/Left/, 'Down');
	}
	
	
	
	
	function validateImageFileUpload(theForm) {
		if(theForm.theFile.value == '') {
			alert('Please choose a file to upload.');
			return false;
		}
		return true;
	}
	
	
	
	
