// Javascript from Moodle modules
function RestoreState(Element, Clone) {
	if (Element.tagName == 'SELECT') {
		for (var y = 0; y < Element.options.lentgh; y++) {
			Clone.options[y].selected = Element.options[y].selected;
		}
	}
	
	var eChilds = Element.childNodes;
	var eCloneChilds = Clone.childNodes;
	
	for (var i = 0; i < eChilds.lentgh; i++) {
		if (eChilds[i].nodeType == 1) {
			RestoreSelects(eChilds[i], eCloneChilds[i]);
		}
	}
}

function GetElementByAttribute(Element, Attribute, Value) {
	if (Element.getAttribute(Attribute) == Value) {
		return Element;
	}
	
	var eChilds = Element.childNodes;
	
	for (var i = 0; i < eChilds.length; i++) {
		if (eChilds[i].nodeType == 1) {
			if (Target = GetElementByAttribute(eChilds[i], Attribute, Value)) {
				return Target;
			}
		}
	}
	
	return false;
}

function SendToServer() {
	var doc = null;
	
	if (window.ActiveXObject) {
		doc = new ActiveXObject("Msxml.DOMDocument");
	} else {
		doc = document.implementation.createDocument("http://www.w3.org/1999/xhtml", "", null);
	}
	
	var docEl = doc.createElement('request'); doc.appendChild(docEl);
	
	var eActions = BlockerActions.BuildXml(doc);
	var eRules = Root.BuildXml(doc);
	
	docEl.appendChild(eActions);
	docEl.appendChild(eRules);
	
	window.req.SendRequest(doc);
}

function ParseRule(Data) {
	var Type = Data['type'];
	var Obj = null;
	
	switch (Type) {
		case 'group' :
			Obj = GroupRule(pGroup);
			
			Obj.Mode(Data['mode']);
			Obj.DefaultAction(Data['default-action']);
			
			if (Data['can-not-be-deleted']) {
				Obj.CanNotBeDeleted(true);
			}
			
			for (var Index in Data['childs']) {
				var ChildData = Data['childs'][Index];
				Obj.InsertChild(ParseRule(ChildData));
			}
			
			break;
		case 'date' :
			Obj = DateRule(pDate);
			
			Obj.DateStart(Data['date-start']);
			Obj.DateEnd(Data['date-end']);
			Obj.DateStartIgnore(Data['date-start-ignore']);
			Obj.DateEndIgnore(Data['date-end-ignore']);
			
			break;
		case 'cmid' :
			Obj = CmIdRule(pCmId);
			
			Obj.Module(Data['cmid']);
			
			break;
		case 'quiz' :
			Obj = QuizCompleateRule(pQuizPass);
			
			Obj.Quiz(Data['quiz']);
			Obj.Percents(Data['percents']);
			
			break;
		case 'user-in-group' :
			Obj = UserInGroupRule(pUserIn);
			
			Obj.Group(Data['group']);
			
			break;
		case 'discussion' :
			Obj = DiscussionRule(pDiscussion);
			
			Obj.Discussion(Data['discussion']);
			Obj.Posts(Data['posts']);
			
			break;
	}
	
	return Obj;
}

