// Check for Browser & Platform for PC & IE specific bits
// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
                && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));

var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac")!=-1);

var oldmenu = null;
var menu = null;

// *******************************************************
// ***  Function: editorBtnImage()                      ***
// *******************************************************
function editorBtnImage()
{
	var image_url  = document.getElementById('editor_images_url').value;
	var image_alt  = document.getElementById('editor_images_alt').value;
	var tag  = '';
	
	// 1) Проверим введен ли URL
	if ( image_url == '' )
	{
		alert('Пожалуйста, введите URL !');
		return false;
	}
	else
	{
		tag = '[img';
		if (image_alt != '' )
		{
			tag = tag + ' alt=' + image_alt;
		}
		tag = tag + ']' + image_url + '[/img]';

		document.getElementById('editor_images_url').value='';
		document.getElementById('editor_images_alt').value='';
		editorBtn(tag, '');
	}
}
// *******************************************************
// ***  Function: editorBtnLink()                      ***
// *******************************************************
function editorBtnLink()
{
	var link_url  = document.getElementById('input_text_link_url').value;
	var link_text = document.getElementById('input_text_link_text').value;
	var link_new  = document.getElementById('input_text_link_new').checked;
	var tag  = '';
	
	// 1) Проверим введен ли URL
	if ( link_url == '' )
	{
		alert('Пожалуйста, введите URL !');
		return false;
	}
	else
	{
		tag = '[url=' + link_url;
		// Ок, URL введен
		if ( link_new )
		{
			tag = tag + ' new';
		}
		tag = tag + ']';
		if ( link_text != '' )
		{
			tag = tag + link_text + '[/url]';
		}
		else
		{
			tag = tag + link_url + '[/url]';
		}
		document.getElementById('input_text_link_url').value='';
		document.getElementById('input_text_link_text').value='';
		document.getElementById('input_text_link_new').checked=false;
		editorBtn(tag, '');
	}
}
// *******************************************************
// ***  Function: editorBtnLink()                      ***
// *******************************************************
function editorBtnEmail()
{
	var email_url  = document.getElementById('input_text_email_url').value;
	var email_text = document.getElementById('input_text_email_text').value;
	var tag  = '';
	
	// 1) Проверим введен ли e-mail
	if ( !emailCheck(email_url) )
	{
		return false;
	}
	else
	{
		tag = '[email=' + email_url + ']';
		if ( email_text != '' )
		{
			tag = tag + email_text + '[/email]';
		}
		else
		{
			tag = tag + email_url + '[/email]';
		}
		document.getElementById('input_text_email_url').value='';
		document.getElementById('input_text_email_text').value='';
		editorBtn(tag, '');
	}
}
// *******************************************************
// ***  Function: editorBtnDocument()                      ***
// *******************************************************
function editorBtnDocument()
{
	var document_url  = document.getElementById('editor_documents_url').value;
	var document_text = document.getElementById('editor_documents_alt').value;
	var tag  = '';
	
	// 1) Проверим введен ли URL
	if ( document_url == '' )
	{
		alert('Пожалуйста, введите URL документа.');
		return false;
	}
	if ( document_text == '' )
	{
		alert('Пожалуйста, введите заголовок ссылки.');
		return false;
	}
	else
	{
		tag = '[doc=' + document_url + ']' + document_text + '[/doc]';
		
		document.getElementById('editor_documents_url').value='';
		document.getElementById('editor_documents_alt').value='';
		editorBtn(tag, '');
	}
}
// *******************************************************
// ***  Function: editorBtn()                          ***
// *******************************************************
function editorBtn(openTag, closeTag)
{
	
	if ( oldmenu )
	{
		oldmenu.style.display='none';
		oldmenu.open = false;
		//oldmenu = null;
	}
	
	var theSelection = false;
	var txtarea = document.getElementById('txt_editor');

	txtarea.focus();

	if ( is_ie && is_win)
	{
		// === IE ===
		theSelection = document.selection.createRange().text; // Get text selection
		if (theSelection) {
			// Add tags around selection
			document.selection.createRange().text = openTag + theSelection + closeTag;
			txtarea.focus();
			theSelection = '';
			return;
		}
		else
		{
			// Add tags
			document.selection.createRange().text = openTag + closeTag;
			return;
		}
	}
	else if (txtarea.textLength)
	{
		// === Netscape ===
		var selLength = txtarea.textLength;
		var selStart  = txtarea.selectionStart;
		var selEnd    = txtarea.selectionEnd;
		
		if (selEnd == 1 || selEnd == 2)
		{
			selEnd = selLength;
		}
	
		var s1 = (txtarea.value).substring(0,selStart);
		var s2 = (txtarea.value).substring(selStart, selEnd)
		var s3 = (txtarea.value).substring(selEnd, selLength);
		
		txtarea.value = s1 + openTag + s2 + closeTag + s3;
		return;
	}
	else
	{
		// === Opera, other drack ===
		txtarea.value = txtarea.value + openTag + closeTag;
	}
	txtarea.focus();
}
// *******************************************************
// ***  Function: EditorMenu()                         ***
// *******************************************************
function EditorMenu(menu)
{
	//alert('Inside EditorMenu');
	if ( oldmenu && oldmenu != menu )
	{
		oldmenu.style.display='none';
		oldmenu.open = false;
	}
	//else
	//{
	//	menu.open = false;
	//}
	
	if(menu.open) 
	{
		menu.style.display='none';
	}
	else
	{
		menu.style.display='block';
	}
	menu.open=!menu.open;
	oldmenu = menu;
	return false;
}

// *******************************************************
// ***  Function: EditorPreview()                      ***
// *******************************************************

function EditorPreview(text)
{
	//alert(window);
	var OpenPreviewWin = window.open('editorpreview.php', "EditorPreviewWindow", "toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,directories=no,status=yes,width=800,height=600");
}

