﻿isOpera = navigator.userAgent.indexOf('Opera') > -1;
isIE = navigator.userAgent.indexOf('MSIE') > 1 && !isOpera; 
isMoz = navigator.userAgent.indexOf('Mozilla/5.') == 0 && !isOpera;
isMozFirefox = isMoz && navigator.userAgent.indexOf('Firefox') > -1;
IE4 = document.all;

function FormatCurrency (
	amount,
	noDecimals)
{
	if (typeof (noDecimals) == 'undefined'
		|| noDecimals == null)
	{
		noDecimals = false;
	}

	var decimalPlaces = 2;	
	if (noDecimals)
	{
		decimalPlaces = 0;
	}
	amount = roundNumber (amount, decimalPlaces);
	
	if (isNaN(amount))
	{
		return '';
	}
	
	amount = amount + "";
	var index = amount.indexOf ('.');

	// Add decimals
	if (index == -1)
	{
		amount = amount + ".00";
	}
	else if (amount.length - index == 2)
	{
		amount = amount + "0";
	}
	
	if (amount.length < 7)
	{
		if (noDecimals)
		{
			amount = amount.substring (0, amount.indexOf ('.'));
		}
		return amount;
	}
	
	// Add commas
	index = amount.length-6;
	var result = amount.substring (index);
	
	index = index - 3;
	
	while (index > 0)
	{	
		result = amount.substring (index, index+3) + "," + result;
		index = index - 3;
	}
	amount = amount.substring (0, index+3) + "," + result;
	
	if (noDecimals)
	{
		amount = amount.substring (0, amount.indexOf ('.'));
	}
	return amount;
}

function FormatControlAsCurrency (control)
{
	var value = control.value;
	control.value = FormatCurrency (control.value);
	if (control.value != value)
	{
		control.fireEvent ('onchange');
	}
}

function FormatInt (number)
{
	number = getInt (number);

	if (isNaN(number))
	{
		return '';
	}
	
	number = number + "";
	
	if (number.length < 4)
	{
		return number;
	}

	// Add commas
	var index = number.length-3;
	var result = number.substring (index);

	index = index - 3;

	while (index > 0)
	{	
		result = number.substring (index, index+3) + "," + result;
		index = index - 3;
	}

	return + number.substring (0, index+3) + "," + result;
}

function FormatControlAsInt (control)
{
	var value = control.value;
	control.value = FormatInt (control.value);
	if (control.value != value)
	{
		control.fireEvent ('onchange');
	}
}

function FormatDecimal (amount)
{
	amount = roundNumber(amount);
	
	if (isNaN(amount))
	{
		return '';
	}
	
	amount = amount + "";
	var index = amount.indexOf ('.');

	// Add decimals
	if (index == -1)
	{
		amount = amount + ".00";
	}
	else if (amount.length - index == 2)
	{
		amount = amount + "0";	
	}
	
	if (amount.length < 7)
	{
		return amount;
	}
	
	// Add commas
	index = amount.length-6;
	var result = amount.substring (index);
	
	index = index - 3;
	
	while (index > 0)
	{	
		result = amount.substring (index, index+3) + "," + result;
		index = index - 3;
	}
	
	return + amount.substring (0, index+3) + "," + result;
}

function FormatControlAsDecimal (control)
{
	var value = control.value;
	control.value = FormatDecimal (control.value);
	if (control.value != value)
	{
		control.fireEvent ('onchange');
	}
}

function roundNumber(num, decimalPlaces)
{
	if (typeof decimalPlaces == 'undefined' || decimalPlaces == null)
	{
		decimalPlaces = 2;
	}
	num = getFloat(num + '');
	var newnumber = Math.round(num * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces);

	return newnumber;
}


function EnableDisableControlsFromCheckBoxState(
	chkBox,
	invert,
	ctrl1Name,
	ctrl2Name,
	ctrl3Name,
	ctrl4Name,
	ctrl5Name)
{
	if (invert == null)
	{
		invert = false;
	}
	var disabled = chkBox.checked;
	if (invert)
	{
		disabled = !disabled;
	}
	getEl(ctrl1Name).disabled = disabled;
	if (ctrl2Name)
	{
		getEl(ctrl2Name).disabled = disabled;
	}
	if (ctrl3Name)
	{
		getEl(ctrl3Name).disabled = disabled;
	}
	if (ctrl4Name)
	{
		getEl(ctrl4Name).disabled = disabled;
	}
	if (ctrl5Name)
	{
		getEl(ctrl5Name).disabled = disabled;
	}
}

