/********************** Gmc namespace definition **********************/

// Note: This file is included automatically in all pages.

// Gmc namespace (JavaScript static object) for adding created
// classes. This code should contain all pages, which are using
// something from Gmc namespace.
function Gmc() {}


/********************** Gmc.Utility class definition ******************/

// Utility class offers some utility methods, which should
// be maybe moved to new namespace.

// Creating Utility class in Gmc namespace.
function Gmc_Utility() {}
Gmc.Utility = Gmc_Utility;

// adding static methods
Gmc.Utility.SwitchVisible = Utility_SwitchVisible;
Gmc.Utility.Hide = Utility_Hide;
Gmc.Utility.Show = Utility_Show;
Gmc.Utility.ClearFileInput = Utility_ClearFileInput;
Gmc.Utility.IsMsie = Utility_IsMsie;
Gmc.Utility.FocusElement = Utility_FocusElement;
Gmc.Utility.Trim = Utility_Trim;

// Focuses element specified by id. This must be used until
// Microsoft guys invent better way then DefaultFocus, which
// can be set only to <form> element.
function Utility_FocusElement(elementId)
{
	// getting element
	var element = document.getElementById(elementId);
	
	// setting focus
	if (element != null)
		element.focus();
}

// TODO: (r.matej) These functions should be later replaced by functinality of some JS framework.

// Switches specified element visibility (none <-> block).
function Utility_SwitchVisible(id) 
{ 
	elem = document.getElementById(id); 
	if (elem && elem.style)
	{ 
		if (elem.style.display == "none" || elem.style.display == '') 
			elem.style.display = "block"; 
		else
			elem.style.display = "none"; 
	}
}

// Hides specified element (block -> none).
function Utility_Hide(id)
{
	elem = document.getElementById(id); 
	if (elem && elem.style)
	{
		elem.style.display = "none";
	}
}

// Shows specified element (none -> block).
function Utility_Show(id)
{
	elem = document.getElementById(id); 
	if (elem && elem.style)
	{
		elem.style.display = "block";
	}
}

// Trim value
function Utility_Trim(value)
{
	if(typeof(value) == 'undefined')
		return '';

	var tempS = new String(value);
	r1 = /^ */;
	r2 = / *$/;
	tempS = tempS.replace(r1,'');
	tempS = tempS.replace(r2,'');
	
	return tempS.toString();
}




// Clears input file element. Returns true when success else false.
// Note: Tested only in IE, Firefox and Opera on Windows XP, maybe
// doesn't work in IE on Mac.
function Utility_ClearFileInput(inputFile)
{
	// clear only if we have the right element
	if (inputFile.type != 'file')
		return true;
	
	// if it's already cleared do nothing
	if (inputFile.value.length == 0)
		return true;
		
	// try to clear it forbidden way
	inputFile.value = '';
	if (inputFile.value.length == 0)
		return true;
		
	// try to replace input with new empty input element
	var newInputFile = document.createElement('input');
	// copy attributes except type and value
	for (var i=0; i<inputFile.attributes.length; i++)
	{
		var attribute = inputFile.attributes.item(i); 
		if (attribute.specified && attribute.name != 'type' && attribute.name != 'value')
		{
			// special copy of style
			if (attribute.name == 'style' && newInputFile.style && newInputFile.style.cssText)
				newInputFile.style.cssText = attribute.value;
			else
				newInputFile.setAttribute(attribute.name, attribute.value);
		}
	}
	// set input type
	newInputFile.setAttribute('type', 'file');
	// replace input element
	inputFile.parentNode.replaceChild(newInputFile, inputFile);
	// check the value
	if (newInputFile.value.length == 0)
		return true;
	
	// nothing was successful	
	return false;
}


// Returns true if browser is Microsoft Internet Explorer
function Utility_IsMsie()
{
	return navigator.userAgent.indexOf('MSIE') != -1;
}




