
// "num_in" is in cents.
function fnNumToDollarStr(num_in)
{
	var num_in2= Math.round(num_in);
	var n_cents = num_in2 % 100;
	var str_cents = fnNumToDigitStr(n_cents,2);
	var n_dollars = Math.floor(num_in2/100);
	var str_out = "$" +n_dollars +"." +str_cents;
	return str_out;
}

function fnNumToDigitStr(in_num, num_digits)
{
	if (num_digits>1000)
		return "error - overflow";
	var str_num = "" + in_num;
	while (str_num.length<num_digits)
		str_num = "0" +str_num;
	return str_num;
}

function fnDoesStrHaveValue(str_in)
{
	if (typeof(str_in)=="undefined")
		return false;
	if (!str_in)
		return false;
	if (""==str_in)
		return false;
	if (str_in=="null")
		return false;
	if (str_in=="Null")
		return false;
	if (str_in=="NULL")
		return false;
	if (str_in==" ")
		return false;
	if (str_in=="  ")
		return false;
	return true;
}

function fnFormatPhone(str_in)
{
	var str_out="";
	if (!fnDoesStrHaveValue(str_in))
		return str_out;
	var	regex_nonnum = /\D/g;
	
	// Split for extension
	var array_x = str_in.split("x");
	var str_ext = "";
	if (array_x.length>1)
		str_ext = " x" +array_x[1].replace(regex_nonnum, ""); 
	str_out = array_x[0];
	
	str_out = str_out.replace(regex_nonnum, ""); 

	var num_digits = str_out.length;
	if (str_out[0]=="1" && num_digits==11)
		str_out = str_out.slice(1,4) +"." +str_out.slice(4,7) +"." +str_out.slice(7,11);
	else if (num_digits>10)
		return str_in;
	else if (num_digits==10)
		str_out = str_out.slice(0,3) +"." +str_out.slice(3,6) +"." +str_out.slice(6,10);
	else if (num_digits==7)
		str_out = str_out.slice(0,3) +"." +str_out.slice(3,7);
	else if (num_digits==7)
		return str_in;
	
	str_out += str_ext;
	return str_out;
}

function fnIsDef(entity)
{
	return (typeof(entity)!="undefined");
}

function fncx1(str_in)
{
	var str_out="";
	var i=0;
	var c_in = '';
	var n=0;
	var the_rand = Math.round(Math.random * 9);
	for (i=0;i<str_in.length; i++)
	{
		str_out+=n;
		c_in=str_in.charAt(i);
		n=parseInt(c_in,10);
		n=n+CX1.charAt(i%CX1.length);
		n=n%10;
		str_out+=n;
	}
	return str_out;
}

// Convert from MM/DD/YYYY or MM/DD to JS date (is tolerant of other separators , . - and of YY)
function fnDisplayDate2JSDate(display_date)
{
	var js_date = new Date();

	if ((display_date == null) || (display_date == ""))
		return null;

	var	regEx = /\s/g;
	display_date = display_date.replace(regEx, "");
	var	array_date = display_date.split("/");
	if (2 > array_date.length)
	{
		array_date = display_date.split("-");
		if (2 > array_date.length)
		{
			array_date = display_date.split(".");
			if (2 > array_date.length)
			{
				array_date = display_date.split(",");
				if (2 > array_date.length)
				{
					return null;
				}
			}
		}
	}
	if (array_date.length>2)
	{
		n_year = parseInt(array_date[2],10);
		if (n_year>=100)
			js_date.setFullYear(n_year);
		else
		{
			n_year += js_date.getFullYear() - (js_date.getFullYear() % 100);
			js_date.setFullYear(n_year);
		}
	}
	var n_month = parseInt(array_date[0],10);
	var n_day = parseInt(array_date[1],10);
	js_date.setMonth(n_month-1,n_day);
	
	return js_date;
}

// Stuff below ported from QT

function nyi()
{
	alert("Sorry.  This feature is not yet implemented.");
}

function fnLogError(the_msg)
{
	if (DEBUG_MODE != 0)
	{
		var str_msg = "" + the_msg;
		alert(str_msg);
	}
}

function fnLogInfo(the_msg)
{
	if (DEBUG_MODE > 1)
	{
		var str_msg = "" + the_msg;
		alert(str_msg);
	}
}

// Compares the day of 2 dates
// returns: true: day is teh same
//			false: day is not the same
function fnSameDates(date_left, date_right)
{
	if (	(date_left.getFullYear() == date_right.getFullYear())
		&& 	(date_left.getMonth() == date_right.getMonth())
		&& 	(date_left.getDate() == date_right.getDate()))
		return true;
	else
		return false;
}

/* 	fnCompareJSDates
	Granularity = day
	returns:
		-1: day of date_left is less than date_right
		 0: day of date_left is equal to date_right
		 1: day of date_left is more than date_right
*/
function fnCompareJSDates(date_left, date_right)
{
	if (date_left.getFullYear() < date_right.getFullYear())
		return -1;
	if (date_left.getFullYear() > date_right.getFullYear())
		return 1;
	if (date_left.getMonth() < date_right.getMonth())
		return -1;
	if (date_left.getMonth() > date_right.getMonth())
		return 1;
	if (date_left.getDate() < date_right.getDate())
		return -1;
	if (date_left.getDate() > date_right.getDate())
		return 1;
	return 0;
}

