/*
function get_obj_by_path(obj) {
	var routes = [];
	var step;
	for (var i = 1; i < arguments.length; i++) {
		var parts = arguments[i].split(/\s+/);
		var step = parts[0];
		if (!parts[1]) {
			parts[1] = 0;
		}
		for (var j = 1; j < parts.length; j++) {
			routes[routes.length] = { "step": step, "index": parts[j] };
		}
	}
	var result = obj;
	var route;
	for (var i = 0; i < routes.length; i++) {
		route = routes[i];
		with (route) {
			if (step == "parent") {
				while (index-- >= 0) {
					result = result.parentNode;
				}
			} else if (step == "prev") {
				result = not_text_previous_subling(result, index);
			} else if (step == "next") {
				result = not_text_next_subling(result, index);
			} else if (step == "child") {
				result = not_text_child(result, index);
			}
		}
	}
	return result;
}
*/
function validate(form){
	var _validate=new Function('form',form.hvalidate.value)
	return _validate(form);
}
function getParentForm(e){
	if (e.tagName.toLowerCase()=='form') return e;
	var p=e.parentNode;
	if (p.tagName.toLowerCase()=='form') return p;
	if (p==null||p.tagName.toLowerCase()=='body')return false;
	return getParentForm(p);
}
function get_obj_by_path(obj, path) {
	path = path;
	var routes = [];
	var step;
	var index;
	var parts = path.split(/\s+/);
	var j;
	for (var i = 0; i < parts.length; i++) {
		step = parts[i];
		index = 0;
		if (i == parts.length - 1) {
			routes[routes.length] = { step: step, index: index };
			break;
		}
		for (j = i + 1; j < parts.length; j++) {
			if (parts[j].search(/^\d+$/gi) == -1) {
				if (j == i + 1) {
					routes[routes.length] = { step: step, index: index };
				}
				break;
			} else {
				routes[routes.length] = { step: step, index: parts[j] }
			}
		}
		i = j - 1;
	}

	var result = obj;
	var route;
	for (var i = 0; i < routes.length && result; i++) {
		route = routes[i];
		with (route) {
			if (step == "parent") {
				while (index-- >= 0 && result) {
					result = result.parentNode;
				}
			} else if (step == "prev") {
				result = not_text_previous_subling(result, index);
			} else if (step == "next") {
				result = not_text_next_subling(result, index);
			} else if (step == "child") {
				result = not_text_child(result, index);
			} else if (step == "last") {
				result = not_text_last_child(result, index);
			} else if (step == "") {	//	do nothing, continue
			} else {
				alert("get_obj_by_path:\nUnknow step type:\"" + step + "\"");
			}
		}
	}
	return result;
}

//	aliases
function not_text_child(obj, index) {
	return not_text_child_by_index(obj, index);
}
function not_text_last_child(obj, index) {
	return not_text_last_child_by_index(obj, index);
}
function not_text_next_subling(obj, index) {
	return not_text_next_subling_by_index(obj, index);
}
function not_text_previous_subling(obj, index) {
	return not_text_previous_subling_by_index(obj, index);
}

function not_text_child_length(obj) {
	var length = 0;
	var result = obj.firstChild;
	for (; result; result = result.nextSibling) {
		if (result.nodeType != 3) length++;
	}
	return length;
}

function not_text_child_by_index(obj, index) {
	//	when damn mozilla gets white spaces it creates new node, while IE does not
	if (index == undefined) {
		index = 0;
	}
	var i = -1;
	var result = obj.firstChild;
	for (; result; result = result.nextSibling) {
		if (result.nodeType != 3) i++;
		if (i == index) break;
	}
	return result;
}

function not_text_last_child_by_index(obj, index) {
	if (index == undefined) {
		index = 0;
	}
	var i = -1;
	var result = obj.lastChild;
	for (; result; result = result.previousSibling) {
		if (result.nodeType != 3) i++;
		if (i == index) break;
	}
	return result;
}

function not_text_next_subling_by_index(obj, index) {
	if (index == undefined) {
		index = 0;
	}
	var i = -1;
	var result = obj.nextSibling;
	for (; result; result = result.nextSibling) {
		if (result.nodeType != 3) i++;
		if (i == index) break;
	}
	return result;
}

function not_text_previous_subling_by_index(obj, index) {
	if (index == undefined) {
		index = 0;
	}
	var i = -1;
	var result = obj.previousSibling;
	for (; result; result = result.previousSibling) {
		if (result.nodeType != 3) i++;
		if (i == index) break;
	}
	return result;
}

function set_inner_html(element, value) {
	var el;
	if (typeof(element) == "string") {
		el = document.getElementById(element);
	} else {
		el = element;
	}
	if (!el) return false;
	el.innerHTML = value;
	return true;
}

