﻿function hide_box(boxId) {
	if (window.top.document.getElementById(boxId) && window.top.document.getElementById('clona' + boxId)) {
		var clona1 = window.top.document.getElementById(boxId);
		var clona = window.top.document.getElementById('clona' + boxId);

		window.top.document.body.removeChild(clona);
		window.top.document.body.removeChild(clona1);
	}
}

// --- HELP
function ShowHelp(atrHeaderText, atrFooterText, atrCloseButtonTitle) {
	return show_box("/FileManager/Help.aspx", 600, 400, atrHeaderText, atrFooterText, atrCloseButtonTitle, '0', '');
}

// Shows a box if it wasn't shown yet or is hidden
// or hides it if it is currently shown
function show_box(url, width, height, atrHeaderText, atrFooterText, atrCloseButtonTitle, atrWithHelp, atrHelpWindowHeaderText) {
	
	var href = url;
	var wo = document;
	if (window != window.top) {
		wo = window.top.document;
	}
	var boxId = GenerateBoxId(url);
	var maxZindex = getMaxZIndex();

	// Clona
	var div;
	if (Br.IE) {
		div = wo.createElement('iframe');
		div.id = 'clona' + boxId;
		div.style.zIndex = maxZindex + 1;
		div.className = "clona";
		height = height +5 ;
	}
	else {
		div = wo.createElement('div');
		div.id = 'clona' + boxId;
		div.style.zIndex = maxZindex + 1;
		div.className = "clona";
	}
	wo.body.appendChild(div);

	//margin-left: (-1)*(sirka)/2 :)
	//margin-top: (-1)*(vyska)/2 :)    
	// hlavni okno	
	var boxdiv;
	boxdiv = wo.createElement('div');
	boxdiv.setAttribute('id', boxId);
	boxdiv.className = 'fM';
	boxdiv.style.width = width + 'px';
	boxdiv.style.marginLeft = (-1) * (width / 2) + 'px';
	boxdiv.style.marginTop = (-1) * (height / 2) + 'px';
	boxdiv.style.zIndex = maxZindex + 2;
	wo.body.appendChild(boxdiv);

	//nastaveni hlavicky
	//hlavicka
	var headerDiv = wo.createElement('div');
	headerDiv.className = 'head';

	//logo
	var logo_area = wo.createElement('div');
	logo_area.className = "logo";

	var logo_link = wo.createElement('a');
	logo_link.setAttribute("href", "http://www.netdirect.cz");
	logo_link.innerHTML = "NetDirect s.r.o.";
	logo_area.appendChild(logo_link);

	headerDiv.appendChild(logo_area);

	//text
	var headerText = wo.createElement('h3');
	if (isNotNull(atrHeaderText))
		headerText.innerHTML = atrHeaderText;
	else
		headerText.innerHTML = "&nbsp;";
	headerDiv.appendChild(headerText);

	// buttons area
	var btn_area = wo.createElement('div');
	btn_area.className = "sysButtons";

	if (atrWithHelp == '1') {
		// help	
		var help_button = wo.createElement('a');
		help_button.setAttribute("title", atrHelpWindowHeaderText);
		help_button.className = "quest btn";
		help_button.innerHTML = "?<span></span>";
		help_button.onclick = function()
		{ ShowHelp(atrHelpWindowHeaderText, atrFooterText, atrCloseButtonTitle); }
		btn_area.appendChild(help_button);
	}

	// close
	var close_button = wo.createElement('a');
	close_button.setAttribute("title", atrCloseButtonTitle);
	close_button.className = "close btn";
	close_button.innerHTML = "x<span></span>";
	close_button.onclick = function()
	{ hide_box(boxId); }
	btn_area.appendChild(close_button);

	headerDiv.appendChild(btn_area);
	boxdiv.appendChild(headerDiv);

	href = addUrlParameter(href, "FameID", boxId);
	//nastaveni tela
	var body_area = wo.createElement('div');
	body_area.className = "body fMBody";
	body_area.style.height = height + 'px';

	var contents = wo.createElement('iframe');
	contents.frameBorder = '0';
	contents.style.width = '100%';
	contents.style.height = '100%';
	contents.style.overflow = 'hidden';

	boxdiv.opener = document;
	contents.opener = document;
	body_area.appendChild(contents);

	boxdiv.contents = body_area;
	boxdiv.appendChild(body_area);

	//nastaveni paticky
	//paticka
	var bottomDiv = wo.createElement('div');
	bottomDiv.className = 'footer';

	//text
	var bottomText = wo.createElement('p');
	bottomText.innerHTML = atrFooterText + "&nbsp;";

	bottomDiv.appendChild(bottomText);
	boxdiv.appendChild(bottomDiv);

	if (contents.contentWindow) {
		contents.contentWindow.document.location.replace(
			href);
	}
	else {
		contents.src = href;
	}

	return;
}