/* 	fnCompareJSDatetimes
	Granularity = ms
	returns:
		-1: day of date_left is less than date_right
		 0: day of date_left is equal to date_right
		 1: day of date_left is more than date_right
*/
function fnCompareJSDatetimes(date_left, date_right)
{
	if (date_left.getTime() < date_right.getTime())
		return -1;

	if (date_left.getTime() > date_right.getTime())
		return 1;

	return 0;
}


// returns: true: time is the same
//			false: time is not the same
function fnSameTime(time_left, time_right)
{
	if (time_left==time_right)
		return true;

	var array_left = time_left.split(":");
	var array_right = time_right.split(":");
	var done = false;
	var n_left = 0;
	var n_right = 0;
	var i = 0;

	for (i=0; (i<array_left.length) || (i<array_right.length); i++)
	{
		n_left = 0;
		n_right = 0;
		if (array_left.length > i)
			n_left = parseInt(array_left[i],10);
		if (array_right.length > i)
			n_right = parseInt(array_right[i],10);

		if (n_left != n_right)
			return false;
	}
	return true;
}


function fnSQLTS2DisplayDatetime(sql_ts)
{
	if ((sql_ts == null) || (sql_ts == ""))
		return "";
	var	array_datetime = sql_ts.split(" ");
	var str_out = "";

	if (array_datetime.length > 0)
		str_out += fnSQLDate2DisplayDate(array_datetime[0]);
	if (array_datetime.length > 1)
	{
		str_out += " ";
		str_out += DBTimeToString2(array_datetime[1]);
	}
	return str_out;
}

// to July 01, 2007 - 4:01p
function fnSQLTS2DisplayDatetime2(sql_ts)
{
	if ((sql_ts == null) || (sql_ts == ""))
		return "";
	var	array_datetime = sql_ts.split(" ");
	var str_out = "";

	if (array_datetime.length > 0)
		str_out += fnSQLDate2DisplayDate2(array_datetime[0]);
	if (array_datetime.length > 1)
	{
		str_out += "  ";
		str_out += DBTimeToString2(array_datetime[1]);
	}
	return str_out;
}

function fnSQLTS2CompactDatetime(sql_ts)
{
	var str_out = "";
	var	array_date_target = sql_ts.split(" ");
	if ((!array_date_target) || (array_date_target.length<2))
		return str_out;
	var js_date_target = fnSQLDatetime2JSDate(array_date_target[0],array_date_target[1]);
	var js_date_today = new Date();
	var str_temp = "";
	
	if (js_date_target.getFullYear() == js_date_today.getFullYear())
	{
		// If it is this year
		if (	(js_date_target.getMonth() == js_date_today.getMonth())
			&& 	(js_date_target.getDate() == js_date_today.getDate()))
		{
			// It is this month and day, just show time
			str_out = fnJSDate2DisplayTime2(js_date_target);
		}
		else
		{
			// It is a different month or day, just show month and day
			// js date month is 0-indexed
			str_out = fnJSDate2Month(js_date_target);
			str_out += " " + js_date_target.getDate();			
		}
	}
	else
	{
		// If it is a diff year, just show year
		str_out = js_date_target.getFullYear();
	}
	return str_out;
}

// Month and Year on diff year
function fnSQLTS2CompactDatetime2(sql_ts)
{
	var str_out = "";
	var	array_date_target = sql_ts.split(" ");
	if ((!array_date_target) || (array_date_target.length<2))
		return str_out;
	var js_date_target = fnSQLDatetime2JSDate(array_date_target[0],array_date_target[1]);
	var js_date_today = new Date();
	var str_temp = "";
	
	if (js_date_target.getFullYear() == js_date_today.getFullYear())
	{
		// If it is this year
		if (	(js_date_target.getMonth() == js_date_today.getMonth())
			&& 	(js_date_target.getDate() == js_date_today.getDate()))
		{
			// It is this month and day, just show time
			str_out = fnJSDate2DisplayTime2(js_date_target);
		}
		else
		{
			// It is a different month or day, just show month and day
			// js date month is 0-indexed
			str_out = fnJSDate2Month(js_date_target);
			str_out += " " + js_date_target.getDate();			
		}
	}
	else
	{
		// If it is a diff year, just show year
		str_out = fnJSDate2Month(js_date_target) + " " + js_date_target.getFullYear();
	}
	return str_out;
}

function fnJSDate2DisplayDateFull(jsdate)
{
	var	str_out = "";

	if (!jsdate)
		return str_out;

	str_out = fnJSDate2DOTW(jsdate);
	str_out += ", " + fnJSDate2Month(jsdate);
	str_out += " " + jsdate.getDate();
	str_out += ", " + jsdate.getFullYear();

	return str_out;
}

function fnJSDate2DisplayDate2(jsdate)
{
	var	str_out = "";

	if (!jsdate)
		return str_out;

	str_out = fnJSDate2Month(jsdate);
	str_out += " " + jsdate.getDate();
	str_out += ", " + jsdate.getFullYear();

	return str_out;
}

function fnJSDate2DisplayDate3(jsdate)
{
	var	str_out = "";

	if (!jsdate)
		return str_out;

	str_out += (jsdate.getMonth()+1);
	str_out += "/" + jsdate.getDate();
	str_out += "/" + jsdate.getFullYear();

	return str_out;
}

function fnJSDate2SQLDate(js_date)
{
	if (!js_date)
		return null;
	var str_out = js_date.getFullYear();
	str_out += "-" + (js_date.getMonth()+1);
	str_out += "-" + js_date.getDate();
	return str_out;
}

