/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// general purpous functions
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function SetInnerHTMLUsingDomParser(element, text)
{

	// try and use the DOMParser
	var parser = new DOMParser();

	var doc = parser.parseFromString('<div xmlns="http://www.w3.org/1999/xhtml">' + text + '<\/div>', 'application/xhtml+xml');

	for (var i = 0; i < doc.childNodes.length; ++i)
	{
		element.appendChild(document.importNode(doc.childNodes[i], true));
	}
}

function SetInnerHTMLUsingInnerHTML(element, text)
{

	element.innerHTML = text;
}

var SetInnerHtmlDelegate;

function SetInnerHtml(element, text)
{
	return SetInnerHTMLUsingInnerHTML(element, text);

//	// if we've already decided on the function to use
//	if (SetInnerHtmlDelegate)
//	{
//		// use it
//		SetInnerHtmlDelegate(element, text);
//	}
//	else
//	{
//		// assume we're using the DOM parser
//		SetInnerHtmlDelegate = SetInnerHTMLUsingDomParser;

//		try
//		{
//			// try and use it
//			SetInnerHtmlDelegate(element, text);
//		}
//		catch (ex)
//		{
//			// if there's an error, use innerHTML
//			SetInnerHtmlDelegate = SetInnerHTMLUsingInnerHTML;
//			SetInnerHtmlDelegate(element, text);
//		}
//	}
}

// delete all the children of an element
function DeleteChildren(parent)
{

	parent.innerHTML = "";
	return;

	// remove any current elements
	while (parent.lastChild)
	{
		parent.removeChild(parent.lastChild);
	}
}


// this function is used to call a callback function in the given context
function CallInContext(context, method, callbackData)
{
	var callContext = new Object();
	callContext.context = context;
	callContext.method = method;
	callContext.callbackData = callbackData;
	return (function(a1, a2, a3, a4, a5) { if (callContext.callbackData !== undefined && callContext.callbackData !== null) callContext.method.call(callContext.context, callContext.callbackData, a1, a2, a3, a4, a5); else callContext.method.call(callContext.context, a1, a2, a3, a4, a5); });
}


// execute a Json request
function ExecuteJsonRequest(dataObject, additionalArguments, callbackContext, callbackFunction, callbackData)
{

	var postValues = "";
	if (dataObject)
	{
		var strDataObject = JSON.stringify(dataObject);

		postValues = "dataObject=" + strDataObject;
	}

	if (additionalArguments)
	{
		if (postValues.length > 0)
		{
			postValues += "&";
		}

		postValues += additionalArguments;
	}

	$.post(document.location.href, postValues, CallInContext(callbackContext, callbackFunction, callbackData), "json");
}

// execute a Json request
function ExecuteHtmlRequest(dataObject, additionalArguments, callbackContext, callbackFunction, callbackData)
{

	var postValues = "";
	if (dataObject)
	{
		var strDataObject = JSON.stringify(dataObject);

		postValues = "dataObject=" + strDataObject;
	}

	if (postValues.length > 0)
	{
		postValues += "&";
	}

	if (additionalArguments)
	{
		postValues += "&" + additionalArguments;
	}

	$.post(document.location.href, postValues, CallInContext(callbackContext, callbackFunction), "html");
}

function DumpObjectToElement(ob, displayDiv)
{

	for (var key in ob)
	{
		var value = ob[key];

		var container = document.createElement("div");
		container.className = "OutputHotelDetails";

		var keySpan = document.createElement("span");
		keySpan.className = "OutputKey";
		container.appendChild(keySpan);
		SetInnerHtml(keySpan, key);

		var keyValue = document.createElement("span");
		keyValue.className = "OutputValue";
		container.appendChild(keyValue);

		displayDiv.appendChild(container);

		if (value !== null && typeof (value) == "object")
		{
			// create a new control
			var childDiv = document.createElement("div");
			container.appendChild(childDiv);

			var currentIndent = 0;
			if (displayDiv.style.marginLeft.length > 2)
			{
				currentIndent = parseInt(displayDiv.style.marginLeft.substr(0, displayDiv.style.marginLeft.length - 2), 10);
			}
			childDiv.style.marginLeft = currentIndent + 50 + "px";

			DumpObjectToElement(value, childDiv);
		}
		else
		{
			// output this value
			if (value === null)
			{
				SetInnerHtml(keyValue, "<null>");
			}
			else
			{
				SetInnerHtml(keyValue, value);
			}

			var clearingDiv = document.createElement("div");
			clearingDiv.className = "ClearBoth";
			container.appendChild(clearingDiv);

		}
	}
}

function StringCompare(a, b)
{
	if (a > b) return 1;
	else return -1;
}

function SetSelectNumbers(select, start, end, selected)
{
	select.options.length = 0;

	for (var iValue = start; iValue <= end; iValue++)
	{
		select.options.add(new Option(iValue, iValue));
	}

	if (selected >= start && selected <= end)
	{
		select.value = selected;
	}
}

// repeat a string (or character) n times
function Repeat(s, n)
{
	if (!n) return "";
	if (n & 1) return Repeat(s, n - 1) + s;
	var t = Repeat(s, n >> 1);
	return t + t;
}

function isNumeric(value)
{
	if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
	return true;
}


