var site_timeout;
var redirURL;
var wr;
var _NSDM = {
	request:function(obj) {
		_NSDMDialogs.show_page();
		windowed = false;
		switch (obj) {
			case 'site_search':
				_NSDM.goto(_search_url + '?' + $('site_search').serialize(), '_self');
				break;
			case 'forum_search':
				_NSDM.goto(_forums_url + '?' + $('forum_search').serialize(), '_self');
				break;
			case 'forums_post':
				_NSDM.send(_forums_url, $(obj).serialize(), 1);
				break;
		}
	},
	timed_out:function() {
		clearTimeout(site_timeout);
		_NSDMDialogs.hide_loading();
		_NSDMDialogs.error('<b>Maximum server execution time exceeded!</b><br /><br />The server encountered an error or simply timed out.<br />Please try again in a few minutes or contact the system administrator.');
	},
	send:function(url, values, method, returnObj) {
		site_timeout = setTimeout("_NSDM.timed_out();", 25000);
		new Ajax.Request(url, {
			method: 'post',
			parameters: values,
			onSuccess: function(transport) {
				clearTimeout(site_timeout);
				var results = transport.responseText;
				switch(method){
					case 1:
						_NSDM.request_results(results);
						break;
					case 2:
						$(returnObj).update(results);
						break;
					
					case 3:
						returnObj(results);
						break;
						
					case 4:
						// do nothing
						break;
				}
			},
			onFailure: function() {
				alert('Something went wrong...');
			}
		})
	},
	request_results:function(res) {
		_NSDMDialogs.hide_loading();
		var response = [];
		response = _NSDM.parse_response(res);
		if (response[0] == "error") {
			_NSDMDialogs.error(response[1]);
		} else if (response[0] == "success") {
			_NSDMDialogs.success(response[1]);
			if (response[2] == 0) {setTimeout("_NSDMDialogs.success_close();",1000);}
			else if (response[2] == 'null') {setTimeout("_NSDM.reload_it();", response[3]);}
			else {
				redirURL = response[2];
				setTimeout("_NSDM.redirect();", response[3]);
			}
		} else {
			_NSDMDialogs.error(res);
		}
	},
	parse_response:function(data){
		var response_array = [];

		var matches = data.match(/<status>(.*?)<\/status>/);
		response_array[0] = matches[1];

		var matches = data.match(/<confirmation>(.*?)<\/confirmation>/);
		response_array[1] = _NSDM.txt_replace(unescape(matches[1]), '+', ' ');

		var matches = data.match(/<redirect>(.*?)<\/redirect>/);
		response_array[2] = unescape(matches[1]);
		
		var matches = data.match(/<timer>(.*?)<\/timer>/);
		response_array[3] = unescape(matches[1]);

		return response_array;

	},
	framed_response:function(response) {
		clearTimeout(site_timeout);
		_NSDMDialogs.hide_loading();
		var result = response.split('|^|');
		
		info = _NSDM.txt_replace(unescape(result[1]), '+', ' ');
		url = unescape(result[2]);
		
		if (result[0] == "error") {
			_NSDMDialogs.error(info)
		} else if (result[0] == "success") {
			_NSDMDialogs.success(info);
			window.parent.redirURL = url;
			setTimeout("window.parent._NSDM.redirect()", result[3]);
		} else {
			_NSDMDialogs.error(response)
		}
	},
	site_updater:function(obj,data,url) {
		new Ajax.PeriodicalUpdater(obj, url, {
			parameters: data,
			method: 'get',
			frequency: 3,
			decay: 2
		});
	},
	goto:function(_url, _target) {
		window.open(_url, _target);
	},
	jumper:function(_url) {
		this.goto(_url, '_self');
	},
	txt_replace:function(str, replace_1, replace_2) {
		var strReplaceAll = str;
		var intIndexOfMatch = strReplaceAll.indexOf(replace_1);
		while (intIndexOfMatch != -1) {
			strReplaceAll = strReplaceAll.replace(replace_1, replace_2);
			intIndexOfMatch = strReplaceAll.indexOf(replace_1);
		}
		return (strReplaceAll)
	},
	redirect:function() {
		_NSDM.goto(redirURL, '_self');
	},
	show_hide:function(_id) {
		(($(_id).style.display == 'block') ? $(_id).hide() : $(_id).show());
	},
	reload_it:function() {
		document.location.reload()
	},
	txt_cnt:function(field, countfield, maxlimit) {
		if ($(field).value.length > maxlimit) {
			$(field).value = $(field).value.substring(0, maxlimit);
		} else {
			$(countfield).value = maxlimit - $(field).value.length;
		}
	},
	submit_it:function(obj){
		site_timeout = setTimeout("_NSDM.timed_out();", 25000);
		_NSDMDialogs.show_page();
		$(obj).submit();	
	},
	donate:function(_url) {
		window.open(_url, 'ajax_iframe');
	}
}