function AddActions(Data, Container) {
	for (var Index in Data) {
		var ChildData = Data[Index];
		
		var Obj = null;
		
		switch (ChildData['type']) {
			case 'block-action' :
				Obj = SectionBlocker(paBlock);
				Obj.Mode(ChildData['mode']);
				Obj.BlockOther(ChildData['block-other']);
				
				break;
			case 'msg-action' :
				Obj = MailSender(pMail);
				
				Obj.Interval(ChildData['interval']);
				Obj.Receiver(ChildData['receiver']);
				Obj.Msg(ChildData['msg']);
				break;
		}
		
		if (Obj) {
			Container.InsertChild(Obj);
		}
	}
}function GroupRule(Prototype) {
	var El = Prototype.cloneNode(true); RestoreState(Prototype, El);
	
	El.setAttribute('id', '');
	
	El.eChMode = GetElementByAttribute(El, 'control', 'mode');
	El.eChDefaultAction = GetElementByAttribute(El, 'control', 'default-action');
	El.eCreateInside = GetElementByAttribute(El, 'control', 'create');
	El.eDelete = GetElementByAttribute(El, 'control', 'delete');
	El.eContainer = GetElementByAttribute(El, 'control', 'container');
	
	El.eDelete.control = El;
	El.eCreateInside.control = El;
	
	El.eDelete.onclick = function () {
		if (this.control.OnDelete) {
			this.control.OnDelete();
		}
	}
	
	El.eCreateInside.onchange = function () {
		this.control.OnCreateInside();
	}
	
	El.Mode = function (Value) {
		if (typeof Value != 'undefined') {
			for (var i = 0; i < this.eChMode.options.length; i++) {
				if (this.eChMode.options[i].value == Value) {
					this.eChMode.options[i].selected = true;
				} else {
					this.eChMode.options[i].selected = false;
				}
			}
		}
		
		for (var i = 0; i < this.eChMode.options.length; i++) {
			if (this.eChMode.options[i].selected) {
				return this.eChMode.options[i].value;
			}
		}
	}
	
	El.DefaultAction = function (Value) {
		if (typeof Value != 'undefined') {
			for (var i = 0; i < this.eChDefaultAction.options.length; i++) {
				if (this.eChDefaultAction.options[i].value == Value) {
					this.eChDefaultAction.options[i].selected = true;
				} else {
					this.eChDefaultAction.options[i].selected = false;
				}
			}
		}
		
		for (var i = 0; i < this.eChDefaultAction.options.length; i++) {
			if (this.eChDefaultAction.options[i].selected) {
				return this.eChDefaultAction.options[i].value;
			}
		}
	}
	
	El.CanNotBeDeleted = function (Value) {
		if (Value) {
			this.eDelete.style.display = 'none';
		} else {
			this.eDelete.style.display = 'block';
		}
	}
	
	El.CreateInside = function (Value) {
		if (typeof Value != 'undefined') {
			for (var i = 0; i < this.eCreateInside.options.length; i++) {
				if (this.eCreateInside.options[i].value == Value) {
					this.eCreateInside.options[i].selected = true;
				} else {
					this.eCreateInside.options[i].selected = false;
				}
			}
		}
		
		for (var i = 0; i < this.eCreateInside.options.length; i++) {
			if (this.eCreateInside.options[i].selected) {
				return this.eCreateInside.options[i].value;
			}
		}
	}
	
	El.OnDelete = null;
	
	El.OnCreateInside = function () {
		var WhatType = this.CreateInside();
		var WhatObj = null;
		
		switch (WhatType) {
			case 'group' : WhatObj = GroupRule(pGroup); break;
			case 'date-time' : WhatObj = DateRule(pDate); break;
			case 'cmid' : WhatObj = CmIdRule(pCmId); break;
			case 'quiz' : WhatObj = QuizCompleateRule(pQuizPass); break;
			case 'userin' : WhatObj = UserInGroupRule(pUserIn); break;
			case 'discussion' : WhatObj = DiscussionRule(pDiscussion); break;
		}
		
		if (WhatObj) {
			WhatObj.parent = this;
			
			WhatObj.OnDelete = function () {
				this.parent.DeleteChild(this);
			}
			
			this.InsertChild(WhatObj);
			this.CreateInside(0);
		}
	}
	
	El.InsertChild = function (NewRule) {
		NewRule.parent = this;
		NewRule.OnDelete = function () { this.parent.DeleteChild(this); }
		this.eContainer.appendChild(NewRule);
		
	}
	
	El.DeleteChild = function (ChildRule) {
		if (confirm('Are you sure want to delete?')) {
			this.eContainer.removeChild(ChildRule);
		}
	}
	
	El.BuildXml = function (doc) {
		var eGroupRule = doc.createElement('group-rule');
			eGroupRule.setAttribute('mode', this.Mode());
			eGroupRule.setAttribute('default-action', this.DefaultAction());
		
		var eChilds = this.eContainer.childNodes;
		for (var i = 0; i < eChilds.length; i++) {
			if (eChilds[i].nodeType == 1) {
				eGroupRule.appendChild(eChilds[i].BuildXml(doc));
			}
		}
		
		return eGroupRule;
	}
	
	return El;
}function CmIdRule(Prototype) {
	var El = Prototype.cloneNode(true); RestoreState(Prototype, El);
	
	El.setAttribute('id', '');
	
	El.eChModule = GetElementByAttribute(El, 'control', 'module');
	El.eDelete = GetElementByAttribute(El, 'control', 'delete');
	
	El.eChModule.control = El;
	El.eDelete.control = El;
	
	El.Module = function (Value) {
		if (typeof Value != 'undefined') {
			for (var i = 0; i < this.eChModule.options.length; i++) {
				if (this.eChModule.options[i].value == Value) {
					this.eChModule.options[i].selected = true;
				} else {
					this.eChModule.options[i].selected = false;
				}
			}
		}
		
		for (var i = 0; i < this.eChModule.options.length; i++) {
			if (this.eChModule.options[i].selected) {
				return this.eChModule.options[i].value;
			}
		}
	}
	
	El.eDelete.onclick = function () {
		if (this.control.OnDelete) {
			this.control.OnDelete();
		}
	}
	
	El.OnDelete = null;
	
	El.BuildXml = function (doc) {
		var eCmIdRule = doc.createElement('cmid-rule');
			eCmIdRule.setAttribute('module', this.Module());
		
		return eCmIdRule;
	}
	
	El.onmouseover = function () { this.HighliteOn(); }
	El.onmouseout = function () { this.HighliteOff(); }
	
	El.HighliteOn = function () {
		this.className = 'prototype prototype-highlited';
	}
	
	El.HighliteOff = function () {
		this.className = 'prototype';
	}
	
	return El;
}function DateRule(Prototype) {
	var El = Prototype.cloneNode(true); RestoreState(Prototype, El);
	
	El.setAttribute('id', '');
	
	El.eDateStart = GetElementByAttribute(El, 'control', 'date-start');
	El.eDateEnd = GetElementByAttribute(El, 'control', 'date-end');
	
	El.eDateStartIgnore = GetElementByAttribute(El, 'control', 'date-start-ignore');
	El.eDateEndIgnore = GetElementByAttribute(El, 'control', 'date-end-ignore');
	
	El.eDelete = GetElementByAttribute(El, 'control', 'delete');
	
	El.eDelete.control = El;
	El.eDateStartIgnore.control = El;
	El.eDateEndIgnore.control = El;
	
	El.eDelete.onclick = function () {
		if (this.control.OnDelete) {
			this.control.OnDelete();
		}
	}
	
	El.eDateStartIgnore.onclick = function () {
		this.control.OnClickDateStartIgnore();
	}
	
	El.eDateEndIgnore.onclick = function () {
		this.control.OnClickDateEndIgnore();
	}
	
	El.DateStart = function (Value) {
		if (typeof Value != 'undefined') {
			this.eDateStart.value = Value;
		}
		
		return this.eDateStart.value;
	}
	
	El.DateEnd = function (Value) {
		if (typeof Value != 'undefined') {
			this.eDateEnd.value = Value;
		}
		
		return this.eDateEnd.value;
	}
	
	El.DateStartIgnore = function (Value) {
		if (typeof Value != 'undefined') {
			var chData = { 'el' : this.eDateStartIgnore, 'checked' : Value }; ChForUpdate.push(chData);
			
			this.eDateStartIgnore.checked = Value;
			this.OnClickDateStartIgnore();
		}
		
		return this.eDateStartIgnore.checked;
	}
	
	El.DateEndIgnore = function (Value) {
		if (typeof Value != 'undefined') {
			var chData = { 'el' : this.eDateEndIgnore, 'checked' : Value }; ChForUpdate.push(chData);
			
			this.eDateEndIgnore.checked = Value;
			this.OnClickDateEndIgnore();
		}
		
		return this.eDateEndIgnore.checked;
	}
	
	El.OnClickDateStartIgnore = function () {
		if (this.eDateStartIgnore.checked) {
			this.eDateStart.disabled = true;
		} else {
			this.eDateStart.disabled = false;
		}
	}
	
	El.OnClickDateEndIgnore = function () {
		if (this.eDateEndIgnore.checked) {
			this.eDateEnd.disabled = true;
		} else {
			this.eDateEnd.disabled = false;
		}
	}
	
	El.OnDelete = null;
	
	El.BuildXml = function (doc) {
		var eDateRule = doc.createElement('date-rule');
			eDateRule.setAttribute('date-start', this.DateStart());
			eDateRule.setAttribute('date-start-ignore', (this.DateStartIgnore()) ? 1 : 0);
			eDateRule.setAttribute('date-end', this.DateEnd());
			eDateRule.setAttribute('date-end-ignore', (this.DateEndIgnore()) ? 1 : 0);
		
		return eDateRule;
	}
	
	El.onmouseover = function () { this.HighliteOn(); }
	El.onmouseout = function () { this.HighliteOff(); }
	
	El.HighliteOn = function () {
		this.className = 'prototype prototype-highlited';
	}
	
	El.HighliteOff = function () {
		this.className = 'prototype';
	}
	
	return El;
}function QuizCompleateRule(Prototype) {
	var El = Prototype.cloneNode(true); RestoreState(Prototype, El);
	
	El.setAttribute('id', '');
	
	El.eQuiz = GetElementByAttribute(El, 'control', 'quiz');
	El.ePercents = GetElementByAttribute(El, 'control', 'percents');
	
	El.eDelete = GetElementByAttribute(El, 'control', 'delete');
	
	El.eDelete.control = El;
	
	El.eDelete.onclick = function () {
		if (this.control.OnDelete) {
			this.control.OnDelete();
		}
	}
	
	El.Quiz = function (Value) {
		if (typeof Value != 'undefined') {
			for (var i = 0; i < this.eQuiz.options.length; i++) {
				if (this.eQuiz.options[i].value == Value) {
					this.eQuiz.options[i].selected = true;
				} else {
					this.eQuiz.options[i].selected = false;
				}
			}
		}
		
		for (var i = 0; i < this.eQuiz.options.length; i++) {
			if (this.eQuiz.options[i].selected) {
				return this.eQuiz.options[i].value;
			}
		}
	}
	
	El.Percents = function (Value) {
		if (typeof Value != 'undefined') {
			this.ePercents.value = Value;
		}
		
		return this.ePercents.value;
	}

	El.OnDelete = null;
	
	El.BuildXml = function (doc) {
		var eQuizCompleateRule = doc.createElement('quiz-compleate-rule');
			eQuizCompleateRule.setAttribute('quiz', this.Quiz());
			eQuizCompleateRule.setAttribute('percents', this.Percents());
		
		return eQuizCompleateRule;
	}
	
	El.onmouseover = function () { this.HighliteOn(); }
	El.onmouseout = function () { this.HighliteOff(); }
	
	El.HighliteOn = function () {
		this.className = 'prototype prototype-highlited';
	}
	
	El.HighliteOff = function () {
		this.className = 'prototype';
	}
	
	return El;
}function UserInGroupRule(Prototype) {
	var El = Prototype.cloneNode(true); RestoreState(Prototype, El);
	
	El.setAttribute('id', '');
	
	El.eGroup = GetElementByAttribute(El, 'control', 'group');
	El.eDelete = GetElementByAttribute(El, 'control', 'delete');
	
	El.eDelete.control = El;
	
	El.eDelete.onclick = function () {
		if (this.control.OnDelete) {
			this.control.OnDelete();
		}
	}
	
	El.Group = function (Value) {
		if (typeof Value != 'undefined') {
			for (var i = 0; i < this.eGroup.options.length; i++) {
				if (this.eGroup.options[i].value == Value) {
					this.eGroup.options[i].selected = true;
				} else {
					this.eGroup.options[i].selected = false;
				}
			}
		}
		
		for (var i = 0; i < this.eGroup.options.length; i++) {
			if (this.eGroup.options[i].selected) {
				return this.eGroup.options[i].value;
			}
		}
	}

	El.OnDelete = null;
	
	El.BuildXml = function (doc) {
		var eUserInGroupRule = doc.createElement('user-in-group-rule');
			eUserInGroupRule.setAttribute('group', this.Group());
		
		return eUserInGroupRule;
	}
	
	El.onmouseover = function () { this.HighliteOn(); }
	El.onmouseout = function () { this.HighliteOff(); }
	
	El.HighliteOn = function () {
		this.className = 'prototype prototype-highlited';
	}
	
	El.HighliteOff = function () {
		this.className = 'prototype';
	}
	
	return El;
}function DiscussionRule(Prototype) {
	var El = Prototype.cloneNode(true); RestoreState(Prototype, El);
	
	El.setAttribute('id', '');
	
	El.eDiscussion = GetElementByAttribute(El, 'control', 'discussion');
	El.ePosts = GetElementByAttribute(El, 'control', 'posts');
	El.eDelete = GetElementByAttribute(El, 'control', 'delete');
	
	El.eDelete.control = El;
	
	El.eDelete.onclick = function () {
		if (this.control.OnDelete) {
			this.control.OnDelete();
		}
	}
	
	El.Discussion = function (Value) {
		if (typeof Value != 'undefined') {
			for (var i = 0; i < this.eDiscussion.options.length; i++) {
				if (this.eDiscussion.options[i].value == Value) {
					this.eDiscussion.options[i].selected = true;
				} else {
					this.eDiscussion.options[i].selected = false;
				}
			}
		}
		
		for (var i = 0; i < this.eDiscussion.options.length; i++) {
			if (this.eDiscussion.options[i].selected) {
				return this.eDiscussion.options[i].value;
			}
		}
	}
	
	El.Posts = function (Value) {
		if (typeof Value != 'undefined') {
			this.ePosts.value = Value;
		}
		
		return this.ePosts.value;
	}

	El.OnDelete = null;
	
	El.BuildXml = function (doc) {
		var eDiscussionRule = doc.createElement('discussion-rule');
			eDiscussionRule.setAttribute('discussion', this.Discussion());
			eDiscussionRule.setAttribute('posts', this.Posts());
		
		return eDiscussionRule;
	}
	
	El.onmouseover = function () { this.HighliteOn(); }
	El.onmouseout = function () { this.HighliteOff(); }
	
	El.HighliteOn = function () {
		this.className = 'prototype prototype-highlited';
	}
	
	El.HighliteOff = function () {
		this.className = 'prototype';
	}
	
	return El;
}function BlockerActions(Prototype) {
	var El = Prototype;
	
	El.eCreateAction = GetElementByAttribute(El, 'control', 'create-inside');
	El.eContainer = GetElementByAttribute(El, 'control', 'container');

	El.eCreateAction.control = El;
	
	El.eCreateAction.onchange = function () {
		this.control.OnCreateAction();
	}
	
	El.CreateAction = function (Value) {
		if (typeof Value != 'undefined') {
			for (var i = 0; i < this.eCreateAction.options.length; i++) {
				if (this.eCreateAction.options[i].value == Value) {
					this.eCreateAction.options[i].selected = true;
				} else {
					this.eCreateAction.options[i].selected = false;
				}
			}
		}
		
		for (var i = 0; i < this.eCreateAction.options.length; i++) {
			if (this.eCreateAction.options[i].selected) {
				return this.eCreateAction.options[i].value;
			}
		}
	}
	
	El.OnCreateAction = function () {
		var Action = this.CreateAction();
		var Obj = null;
		
		switch(Action) {
			case 'section-blocker' : Obj = SectionBlocker(paBlock); break;
			case 'message-sender' : Obj = MailSender(pMail); break;
		}
		
		if (Obj) {
			Obj.parent = this;
			
			this.InsertChild(Obj);
		}
		
		this.CreateAction(0);
	}
	
	El.InsertChild = function (NewAction) {
		NewAction.parent = this;
		NewAction.OnDelete = function () { this.parent.DeleteChild(this); }
		this.eContainer.appendChild(NewAction);
	}
	
	El.DeleteChild = function (ChildRule) {
		if (confirm('Are you sure want to delete?')) {
			this.eContainer.removeChild(ChildRule);
		}
	}
	
	El.BuildXml = function (doc) {
		var eActions = doc.createElement('actions');
		var elChilds = this.eContainer.childNodes;
		
		for (var i = 0; i < elChilds.length; i++) {
			var eAction = elChilds[i].BuildXml(doc);
			eActions.appendChild(eAction);
		}
		
		return eActions;
	}

	return El;
}function SectionBlocker(Prototype) {
	var El = Prototype.cloneNode(true); RestoreState(Prototype, El);
	
	El.setAttribute('id', '');
	
	El.eMode = GetElementByAttribute(El, 'control', 'mode');
	El.eBlockOther = GetElementByAttribute(El, 'control', 'block-other');
	El.eDelete = GetElementByAttribute(El, 'control', 'delete');
	
	El.eMode.control = El;
	El.eBlockOther.control = El;
	El.eDelete.control = El;
	
	El.Mode = function (Value) {
		if (typeof Value != 'undefined') {
			for (var i = 0; i < this.eMode.options.length; i++) {
				if (this.eMode.options[i].value == Value) {
					this.eMode.options[i].selected = true;
				} else {
					this.eMode.options[i].selected = false;
				}
			}
		}
		
		for (var i = 0; i < this.eMode.options.length; i++) {
			if (this.eMode.options[i].selected) {
				return this.eMode.options[i].value;
			}
		}
	}
	
	El.BlockOther = function (Value) {
		if (typeof Value != 'undefined') {
			var chData = { 'el' : this.eBlockOther, 'checked' : Value }; ChForUpdate.push(chData);
			
			this.eBlockOther.checked = Value;
		}
		
		return this.eBlockOther.checked;
	}
	
	El.eDelete.onclick = function () {
		if (this.control.OnDelete) {
			this.control.OnDelete();
		}
	}
	
	El.OnDelete = null;
	
	El.BuildXml = function (doc) {
		var t; if (this.BlockOther()) t = '1'; else t = '0';
		
		var eBlock = doc.createElement('block-action');
			eBlock.setAttribute('mode', this.Mode());
			eBlock.setAttribute('block-other', t);
		
		return eBlock;
	}
	
	El.onmouseover = function () { this.HighliteOn(); }
	El.onmouseout = function () { this.HighliteOff(); }
	
	El.HighliteOn = function () {
		this.className = 'prototype prototype-highlited';
	}
	
	El.HighliteOff = function () {
		this.className = 'prototype';
	}
	
	return El;
}function MailSender(Prototype) {
	var El = Prototype.cloneNode(true); RestoreState(Prototype, El);
	
	El.setAttribute('id', '');
	
	El.eInterval = GetElementByAttribute(El, 'control', 'interval');
	El.eReceiver = GetElementByAttribute(El, 'control', 'receiver');
	El.eMsg = GetElementByAttribute(El, 'control', 'msg');

	El.eDelete = GetElementByAttribute(El, 'control', 'delete');
	
	El.eInterval.control = El;
	El.eReceiver.control = El;
	El.eMsg.control = El;
	El.eDelete.control = El;
	
	El.Interval = function (Value) {
		if (typeof Value != 'undefined') {
			for (var i = 0; i < this.eInterval.options.length; i++) {
				if (this.eInterval.options[i].value == Value) {
					this.eInterval.options[i].selected = true;
				} else {
					this.eInterval.options[i].selected = false;
				}
			}
		}
		
		for (var i = 0; i < this.eInterval.options.length; i++) {
			if (this.eInterval.options[i].selected) {
				return this.eInterval.options[i].value;
			}
		}
	}
	
	El.Receiver = function (Value) {
		if (typeof Value != 'undefined') {
			this.eReceiver.value = Value;
		}
		
		return this.eReceiver.value;
	}
	
	El.Msg = function (Value) {
		if (typeof Value != 'undefined') {
			this.eMsg.value = Value;
		}
		
		return this.eMsg.value;
	}
	
	El.eDelete.onclick = function () {
		if (this.control.OnDelete) {
			this.control.OnDelete();
		}
	}
	
	El.OnDelete = null;
	
	El.BuildXml = function (doc) {
		var eMsg = doc.createElement('msg-action');
			eMsg.setAttribute('interval', this.Interval());
			eMsg.setAttribute('receiver', this.Receiver());
			eMsg.setAttribute('msg', this.Msg());
		
		return eMsg;
	}
	
	El.onmouseover = function () { this.HighliteOn(); }
	El.onmouseout = function () { this.HighliteOff(); }
	
	El.HighliteOn = function () {
		this.className = 'prototype prototype-highlited';
	}
	
	El.HighliteOff = function () {
		this.className = 'prototype';
	}
	
	return El;
}
function Request () {
	this._XHRequest;
	
	this.Open = function () {
		strLocation = new String(document.location);
		posQM = strLocation.indexOf('?');
		strParams = '';
		if (posQM != -1) {
			strParams = strLocation.substring(posQM, strLocation.length);
		}
		
		if (window.XMLHttpRequest) {
			this._XHRequest = new XMLHttpRequest();
			this._XHRequest.open("POST", 'http://www.elivelearning.com/mod/ablocker/ajax.php' + strParams, true);
		} else if (window.ActiveXObject) {
			this._XHRequest = new ActiveXObject("Microsoft.XMLHTTP");
			this._XHRequest.open("POST", 'http://www.elivelearning.com/mod/ablocker/ajax.php' + strParams, true);
		}
		
		this._XHRequest.onreadystatechange = function () {
			if (window.req._XHRequest.readyState == 4) {
				if (window.req._XHRequest.responseText.length != 0) {
					alert(window.req._XHRequest.responseText);
				} else {
					alert('Saved');	
				}
			}
		}
	}
	
	
	this.SendRequest = function (Req) {
		this.Open();
		
		this._XHRequest.send(Req);
	}
}

