﻿var clientPC = navigator.userAgent.toLowerCase(); // Get client info

// Support properties
var is_gecko = ( (clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0')==-1));

var is_safari = (( clientPC.indexOf('applewebkit')!=-1) && 
                ( clientPC.indexOf('spoofer')==-1 ));

var is_khtml = ( navigator.vendor == 'KDE' || 
                ( document.childNodes && !document.all && !navigator.taintEnabled ));

if (clientPC.indexOf('opera') != -1) 
{
	var is_opera = true;
	var is_opera_preseven = ( window.opera && !document.childNodes );
	var is_opera_seven = ( window.opera && document.childNodes );
}



// Inserts a tag into the selected position in the text area.
function InsertTag( strOpenTag, strCloseTag, strDefaultText, strAttributeSet )
{
    // Get the text area.
    var txtContent = document.getElementsByTagName('textarea')[0];
        
    // IE
	if ( document.selection  && !is_gecko ) 
	{
	    // Get the selection
		var strText = document.selection.createRange().text;
		
		// Do we have any text selected.
		if ( !strText )
			strText = strDefaultText;
		
		// Construct the attribute set for the tag.	
        strText = AddAttributes( strAttributeSet, strText );
        
        // Refocus.
        txtContent.focus();
		
		// Put the mark up in.
		document.selection.createRange().text = strOpenTag + 
		    strText + strCloseTag;
	}
	// MOZZIE
	else
	{	
        // They may highlight text in the text box.
        var bHighlighted = false;

        // Starting position of the cursor.
        var nStartPos = txtContent.selectionStart;

        // Ending position of the cursor.
        var nEndPos = txtContent.selectionEnd;

        // Get the location of the scrolbar.
        var nTop = txtContent.scrollTop;

        // Are we highlighting?
        if ( nEndPos - nStartPos )
	        bHighlighted = true;
        	
        // Get any highlighted text.
        var strText = ( txtContent.value ).substring( nStartPos, nEndPos );

        // Do we have any text?
        if ( !strText )
	        strText = strDefaultText;

        // Construct the attribute set for the tag.	
        strText = AddAttributes( strAttributeSet, strText );

        // Construct the string to insert.
        var strSubstring = strOpenTag + strText + strCloseTag;

        // Set the content value.
        txtContent.value = txtContent.value.substring( 0, nStartPos ) + strSubstring +
	        txtContent.value.substring( nEndPos, txtContent.value.length );
        	
        txtContent.focus();

        // set new selection
        if ( bHighlighted ) 
        {
            // Get the end position for the cursor.
	        var nPosition = nStartPos + ( strOpenTag.length + strText.length + 
	            strCloseTag.length );
	        txtContent.selectionStart = nPosition;
	        txtContent.selectionEnd = nPosition;
        } 
        else 
        {
            // Set the start highlight position to selection start.
	        txtContent.selectionStart = nStartPos + strOpenTag.length;
        	
	        // Set the start highlight position to selection start + all text added.
	        txtContent.selectionEnd = nStartPos + strOpenTag.length + strText.length;
        }

        // Set the text area to focus on what weve just put in.
        txtContent.scrollTop = nTop;
	}
}

// Creates an attribute string for the tag created.
function AddAttributes( strAttributeSet, strValue )
{
    // The string to construct with attributes.
    var strAttributes = " ";
    
    // Put the relevant attributes in the string.
    if( strAttributeSet == "doc" )
    {
        // The name attribute.
        strAttributes += "[name ";
        
        // Do we have a value for it?
        if( strValue == "" )
            strAttributes += "document name..";
        else 
            strAttributes += strValue;
            
        strAttributes += " name] ";
        
        // The name attribute.
        strAttributes += "[text ";
        
        // Do we have a value for it?
        if( strValue == "" )
            strAttributes += "text when viewing..";
        else 
            strAttributes += strValue;
            
        strAttributes += " text] ";
        
        return strAttributes;
    }
    
    // Put the relevant attributes in the string.
    else if( strAttributeSet == "link" )
    {
        // The name attribute.
        strAttributes += "[address ";
        
        // Do we have a value for it?
        if( strValue == "" )
            strAttributes += "web address..";
        else 
            strAttributes += strValue;
        
        strAttributes += " address] ";
        
        // The name attribute.
        strAttributes += "[text ";
        
        // Do we have a value for it?
        strAttributes += "text to see..";
        
        strAttributes += " text]";
            
        return strAttributes;
    }
    // Put the relevant attributes in the string.
    else if( strAttributeSet == "resource" )
    {
        // The name attribute.
        strAttributes += "[resource ";
        
        // Do we have a value for it?
        if( strValue == "" )
            strAttributes += "resource name..";
        else 
            strAttributes += strValue;
            
        strAttributes += " resource]";
            
        return strAttributes;
    }
    
    // Nothing to do if anything els.
    else 
        return strValue;
}