//******************************** FILTERS.JS *******************************

// Range and character filters for <INPUT> tags, display filters, etc.

// - - - D I S P L A Y   F I L T E R S - - -

	// (used by insertScaledText function below)

	function logBaseNofM (n, m)
		{
		return Math.log(m) / Math.log(n);
		}

	// (used by insertScaledText function below)

	function logBase2 (m)
		{
		return logBaseNofM(2, m);
		}

	// insert text scaled to fit a given page element
	// (used- by priorityDesigner.js)

	function insertScaledText (element, newText)
		{
		//debug=true;
		//rdb("insertScaledText(" + element + ", " + newText + ")");
		var width = element.clientWidth;
		var height = element.clientHeight;
		//rdb("width = " + width + ", height = " + height);
		if (!newText || !newText.length || !width || !height)
			return;
		var len = newText.length;
		var fontSize, aproxPix;
		if ((len * 10) >= width)
			fontSize = parseInt(1.8 * (width / len));
		else
			{
			aproxPix = len * logBase2(len);
			fontSize = parseInt((width - aproxPix) / 12);
			}
		if (fontSize > height / 2)
			element.style.fontSize = (height / 2) + 'px';
		else
			element.style.fontSize = (fontSize + 1) + 'px';
		//rdb("element.style.fontSize set to " + element.style.fontSize + "px");
		element.innerText = newText;
		}

// - - - M O U S E   E N H A N C E M E N T S - - -
// (these events are to be attached to text captions alongside radio buttons and checkboxes)

	// onclick event: radioID must be unique on the page

	function selectRadioButton (radioID)
		{
		//rdb('filters.js::selectRadioButton(radioID="'+radioID+'")');
		if (!document.all[radioID].disabled)
			{
			document.all[radioID].checked = true;
			document.all[radioID].focus();
			}
		}

	// onclick event: checkboxID must be unique on the page

	function toggleCheckbox (checkboxID)
		{
		//rdb('filters.js::toggleCheckbox(checkboxID="'+checkboxID+'")');
		if (!document.all[checkboxID].disabled)
			{
			document.all[checkboxID].checked = !document.all[checkboxID].checked;
			document.all[checkboxID].focus();
			}
		}

	// onclick event: radioID must be unique on the page
	// (this is an enhanced version of selectRadioButton above)

	function changeRadio (radioID)
		{
		selectRadioButton(radioID);
		if (dataHasChanged)
			setTimeout("dataHasChanged(document.all['"+radioID+"']);", 10);
		else if (enableControls)
			setTimeout("enableControls();", 10);
		// if user supplies dataHasChanged(obj) then user must manually call enableControls();
		}

	// onclick event: checkboxID must be unique on the page
	// (this is an enhanced version of toggleCheckbox above)

	function changeCheckbox (checkboxID)
		{
		toggleCheckbox(checkboxID);
		if (dataHasChanged)
			setTimeout("dataHasChanged(document.all['"+checkboxID+"']);", 10);
		else if (enableControls)
			setTimeout("enableControls();", 10);
		// if user supplies dataHasChanged(obj) then user must manually call enableControls();
		}