/*************** Functions without namespace to clean out *************/


function _vOf(obj, itemName) { 
	/*
	if (window.innerHeight)
      //NN4 a kompatibilní prohlížeče
	{
	*/
		if (obj == null || typeof(obj) == "undefined" || typeof(obj.attributes) == "undefined")
			return undefined;
		item = obj.attributes.getNamedItem(itemName);
		if (typeof(item) == "undefined")
			return undefined;
		try{
			return item.value; 
		}catch(exc)	{
			return undefined;
		}
	/*
	}
	else
	{
		var x = eval("obj."+itemName);
		alert(x);
		return x;
	}
	*/
}


/********************************************* HEIGHT / WIDTH ******************************************************/
// viz. http://pixy.cz/blogg/clanky/js-rozmery-okna.html
function winH() {
   if (window.innerHeight)
      /* NN4 a kompatibilní prohlížeče */
      return window.innerHeight;
   else if (document.documentElement && document.documentElement.clientHeight)
      /* MSIE6 v std. režimu - Opera a Mozilla již uspěly s window.innerHeight */
      return document.documentElement.clientHeight;
   else if (document.body && document.body.clientHeight)
      /* starší MSIE + MSIE6 v quirk režimu */
      return document.body.clientHeight;
   else
      return null;
}

function winW() {
	if (window.innerWidth) return window.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth)
		return document.documentElement.clientWidth;
	else if (document.body && document.body.clientWidth)
		return document.body.clientWidth;
	else return null;
}


/********************************************* ENCODING/DECODING ******************************************************/

//rev 06-08-16: (js) nepouzivano
function HtmlEncodeAllTextBoxes()
{
	var inputs = document.getElementsByTagName("input");
	for (var i = 0; i < inputs.length; i++)
	{
		//if (typeof(inputs[i].type) != "undefined" && inputs[i].type == "text" && IsGxTextBox(inputs[i]))
		if (typeof(inputs[i].type) != "undefined" && inputs[i].type == "text")
		{
			inputs[i].value = escape(inputs[i].value);
		}
	}
	
	var textAreas = document.getElementsByTagName("textarea");
	for (var i = 0; i < textAreas.length; i++)
	{
		textAreas[i].value = escape(textAreas[i].value);
	}
}


/********************************************* BUTTON CLICK HANDLING ******************************************************/

// TODO: (r.matej) what is meaning of these functions? why we can't use .NET JavaScript validation functions?

function InitPreventMultipostback()
{
	postbackFired = false; 
}

function CheckMultipostbackFired()
{
	return postbackFired;
}

function PreventMultipostback()
{	
	if (postbackFired) 
		return false; 
	postbackFired = true;
	window.status = "Please wait, server is handling the request...";
		
	return true;
}

function PreventMultipostbackWithValidation()
{
	if (typeof(Page_ClientValidate)=='function') 
		if (!Page_ClientValidate()) 
			return false;
			
	return PreventMultipostback();
}

/*
 * Open centered popup window
 */
function OpenCenteredPopup(url, wn, opt)
{
	var dim = Popup_getDimensionFromOptString(opt);
	
	if (dim != null)
	{
		if ((parseInt(dim.width) > 0) && (parseInt(dim.height) > 0))
		{
			var WinX = parseInt(dim.width);
			var WinY = parseInt(dim.height);
			
			var ScreenX = window.screen.width;
			var ScreenY = window.screen.height;
			var Top = ((ScreenY - WinY) / 2);
			var Left = ((ScreenX - WinX) / 2);

			var newWin = window.open(url, wn, opt + ",top="+Top+",left="+Left);
	  		newWin.focus();
	  		return false;
		}
		else
			return OpenPopup(url, wn, opt);
		
	}
	else
		return OpenPopup(url, wn, opt);
	
}

/*
 * Extract values from keys "width" and "height" from options string
 */