function ShowOrHideControlsFromCheckBoxState(
	chkBox,
	invert,
	ctrl1Name,
	ctrl2Name,
	ctrl3Name,
	ctrl4Name,
	ctrl5Name)
{
	if (invert == null)
	{
		invert = false;
	}
	
	var visibility = 'visible';
	var showHide = chkBox.checked;

	if (invert)
	{
		showHide = !showHide;
	}

	if (showHide)
	{
		visibility = 'visible';
	}
	else
	{
		visibility = 'hidden';
	}
	
	getEl(ctrl1Name).style.visibility = visibility;
	if (ctrl2Name)
	{
		getEl(ctrl2Name).style.visibility = visibility;
	}
	if (ctrl3Name)
	{
		getEl(ctrl3Name).style.visibility = visibility;
	}
	if (ctrl4Name)
	{
		getEl(ctrl4Name).style.visibility = visibility;
	}
	if (ctrl5Name)
	{
		getEl(ctrl5Name).style.visibility = visibility;
	}
}

function getEl (id)
{
	return document.getElementById (id);
}

function EnableDisableControlsFromRadioState(
	radio, 
	invert, 
	ctrl1Name, 
	ctrl2Name, 
	ctrl3Name, 
	ctrl4Name, 
	ctrl5Name,
	ctrl6Name, 
	ctrl7Name, 
	ctrl8Name, 
	ctrl9Name,
	ctrl10Name,
	ctrl11Name, 
	ctrl12Name, 
	ctrl13Name, 
	ctrl14Name, 
	ctrl15Name)
{
	if (invert == null)
	{
		invert = false;
	}
	var disabled = radio.checked;
	if (invert)
	{
		disabled = !disabled;
	}

	EnableDisableControl (ctrl1Name, disabled);
	EnableDisableControl (ctrl2Name, disabled);
	EnableDisableControl (ctrl3Name, disabled);
	EnableDisableControl (ctrl4Name, disabled);
	EnableDisableControl (ctrl5Name, disabled);
	EnableDisableControl (ctrl6Name, disabled);
	EnableDisableControl (ctrl7Name, disabled);
	EnableDisableControl (ctrl8Name, disabled);
	EnableDisableControl (ctrl9Name, disabled);
	EnableDisableControl (ctrl10Name, disabled);
	EnableDisableControl (ctrl11Name, disabled);
	EnableDisableControl (ctrl12Name, disabled);
	EnableDisableControl (ctrl13Name, disabled);
	EnableDisableControl (ctrl14Name, disabled);
	EnableDisableControl (ctrl15Name, disabled);
}

function EnableDisableControl (controlName, disabled)
{
	if (!controlName)
	{
		return;
	}
	
	var control = getEl(controlName);
	
	if (control.type != undefined && control.type.toUpperCase () == "TEXT")
	{
		if (disabled)
		{
			control.readOnly = "readonly";
			control.className = "TextBoxLabelMode";
		}
		else
		{
			control.readOnly = false;
			control.className = "TextBox";
		}
	}
	else if (control.tagName.toUpperCase () == "SPAN")
	{
		if (disabled)
		{
			control.enabled = false;
			if (control.innerHTML.indexOf ("<") == -1)
			{
				control.innerHTML = "";				
			}
			else
			{
				control.style.display = "none";
			}
		}
		else
		{
			control.enabled = true;
		}
	}
	else if (control.tagName.toUpperCase () == "IMG")
	{
		if (disabled)
		{
			control.style.display = "none";
		}
		else
		{
			control.style.display = "";
		}
	}
	else
	{
		control.disabled = disabled;
	}
	
	if (disabled)
	{
		control.value = '';
	}
}

function GetWeeksDiff (startDate, endDate)
{
	if (startDate == '' || endDate == '')
	{
		return 0;
	}

	// Capitalize the month name for function getDateFromDashString();
	startDate = capitalizeDashedDateMonth (startDate);
	endDate = capitalizeDashedDateMonth (endDate);

	var date1 = getDateFromDashString(startDate);
	var date2 = getDateFromDashString(endDate);
	// sets difference date to difference of first date and second date
	var timediff = date2.getTime() - date1.getTime();
	var weeks = Math.ceil(timediff / (1000 * 60 * 60 * 24 * 7));
	return weeks;
}

// dateString format must = "dd-MMM-yyyy"
function capitalizeDashedDateMonth (dateString)
{
	dateString = dateString.substring(0, 4) + dateString.substring(4, 6).toLowerCase() + dateString.substring(6);
	return dateString;
}

function getMonthNameShort(month)
{
	return getMonthName(month).substring(0, 3);
}
function getMonthName(month)
{
	switch (month)
	{
		case 1: return 'January'; break;
		case 2: return 'February'; break;
		case 3: return 'March'; break;
		case 4: return 'April'; break;
		case 5: return 'May'; break;
		case 6: return 'June'; break;
		case 7: return 'July'; break;
		case 8: return 'August'; break;
		case 9: return 'September'; break;
		case 10: return 'October'; break;
		case 11: return 'November'; break;
		case 12: return 'December'; break;
		default: return '(invalid modth number: ' + month + ')'; break;
	}
}

