// Angel-Board Javascript External Source File
// Author: Jason Berry
//
// Date: 7.24.2002
//
// These functions are to be used only for Angel-Boards files and may not
// be copied or used unless credit is given and permission has been by its
// Author!

// Opens a window to search for users.
function seek_user( form_elm, formName )
{
   if ( form_elm == null )
       form_elm = "user_q";
   if ( formName == null )
       formName = "main";
   var findUser = window.open( "search.php?act=users&formName=" + formName + "&element=" + form_elm, "user_search", "WIDTH=400,HEIGHT=100,alwaysRaised,dependent" );
}

// Go to a Board
function goBoard( url )
{
   if ( url != "" )
   {
      if ( url.charAt( 0 ) == "@" )
      {
	 url = url.substr( 1 );
	 location = "index.php?cID=" + url;
      }
      else
         location = "angelboard.php?boardID=" + url;
   }
}

// Go to a specific URL. Useful with Buttons
function goTo( url )
{
   if ( url != "" )
   {
      if ( url.charAt( 0 ) != "@" )
	  location = url;
      else
      {
	  var newWin = open( url.substr( 1 ), "" );
      }
   }
}

// Check to see if the user wants to check their PMs
function pm_prompt( hasNew, numPMs )
{
   if ( hasNew > 0 )
   {
      if ( confirm( "You have " + numPMs + " new message(s). Do you wish to read them now?" ) )
      {
         if ( confirm( "Open your Inbox in a new window?" ) )
         {
            var pmWindow = window.open( "inbox.php", "", "" );
         }
         else
 	    location = "inbox.php";
      }
      else
         alert( "You can check your PMs at any time by going to the link at the top of the page." );
   }
}

// A function to check all of a series of boxes
function check_all( checkName, setBool, maxIndex )
{
   for ( i = 0; i < maxIndex; i++ )
      eval( "document.forms[0]." + checkName + "[i].checked = " + setBool );
}

// ************************************* Posting Stuff *****************************
// CaretPosition() and getCaretPosition() : http://www.theblueform.com/Home/TheMakingOf.aspx
// CaretPosition object
function CaretPosition()
{
 var start = null;
 var end = null;
}
// Next, how to retrieve the caret positions... 
function getCaretPosition(oField)
{
 // Initialise the CaretPosition object
 var oCaretPos = new CaretPosition();

 // MSIE
 if ( document.selection || navigator.userAgent.toLowerCase().indexOf( 'msie' ) != -1 )
 {
  // Focus on the text box
  oField.focus();

  var oSel = document.selection.createRange();

  var selectionLength = oSel.text.length;

  oSel.moveStart ('character', -oField.value.length);

  oCaretPos.start = oSel.text.length - selectionLength;

  oCaretPos.end = oSel.text.length;
 }
 // Firefox support
 else if( oField.selectionStart >= '0')
 {
  oCaretPos.start = oField.selectionStart;
  oCaretPos.end = oField.selectionEnd;
 }

 // Return results
 return (oCaretPos);
}
// *********************************************************
// Updates the Help Bar
function showHelp( n )
{
   document.main.helpLine.value = n;
}

// Insert Code / Wrap code around Text..
function addCode( code )
{
   var textfield = document.main.message;
	var carotPos  = getCaretPosition( textfield );
	var txt       = textfield.value;
	if ( ( carotPos.end - carotPos.start ) > 0 )
	{
	   textfield.value = txt.substring( 0, carotPos.start );;
	   textfield.value += "[" + code + "]" + txt.substring( carotPos.start, carotPos.end ) + "[/" + code + "]";
	   textfield.value += txt.substring( carotPos.end );
	}
	else
		textfield.value += "[" + code + "]text[/" + code + "]";
   textfield.focus();
}

// Add or wrap codes.
function addSpecialCode( code )
{
   var textfield = document.main.message;
   var carotPos  = getCaretPosition( textfield );
   var txt       = textfield.value;
   if ( ( carotPos.end - carotPos.start ) > 0 ) // Wrap
   {
	   textfield.value = txt.substring( 0, carotPos.start );
	   if ( code != 'whisper' )
		   textfield.value += "[" + code + "]" + txt.substring( carotPos.start, carotPos.end ) + "[/" + code + "]";
		else
		   textfield.value += "[whisper to (insert names)]" + txt.substring( carotPos.start, carotPos.end ) + "[/whisper]";
	   textfield.value += txt.substring( carotPos.end );
	} // Otherwise, offer prompt.
	else
	{
	   var getWhat = ( code != 'whisper' ) ? 'URL' : 'Name(s) [Comma-separated]';
	   var value = prompt( 'Please enter the desired ' + getWhat, '' );
	   if ( value != '' && value != null )
	   {
	      if ( code == 'link' )
			   textfield.value += "[link=" + value + "]Link Text[/link]";
			else if ( code == 'img' )
				textfield.value += "[img]" + value + "[/img]";
			else if ( code == 'whisper' )
				textfield.value += "[whisper to " + value + "]Whisper Text[/whisper]";
		}
	}
	textfield.focus();
}

