// $Id:$

addEvent( window, 'load', load ) || (window.onload = load);

var tfStatus;
var taMessage;
var smilies;
var smBtn;

function load() {
    tfStatus = getNode( "stat" );
    if( tfStatus==null ) return;
    taMessage = getNode( "message" );
    smilies = getNode( "smilies" );

    smBtn = getNode( "smBtn" );

    document.onmouseup = released;
    getNode( "tools" ).className = "";
}

function code(tagName, value) {

    var sel = false;
    var selText = "";
    var UseSelText = false;
    var imgTags = new Array("IMGLEFT", "IMG", "IMGRIGHT");

    var textarea = document.forms['editform'].message;
    if ( textarea.selectionStart != textarea.selectionEnd ) {
        selText = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
        sel = selText.length != 0;
    } else if( document.selection && document.selection.createRange)  {
        sel = document.selection.createRange();
        selText = sel.text;
    }

    if( value == null ) value = "";

    if ( imgTags.indexOf(tagName) != -1 ) {
        value = prompt( "Enter the URL for the image", value );
        if( value == null ) return;
    }

    if( tagName == "URL" || tagName == "EMAIL" ) {
        if( selText.length == 0 ) {
            selText = prompt( "Enter the text to be displayed for the link", "" );
        }
        value = prompt( "Enter the " + (tagName=="URL"?"full URL":"E-mail") + " for the link", value );
        if( value == null ) {
            return;
        }
    }

	if( tagName == "BOXTOP") {
		//BoxAlign = prompt( "Enter L,R or N for Left, Right or No Float", "" );
		//if( BoxAlign == null ) return;
		//WidthOfBox = prompt( "Enter box width", "" );
		//if( WidthOfBox == null ) return;
		//if( selText.length == 0 )
		//	  selText = prompt( "Enter the caption for the box top (Optional)", "" );
		var myText = "[BOX=" + value + "]";
		UseSelText = true;
		ShowForm('boxstartpopup','300','150','150','articles');

	}

	if( tagName == "BOXFOOT") {
		if( selText.length == 0 )
			  selText = prompt( "Enter the footer notes for the box (Optional)", "" );
		var myText = "[/BOX" + "=" + selText + "]";
		UseSelText = true;
	}
	if (tagName == "ARTICLES") {
		var myText = "[ARTICLES=" + value + "]";
		UseSelText = true;
		ShowForm('articlespopup','300','150','150','articles');
	}
	if (tagName == "LINKS") {
		var myText = "[LINKS=" + value + "]";
		UseSelText = true;
		ShowForm('linkspopup','300','150','150','articles');
	}
	if( tagName == "INDEX") {
		var myText = "[INDEX]";
		UseSelText = true;
		value = 1;
	}

	if( tagName == "PAGE") {
			if( selText.length == 0 )
			  selText = prompt( "Enter the text to be displayed for the link", "" );
			value = prompt( "Enter the page number or url for the internal link", value );
			if( value == null ) return;
	}

    if (tagName == "VIDEO") {
        if (selText.length) {
            value = selText;
        } else {
            value = prompt( "Paste the URL of the video here\n(YouTube only right now,\nhttp://www.youtube.com/watch?v=xxxxxxxx)", value);
        }
        if (! value.match('https?://')) return;
    }

    if( tagName == "WHISPER" ) {
        if( selText.length == 0 )
              selText = prompt( "Enter the text to be whispered", selText );
        value = prompt( "Enter a comma separated list of usernames to whisper to.\n\nFor example: name1,name2", "" );
    }

    if( imgTags.indexOf(tagName) == -1 && !sel && !selText && !value) {
        selText = prompt("Enter the text to be formatted:", "");
        if( !selText ) selText = "";
    }

    if( document.selection ) {
        if( sel && (imgTags.indexOf(tagName) == -1 || selText.length == 0) ) {
            sel = typeof(sel) == 'boolean' ? document.selection.createRange() : sel;
	    //sel = document.selection.createRange();
	    if (sel.text == "" || sel.text == null) {
alert("no text");
	      if (UseSelText == true) {
		document.editform.message.value += myText;
	      } else {
		document.editform.message.value += "[" + tagName + (value?"="+value:"") + "]"  + (imgTags.indexOf(tagName) == -1 ? selText  +  "[/" + tagName + "]" : "");
	      }
	       
	    } else {
	      if (UseSelText == true) {
		sel.text = myText;
	      } else {
		sel.text = "[" + tagName + (value?"="+value:"") + "]"  + (imgTags.indexOf(tagName) == -1 ? selText  +  "[/" + tagName + "]" : "");
	      }
	      sel.select();
	    }
        } else {
	    if (UseSelText == true) {
	      var tagText = myText;
	    } else {
	      var tagText = "[" + tagName + (value?"="+value:"") + "]"  + (imgTags.indexOf(tagName) == -1 ? selText  +  "[/" + tagName + "]" : "");
	    }
            textarea.value += tagText;
alert("Second Bit");
        }
    } else {
        var start = textarea.selectionStart;
	if (UseSelText) {
	    var tagText = myText;
	} else {
	    var tagText = "[" + tagName + (value ? "=" + value : "") + "]"  + (imgTags.indexOf(tagName) == -1 ? selText  +  "[/" + tagName + "]" : "");
	}
        textarea.value = textarea.value.substring(0, textarea.selectionStart) + tagText + textarea.value.substring(textarea.selectionEnd);
        textarea.focus();
        textarea.selectionStart = start;
        textarea.selectionEnd = start + tagText.length;
    }

    switch( tagName ) {
        case "SMALL" :  document.forms['editform'].size.selectedIndex = 0; break;
        case "LARGE" :  document.forms['editform'].size.selectedIndex = 0; break;
        case "HUGE"  :  document.forms['editform'].size.selectedIndex = 0; break;
        case "FONT"  :  document.forms['editform'].font.selectedIndex = 0; break;
        case "COLOR" :  document.forms['editform'].color.selectedIndex = 0; break;
    }

    return;
}