function add_commas(text) {
	text = text + "";
	var result_text = "";
	var float_part = "";
	var digits = new Array();
	for (var i = 0; i < text.length; i++) {
		if (text.charAt(i) == ".") {
			float_part = text.substring(i, text.length);
			break;
		}
		digits[digits.length] = text.charAt(i);
	}
	var counter = 0;
	for (var i = digits.length - 1; i >= 0; i--) {
		counter++;
		if (counter > 3 && digits[i] != "-") {
			counter = 1;
			result_text = "," + result_text;
		}
		result_text = digits[i] + result_text;
	}
	result_text = result_text + float_part;
	return result_text;
}

function on_header_click(column, prefix) {
	var properties_name = "properties";
	var after_header_click_name = "after_header_click";
	var refresh_page_name = "refresh_page";

	if (prefix != undefined) {
		properties_name = prefix + "_" + properties_name;
		after_header_click_name = prefix + "_" + after_header_click_name;
		refresh_page_name = prefix + "_" + refresh_page_name;
	}

	eval("var properties = window." + properties_name);
	eval("var after_header_click = window." + after_header_click_name);
	eval("var refresh_page = window." + refresh_page_name);
	with (properties) {
		if (sort_column == column) {
			sort_direction = (sort_direction == "asc") ? "desc" : "asc";
		} else {
			sort_column = column;
			sort_direction = "asc";
		}
	}
	if (after_header_click) {
		after_header_click();
	} else {
		refresh_page();
	}
}

function preload_images() {
	if (document.images) {
		if (!document.p) document.p = new Array();
		for (var i = 0;  i < arguments.length; i++) {
			var img = new Image();
			img.src = arguments[i];
			document.p[document.p.length] = img;
		}
	}
}

function move_options_up(obj) {
	if (!has_options(obj)) { return; }
	for (var i = 0; i < obj.options.length; i++) {
		if (obj.options[i].selected) {
			if (i != 0 && !obj.options[i - 1].selected) {
				swap_options(obj, i, i - 1);
				obj.options[i - 1].selected = true;
			}
		}
	}
}

function move_options_down(obj) {
	if (!has_options(obj)) { return; }
	for (var i = obj.options.length - 1; i >= 0; i--) {
		if (obj.options[i].selected) {
			if (i != (obj.options.length - 1) && ! obj.options[i + 1].selected) {
				swap_options(obj, i, i + 1);
				obj.options[i + 1].selected = true;
			}
		}
	}
}

function has_options(obj) {
	if (obj != null && obj.options != null) { return true; }
	return false;
}

function swap_options(obj, i, j) {
	var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2 = new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
}

function delete_options(obj) {
	var o = obj.options;
	for (var i = o.length - 1; i >= 0; i--) {
		if (o[i].selected) o[i] = null;
	}
}

function get_dimensions(obj) {
	var result = { "left": 0, "right" : 0, "top": 0, "bottom" : 0, "width" : obj.offsetWidth, "height" : obj.offsetHeight };
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			result.left += obj.offsetLeft;
			result.top += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else {
		if (obj.x) {
			result.left += obj.x;
		}
		if (obj.y) {
			result.top += obj.y;
		}
	}
	result.right = result.left + result.width;
	result.bottom = result.top + result.height;
	return result;
}

function get_size(obj) {
	result = {};
	if (obj.style.display != "none") {
		result.width = obj.offsetWidth;
		result.height = obj.offsetHeight;
	} else {
		var original_visibility = obj.style.visibility;
		var original_position = obj.style.position;
		obj.style.visibility = "hidden";
		obj.style.position = "absolute";
		obj.style.display = "";
		result.width = obj.clientWidth;
		result.height = obj.clientHeight;
		obj.style.display = "none";
		obj.style.position = original_position;
		obj.style.visibility = original_visibility;
	}
	return result;
}

function is_intersected(dim1, dim2) {
	if ((Math.min(dim1.right, dim2.right) > Math.max(dim1.left, dim2.left)) && (Math.min(dim1.bottom, dim2.bottom) > Math.max(dim1.top, dim2.top))) {
		return true;
	} else {
		false;
	}
}
function is_inside(dim, point) {
	if ((dim.left <= point.x) && (dim.right >= point.x) && (dim.top <= point.y) && (dim.bottom >= point.y)) {
		return true;
	} else {
//		alert(dim.left + " <= " + point.x + " <= " + dim.right + " && " + dim.top + " <= " + point.y + " <= " + dim.bottom);
		return false;
	}
}