function fnJSDate2DisplayDate_NoYear(jsdate)
{
	var	str_out = "";

	if (!jsdate)
		return str_out;

	str_out = fnJSDate2DOTW(jsdate);
	str_out += ", " + fnJSDate2Month(jsdate);
	str_out += " " + jsdate.getDate();

	return str_out;
}

function fnGetTodayDateString()
{
	var	str_out = "";
	var date1 = new Date();
	var theDay = date1.getDate();
	var	theMonth = date1.getMonth()+1;
	var	theFullYear = date1.getFullYear();

	str_out += theMonth + "/" + theDay + "/" + theFullYear;
	return str_out;
}

function fnGetTodayDateAsSQLDate()
{
	var	str_out = "";
	var date1 = new Date();
	var theDay = date1.getDate();
	var	theMonth = date1.getMonth()+1;
	var	theFullYear = date1.getFullYear();

	str_out += theFullYear + "-" + theMonth + "-" + theDay;
	return str_out;
}

function fnDate2SQLTime_no_sec(in_date)
{
	var	str_out = "";
	var theHour = in_date.getHours();
	var	theMin = in_date.getMinutes();
	var str_hour = "";
	var str_min = "";

	str_hour += theHour;
	str_min += theMin;

	if (theMin<10)
		str_min = "0" + str_min;

	str_out += str_hour + ":" + str_min + ":00";
	return str_out;
}

// Convert from MM/DD/YYYY to YYYY-MM-DD
function fnDisplayDate2SQLDate(display_date)
{
	var sql_date = "";

	if ((display_date == null) || (display_date == ""))
		return DEFAULT_DATE;

	var	regEx = /\s/g;
	display_date = display_date.replace(regEx, "");
	var	array_date = display_date.split("/");

	if (3 > array_date.length)
		return DEFAULT_DATE;

	sql_date = array_date[2] + "-" + array_date[0] + "-" + array_date[1];

	return sql_date;
}


// Convert from YYYY-MM-DD to MM/DD/YYYY
function fnSQLDate2DisplayDate(sql_date)
{
	var display_date = "";

	if ((sql_date == null) || (sql_date == ""))
		return DEFAULT_DATE;

	var	regEx = /\s/g;
	sql_date = sql_date.replace(regEx, "");
	var	array_date = sql_date.split("-");

	if (3 > array_date.length)
		return DEFAULT_DATE;

	display_date = array_date[1] + "/" + array_date[2] + "/" + array_date[0];

	return display_date;
}

function fnParseLiterals1(str_in)
{
	return str_in.replace(/\$/gi,"$0");
}

// Convert from YYYY-MM-DD to dotw, Month Day, YYYY
function fnSQLDate2DisplayDate2(sql_date)
{
	var display_date = "";

	if ((sql_date == null) || (sql_date == ""))
		return DEFAULT_DATE;

	var js_date = fnSQLDate2JSDate(sql_date);
	var dotw = js_date.getDay()+1;
	
	switch (dotw)
	{
		case 1:
			display_date += "Sun - ";
			break;
		case 2:
			display_date += "Mon - ";
			break;
		case 3:
			display_date += "Tue - ";
			break;
		case 4:
			display_date += "Wed - ";
			break;
		case 5:
			display_date += "Thu - ";
			break;
		case 6:
			display_date += "Fri - ";
			break;
		case 7:
			display_date += "Sat - ";
			break;
	}

	var	regEx = /\s/g;
	sql_date = sql_date.replace(regEx, "");
	var	array_date = sql_date.split("-");	// array is [YYYY][MM][DD]

	if (3 > array_date.length)
		return DEFAULT_DATE;

	display_date += fnMonth2Str(parseInt(array_date[1],10));
	display_date += " ";
	display_date += array_date[2];
	display_date += ", ";
	display_date += array_date[0];

	return display_date;
}

function fnDOTW2Str(n_dotw)
{
	var str_dotw = "";
	switch (n_dotw)
	{
		case 1:
			str_dotw = "Sunday";
			break;
		case 2:
			str_dotw = "Monday";
			break;
		case 3:
			str_dotw = "Tuesday";
			break;
		case 4:
			str_dotw = "Wednesday";
			break;
		case 5:
			str_dotw = "Thursday";
			break;
		case 6:
			str_dotw = "Friday";
			break;
		case 7:
			str_dotw = "Saturday";
			break;
	}
	return str_dotw;
}

function fnMonth2Str(n_month)
{
	var str_month="";
	switch (n_month)
	{
		case 1:
			str_month = "Jan";
			break;
		case 2:
			str_month = "Feb";
			break;
		case 3:
			str_month = "Mar";
			break;
		case 4:
			str_month = "Apr";
			break;
		case 5:
			str_month = "May";
			break;
		case 6:
			str_month = "Jun";
			break;
		case 7:
			str_month = "Jul";
			break;
		case 8:
			str_month = "Aug";
			break;
		case 9:
			str_month = "Sep";
			break;
		case 10:
			str_month = "Oct";
			break;
		case 11:
			str_month = "Nov";
			break;
		case 12:
			str_month = "Dec";
			break;
	}
	return str_month;
}