function makeboxstart () {

  if (document.forms['editform'].float[0].checked) {
    var float = 'L';
  }

  if (document.forms['editform'].float[1].checked) {
    var float = 'N';
  }

  if (document.forms['editform'].float[2].checked) {
    var float = 'R';
  }

  var values = float + "," + document.forms['editform'].boxstartwidth.value + "," + document.forms['editform'].boxstartcaption.value
  
  code ('BOXTOP', values);
}

function list( value ) {
    var sel = false;
    var selText = "";

    if( document.selection )  {
        sel = document.selection.createRange();
        selText = sel.text;
    }

	if( value ==  null ) {
		value = prompt( "For a (please resize this dialog window to read more) \n\nNumbered list, enter OL\nBullet list, enter UL", "" );
		if( value == null ) 
			value = "";
	  else {
			value = value.substring(0,2).toLowerCase();
			value = (value=="ol" || value=="ul")?value:false;
		}
	}

    if( !sel ) {
        while( 1 ) {
            listItem = prompt( "Enter a list item.\n Leave the box empty or press Cancel to complete the list.", "");
            if( !listItem || listItem == "" ) break;
            selText += "[li]" + listItem + "[/li]";
        }
        document.forms['editform'].message.value += "[" + value + "]"  +  selText  +  "[/" + value + "]";
    } else {
        selText = selText.replace(/\n/g, "\n  [LI]");
        sel.text = "[" +value +  "][li]"  +  selText  +  "[/li][/" +value + "]";
      sel.collapse(false);
      sel.select();
    }
    return;
}

function smilie( smile ) {
    if( !document.selection )  {
        var textarea = document.forms['editform'].message;
        var start = textarea.selectionStart;

        textarea.value = textarea.value.substr(0, start) + smile + " " + textarea.value.substr(textarea.selectionEnd, textarea.textLength);
        textarea.selectionStart = start + smile.length + 1;
        textarea.selectionEnd = textarea.selectionStart;

        return;
    }
    var sel = document.selection.createRange();
    sel.text = smile + " ";
    sel.collapse(false);
    sel.select();
}



var c=0;
var s=false;
var state = 'none';

function ShowForm(element,xwidth,yheight,posleft,titlebar) {

  if (state == 'block') { 
    state = 'none'; 
  } 
  else { 
    state = 'block'; 
  } 
//  if (document.all) { //IS IE 4 or 5 (or 6 beta) 
//    eval( "document.all." + element + ".style.display = state"); 
//    eval( "document.all." + element + ".style.width = " + xwidth + "px"); 
//    eval( "document.all." + element + ".style.height = " + yheight + "px"); 
//    eval( "document.all." + element + ".style.left = " + posleft + "px"); 
//  } 
  if (document.layers) { //IS NETSCAPE 4 or below 
    document.layers[element].display = state; 
    document.layers[element].width = xwidth + "px"; 
    document.layers[element].height = yheight + "px"; 
    document.layers[element].left = posleft + "px"; 
  } 
//  if (document.getElementById &&!document.all) {
  if (document.getElementById) {  
    hza = document.getElementById(element); 
    hza.style.display = state; 
    hza.style.width = xwidth + "px"; 
    hza.style.height = yheight + "px"; 
    hza.style.left = posleft + "px"; 
  }

}

function makearticlelist () {
  //var values = document.editform.articlelisthowmany.value + "," + document.editform.articlelisttype.value + ",CAT," + document.editform.articlelistgroup.value;
  //var howmanyelement = document.getElementById('articlelisthowmany');
  //var howmany = howmanyelement.value;

  var values = document.getElementById('articlelisthowmany').value + "," + document.getElementById('articlelisttype').value + ",CAT," + document.getElementById('articlelistgroup').value;  
  code ('ARTICLES', values);
}

function makelinklist () {
  var values = document.forms['editform'].linklisthowmany.value + "," + document.forms['editform'].linklisttype.value + "," + document.forms['editform'].linklistgroup.value;
  
  code ('LINKS', values);
}


function help( node ) {
  if( !node || !tfStatus ) return;
  tfStatus.value = node.title;
}



var imgPressed;
function pressed( node ) {
    imgPressed = node;
    if( node.setAttribute ) {
      imgPressed.className = "pressed";
    }
}

function released( ) {
    if( !imgPressed ) return;
    if( imgPressed.setAttribute ) {
      imgPressed.className = "";
    }
}



// COMMON FUNCTIONS
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for (var i=0; i < this.length; i++) {
            if (this[i] == obj) {
                return i;
            }
        }
        return -1;
    }
}

function toggle( idStr ) {
    var node = getNode( idStr );

    if( node == null ) return;

    if( node.style.display == "" || node.style.display == "none" )
    node.style.display = "block"; 
    else
      node.style.display = "none"; 

}

function addEvent( node, evtType, func ) {
    if( node.addEventListener ) {
        node.addEventListener( evtType, func, false );
        return true;
    } else if( node.attachEvent ) {
        return node.attachEvent( "on" + evtType, func );
    } else {
        return false;
    }
}


function getNode( nodeId ) {
    if( document.getElementById ) {
        return document.getElementById( nodeId );
    }
    else if( document.all && document.all( nodeId ) ) {
        return document.all( nodeId );
    }
    else if( document.layers && document.layers[ nodeId ] ) {
        return document.layers[ nodeId ];
    }
    else {
        return false;
    }
} 

