﻿////////////////////////////////////////////////////////////////////////

connect(window.document, "onkeypress", CreateLoggingPane);

////////////////////////////////////////////////////////////////////////
/* From mochikit trac at http://trac.mochikit.com/wiki/ParsingHtml */
function evalHTML(value) 
{
	if (typeof(value) != 'string') return null;
	value = MochiKit.Format.strip(value);
	if (value.length == 0) return null;
	var parser = MochiKit.DOM.DIV();
	var html = MochiKit.DOM.currentDocument().createDocumentFragment();
	var child;
	parser.innerHTML = value;
	while((child = parser.firstChild))
	{
		html.appendChild(child)
	}
	return html;
}
////////////////////////////////////////////////////////////////////////
// From http://www.jasonbunting.com/blahg/PermaLink,guid,1533adbf-074c-4278-b5be-b42a15a79393.aspx
function Format(formatString, args)
{
   var countOfSuppliedFormatItems = arguments.length - 1;
   for (var i = 0; i < countOfSuppliedFormatItems; i++)
   {
      var replacementString = arguments[i+1].toString();
      var formatItemRegex = new RegExp("\\{[" + i.toString() + "]{1}\\}" , "g");
      formatString = formatString.replace(formatItemRegex, replacementString);
   }
   return formatString;
}
// add our formatting method to the native String object
String.Format = Format;
////////////////////////////////////////////////////////////////////////
// Logging Helpers
function ListObjectKeys(obj)
{
	var totalKeys = keys(obj).length;
	var id = (obj.id != null) ? obj.id : "";
	logWarning(Format("The following {0} items are keys on the object whose Id, if it has one, is '{1}'.", totalKeys, id));
	for(var i = 0; i < totalKeys; i++)
	{
		logWarning(keys(obj)[i]);
	}
}
function ListObjectKeyValues(obj)
{
   for(k in obj)
   {
      log(obj[k]);
   }
}
function ListObjectKeysAndValues(obj)
{
	for(k in obj)
	{
		logWarning(k, ": ", obj[k]);
	}
}
////////////////////////////////////////////////////////////////////////
function CreateLoggingPane(evt)
{
	if(evt.key().string == "~")
	{
		createLoggingPane(true);
		evt.stop();
	}	
}
////////////////////////////////////////////////////////////////////////
// These are for use when getting values to be passed to C# code (via 
// AJAX) where parameters are nullable types (e.g. if a C# method takes 
// a nullable int as a param, you should use the GetNullableNumericValue() 
// function below to prepare the value to be passed to the server-side code
function GetNullableNumericValue(value)
{
	return (value == "" || value.toString() == "NaN") ? null : value;
}
function GetNullableDateTimeValue(value)
{
	var dateTime = (value == "" || value.toString() == "NaN") ? null : new Date(value);
	return (dateTime.toString() != "NaN") ? dateTime : null;
}
///////////////////////////////////////////////////////////////////////////////////////
// code originally taken from Telligent's Community Server and left unmodified this
// code helps determine positioning of elements of the page relative to each other
function getposOffset(elem, offsetType) {
   var totalOffset = (offsetType == "left") ? elem.offsetLeft : elem.offsetTop;
   var parentElem = elem.offsetParent;
   while (parentElem != null) {
      totalOffset = (offsetType == "left") ? totalOffset + parentElem.offsetLeft : totalOffset + parentElem.offsetTop;
      parentElem = parentElem.offsetParent;
   }
   return totalOffset;
}

// ideas originally taken from Telligent's Community Server 
// (was ToggleSearchMenu()) but modified quite a bit by Jason Bunting for use here
function SetAssociatedElementDisplay(/* bool */ makeVisible, elementToShow, elementToAssociateWith) {
   elementToShow.style.left = getposOffset(elementToAssociateWith, "left");
   elementToShow.style.top = getposOffset(elementToAssociateWith, "top") + elementToAssociateWith.offsetHeight - 1;
   if(makeVisible == true) {
      blindDown(elementToShow, {duration:".15"});
   } else {
      blindUp(elementToShow, {duration:".05"});
   }
}
///////////////////////////////////////////////////////////////////////////////////////





////////////////////////////////////////////////////////////////////////




////////////////////////////////////////////////////////////////////////




////////////////////////////////////////////////////////////////////////




////////////////////////////////////////////////////////////////////////




///////////////////////////////////////////////////////////////////
//					Drop Down List (DDL) Helper Functions				  //
///////////////////////////////////////////////////////////////////

// this will remove from the DropDownList indicated by the 
// dropDownListId that option which has the value of optionValue
function DDLRemoveOption(dropDownList, optionValue)
{
	if(dropDownList != null && dropDownList.options != null)
	{
		for(i = 0; i < dropDownList.options.length; i++)
		{
			if(dropDownList.options[i].value == optionValue)
			{
				dropDownList.options[i] = null;
				break;
			}
		}
	}
}
// nameValueArray is expected to be an array of objects that have a 'name' and 'value' property
function DDLReplaceOptions(dropDownList, nameValueArray)
{
	if(dropDownList != null && dropDownList.options != null && nameValueArray != null && nameValueArray.length > 0)
	{
		DDLClearOptions(dropDownList);
		DDLAppendOptions(dropDownList, DDLConvertToOptions(nameValueArray));
	}
}
function DDLClearOptions(dropDownList)
{
	if(dropDownList != null) dropDownList.length = 0;
}