function fnSQLDate2SectionHeaderDate(sql_date)
{
	var display_date = "";
	var str_dotw = "";

	if ((sql_date == null) || (sql_date == ""))
		return DEFAULT_DATE;

	var js_date = fnSQLDate2JSDate(sql_date);
	display_date += "<span>";
	display_date += fnDOTW2Str(js_date.getDay()+1);
	display_date += "</span>";

	var	regEx = /\s/g;
	sql_date = sql_date.replace(regEx, "");
	var	array_date = sql_date.split("-");	// array is [YYYY][MM][DD]

	if (3 > array_date.length)
		return DEFAULT_DATE;

	display_date += "<span class='style_weekly_section_header_date'>";
	display_date += fnMonth2Str(parseInt(array_date[1],10));
	display_date += " ";
	display_date += array_date[2];
	display_date += ", ";
	display_date += array_date[0];
	display_date += "</span>";

	return display_date;
}

function fnSQLDatetime2JSDate(sql_date, sql_time)
{
	var	js_date = fnSQLDate2JSDate(sql_date);
	var str_min = "0";
	if (sql_time)
	{
		str_min = DBTimeToMin(sql_time);
		js_date.setHours(DBTimeTo24Hour2(sql_time));
		js_date.setMinutes(parseInt(str_min, 10));
	}
	return js_date;
}

function fnSQLDatetime2JSDate2(sql_datetime)
{
	var	array_date = sql_datetime.split(" ");
	return fnSQLDate2JSDate(array_date[0]);
}

function fnSQLTime2JSDate(sql_time)
{
	var	js_date = new Date();
	var str_min = DBTimeToMin(sql_time);
	js_date.setHours(DBTimeTo24Hour2(sql_time));
	js_date.setMinutes(parseInt(str_min, 10));
	return js_date;
}

function fnSQLDate2JSDate(sql_date)
{
	var js_date = new Date();
	if ((sql_date == null) || (sql_date == ""))
		return null;
	var	regEx = /\s/g;
	sql_date = sql_date.replace(regEx, "");
	var	array_date = sql_date.split("-");
	if (3 > array_date.length)
		return null;
	var n_year = parseInt(array_date[0],10);
	var n_month = parseInt(array_date[1],10)-1;
	var n_day = parseInt(array_date[2],10);

	js_date.setFullYear(n_year);
	js_date.setMonth(n_month,n_day);

	return js_date;
}


function fnVerboseDisplayDateToSQLDate(str_display_date)
{
	var js_date = new Date();

	if (str_display_date == null)
		return js_date;
	var	date_array = str_display_date.split(" ");
	// if "today" return today's date
	if (date_array.length<3)
		return fnGetTodayDateAsSQLDate();
	var str_month = "" + date_array[1];
	var str_month_out = "";

	if (str_month == "Jan")
		str_month_out = "1";
	else if (str_month == "Feb")
		str_month_out = "2";
	else if (str_month == "Mar")
		str_month_out = "3";
	else if (str_month == "Apr")
		str_month_out = "4";
	else if (str_month == "May")
		str_month_out = "5";
	else if (str_month == "Jun")
		str_month_out = "6";
	else if (str_month == "Jul")
		str_month_out = "7";
	else if (str_month == "Aug")
		str_month_out = "8";
	else if (str_month == "Sep")
		str_month_out = "9";
	else if (str_month == "Oct")
		str_month_out = "10";
	else if (str_month == "Nov")
		str_month_out = "11";
	else if (str_month == "Dec")
		str_month_out = "12";

	var temp_array = date_array[2].split(",");
	var	str_day = "" + temp_array[0];
	var str_year = date_array[3];

	var str_out = str_year + "-" + str_month_out + "-" + str_day;

	return str_out;
}


function fnIsDigit(c)
{
	if ('0'==c)
		return true;
	else if ('1'==c)
		return true;
	else if ('2'==c)
		return true;
	else if ('3'==c)
		return true;
	else if ('4'==c)
		return true;
	else if ('5'==c)
		return true;
	else if ('6'==c)
		return true;
	else if ('7'==c)
		return true;
	else if ('8'==c)
		return true;
	else if ('9'==c)
		return true;

	return false;
}

function fnStrTime2SQLTime(str_time)
{
	var str_out = "";
	var the_hour=0;
	var the_min=0;
	var the_sec=0;
	var	i = 0;
	var	j = 0;
	var c;
	var ampm='a';
	var str_temp="";
	var str_temp2="";
	var n_temp = 0;

	if ((str_time == null) || (str_time == ""))
		return DEFAULT_TIME;
		
	var	regEx = /\s/g;
	str_time = str_time.replace(regEx, "");
	if (str_time.search(/p/i)>=0)
		ampm='p'
	var	array_time = str_time.split(":");
	
	// Now we have dealt with the ampm and the :, we no longer need non digits, so strip them out once and for all
	for (i=0; i<array_time.length; i++)
	{
		array_time[i]=array_time[i].replace(/\D/g,"");
	}
	
	if (array_time.length>=2)
	{
		if (array_time[0].length<1)
			return DEFAULT_TIME;
		the_hour = parseInt(array_time[0], 10);
		if (array_time[1].length>0)
			the_min = parseInt(array_time[1], 10);
	}
	else
	{
		str_temp = array_time[0];
		n_temp = str_temp.length;
		if (n_temp<=2)
		{
			the_hour = parseInt(str_temp, 10);
		}
		else
		{			
			// strip off the last two digits to use for minutes
			str_temp2="";
			str_temp2 += str_temp.charAt(n_temp-2);
			str_temp2 += str_temp.charAt(n_temp-1);
			the_min = parseInt(str_temp2,10);
			str_temp2="";			
			for (i=0; i<n_temp-2; i++)
			{
				c = str_temp.charAt(i);	
				str_temp2 += c;
			}
			the_hour = parseInt(str_temp2, 10);
		}
	}
		
	if ((the_hour<13) && (ampm=='p'))
		the_hour+=12;
	else if ((ampm=='a') && (the_hour==12))
		the_hour=0;		
	if (24==the_hour)
		the_hour=12;		
		
	str_out = the_hour + ":" + the_min + ":00";
	return str_out;		
}

