var XHTTPReqURL = '/lv/sistema/r/';
var LOADING = '<p><span class="note"> Datu ielāde / Loading...</span></p>';

var orules = {
	'.sys-user-show-list-button' : function(el) {
		el.onclick = function() {
			// class
			var c = document.getElementById('c').options[document.getElementById('c').selectedIndex].value;
			// query
			var q = document.getElementById('q').value;
			execGETRequest('users-mgmt-rs', 'view=sysusers-list-by-class&c='+c+'&q='+q);
		}
	},
	'.sys-user-list-edit' : function (el) {
		el.onclick = function ()
		{
			var sysuserid = el.name;
			execGETRequest('users-mgmt-rs', 'view=sysuser&sysuid='+sysuserid);
		}
	},
	'.sys-user-delete' : function (el) {
		el.onclick = function ()
		{
			if (confirmByMessage('Vai tiešām Jūs vēlaties dzēst šo ierakstu?\n\nDo you really want to delete this record?'))
			{
				// delete object by id
				var sysuserid = el.name;
				// class
				var c = document.getElementById('c').options[document.getElementById('c').selectedIndex].value;
				// query
				var q = document.getElementById('q').value;
				
				execGETRequest('users-mgmt-rs', 'view=sysusers-list-by-class&c='+c+'&q='+q+'&sysuid='+sysuserid+'&cmd=del-sysuser');
			}
		}
	},
	// Klasifikatoru datu labošanas formas izvade
	'.classificator-edit' : function (el) {
		el.onclick = function ()
		{
			classid = el.name;
			execGETRequest('classes-rs', 'view=class&classid='+classid);
		}
	},
	// Izvada pilnu klasiofikatoru sarakstu
	'.show-class-list' : function (el) {
		el.onclick = function ()
		{
			classid = el.name;
			execGETRequest('classes-rs', 'view=class-list');
		}
	},
	// Klasifikatoru dzēšanas metode
	'.classificator-delete' : function (el) {
		el.onclick = function ()
		{
			classid = el.name;
			if (confirmByMessage('Vai tiešām Jūs vēlaties dzēst šo ierakstu?\n\nDo you really want to delete this record?'))
			{
				execGETRequest('classes-rs', 'view=class-list&classid='+classid+'&cmd=del-class');
			}
		}
	},
	// Publikāciju filtrēšanas izvade
	'.publication-filter' : function(el) {
		el.onclick = function() {
			// class
			var publication_view = document.getElementById('publication_view').options[document.getElementById('publication_view').selectedIndex].value;
			// query
			var query = document.getElementById('query').value;
			var publication_start = document.getElementById('publication_start').value;
			var publication_end = document.getElementById('publication_end').value;
			
			var url = XHTTPReqURL;
			var pars = 'view=publication-list&publication_view='+publication_view+'&publication_start='+publication_start+'&publication_end='+publication_end+'&query='+query;
			location.href= '?'+pars;
		}
	},
	'.show-by-page' : function(el) {
		el.onclick = function() {
			var page = el.innerHTML;
			// class
			var publication_view = document.getElementById('publication_view').options[document.getElementById('publication_view').selectedIndex].value;
			// query
			var query = document.getElementById('query').value;
			var publication_start = document.getElementById('publication_start').value;
			var publication_end = document.getElementById('publication_end').value;
			
			var url = XHTTPReqURL;
			var pars = 'view=publication-list&publication_view='+publication_view+'&publication_start='+publication_start+'&publication_end='+publication_end+'&query='+query+'&page='+page;
			location.href= '?'+pars;
			return false;
		}
	},
	'.data-list-row' : function(el) {
		var previous_backgroundColor = el.style.backgroundColor
		el.onmouseover = function() {
			el.style.backgroundColor = '#eff2ff';
			return false;
		}
		el.onmouseout = function() {
			el.style.backgroundColor = previous_backgroundColor;
			return false;
		}
	},
	'.attach-item' : function(el) {
		var previous_backgroundColor = el.style.backgroundColor
		el.onmouseover = function() {
			el.style.backgroundColor = '#eff2ff';
			return false;
		}
		el.onmouseout = function() {
			el.style.backgroundColor = previous_backgroundColor;
			return false;
		}
	},
	'.publication-list-edit' : function (el) {
		el.onclick = function ()
		{
			pubid = el.name;
			location.href = '?view=publication&publication_id='+pubid;
			return false;
		}
	},
	'.publication-cancel' : function (el) {
		el.onclick = function ()
		{
			location.href = '?';
			return false;
		}
	},	
	'.publication-data-save' : function (el) {
		el.onclick = function ()
		{
			var pubform = document.getElementById("publication-edit");
			document.getElementById("_cmd").value = "edit-article";
			
			alert(document.getElementById("_cmd").value);
			pubid = el.name;
			if (pubid>0) pubform.action = '?view=publication&pubid='+pubid;
			pubform.submit();
			return false;
		}
	},
	'.publication-data-publish' : function (el) {
		el.onclick = function ()
		{
			var pubform = document.getElementById("publication-edit");
			document.getElementById("_cmd").value = "publish-article";
			pubform.submit();
			return false;
		}
	},
	'.attachment-list-edit' : function (el) {
		el.onclick = function ()
		{
			attid = el.name;
			pubid = document.getElementById("_pub_id").value;
			location.href = '?view=attachments&publication_id='+pubid+'&attachment_id='+attid;
			return false;
		}
	},
	'.attachment-cancel' : function (el) {
		el.onclick = function ()
		{
			pubid = document.getElementById("_pub_id").value;
			location.href = '?view=attachments&publication_id='+pubid;
			return false;
		}
	},
	// Pielikumu faila dzēšana izmantojot POST request
	'.attachment-delete' : function(el) {
		el.onclick = function() {
			if (confirmByMessage('Vai tiešām Jūs vēlaties dzēst šo pielikumu failu?\n\nDo you really want to delete this attachment?'))
			{
				$('cmd').value='delete-attachment';
				$('file-edit').submit();
				return true;
			}
		}
	},
	// Publikācijas datu dzēšana izmantojot POST request
	'.publication-delete' : function(el) {
		el.onclick = function() {
			if (confirmByMessage('Vai tiešām Jūs vēlaties dzēst šo publikāciju?\n\nDo you really want to delete this attachment?'))
			{
				$('cmd').value='delete-publication';
				$('publication-edit').submit();
				
			}
		}
	},
	'.classes-edit-form td' : function(el) {
		el.onclick = function() {
			if (el.className == 'checked-class') el.className=''; else el.className='checked-class';
		}
	},
	'.advert-cancel' : function(el) {
		el.onclick = function() {
			location.href='../';
		}
	},
	'#adverts-categories li' : function(el) {
		el.onclick = function() {
			// Novāc iekrāsojumu no pārējiem
			var elems = this.parentNode.getElementsByTagName("li");
			for (i=0; i<elems.length; i++) 
			{ 
				if ( elems.item(i).className != 'special') elems.item(i).className = '';
			}
			
			// Iekrāso man vajadzīgo
			el.className='opened';
			var values = el.id.split("-");
			
			execGETRequest('adverts-rs', 'view=adverts-by-category&category_id='+values[2]);
		}
	},
	'.show-adv-by-page' : function(el) {
		el.onclick = function() {
			var values = this.parentNode.title.split("-");
			var category_id = values[0];
			var page = values[1];
			execGETRequest('adverts-rs', 'view=adverts-by-category&category_id='+category_id+'&p='+page);
		}
	},
	'.show-dis-by-page' : function(el) {
		el.onclick = function() {
			var values = this.parentNode.title.split("-");
			var theme_id = values[0];
			var page = values[1];
			execGETRequest('discussion-rs', 'view=theme&theme_id='+theme_id+'&page='+page);
		}
	},
	'.show-dis-query-by-page' : function(el) {
		el.onclick = function() {
			var values = this.parentNode.title.split("-");
			var query = values[0];
			var page = values[1];
			execGETRequest('discussion-rs', 'view=search&query='+query+'&page='+page);
		}
	},
	'.show-opinions-by-page' : function(el) {
		el.onclick = function() {
			var values = this.parentNode.title.split("-");
			var discussion_id = values[0];
			var page = values[1];
			execGETRequest('discussion-rs', 'view=discussion-view&discussion_id='+discussion_id+'&page='+page);
		}
	},	
	'img.thumb' : function(el) {
		var previous_borderColor = el.style.borderColor;
		el.onclick = function() {
			if (el.name != 'none')
			{
				if (values = el.name.split("-"))
				{
					var image_id = values[0]; var width = values[1]; var height = values[2];
					openPopup("/b/"+image_id, width, height);
				}
			}
		}
		el.onmouseover = function() {
			el.style.borderColor = '#999999';
			return false;
		}
		el.onmouseout = function() {
			el.style.borderColor = previous_borderColor;
			return false;
		}
	},
	'#adv_body' : function(el) {
		el.onkeydown = function() {
			if (el.value.length<=750)
			{
				var remain = 750-el.value.length;
				$('adv_body_chars').value=remain;
			}
			else
			{
				el.value = el.value.substring(0,750);
			}
		}
		el.onkeyup = function() {
			if (el.value.length<=750)
			{
				var remain = 750-el.value.length;
				$('adv_body_chars').value=remain;
			}
			else
			{
				el.value = el.value.substring(0,750);
			}
		}
	},
	'#dis_body' : function(el) {
		el.onkeydown = function() {
			if (el.value.length<=5000)
			{
				var remain = 5000-el.value.length;
				$('dis_body_chars').value=remain;
			}
			else
			{
				el.value = el.value.substring(0,5000);
			}
		}
		el.onkeyup = function() {
			if (el.value.length<=5000)
			{
				var remain = 5000-el.value.length;
				$('dis_body_chars').value=remain;
			}
			else
			{
				el.value = el.value.substring(0,5000);
			}
		}
	},
	'#com_body' : function(el) {
		el.onkeydown = function() {
			if (el.value.length<=5000)
			{
				var remain = 5000-el.value.length;
				$('com_body_chars').value=remain;
			}
			else
			{
				el.value = el.value.substring(0,5000);
			}
		}
		el.onkeyup = function() {
			if (el.value.length<=5000)
			{
				var remain = 5000-el.value.length;
				$('com_body_chars').value=remain;
			}
			else
			{
				el.value = el.value.substring(0,5000);
			}
		}
	},
	'#faq_question' : function(el) {
		el.onkeydown = function() {
			if (el.value.length<=5000)
			{
				var remain = 5000-el.value.length;
				$('faq_question_chars').value=remain;
			}
			else
			{
				el.value = el.value.substring(0,5000);
			}
		}
		el.onkeyup = function() {
			if (el.value.length<=5000)
			{
				var remain = 5000-el.value.length;
				$('faq_question_chars').value=remain;
			}
			else
			{
				el.value = el.value.substring(0,5000);
			}
		}
	},
	'.discussion-add' : function(el) {
		el.onclick = function() {
			location.href = './add/0';
		}
	},
	'.discussion-reply' : function(el) {
		el.onclick = function() {
			var reply_id = el.name;
			execGETRequest('discussion-rs', 'view=discussion-reply&comment_id='+reply_id);
		}
	},
	'.discussion-search' : function(el) {
		el.onclick = function() {
			discussionQuery();
		}
	},
	'.discussion-query' : function(el) {
		el.onkeydown = function(f) {
			var keycode; if (window.event) keycode = window.event.keyCode; else if (f) keycode = f.which;
			switch(keycode)
			{
				case 13:
					discussionQuery();
				break;
			}
		}
	},	
	'.edit-comment' : function(el) {
		el.onclick = function() {
			var comment_id = el.id.split("-")[2];
			execGETRequest('discussion-rs', 'view=opinion-edit&comment_id='+comment_id);
		}
	},
	'.open-new-win' : function(el) {
		el.onclick = function() {
			window.open( el.href , "_blank", "");
			return false;
		}
	},
	'.calendar-by-date-filter' : function(el) {
		el.onclick = function() {
			var calendar_month = $('calendar_month').value;
			var calendar_year = $('calendar_year').value;
			execGETRequest('calendar-events-rs', 'view=events-list&calendar_year='+calendar_year+'&calendar_month='+calendar_month)
		}
	},
	'.calendar-special' : function(el) {
		var previous_borderColor = el.style.borderColor;
		el.onmouseover = function() {
			el.style.borderColor = '#ffae00';
			return false;
		}
		el.onmouseout = function() {
			el.style.borderColor = previous_borderColor;
			return false;
		}
	},
	'.gallery-filter' : function(el) {
		el.onclick = function() {
			var query = $('gallery-query').value;
			execGETRequest('gallery-filter-rs', 'view=gallery-filter&query='+query);
		}
	},
	'#gallery-query': function(el) {
		el.onkeydown = function(f) {
			var keycode; if (window.event) keycode = window.event.keyCode; else if (f) keycode = f.which;
			switch(keycode)
			{
				case 13:
					var query = $('gallery-query').value;
					execGETRequest('gallery-filter-rs', 'view=gallery-filter&query='+query);
					return false;
				break;
			}
		}
	},
	'.article-filter' : function(el) {
		el.onclick = function() {
			var query = $('article-query').value;
			execGETRequest('article-filter-rs', 'view=article-filter&query='+query);
		}
	},
	'#article-query': function(el) {
		el.onkeydown = function(f) {
			var keycode; if (window.event) keycode = window.event.keyCode; else if (f) keycode = f.which;
			switch(keycode)
			{
				case 13:
					var query = $('article-query').value;
					execGETRequest('article-filter-rs', 'view=article-filter&query='+query);
					return false;
				break;
			}
		}
	},
	'a.view-gallery': function(el) {
		el.onclick = function() {
			if (values = el.name.split("-"))
			{
				if (values[0]=="pub")
				{
					var publication_id = values[1];
					var lang = values[2];
					openPopup("/"+lang+"/ga/"+publication_id+"/", 520, 500);
					return false;
				}
			}
		}
	},
	'.procurement-filter' : function(el) {
		el.onclick = function() {
			var query = $('procurement-query').value;
			execGETRequest('procurement-filter-rs', 'view=procurement-filter&query='+query);
		}
	},
	'#procurement-query': function(el) {
		el.onkeydown = function(f) {
			var keycode; if (window.event) keycode = window.event.keyCode; else if (f) keycode = f.which;
			switch(keycode)
			{
				case 13:
					var query = $('procurement-query').value;
					execGETRequest('procurement-filter-rs', 'view=procurement-filter&query='+query);
					return false;
				break;
			}
		}
	},
	'.show-proc-by-page' : function(el) {
		el.onclick = function() {
			var values = this.parentNode.title.split("-");
			var category_id = values[0];
			var page = values[1];
			execGETRequest('procurement-rs', 'view=procurement-list&category_id='+category_id+'&page='+page);
		}
	},
	'.faq-filter' : function(el) {
		el.onclick = function() {
			var query = $('faq-query').value;
			execGETRequest('faq-filter-rs', 'view=faq-filter&query='+query);
		}
	},
	'#faq-query': function(el) {
		el.onkeydown = function(f) {
			var keycode; if (window.event) keycode = window.event.keyCode; else if (f) keycode = f.which;
			switch(keycode)
			{
				case 13:
					var query = $('faq-query').value;
					execGETRequest('faq-filter-rs', 'view=faq-filter&query='+query);
					return false;
				break;
			}
		}
	},
	'.show-faq-by-page' : function(el) {
		el.onclick = function() {
			var values = this.parentNode.title.split("-");
			var category_id = values[0];
			var page = values[1];
			execGETRequest('faq-rs', 'view=faq-list&category_id='+category_id+'&page='+page);
		}
	},
	'#contentbtn-bold' : function(el) {
		el.onclick = function() {
			var contentId = document.getElementById("content-edit").value;
			setBBCodeOutput("bold", contentId, "[b]", "[/b]");
		}
	},
	'#contentbtn-italic' : function(el) {
		el.onclick = function() {
			var contentId = document.getElementById("content-edit").value;
			setBBCodeOutput("italic", contentId, "[i]", "[/i]");
		}
	},
	'#contentbtn-underline' : function(el) {
		el.onclick = function() {
			var contentId = document.getElementById("content-edit").value;
			setBBCodeOutput("underline", contentId, "[u]", "[/u]");
		}
	},
	'#contentbtn-strikethrough' : function(el) {
		el.onclick = function() {
			var contentId = document.getElementById("content-edit").value;
			setBBCodeOutput("strikethrough", contentId, "[strike]", "[/strike]");
		}
	},
	'#contentbtn-email' : function(el) {
		el.onclick = function() {
			var contentId = document.getElementById("content-edit").value;
			setBBCodeOutput("email", contentId, "[email]", "[/email]");
		}
	},
	'#contentbtn-link' : function(el) {
		el.onclick = function() {
			var contentId = document.getElementById("content-edit").value;
			setBBCodeOutput("link", contentId, "[url]", "[/url]");
		}
	},
	'#contentbtn-bullist' : function(el) {
		el.onclick = function() {
			var contentId = document.getElementById("content-edit").value;
			setBBCodeOutput("bullist", contentId, "[list]", "[/list]");
		}
	},
	'#contentbtn-headline' : function(el) {
		el.onclick = function() {
			var contentId = document.getElementById("content-edit").value;
			setBBCodeOutput("headline", contentId, "[size=2]", "[/size]");
		}
	},
	'button.page-cancel' : function(el) {
		el.onclick = function() {
			location.href = './';
		}
	},
	'button.page-save' : function(el) {
		el.onclick = function() {
			var pubform = document.getElementById("page-edit");
			document.getElementById("cmd").value = "edit-page";
			pubform.submit();
		}
	},
	'a.delete-attach-item' : function(el) {
		el.onclick = function() {
			return confirmByMessage('Vai tiešām Jūs vēlaties dzēst šo pielikuma failu?\n\nDo you really want to delete this file?');
		}
	},
	'.tourism-filter' : function(el) {
		el.onclick = function() {
			var query = $('tourism-query').value;
			execGETRequest('tourism-filter-rs', 'view=tourism-filter&query='+query);
		}
	},
	'#tourism-query': function(el) {
		el.onkeydown = function(f) {
			var keycode; if (window.event) keycode = window.event.keyCode; else if (f) keycode = f.which;
			switch(keycode)
			{
				case 13:
					var query = $('tourism-query').value;
					execGETRequest('tourism-filter-rs', 'view=tourism-filter&query='+query);
					return false;
				break;
			}
		}
	},
	'.tourism-info-filter' : function(el) {
		el.onclick = function() {
			var query = $('tourism-info-query').value;
			execGETRequest('tourism-info-filter-rs', 'view=tourism-info-filter&query='+query);
		}
	},
	'#tourism-info-query': function(el) {
		el.onkeydown = function(f) {
			var keycode; if (window.event) keycode = window.event.keyCode; else if (f) keycode = f.which;
			switch(keycode)
			{
				case 13:
					var query = $('tourism-info-query').value;
					execGETRequest('tourism-info-filter-rs', 'view=tourism-info-filter&query='+query);
					return false;
				break;
			}
		}
	},	
	'#reg_confirm' : function(el) {
		el.onclick = function() {
			if (el.checked) 
				document.getElementById('regbutton').disabled=false;
			else
				document.getElementById('regbutton').disabled=true;
		}
	},
	'.procurement-by-category' : function(el) {
		el.onclick = function() {
			box = $('procurement-sel-category');
			var destination = box.options[box.selectedIndex].value;;
			if (destination==0)
			location.href='/lv/sodien/iepirkumi/';
			else
			location.href='/lv/sodien/iepirkumi/c/'+destination+'/selected';
		}
	},
	'.show-regul-by-page' : function(el) {
		el.onclick = function() {
			var values = this.parentNode.title.split("-");
			var category_id = values[0];
			var page = values[1];
			execGETRequest('regulation-rs', 'view=regulation-list&category_id='+category_id+'&page='+page);
			return false;
		}
	},
	'.show-resol-by-page' : function(el) {
		el.onclick = function() {
			var values = this.parentNode.title.split("-");
			var category_id = values[0];
			var page = values[1];
			execGETRequest('resolution-rs', 'view=resolution-list&category_id='+category_id+'&page='+page);
		}
	},
	'.show-docs-by-page' : function(el) {
		el.onclick = function() {
			var values = this.parentNode.title.split("-");
			var category = values[0];
			var category2 = values[1];
			var rows_per_page = values[2];
			var body_info = values[3];
			var page = values[4];
			execGETRequest('documents-rs', 'view=document-list&category='+category+'&category2='+category2+'&rows_per_page='+rows_per_page+'&body_info='+body_info+'&page='+page);
			return false;
		}
	}	
	// 
	
	
	
	
	//,
	//'#example li' : function(el){
	//	el.onclick = function(){
	//		this.parentNode.removeChild(this);
	//		return false;
	//	}
	//}
};

