// Ver 1.0.100312 mod activesite

var ajaxTimeout = 30000; // 30 secs

function ajaxSendRequest(url, callback, postData) {

	var req = createXMLHTTPObject();

	if (!req) return;

	var method = (postData) ? "POST" : "GET";

	req.open(method, url, true);
	req.setRequestHeader("User-Agent", "XMLHTTP/1.0");

	if (postData) {

		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.setRequestHeader("Content-Length", postData.length);

	}

	req.setRequestHeader("If-Modified-Since", new Date(0)); // January 1, 1970

	req.onreadystatechange = function () {

		if (req.readyState != 4) return;

		clearTimeout(reqTimeout);

		if (req.aborted)
			return;

		document.body.style.cursor = "default";

		if ((req.status != 200) && (req.status != 304) && (req.status != 0)) {

			alert("HTTP error: " + req.status);
			return false;

		}

		callback(req);

	}

	if (req.readyState == 4) return;

	document.body.style.cursor = "wait";

	req.send(postData);

	var reqTimeout = setTimeout(function() { req.aborted = true; req.abort(); alert("Request timed out"); document.body.style.cursor = "default"; }, ajaxTimeout);

}

var XMLHttpFactories = [
	function () { return new XMLHttpRequest() },
	function () { return new ActiveXObject("Msxml2.XMLHTTP") },
	function () { return new ActiveXObject("Msxml3.XMLHTTP") },
	function () { return new ActiveXObject("Microsoft.XMLHTTP") }
];

function createXMLHTTPObject() {

	var xmlhttp = false;

	for (var i = 0; i < XMLHttpFactories.length; i++) {

		try {

			xmlhttp = XMLHttpFactories[i]();

		} catch (e) {

			continue;

		}

		break;

	}

	if (!xmlhttp)
		alert("We are sorry, but your browser is not AJAX compatible. We recomends to use Internet Exploer 7.0 and above, or FireFox 3.0 and above, or Opera 9.0 and above...");

	return xmlhttp;

}

function ajaxExeScripts(el) {

	if (el.getElementsByTagName)
		var els = el.getElementsByTagName("SCRIPT");
	else
		if (!el.children)
			var els = el.children;
		else
			return;

	for (var i = 0; i < els.length; i++)
		if ((els[i].tagName == "SCRIPT") && (els[i].src != "")) {

			var head = document.getElementsByTagName("head")[0];

			if (head) {

				var scripts = head.getElementsByTagName("SCRIPT");
				var j;
				var found = false;						

				if (scripts)
					for (j = 0; j < scripts.length; j++)
						if (scripts[j].src == els[i].src)
							found = true;

				if (!found) {

					var script = document.createElement("SCRIPT");

					script.type = "text/javascript";
					script.src = els[i].src;
					head.appendChild(script);

				}

			}

		}

	setTimeout(function() { // Unexpected error fix

		for (var i = 0; i < els.length; i++)
			if (els[i].tagName == "SCRIPT")
				if (els[i].innerHTML != "") {

					var scrp = els[i].innerHTML.replace(/^\s+|\s+$/g, "");

					if (scrp)
						if (window.execScript)
							window.execScript(scrp);
						else
							if (window.parent.eval)
								window.parent.eval(scrp);

				} else;
			else
				ajaxExeScripts(els[i]);

	}, 100);

}

// --------------------------------------------------------------------------

