﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace('BBWebMethods.Common');

BBWebMethods.Common.FormSubmitter = function () {
}
BBWebMethods.Common.FormSubmitter.get = function (target, action, paramsMap) {
	BBWebMethods.Common.FormSubmitter._call(target, action, paramsMap, "get");
}
BBWebMethods.Common.FormSubmitter.post = function (target, action, paramsMap) {
	BBWebMethods.Common.FormSubmitter._call(target, action, paramsMap, "post");
}
BBWebMethods.Common.FormSubmitter._call = function (target, action, paramsMap, method) {
	try {
		var postedForm = document.createElement("FORM");
		postedForm.setAttribute("target", target);
		postedForm.setAttribute("action", action);
		postedForm.setAttribute("method", method);

		if (paramsMap != null)
			for (var id in paramsMap) {
				var value = paramsMap[id];
				if (value) {
					var param = document.createElement("INPUT");
					param.setAttribute("id", "paramsid");
					param.setAttribute("name", id);
					param.setAttribute("value", value);
					param.setAttribute("type", "hidden");
					postedForm.appendChild(param);
				}
			}

		// insert new form after asp.net form
		var aspnetForm = document.forms[0];
		aspnetForm.parentNode.insertBefore(postedForm, aspnetForm.nextSibling);

		//alert(postedForm.elements["params"].value);
		postedForm.submit();

		// remove new form
		aspnetForm.parentNode.removeChild(postedForm);
	}
	catch (e) {
		alert(e.message);
	}
}
BBWebMethods.Common.FormSubmitter.registerClass('BBWebMethods.Common.FormSubmitter');

BBWebMethods.Common.AsyncPostbackQueueing = function () {
}
BBWebMethods.Common.AsyncPostbackQueueing.init = function () {
	// init queues
	var pbQueue = new Array();
	var argsQueue = new Array();

	var initRequestCallback = function InitializeRequestHandler(sender, args) {
		if (prm.get_isInAsyncPostBack()) {
			// we're busy right now, cancel async postback, let it run later
			args.set_cancel(true);
			pbQueue.push(args.get_postBackElement().id);
			argsQueue.push(document.forms[0].__EVENTARGUMENT.value);
		}
	};

	var endRequestCallback = function EndRequestHandler(sender, args) {
		// as long as there's something waiting
		if (pbQueue.length > 0) {
			// run the async postback now
			__doPostBack(pbQueue.shift(), argsQueue.shift());
		}
	};

	// setup callbacks
	var prm = Sys.WebForms.PageRequestManager.getInstance();
	prm.add_initializeRequest(initRequestCallback);
	prm.add_endRequest(endRequestCallback);
}
BBWebMethods.Common.AsyncPostbackQueueing.registerClass('BBWebMethods.Common.AsyncPostbackQueueing');

BBWebMethods.Common.WebScriptProxyHelper = function () {
}
BBWebMethods.Common.WebScriptProxyHelper._isSecure = null;
BBWebMethods.Common.WebScriptProxyHelper.init = function () {
	if (location.protocol == 'https:')
		BBWebMethods.Common.WebScriptProxyHelper._isSecure = true;
	else
		BBWebMethods.Common.WebScriptProxyHelper._isSecure = false;
}
BBWebMethods.Common.WebScriptProxyHelper.ensureCorrectScheme = function (proxy) {
	if (!proxy || proxy.__BB_ensuredCorrectScheme)
		return;
	var path = proxy.get_path();
	if (!path)
		return;
	if (BBWebMethods.Common.WebScriptProxyHelper._isSecure)
		proxy.set_path(path.replace(/^http:/i, 'https:'));
	else
		proxy.set_path(path.replace(/^https:/i, 'http:'));
	path.__BB_ensuredCorrectScheme = true;
}
BBWebMethods.Common.WebScriptProxyHelper.registerClass('BBWebMethods.Common.WebScriptProxyHelper');
BBWebMethods.Common.WebScriptProxyHelper.init();

if (Sys && Sys.Application) {
	Sys.Application.notifyScriptLoaded();
}