Behaviour.register(orules);



//------------------------------------------------------------------------------
// Satura mainīšana pēc atbilstošas pogas nospiešanas
function setBBCodeOutput(buttonId, contentId, tagOpen, tagClose)
{
	myText = document.getElementById(contentId);
	
	mySelection = '';
	if ( window.getSelection || document.getSelection ) {
		mySelection = myText.value.substring(myText.selectionStart,myText.selectionEnd);
	}
	else 
	{
		mySelection = iestr = document.selection.createRange().text;
	}
	
	var Content = '';
	
	switch(buttonId)
	{
		case "email" :
			tmpemail = prompt("Ievadiet e-pasta adresi / Enter Email Address:", "");
			if (tmpemail != null) Content = tagOpen+tmpemail+tagClose; else Content = '';
		break;
		
		case "bullist" :
			Content = tagOpen + "\n";
			
			do {
				tmpval = prompt("Lūdzu ievadiet elementa datus / Please enter an item:", "");
				if ( tmpval != undefined && tmpval != '') 
				{
					Content += "* " + tmpval + "\n";
				}
			} while (tmpval != null && tmpval != "" && tmpval != undefined);
			Content = Content + tagClose + "\n";
		break;
		
		case "link" :
			var tmptitle = '';
			
			tagOpen = tagOpen.substring(0,tagOpen.length-1);
			Content  = tagOpen;
			tmphref  = prompt("Ievadiet tīmekļa lapas URL adresi / Enter Web page URL address:","http://");
			if (mySelection == '') tmptitle = prompt("Ievadiet saites nosaukumu / Enter link title","");
			
			if (tmphref == null || tmphref == "" || tmphref == undefined) Content = Content + ']'; else Content = Content + '='+tmphref+']';
			if (tmptitle == null || tmptitle == "" || tmptitle == undefined ) Content = Content+mySelection; else Content = Content + tmptitle;
			Content = Content + tagClose;
		break;
		
		default :
			Content = tagOpen+mySelection+tagClose;
		break;
	}
	
	var startPos = myText.selectionStart;
	var endPos = myText.selectionEnd;
	var cursorPos = endPos;
	var scrollTop = myText.scrollTop;
	
	if ( window.getSelection || document.getSelection ) 
	{
		// Almost Mozilla or Opera
		if ( myText.selectionStart != myText.selectionEnd ) 
		{
			if ( Content != '' ) 
			{
				myText.value = 
					myText.value.substring(0,myText.selectionStart)
					+ Content
					+ myText.value.substring(myText.selectionEnd,myText.value.length);
				cursorPos += Content.length - mySelection.length;
			}
			else 
			{
				myText.value = 
					myText.value.substring(0,myText.selectionStart)
					+ tagOpen
					+ myText.value.substring(myText.selectionStart,myText.selectionEnd)
					+ tagClose
					+ myText.value.substring(myText.selectionEnd,myText.value.length);
				cursorPos += tagOpen.length + tagClose.length;
			}
		}
		else 
		{
			if ( Content != '' ) 
			{
				myText.value = 
					myText.value.substring(0,myText.selectionStart)
					+ Content
					+ myText.value.substring(myText.selectionEnd,myText.value.length);
				cursorPos += Content.length;
			}
			else 
			{
				myText.value = 
					myText.value.substring(0,myText.selectionStart)
					+ tagOpen
					+ myText.value.substring(myText.selectionStart,myText.selectionEnd)
					+ tagClose
					+ myText.value.substring(myText.selectionEnd,myText.value.length);
				cursorPos += tagOpen.length;
			}
		}
		myText.focus();
		myText.selectionStart = cursorPos;
		myText.selectionEnd = cursorPos;
		myText.scrollTop = scrollTop;
	}
	else if (document.selection) 
	{
		// Almost suckzors
		myText.focus();
		iestr = document.selection.createRange();
		if ( iestr.text.length > 0 ) 
		{
			if ( Content != '' ) {
				iestr.text = Content;
			}
			else 
			{
				iestr.text = tagOpen + iestr.text + tagClose;
			}
		}
		else 
		{
			if ( Content != '' ) 
			{
				iestr.text = Content;
			}
			else 
			{
				iestr.text = tagOpen + tagClose;
			}
		}
		myText.focus();
	}
}