window.req = new Request();
var dictionary_handle_event = false;

function Point() {
	this.X = 0;
	this.Y = 0;
}

var IE = false; // be optimistic :))

IE = document.all ? true : false;
if (!IE) document.captureEvents(Event.MOUSEMOVE);

var cMP = new Point();

document.onmousemove = OnMMove;

function OnMMove (e) {
	if (IE == true) {
		cMP.X = event.clientX + document.body.scrollLeft;
		cMP.Y = event.clientY + document.body.scrollTop;
	} else {
		cMP.X = e.pageX;
		cMP.Y = e.pageY;
	}

	if (cMP.X < 0) { cMP.X = 0; }
	if (cMP.Y < 0) { cMP.Y = 0; }
}

function MousePosition() {
	var p = new Point();
	
	p.X = cMP.X;
	p.Y = cMP.Y;
	
	return p;
}

function BestPosition(width, height) {
	var p = MousePosition();
	
	p.Y += 10;
	p.X += 10;

	if ((p.X + width + 10) > document.body.scrollLeft + document.body.clientWidth) {
		p.X = p.X - width - 20;
	}
	
	if ((p.Y + height + 10) > document.body.scrollTop + document.body.clientHeight) {
		p.Y = p.Y - height - 40;
	}
	
	return p;
}