function show_anim_window(id,text, width, height) {	
	var wo = document;
	if (window != window.top) {
		wo = window.top.document;
	}
	var boxId =id;
	var maxZindex = getMaxZIndex();

	// Clona
	var div;
	if (Br.IE) {
		div = wo.createElement('iframe');
		div.id = 'clona' + boxId;
		div.style.zIndex = maxZindex + 1;
		div.className = "clona";
		height = height + 5;
	}
	else {
		div = wo.createElement('div');
		div.id = 'clona' + boxId;
		div.style.zIndex = maxZindex + 1;
		div.className = "clona";
	}
	wo.body.appendChild(div);

	//margin-left: (-1)*(sirka)/2 :)
	//margin-top: (-1)*(vyska)/2 :)    
	// hlavni okno	
	var boxdiv;
	boxdiv = wo.createElement('div');
	boxdiv.setAttribute('id', boxId);
	boxdiv.className = 'uploadAnimationWindow';
	boxdiv.style.width = width + 'px';
	boxdiv.style.marginLeft = (-1) * (width / 2) + 'px';
	boxdiv.style.marginTop = (-1) * (height / 2) + 'px';
	boxdiv.style.zIndex = maxZindex + 2;
	wo.body.appendChild(boxdiv);

	var body_area = wo.createElement('div');
	body_area.className = "body";
	body_area.style.height = height + 'px';

	var text_area = wo.createElement('p');
	text_area.innerHTML = text;	
	
	var img_area = wo.createElement('div');
	img_area.className = 'uploadingAninationImage';	
	
	boxdiv.opener = document;
	body_area.appendChild(text_area);
	body_area.appendChild(img_area);
	boxdiv.contents = body_area;
	boxdiv.appendChild(body_area);	
	
	return;
}