function fnDisplayTime2JSTime(str_time)
{
	var str_out = "";
	var the_hour=0;
	var the_min=0;
	var the_sec=0;
	var	i = 0;
	var c;
	var ampm;


	if ((str_time == null) || (str_time == ""))
		return DEFAULT_TIME;

	var	regEx = /\s/g;

	str_time = str_time.replace(regEx, "");

	var	array_time = str_time.split(":");

	if (2 > array_time.length)
		return DEFAULT_TIME;

	the_hour = parseInt(array_time[0],10);

	c = array_time[1].charAt(1);
	if (fnIsDigit(c))
	{
		the_min += 10 * parseInt(array_time[1].charAt(0),10);
		the_min += parseInt(array_time[1].charAt(1));
		ampm = array_time[1].charAt(2);
	}
	else
	{
		the_min += parseInt(array_time[1].charAt(0),10);
		ampm = array_time[1].charAt(1);
	}

	if (ampm == 'p')
		the_hour += 12;

	if (12 == the_hour)
		the_hour = 0;
	else if (24 == the_hour)
		the_hour = 12;

	var date_out = new Date();

	date_out.setHours(the_hour);
	date_out.setMinutes(the_min);

	return date_out;
}
function fnJSDate2DisplayTime(the_date)
{
	var	str_out = "";
	var	the_hour = 0;
	var	the_min = 0;
	var the_pm = 0;

	the_hour = the_date.getHours();

	if (the_date.getHours()==12)
	{
		the_pm = 1;
	}
	else if (the_date.getHours()>12)
	{
		the_pm = 1;
		the_hour -= 12;
	}
	else if (the_date.getHours()==0)
	{
		the_pm = 0;
		the_hour = 12;
	}

	the_min = the_date.getMinutes();

	str_out += the_hour + ":";
	if (the_min<10)
		str_out += "0";
	str_out += the_min;

	if (the_pm == 0)
		str_out += "am";
	else
		str_out += "pm";

	return str_out;
}

function fnJSDate2DisplayTime2(the_date)
{
	var	str_out = "";
	var	the_hour = 0;
	var	the_min = 0;
	var the_pm = 0;

	the_hour = the_date.getHours();

	if (the_date.getHours()==12)
	{
		the_pm = 1;
	}
	else if (the_date.getHours()>12)
	{
		the_pm = 1;
		the_hour -= 12;
	}
	else if (the_date.getHours()==0)
	{
		the_pm = 0;
		the_hour = 12;
	}

	the_min = the_date.getMinutes();

	str_out += the_hour + ":";
	if (the_min<10)
		str_out += "0";
	str_out += the_min;

	if (the_pm == 0)
		str_out += "a";
	else
		str_out += "p";

	return str_out;
}

function ReturnDateString()
{
	var strOut = "";
	var date1 = new Date();
	var the_year = date1.getFullYear();
	var the_month = date1.getMonth() + 1; 	// sql is 1-indexed, java & javascript are 0-indexed.  I am using 1-indexed months
	var the_day = date1.getDate();

	strOut = the_year + "-" + the_month + "-" + the_day;
	return strOut;
}

function fnJSDate2DOTW(js_date)
{
	var str_out = "";
	if (!js_date)
		return str_out;
	var n_dotw = js_date.getDay();
	switch (n_dotw)
	{
		case 0:
			str_out = "Sun";
			break;
		case 1:
			str_out = "Mon";
			break;
		case 2:
			str_out = "Tue";
			break;
		case 3:
			str_out = "Wed";
			break;
		case 4:
			str_out = "Thu";
			break;
		case 5:
			str_out = "Fri";
			break;
		case 6:
			str_out = "Sat";
			break;
	}
	return str_out;
}

function fnJSDate2Month(js_date)
{
	var str_out = "";
	if (!js_date)
		return str_out;
	var n_month = js_date.getMonth();
	switch (n_month)
	{
		case 0:
			str_out += "Jan";
			break;
		case 1:
			str_out += "Feb";
			break;
		case 2:
			str_out += "Mar";
			break;
		case 3:
			str_out += "Apr";
			break;
		case 4:
			str_out += "May";
			break;
		case 5:
			str_out += "Jun";
			break;
		case 6:
			str_out += "Jul";
			break;
		case 7:
			str_out += "Aug";
			break;
		case 8:
			str_out += "Sep";
			break;
		case 9:
			str_out += "Oct";
			break;
		case 10:
			str_out += "Nov";
			break;
		case 11:
			str_out += "Dec";
			break;
	}
	return str_out;
}

function JSDateToSQLDate(date1)
{
	var strOut = "";
	var the_year = date1.getFullYear();
	var the_month = date1.getMonth() + 1; 	// sql is 1-indexed, java & javascript are 0-indexed.  I am using 1-indexed months
	var the_day = date1.getDate();

	strOut = the_year + "-" + the_month + "-" + the_day;
	return strOut;
}