function ajaxInterface(action, actionclass, url) {

	this.url = url ? url : "";
	this.action = action ? action : "";
	this.actionclass = actionclass ? actionclass : "";
	this.pdata = new Array();
	this.pdatastr = "";
	
	var execscripts = true;
	var container = null;
	var finfunc = null;
	var errorHandler = null;

	this.setParam = function(name, value) {

		this.pdata[name] = value;

	}

	this.getParam = function(name) {

		if (this.pdata[name])
			return this.pdata[name];
		else
			return NULL;

	}

	this.parseForm = function(form) {

		if (!form)
			return;

		if (form.elements.length > 0)
			for (var i = 0; i < form.elements.length; i++)
				if (form.elements[i].type == "checkbox")
					this.pdata[form.elements[i].name] = form.elements[i].checked ? 1 : 0;
				else
					if (form.elements[i].type == "radio")
						if (form.elements[i].checked)
							this.pdata[form.elements[i].name] = form.elements[i].value;
						else;
					else
						this.pdata[form.elements[i].name] = form.elements[i].value;

	}

	this.getPData = function() {

		var res = "src=ajax&";

		for (var name in this.pdata)
			res += name + "=" + encodeURIComponent(this.pdata[name]) + "&";

		res += this.pdatastr;

		return res;

	}

	this.setContainer = function(id) {

		container = document.getElementById(id);

	}

	this.setExec = function(flag) {

		execscripts = flag;

	}

	this.setOnFinish = function(func) {

		finfunc = func;

	}

	this.doRequest = function() {

		if (this.action)
			this.setParam("action", this.action);

		if (this.actionclass)
			this.setParam("actionclass", this.actionclass);

		ajaxSendRequest(this.url, this.onReady, this.getPData());

	}

	this.setErrorHandler = function(func) {

		errorHandler = func;

	}

	this.onReady = function(req) {

		var doc = null;
		var resText = req.responseText;

		if (req.responseXML) {

			var doc = req.responseXML.documentElement;

			if (doc)
				resText = getNodeAttribute(doc, "result");

		}

		if (resText.indexOf("Error:") == 0) {

			if (errorHandler)
				errorHandler(resText.substr(7));
			else
				alert(resText.substr(7));

			return;

		}

		if (resText.indexOf("You have an error") == 0) {

			if (errorHandler)
				errorHandler(resText, doc);
			else
				alert(resText);

			return;

		}

		if (container)
			if (resText == "OK") {

				if (finfunc)
					finfunc(resText, doc);

				return true;

			} else {

				if ("innerHTML" in container)
					container.innerHTML = resText;

				if (finfunc)
					finfunc(resText, doc);

				if (execscripts)
					ajaxExeScripts(container);

			}
		else
			if (resText == "OK") {

				if (finfunc)
					finfunc(resText, doc);

				return true;

			} else {

				if (errorHandler)
					errorHandler(resText, doc);
				else
					alert(resText);

				return;

			}

	}

}

// --------------------------------------------------------------------------
// Additional functions

function getNodeAttribute(el, name) {

	for (var x = 0; x < el.attributes.length; x++)
		if (el.attributes[x].nodeName.toLowerCase() == name)
			return el.attributes[x].nodeValue;

}

function getOffsetLeft(el) {

	var offset = el.offsetLeft;

	while (el.offsetParent) {

		el = el.offsetParent;

		if (el.currentStyle) {

			if (el.currentStyle.position == "absolute")
				break;

			if (el.currentStyle.overflow != "visible")
				break;

		}

		offset += el.offsetLeft;

	}

	return offset;

}

function getOffsetTop(el) {

	var offset = el.offsetTop;

	while (el.offsetParent) {

		el = el.offsetParent;

		if (el.currentStyle) {

			if (el.currentStyle.position == "absolute")
				break;

			if (el.currentStyle.overflow != "visible")
				break;

		}

		offset += el.offsetTop;

	}

	return offset;

}

function getParentForm(el) {

	if (!el)
		return false;

	if (el.tagName == "FORM")
		return el;
	else
		if (el.parentElement)
			return getParentForm(el.parentElement);
		else
			return false;

}

function getInsideForm(el) {

	var form = null;

	if (!el)
		return false;

	if (el.tagName == "FORM")
		return el;
	else
		if (el.children) {

			for (var i = 0; i < el.children.length; i++)
				if (form = getInsideForm(el.children[i]))
					return form;
				else;

			return false;

		} else
			return false;

}

function addHandler(object, event, handler) {

	if (typeof object.addEventListener != 'undefined')
		object.addEventListener(event, handler, false);
	else if (typeof object.attachEvent != 'undefined')
		object.attachEvent('on' + event, handler);
	else
		throw "Incompatible browser";

}

function removeHandler(object, event, handler) {

	if (typeof object.removeEventListener != 'undefined')
		object.removeEventListener(event, handler, false);
	else if (typeof object.detachEvent != 'undefined')
		object.detachEvent('on' + event, handler);
	else
		throw "Incompatible browser";

}

