/*
 * rcbs.js
 *
 * Assumptions:
 *	jQuery
 */

// -----------------------------------------------------------------------------

function rcbsCentury() {
	var thisYear = new Date().getFullYear();
	var remainder = thisYear % 100;
	return thisYear - remainder; // 2011 => 2000
}

// -----------------------------------------------------------------------------

function rcbsShowMenu(menuNum) {
    var showId = '#menu' + menuNum;
    var hideId = '#menu' + Math.abs(menuNum - 1);
    //alert(showId + " " + hideId);
    jQuery(showId).show();
    jQuery(hideId).hide();
  }

// -----------------------------------------------------------------------------

function rcbsStringToInt(s) {
	s = jQuery.trim(s);	
	if (! s.match(/^\d+$/)) throw NaN;
	var i = parseInt(s);
	return i;
}

// -----------------------------------------------------------------------------

function rcbsToday() {
	var d = new Date();
	d.setHours(0);
	d.setMinutes(0);
	d.setSeconds(0);
	d.setMilliseconds(0);
	return d;
}	

// -----------------------------------------------------------------------------

function rcbsValidTime(hh_in, mm_in) {
	try {
		var hh = rcbsStringToInt(hh_in); 
		if (hh > 12) return false; // No military time
		var mm = rcbsStringToInt(mm_in);
		var dt = new Date(2000, 1, 1, hh, mm);
		// Make sure the JavaScript Date object didn't "help" us.
		// See comment in rcbsValidDate(). 
		if (dt.getHours() != hh) return false;
		if (dt.getMinutes() != mm) return false;
		return true;
	}
	catch (ex) {
		return false;
	}
}                
    
// -----------------------------------------------------------------------------   
 
// returns an array: [boolean valid, date value if valid]               
function rcbsValidDate(mm_in, dd_in, yy_in) {
	try {
		var mm = rcbsStringToInt(mm_in) - 1; // 0 - 11
		var dd = rcbsStringToInt(dd_in);
		var yyyy = rcbsStringToInt(yy_in);
		if (yyyy < 100) {
			yyyy += rcbsCentury(); // 2-digit year => 4-digit year.
		}
		var dt = new Date(yyyy, mm, dd);
		/*
		 * Make sure the JavaScript Date object didn't "help" us.
		 * The Date constructor interprets out-of-range numbers.
		 * For example, it will convert August 32 to September 1.
		 * Check for this by making sure the numbers we finish with
		 * are the same as the numbers we started with.
		 */
		if (dt.getMonth() != mm) return [false, null];
		if (dt.getDate() != dd) return [false, null];
		if (dt.getFullYear() != yyyy) return [false, null];
		return [true, dt];
	}
	catch (ex) {
		return [false, null];
	}
}   

// -----------------------------------------------------------------------------          