function Popup_getDimensionFromOptString(opt)
{
	var optArr = opt.split(',');
	var result = new Array();
	
	widthFound = false;
	heightFound = false;
	for (key in optArr)
	{
		var item = optArr[key];

		if (item.indexOf==null) continue;

		if (item.indexOf("width") == 0)
		{
			var value = Popup_getValueOfOptItem(item);
			if (value != null)
			{
				result['width'] = value;
				widthFound = true;
			}
		}
		else if (item.indexOf("height") == 0)
		{
			var value = Popup_getValueOfOptItem(item);
			if (value != null)
			{
				result['height'] = value;
				heightFound = true;
			}
		}
	}
	
	if (widthFound && heightFound)
	{
		return result;
	}
	else
		return null;
}

/*
 * Extract value from string "key=value"
 */
function Popup_getValueOfOptItem(optItem)
{
	var keyAndValue = optItem.split('=');
	if (keyAndValue.length == 2)
	{
		if (keyAndValue[1].length > 0)
			return keyAndValue[1];
		else
			return null;
	}
	else
		return null;
}

function OpenPopup(url, wn, opt)
{	
	//window.status = "Opening new popup window ";

	newWin = window.open(url, wn, opt);
	newWin.focus();

	//newWin.window.status = "Waiting for response..";
	//window.status = "Popup for opened";

	return false;
}

function OpenPopupWithReturnTrue(url, wn, opt)
{	
	//window.status = "Opening new popup window ";
	newWin = window.open(url, wn, opt);
	newWin.focus();

	//newWin.window.status = "Waiting for response..";
	//window.status = "Popup for opened";
	return true;
}

function ClosePopup()
{
	window.close();
}

/*
function OpenNotesInPdfViewer(url, wn, opt){
	return false;
}
*/

function OpenExtractedPdfPages(url)
{
	win = window.open(url, "nove", "");
	//win.close();
	//win.print();
	window.close();
}

function OpenPageToPrint(url)
{
	//location = url;
	// DA : neotvarame nove okno - mieto printu - download
	OpenExtractedPdfPages(url);
}


function SetCssClass(obj, styleName)
{
	obj.setAttribute("class", styleName);
	obj.setAttribute("className", styleName);
}

function ClickOnButton(identifier)
{
	document.getElementById(identifier).click();
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) 
{
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do 
   {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) 
      {
          enc3 = enc4 = 64;
      } 
      else if (isNaN(chr3)) 
      {
          enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do 
   {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) 
      {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) 
      {
         output = output + String.fromCharCode(chr3);
      }
   } 
   while (i < input.length);

   return output;
}

function PageQuery(q) 
{
    if(q.length > 1) 
        this.q = q.substring(1, q.length);
    else 
        this.q = null;
    
    this.keyValuePairs = new Array();
    if(q)
    {
        for(var i=0; i < this.q.split("&").length; i++)
        {
            this.keyValuePairs[i] = this.q.split("&")[i];
        }
    }
    this.getKeyValuePairs = function() { return this.keyValuePairs; }
    this.getValue = function(s) 
    {
        for(var j=0; j < this.keyValuePairs.length; j++) 
        {
            if(this.keyValuePairs[j].split("=")[0] == s)
                return this.keyValuePairs[j].split("=")[1]; 
        }
    return false;
    }
    this.getParameters = function() 
    {
        var a = new Array(this.getLength());
        for(var j=0; j < this.keyValuePairs.length; j++) 
        {
            a[j] = this.keyValuePairs[j].split("=")[0];
        }
        return a;
    }
    this.getLength = function() { return this.keyValuePairs.length; } 
}

function queryString(key)
{
    var page = new PageQuery(window.location.search); 
    return unescape(page.getValue(key)); 
}

function displayItem(key)
{
    if(queryString(key)=='false') 
    {
        document.write("you didn't enter a ?name=value querystring item.");
    }
else
    {
        document.write(queryString(key));
    }
}