function getCurrSelection() {
	if(document.selection)
		return document.selection;
	else
		return window.getSelection();
}

function getRange(sel) {
	if(!sel)
		sel = getSelection();

	if(typeof sel != "undefined") {
		return sel.getRangeAt(0);
	} else {
		return window.document.createRange();
	}
}

function hasParent(obj, par_id) {
	if(!obj) {
		return false;
	}

	if(obj.id == par_id) {
//		alert(obj.id);
		return true;
	} else {
		if(obj.parentNode) {
			return hasParent(obj.parentNode, par_id);
		} else {
			return false;
		}
	}
}

function trim(str) {
	var res = str.replace(/^\s+/, '');
	return res.replace(/\s+$/, '');

}


function gen_popup(src, width, height, page_header) {
	if(!page_header)
		page_header = "";

	win = window.open("", "_blank", "width=" + (width) + ",height=" + (height) + ",titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no");

	win.document.write("<html><head><title>" + page_header + "</title></head><body leftmargin=0 topmargin=0 marginwidth=0 marginheight=0><table style='height: 100%' cellspacing='0' cellpadding='0' border='0'><tr><td valgin='middle'>");
	win.document.write("<" + "script" + ">document.title = \"" + page_header + "\";\r\nfunction _CloseOnEsc(e) {if(e){ event = e;} if (event.keyCode == 27) { window.close(); return; }} document.onkeydown = _CloseOnEsc;<" + "/script>");
	win.document.write("<img src=\"" + src +  "\" onclick=\"javascript: window.close();\" style=\"cursor: pointer;\" />");
	win.document.write("</td></tr></table></body></html>");
	win.document.close();
	return false;
}

function getPageSizes(){
	
	var iScrollX, iScrollY;
	
	if (window.innerHeight && window.scrollMaxY) {
		iScrollX = document.body.scrollWidth;
		iScrollY = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
		iScrollX = document.body.scrollWidth;
		iScrollY = document.body.scrollHeight;
	} else {
		iScrollX = document.body.offsetWidth;
		iScrollY = document.body.offsetHeight;
	}
	
	var iWinWidth, iWinHeight;
	if (self.innerHeight) {
		iWinWidth = self.innerWidth;
		iWinHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		iWinWidth = document.documentElement.clientWidth;
		iWinHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		iWinWidth = document.body.clientWidth;
		iWinHeight = document.body.clientHeight;
	}	
	
	if(iScrollY < iWinHeight){
		iPageHeight = iWinHeight;
	} else { 
		iPageHeight = iScrollY;
	}

	if(iScrollX < iWinWidth){	
		iPageWidth = iWinWidth;
	} else {
		iPageWidth = iScrollX;
	}

	var arrSizes = new Array(iPageWidth,iPageHeight,iWinWidth,iWinHeight) 
	return arrSizes;
}

function checkRegdataForm(the_form, s_fields) {
	if (!the_form) {
		alert("Передан неверный идентификатор формы");
		return false;
	}
	if (typeof(s_fields) !== 'string') var s_fields = '';
	var arr_fields = s_fields.split(/\s*[,;]\s*/ig);
	var b_need_all = (!(arr_fields.length) || !(s_fields.length));
	var arr_field_names = new Array();
	if (!b_need_all) {
		var f = 0;
		for (f=0; f<arr_fields.length; f++) {
			arr_field_names[arr_fields[f]] = 1;
		}
	}
	var arr_input_elements = the_form.elements;
	var i_element = 0;
	for (i_element=0; i_element<arr_input_elements.length; i_element++) {
		var the_element = arr_input_elements[i_element];
		var s_el_name = the_element.id;
		if (b_need_all || (typeof(arr_field_names[s_el_name]) !== 'undefined' && arr_field_names[s_el_name] === 1)) {
			var s_el_type = the_element.type;
			s_el_type = s_el_type.toUpperCase();
			if ((s_el_type === 'TEXT' || s_el_type === 'PASSWORD') && !the_element.value.length) {
				the_element.focus();
				s_alert = "Необходимо заполнить все обязательные поля";
				var theLabel = the_element.previousSibling;
				if (theLabel && theLabel.tagName && theLabel.tagName.toUpperCase() === 'LABEL') {
					s_alert += "\r\n" + "Не заполнено поле \"" + theLabel.childNodes[0].nodeValue + "\"";
				}
				alert(s_alert);
				return false;
			}
		}
	}
	if (the_form.elements['password'] && the_form.elements['password_confirm'] && the_form.elements['password'].value !== the_form.elements['password_confirm'].value) {
		var theLabel_1 = the_form.elements['password'].previousSibling;
		var theLabel_2 = the_form.elements['password_confirm'].previousSibling;
		s_alert = "Значения полей \"" + theLabel_1.childNodes[0].nodeValue + "\" и \"" + theLabel_2.childNodes[0].nodeValue + "\" не совпадают";
		alert(s_alert);
		the_form.elements['password_confirm'].focus();
		return false;
	}
	if (the_form.elements['email']) {
		var s_email = the_form.elements['email'].value;
		var regexp = /[\w\-]+@[\w\-]+\.[\w]+/;
		if (!regexp.test(s_email)) {
			alert("Неверный синтаксис электронного адреса (email)");
			the_form.elements['email'].focus();
			return false;
		}
	}
	saveDataForm(the_form);
	return true;
}