function disable_form(form) {
	var element;
	for (var i = 0; i < form.elements.length; i++) {
		element = form.elements[i];
		element.old_disabled = element.disabled;
		element.disabled = true;
	}
}

function enable_form(form) {
	var element;
	for (var i = 0; i < form.elements.length; i++) {
		element = form.elements[i];
		element.disabled = element.old_disabled;
	}
}

function trim(string) {
	string = string.replace(/^\s+/gi, "");
	string = string.replace(/\s+$/gi, "");
	return string;
}

function get_cookies() {
	var cookies_array = document.cookie.split(";");
	var cookies = {};
	for (var i = 0; i < cookies_array.length; i++) {
		var cookie_params = cookies_array[i].split("=");
		cookies[trim(cookie_params[0])] = unescape(cookie_params[1]);
	}
	return cookies;
}

function set_cookie(name, value, expires) {
	var cookie = name + "=" + escape(value);
	if (expires != undefined) {
		cookie += ";expires=" + expires.toUTCString();
	}
	document.cookie = cookie;
}

function get_cookie(name) {
	return get_cookies()[name];
}

function isSubmitEnter(e) {
	if (typeof e!='object') return false;
	var whichKey, targ;

	if (e.target) {targ = e.target;}
	else if (e.srcElement) {targ = e.srcElement;}

	if(document.all) {whichKey = window.event.keyCode;}
	else if(document.layers) {whichKey = e.which;}
	else {whichKey = e.keyCode;}
	if(whichKey == 13) {
		switch(targ.tagName.toUpperCase()) {
			case "TEXTAREA":
			case "SELECT":
				return false;
				break;
			case "INPUT":
				targ.blur();
				targ.focus();
				return true;
				break;
			default:
				return true;
		}
	}else
		return false;
}


function array_search(needle, haystack, strict) {
	for (var i = 0; i < haystack.length; i++) {
		if (strict && (needle == haystack[i]) || !strict && (needle == haystack[i])) {
			return i;
		}
	}
	return false;
}

function in_array(needle, haystack, strict) {
	for (var i = 0; i < haystack.length; i++) {
		if (strict && (needle == haystack[i]) || !strict && (needle == haystack[i])) {
			return true;
		}
	}
	return false;
}

function array_unique(array) {
	var result = [];
	var element;
	for (var i = 0; i < array.length; i++) {
		element = array[i];
		if (array_search(element, result, true) === false) {
			result.push(element);
		}
	}
	return result;
}