function DDLAppendOptions(dropDownList, options)
{
	if(options != null && options.length > 0 && dropDownList != null)
	{
		for(var i = 0; i < options.length; i++)
		{
			dropDownList.options[dropDownList.options.length] = options[i];
		}
	}
}
function DDLConvertToOptions(nameValueArray)
{
	var options = new Array();
	if(nameValueArray != null && nameValueArray.length > 0)
	{
		for(i = 0; i < nameValueArray.length; i++)
		{
			options[i] = new Option(nameValueArray[i].Name, nameValueArray[i].Value);
		}
   }
   return options;
}
function DDLPrependOptions(dropDownList, options)
{
	if(options != null && options.length > 0 && dropDownList != null)
	{
		var arrayOfOriginalOptions = MochiKit.Iter.list(dropDownList.options);
		DDLClearOptions(dropDownList);
		DDLAppendOptions(dropDownList, options);
		DDLAppendOptions(dropDownList, arrayOfOriginalOptions);
   }
}

function getAllByNodeAttributeValue(attribute, value) {
	var elements = [];
	MochiKit.Base.nodeWalk(MochiKit.DOM.currentDocument(), function (elem) {
		if((elem[attribute] != null 
		   && elem[attribute].toUpperCase() == value.toUpperCase()) 
		   || (elem.attributes != null 
		   && elem.attributes.getNamedItem(attribute) != null 
		   && elem.attributes.getNamedItem(attribute).nodeValue.toUpperCase() == value.toUpperCase())) 
		{
			elements.push(elem);
		}
		return elem.childNodes;
	});
	return elements;
}

function getAllByNodeAttribute(attribute) {
	var elements = [];
	MochiKit.Base.nodeWalk(MochiKit.DOM.currentDocument(), function (elem) {
		if(elem[attribute] != null) {
			elements.push(elem);
		}
		return elem.childNodes;
	});
	return elements;
}

function IsVisible(node)
{
   return (node.style.display != "none");
}

