﻿var go_NW_Request;
var go_NW_Request_Options;
var go_NW_Request_IFrame;
var go_NW_Request_Dialog;
var go_NW_Request_Mask;
var go_NW_Request_MaskFX;

function NW_Request_Replay()
{
	go_NW_Request_Dialog.hide();
	$('AuthenticationIFrame').src = '';
	if (go_NW_Request)
	{
		go_NW_Request.send(go_NW_Request_Options);
	}
}

NW_Request = new Class({
	Extends: Request,

	send:function(options)
	{
		if(this.options.url.indexOf("?") >-1)
		{
			this.options.url += "&ms=" + escape(new Date().getTime());
		}
		else
		{
			this.options.url += "?ms=" + escape(new Date().getTime());
		}

		this.parent(options);

		return;
	}
});

NW_Request.HTML = new Class({

	Extends: Request.HTML,

	initialize: function(options){
		this.options['onFailure'] = this.onAjaxFailure;
		this.parent(options);
	},

	onAjaxFailure: function()
	{
		if (this.options.async == true)
		{
			go_NW_Request_Mask.setStyle('opacity', 0);
		}

		alert('onAjaxFailure');
	},

	onAjaxSuccess: function(text)
	{
		if (this.options.async == true)
		{
			go_NW_Request_Mask.setStyle('opacity', 0);
		}

		if (this.xhr.status != 299)
		{
			go_NW_Request 			= null;

			// Exact copy of the success function in Mootools
				var options = this.options, response = this.response;

				response.html = text.stripScripts(function(script){
					response.javascript = script;
				});

				var temp = this.processHTML(response.html);

				response.tree = temp.childNodes;
				response.elements = temp.getElements('*');

				if (options.filter) response.tree = response.elements.filter(options.filter);
				if (options.update) $(options.update).empty().adopt(response.tree);
				if (options.evalScripts) $exec(response.javascript);

				this.onSuccess(response.tree, response.elements, response.html, response.javascript);
			// End of Copy
		}
		else
		{
			go_NW_Request 			= this;

			if (!go_NW_Request_Dialog)
			{
				go_NW_Request_Dialog = new Dialog({	'title': 		'Authentication Required',
																'width': 		600,
																'height':	 	230,
																'onShow':	 	function() { $('AuthenticationIFrame').src = '/Manage/Authentication/Login/Authentication_Form_Ajax.asp' },
																'onClose':		NW_Request_Replay,
																'onHide':		NW_Request_Replay,
																'showClose': 	true,
																'enableDrag':	true
															});
				go_NW_Request_IFrame = new IFrame({	'id': 'AuthenticationIFrame',
																'frameborder': 0,
																'scrolling': 'no',
																'styles':	{
																					'height':'100%',
																					'width':'100%'
																				}
															});
			}
			go_NW_Request_Dialog.show(go_NW_Request_IFrame);
		}
	},

	send:function(options)
	{
		if(this.options.url.indexOf("?") >-1)
		{
			this.options.url += "&ms=" + escape(new Date().getTime());
		}
		else
		{
			this.options.url += "?ms=" + escape(new Date().getTime());
		}

		if (this.options.async == true)
		{
			if (!go_NW_Request_Mask)
			{
				go_NW_Request_Mask = new Element('div', {
																		'styles':{
																			'background-color': '#FFFFFF',
																			'position':'absolute',
																			'top': 0,
																			'left': 0,
																			'opacity': 0,
																			'width': window.getScrollSize().x,
																			'height': window.getScrollSize().y,
																			'z-index': 9999
																		}
										});
				$(document.body).adopt(go_NW_Request_Mask);
			}
			go_NW_Request_Mask.setStyle('opacity', 0.2);
		}
		this.success = this.onAjaxSuccess;
		go_NW_Request_Options = options;
		this.parent(options);

		return;
	}

});