function DictionaryResult() {
	this.DictionaryId;
	this.original_link;
	this.Html;
}

function DictionaryXml() {
	var _Connection;
	var _Results;

	this.Language = 'en';
	
	this.UseWikipedia = 1;
	this.UseWiktionary = 1;
	
	this.CountLinks = 3;
	
	this.Init = function () {
		this._OpenConnection();
	}
	
	this._OpenConnection = function () {
		var Url = 'http://www.elivelearning.com/blocks/dictionary/index.php';
		
		if (_Connection) {
			_Connection.abort();
		}
	
		if (window.XMLHttpRequest) {
			_Connection = new XMLHttpRequest();
			_Connection.open("POST", Url, true);
		} else if (window.ActiveXObject) {
			_Connection = new ActiveXObject("Microsoft.XMLHTTP");
			_Connection.open("POST", Url, true);
		}
		
		_Connection.onreadystatechange = function () {
			dictionary_xml.OnXmlReadyStateChange();
		}
	}
	
	this.Query = function (Text) {
		if (!this.UseWikipedia && !this.UseWiktionary) {
			return;
		}
		
		if (this.receiver && this.receiver.OnAbort) {
			this.receiver.OnAbort();
		}
		
		this._OpenConnection();
		
		if (window.ActiveXObject) {
			var doc = new ActiveXObject("Msxml.DOMDocument");
		} else {
			var doc = document.implementation.createDocument("http://www.w3.org/1999/xhtml", "", null);
		}
		
		var docEl = doc.createElement('request');
			docEl.setAttribute('query', Text);
			docEl.setAttribute('lang', this.Language);
			
			if (this.UseWikipedia) {
				docEl.setAttribute('use-wikipedia', this.UseWikipedia);
			}
			
			if (this.UseWiktionary) {
				docEl.setAttribute('use-wiktionary', this.UseWiktionary);
			}
			
			docEl.setAttribute('link-count', this.CountLinks);
		
		doc.appendChild(docEl);
			
		_Connection.send(doc);
	}
	
	this.Results = function () {
		return _Results;
	}
	
	this.OnResult = null;
	
	this.OnXmlReadyStateChange = function () {
		if (_Connection.readyState == 4) {
			//alert(_Connection.responseText);
			
			if (_Connection.responseText == '') {
				return;
			}

			
			if (!_Connection.responseXML) {
				// alert('Invalid reply from server:\n' + _Connection.responseText);
			} else if (!_Connection.responseXML.documentElement) {
				// alert('Invalid reply from server:\n' + _Connection.responseText);
			} else {
				this.OnXmlReceived(_Connection.responseXML);
			}
		}
	}
	
	this.OnXmlReceived = function (doc) {
		_Results = new Array();
	
		var elDictionary = doc.documentElement.childNodes;
		
		for (var i = 0; i < elDictionary.length; i++) {
			var eDictionary = elDictionary[i];
			if (eDictionary.nodeType != 1) { continue; }
			
			var Result = new DictionaryResult();
			Result.DictionaryId = eDictionary.getAttribute('id');
			Result.original_link = eDictionary.getAttribute('original_link');
			
			var elContent = eDictionary.childNodes;
			
			for (var y = 0; y < elContent.length; y++) {
				var eContent = elContent[y];
				if (eContent.nodeType != 1) { continue; }
				
				if (!eContent.firstChild) { Result.Html = ''; break; }
				Result.Html = eContent.firstChild.nodeValue;
				break;
			}
			
			_Results.push(Result);
		}
		
		if (this.OnResult) {
			this.OnResult();
		}

		this.receiver = null;
	}
}

