// ============================================================
// GLOBAL FUNCTIONS FROM VB
// ============================================================


// #############################################################################
// lets define the browser we have instead of multiple calls throughout the file
var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = (userAgent.indexOf('opera') != -1);
var is_saf    = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == "Apple Computer, Inc."));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4    = ((is_ie) && (userAgent.indexOf("msie 4.") != -1));
var is_moz    = ((navigator.product == 'Gecko') && (!is_saf));
var is_kon    = (userAgent.indexOf('konqueror') != -1);
var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));

// catch possible bugs with WebTV and other older browsers
var is_regexp = (window.RegExp) ? true : false;

// #############################################################################
// let's find out what DOM functions we can use
var myDOMtype = '';
if (document.getElementById)
{
	myDOMtype = "std";
}
else if (document.all)
{
	myDOMtype = "ie4";
}
else if (document.layers)
{
	myDOMtype = "ns4";
}



// make an array to store cached locations of objects called by fetch_object
var myobjects = new Array();

// #############################################################################
// function to emulate document.getElementById
function fetch_object(idname, forcefetch)
{
	if (forcefetch || typeof(myobjects[idname]) == "undefined")
	{
		switch (myDOMtype)
		{
			case "std":
			{
				myobjects[idname] = document.getElementById(idname);
			}
			break;

			case "ie4":
			{
				myobjects[idname] = document.all[idname];
			}
			break;

			case "ns4":
			{
				myobjects[idname] = document.layers[idname];
			}
			break;
		}
	}
	return myobjects[idname];
}

// =======================================================
// MY FUNCTIONS 
// =======================================================


var commentform=false;

function quotepost(postid,postname,postsize)
{
	var s="";

	if (commentform)
	{
	s='* Будет добавлена цитата сообщения от <b>'+postname+'</b> размером '+postsize+' символов, id #'+postid+'. [<a href=# onclick=\'return unquote();\'>отменить</a>]';

	fetch_object("qp_msgtd").innerHTML = s;
	fetch_object("qp_postid").value = postid;
	fetch_object("new_message").focus();

	}
	else
	{
	alert('отключено');
	}

return false;
}

function unquote()
{
fetch_object("qp_msgtd").innerHTML = '';
fetch_object("qp_postid").value = '';
fetch_object("new_message").focus();

return false;
}


function quoteSelection()
{
	theSelection = false;
	theSelection = document.selection.createRange().text; // Get text selection

	if (theSelection) 
	{
			insert( '[quote]' + theSelection + '[/quote]');
			fetch_object("new_message").focus();
			theSelection = '';
			return;
	}
	else
	{
			alert('Выделите нужный текст');
	} 
}


function selto(bbcode)
{
	var p1="";
	var p2="";
	theSelection = false;
	theSelection = document.selection.createRange().text; // Get text selection


	switch (bbcode)
	{
		case 'b':
		{
			p1='[b]';	p2='[/b]';
		}
		break;
		
		case 'i':
		{
			p1='[i]';	p2='[/i]';
		}
		break;

		case 'u':
		{
			p1='[u]';	p2='[/u]';
		}
		break;
		case 's':
		{
			p1='[s]';	p2='[/s]';
		}
		break;
    case 'quote':
    {
      p1='[quote]'; p2='[/quote]';
    }
		
		default:
		{
			return false;
		}
	}

	

	if (theSelection) 
	{
			insert( p1 + theSelection + p2);
			fetch_object("new_message").focus();
			theSelection = '';
			return;
	}
	else
	{
			alert('Выделите нужный текст');
	} 
}


function storeCaret(el) 
{ 
	if (el.createTextRange) 
		{el.caretPos = document.selection.createRange().duplicate();}
}

function insert(text) 
{    
	if (fetch_object("new_message").createTextRange && fetch_object("new_message").caretPos) 
		{
			var caretPos = fetch_object("new_message").caretPos;												
			caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
			fetch_object("new_message").focus();
		} 
	else 
		{
		
		fetch_object("new_message").value  += text;
		fetch_object("new_message").focus();
		} 
}  
