
// Copyright (c) 2000-2002 N.Mikhailov, mn@ixbt.com, http://forum.iXBT.com

var old_action = "";

function p1(text){ 
if (text!="") paste("[b]"+text+"[/b]\n", 1);
}

function p2(text){ 
if (text!="") paste("[i]"+text+"[/i]\n", 0);
}

function paste(text, flag){ 
if ((document.selection)&&(flag)) {
	document.postform.message.focus();
	document.postform.document.selection.createRange().text = text;
} else document.postform.message.value += text;
}

function get_selection() {
   if (document.getSelection){
	selection = document.getSelection();
	selection = selection.replace(/\r\n\r\n/gi, "_doublecaret_");
	selection = selection.replace(/\r\n/gi, " ");
       while (selection.indexOf("  ") !=-1) selection = selection.replace(/  /gi, ""); 
	selection = selection.replace(/_doublecaret_/gi, "\r\n\r\n");
  } else
      selection = document.selection.createRange().text;
}

function setup_postform(enablefocus){
if (enablefocus) {document.postform.UserName.focus()}
var testUserName = getCookie("user")
var testPassword = getCookie("pass")
if ((document.cookie)&&(testUserName != null)) {
	document.postform.UserName.value = testUserName
	if (testPassword!=null) {document.postform.Password.value = testPassword}
	if (enablefocus) {document.postform.message.focus()}
}
if (getCookie("setup")!=null){
	if (getCookie("setup").indexOf("D") != -1) {document.postform.without_smilies.checked = true}
}
if ((!enablefocus)&&(navigator.appVersion.indexOf("MSIE")!= -1)){ 
	var i=1;
	while (document.all("tbl"+i) != null) {document.all("tbl"+i).style.tableLayout="fixed"; i++}

	//var testform_cookie = getCookie('setup2')
	//if (testform_cookie!=null){
	//	form_cookie=testform_cookie.split("|");
	//	if ((form_cookie[3] != null)&&(form_cookie[3] >=20)&&(form_cookie[3]<=250)) {document.postform.message.cols=form_cookie[3]} else {document.postform.message.style.width="100%"};
	//	if (form_cookie[4] != null) {document.postform.message.rows=form_cookie[4]}
	//	if (form_cookie[5] != null) {document.postform.message.style.fontSize=form_cookie[5]}
	//	if (form_cookie[6] != null) {document.postform.message.accessKey=form_cookie[6]}
	//} else {document.postform.message.style.width="100%"}
	document.postform.message.style.width="100%";
}
document.postform.message.style.width="100%";

switch(navigator.appName) {
   case "Microsoft Internet Explorer":
      Key = "event.ctrlKey && event.keyCode == 13";
      document.onkeydown = get_key;
      break;
   case "Netscape":
      Key = "(e.modifiers == 2 && e.which == 10) || (e.ctrlKey && e.which == 13)";
      document.captureEvents(Event.KEYDOWN);
      document.onkeydown = get_key;
      break;
}
}

function get_key(e) {
if (eval(Key)) {
	if (check_postform()){
		check_submit();
		document.postform.submit();
		submit_once(document.postform);
	} else {return false}
}
}

function setup_postform2(){
var testUserName = getCookie("user")
var testPassword = getCookie("pass")
if ((document.cookie)&&(testUserName != null)) {
	document.postform2.UserName.value = testUserName
	if (testPassword!=null) {document.postform2.Password.value = testPassword}
	}
document.postform2.message.style.width="100%";
}

function setup_postform3(){
var testUserName = getCookie("user")
var testPassword = getCookie("pass")
if ((document.cookie)&&(testUserName != null)) {
	document.postform3.UserName.value = testUserName
	if (testPassword!=null) {document.postform3.Password.value = testPassword}
	}
document.postform3.message.style.width="100%";
}

function setup_loginform(){
var testUserName = getCookie("user")
var testPassword = getCookie("pass")
if ((document.cookie)&&(testUserName != null)) {
	document.loginform.UserName.value = testUserName
	if (testPassword!=null) {document.loginform.Password.value = testPassword}
	}
}

function check_postform(){
var max = 42000;
if (document.postform.UserName.value.length == 0){
	alert('Вы забыли указать своё имя!');
	return false;
}
if (document.postform.Password.value.length == 0){
	alert('Вы забыли указать свой пароль!');
	return false;
}
if ((document.postform.forum) && (document.postform.forum.value == "")){
	alert('Вы забыли указать раздел, в который вы хотите поместить объявление!');
	return false;
}
if (document.postform.thread_subject.value.length == 0){
	alert('Вы забыли указать название объявления!');
	return false;
}
if (document.postform.message.value.length == 0){
	alert('Напишите, пожалуйста, что-нибудь о своём объявлении!');
	return false;
}
var x = 0;
for (i=0; i<document.postform.type_message.length; i++){
	if (document.postform.type_message[i].checked) x++;
}
if (x == 0){
	alert('Вы забыли указать тип объявления (продажа, покупка и т.п)!');
	return false;
}
if (document.postform.message.value.length > max){
	alert("Размер Вашего сообщения превышает максимум на "+(document.postform.message.value.length-max)+" символов! Пожалуйста, уменьшите его, разбив на несколько частей!");
	return false;
}
return true;
}


function check_loginform(){
if ((document.loginform.UserName.value.length == 0)||(document.loginform.Password.value.length == 0)){
	alert('Вы заполнили не все поля! Укажите своё имя и пароль!')
	return false;
 }
return true;
}

function check_submit() {
if (old_action!="") document.postform.id.value=old_action
document.postform.target="_self";
}

function submit_once(theform){
if (theform.target=="preview") {return}
if (document.all||document.getElementById){
for (i=0;i<theform.length;i++){
var tempobj=theform.elements[i]
if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset") {tempobj.disabled=true}
}
}
}

function getCookie(name) {
var prefix = name + "="
var StartIndex = document.cookie.indexOf(prefix)
if (StartIndex == -1)
return null
var EndIndex = document.cookie.indexOf(";", StartIndex + prefix.length)
if (EndIndex == -1)
EndIndex = document.cookie.length
return unescape(document.cookie.substring(StartIndex + prefix.length, EndIndex))
}
