var Mozilla = (navigator.appName.indexOf("Netscape") != -1);

function forum_check_reg_form() {
	forum_login = document.getElementById('forum_login');
	forum_password = document.getElementById('forum_password');
	forum_password_check = document.getElementById('forum_password_check');
	forum_email = document.getElementById('forum_email');

	if(!forum_login || !forum_password || !forum_password_check || !forum_email) {
		alert("Неправильно сформирована форма.");
		return false;
	}

	if(forum_login.value == "") {
		alert("Необходимо заполнить поле \"Логин\"");
		return false;
	}

	if(forum_password.value == "") {
		alert("Необходимо заполнить поле \"Пароль\"");
		return false;
	}

	if(forum_password.value != forum_password_check.value) {
		alert("Пароли не совпадают.");
		return false;
	}

	if(forum_email.value == "") {
		alert("Необходимо заполнить поле \"E-mail\"");
		return false;
	}

	return true;
}


function forum_check_form(obj) {
	if(!obj)
		return false;


	if(obj.captcha) {
		if(md5(obj.captcha.value) != getCookie("captcha1")) {
			alert("Неправильный код.");
			return false;
		}
	}


	if(obj.nickname)
		vlogin = obj.nickname.value;
	else
		vlogin = "";

	if(obj.title)
		vtitle = obj.title.value;
	else
		vtitle = "";

	if(obj.body)
		vbody = obj.body.value;
	else
		vbody = "";

	if(vlogin.length == 0) {
		alert("Введите имя.");
		return false;
	}



	if(vtitle.length == 0) {
		alert("Введите заголовок.");
		return false;
	}

	bd = vbody.replace(/[ \t\r\n]*/g, "");

	if(bd.length == 0) { 
		alert("Введите текст сообщения");
		return false;
	}
	return true;
}

function forum_quote(mess_id) {
    var author = "", is_ie = site.getInstance().isIE, range, sel_str, qmess, sel, real_id, author_obj,
		mess_obj = document.getElementById('mess_' + mess_id),
		body_obj = document.getElementById('message');

	if (!mess_obj) return false;
	if (!body_obj) return false;

	sel = getCurrSelection();
	if (is_ie) {
		range = sel.createRange();
		sel_str = range.text;
	}
	else {
		if (sel.rangeCount) {
			range = getRange(sel);
			sel_str = range;
		}
		else sel_str = "";
	}

	qmess = (is_ie) ? mess_obj.outerText : mess_obj.textContent;
	qmess = mess_obj.innerHTML;
	var bb_from = [/<strong>/g, /<em>/g, /<\/strong>/g, /<\/em>/g, /<div class="quote">/g, /<\/div>/g, /<u>/g, /<\/u>/g, /<br ?\/?>/g],
		bb_to = ["[b]", "[i]", "[/b]", "[/i]", "[QUOTE]", "[/QUOTE]", "[u]", "[/u]", "\r\n"];

	for (var i = 0; i < bb_from.length; i++) {
		qmess = qmess.replace(bb_from[i], bb_to[i]);
	}

	if (sel_str && typeof sel_str != "undefined" && qmess) {
		if (is_ie) {
			if (qmess.replace(sel_str, "") != qmess) {
				qmess = sel_str;
			}
		}
		else {
			if(hasParent(range.commonAncestorContainer, 'mess_' + mess_id)) {
				qmess = range;
			}
		}
	}


	real_id = mess_obj.id.substr(5, mess_obj.id.length - 5);
	author_obj = document.getElementById('author_' + real_id);
	if (author_obj) {
		author = (is_ie) ? author_obj.innerText : author_obj.textContent;
		author = "[b]" + trim(author) + "[/b]\n";
	}
	else author = "";

	if (qmess) {
		if (body_obj.value.length == 0)
			body_obj.value += "[QUOTE]" + author + qmess + "[/QUOTE]\n";
		else
			body_obj.value += "\n[QUOTE]" + author + qmess + "[/QUOTE]\n";
	}
	document.getElementById('message').focus();
}


function forum_toAuthor(obj) {
	if(is_ie)
		res = obj.innerText;
	else
		res = obj.textContent;

	res = trim(res);

	res = "[b]" + res + "[/b]\r\n";

	document.getElementById('message').value += res;
	window.location = "#add";

	document.getElementById('message').focus();

	return false;
}



function forum_insert_smile(obj, element_name) {
	if(!obj) return;
	var obj = obj.firstChild;
	if(!obj) return;

	var alt = obj.alt;
	if(!alt) return false;

	var obj = document.getElementById('message');
	if(!obj) obj = document.getElementById('forum_body');
	if(!obj) obj = document.getElementById(element_name);
	if(!obj) return false;

	obj.value += "[smile:" + alt + "] ";
	obj.focus();

	return true;
}