// - - - G E N E R I C   K E Y B O A R D   F I L T E R S - - -

	var Keyboard = new Object ();
	Keyboard.ENTER_KEY = 13;
	Keyboard.SPACE = 32;
	Keyboard.TAB = 9;

	// onkeypress event: filter out Enter and Spacebar for body tags ... prevents IE5.5 from scrolling!
	// also: enforce upper-case-only data input if input tag has an upperCase property set to true

	function window_onkeypress ()
		{
		if (event.srcElement.type == "text" || event.srcElement.type == "password")
			{
			event.cancelBubble = true;
			// handle input tag property "upperCase"
			if (event.srcElement.upperCase == true && event.keyCode >= 97 && event.keyCode <= 122)
				event.keyCode -= 32;
			// handle ctrl-delete in window_onkeydown event below
			return true;
			}
		else if (event.srcElement.type == "button" && (event.keyCode == Keyboard.ENTER_KEY || event.keyCode == Keyboard.SPACE))
			{
			event.cancelBubble = true;
			event.srcElement.onclick();
			return false;
			}
		return (event.keyCode != Keyboard.ENTER_KEY && event.keyCode != Keyboard.SPACE) ? true : false;
		}

	// onkeydown event: enable Control-Delete key sequence to delete entire field	and enter key to tab

	function window_onkeydown ()
		{
		if (event.srcElement.type == "text" || event.srcElement.type == "password")
			{
			if (event.keyCode == 46 && event.ctrlKey)		// (Ctrl-Del)
				event.srcElement.value = "";
			if (event.keyCode == Keyboard.ENTER_KEY)		// (Enter)
				event.keyCode = Keyboard.TAB;
			return true;
			}
		else if (event.srcElement.type == "button" && (event.keyCode == Keyboard.ENTER_KEY || event.keyCode == Keyboard.SPACE))
			{
			event.cancelBubble = true;
			event.srcElement.onclick();
			return false;
			}
		return (event.keyCode != Keyboard.ENTER_KEY && event.keyCode != Keyboard.SPACE) ? true : false;
		}

	// onkeydown event: enable only enter key

	function window_onkeydown2 ()
		{
		if (event.srcElement.type == "text" || event.srcElement.type == "password")
			{
			if (event.keyCode == Keyboard.ENTER_KEY)		// (Enter)
				event.keyCode = Keyboard.TAB;
			return true;
			}
		else if (event.srcElement.type == "button" && (event.keyCode == Keyboard.ENTER_KEY || event.keyCode == Keyboard.SPACE))
			{
			event.cancelBubble = true;
			event.srcElement.onclick();
			return false;
			}
		return (event.keyCode != Keyboard.ENTER_KEY && event.keyCode != Keyboard.SPACE) ? true : false;
		}

// - - - S P E C I F I C   K E Y B O A R D   F I L T E R S - - -
/*
 IMPORTANT!  Use these in conjunction with input tag's "maxLength=n"
				 attribute to limit string's length, even for durations
				 otherwize selections will not be handled properly
*/

	// onkeypress event: expecting n-digit numeral

	function filterNumericInput ()
		{
		// keycode must be >= '0' and <= '9'
		var min = event.srcElement.min;
		var max = event.srcElement.max;
		var val = parseInt(stripLeadingZeros(event.srcElement.value));
		var width = event.srcElement.maxlength;
		var zeroPad = (event.srcElement.zeropad != null) ? true : false;

		if (event.keyCode == 38)
			{
			if (min != null && max != null)
				{
				if (val + 1 <= max)
					event.srcElement.value = (zeroPad) ? zeroPadToNPlaces(val + 1, width) : val + 1;
				}
			return false;
			}
		if (event.keyCode == 40)
			{
			if (event.srcElement.min != null && event.srcElement.max != null)
				{
				if (val - 1 >= min)
					event.srcElement.value = (zeroPad) ? zeroPadToNPlaces(val - 1, width) : val - 1;
				}
			return false;
			}

		if (zeroPad)
			event.srcElement.value = zeroPadToNPlaces(event.srcElement.value, width);
		if (this.calculateNewDate)
			setTimeout("calculateNewDate()", 100);
		return (event.keyCode >= 48 && event.keyCode <= 57) ? true : false;
		}

	// onkeypress event: single character alphanumeric or punctuation replacement input field

	function filterReplaceWithAnykey ()
		{
		event.srcElement.value = '';
		return true;
		}

	// onkeypress event: single character alphanumeric-only replacement input field

	function filterReplaceWithAlphanumKey ()
		{
		if ((event.keyCode >= 65 && event.keyCode <= 90)
					 || (event.keyCode >= 96 && event.keyCode <= 122)
					 || (event.keyCode >= 48 && event.keyCode <= 57))
			{
			event.srcElement.value = '';
			return true;
			}
		return false;
		}

	/* - - - N O T   U S E D - - -
	// onkeypress event: expecting mm:ss.hh

	function filterDurationInput (who)
		{
		// keycode must be >= '0' and <= '9' or == ':' or == '.'
		return ((event.keyCode >= 48 && event.keyCode <= 57) || event.keyCode == 58 || event.keyCode == 46) ? true : false;
		}
	*/

