var location_path = GetLocation();
var isprint = GetState();
var state;
var id_content = "content";
var id_link_print = "linkPrintVersion";
var arr_elem = new Array();
var arr_elem_val = new Array();

var cur_lang = location.href + "";
if (cur_lang.indexOf("eng") == -1) {
	var alt_print = "Распечатать страницу";
	var alt_version = "Версия для печати";
	var alt_back = "Вернуться назад";
} else {
	var alt_print = "Print the page";
	var alt_version = "Version for print";
	var alt_back = "Back";
}

function BuildPrintVersion() {
	var link = document.getElementById(id_link_print);
	var linkPrint = document.getElementById("id_linkToPrintPage");

	var content_elem = document.getElementById(id_content);
	if(content_elem == null) {
		alert("Для данного сервиса требуется более поздняя версия браузера!");
		return;
	}

	var all_colls = document.getElementsByTagName("TABLE");
	var content_colls = content_elem.getElementsByTagName("TABLE");

	if(state == null) {

		var left_elem = document.getElementById("left_col");
		if (left_elem != null) left_elem.style.display = "none";
		var right_elem = document.getElementById("right_col");
		if (right_elem != null) right_elem.style.display = "none";

		var head_elem = document.getElementById("text_head");
		if (head_elem != null) head_elem.style.display = "";
		var bottom_elem = document.getElementById("text_bottom");
		if (bottom_elem != null) bottom_elem.style.display = "";

		var arr = new Array(content_elem);

		for(var i = 0; i < all_colls.length; i++) {
			var elem = all_colls[i];
			var elem_colls = elem.getElementsByTagName("TABLE");
			for(var j = 0; j < elem_colls.length; j++) {
				if(elem_colls[j] == content_elem) {
					arr[arr.length] = elem;
					break;
				}
			}
			arr_elem[arr_elem.length] = elem;
			arr_elem_val[arr_elem_val.length] = elem.style.display;
			elem.style.display = "none";
		}

		for(var i = 0; i < content_colls.length; i++) arr[arr.length] = content_colls[i];
		for(var i = 0; i < arr.length; i++) arr[i].style.display = "block";

		var table = arr[1];
		var content_cell;
		for(var i = 0; i < table.rows.length; i++) {
			for(var j = 0; j < table.rows[i].cells.length; j++) {
				var cell = table.rows[i].cells[j];
				if(content_cell == null) content_cell = cell;
				if(cell.offsetWidth > content_cell.offsetWidth) content_cell = cell;
			}
			for(var j = 0; j < table.rows[i].cells.length; j++) {
				var cell = table.rows[i].cells[j];
				if(cell.offsetHeight > content_cell.offsetHeight) content_cell = cell;
			}
		}
		
		for(var i = 0; i < table.rows.length; i++) {
			for(var j = 0; j < table.rows[i].cells.length; j++) {
				var cell = table.rows[i].cells[j];
				if(cell == content_cell) continue;
				arr_elem[arr_elem.length] = cell;
				arr_elem_val[arr_elem_val.length] = cell.style.display;
				cell.style.display = "none";
			}
		}
		
		if(link != null) {
		  if (linkPrint != null) linkPrint.innerHTML = "<img src=\"print.gif\" alt=\"" + alt_print + "\">";
		  link.innerHTML = "<img src=\"/-template/image/print_back.gif\" hspace=\"2\" alt=\"" + alt_back + "\">";	
		  state = true;
		}

		window.location.href = "#print";
	} else {
		for(var i = 0; i < arr_elem.length; i++) {
			var elem = arr_elem[i];
			elem.style.display = arr_elem_val[i];
		}
		var left_elem = document.getElementById("left_col");
		if (left_elem != null) left_elem.style.display = "";
		var right_elem = document.getElementById("right_col");
		if (right_elem != null) right_elem.style.display = "";

		var head_elem = document.getElementById("text_head");
		if (head_elem != null) head_elem.style.display = "none";
		var bottom_elem = document.getElementById("text_bottom");
		if (bottom_elem != null) bottom_elem.style.display = "none";

		if(link != null) {
		  if (linkPrint != null) linkPrint.innerHTML = "";
		  link.innerHTML = "<img src=\"print.gif\" alt=\"" + alt_version + "\">";
		  state = null;
		}

		window.location.href = location_path + "#web";
	}
}

function GetLocation() {
	var path = window.location.href;
	var hash = window.location.hash;
	var ind = path.lastIndexOf(hash);
	if(ind != -1) path = path.substring(0, ind);
	return path;
}

function GetState() {
	var hash = (window.location.hash + "").toLowerCase();
	var ind = hash.indexOf("#");
	if(ind != -1) hash = hash.substring(ind + 1);
	if(hash == "print") return true;
	return false;
}