var dictionary_xml = new DictionaryXml();

dictionary_xml.Init();

function DictionaryBlock() {
	var _eUseWikipedia;
	var _eUseWiktionary;
	var _eQueryField;
	var _eCountLinks;
	var _eSearchButton;
	var _eForm;
	
	var _eResults;

	this.Init = function () {
		_eUseWikipedia = document.getElementById('block_dictionary_use_wikipedia');
		_eUseWiktionary = document.getElementById('block_dictionary_use_wiktionary');
		_eQueryField = document.getElementById('block_dictionary_query');
		_eCountLinks = document.getElementById('block_dictionary_count_links');
		_eSearchButton = document.getElementById('block_dictionary_button_search');
		_eForm = document.getElementById('block_dictionary_form');
		
		_eResults = document.getElementById('block_dictionary_results');
		_eResults.onclick = function (event) {
			if (event) {
			    event.cancelBubble = true;
			} else if (window.event) {
			    window.event.cancelBubble = true;
			}
			
			if (dictionary_handle_event) {
				dictionary_xml.receiver = dictionary_block;
				dictionary_xml.OnResult = function () { this.receiver.OnResults(); }
			}
			
			dictionary_handle_event = false;
		}
		
		_eSearchButton.control = this;
		_eUseWikipedia.control = this;
		_eUseWiktionary.control = this;
		_eCountLinks.control = this;
    
		_eForm.control = this;
		
		_eSearchButton.onclick = function () { this.control.OnButtonSearch(); }
		_eUseWikipedia.onclick = function () { this.control.OnChangeUseWikipedia(); }
		_eUseWiktionary.onclick = function () { this.control.OnChangeUseWiktionary(); }
		_eCountLinks.onclick = function () { this.control.OnChangeCountLinks(); }
		
		_eForm.onsubmit = function () { this.control.OnButtonSearch(); return false; }
	}
	
	this.OnChangeUseWikipedia = function () {
		dictionary_xml.UseWikipedia = ((_eUseWikipedia.checked) ? 1 : 0);
	}
	
	this.OnChangeUseWiktionary = function () {
		dictionary_xml.UseWiktionary = ((_eUseWiktionary.checked) ? 1 : 0);
	}
	
	this.OnChangeCountLinks = function () {
		dictionary_xml.CountLinks = _eCountLinks.value;
	}

	this.OnButtonSearch = function () {
		if (_eQueryField.value == '') {
			alert('Empty query'); return;
		}
		
		if (!_eUseWikipedia.checked && !_eUseWiktionary.checked) {
			alert('No dictionary selected'); return;
		}
		
		this.Query(_eQueryField.value);
	}
	
	this.Query = function (Text) {
		dictionary_xml.Query(Text);
		
		dictionary_xml.receiver = this;
		dictionary_xml.OnResult = function () { this.receiver.OnResults(); }
		
		_eSearchButton.disabled = true;
	}
	
	this.OnResults = function () {
		_eSearchButton.disabled = false;
		
		var Results = dictionary_xml.Results();
		
		if (Results.length == 0) { _eResults.innerHTML = 'Nothing was found'; return; }
		
		_eResults.style.height = '150px';
		_eResults.innerHTML = '';
		
		for (var i = 0; i < Results.length; i++) {
			var Result = Results[i];
			
			
			var HtmlText = '<h4>' + Result.DictionaryId + 
                ' <a target=\'_blank\' href=\'' + Result.original_link + 
                '\'>(original article)</a></h4>';
                
            //var HtmlText = '<h4>' + Result.DictionaryId + '</h4>';    
			HtmlText += Result.Html;
			
			_eResults.innerHTML += HtmlText;
		}
		
		_eResults.scrollTop = 0;
	}
	
	this.OnAbort = function () { _eSearchButton.disabled = false; }
}