function DBTimeToString(the_time)
{
	var str_time = "";
	str_time += DBTimeToHour(the_time);
	str_time += ":";
	str_time += DBTimeToMin(the_time);
	str_time += DBTimeToAmPm(the_time);
	return str_time;
}

// Shows a,p instead of am,pm
function DBTimeToString2(the_time)
{
	var strTime = "";
	strTime += DBTimeToHour(the_time);
	strTime += ":";
	strTime += DBTimeToMin(the_time);
	strTime += DBTimeToAmPm2(the_time);

	return strTime;
}

function Time24ToDisplayTime(hh,mm)
{
	var	ampm = "a";
	var str_hh = "";
	var str_mm = "";
	var	str_out = "";

	if (hh==24)
	{
		ampm = "a";
		hh -= 12;
	}
	else if (hh==0)
	{
		ampm = "a";
		hh = 12;
	}
	else if (hh>12)
	{
		ampm = "p";
		hh -= 12;
	}
	else if (hh==12)
	{
		ampm = "p";
	}

	str_out += hh;
	str_out += ampm;

	return str_out;
}

function DBTimeToHour(the_time)
{
	var strOut = "";
	var offset = 0;
	var	nTime = 0;

	offset = the_time.indexOf(":");

	nTime = parseInt(the_time.substring(0, offset),10);

	if (nTime>12)
		nTime -= 12;

	if (nTime<1)
		nTime = 12;

	strOut += nTime;

	return strOut;
}

function DBTimeTo24Hour(the_time)
{
	var strOut = "";
	var offset = 0;
	var	nTime = 0;

	offset = the_time.indexOf(":");

	nTime = parseInt(the_time.substring(0, offset),10);

	if (nTime<1)
		nTime = 12;

	strOut += nTime;

	return strOut;
}

/* Convert DBTime to just the hour part as a 24 hr numerical value */
function DBTimeTo24Hour2(the_time)
{
	var offset = 0;
	var nHour = 0;

	offset = the_time.indexOf(":");
	nHour = parseInt(the_time.substring(0, offset),10);

	return nHour;
}

/* convert from SQL time (not date) to num of mins since the midnight start of day */
function fnSQLTime2TotalMins(sql_time)
{
	var n_hour = DBTimeTo24Hour2(sql_time);
	var n_min = parseInt(DBTimeToMin(sql_time),10);
	return (n_hour*60 + n_min);
}

/* Convert SQLTime to a num of half hours since 0 */
function SQLTimeToHalfHrNum(sql_time)
{
	var array_str_time = sql_time.split(":");
	var n_hour = 0;
	var n_half = 0;
	var n_out = 0;

	if (array_str_time.length<2)
		return n_out;

	n_hour = parseInt(array_str_time[0],10);
	n_min = parseInt(array_str_time[1],10);

	n_out = n_hour * 2;
	if (n_min>15)
		n_out++;

	return n_out;
}

function fnHourTo24HourCode(the_hour, and_a_half)
{
	var str_out = "";

	if (the_hour<=9)
		str_out += "0";
	str_out += the_hour;
	if (and_a_half)
		str_out += "30";
	else
		str_out += "00";

	return str_out;
}


function fnHalfHoursTo24HourCode(half_hours)
{
	var the_hour = Math.floor(half_hours/(2.0));
	var and_a_half = false;
	if ((half_hours % 2) != 0)
		and_a_half = true;

	return fnHourTo24HourCode(the_hour, and_a_half);
}


function fnHourTo24HourCode2(sql_time)
{
	var str_out = "";
	var str_hour = DBTimeTo24Hour2(sql_time);
	var str_min = DBTimeToMin(sql_time);
	var the_hour = parseInt(str_hour, 10);
	var the_min = parseInt(str_min, 10);

	if (the_hour<=9)
		str_out += "0";
	str_out += str_hour;

	if (the_min<30)
		str_out += "00";
	else
		str_out += "30";

	return str_out;
}

function DBTimeToAmPm(the_time)
{
	var strOut = "am";
	var offs = 0;
	var	nTime = 0;

	offs = the_time.indexOf(":");
	nTime = parseInt(the_time.substring(0, offs),10);

	if (nTime>=12)
		strOut = "pm";

	return strOut;
}

// Shows a,p instead of am,pm
function DBTimeToAmPm2(the_time)
{
	var strOut = "a";
	var offs = 0;
	var	nTime = 0;

	offs = the_time.indexOf(":");
	nTime = parseInt(the_time.substring(0, offs),10);

	if (nTime>=12)
		strOut = "p";

	return strOut;
}

function DBTimeToMin(the_time)
{
	var strOut = "";
	var offs = 0;
	var	strMin = "";

	offs = the_time.indexOf(":");
	strMin += the_time.charAt(offs+1);
	if (the_time.charAt(offs+2)!=':')
		strMin += the_time.charAt(offs+2);

	if (strMin.length<1)
		strOut+="0";
	if (strMin.length<2)
		strOut+="0";
	strOut += strMin;

	return strOut;
}

function fnShowHide(bShow, elem)
{
	if (bShow)
	{
		fnShow(elem);
	}
	else
	{
		fnHide(elem);
	}
}