//------------------------------------------------------------------------------
// Izpildīt Ajax Get Request
function execGETRequest(result_obj, pars)
{
	$(result_obj).innerHTML = LOADING;
	
	var url = XHTTPReqURL;
	
	var myAjax = new Ajax.Updater(result_obj,
		url,
		{method: 'get', 
			parameters: pars,
			onComplete: function(element){
				Behaviour.apply();
			}
		}
		);
}

//------------------------------------------------------------------------------
// Filtrēt datus no diskusijas
function discussionQuery()
{
	var query = $('query').value;
	execGETRequest('discussion-rs', 'view=search&query='+query);
}

//------------------------------------------------------------------------------
// Formas obligāto lauku automātiska pārbaude

// Pārbauda formu, vai ir aizpildīti visi obligātie (mandatory) elementi
function validateMandatoryFields(form)
{
	var mandatoryFieldCount = 0;
	var filledMandatoryFieldCount = 0;
	
	var firstNotFilledMandatoryField = null;
	
	if (form.elements != null)
	{
		for (i=0; i<form.elements.length; i++)
		{
			var fieldClassName = form.elements[i].className;
			if (isMandatoryField(fieldClassName))
			{
				mandatoryFieldCount++;
				switch(form.elements[i].type)
				{
					case 'text' :
						if (filledMandatoryElement(form.elements[i].value)) filledMandatoryFieldCount++;
						else
						{
							if (firstNotFilledMandatoryField == null) firstNotFilledMandatoryField = form.elements[i];
						}
						break;
					case 'password':
						if (filledMandatoryElement(form.elements[i].value)) filledMandatoryFieldCount++;
						else
						{
							if (firstNotFilledMandatoryField == null) firstNotFilledMandatoryField = form.elements[i];
						}
						break;
				}
			}
		}
	}
	
	if (mandatoryFieldCount != filledMandatoryFieldCount)
	{
		// Izvadam kļūdas paziņojumu
		alert("Lūdzu, aizpildiet visus obligātos datu laukus formā!\n\nPlease fill all mandatory fields in the current form!");
		
		// Aktivizēt pirmo neaizpildīto lauku
		firstNotFilledMandatoryField.focus();
		firstNotFilledMandatoryField.select();
		
		// Neļaujam formai izpildīties
		return false;
	}
	else return true;
	
}

// Pārbaudīt, vai konkrētajam elementam ir piesaistīta obligātā elements klase
function isMandatoryField(className)
{
	var re=/mandatory/i;
	return re.test(className);
}

// Pārbauda, vai ir ievadīts kautkas teksta laukā
function filledMandatoryElement(elemValue)
{
	var re=/([a-zA-Z0-9\а\б\в\г\д\е\ё\ж\з\и\й\к\л\м\н\о\п\р\с\т\у\ф\х\ц\ч\ш\щ\ь\ы\ъ\э\ю\я\ā\č\ē\ģ\ī\ķ\ļ\ņ\š\ū\ž\_\.\,\?\!\`\@\#\$\%\^\&\*\(\)\-\+\\\/\>\<]{1,})+/i;
	return re.test(elemValue);
}


// Pārbauda, vai lietotājs apstiprina izvēli pēc darbības
function confirmByMessage(msg)
{
	return confirm(msg);
}

// Atvērt popup logu
function openPopup(url, w, h)
{
	l = ( screen.width - w ) / 2;
	t = ( screen.height - h ) / 2 - 20;
	var wndws = window.open( url , "_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, left="+l+", top="+t+", width="+w+",height="+h );
}