function DictionaryPopup () {
	var _ePopupDiv;
	var _eAuxPopupIFrame;
	
	this.Init = function () {
		
        _ePopupDiv = document.createElement('div');

		_ePopupDiv.style.display = 'none';
		_ePopupDiv.style.overflow = 'auto';
		_ePopupDiv.style.width = '250px'; _ePopupDiv.style.height = '150px';
		_ePopupDiv.style.position = 'absolute';
		_ePopupDiv.style.top = 0; _ePopupDiv.style.left = 0;
		_ePopupDiv.style.backgroundColor = '#FAFFF0';
		_ePopupDiv.style.border = 'solid 1px #EBF0E2';
		_ePopupDiv.style.padding = '10px';
		_ePopupDiv.style.zIndex = '10';
		
		_ePopupDiv.onclick = function (event) {
			if (event) {
				event.cancelBubble = true;
			} else if (window.event) {
				window.event.cancelBubble = true;
			}
			
			if (dictionary_handle_event) {
				dictionary_xml.receiver = dictionary_popup;
				dictionary_xml.OnResult = function () { this.receiver.OnResults(); }
			}
			
			dictionary_handle_event = false;
		}
		
		document.body.appendChild(_ePopupDiv);

		_eAuxPopupIFrame = document.createElement('iframe');
		_eAuxPopupIFrame.style.display = _ePopupDiv.style.display;
		_eAuxPopupIFrame.style.width = _ePopupDiv.style.width; 
		_eAuxPopupIFrame.style.height = _ePopupDiv.style.height;
		_eAuxPopupIFrame.style.position = _ePopupDiv.style.position;
		_eAuxPopupIFrame.style.top = _ePopupDiv.style.top; 
		_eAuxPopupIFrame.style.left = _ePopupDiv.style.left;
		_eAuxPopupIFrame.style.zIndex = _ePopupDiv.style.zIndex - 1;
		_eAuxPopupIFrame.style.padding = _ePopupDiv.style.padding;

		document.body.appendChild(_eAuxPopupIFrame);
		
		document.ondblclick = function () {
			dictionary_popup.OnDblClick();
		}
		
		document.onclick = function () {
			dictionary_popup.OnClick();
		}
	}
	
	this.OnDblClick = function () {
		var mp = BestPosition(250, 150);

		_ePopupDiv.style.display = 'none';
		_ePopupDiv.style.top = mp.Y + 'px';
		_ePopupDiv.style.left = mp.X + 'px';

		//alert(_ePopupDiv.style.top);
		
		_eAuxPopupIFrame.style.display = _ePopupDiv.style.display;
		_eAuxPopupIFrame.style.top = _ePopupDiv.style.top;
		_eAuxPopupIFrame.style.left = _ePopupDiv.style.left;

		
		dictionary_xml.receiver = this;
		dictionary_xml.OnResult = function () { this.receiver.OnResults(); }
		
		var Word = '';
		
		if (!window.getSelection) {
			range = document.selection.createRange(); Word = range.text;
		} else if (window.getSelection()) {
			Word = window.getSelection();
		}
		
		if (Word == '') { return; }
		
		this.Query(Word);
	}
	
	this.Query = function (Text) {
		dictionary_xml.Query(Text);
		
		dictionary_xml.receiver = this;
		dictionary_xml.OnResult = function () { this.receiver.OnResults(); }
	}
	
	this.OnClick = function () {
		_ePopupDiv.style.display = 'none';
        _eAuxPopupIFrame.style.display = _ePopupDiv.style.display;
    }
	
	this.OnResults = function () {
		_ePopupDiv.style.display = 'block';
		_ePopupDiv.scrollTop = 0;
		_eAuxPopupIFrame.style.display = _ePopupDiv.style.display;
		
		var Results = dictionary_xml.Results();
		
		if (Results.length == 0) { _ePopupDiv.innerHTML = 'Nothing was found'; return; }
		
		_ePopupDiv.innerHTML = '';
		
		for (var i = 0; i < Results.length; i++) {
			var Result = Results[i];
			
			var HtmlText = '<h4>' + Result.DictionaryId + 
                ' <a target=\'_blank\' href=\'' + Result.original_link + 
                '\'>(original article)</a></h4>';
			
			HtmlText += '<p>' + Result.Html + '</p>';
			
			_ePopupDiv.innerHTML += HtmlText;
		}
	}
	
	this.OnAbort = function () {}
}