function swap_class_names(obj, class_name_remove, class_name_add) {
	if (!obj) {
		return false;
	} else {
		var classes = obj.className.split(/\s+/);
		var found = false;
		if (class_name_remove !== undefined) {
			for (var i = classes.length - 1; i >= 0; i--) {
				if (classes[i] == class_name_remove) {
					if (class_name_add !== undefined) {
						classes[i] = class_name_add;
					} else {
						classes[i] = null;
					}
					found = true;
					break;
				}
			}
		}
		if (!found && class_name_add !== undefined) {
			classes.push(class_name_add);
		}
		classes = array_unique(classes);
		obj.className = classes.join(" ");
	}
}
function htmlSpecialChars(tmp){
	tmp = tmp.replace(/&/g, "&amp;");
	tmp = tmp.replace(/"/g, "&quot;");
	tmp = tmp.replace(/'/g, "&#39;");
	tmp = tmp.replace(/</g, "&lt;");
	tmp = tmp.replace(/>/g, "&gt;");
	return tmp;
}

// elements begin

var one_or_more_elements_has_errors;

function set_element_error(element, error) {
	swap_class_names(element, undefined, "error");
	if (error != "") {
		element.title = error;
	}
	one_or_more_elements_has_errors = true;
}

function clear_element_error(element) {
	swap_class_names(element, "error");
	element.title = "";
}

// elements end

function debug(obj, parent) {
	var type = typeof obj;
	if (type != "object" && type != "array") {
		alert(obj);
	} else {
		for (var i in obj) {
			try {
				var text = i;
				if (parent !== undefined) {
					text = parent + "." + text;
				}
				type = typeof obj[i];
				if (type == "object" || type == "array") {
					debug(obj[i], text)
				} else if (!confirm(text + " = " + obj[i])) {
					break;
				}
			} catch(e) {
				alert(i + " = Exception: " + e.toString());
			}
		}
	}
}


function get_base_href() {
	var base_href = "";
	if (window.base_href) {	//	already calculated
		base_href = window.base_href;
	} else {
		var bases = document.getElementsByTagName("base");
		if (bases.length) {
			var base = document.getElementsByTagName("base")[0];
			var attribute = base.attributes.getNamedItem("href");
			if (attribute) {
				base_href = attribute.nodeValue.replace(/\/[^\/]*$/i, "");
			}
		}
		window.base_href = base_href;	//	save for future calls
	}
	return base_href;
}

function get_absolute_location(location) {
	var real_location = location;
	if (real_location.search(/^(http:\/)?\//i) !== 0) {	//	relative url
		var base_href = get_base_href();
		if (base_href) {
			real_location = base_href + "/" + real_location;
		}
	}
	return real_location;
}


function change_location(location) {
	window.location.href = get_absolute_location(location);
}


function add_event_listener(el, evname, func) {
	if (el.attachEvent) { // IE
		el.attachEvent("on" + evname, func);
	} else if (el.addEventListener) { // Gecko / W3C
		el.addEventListener(evname, func, true);
	} else {
		el["on" + evname] = func;
	}
};

function get_selected_value(select) {
	var result = [];
	for (var i = 0; i < select.options.length; i++) {
		if (select.options[i].selected) {
			result.push(select.options[i].value);
		}
	}
	if (result.length == 0) {
		return false;
	} else if (result.length == 1) {
		return result[0];
	} else {
		return result;
	}
}

function set_selected_value(select, value) {
	for (var i = 0; i < select.options.length; i++) {
		if (select.options[i].value == value) {
			select.options[i].selected = true;
			break;
		}
	}
}

function set_node_value(node, value, nbsp_if_empty) {
	if ((value === null || value === false || value === "") && nbsp_if_empty) {
		value = "\u00a0";
	}
	var new_node = document.createTextNode(value);
	if (node.firstChild) {
		node.replaceChild(new_node, node.firstChild);
	} else {
		node.appendChild(new_node);
	}
}

function on_form_keypress(event, enter_callback, escape_callback) {
	var event_information = get_event_information(event);
	if (is_enter_pressed(event_information)) {
		if (enter_callback) {
			enter_callback();
		}
		return false;
	}
	if (is_escape_pressed(event_information)) {
		if (escape_callback) {
			escape_callback();
		}
		return false;
	}
}

function is_enter_pressed(event_information) {
	var tag_name = event_information.target.tagName.toLowerCase();
	var result = false;
	if (event_information.code == "13" && tag_name == "input") {
		result = true;
	}
	return result;
}

function is_escape_pressed(event_information) {
	var result = false;
	if (event_information.code == "27") {
		result = true;
	}
	return result;
}

function get_event_information(e) {
	var result = {};

	if (e.target) {
		result.target = e.target;
	} else if (e.srcElement) {
		result.target = e.srcElement;
	}

	if (browser.is_ie()) {
		result.code = window.event.keyCode;
	} else if (document.layers) {
		result.code = e.which;
	} else {
		result.code = e.keyCode;
	}

	return result;
}

function stop_event(event) {
	if (browser.is_ie()) {
		event.cancelBubble = true;
		event.returnValue = false;
	} else {
		event.preventDefault();
		event.stopPropagation();
	}
}

function get_form(obj) {
	form = null;
	while(obj) {
		if (obj.tagName.toLowerCase() == "form") {
			form = obj;
			break;
		}
		obj = obj.parentNode;
	}
	return form;
}

function get_form_element(form, name) {
	if (form && form.elements && form.elements.length) {
		for (var i = 0; i < form.elements.length; i++) {
			if (form.elements[i].name == name) {
				return form.elements[i];
			}
		}
	}
}

function js2dom(js) {
//	alert(js instanceof Array);
	var result;
	if (!js.name) {
		result = document.createTextNode(js.value);
		if (js.html) {
			result.innerHTML = js.html;
		}
	} else {
		result = document.createElement(js.name);
		if (js.attributes) {
			for (var i in js.attributes) {
				if (js.attributes.hasOwnProperty(i)) {
					if (i == "class") {
						result.className = js.attributes[i];
						continue;
					}
					result.setAttribute(i, js.attributes[i]);
				}
			}
		}
		if (js.onclick) {
			result.onclick = js.onclick;
		}
		if (js.value) {
			result.appendChild(document.createTextNode(js.value));
		} else if (js.html) {
			result.innerHTML = js.html;
		} else if (js.childs) {
			for (var i in js.childs) {
				if (js.childs[i] && js.childs.hasOwnProperty(i)) {
					result.appendChild(js2dom(js.childs[i]));
				}
			}
		}
	}
	return result
}

function calendar_date_changed(calendar) {
	calendar.params.inputField.value = calendar.date.print(calendar.params.ifFormat);
};
function get_market_date() {
	var result = new Date();
	result.setMinutes(result.getMinutes() + result.getTimezoneOffset() + market_gmt * 60);
	return result;
}

