////$Id: functions.js,v 1.4 2005/11/28 21:36:23 ge_allan Exp $////

/*
This file contain Javascript functions related to date and time
*/

function time_add(time_obj, adjust_by, interval) {
	/*
	NOTES:
	- add or subtract a specified period from a date
	- this is better than just multiplying the timestamp because this takes daylight savings time into consideration
	INPUT:
	- time_obj : Javascript time object
	- adjust_by : the number, positive to add or negative to subtract, you want to adjust the time with
	- interval : the unit for the adjust_by number
	OUTPUT:
	- Javascript time object
	*/
	var newtime, timeadd_msecs, currtime_msecs;
	switch (interval) {
	case 'hour': case 'hours': case 'hr': case 'hrs':
		timeadd_msecs = adjust_by * 60 * 60 * 1000;
		break;
	case 'minute': case 'minutes': case 'min': case 'mins':
		timeadd_msecs = adjust_by * 60 * 1000;
		break;
	case 'second': case 'seconds': case 'sec': case 'secs':
		timeadd_msecs = adjust_by * 1000;
		break;
	case 'day': case 'days':
		timeadd_msecs = adjust_by * 24 * 60 * 60 * 1000;
		break;
	case 'month': case 'months':
		timeadd_msecs = adjust_by * 30 * 24 * 60 * 60 * 1000;
		break;
	case 'year': case 'years': case 'yr': case 'yrs':
		timeadd_msecs = adjust_by * 365 * 24 * 60 * 60 * 1000;
		break;
	default:
		return false;
		//system_error('Configuration error. Interval not defined.', array('Function' => 'time_add()', 'Interval' => $interval));
	}
	currtime_msecs = time_obj.getTime();
	newtime = new Date(currtime_msecs + timeadd_msecs);
	return newtime;
}

function time_difference(time1, time2) {
	/*
	NOTES:
	- calculate difference between two Javascript time objects
	- developer notes QUEST: had big trouble with "Function expected" when function was called time_diff for some STRANGE reason!! This name was not defined in function or variable names other places!!
	OUTPUT:
	- milliseconds
	*/
	var diff;
	diff = time2.getTime() - time1.getTime();
	return diff;
}

function time_now_difference(time_obj) {
	/*
	NOTES:
	- calculate difference between now and a specified Javascript time object
	INPUT:
	- time_obj : a Javascript time object
	OUTPUT:
	- time difference in milliseconds relative to now
	*/
	var diff;
	now_time = new Date();
	diff = new_time.getTime() - time_obj.getTime();
	return diff;
}

function time_diff_server_client(server_time) {
	/*
	NOTES:
	- difference between web server time and the client/browser/local computer time
	- this must function must be run AS SOON AS POSSIBLE after getting timestamp from the server
	INPUT:
	- curr_server_time : current UNIX timestamp from server (seconds after 1970)
	OUTPUT:
	- time difference in milliseconds relative to server/correct time
	- positive: client is ahead of actual time
	- negative: client is behind actual time
	- you can SUBTRACT this difference when doing time calculations on client time for getting correct time results (assuming server has the correct time)
	*/
	var diff;
	client_time = new Date();
	server_time = new Date(server_time * 1000);
	diff = client_time.getTime() - server_time.getTime();
	window.curr_client_time_offset = diff; //set global variable
	return diff;
}

function get_time() {
	if (window.curr_client_time_offset) {  //check for global variable containing the client time offset from server time
		return new Date( (new Date()).getTime() - window.curr_client_time_offset);
	} else {
		return new Date();
	}
}

function convert_from_milliseconds(milliseconds, interval) {
	var diff_target;
	switch (interval) {
	case 'week': case 'weeks': case 'wk': case 'wks':
		diff_target = milliseconds / 1000 / 60 / 60 / 24 / 7;
		break;
	case 'day': case 'days':
		diff_target = milliseconds / 1000 / 60 / 60 / 24;
		break;
	case 'hour': case 'hours': case 'hr': case 'hrs':
		diff_target = milliseconds / 1000 / 60 / 60;
		break;
	case 'minute': case 'minutes': case 'min': case 'mins':
		diff_target = milliseconds / 1000 / 60;
		break;
	case 'second': case 'seconds': case 'sec': case 'secs':
		diff_target = milliseconds / 1000;
		break;
	default:
		return false;
		//system_error('Interval has not been defined.', array('Interval' => $interval) );
	}
	return diff_target;
}

function convert_to_milliseconds(time_period, source_interval) {
	var diff_target;
	switch (source_interval) {
	case 'week': case 'weeks': case 'wk': case 'wks':
		diff_target = time_period * 1000 * 60 * 60 * 24 * 7;
		break;
	case 'day': case 'days':
		diff_target = time_period * 1000 * 60 * 60 * 24;
		break;
	case 'hour': case 'hours': case 'hr': case 'hrs':
		diff_target = time_period * 1000 * 60 * 60;
		break;
	case 'minute': case 'minutes': case 'min': case 'mins':
		diff_target = time_period * 1000 * 60;
		break;
	case 'second': case 'seconds': case 'sec': case 'secs':
		diff_target = time_period * 1000;
		break;
	default:
		return false;
		//system_error('Interval has not been defined.', array('Interval' => $interval) );
	}
	return diff_target;
}