// --------------------------------------------------------------------------
// Windows

function ajaxWindow(actionscript, actionclass) {

	var actionscript = actionscript; // Executor
	var actionclass = actionclass ? actionclass : ""; // Action Class
	var body = document.body;
	var posx = 0;
	var posy = 0;
	var width = 0;
	var height = 0;
	var nearmousex = false;
	var nearmousey = false;
	var div = null;
	var isShadow = true;
	var onClose = null;
	var onApply = null;
	var doCloseWindow = this.closeWindow;
	var doShowWindow = this.showWindow;
	var addData = new Array();
	var modal = true;
	var divm = null;
	var attachedForm = null;
	var wasAction = false;
	var errorHandler = null;

	this.setErrorHandler = function(func) {

		errorHandler = func;

	}

	this.setActionClass = function(svalue) {

		actionclass = svalue;

	}

	this.setActionScript = function(svalue) {

		actionscript = svalue;

	}

	this.getActionScript = function(svalue) {

		return actionscript;

	}

	this.setSize = function(w, h) {

		width = w ? w : "auto";
		height = h ? h : "auto";

	}

	this.setEnabledShadow = function(setS) {

		isShadow = setS;

	}

	this.setPosition = function(x, y) {

		nearmousex = false;
		nearmousey = false;
		posx = x;
		posy = y;

		if (x == "mouse") {

			nearmousex = true;
			posx = 0;

			if (!y)
				y = "mouse";

		}

		if (y == "mouse") {

			nearmousey = true;
			posy = 0;

		} 

	}

	this.getContainer = function() {

		return div;

	}

	this.setOnClose = function(func) {

		onClose = func;

	}

	this.setOnApply = function(func) {

		onApply = func;

	}

	this.setParam = function(name, value) {

		addData[name] = value;

	}

	this.getParam = function(name, value) {

		return addData[name];

	}

	this.setModal = function(value) {

		modal = value;

	}

	this.attachForm = function(form) {

		attachedForm = form;

	}

	this.calcZIndex = function() {

		if (!document.all)
			return 2;

		var all = document.all.tags("DIV");
		var zIndex = 0;
		var i;

		for (i = 0; i < all.length; i++)
			if (all[i].style)
				if (all[i].style.zIndex > zIndex) zIndex = all[i].style.zIndex + 1;

		return zIndex + 1;

	}

	this.getUniqueId = function() {

		var dateObject = new Date();
		var uniqueId = 
			dateObject.getFullYear() + '' + 
			dateObject.getMonth() + '' + 
			dateObject.getDate() + '' + 
			dateObject.getTime();

		return uniqueId;

	}

	this.createAjax = function(form, action) {

		if (form) {

			var form = getParentForm(form);

			if (!action)
				action = form.action ? form.action.value : "";

		}

		if (!actionscript)
			alert("Error: URL for ajax action is undefined");

		var Ajax = new ajaxInterface(action, actionclass);

		Ajax.url = actionscript;

		if (form)
			Ajax.parseForm(form);

		if (attachedForm)
			Ajax.parseForm(attachedForm);

		Ajax.setErrorHandler(this.errorHandlerLocal);

		for (var pname in addData)
			Ajax.setParam(pname, addData[pname]);

		return Ajax;

	}

	this.errorHandlerLocal = function(res) {

		if (div)
			if (!div.innerHTML)
				closeWindow();

		if (errorHandler)
			errorHandler(res);
		else
			alert(res);

	}

	detectPosition = function(div) {

		if (!div)
			return;

		var posx = getOffsetLeft(div);
		var posy = getOffsetTop(div);

		var rs = div.style;

		if (posx + div.offsetWidth + 10 > document.body.clientWidth)
			rs.left = document.body.clientWidth - div.offsetWidth - 25;
		else
			rs.left = posx;

		rs.top = posy;

	}

	this.repaintBlock = function() {

		if (!div)
			return;

		detectPosition(div);

		if (getOffsetLeft(div) + div.offsetWidth + 3 < document.body.clientWidth)
			if (isShadow)
				if (window.makeDropShadow)
					makeDropShadow(div, "#000000", 3);

		if (window.resizeFrame)
			window.resizeFrame();

	}
         
	this.openWindow = function(action, id, pdata) {

		var Ajax = this.createAjax(null, action);

		if (id)
			Ajax.setParam("id", id);

		if (pdata)
			if ((pdata.tagName) && (pdata.tagName == "form"))
				Ajax.parseForm(pdata);
			else
				Ajax.pdatastr = pdata;

		if (this.div)
			if (div.removeNode)
				div.removeNode(true);
			else
				if (div.parentNode)
					   div.parentNode.removeChild(div);

		this.closeWindow();

		if (modal) { // Prepare modal window closer

			divm = document.createElement("DIV");
			divm.style.width = document.body.clientWidth;
			divm.style.height = document.body.clientHeight;
			divm.style.position = "absolute";
			divm.style.top = 0 + "px";
			divm.style.left = 0 + "px";
			divm.style.zIndex = this.calcZIndex();
			divm.style.backgroundImage = "url(../images/pixel.gif)";

			body.appendChild(divm);

		}

		div = document.createElement("DIV");
		div.className = "clsAjaxWindow";
		div.id = this.getUniqueId();

		if (window.addEventListener) // Mozilla, Netscape, Firefox
			div.addEventListener("onresize", this.repaintBlock, false);
		else
			div.attachEvent("onresize", this.repaintBlock);

		var rs = div.style;

		posx = nearmousex ? mousex : posx == "center" ? document.body.clientWidth / 2 : posx;
		posy = nearmousey ? mousey : posy == "center" ? document.body.clientHeight / 2 : posy;

		rs.position = "absolute";
		rs.display = "none";
		rs.left = posx + "px";
		rs.top = posy + "px";
		rs.width = width ? height : "auto";
		rs.height = height ? height : "auto";
		rs.zIndex = this.calcZIndex();

		body.appendChild(div);

		Ajax.setContainer(div.id);
		Ajax.setOnFinish(this.showWindow);
		Ajax.doRequest();

	}

	this.showWindow = function() {

		if (!div)
			return;

		var isvisible = div.style.display == "block";

		div.style.display = "block";

		if ((!isvisible) && (!wasAction)) {

			var rs = div.style;

			if (posx + div.offsetWidth + 10 > document.body.clientWidth)
				rs.left = document.body.clientWidth - div.offsetWidth - 25;
			else
				rs.left = posx;

			rs.top = posy;

			if (window.makeDropShadow)
				makeDropShadow(div, "#000000", 3);

			if (window.resizeFrame)
				window.resizeFrame();

		}

		wasAction = false;

	}

	this.closeWindow = function() {

		if (onClose)
			onClose();

		if (!div)
			return;

		if (isShadow)
			if (window.removeDropShadow)
			        removeDropShadow(div);

		if (div.removeNode)
			div.removeNode(true);
		else
			if (div.parentNode)
				   div.parentNode.removeChild(div);

		if (divm.removeNode)
			divm.removeNode(true);
		else
			if (divm.parentNode)
				   divm.parentNode.removeChild(divm);

		if (window.resizeFrame)
			window.resizeFrame();

	}

	this.applyWindow = function(clicker, action) {

		var Ajax = this.createAjax(clicker, action);

		if (!div) {

			div = document.createElement("DIV");
			div.id = this.getUniqueId();
			body.appendChild(div);

		}

		Ajax.setContainer(div.id);

		doCloseWindow = this.closeWindow;
		doShowWindow = this.showWindow;

		Ajax.setOnFinish(this.checkActionWindow);
		Ajax.doRequest();

		return false;

	}

	this.actionWindow = function(clicker, action) {

		wasAction = true;

		this.applyWindow(clicker, action);

	}

	this.checkActionWindow = function(res) {

		if (!div)
			return;

		if ((!div.innerHTML) || (res == "OK"))
			doCloseWindow();
		else
			doShowWindow();

		if (onApply)
			onApply();

	}

	closeWindow = this.closeWindow;

}