function addFont( n )
{ 
   var textfield = document.main.message;
   var carotPos  = getCaretPosition( textfield );
   var txt       = textfield.value;
   var selection = document.getElementsByName( n )[0]; // Get the Drop down
   if ( selection.selectedIndex == 0 )
   	return;
   	
   if ( ( carotPos.end - carotPos.start ) > 0 ) // Wrap
   {
	   textfield.value = txt.substring( 0, carotPos.start );
      if ( n == 'fontsize' )
		   textfield.value += "[size=" + selection.options[selection.selectedIndex].value + "]" + txt.substring( carotPos.start, carotPos.end ) + "[/size]";
		else
		   textfield.value += "[color=" + selection.options[selection.selectedIndex].value + "]" + txt.substring( carotPos.start, carotPos.end ) + "[/color]";
	   textfield.value += txt.substring( carotPos.end );
	} // Otherwise, offer prompt.
	else
	{
      if ( n == 'fontsize' )
		   textfield.value += "[size=" + selection.options[selection.selectedIndex].value + "]text[/size]";
		else
		   textfield.value += "[color=" + selection.options[selection.selectedIndex].value + "]color[/color]";
	}
   document.main.message.focus();
}

// Opens a Smiley
function smilies()
{
   var smileWindow = window.open( "add_smiles.php", "smile_window", "WIDTH=220,HEIGHT=200,alwaysRaised,dependent, scrollbars" );
}

// Opens a Code Window
function more_codes()
{
   var codeWindow = window.open( "more_codes.php", "code_window", "WIDTH=300,HEIGHT=280,alwaysRaised,dependent,scrollbars" );
}
// *****************************************************************


// Similar to the PHP Message Prompt Function, but in Javascript
function redirect( error, url )
{
   alert( error );
   location = url;
}

// Sends Form information to a separate location
function previewPost( goURL, pmType )
{
   // Set the preview variable and then submit the form.
   document.main.preview.value = pmType;

   // Redirect the form
   document.main.action = goURL;

   document.main.submit();
}


// Check to make sure that two passwords are similar
function checkPW( pw1, pw2 )
{
   if ( pw1 != pw2 )
   {
      alert( "Your passwords do not match." );
      return false;
   }
   else
      return true;
}

function mark_all( start, count, status )
{
   for (i = 0; i < count; i++)
       eval( "document.main.elements[" + (start + i) + "].checked = " + status );
}

function form_exec( form, act, winprompt )
{
   if ( winprompt == 0 )
   {
      form.action = act;
      form.submit();
   }
   else
   {
      if ( confirm( "Are you sure you wish to proceed?" ) )
      {
	 form.action = act;
	 form.submit();
      }
   }
}

// Opens a Help Window
function show_help( key )
{
   if ( key.charAt( 0 ) == "?" ) // We expect a user group ID here
       key = key.substr( 1 );
   var helpWindow = window.open( "help.php?key=" + key, "help_window", "WIDTH=400,HEIGHT=200,alwaysRaised,dependent" );
}

// Opens a User List Window
function viewUserList( id )
{
   var userListWindow = window.open( "user_list.php?id=" + id, "userlist_window", "WIDTH=300,HEIGHT=280,alwaysRaised,dependent,scrollbars" );
}

// Opens Report Post
function reportPost( msgID, isPost )
{
   var reportWindow = window.open( "report.php?msgID=" + msgID + "&isPost=" + isPost, "report_window", "WIDTH=320,HEIGHT=230,alwaysRaised,dependent,scrollbars" );
}

var ie = navigator.appName;
ie = ( !( navigator.appVersion.charAt(0)=="4" && navigator.appName == "Netscape" )  ) ? true : false;

var bgTmp = "";

// Highlights a particular menu option
function menu_hl( which, highLight )
{
   bgTmp = ( ie ) ? which.style.backgroundColor : document.which.backgroundColor;

   if ( ie )
      which.style.backgroundColor = highLight;
   else
      document.which.backgroundColor = highLight;

}

// Unhighlights a particular menu option
function menu_uhl( which )
{
   if ( ie )
      which.style.backgroundColor = bgTmp;
   else
      document.which.backgroundColor = bgTmp;
}