// Shows a box if it wasn't shown yet or is hidden
// or hides it if it is currently shown
function show_alert_box(text, width, height, atrHeaderText, atrFooterText, atrCloseButtonTitle, atrWithHelp, atrHelpWindowHeaderText) {
	var cutText = text;
	if (cutText.length > 100) {
		cutText = cutText.substring(0, 100);
	}
	var wo = document;
	if (window != window.top) {
		wo = window.top.document;
	}

	var boxId = GenerateBoxId(cutText); 
	var maxZindex = getMaxZIndex();

	// Clona
	var div;
	if (Br.IE) {
		div = wo.createElement('iframe');
		div.id = 'clona' + boxId;
		div.style.zIndex = maxZindex + 1;
		div.className = "clona";
	}
	else {
		div = wo.createElement('div');
		div.id = 'clona' + boxId;
		div.style.zIndex = maxZindex + 1;
		div.className = "clona";
	}
	wo.body.appendChild(div);

	//margin-left: (-1)*(sirka)/2 :)
	//margin-top: (-1)*(vyska)/2 :)    
	// hlavni okno	
	var boxdiv;
	boxdiv = wo.createElement('div');
	boxdiv.setAttribute('id', boxId);
	boxdiv.className = 'fM';
	boxdiv.style.width = width + 'px';
	boxdiv.style.marginLeft = (-1) * (width / 2) + 'px';
	boxdiv.style.marginTop = (-1) * (height / 2) + 'px';
	boxdiv.style.zIndex = maxZindex + 2;
	wo.body.appendChild(boxdiv);

	//nastaveni hlavicky
	//hlavicka
	var headerDiv = wo.createElement('div');
	headerDiv.className = 'head';

	//logo
	var logo_area = wo.createElement('div');
	logo_area.className = "logo";

	var logo_link = wo.createElement('a');
	logo_link.setAttribute("href", "http://www.netdirect.cz");
	logo_link.innerHTML = "NetDirect s.r.o.";
	logo_area.appendChild(logo_link);

	headerDiv.appendChild(logo_area);

	//text
	var headerText = wo.createElement('h3');
	if (isNotNull(atrHeaderText))
		headerText.innerHTML = atrHeaderText;
	else
		headerText.innerHTML = "&nbsp;";
	headerDiv.appendChild(headerText);

	// buttons area
	var btn_area = wo.createElement('div');
	btn_area.className = "sysButtons";

	if (atrWithHelp == '1') {
		// help	
		var help_button = wo.createElement('a');
		help_button.setAttribute("title", atrHelpWindowHeaderText);
		help_button.className = "quest btn";
		help_button.innerHTML = "?<span></span>";
		help_button.onclick = function()
		{ ShowHelp(atrHelpWindowHeaderText, atrFooterText, atrCloseButtonTitle); }
		btn_area.appendChild(help_button);
	}

	// close
	var close_button = wo.createElement('a');
	close_button.setAttribute("title", atrCloseButtonTitle);
	close_button.className = "close btn";
	close_button.innerHTML = "x<span></span>";
	close_button.onclick = function()
	{ hide_box(boxId); }
	btn_area.appendChild(close_button);


	headerDiv.appendChild(btn_area);
	boxdiv.appendChild(headerDiv);

	//nastaveni tela
	var body_area = wo.createElement('div');
	body_area.className = "body fMBody";
	body_area.style.height = height + 'px';

	var content_env = wo.createElement('div');
	content_env.className = "content_env";

	var content = wo.createElement('div');
	content.className = "content";

	var text_area = wo.createElement('p');
	text_area.innerHTML = text;

	var btn_area = wo.createElement('p');
	btn_area.className = "btn_close";
	var close_button1 = wo.createElement('input');
	close_button1.setAttribute("type", "button");
	close_button1.setAttribute("title", atrCloseButtonTitle);
	close_button1.setAttribute("value", atrCloseButtonTitle);
	close_button1.onclick = function()
	{ hide_box(boxId); }

	content.appendChild(text_area);
	btn_area.appendChild(close_button1);
	content.appendChild(btn_area);
	content_env.appendChild(content);
	body_area.appendChild(content_env);

	boxdiv.opener = document;
	boxdiv.contents = body_area;
	boxdiv.appendChild(body_area);

	//nastaveni paticky
	//paticka
	var bottomDiv = wo.createElement('div');
	bottomDiv.className = 'footer';

	//text
	var bottomText = wo.createElement('p');
	bottomText.innerHTML = atrFooterText + "&nbsp;";

	bottomDiv.appendChild(bottomText);
	boxdiv.appendChild(bottomDiv);

	return;
}

function getMaxZIndex() {
	var wo = document;
	if (window != window.top) {
		wo = window.top.document;
	}
	var allElems = wo.getElementsByTagName ?
	wo.getElementsByTagName("*") :
	wo.all; // or test for that too
	var maxZIndex = 0;
	for (var i = 0; i < allElems.length; i++) {
		var elem = allElems[i];
		var cStyle = null;
		if (elem.currentStyle) { cStyle = elem.currentStyle; }
		else if (wo.defaultView && wo.defaultView.getComputedStyle) {
			cStyle = wo.defaultView.getComputedStyle(elem, "");
		}
		var sNum;
		if (cStyle) {
			sNum = Number(cStyle.zIndex);
		} else {
			sNum = Number(elem.style.zIndex);
		}
		if (!isNaN(sNum)) {
			maxZIndex = Math.max(maxZIndex, sNum);
		}
	}
	return maxZIndex;
}

function SetRetrunValue(retObjID, value, frameID, submitFormID) {
	var obj;
	if (frameID != null && frameID != undefined && frameID != "") {
		var fr = window.top.document.getElementById(frameID);
		if (fr != null && fr != undefined && fr != "") {
			var opener = fr.opener;
			if (opener != null && opener != undefined && opener != "") {
				obj = opener.getElementById(retObjID);
				if (obj != null && obj != undefined && obj != "") {
					obj.value = value;
					obj.focus();
				}

				if (submitFormID != null && submitFormID != undefined && submitFormID != "") {
					var form = opener.getElementById(submitFormID);
					if (form != null && form != undefined && form != "")
						form.submit();
				}
			}
		}
	}
	else {
		obj = window.top.document.getElementById(retObjID);
		if (obj != null && obj != undefined && obj != "") {
			obj.value = value;
			obj.focus();
		}

		if (submitFormID != null && submitFormID != undefined && submitFormID != "") {
			var form = window.top.document.getElementById(submitFormID);
			if (form != null && form != undefined && form != "")
				form.submit();
		}
	}
}