// --------------------------------------------------------------------------
// Container

function ajaxContainer(name, actionscript, action, actionclass, autoload) {

	var name = name;
	var actionscript = actionscript; // Executor
	var action = action ? action : ""; // Action
	var actionclass = actionclass ? actionclass : ""; // Action Class
	var form = null;
	var searchform = true;
	var addData = new Array();
	var onApply = null;
	var onLoad = null;
	var errorHandler = null;
	var isIE = document.all || window.opera;

	this.setErrorHandler = function(func) {

		errorHandler = func;

	}

	this.setAction = function(svalue) {

		action = svalue;

	}

	this.setActionClass = function(svalue) {

		actionclass = svalue;

	}

	this.setActionScript = function(svalue) {

		actionscript = svalue;

	}

	this.setForm = function(sform) {

		form = sform ? sform : null;

	}

	this.setContainer = function(sname) {

		name = sname;

	}

	this.setSearchForm = function(svalue) {

		searchform = svalue ? true : false;

	}

	this.setParam = function(name, value) {

		addData[name] = value;

	}

	this.getParam = function(name, value) {

		return addData[name];

	}

	this.setOnApply = function(func) {

		onApply = func;

	}

	this.setOnLoad = function(func) {

		onLoad = func;

	}

	this.loadData = function(curaction, pdata, onapply) {

		var Ajax = new ajaxInterface(curaction ? curaction : action, actionclass);
		var container = document.getElementById(name) ? document.getElementById(name) : null;

		if (!container)
			return;

		Ajax.url = actionscript;

		if (pdata)
			if (pdata.tagName)
				Ajax.parseForm(getParentForm(pdata));
			else
				Ajax.pdatastr = pdata;

		if (form)
			Ajax.parseForm(form);

		if (searchform)
			if (cform = getInsideForm(container))
				Ajax.parseForm(cform);

		for (var pname in addData)
			Ajax.setParam(pname, addData[pname]);

		if (window.adm_loading)
			if ((container.innerHTML == "") || (container.innerHTML == "&nbsp;")) {

				var lp = document.createElement("DIV");

				lp.className = "clsLoading";
				lp.innerHTML = window.adm_loading + "...";

				container.appendChild(lp);

				if (window.resizeFrame)
					window.resizeFrame();

			}

		Ajax.setContainer(name);

		if (errorHandler)
			Ajax.setErrorHandler(errorHandler);

		if (onapply)
			Ajax.setOnFinish(onapply);
		else
			if (onLoad)
				Ajax.setOnFinish(onLoad);
			else
				if (window.resizeFrame)
					Ajax.setOnFinish(resizeFrame);

		Ajax.doRequest();

	}

	this.applyAction = function(curaction, id) {

		this.loadData(curaction, id != undefined ? "id=" + id : "", onApply);

	}

	this.doAction = function(curaction, curactionclass, form) {

		if (form) {

			var form = getParentForm(form);

			if (!curaction)
				curaction = form.action ? form.action.value : "";

			this.setForm(form);

		}

		this.setAction(curaction);
		this.setActionClass(curactionclass);
		this.loadData();

	}

	this.doActionID = function(curaction, curactionclass, id) {

		this.setParam("id", id);
		this.setAction(curaction);
		this.setActionClass(curactionclass);
		this.loadData();

	}

	this.applyContainer = function(form, curaction) {

		if (form) {

			var form = getParentForm(form);

			if (!curaction)
				curaction = form.action ? form.action.value : "";

		}

		this.loadData(curaction, form, onApply);

		return false;

	}

	this.redirectForm = function(form, target, finfunc) {

		if (!target) {

			alert("Error: Traget is undefined");
			return false;

		}

		var frame = document.getElementById(target);

		if (!frame)
			return false;

		var Ajax = new ajaxInterface(action, actionclass);

		Ajax.setContainer(target);

		if (finfunc)
			Ajax.setOnFinish(finfunc);

		if (isIE)
			sframe = frames[target];
		else
			sframe = document.getElementById(target).contentWindow;

		if (form.target != target) {

			form.target = target;
			frame.attachEvent("onload", function() { var req = function() { var responseText; }; req.responseText = sframe.document.body.innerText; Ajax.onReady(req); });

		}

		return true;

	}

	if (autoload == undefined)
		if (window.pushOnLoadProcess)
			pushOnLoadProcess(this.loadData);

}