function ChangeVisibility(node, shouldBeVisible)
{
   if(shouldBeVisible)
      showElement(node);
   else
      hideElement(node);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Functions to provide semantic meaning for key codes
if (typeof(MochiKitWidget) == 'undefined') {
    MochiKitWidget = function() {};
}

MochiKitWidget.KeyEvent = function (evt) {
    this.ctor(evt);
};
MochiKitWidget.KeyEvent.prototype = new MochiKitWidget();

MochiKit.Base.update(MochiKitWidget.KeyEvent.prototype, 
{
   /* our member variables, as it were */
   m_Event: null,
   m_NumberKeys : null,
   m_BasicOperationKeys : null,

   /* our 'constructor'... */
   ctor: function(evt)
   {      
      m_Event = evt;
      
      m_NumberKeys = new Object();
      m_NumberKeys["keypress"] = [48,49,50,51,52,53,54,55,56,57];
      m_NumberKeys["keydown"] = [48,49,50,51,52,53,54,55,56,57,96,97,98,99,100,101,102,103,104,105];
      
      m_BasicOperationKeys = new Object();
      m_BasicOperationKeys["keypress"] = [0];
      m_BasicOperationKeys["keydown"] = [8,9,17,27,35,36,37,39,45,46,144];
      m_BasicOperationKeys["keydown"]["shift"] = [9];
      m_BasicOperationKeys["keydown"]["ctrl"] = [65,67,86,88];
      m_BasicOperationKeys["keydown"]["alt"] = [9];
   },
   
   FilterForInteger : function() {
      if(!(this.IsNumber() || this.IsBasicOperation())) {
         m_Event.stop();
      }
   },
   
   IsNumber : function () {
      return some(m_NumberKeys[m_Event.type()], this.IsMatch);
   },
   
   IsBasicOperation : function () {
      return some(m_BasicOperationKeys[m_Event.type()], this.IsMatch);
   },
   
   IsMatch : function (keyCode) {
      return (keyCode == m_Event.key().code);
   }
});
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////







// discover other ways of doing this to see what is best
// e.g. maybe a set of arrays of acceptable key codes 
// would be better, and joining those for various things
function IsNumberKeyCode(keyCode)
{
   return (keyCode >= 48 && keyCode <= 57);
}
function IsEnterKeyCode(keyCode)
{
   return (keyCode == 12);
}
function IsTabKeyCode(keyCode)
{
   return (keyCode == 9);
}
function IsPeriodKeyCode(keyCode)
{
   return (keyCode == 190);
}
function IsDecimalKeyCode(keyCode)
{
   return (keyCode == 110);
}
function IsBackspaceKeyCode(keyCode)
{
   return (keyCode == 8);
}
function IsDeleteKeyCode(keyCode)
{
   return (keyCode == 46);
}
function IsRArrowKeyCode(keyCode)
{
   return (keyCode == 39);
}
function IsLArrowKeyCode(keyCode)
{
   return (keyCode == 37);
}
function IsLeftOrRightKeyCode(keyCode)
{
   return (IsLArrowKeyCode(keyCode) || IsRArrowKeyCode(keyCode));
}
function IsBasicOperationKeyCode(keyCode)
{
   return (IsEnterKeyCode(keyCode) || IsTabKeyCode(keyCode) || IsBackspaceKeyCode(keyCode) || IsDeleteKeyCode(keyCode) || IsLeftOrRightKeyCode(keyCode));
}

function getAllByNodeAttributeValue(attribute, value) {
	var elements = [];
	MochiKit.Base.nodeWalk(MochiKit.DOM.currentDocument(), function (elem) {
		if((elem[attribute] != null 
		   && elem[attribute].toUpperCase() == value.toUpperCase()) 
		   || (elem.attributes != null 
		   && elem.attributes.getNamedItem(attribute) != null 
		   && elem.attributes.getNamedItem(attribute).nodeValue.toUpperCase() == value.toUpperCase())) 
		{
			elements.push(elem);
		}
		return elem.childNodes;
	});
	return elements;
}

function getAllByNodeAttribute(attribute) {
	var elements = [];
	MochiKit.Base.nodeWalk(MochiKit.DOM.currentDocument(), function (elem) {
		if(elem[attribute] != null) {
			elements.push(elem);
		}
		return elem.childNodes;
	});
	return elements;
}

function IsVisible(node)
{
   return (node.style.display != "none");
}

function ChangeVisibility(node, shouldBeVisible)
{
   if(shouldBeVisible)
      showElement(node);
   else
      hideElement(node);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Functions to provide semantic meaning for key codes
if (typeof(MochiKitWidget) == 'undefined') {
    MochiKitWidget = function() {};
}

MochiKitWidget.KeyEvent = function (evt) {
    this.ctor(evt);
};
MochiKitWidget.KeyEvent.prototype = new MochiKitWidget();

MochiKit.Base.update(MochiKitWidget.KeyEvent.prototype, 
{
   /* our member variables, as it were */
   m_Event: null,
   m_NumberKeys : null,
   m_BasicOperationKeys : null,

   /* our 'constructor'... */
   ctor: function(evt)
   {      
      m_Event = evt;
      
      m_NumberKeys = new Object();
      m_NumberKeys["keypress"] = [48,49,50,51,52,53,54,55,56,57];
      m_NumberKeys["keydown"] = [48,49,50,51,52,53,54,55,56,57,96,97,98,99,100,101,102,103,104,105];
      
      m_BasicOperationKeys = new Object();
      m_BasicOperationKeys["keypress"] = [0];
      m_BasicOperationKeys["keydown"] = [8,9,17,27,35,36,37,39,45,46,144];
      m_BasicOperationKeys["keydown"]["shift"] = [9];
      m_BasicOperationKeys["keydown"]["ctrl"] = [65,67,86,88];
      m_BasicOperationKeys["keydown"]["alt"] = [9];
   },
   
   FilterForInteger : function() {
      if(!(this.IsNumber() || this.IsBasicOperation())) {
         m_Event.stop();
      }
   },
   
   IsNumber : function () {
      return some(m_NumberKeys[m_Event.type()], this.IsMatch);
   },
   
   IsBasicOperation : function () {
      return some(m_BasicOperationKeys[m_Event.type()], this.IsMatch);
   },
   
   IsMatch : function (keyCode) {
      return (keyCode == m_Event.key().code);
   }
});
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////







// discover other ways of doing this to see what is best
// e.g. maybe a set of arrays of acceptable key codes 
// would be better, and joining those for various things
function IsNumberKeyCode(keyCode)
{
   return (keyCode >= 48 && keyCode <= 57);
}
function IsEnterKeyCode(keyCode)
{
   return (keyCode == 12);
}
function IsTabKeyCode(keyCode)
{
   return (keyCode == 9);
}
function IsPeriodKeyCode(keyCode)
{
   return (keyCode == 190);
}
function IsDecimalKeyCode(keyCode)
{
   return (keyCode == 110);
}
function IsBackspaceKeyCode(keyCode)
{
   return (keyCode == 8);
}
function IsDeleteKeyCode(keyCode)
{
   return (keyCode == 46);
}
function IsRArrowKeyCode(keyCode)
{
   return (keyCode == 39);
}
function IsLArrowKeyCode(keyCode)
{
   return (keyCode == 37);
}
function IsLeftOrRightKeyCode(keyCode)
{
   return (IsLArrowKeyCode(keyCode) || IsRArrowKeyCode(keyCode));
}
function IsBasicOperationKeyCode(keyCode)
{
   return (IsEnterKeyCode(keyCode) || IsTabKeyCode(keyCode) || IsBackspaceKeyCode(keyCode) || IsDeleteKeyCode(keyCode) || IsLeftOrRightKeyCode(keyCode));
}