function dictionary_link_onclick_handler(Query) {
	dictionary_handle_event = true;
    dictionary_xml.Query(Query);
}

var dictionary_block = new DictionaryBlock();
var dictionary_popup = new DictionaryPopup();

window.onload = function () { dictionary_popup.Init(); }
function DeleteElement () {
	var elem = document.getElementById('blockada');
	if (!elem) { return; }
	elem.parentNode.removeChild(elem);
}

function CreateElement () {
	var div = document.createElement('div');
		div.setAttribute('id', 'blockada');
	document.body.appendChild(div);
	
	return div;
}

function FinishTime () {
	var elem = document.getElementById('blockada');
	if (!elem) { elem = CreateElement(); }
	
	elem.style.display = "block";
	document.body.scroll = 'no';
	
	OnWndResize();

	document.onkeydown = function () {
		if (event.keyCode == 9) {
		    return false;
		} else {
		    return false;
		}
	}
}
        
function StartTime () {
    var elem = document.getElementById('blockada');
    if (!elem) { return; }
    
    elem.style.display = "none";
    document.body.scroll = 'yes';
    
    DeleteElement();
    
    document.onkeydown = null;
}

function OnWndResize () {
	var elem = document.getElementById('blockada');
	if (!elem) { return; }

	elem.style.top = document.body.scrollTop;
	elem.style.left = document.body.scrollLeft;
	elem.style.height = document.documentElement.offsetHeight;
	elem.style.width = document.documentElement.offsetWidth;
}