function countCharOccurrences(str, charToCount)
{
	var count = 0;
	for (var i=0; i<str.length; i++)
	{
		if (str.charAt(i) == charToCount)
		{
			count++;
		}
	}
	return count;
}

function checkDateTextBoxFutureDate (dtbID, date)
{
	var showAlert = true;
	var clearCalendarIfFuture = true;
	var cal = getEl (dtbID);

	var today = new Date();
	today.setHours(0,0,0,0);
	if (date > today)
	{
		if (showAlert)
		{
			alert('Sorry, this date cannot be set to a future date.\n\nThe current date is: ' + today.toDateString());
		}
		if (clearCalendarIfFuture)
		{
			cal.value = '';
		}
	}
}

function checkDateTextBoxMustBeFutureDate (dtbID, date)
{
	var showAlert = true;
	var clearCalendarIfNotFuture = true;
	var cal = getEl (dtbID);

	var today = new Date();
	today.setHours(0,0,0,0);
	if (date <= today)
	{
		if (showAlert)
		{
			alert('Sorry, this date has to be set to a future date.\n\nThe current date is: ' + today.toDateString());
		}
		if (clearCalendarIfNotFuture)
		{
			cal.value = '';
		}
	}
}


function checkCalendarFutureDate(cal, showAlert, clearCalendarIfFuture)
{
	if (showAlert == undefined || showAlert == null)
	{
		showAlert = true;
	}
	if (clearCalendarIfFuture == undefined || clearCalendarIfFuture == null)
	{
		clearCalendarIfFuture = true;
	}
	
	var date = getDateFromDashString(cal.value);
	var today = new Date();
	today.setHours(0,0,0,0);
	if (date > today)
	{
		if (showAlert)
		{
			alert('Sorry, this date cannot be set to a future date.\n\nThe current date is: ' + today.toDateString());
		}

		if (clearCalendarIfFuture)
		{
			cal.value = '';
			if (getEl(cal.name + 'Span'))
			{
				getEl(cal.name + 'Span').innerText = '';
			}
		}
	}
}

function Trim (s)
{
	return s.replace(/^\s*|\s*$/g,"");
}

function getFloat (s)
{
	s = s + "";
	return parseFloat (s.replace (/ /g, "").replace (/,/g, ""))
}

function getInt (s)
{
	s = s + "";
	while (s.length > 1 && s.indexOf("0") == 0)
	{
		s = s.substring (1);
	}
	return parseInt (s.replace (/ /g, "").replace (/,/g, ""))
}

function getIntFromControl (controlName)
{
	var number = 0;
	var value = getEl (controlName).value;
	value = value.replace (/,/g, "");
	value = parseInt (value);
	
	if (!isNaN(value))
	{
		number = value;
	}
	
	return number;
}

function getFloatFromControl (controlName)
{
	var number = 0;
	var value = getEl (controlName).value;
	value = value.replace (/,/g, "");
	value = parseFloat (value);
	
	if (!isNaN(value))
	{
		number = value;
	}
	
	return value
}

function newAlert (
	title,
	mess,
	icon,
	mods) 
{
   (IE4) ? makeMsgBox (title, mess, icon, 0, 0, mods) : alert(mess);
}

function newConfirm (
	title,
	mess,
	icon,
	defbut,
	mods) 
{
   if (IE4) 
   {
      //icon = (icon==0) ? 0 : 2;
      defbut = (defbut==0) ? 0 : 1;
      retVal = makeMsgBox (title, mess, icon, 4, defbut, mods);
      retVal = (retVal==6);
   }
   else 
   {
      retVal = confirm(mess);
   }
   return retVal;
}

function newPrompt (
	title,
	mess,
	def) 
{
   retVal = (IE4) ? makeInputBox (title, mess, def) : prompt (mess, def);
   return retVal;
}


function ResizeAllMultilineTextboxesForPrinting ()
{
	var tAreas = document.getElementsByTagName('textarea');
	for (var t = 0; t < tAreas.length; t++)
	{
		tAreas[t].style.height = tAreas[t].scrollHeight;
		if (isMoz)
		{
			tAreas[t].style.height = tAreas[t].scrollHeight + 10; // Add few px to get rid of scrollbar.
		}
		tAreas[t].style.overflow = 'hidden'; // Hide V-scrollbar
	}
}

function getAbsoluteLeft (id)
{
	var o = getEl (id);
	var oLeft = o.offsetLeft;
	while(o.offsetParent != null)
	{
		var oParent = o.offsetParent;
		oLeft += oParent.offsetLeft;
		o = oParent;
	}
	return oLeft;
}


function getAbsoluteTop (id)
{
	var o = getEl (id);
	var oTop = o.offsetTop;
	while(o.offsetParent != null)
	{
		var oParent = o.offsetParent;
		oTop += oParent.offsetTop;
		o = oParent;
	}
	return oTop;
}

