Geonetric = {
	String: {
		Empty: "",
		IsNullOrEmpty: function(value)
		{
			if ((value == null) || (value == undefined) || (value == Geonetric.String.Empty))
			{
				return true;
			}
			return false;
		},
		NormalizeSpace: function(textToReplace)
		{
			return textToReplace.replace(/^\s*|\s(?=\s)|\s*$/g, "");
		}
	},
	Page: {
		VirtualDirectory: "/",
		ResolveUrl: function(url)
		{
			if (Geonetric.String.IsNullOrEmpty(url))
			{
				return url;
			}
			if ((url.length >= 2) && (url.substring(0, 2) == "~/"))
			{
				return Geonetric.Page.VirtualDirectory + url.substring(2);
			}
			return url;
		},
		DisplayError: function(err)
		{
			if (typeof DisplayMessage == "function")
			{
				DisplayMessage(err.message ? err.message : err);
			}
			else
			{
				alert(err.message ? err.message : err);
			}
		}
	},
	DOM: {
		GetElementById: function(elementId)
		{
			return document.getElementById(elementId);
		},
		GetElementByClass: function(className)
		{
			return $("." + className);
		},
		GetClassesOnElement: function(domElement)
		{
			var classes = new Array();
			if (domElement != null && !Geonetric.String.IsNullOrEmpty(domElement.className))
			{
				var matches = domElement.className.split(" ");
				var counter = 0;
				for (counter = 0; counter < matches.length; counter++)
				{
					var match = matches[counter].trim();
					if (!Geonetric.String.IsNullOrEmpty(match))
					{
						classes.push(match);
					}
				}
			}
			return classes;
		},
		IsDomElementVisible: function(obj)
		{
			if (obj == document)
				return true;
			if (!obj)
				return false;
			if (!obj.parentNode)
				return false;
			if (obj.style)
			{
				if (obj.style.display == 'none') return false;
				if (obj.style.visibility == 'hidden') return false;
			}

			// Try the computed style in a standard way
			if (window.getComputedStyle)
			{
				var style = window.getComputedStyle(obj, "");
				if (style.display == 'none') return false;
				if (style.visibility == 'hidden') return false;
			}

			// Or get the computed style using IE's proprietary way
			var style = obj.currentStyle;
			if (style)
			{
				if (style['display'] == 'none') return false;
				if (style['visibility'] == 'hidden') return false;
			}

			return Geonetric.DOM.IsDomElementVisible(obj.parentNode);
		},
		GetTextContent: function(node)
		{
			var textNodes = new Array();
			Geonetric.DOM.AddAllTextNodes(node, textNodes);
			var sb = new Array();
			var counter = 0;
			for (counter = 0; counter < textNodes.length; counter++)
			{
				var text = textNodes[counter];
				sb.push(text.data);
			}
			var returnedString = sb.join("");
			var output = new Array();
			var i = 0;
			for (i = 0; i < returnedString.length; i++)
			{
				var currentChar = returnedString.charCodeAt(i);
				if (currentChar != 160)
				{
					output.push(String.fromCharCode(currentChar));
				}
			}

			return output.join("").trim();
		},
		AddAllTextNodes: function(node, texts)
		{
			var i = 0;
			for (i = 0; i < node.childNodes.length; i++)
			{
				var child = node.childNodes[i];
				if (child.nodeType == 1)
				{
					Geonetric.DOM.AddAllTextNodes(child, texts);
				}
				if (child.nodeType == 3)
				{
					texts.push(child);
				}
			}
		}
	},
	Diagnostics: {
		IS_DEBUG: false,
		Log: function(obj)
		{
			if (Geonetric.Diagnostics.IS_DEBUG)
			{
				DisplayMessage(obj);
			}
		},
		//LogStackTrace : function ()
		//{
		//	var trace = Geonetric.Diagnostics.GetGetStackTraceFunction()();
		//
		//	Geonetric.Diagnostics.Log(trace.join("<br /><br />"));
		//}
		GetGetStackTraceFunction: function()
		{

			var mode;
			try { (0)() }
			catch (e)
			{
				mode = e.stack ? 'Firefox' : window.opera ? 'Opera' : 'Other';
			}

			switch (mode)
			{
				case 'Firefox': return function()
				{
					try { (0)() } catch (e)
					{
						return e.stack.replace(/^.*?\n/, '').
							replace(/(?:\n@:0)?\s+$/m, '').
							replace(/^\(/gm, '{anonymous}(').
							split("\n");
					}
				};

				case 'Opera': return function()
				{
					try { (0)() } catch (e)
					{
						var lines = e.message.split("\n"),
							ANON = '{anonymous}',
							lineRE = /Line\s+(\d+).*?in\s+(http\S+)(?:.*?in\s+function\s+(\S+))?/i,
							i, j, len;

						for (i = 4, j = 0, len = lines.length; i < len; i += 2)
						{
							if (lineRE.test(lines[i]))
							{
								lines[j++] = (RegExp.$3 ?
									RegExp.$3 + '()@' + RegExp.$2 + RegExp.$1 :
									ANON + RegExp.$2 + ':' + RegExp.$1) +
									' -- ' + lines[i + 1].replace(/^\s+/, '');
							}
						}

						lines.splice(j, lines.length - j);
						return lines;
					}
				};

				default: return function()
				{
					var curr = arguments.callee.caller,
						FUNC = 'function', ANON = "{anonymous}",
						fnRE = /function\s*([\w\-$]+)?\s*\(/i,
						stack = [], j = 0,
						fn, args, i;

					while (curr)
					{
						fn = fnRE.test(curr.toString()) ? RegExp.$1 || ANON : ANON;
						args = stack.slice.call(curr.arguments);
						i = args.length;

						while (i--)
						{
							switch (typeof args[i])
							{
								case 'string': args[i] = '"' + args[i].replace(/"/g, '\\"') + '"'; break;
								case 'function': args[i] = FUNC; break;
							}
						}

						stack[j++] = fn + '(' + args.join() + ')';
						curr = curr.caller;
					}

					return stack;
				};
			}

		},
		LogStackTrace: function()
		{
			var trace = Geonetric.Diagnostics.GetGetStackTraceFunction()();

			Geonetric.Diagnostics.Log(trace.join("<br /><br />"));
		}
	}
}

$id = Geonetric.DOM.GetElementById;
$class = Geonetric.DOM.GetElementByClass;
$isVisible = Geonetric.DOM.IsDomElementVisible;
String.prototype.NormalizeSpace = function(){ return Geonetric.String.NormalizeSpace(this); };


/* GEONETRIC ONSUBMIT EVENT */
// EXAMPLE: Geonetric_DoPostBackPreparationFunctions.push(function(item1,item2){alert(1);});

window.Geonetric_FormSubmitPrepare = function()
{
	var prepCount = 0;
	for(prepCount = 0; prepCount<window.Geonetric_DoPostBackPreparationFunctions.length; prepCount++)
	{
		try
		{
			window.Geonetric_DoPostBackPreparationFunctions[prepCount]();
		}
		catch(err)
		{
		}
	}
};

window.Geonetric_DoPostBack = function(_df,_e0)
{
	Geonetric_FormSubmitPrepare();
	Geonetric_OldDoPostBack(_df,_e0);
};

window.Geonetric_FormSubmitHandler = function(e,_dd)
{
	var ret=true;
	if ( !window.Geonetric_HandlingFormSubmit )
	{
		window.Geonetric_HandlingFormSubmit=true;

		Geonetric_FormSubmitPrepare();
		if ( !_dd&&window.Geonetric_OldSubmitHandler )
		{
			ret=Geonetric_OldSubmitHandler(e);
		}
		window.Geonetric_HandlingFormSubmit=false;
	}
	return ret;
};

$(function()
{
	if (document.forms[0] && document.forms[0].onsubmit != window.Geonetric_FormSubmitHandler)
	{
		window.Geonetric_OldSubmitHandler=document.forms[0].onsubmit;
		document.forms[0].onsubmit=window.Geonetric_FormSubmitHandler;
	}

	if (window.__doPostBack && window.__doPostBack != window.Geonetric_DoPostBack)
	{
		window.Geonetric_OldDoPostBack=window.__doPostBack;
		window.__doPostBack=window.Geonetric_DoPostBack;
	}
});

/* END GEONETRIC ONSUBMIT EVENT */


/* GEONETRIC VALIDATION ONCHAGE EVENTS EVENT */
Geonetric.Validation = {
	TextBox: {
		WireUpPreProcessChangeEvent: function(elId, preProcessFunc)
		{
			var el = document.getElementById(elId);
			var oldPointer = el.onchange;
			el.onchange = function(event)
			{
				return Geonetric.Validation.TextBox.HandleChangeEventWithEvent(event, preProcessFunc, oldPointer, el);
			};
		},
		HandleChangeEventWithEvent: function(event, preProcessFunc, pointer, el)
		{
			preProcessFunc(el.value, el);
			return pointer(event);
		}
	},
	Globalization: {
		UpdateValidation: function(controlId, controlInfo)
		{
			var isRequired = controlInfo._isRequired;
			var labelText = controlInfo._labelText;
			var helpText = controlInfo._helpText;
			var maxLength = controlInfo._maxLength;
			var regex = controlInfo._regex;
			var ignoreCase = controlInfo._ignoreCase;

			var targetControl = $id(controlId);

			var targetRequiredValidator = $id(targetControl.requiredvalidator);
			ValidatorEnable(targetRequiredValidator, isRequired);
			$(targetRequiredValidator).hide();
			//targetRequiredValidator.hasFired = true;

			var targetRegexValidator = $id(targetControl.regexvalidator);
			targetRegexValidator.validationexpression = Geonetric.String.IsNullOrEmpty(regex) ? ".*" : new RegExp(regex, (ignoreCase ? "i" : ""));

			ValidatorEnable(targetRegexValidator, false);
			ValidatorEnable(targetRegexValidator, true);

			Geonetric.Validation.Globalization._updateLabelNode(targetControl, isRequired, labelText, helpText, maxLength);
		},
		_updateLabelNode: function(targetControl, isRequiredWhen, labelText, helpText, maxLength)
		{
			var label = targetControl.parentNode.getElementsByTagName('label')[0];
			var childNodeCollection = label.childNodes;
			var i = 0;
			for (i = childNodeCollection.length - 1; i >= 0; i--)
			{
				if (childNodeCollection[i].nodeType != 2)
				{
					label.removeChild(childNodeCollection[i]);
				}
			}

			if (isRequiredWhen)
			{
				var span = label.ownerDocument.createElement('span');
				span.setAttribute('class', 'Required');
				var requiredTextNode = label.ownerDocument.createTextNode(' * ');
				span.appendChild(requiredTextNode);
				label.appendChild(span);
			}
			var textNode = label.ownerDocument.createTextNode(labelText);
			label.appendChild(textNode);
			if (!Geonetric.String.IsNullOrEmpty(helpText))
			{
				var br = label.ownerDocument.createElement('br');
				label.appendChild(br);
				var small = label.ownerDocument.createElement('small');
				var smallTextNode = label.ownerDocument.createTextNode(helpText);
				small.appendChild(smallTextNode);
				label.appendChild(small);
			}
			if (targetControl.attributes.getNamedItem('maxlength') != null)
			{
				targetControl.removeAttribute('maxlength');
			}
			if (maxLength > 0)
			{
				targetControl.setAttribute('maxlength', maxLength.toString());
			}
		}

	}
}
/* END GEONETRIC VALIDATION ONCHAGE EVENTS EVENT */