// - - - D A T A   C H E C K I N G   ( after the fact ) - - -

	function formatDuration (_totalSecs)
		{
		var mins = parseInt(_totalSecs / 60);
		var secs = _totalSecs - mins * 60;
		return mins + ":" + (secs < 10 ? "0" : "") + secs;
		}

	function zeroPadToNPlaces (_val, _width)
		{
		_val = parseInt(_val);
		while (new String(_val).length < _width)
			_val = "0" + _val;
		return _val;
		}

	function stripLeadingZeros (_val)
		{
		while (_val.substring(0,1) == "0")
			_val = _val.substring(1);
		if (_val == "")
			_val = "0";
		return _val;
		}

	// display an alert if a field should be non-zero, but isn't

	function checkNonZero (who)
		{
		if (parseInt(who.value) == 0 || who.value < "        ")
			{
			showDialogBox(resources.res_MustBeNonZero, 'customAlert');
			return false;
			}
		return true;
		}

	// replace a blank or zero value with a one

	function makeNonZero (value)
		{
		if (value.value != null)
			{
			if (parseInt(value.value) == 0 || value.value < "        ")
				value.value = 1;
			return value.value;
			}
		if (parseInt(value) == 0 || value < "        ")
			value = 1;
		return value;
		}

	// handles to the functions in the loading.htm frame:

	var ANALYSIS = 0;
	var MY_LOG = 1;
	var ANALYSIS_PLAY = 2;
	var MY_STATION_NOT_SET = 3;
	var ADVERTISERS = 4;
	var ISCI = 5;
	var AUDIENCERESPONSE = 6;

	var SERVER_TIMEOUT = 10;
	var NO_DATA = 11;
	var CANCELLED = 12;
	var ERROR = 13;

	var RENDERING = 14;

	function noDataWasFound (_msgNumber)
		{
		if (window.frames.loading && window.frames.loading.noDataWasFound)
			window.frames.loading.noDataWasFound(_msgNumber);
		}

	function showErrorMsg (_msgNumber)
		{
		if (window.frames.loading && window.frames.loading.showErrorMsg)
			window.frames.loading.showErrorMsg(_msgNumber);
		}

	function showMessage (_msgNumber)
		{
		if (window.frames.loading && window.frames.loading.showMessage)
			window.frames.loading.showMessage(_msgNumber);
		}

	function cancelQuery ()
		{
		if (window.frames.loading && window.frames.loading.cancelQuery)
			window.frames.loading.cancelQuery();
		}

	function stopClock ()
		{
		if (window.frames.loading && window.frames.loading.stopClock)
			window.frames.loading.stopClock();
		}

	function showCancelButton ()
		{
		if (window.frames.loading && window.frames.loading.showCancelButton)
			window.frames.loading.showCancelButton();
		}

	function startKeepAlive ()
		{
		if (window.frames.loading && window.frames.loading.startKeepAlive)
			window.frames.loading.startKeepAlive();
		}

	function stopKeepAlive ()
		{
		if (window.frames.loading && window.frames.loading.stopKeepAlive)
			window.frames.loading.stopKeepAlive();
		}

	// data-checking functions

	function isBlank (_string)
		{
		return (_string == "undefined" || _string == null || _string == "") ? true : false;
		}

	function pluralOf (_text, _count)
		{
		return (_count == 1 || _count == -1) ? _text : _text + "s";
		}

	function formatNumber (_number)
		{
		result = String(_number);
		var ptr = (result.indexOf(".") >= 0) ? result.indexOf(".") - 3 : result.length - 3;
		while (ptr > 0)
			{
			result = result.substr(0, ptr) + "," + result.substr(ptr);
			ptr -= 3;
			}
		return result;
		}

	function unformatNumber (_number)
		{
		result = String(_number).replace(",", "");
		return result;
		}

	function round (_number, _decimalPlaces)
		{
		var pot = Math.pow(10, _decimalPlaces);
		var result = parseInt(_number * pot) / pot;
		var dec = String(result).indexOf(".");
		if (dec < 0)
			{
			dec = String(result).length;
			result = String(result) + ".";
			}
		result = String(result) + "00000000000000000000000000000000000000000000";
		result = result.substr(0, dec + 1 + _decimalPlaces);
		//result = result.substr(0, dec) + ".<sup>" + result.substr(dec + 1, _decimalPlaces) + "</sup>";

		return result;
		}

	function toPercent (_portion, _total)
		{
		var pct = (_total) ? (parseInt(_portion / _total * 1000) / 10) : 0;
		var whichPie = (pct < 1 ? "0" : (pct > 99 ? "13" : parseInt(pct / 100 * 12) + 1));
		pct = round(pct, 1);
		returnVal = pct + "%&nbsp;<img src='images/pct" + whichPie + ".png' align=absmiddle>";
		return returnVal;
		}

// - - - D I R E C T   C O D E - - -

	/*
	if (document.all.body)
		{
		document.all.body.oncontextmenu = "alert('context 1'); event.returnValue = false; return false";
		document.all.body.ondrop = "event.returnValue = false; return false";
		}
	*/