function fnShow(elem)
{
	if (null != elem)
	{
		if (document.getElementById(elem))
			document.getElementById(elem).style.display = "";
	}
}

function fnShowBlock(elem)
{
	if (null != elem)
	{
		if (document.getElementById(elem))
			document.getElementById(elem).style.display = "block";
	}
}

function fnHide(elem)
{
	if (null != elem)
	{
		if (document.getElementById(elem))
			document.getElementById(elem).style.display = "none";
	}
}

function fnEnableDisable(bEnable, str_elem)
{
	if (bEnable)
		fnEnable(str_elem);
	else
		fnDisable(str_elem);
	return bEnable;
}

function fnEnable(str_elem)
{
	var elem = document.getElementById(str_elem);
	if (elem!= null)
	{
		elem.disabled = false;
		if (!(browser.isIE))
		{
			elem.style.color = "#000000";
			elem.style.borderTopColor="#909080";
			elem.style.borderLeftColor="#909080";
			elem.style.borderRightColor="#010101";
			elem.style.borderBottomColor="#010101";
		}
	}
}
function fnDisable(str_elem)
{
	var elem = document.getElementById(str_elem);
	if (elem!= null)
	{
		elem.disabled = true;
		if (!(browser.isIE))
		{
			elem.style.color = "#aaaaaa";
			elem.style.borderTopColor="#aaaaaa";
			elem.style.borderLeftColor="#aaaaaa";
			elem.style.borderRightColor="#aaaaaa";
			elem.style.borderBottomColor="#aaaaaa";
		}
	}
}

function fnChangeClass(str_elem, str_newclass)
{
	if (null != str_elem)
	{
		if (document.getElementById(str_elem))
			document.getElementById(str_elem).className = str_newclass;
	}		
}

function fnSetBGColor(elem,str_color)
{
	if (null != elem)
	{
		if (document.getElementById(elem))
			document.getElementById(elem).style.backgroundColor = str_color;
	}
}

function fnSetChecked(elem)
{
	if (null != elem)
	{
		if (document.getElementById(elem))
			document.getElementById(elem).checked = true;
	}
}
function fnSetUnchecked(elem)
{
	if (null != elem)
	{
		if (document.getElementById(elem))
			document.getElementById(elem).checked = false;
	}
}
function fnGetChecked(elem)
{
	if (!elem)
		return false;
	if (!(document.getElementById(elem)))
		return false;
	return document.getElementById(elem).checked;
}

function fnSetSelectedIndex(elem, the_index)
{
	if (null != elem)
	{
		if (document.getElementById(elem))
			document.getElementById(elem).selectedIndex = the_index;
	}
}

function fnGetSelectedIndex(elem)
{
	if (null != elem)
	{
		if (document.getElementById(elem))
			return document.getElementById(elem).selectedIndex;
	}
	return 0;
}

function fnFocus(elem)
{
	if ((document.getElementById(elem)!= null) && (document.getElementById(elem).disabled == false) && (document.getElementById(elem).display != "none"))
		document.getElementById(elem).focus();
}

function fnSetRTEValue(str_rte, str_value)
{
	if (document.getElementById(str_rte) != null)
		document.getElementById(str_rte).contentWindow.document.body.innerHTML = str_value;
}

function fnResetInnerhtml(elem)
{
	if (document.getElementById(elem)!= null)
		document.getElementById(elem).innerHTML = "";
}

function fnSetInnerhtml(elem, str_value)
{
	if (document.getElementById(elem)!= null)
		document.getElementById(elem).innerHTML = str_value;
}

function fnGetInnerhtml(str_elem)
{
	if (document.getElementById(str_elem)!= null)
		return document.getElementById(str_elem).innerHTML;
	return "";
}

function fnSetValue(elem, str_value)
{
	if (document.getElementById(elem)!= null)
		document.getElementById(elem).value = str_value;
}

function fnResetInnertext(str_elem)
{
	if (document.getElementById(str_elem)!= null)
	{
		if (browser.isIE)
			document.getElementById(str_elem).innerText = "";
		else
			document.getElementById(str_elem).textContent = "";
	}
}

function fnSetInnertext(str_elem, str_value)
{
	if (document.getElementById(str_elem)!= null)
	{
		if (browser.isIE)
			document.getElementById(str_elem).innerText = str_value;
		else
			document.getElementById(str_elem).textContent = str_value;
	}
}

function fnGetInnertext(str_elem)
{
	if (document.getElementById(str_elem)!= null)
	{
		if (browser.isIE)
			return document.getElementById(str_elem).innerText;
		else
			return document.getElementById(str_elem).textContent;
	}
	return "";
}

function fnGetElemValue(str_elem)
{
	if (document.getElementById(str_elem)!= null)
		return document.getElementById(str_elem).value;
	return "";
}

function fnSetElemValue(elem, str_value)
{
	if (document.getElementById(elem)!= null)
		document.getElementById(elem).value = str_value;
}

function fnSetElemColor(elem, str_value)
{
	if (document.getElementById(elem)!= null)
		document.getElementById(elem).style.color = str_value;
}

function fnStripFinalChar(str_in)
{
	var str_out = "";
	var i = 0;
	var new_len = str_in.length - 1;

	for (i = 0; i<new_len; i++)
	{
		str_out += str_in.charAt(i);
	}

	return str_out;
}

