// public js functions for WR webs

function wrpopup(url){
    window.open(url,"wrpopup");
} 

// pomocna promenna
var usePopup = 1;

function popupImage(src){
	if(usePopup == 0){return true}
    var directories='no';
    var toolbar='no';
    var status='no';
    var menubar='no';
    var location='no';
    var posWidth = 540;
    var posHeight = 540;
    wname = 'image';
    
	if(window.screen.availHeight < posHeight){posHeight = window.screen.availHeight;} 
	if(window.screen.availWidth < posWidth){posWidth = window.screen.availWidth;}
	var posLeft = (window.screen.availWidth - posWidth)/2;
	var posTop = (window.screen.availHeight - posHeight)/2;
    var url = "/noRW_wr/popup/popup.html?"+src;
	var args='scrollbars=no,resizable=yes,location='+location+',directories='+directories+',status='+status+',menubar='+menubar+',toolbar='+toolbar+',width=' + posWidth + ',height=' + posHeight + ',left=' + posLeft + ',top=' + posTop;
	var win = window.open(url,wname,args);
	if(win){
		win.focus;
	}
    return true;
}


// ovladani fancyboxu
if(typeof(jQuery) == 'function'){	 
	$(document).ready(function(){ 		
		if($(".fancyPics").length > 0){
			$(document).ready(function(){
	  				$(".fancyPics a").fancybox();
	  		});
	  		usePopup = 0;
  		}
	});

}

function showDiv(divId){
    document.getElementById(divId).style.display = 'block';
}

function hideDiv(divId){
    document.getElementById(divId).style.display = 'none';
}

/*
	Finds all form elements (input, textarea, select) inside element with id "id". Result
	encode into an array in the form "name^value".
*/

function getFormItems(id)
{
	var root = document.getElementById(id);

	if(root == null)
		return true;

	var params = {};

	var inputs = root.getElementsByTagName("input");

	for(var i = 0; i < inputs.length; i++)
		if(inputs[i].getAttribute("type") != "reset")
			if((inputs[i].getAttribute("type") != "radio" && inputs[i].getAttribute("type") != "checkbox") || inputs[i].checked)
				params[inputs[i].getAttribute("name")] = inputs[i].value;

	var texts = root.getElementsByTagName("textarea");

	for(var i = 0; i < texts.length; i++)
		params[texts[i].getAttribute("name")] = texts[i].value;

	var selects = root.getElementsByTagName("select");

	for(var i = 0; i < selects.length; i++)
		for(var j = 0; j < selects[i].options.length; j++)
			if(selects[i].options[j].selected)
				params[selects[i].getAttribute("name")] = selects[i].options[j].value;

	return params;
}

function findSubmit(id)
{
	var root = document.getElementById(id);

	if(root == null)
		return false;

	var inputs = root.getElementsByTagName("input");

	for(idx in inputs)
		if(inputs[idx].type == "submit")
			return inputs[idx];

	return false;
}

var use_ajax_forms = true; // do we want ordinary or AJAX request?

/*
	ajaxForm sends form to the server with AJAX.
	
	Form elements are found inside DOM element with id "parentId". Result returned by
	server is injected into element again with "parentId". Typically parentId is <form> tag
	but can be anything (as far as it contains all elements of interest).
*/

function ajaxForm(parentId)
{
	if(use_ajax_forms == false)
	  return true;

	parentId = "myformDec_" + parentId;

	var params = getFormItems(parentId);
	var submit = findSubmit(parentId);

	submit.disabled = true;
	$(submit).addClass("disabled");

	$.post("?ajax=1", params, function(response) {
		if(response.substr(0, 1) == "1") {
			var url = response.substr(1);

			window.location.href = url;
		}
		else {
			document.getElementById(parentId).innerHTML = response;
			
			submit.disabled = false;
			$(submit).removeClass("disabled");
		}
	});
	
	return false;
}
