(function ($) {

$(document).ready(function() {

CommonBehaviour.apply();
UserPageBehaviour.apply();

//CommonBehaviour.highlightInactiveLinks();

});


PureCommonBehaviour = {
	preloadImages: function() {
		var root = "/img/", pic;
		var img = [
			/* calc only */
			"calc/checked-down-arrow.png",
			"calc/checked-right-arrow.png",
			"calc/highlit-down-arrow.png",
			"calc/right-arrow.png",
			"calc/r11-bright-inner-outline.gif",
			"calc/dropdown-bottom-shade.png",
			"calc/dropdown-shade.png",
			"calc/dropdown-shaded-left-border.png",
			"calc/dropdown-shaded-right-border.png",
			"calc/hovering-panel.png",
			"calc/prompt/prompt_01.png",
			"calc/prompt/prompt_02.png",
			"calc/prompt/prompt_03.png",
			"calc/prompt/prompt_04.png",
			"calc/prompt/prompt_06.png",
			"calc/prompt/prompt_07.png",
			"calc/prompt/prompt_08.png",
			"calc/prompt/prompt_09.png",
			"datepicker/datepicker_b.png",
			"datepicker/datepicker_bl.png",
			"datepicker/datepicker_br.png",
			"datepicker/datepicker_l.png",
			"datepicker/datepicker_r.png",
			"datepicker/datepicker_t.png",
			"datepicker/datepicker_tl.png",
			"datepicker/datepicker_tr.png",
			"datepicker/round_borders_thick_gray.png",
			"datepicker/round_corners_gray.png",
			/* global litebox */
			"popup/litebox_01_left.png",
			"popup/litebox_01_inner.png",
			"popup/litebox_01_right.png",
			"popup/litebox_02_left.png",
			"popup/litebox_02_right.png",
			"popup/litebox_03_left.png",
			"popup/litebox_03_inner.png",
			"popup/litebox_03_right.png",
			/* photos */
			"photo/001.png",
			"photo/002.png",
			"photo/003.png",
			"photo/004.png"
		];
		for (var i = 0; img && i < img.length; i++) {
			pic = new Image(); pic.src = root + img[i];
		}
	}
};

PureCommonBehaviour.preloadImages();


CommonBehaviour = {
	apply: function() {
		$(".realAvtostrah").text("Автострахование");

		$(".emphasised").addClass("xdecorated");

		Extra.imageRoot = "/img/";
		Extra.decorate();
		//if (document.getElementById("incalc")) Extra.decorateCalculator();

		Foldable.init(".foldable");
		Foldable.init(".foldable2");
		/*Foldable.init("#sections", { 
			item: "section", body: "subsections", siblingsUnaffected: true,
			selected: "li.selected"
		});*/
		$(".foldable2 .item").removeClass("unfolded");
		$(".foldable2 .item .tip").hide();
		$(".foldable2 .item:first").addClass("unfolded");
		$(".foldable2 .item:first .tip").show();


		MenuAnchor.init(".menu li a");
		SortableTable.initCalcTables();
		FormValidation.init();

		this.bindLiteboxCalls();
		this.decorateUnderlyingLabels();
		this.initialiseContactsDropDown();

		Calendar.init();

		$("#contents .sidebar .shaded .title").each(function() {
			var ref = $(this).find("a").get(0);
			if (ref) {
				$(this).click(function() {
					document.location.href = $(ref).attr("href");
				});
				$(this).css("cursor", "pointer");
			}
		});

		//PureCommonBehaviour.preloadImages();
		this.tunePrintLayout();
		this.renderClickableBlocks();

		var e = document.getElementById("storyPhoto");
		if (e && e.style.visibility == "hidden") e.style.visibility = "visible";
	},
	bindLiteboxCalls: function() {
		$(".litebox .form").css({ paddingLeft: "36px", paddingRight: "36px" });
		$("#phoneBanner, #liteboxCallbackOrderRef").click(function() {
			GlobalLitebox.show("#liteboxCallbackOrderForm");
			return false;
		});
		$("#liteboxCalcSampleRef").click(function(){
			GlobalLitebox.show("#liteboxCalcSample");
			return false;
		});
		$("#liteboxRegistrationRef").click(function() {
			GlobalLitebox.show("#liteboxRegistrationForm");
			return false;
		});
	},
	initialiseContactsDropDown: function() {
		var phoneImageRoot = "/img/phone/";
		var phoneElement = $("#phoneBanner img").get(0);
		DropDown.init();
		var loc = [
			"ekaterinburg", "kazan", "krasnodar", "krasnojarsk", "moscow",
			"n-novgorod", "perm", "rostov-na-donu", "samara", "saratov",
			"st-petersburg", "tula",
			"arhangelsk", "barnaul", "voronezh", "vladimir", "novgorod",
			"izhevsk", "irkutsk", "novosibirsk", "omsk", "riazan",
			"tiumen", "ufa"
		];
		var locId = [
			"Ekaterinburg", "Kazan", "Krasnodar", "Krasnojarsk", "Moscow",
			"NNovgorod", "Perm", "RostovNaDonu", "Samara", "Saratov",
			"StPetersburg", "Tula",
			"Arhangelsk", "Barnaul", "Voronezh", "Vladimir", "Novgorod",
			"Izhevsk", "Irkutsk", "Novosibirsk", "Omsk", "Riazan",
			"Tiumen", "Ufa"
		];
		var setLoc = function(k) {
			$("#loc" + locId[k]).click(function() {
				$(phoneElement).attr("src", phoneImageRoot + loc[k] + ".gif");
				return false;
			});
		};
		for (var i = 0; i < loc.length; i++) setLoc(i);
		$("#regionSwitchAlt").change(function() {
			var v = $(this).val();
			if (v) $(phoneElement).attr("src", phoneImageRoot + v + ".gif");
		});
	},
	decorateUnderlyingLabels: function() {
		$("label.underlying").each(function() {
			var id = $(this).attr("for");
			var l = $(this), i = $("#" + id);
			var disable = function() {
				if ((!i.val() || !i.val().replace(/(^\s*|\s*$)/, "")) && l && l.text()) {
					i.addClass("shaded"); i.val(l.text());
				}
			}
			var enable = function() {
				i.removeClass("shaded");
				if (l && l.text() && i.val() == l.text()) i.val("");
			}
			disable();
			i.focus(enable);
			i.blur(disable);
			i.closest('form').submit(function(){
				enable();
				return true;
			});
		});
	},
	tunePrintLayout: function() {
		$(".printLayout table.line td").each(function() {
			if (!$(this).hasClass("blank"))
				$(this).css("width", $(this).width() + "px");
		});
	},
	highlightInactiveLinks: function() {
		$("a[href=#]").each(function(){
			if (!$(this).hasClass('scripted') && !($(this).data('events'))){
				$(this).css({"background-color": "yellow", "color": "red"});
			}
		});
		$("a.scripted").each(function(){
			if (!($(this).data('events'))){
				$(this).css({"background-color": "#ff26ff", "color": "#7aff92"});
			}
		});
	},
	renderClickableBlocks: function() {
		$(".refBlock").each(function() {
			var e = $(this), a = e.find("a").get(0), ref = null;
			if (a) ref = a.getAttribute("href");
			if (ref) {
				e.click(function() { document.location.href = ref; });
				e.css("cursor", "pointer");
			}
		});
	}
}


UserPageBehaviour = {
	apply: function() {
		$("#userPage .extraInfo .hideButton").click(function() {
			$(this).parent().hide();
		});
		$("#userInsuranceFilterSwitchOff").click(function() {
			$(this).hide();
			$(this).parent().siblings("h3").addClass("obscure");
			$("#userInsuranceFilterForm").hide();
			$("#userInsuranceFilterSwitchOn").show();
			return false;
		});
		$("#userInsuranceFilterSwitchOn").click(function() {
			$(this).hide();
			$(this).parent().siblings("h3").removeClass("obscure");
			$("#userInsuranceFilterForm").show();
			$("#userInsuranceFilterSwitchOff").show();
			return false;
		});
		$("#userCalcFilterSwitchOff").click(function() {
			$(this).hide();
			$(this).parent().siblings("h3").addClass("obscure");
			$("#userCalcFilterForm").hide();
			$("#userCalcFilterSwitchOn").show();
			return false;
		});
		$("#userCalcFilterSwitchOn").click(function() {
			$(this).hide();
			$(this).parent().siblings("h3").removeClass("obscure");
			$("#userCalcFilterForm").show();
			$("#userCalcFilterSwitchOff").show();
			return false;
		});

		this.tuneDimensions();
		$(window).resize(this.tuneDimensions);
	},
	tuneDimensions: function() {
		var sidebar = $("#contents .sidebar");
		var userPage = $("#userPage"), dh = 40;
		userPage.css("height", "auto");
		if (sidebar.height() - dh > userPage.height())
			userPage.height(sidebar.height() - dh);
	}
};


MenuAnchor = {
	toggle: function(menuAnchor) {
		var li = $(menuAnchor.parents("li").get(0));
		var ul = li.parent();
		ul.find("li").each(function() {
			$(this).removeClass("selected");
			$(this).removeClass("xdecorated");
			$(this).find(".corner").remove();
		});
		if (!li.hasClass("selected")) {
			li.addClass("selected");
			if (!li.hasClass("xdecorated")) li.addClass("xdecorated");
			Extra.decorate(li.get(0));
		}
		this.switchLayer(menuAnchor);
	},
	switchLayer: function(menuAnchor) {
		var li = $(menuAnchor.parents("li").get(0));
		if (!li.hasClass("selected")) return;
		var ul = li.parent();
		var anchorId = menuAnchor.attr("href").replace(/.*\#/, "");
		if (anchorId) {
			var anchor = $("#" + anchorId);
			var article = anchor.parent("div");
			var tipId = "#" + anchorId + "Tip";
			if (article) {
				article.siblings().hide();
				article.show();
			}
			ul.siblings(".tip").each(function() {
				if (!$(this).is(tipId)) $(this).fadeOut("fast");
				else if ($(this).css("display") == "none")
					$(this).fadeIn("fast");
			});
		}
	},
	init: function(selector) {
		$(selector).each(function() {
			MenuAnchor.switchLayer($(this));
		});
		$(selector).click(function(event) {
			event.preventDefault();
			MenuAnchor.toggle($(this));
			return false;
		});
	}
};


Foldable = {
	init: function(parentSelector, structure) {
		if (!parentSelector) parentSelector = ".foldable";
		if (!structure) structure = {};
		var defaultStructure = {
			item: "item", title: "title", body: "tip", active: "scripted",
			unfolded: "unfolded", siblingsUnaffected: 0, initiallyUnfolded: 0,
			selected: null
		};
		for (var attr in defaultStructure) {
			if (!structure[attr])
				structure[attr] = defaultStructure[attr];
		}
		$(parentSelector).each(function() {
			if ($(this).find("." + structure.item).get().length == 0)
				$(this).hide();
			var folded = 0;
			var hasSelectedItems = (structure.selected && 
				$(this).find(structure.selected).get().length > 0);
			var hasReferenceToCurrentPage = false, ref = document.location.href;
			$(this).find("." + structure.item + " a").each(function() {
				hasReferenceToCurrentPage |= ref.indexOf($(this).attr("href")) > -1;
			});
			$(this).find("." + structure.item).each(function(index) {
				var refers = false;
				$(this).find("a").each(function() {
					refers |= ref.indexOf($(this).attr("href")) > -1;
				});
				if (hasSelectedItems) {
					if ($(this).find(structure.selected).get().length > 0) {
						$(this).addClass(structure.unfolded);
						$(this).find("." + structure.body).css("display", "block");
					}
					else {
						$(this).removeClass(structure.unfolded);
						$(this).find("." + structure.body).css("display", "none");
					}
				}
				else if (structure.initiallyUnfolded)
					$(this).addClass(structure.unfolded);
				else {
					folded = hasReferenceToCurrentPage ? !refers : index != 0;
					if (folded && !structure.siblingsUnaffected) {
						$(this).removeClass(structure.unfolded);
						$(this).find("." + structure.body).css("display", "none");
					}
					else {
						$(this).addClass(structure.unfolded);
						$(this).find("." + structure.body).css("display", "block");
					}
				}
			});
			$(this).find("." + structure.active).click(function(event) {
				var siblingBodies = $(this).parents("." + structure.item).find("." + structure.body);
				var hasVisibleBody = false;
				siblingBodies.each(function() { hasVisibleBody |= $(this).css("display") != "none"; });
				if (!hasVisibleBody) {
					if (!structure.siblingsUnaffected) {
						$(this).parents(parentSelector).find("." + structure.body).each(function() {
							$(this).slideUp("normal");
							$(this).parent().removeClass(structure.unfolded);
						});
					}
					else {
						siblingBodies.each(function() {
							$(this).slideUp("normal");
							$(this).parent().removeClass(structure.unfolded);
						});
					}
					siblingBodies.each(function() {
						if ($(this).css("display") == "none") {
							$(this).slideDown("normal");
							if (!$(this).parent().hasClass(structure.unfolded))
								$(this).parent().addClass(structure.unfolded);
						}
					});
				}
				event.preventDefault();
				return false;
			});
		});
	}
};


GlobalLitebox = {
	global: { wrapping: false, parent: null, width: null, top: null },
	liteboxId: "litebox",
	wrapperId: "liteboxWrapper",
	hideButtonId: "liteboxHideButton",
	shaderId: "liteboxShader",
	instance: {},
	cornerImage: {
		tl: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAE1JREFUeNpi+P//PwMaVgfiXiA+A8R/0CWLgPjbfyQAk2AD4gP/sQCYgjn/cQCQpMF/PICJgYEhnwEPACmwwaeAEWjKTyDNhksBQIABAFT/lpZlCm0EAAAAAElFTkSuQmCC",
		tr: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAEpJREFUeNpi+P///08gPgPE7UCsCMQMyBhEIIPPQJyGTwEMbARiZnwKQKAXpIDxP1gZVvAXiDWYGHADZiDOwWcCCJwlpOA1QIABAGqwgd850mJqAAAAAElFTkSuQmCC",
		bl: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAEtJREFUeNpi/P///08GBgY2BhyACYgfMeABIAVH8ClgAFphAMR//uMADFA8h5ACNiDehU8BDBcB8TdkBYwgCTSgAsTJQOwKYgMEGABcu4ESCQzDqAAAAABJRU5ErkJggg==",
		br: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAEhJREFUeNpi/A8EDLjBayYG/OARIQVHGPFY8QuItfGZMAWI7zD8xw7WATEz2HA0ic9AnAaSgGEQ8RaITwJxCxDLIUuCMECAAQDlYGE5TdVPegAAAABJRU5ErkJggg=="
	},
	create: function(p) {
		var instance = null;
		var idSuffix = ("" + Math.random()).replace(".", "").substring(1, 7);
		var liteboxId = this.liteboxId + idSuffix;
		var wrapperId = this.wrapperId + idSuffix;
		var hideButtonId = this.hideButtonId + idSuffix;
		var shaderId = this.shaderId + idSuffix;

		var wrapper = document.createElement("tbody");
		var wrapperCell = new Array(9), r = null;
		for (var i = 0; i < wrapperCell.length; i++) {
			if (i % 3 == 0) r = document.createElement("tr");
			wrapperCell[i] = document.createElement("td");
			wrapperCell[i].style.padding = "0";
			if (i != 4) wrapperCell[i].style.fontSize = "0";
			else wrapperCell[i].style.padding = "12px";
			if (this.browser() < 2 || i == 1 || i == 3 || i == 4 || i == 5 || i == 7)
				wrapperCell[i].style.background = "#fff";
			if (r) r.appendChild(wrapperCell[i]);
			if (r && i % 3 == 2) wrapper.appendChild(r);
		}
		//if (this.browser() > 1) {
			wrapperCell[0].innerHTML = '<img src="' + this.cornerImage.tl + '" alt="" width="8" height="8"/>';
			wrapperCell[2].innerHTML = '<img src="' + this.cornerImage.tr + '" alt="" width="8" height="8"/>';
			wrapperCell[6].innerHTML = '<img src="' + this.cornerImage.bl + '" alt="" width="8" height="8"/>';
			wrapperCell[8].innerHTML = '<img src="' + this.cornerImage.br + '" alt="" width="8" height="8"/>';
		//}
		
		var twrapper = document.createElement("table");
		twrapper.style.border = "none";
		twrapper.style.borderCollapse = "collapse";
		twrapper.style.margin = "0";
		twrapper.appendChild(wrapper);

		var xwrapper = document.createElement("div");
		xwrapper.setAttribute("id", wrapperId);
		xwrapper.appendChild(twrapper);
		xwrapper.style.display = "none";
		xwrapper.style.zIndex = "9901";
		xwrapper.style.position = "absolute";
		xwrapper.style.margin = "0";

		var litebox = document.createElement("div");
		if (typeof(p) != "undefined" && typeof(p.liteboxId) != "undefined") {
			if (this.global.wrapping) litebox = document.getElementById(p.liteboxId);
			else litebox.setAttribute("id", p.liteboxId);
		}
		else litebox.setAttribute("id", liteboxId);
		litebox.style.display = "block";
		if (!this.global.wrapping && typeof(p) != "undefined" && typeof(p.contents) != "undefined")
			litebox.innerHTML = p.contents;

		var hideButton = document.createElement("div");
		hideButton.innerHTML = "&times;";
		hideButton.setAttribute("id", hideButtonId);
		hideButton.style.font = "normal 20px arial";
		hideButton.style.color = "#d80a0a";
		hideButton.style.position = "absolute";
		hideButton.style.right = "8px";
		hideButton.style.top = "0px";
		hideButton.style.cursor = "pointer";
		
		var shader = document.createElement("div");
		shader.setAttribute("id", shaderId);
		shader.style.display = "none";
		shader.style.backgroundColor = "#000";
		shader.style.opacity = "0.5";
		shader.style.filter = "alpha(opacity=50)";
		shader.style.zIndex = "9900";

		if (wrapperCell && wrapperCell[4]) {
			wrapperCell[4].appendChild(hideButton);
			wrapperCell[4].appendChild(litebox);
			instance = { self: litebox, wrapper: xwrapper, shader: shader, 
				hideButton: hideButton, id: idSuffix };
			hideButton.onclick = function() { GlobalLitebox.hide(instance); };
			if (typeof(p) != "undefined" && typeof(p.parent) != "undefined") {
				this.append(instance, p.parent);
				instance.parent = p.parent;
			}
		}
		this.instance[idSuffix] = instance;
		return instance;
	},
	append: function(litebox, parent) {
		if (parent && litebox) {
			if (litebox.shader) parent.appendChild(litebox.shader);
			if (litebox.wrapper) parent.appendChild(litebox.wrapper);
		}
	},
	show: function(e, p) {
		var litebox = this.getInstance(e);
		if (!litebox) {
			var parent = this.global.parent ? this.global.parent :
				(document.body ? document.body : document.getElementsByTagName("body")[0]);
			var contents = "", liteboxId = null, source = null;
			if (typeof(e) == "string") {
				if (e.match(/^\#[^\s]+$/)) {
					var id = e.replace("#", "");
					e = document.getElementById(id);
					if (e) { contents = e.innerHTML; liteboxId = id; source = e; }
				}
				else contents = e;
			}
			else if (typeof(e) != "undefined" && typeof(e.innerHTML) != "undefined") {
				contents = e.innerHTML; source = e;
			}
			litebox = this.create({ parent: parent, contents: contents, liteboxId: liteboxId });
		}	
		if (litebox) {
			if (litebox.wrapper) litebox.wrapper.style.display = "block";
			if (litebox.shader) litebox.shader.style.display = "block";
			if (!this.global.wrapping && source && (typeof(p) == "undefined" || 
				typeof(p.keepSourceElement) == "undefined" || !p.keepSourceElement))
				source.style.display = "none";

			var f = function() { GlobalLitebox.locate(litebox, p); };
			if (window.addEventListener)
				window.addEventListener("resize", f, false);
			else if (window.attachEvent)
				window.attachEvent("onresize", f);
			else window.onresize = f;
			f();
		}
		return litebox;
	},
	hide: function(e) {
		var litebox = this.getInstance(e);
		if (!litebox) return;
		if (litebox.wrapper) {
			if (litebox.wrapper.parentNode && !this.global.wrapping)
				litebox.wrapper.parentNode.removeChild(litebox.wrapper);
			else litebox.wrapper.style.display = "none";
		}
		if (litebox.shader) {
			if (litebox.shader.parentNode && !this.global.wrapping)
				litebox.shader.parentNode.removeChild(litebox.shader);
			else litebox.shader.style.display = "none";
		}
		if (typeof(litebox.id) != "undefined" && !this.global.wrapping)
			this.instance[litebox.id] = null;
	},
	getInstance: function(e) {
		var id = null, instance = null;
		if (typeof(e) == "string") {
			if (e.match(/^\#[^\s]+$/)) {
				var eid = e.replace("#", ""), lid;
				e = document.getElementById(eid);
				for (var i in this.instance) {
					lid = this.instance[i] ? this.instance[i].self.getAttribute("id") : null;
					if (lid == eid) instance = this.instance[i];
				}
			}
			else id = e;
		}
		else if (typeof(e) != "undefined" && typeof(e.getAttribute) != "undefined") {
			var eid = e.getAttribute("id"), lid;
			for (var i in this.instance) {
				lid = this.instance[i] ? this.instance[i].self.getAttribute("id") : null;
				if (lid == eid) instance = this.instance[i];
			}
		}
		else if (typeof(e) != "undefined" && typeof(e.id) != "undefined")
			id = e.id;

		if (id && !instance) {
			for (var i in this.instance) {
				if (i == id) instance = this.instance[i];
			}
		}
		return instance;
	},
	locate: function(litebox, p) {
		if (!litebox) return;
		var screen = this.getScreenDimensions();
		if (litebox.shader) {
			if (!GlobalLitebox.browser())
				litebox.shader.style.position = "absolute";
			else litebox.shader.style.position = "fixed";
			litebox.shader.style.left = "0";
			litebox.shader.style.top = "0";
			litebox.shader.style.width = screen.width + "px";
			litebox.shader.style.height = screen.height + "px";
		}
		if (litebox.wrapper) {
			if (typeof(p) != "undefined" && typeof(p.width) == "function")
				litebox.wrapper.style.width = p.width();
			else if (typeof(p) != "undefined" && typeof(p.width) != "undefined")
				litebox.wrapper.style.width = p.width;
			else if (typeof(this.global.width) != "undefined" && this.global.width)
				litebox.wrapper.style.width = this.global.width;

			var offset = this.getOffset();
			var w = litebox.wrapper.offsetWidth, h = litebox.wrapper.offsetHeight;
			var x = offset.x + screen.width/2 - w/2, y = offset.y + screen.height/2 - h/2;

			if (typeof(p) != "undefined" && typeof(p.left) == "function")
				litebox.wrapper.style.left = p.left();
			else if (typeof(p) != "undefined" && typeof(p.left) != "undefined")
				litebox.wrapper.style.left = p.left;
			else if (typeof(p) != "undefined" && typeof(p.right) == "function")
				litebox.wrapper.style.right = p.right();
			else if (typeof(p) != "undefined" && typeof(p.right) != "undefined")
				litebox.wrapper.style.right = p.right;
			else litebox.wrapper.style.left = (x < 12 ? 12 : x) + "px";

			if (typeof(p) != "undefined" && typeof(p.top) == "function")
				litebox.wrapper.style.top = p.top();
			else if (typeof(p) != "undefined" && typeof(p.top) != "undefined")
				litebox.wrapper.style.top = p.top;
			else if (typeof(p) != "undefined" && typeof(p.bottom) == "function")
				litebox.wrapper.style.bottom = p.bottom();
			else if (typeof(p) != "undefined" && typeof(p.bottom) != "undefined")
				litebox.wrapper.style.bottom = p.bottom;
			else if (typeof(this.global.top) != "undefined" && this.global.top)
				litebox.wrapper.style.top = this.global.top;
			else litebox.wrapper.style.top = (y < 12 ? 12 : y) + "px";
		}
	},
	getScreenDimensions: function() {
		var w = 0, h = 0;
		if (self.innerHeight) {
			w = self.innerWidth;
			h = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight) {
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		else if (document.body) {
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
		return { width: w, height: h };
	},
	getOffset: function() {
		var xs = 0, ys = 0;
		if (self.pageYoffset) {
			xs = self.pageXoffset;
			ys = self.pageYoffset;
		}
		else if (document.documentElement && document.documentElement) {
			xs = document.documentElement.scrollLeft;
			ys = document.documentElement.scrollTop;
		}
		else if (document.body) {
			xs = document.body.scrollLeft;
			ys = document.body.scrollTop;
		}
		return { x: xs, y: ys };
	},
	set: function(p) {
		if (typeof(p) != "undefined" && p) {
			for (var a in p) this.global[a] = p[a];
		}
	},
	browser: function() {
		var i0 = navigator.appVersion.indexOf("MSIE");
		var i1 = i0 > -1 ? navigator.appVersion.substring(i0).indexOf(";") : -1;
		var v = (i0 > -1 && i1 > -1) ? parseFloat(navigator.appVersion.substring(i0 + 5, i0 + i1)) : 100.0;
		return (navigator.appName.indexOf("Microsoft") < 0 ? 3 : (v < 7.0 ? 0 : (v < 8.0 ? 1 : 2)));
	}
};


Extra = {
	// pure javascript for possible embedding into html
	imageRoot: "img/",
	decorate: function() {
		var c = [ "tl", "tr", "bl", "br" ];
		var e = arguments && arguments.length > 0 ? arguments : this.all("div", "p");
		for (var i = 0; e && i < e.length; i++) {
			if (e[i].className && e[i].className.match(/(^| )answer( |$)/)) {
				var w = document.createElement("div"); w.className = "decorated replyBubble";
				var a = document.createElement("div"); a.className = "replyArrow";
				var p = e[i].parentNode;
				if (p) {
					p.insertBefore(w, e[i]);
					w.appendChild(a);
					w.appendChild(e[i]);
				}
			}
		}
		e = arguments && arguments.length > 0 ? arguments : this.all("div", "li", "p", "span");
		for (var i = 0; e && i < e.length; i++) {
			if (e[i].className && (e[i].className.match(/(^| )x?decorated( |$)/) || 
				e[i].className.match(/(^| )foldable( |$)/) ||
				e[i].className.match(/(^| )xdecoration( |$)/))) {
				var cs = c;
				if (e[i].className.match(/(^| )corners:[^ ]+/)) {
					cs = e[i].className.replace(/(^|.* )corners:([^ ]+).*/, "$2");
					cs = cs ? cs.split(",") : 0;
				}
				var e2 = e[i].childNodes;
				for (var j = 0; e2 && j < e2.length; j++) {
					if (e2[j].className && e2[j].className.match(/(^| )corner( |$)/))
						e[i].removeChild(e2[j]);
				}
				for (var j = 0; cs && j < cs.length; j++) {
					var t = e[i].tagName ? e[i].tagName.toLowerCase() : "";
					var ce = document.createElement(t && t == "span" ? "span" : "div");
					ce.className = cs[j] + " corner";
					e[i].appendChild(ce);
				}
			}
			if (e[i].className && e[i].className.match(/(^| )panel\d+( |$)/)) {
				var n = e[i].className.replace(/(^|.* )panel(\d+)( .*|$)/, "$2");
				var p = e[i].parentNode;
				if (p && n) {
					var p1 = document.createElement("div"); p1.className = "panel" + n + "a";
					var p2 = document.createElement("div"); p2.className = "panel" + n + "b";
					var p3 = document.createElement("div"); p3.className = "panel" + n + "c";
					var p4 = document.createElement("div"); p4.className = "panel" + n + "d";
					p3.appendChild(p4); p2.appendChild(p3); p1.appendChild(p2);
					p.insertBefore(p1, e[i]);
					p4.appendChild(e[i]);
				}
			}
			if (e[i].className && e[i].className.match(/(^| )fading( |$)/)) {
				e[i].innerHTML = "<div class=\"fader\"><\/div>" + e[i].innerHTML;
			}
			if (e[i].className.match(/(^| )extraInfo( |$)/)) {
				e[i].innerHTML =
					"<div class=\"commentArrow\"><\/div>" +
					"<div class=\"tl corner\"><\/div><div class=\"tr corner\"><\/div>" +
					"<div class=\"bl corner\"><\/div><div class=\"br corner\"><\/div>" +
					"<img src=\"" + this.imageRoot + "calc/bright-cross.gif\" class=\"hideButton\" alt=\"&times;\"/> " +
					e[i].innerHTML;
			}
			/*if (e[i].className && e[i].className.match(/(^| )litebox( |$)/)) {
				e[i].innerHTML =
					"<img src=\"" + this.imageRoot + "calc/bright-cross.gif\" class=\"liteboxHideButton\" alt=\"&times;\"/> " +
					e[i].innerHTML;
			}*/
		}
		var e = this.all("label");
		for (var i = 0; e && i < e.length; i++) {
			if (e[i].className && e[i].className.match(/(^| )required( |$)/))
				e[i].innerHTML += "<span class=\"required\">*<\/span>";
		}
	},
	all: function() {
		var e = new Array(), c;
		for (var i = 0; arguments && i < arguments.length; i++) {
			c = document.getElementsByTagName(arguments[i]);
			for (var j = 0; c && j < c.length; j++)
				e.push(c[j]);
		}
		return e;
	},
	decorateCalculator: function() {
		var w = document.getElementById("incalcNoJavaScriptWarning");
		if (w) w.style.display = "none";
		var incalcBoard = document.getElementById("incalcBoard"), e, s;
		if (!incalcBoard) return;
		incalcBoard.style.display = "block";
		var incalcBlock = incalcBoard.getElementsByTagName("div"), incalcLabel, incalcPrompt;
		var youngIE = (this.browser() == 0);
		for (var i = 0; incalcBlock && i < incalcBlock.length; i++) {
			if (incalcBlock[i].className.match(/(^| )parameter( |$)/)) {
				e = incalcBlock[i].getElementsByTagName("div");
				incalcLabel = 0; incalcPrompt = 0;
				for (var j = 0; e && j < e.length; j++) {
					if (!incalcPrompt && e[j].className.match(/(^| )comment( |$)/))
						incalcPrompt = e[j].innerHTML;
					if (!incalcLabel && e[j].className.match(/(^| )label( |$)/))
						incalcLabel = e[j];
				}
				if (incalcLabel)
					s = "<img src=\"" + this.imageRoot + "calc/none.gif\" class=\"question icon\" alt=\"\"" +
					(youngIE && incalcPrompt ? " title=\"" + this.escapeTags(incalcPrompt) + "\"" : "") + "/> " +
					incalcLabel.innerHTML;
				if (incalcLabel && incalcLabel.className.match(/(^| )empty( |$)/))
					s = "<img src=\"" + this.imageRoot + "calc/none.gif\" class=\"empty icon\" alt=\"\"/> " +
					incalcLabel.innerHTML;
				if (incalcLabel && s && !incalcLabel.className.match(/(^| )plain( |$)/)) 
					incalcLabel.innerHTML = s;
				incalcPrompt = "<label>" + s + "<\/label>" + (incalcPrompt ? incalcPrompt : "");
				if (!youngIE && incalcPrompt && incalcLabel) incalcLabel.innerHTML += 
					"<table class=\"promptWrapper\"><tr><td class=\"ctl\"><\/td>" +
					"<td class=\"bt\"><\/td><td class=\"ctr\"><\/td><\/tr>" + 
					"<tr><td class=\"bl\"><\/td><td class=\"body\">" +
					"<div class=\"prompt\">" + incalcPrompt + "<\/div>" +
					"<\/td><td class=\"br\"><\/td><\/tr>" +
					"<tr><td class=\"cbl\"><\/td><td class=\"bb\"><\/td>" +
					"<td class=\"cbr\"><\/td><\/tr><\/table>";
			}
		}
		var incalcTableCell = incalcBoard.getElementsByTagName("td");
		for (var i = 0; incalcTableCell && i < incalcTableCell.length; i++) {
			if (incalcTableCell[i].className.match(/(^| )info( |$)/))
				incalcTableCell[i].innerHTML =
					"<img src=\"" + this.imageRoot + "calc/none.gif\" class=\"question icon\" alt=\"?\"/> " +
					incalcTableCell[i].innerHTML;
		}
	},
	escapeTags: function(s) {
		return s.replace(/<[^>]+>/ig, "");
	},
	browser: function() {
		var i0 = navigator.appVersion.indexOf("MSIE");
		var i1 = i0 > -1 ? navigator.appVersion.substring(i0).indexOf(";") : -1;
		var v = (i0 > -1 && i1 > -1) ? parseFloat(navigator.appVersion.substring(i0 + 5, i0 + i1)) : 100.0;
		return (navigator.appName.indexOf("Microsoft") > -1 && v < 7.0 ? 0 : 1);
	}
};


DropDown = {
	dropDownClass: "dropDown",
	dropDownTitleClass: "title",
	dropDownItemClass: "item",
	dropDownValueClass: "value",
	dropDownSelectedClass: "selected",
	init: function() {
		$("." + DropDown.dropDownClass).each(function() {
			var dd = $(this);
			var title = $($(this).find("." + DropDown.dropDownTitleClass + " ." + 
					DropDown.dropDownValueClass).get(0));
			var set = function(item) {
				var value = $($(item).find("." + DropDown.dropDownValueClass).get(0)).text();
				title.html(value ? value : "&nbsp;");
			};
			dd.find("." + DropDown.dropDownItemClass).click(function() {
				dd.find("." + DropDown.dropDownItemClass).removeClass(DropDown.dropDownSelectedClass);
				$(this).addClass(DropDown.dropDownSelectedClass);
				set(this);
			});
			dd.find("." + DropDown.dropDownItemClass).each(function() {
				if ($(this).hasClass(DropDown.dropDownSelectedClass))
					set(this);
			});
		});
	}
};


Calendar = {
	calendarButtonClass: "calendarButton",
	calendarDateBoxClass: "dateBox",
	calendarDayBoxClass: "dayBox",
	calendarMonthBoxClass: "monthBox",
	calendarYearBoxClass: "yearBox",
	calendarUndefinedBoxClass: "undefinedBox",

	init: function() {
		var now = new Date();
		$("." + this.calendarButtonClass).each(function() {
			var e = $(this);
			if (e.DatePicker) {
				var c = Calendar.getInstance(this);
				e.DatePicker({
					format: "d-m-Y",
					date: now,
					current: now,
					onBeforeShow: function() {
						$(this).DatePickerSetDate(now, true);
					},
					onChange: function(formated) {
						Calendar.setDate(c, e.DatePickerGetDate());
						e.DatePickerHide();
					},
					onRender: function(date) {
						var today = new Date(), maxDate = null;
						today.setHours(0); today.setMinutes(0); today.setSeconds(0);//alert(date+"\n"+today);
						/*if (c.container.className.match(/(^| )max:/)) {
							maxDate = c.container.className.replace(new RegExp("(^|.* )max:([^ ]*).*"), "$2");
							maxDate = !maxDate ? null : maxDate.split("-");
							maxDate = !maxDate ? null : new Date(
								parseInt(maxDate[2]),
								parseInt(maxDate[1]) - 1,
								parseInt(maxDate[0])
							);
						}*/
						return {disabled: date.getTime() < today.getTime() || 
							(maxDate && date.getTime() > maxDate.getTime())};
					},
					locale: {
						days: [ "Воскресенье", "Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота", "Воскресенье" ],
						daysShort: [ "Вс", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб", "Вс" ],
						daysMin: [ "Вс", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб", "Вс" ],
						months: [ "Январь", "Февраль", "Март", "Апрель", "Май", "Июнь",
							"Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь" ],
						monthsShort: [ "Янв", "Фев", "Мар", "Апр", "Май", "Июн", "Июл", "Авг", "Сен", "Окт", "Ноя", "Дек" ],
						weekMin: "нд"
					}
				});
			}
		});
		$("." + this.calendarButtonClass).click(function() {
			if ($(this).DatePickerShow) $(this).DatePickerShow();
		});
		$("." + this.calendarDateBoxClass).each(function() {
			var c = Calendar.getInstance(this);
			var check = function() { Calendar.checkDate(c); }
			$(c.dayBox).change(check);
			$(c.monthBox).change(check);
			$(c.yearBox).change(check);
		});
	},
	bind: function(calendar) {
		if (typeof(InsuranceCalculator) != "undefined" && calendar && calendar.container) {
			if (calendar.dayBox) InsuranceCalculator.bind(calendar.dayBox);
			if (calendar.monthBox) InsuranceCalculator.bind(calendar.monthBox);
			if (calendar.yearBox) InsuranceCalculator.bind(calendar.yearBox);
			if (calendar.undefinedBox) InsuranceCalculator.bind(calendar.undefinedBox);
			var section = InsuranceCalculator.getSection(calendar.container);
			var m = InsuranceCalculator.checkValidity(section);
			InsuranceCalculator.highlight(m, calendar.id);
			InsuranceCalculator.respond(section);
		}
	},
	setDate: function(calendar, date) {
		if (date && date instanceof Date) {
			var d = date.getDate(), m = date.getMonth() + 1, y = date.getFullYear();
			if (calendar.dayBox) $(calendar.dayBox).val(d);
			if (calendar.monthBox) $(calendar.monthBox).val(m);
			if (calendar.yearBox) $(calendar.yearBox).val(y);
			if (calendar.hiddenBox) $(calendar.hiddenBox).val(
				y + "-" + (m < 10 ? "0" : "") + m + "-" + (d < 10 ? "0" : "") + d
			);
			Calendar.bind(calendar);
		}
	},
	checkDate: function(calendar) {
		if (calendar.dayBox && calendar.monthBox && calendar.yearBox) {
			var d = parseInt($(calendar.dayBox).val());
			var m = parseInt($(calendar.monthBox).val());
			var y = parseInt($(calendar.yearBox).val());
			if (!isNaN(d) && !isNaN(m) && !isNaN(y)) {
				var date = new Date(y, m - 1, d);//alert(date);
				Calendar.setDate(calendar, date);
			}
			else return false;
		}
		else return false;
	},
	getInstance: function(e) {
		var p = $(e);
		if ($(e).is("." + Calendar.calendarDateBoxClass)) p = $(e).get(0);
		else p = $(e).parents("." + Calendar.calendarDateBoxClass).get(0);
		var dBox, mBox, yBox, id;
		dBox = $(p).find("." + Calendar.calendarDayBoxClass).get(0);
		mBox = $(p).find("." + Calendar.calendarMonthBoxClass).get(0);
		yBox = $(p).find("." + Calendar.calendarYearBoxClass).get(0);
		xBox = $(p).find("." + Calendar.calendarUndefinedBoxClass).get(0);
		hBox = $(p).find("input[type=hidden]").get(0);
		id = $(p).attr("id");
		return { id: id, container: p, undefinedBox: xBox,
			dayBox: dBox, monthBox: mBox, yearBox: yBox, hiddenBox: hBox };
	}
};


SortableTable = {
	init: function() {
		$('table.sortable').each(function() {
			var $table = $(this);
			$('th', $table).each(function(column) {
				if ($(this).is('.sort-alpha')) {
					$(this).addClass('clickable');
					$(this).hover(function() {
							$(this).addClass('hover');
						}, function() {
							$(this).removeClass('hover');
					});
					$(this).click(function() {
						var rows = $table.find('tbody > tr').get();
						rows.sort(function(a, b) {
							var keyA = $(a).children('td').eq(column).text().toUpperCase();
							var keyB = $(b).children('td').eq(column).text().toUpperCase();
							if (keyA < keyB) return -1;
							if (keyA > keyB) return 1;
							return 0;
						});
						$.each(rows, function(index, row) {
							$table.children('tbody').append(row);
						});
					});
				}
				else if ($(this).is('.sort-float')) {
					$(this).addClass('clickable');
					$(this).hover(
						function() { $(this).addClass('hover'); },
						function() { $(this).removeClass('hover'); }
					);
					$(this).click(function() {
						var rows = $table.find('tbody > tr').get();
						column = parseInt($(this).attr('column'));
						rows.sort(function(a, b) {
							var keyA = parseFloat($(a).children('td').eq(column).text());
							var keyB = parseFloat($(b).children('td').eq(column).text());
							if (isNaN(keyA)) keyA = Number.MAX_VALUE;
							if (isNaN(keyB)) keyB = Number.MAX_VALUE;
							if (keyA < keyB) return -1;
							if (keyA > keyB) return 1;
							return 0;
						});
						$.each(rows, function(index, row) {
							$table.children('tbody').append(row);
						});
					});
				}
			});
		});
	},
	initCalcTables: function() {
		this.init();
		
		/* sorting values */
		
		$('table.calcSample').each(function() {
			var $table = $(this);
			$('th', $table).each(function(column) {
				if ($(this).is('.sort-float')) {
					var rows = $table.find('tbody > tr').get();
					column = parseInt($(this).attr('column'));
					rows.sort(function(a, b) {
						var keyA = parseFloat($(a).children('td').eq(column).text());
						var keyB = parseFloat($(b).children('td').eq(column).text());
						if (isNaN(keyA)) keyA = Number.MAX_VALUE;
						if (isNaN(keyB)) keyB = Number.MAX_VALUE;
						if (keyA < keyB) return -1;
						if (keyA > keyB) return 1;
						return 0;
					});
					var x = 0, k = 0;
					$.each(rows, function(index, row) {
						var z = parseFloat($(this).children('td').eq(column).text());
						if (!isNaN(z)) {
							if (index > 0 && z != x) k++;
							$(this).children('td').eq(column).addClass('n' + k);
							$(this).children('td').eq(column + 1).addClass('n' + k);
							x = z;
						}
					});
				}
			});
		});
		
		/* currency conversion */
		
		$('.toRUR').click(function () {
			$('.toRUR').css('display', 'none');
			$('.toEUR').css('display', '');
			$('.toUSD').css('display', '');
			$('.curRUR').css('display', '');
			$('.curEUR').css('display', 'none');
			$('.curUSD').css('display', 'none');
		});
		
		$('.toEUR').click(function () {
			$('.toRUR').css('display', '');
			$('.toEUR').css('display', 'none');
			$('.toUSD').css('display', '');
			$('.curRUR').css('display', 'none');
			$('.curEUR').css('display', '');
			$('.curUSD').css('display', 'none');
		});
		
		$('.toUSD').click(function () {
			$('.toRUR').css('display', '');
			$('.toEUR').css('display', '');
			$('.toUSD').css('display', 'none');
			$('.curRUR').css('display', 'none');
			$('.curEUR').css('display', 'none');
			$('.curUSD').css('display', '');
		});
	}
};


FormValidation = {
	init: function() {
		var query = window.location.search;
		$("#liteboxCallbackOrderForm button").click(function(event) {
			var e1 = $("#liteboxCallbackOrderErrorNameNone");
			var e2 = $("#liteboxCallbackOrderErrorPhoneNone");
			var e3 = $("#liteboxCallbackOrderErrorInsuranceTypeNone");
			var valid = true;
			if (!$("#liteboxCallbackOrderName").val()) {
				e1.show(); valid = false;
			}
			else e1.hide();
			if (!$("#liteboxCallbackOrderPhone").val()) {
				e2.show(); valid = false;
			}
			else e2.hide();
			if (!$("#liteboxCallbackOrderInsuranceType").val()) {
				e3.show(); valid = false;
			}
			else e3.hide();
			if (!valid) {
				event.preventDefault();
				event.returnValue = false;
				return false;
			}
			return true;
		});
		$("#callbackOrderForm button").click(function(event) {
			var e1 = $("#callbackOrderErrorNameNone");
			var e2 = $("#callbackOrderErrorPhoneNone");
			var e3 = $("#callbackOrderErrorInsuranceTypeNone");
			var valid = true;
			if (!$("#callbackOrderName").val()) {
				e1.show(); valid = false;
			}
			else e1.hide();
			if (!$("#callbackOrderPhone").val()) {
				e2.show(); valid = false;
			}
			else e2.hide();
			if (!$("#callbackOrderInsuranceType").val()) {
				e3.show(); valid = false;
			}
			else e3.hide();
			if (!valid) {
				event.preventDefault();
				event.returnValue = false;
				return false;
			}
			return true;
		});
		$("#liteboxRegistrationForm button").click(function(event) {
			var e1 = $("#liteboxRegistrationErrorSurnameNone");
			var e2 = $("#liteboxRegistrationErrorNameNone");
			var e3 = $("#liteboxRegistrationErrorPhoneNone");
			var e4 = $("#liteboxRegistrationErrorPhoneWrongFormat");
			var e5 = $("#liteboxRegistrationErrorEmailWrongFormat");
			var valid = true;
			var phone = $("#liteboxRegistrationPhone").val();
			var email = $("#liteboxRegistrationEmail").val();
			if (!$("#liteboxRegistrationSurname").val()) {
				e1.show(); valid = false;
			}
			else e1.hide();
			if (!$("#liteboxRegistrationName").val()) {
				e2.show(); valid = false;
			}
			else e2.hide();
			if (!phone) {
				e3.show(); valid = false;
			}
			else e3.hide();
			if (phone && !valid("phone", phone)) {
				e4.show(); valid = false;
			}
			else e4.hide();
			if (x && !valid("email", x)) {
				e5.show(); valid = false;
			}
			else e5.hide();
			if (!valid) {
				event.preventDefault();
				event.returnValue = false;
				return false;
			}
			return true;
		});
		$("#requestForm #formType").change(function() {
			var v = $(this).val();
			$("#requestFormProcessIndicator").show();
			$.ajax({
				url: v + ".php",
				dataType: "html",
				success: function(data) {
					var i0 = data.indexOf("<form"), i1 = data.indexOf("</form>");
					if (i0 != -1 && i1 != -1) {
						$("#requestFormBody").html(data.substring(i0, i1));
						$("#requestFormBodyFrame").show();
					}
					else $("#requestFormBodyFrame").hide();
					i0 = data.indexOf("<title"), i1 = data.indexOf("</title>");
					if (i0 != -1 && i1 != -1) {
						var title = data.substring(i0, i1);
						title = title.replace(/<\/?title(\s+[^>]*)?>/g, "");
						title = title.replace(/\s+-\s+.*/i, "");
						$("#requestFormTitle").html(title);
						$("#requestFormTitle").show();
					}
					else $("#requestFormTitle").hide();
					$("#requestFormProcessIndicator").hide();
					FormValidation.review({action:""});
				},
				error: function() {
					$("#requestFormBodyFrame").hide();
					$("#requestFormProcessIndicator").hide();
				},
				type: "GET"
			});
		});
		var q = query ? query.replace(/.*(\?|&)q=([^&#]*).*/, "$2") : "";
		if (q) $("#requestForm #formType").val(q).change();
		this.review();
	},
	valid: function(format, x) {
		var r = false;
		switch (format.toLowerCase()) {
			case "email":
				r = (x + "").match(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/);
				break;
			case "phone":
				if (x instanceof Array) {
					var r0 = x.length > 0;
					for (var i = 0; i < x.length; i++)
						r0 &= (x[i] && !isNaN(parseInt((x[i] + "").replace(/[+\(\)-\. \'\\\/]/, ""))));
					r = r0;
				}
				else r = !isNaN(parseInt((x + "").replace(/[+\(\)-\. \'\\\/]/, "")));
				break;
		}
		return r;
	},
	review: function(option) {
		var emptyValue = "Не указано";
		var format = function(x, type) {
			var s = x ? x.replace("\u00a0", " ").replace(/(^\s+|\s+$)/g, "") : "";
			if (typeof(type) != "undefined" && type == "label")
				s += !s || s.match(/.*\?$/) ? "" : ":";
			else s = s ? s : emptyValue;
			return s;
		};
		$("form").submit(function() {
			var f = $(this), store = f.find("input[name=_summary]"), summary = [];
			if (typeof(option) != "undefined" && typeof(option.action) != "undefined")
				f.attr("action", option.action);
			if (store.get().length < 1) return;
			f.find(".parameter").each(function() {
				var parameter = {};
				var p = $(this), v = p.children(".value");
				parameter.label = p.children(".label").text();
				if (!parameter.label) parameter.label = p.children("label").text();
				parameter.label = format(parameter.label, "label");
				parameter.value = [];
				v.find("input[type=text], textarea").each(function() {
					parameter.value.push(format($(this).val()));
				});
				v.find("select option:selected").each(function() {
					parameter.value.push(format($(this).text()));
				});
				v.find("input[type=checkbox]:checked, input[type=radio]:checked").each(function() {
					var e = $(this), x = "";
					if (e.attr("id")) x = $("label[for=" + e.attr("id") + "]").text();
					if (!x) {
						var ne = $(this).next();
						if (ne && ne.is("label, span")) x = ne.text();
					}
					if (!x) {
						var ne = this.nextSibling;
						if (ne && ne.data) x = ne.data;
					}
					parameter.value.push(format(x));
				});
				if (parameter.value.length < 1) {
					if (parameter.label) { parameter.value.push(format("")); summary.push(parameter); }
				}
				else summary.push(parameter);
			});
			var s = "";
			for (var i in summary) {
				s += summary[i].label ? summary[i].label + "\n" : "";
				s += summary[i].value.length > 1 ? "- " + summary[i].value.join("\n- ") : summary[i].value[0];
				s += "\n\n";
			}
			var h = f.prevAll(":header").text();
			if (!h) f.parents().each(function() { if (!h) h = $(this).prevAll(":header").text(); });
			s = h + "\n\n" + s;
			store.val(s);
		});
	}
};


})(jQuery);