// Reveals Mod Panel
function showModPanel( btn )
{
    if ( modPanelView == 0 )
    {
        btn.value = "Hide";
  	    document.getElementById('modPanel').style.display = "block";
    }
    else
    {
        btn.value = "Show";
  	    document.getElementById('modPanel').style.display = "none";
    }
    modPanelView = 1 - modPanelView;
}

// Reveals subMenu
function showMenuPanel( menu )
{
    if ( document.getElementById( menu ).style.display == "none" )
    {
        if ( menu == "admin" )
           document.getElementById( "admin_img" ).src  = adminexpanded.src;
        else
	        document.getElementById( "user_img" ).src  = userexpanded.src;

        document.getElementById( menu ).style.display = "block"
    }
    else
    {
        if ( menu == "admin" )
 	        document.getElementById( "admin_img" ).src  = adminexpand.src;
 	     else
 	        document.getElementById( "user_img" ).src  = userexpand.src;
        
        document.getElementById( menu ).style.display = "none";
    }
}

var ImageConsole = new Array();
var ImageCount   = 0;
// Image Control
function imageSize( img )
{
    var width = img.width;
    var height = img.height;
    
    // Image Source, Width, Height, and Scale Direction
    ImageConsole[ImageCount] = new Array( img, width, height, 0 );
    ImageCount++;
    
    if ( width > MAX_IMG_SIZE_X )
    {
        var ratio = height / width;
        width = MAX_IMG_SIZE_X;
        height = MAX_IMG_SIZE_X * ratio;
        
        img.width = width;
        img.height = height;
        
        img.id = "ENLARGE";
        img.alt = img.alt + "\nThis image has been scaled. Click on it to view 100%";
    }
    
	 if ( height > MAX_IMG_SIZE_Y )
    {
		  var ratio = width / height;
		  height = MAX_IMG_SIZE_Y;
		  width  = MAX_IMG_SIZE_Y * ratio 
		  
		  img.width  = width;
		  img.height = height;
		  
		  img.id = "ENLARGE";
		  img.alt = img.alt + "\nThis image has been scaled. Click on it to view 100%";
	 }
}

// Image Handling
function imageHandler( img )
{
    var width = img.width;
    var height = img.height;
    var IDTag = img.id;
    var ImgIndex = 0;
    for ( ImgIndex = 0; ImgIndex < ImageConsole.length; ImgIndex++ )
    {
        if ( ImageConsole[ImgIndex][0] == img )
           break;
    }
    var Orig_width  = ImageConsole[ImgIndex][1];
    var Orig_height = ImageConsole[ImgIndex][2];
    var Direction   = ImageConsole[ImgIndex][3];
    
    var Scale = width / Orig_width;
    
    if (  IDTag == "ENLARGE"  )
    {
       window.open( img.src, "", "alwaysRaised,dependent,scrollbars,resizable" );
    }
    else
    {
        if ( Direction == 0 )
        {
           img.width *= 2;
           if ( img.width > MAX_IMG_SIZE_X )
           {
               img.width = MAX_IMG_SIZE_X;
               ImageConsole[ImgIndex][3] = 1;
           }
        }
        else
        {
           if ( height >= 16 )
               img.width /= 2;
           else
           {
               ImageConsole[ImgIndex][3] = 0;
               img.width *= 2;
           }
        }
        img.height = img.width / ( Orig_width / Orig_height );
    }
}

// Show hidden block
function showOffTopicPost( otb )
{
    document.getElementById( otb ).style.display = "block";
    document.getElementById( "hide" + otb ).style.display = "none";
}

// Show or hide a DIV Tag
function showDiv( img, block )
{
    if ( document.getElementById( block ).style.display == "none" )
    {
        img.src  = divHide.src;
        document.getElementById( block ).style.display = "block";
    }
    else
    {
        img.src  = divShow.src;
        document.getElementById( block ).style.display = "none";
    }
}

// Add Quote to Message
function addQuote( msgID, author )
{
    var quoteText;
	 eval( "quoteText = context" + msgID );
    parent.document.main.message.value += "\r\n[quote=\"" + author + "\"]" + quoteText + "[/quote]\r\n";
}

// Opens Ban Window
function setBan( msgID, isPost )
{
   var banWindow = window.open( "ban.php?msgID=" + msgID + "&amp;isPost=" + isPost, "ban_window", "WIDTH=420,HEIGHT=230,alwaysRaised,dependent,scrollbars" );
}

// Show IP List
function showIPList( ip )
{
   var ipWindow  = window.open( "ip.php?ip=" + ip, "ip_window", "WIDTH=320,HEIGHT=230,alwaysRaised,dependent,scrollbars" );
}