function GenerateBoxId(url) {
	return sha1Hash(url);
}

function sha1Hash(msg) {
	// constants [§4.2.1]
	var K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6];


	// PREPROCESSING 

	msg += String.fromCharCode(0x80); // add trailing '1' bit to string [§5.1.1]

	// convert string msg into 512-bit/16-integer blocks arrays of ints [§5.2.1]
	var l = Math.ceil(msg.length / 4) + 2;  // long enough to contain msg plus 2-word length
	var N = Math.ceil(l / 16);              // in N 16-int blocks
	var M = new Array(N);
	for (var i = 0; i < N; i++) {
		M[i] = new Array(16);
		for (var j = 0; j < 16; j++) {  // encode 4 chars per integer, big-endian encoding
			M[i][j] = (msg.charCodeAt(i * 64 + j * 4) << 24) | (msg.charCodeAt(i * 64 + j * 4 + 1) << 16) |
                      (msg.charCodeAt(i * 64 + j * 4 + 2) << 8) | (msg.charCodeAt(i * 64 + j * 4 + 3));
		}
	}
	// add length (in bits) into final pair of 32-bit integers (big-endian) [5.1.1]
	// note: most significant word would be ((len-1)*8 >>> 32, but since JS converts
	// bitwise-op args to 32 bits, we need to simulate this by arithmetic operators
	M[N - 1][14] = ((msg.length - 1) * 8) / Math.pow(2, 32); M[N - 1][14] = Math.floor(M[N - 1][14])
	M[N - 1][15] = ((msg.length - 1) * 8) & 0xffffffff;

	// set initial hash value [§5.3.1]
	var H0 = 0x67452301;
	var H1 = 0xefcdab89;
	var H2 = 0x98badcfe;
	var H3 = 0x10325476;
	var H4 = 0xc3d2e1f0;

	// HASH COMPUTATION [§6.1.2]

	var W = new Array(80); var a, b, c, d, e;
	for (var i = 0; i < N; i++) {

		// 1 - prepare message schedule 'W'
		for (var t = 0; t < 16; t++) W[t] = M[i][t];
		for (var t = 16; t < 80; t++) W[t] = ROTL(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1);

		// 2 - initialise five working variables a, b, c, d, e with previous hash value
		a = H0; b = H1; c = H2; d = H3; e = H4;

		// 3 - main loop
		for (var t = 0; t < 80; t++) {
			var s = Math.floor(t / 20); // seq for blocks of 'f' functions and 'K' constants
			var T = (ROTL(a, 5) + f(s, b, c, d) + e + K[s] + W[t]) & 0xffffffff;
			e = d;
			d = c;
			c = ROTL(b, 30);
			b = a;
			a = T;
		}

		// 4 - compute the new intermediate hash value
		H0 = (H0 + a) & 0xffffffff;  // note 'addition modulo 2^32'
		H1 = (H1 + b) & 0xffffffff;
		H2 = (H2 + c) & 0xffffffff;
		H3 = (H3 + d) & 0xffffffff;
		H4 = (H4 + e) & 0xffffffff;
	}

	return H0.toHexStr() + H1.toHexStr() + H2.toHexStr() + H3.toHexStr() + H4.toHexStr();
}

//
// function 'f' [§4.1.1]
//
function f(s, x, y, z) {
	switch (s) {
		case 0: return (x & y) ^ (~x & z);           // Ch()
		case 1: return x ^ y ^ z;                    // Parity()
		case 2: return (x & y) ^ (x & z) ^ (y & z);  // Maj()
		case 3: return x ^ y ^ z;                    // Parity()
	}
}

//
// rotate left (circular left shift) value x by n positions [§3.2.5]
//
function ROTL(x, n) {
	return (x << n) | (x >>> (32 - n));
}

//
// extend Number class with a tailored hex-string method 
//   (note toString(16) is implementation-dependant, and 
//   in IE returns signed numbers when used on full words)
//
Number.prototype.toHexStr = function() {
	var s = "", v;
	for (var i = 7; i >= 0; i--) { v = (this >>> (i * 4)) & 0xf; s += v.toString(16); }
	return s;
}