function PXValToInt(str_px, ndefault)
{
	if (null == str_px)
		return ndefault;

	var str_temp = "";
	str_temp += str_px;

	if ("" == str_px)
		return ndefault;

	var val_array = str_temp.split("p");
	var n_out = parseInt(val_array[0],10);

	return n_out;
}

function fnSetScrollTop(str_elem, n_value)
{
	if (document.getElementById(str_elem)!=null)
		document.getElementById(str_elem).scrollTop = n_value;
}

function fnSetFocus(str_elem)
{
	if (!str_elem)
		return;
	var elem = document.getElementById(str_elem);
	if (!elem)
		return;
	elem.focus();
}
function fnBlur(str_elem)
{
	if (!str_elem)
		return;
	var elem = document.getElementById(str_elem);
	if (!elem)
		return;
	elem.blur();
}

// From http://www.mredkj.com/tutorials/tutorial005.html
// http://www.mredkj.com/legal.html "Code marked as public domain is without copyright, and can be used without restriction."
// Modified by me, Dan Burke
function fnInsertOptionBefore(str_select,str_text,str_value)
{
	var elem_sel = document.getElementById(str_select);
	if (!elem_sel)
  		return;
	if (elem_sel.selectedIndex >= 0) 
	{
		var elem_new_opt = document.createElement('option');
		elem_new_opt.text = str_text;
		elem_new_opt.value = str_value;
		var elem_old_opt = elem_sel.options[elem_sel.selectedIndex];  
		if (browser.isIE)
			elem_sel.add(elem_new_opt, elem_sel.selectedIndex);
		else
		  	elem_sel.add(elem_new_opt, elem_old_opt);
	}
}
function fnRemoveOptionSelected(str_select)
{
	var elem_sel = document.getElementById(str_select);
	if (!elem_sel)
  		return;
	var i;
	if (elem_sel.length<1)
		return;
	for (i = elem_sel.length - 1; i>=0; i--) 
	{
		if (elem_sel.options[i].selected)
			elem_sel.remove(i);
	}
}
function fnAppendOptionLast(str_select,str_text,str_value)
{
	var elem_new_opt = document.createElement('option');
	if (!elem_new_opt)
  		return;
	elem_new_opt.text = str_text;
	elem_new_opt.value = str_value;
	var elem_sel = document.getElementById(str_select);
	if (!elem_sel)
  		return;
	if (browser.isIE)
		elem_sel.add(elem_new_opt);
	else
	  	elem_sel.add(elem_new_opt, null);
}
function fnRemoveOptionLast(str_select)
{
	var elem_sel = document.getElementById(str_select);
	if (!elem_sel)
		return;
	if (elem_sel.length > 0)
		elem_sel.remove(elem_sel.length - 1);
}
// End of modified select functions

function fnCompareIgnoreCase(str_left, str_right)
{   
	var str_a = "" +str_left;
	var str_b = "" +str_right;	
	return ((str_a.toLowerCase())==(str_b.toLowerCase()));
}

function fnCenterElem(str_elem)
{
	if (!str_elem || (str_elem==""))
		return;
		
	var elem = document.getElementById(str_elem);
	if (typeof(elem)=="undefined")
		return;
	if (!elem)
		return;
			
	var client_width = xClientWidth();
	var client_height = xClientHeight();
	var elem_height = xHeight(str_elem);
	var elem_width = xWidth(str_elem);	
	var elem_x = (client_width/2) - (elem_width/2);
	var elem_y = (client_height/2) - (elem_height/2);
	xLeft(str_elem,elem_x);
	xTop(str_elem,elem_y);
}
function setOpacity(elem, the_value)
{
	elem.style.opacity = the_value/10;
	elem.style.filter = 'alpha(opacity=' + the_value*10 + ')';
}

function fnGetMouseInString()
{
	if (browser.isIE)
		return "onmouseenter";
	else
		return "onmouseover";
}

function fnGetMouseOutString()
{
	if (browser.isIE)
		return "onmouseleave";
	else
		return "onmouseout";
}

// Safely parse numbers, so that if the input string is invalid, we return the default given
function fnSafeParseInt(str_num, n_default)
{
	if (typeof(str_num)=="undefined")
		return n_default;

	if (str_num == "")
		return n_default;

	if (!str_num)
		return n_default;

	var n_out = n_default;

	try
	{
		n_out = parseInt(str_num,10);
	}
	catch (err)
	{
		n_out = n_default;
	}
    if (typeof (n_out) != "number" || isNaN(n_out))
        n_out = n_default;
	return n_out;
}

function fnAdjustFrameHeight(iframe_id)
{
	var the_height=document.getElementById(iframe_id).contentWindow.document.body.scrollHeight;
	if (the_height<300)
		the_height=300;
	document.getElementById(iframe_id).height=the_height+10;
}
var g_iframe_id="";
function fnAdjustFrameHeightG()
{
	fnAdjustFrameHeight(g_iframe_id)
}
function fnMegaAdjustFrameHeight(iframe_id)
{
	g_iframe_id =iframe_id;
	window.setTimeout(fnAdjustFrameHeightG, 500);
	window.setTimeout(fnAdjustFrameHeightG, 1500);
	window.setTimeout(fnAdjustFrameHeightG, 10000);
}

function fnGetSelectedValue(str_elem, n_default)
{
	if (null == str_elem)
		return n_default;
	var elem = document.getElementById(str_elem);
	if (null == elem)
		return n_default;
	return elem.options[elem.selectedIndex].value;
}
//////////////////// end of Utility Functions ////////////////////////

