/*__________________________FUNCTION LISTING_________________________________
function BigImage(ImageType,ImageName)
function blinkSubcatName()
function CheckNewProjectForm()
function CheckRepImage(WhichImageChoice)
function ClearCategoryLists(IgnoreOptions)
function clearSubCatMenu()
function CreateUndoState(Content,increment)
function DeleteImagePermanently(ImageType,ImageName,ImageDiv)
function DeleteImagesPermanently()
function GenerateProjectCode()
function GenerateSubcatCode()
function imageadd(NewImageName)
function imageAligner(WhichImage,e)
function imagecreate()
function imageDeleter(WhichImage)
function keyhandler(e) 
function linkadd()
function MaxChars(WhichField,MaxLength)
function previewcontent()
function RedoToUndoState()
function RemoveUnusedTags(Tag)
function ReplaceImageText(ObsoleteDiv)
function ReturnToUndoState()
function SetFormFieldnames()
function SetPageHeader()
function SetSelection(CheckBoxCount,e)
function settype(y)
function UpdateDisplay()
function UpdateNavDisplay()
function UpdateOptions()
function UpdateTextarea(Block)
function WriteAllLayers()
*/

//__________________________INITIAL VARIABLES________________________________

document.onkeypress = keyhandler;

UndoEnabled = true; 
SubmitOnEnter = false

alerted="no";
CancelBlink = true;
displaySlashB="0";
displaySlashI="0";
displaySlashU="0";
newformatB="BOLD";
newformatI="ITALICS";
newformatU="UNDERLINED";
slash="";
LockSetType = "off";
UndoState = new Array();
CurrentUndoState=0;
MaxUndoState = 0;
MonitorTextArea="";
PreviousBox = "";
LockButtons=false;

//____________________________________________________________________________

function BigImage(ImageType,ImageName)
{
  ImageName = escape(ImageName);
  window.open('bigimage.asp?ImageName=' + ImageName + '&ImageType=' + ImageType,'imagewindow','height=300,width=300,location=0,toolbar=0,status=0,resizable=1');
}

//____________________________________________________________________________

function blinkSubcatName()
{
  if (CancelBlink==false && (subcategoryname.value=='REQUIRED' || subcategoryname.value=='') && (subcategorycode.value=='REQUIRED' || subcategorycode.value==''))
  {
    if(subcategoryname.value=='REQUIRED'){subcategoryname.value=''; }
    else{subcategoryname.value='REQUIRED';}
    setTimeout('blinkSubcatName();',1000);
  }
}

//____________________________________________________________________________

function CheckNewProjectForm()
{
  SetFormFieldnames();
  if (CancelBlink==false && (subcategoryname.value=='REQUIRED' || subcategoryname.value=='') && (subcategorycode.value=='REQUIRED' || subcategorycode.value==''))
  {
    alert('You have chosen to create a new category in which to place this new project. Before proceding, you must also create a new subcategory.');
    subcategoryname.focus();
    return([false]);
  }

  if (projectTitle.value=='')  {alert('Before proceding, you must enter a title for this proect.'); projectTitle.focus(); return([false]);  }
  if (projectCode.value=='')  {alert('You have entered a title for this project, but not a project code. This should have been automatically generated for you. If you would prefer to use another code, you may, but this value must can be left blank.'); projectCode.focus(); return([false]);  }

  if (imagecount.value=='')  
  {
    if(!confirm('You have not entered the number of images you would like to upload. You can leave this blank, but there will be no images for this project. You can always add images to the project later if you choose to proceed.')){imagecount.focus(); return([false]);}
  }
  xform.submit();
}

//____________________________________________________________________________

function CheckRepImage(WhichImageChoice)
{
  SetFormFieldnames();
  if(! isNaN(xform.elements['newimagelist'].length))
  {
    if (!xform.elements['newimagelist'][WhichImageChoice].checked)  {    xform.elements['newimagelist'][WhichImageChoice].checked=true;  }
  }
  else
  {
    if (!xform.elements['newimagelist'].checked)  {    xform.elements['newimagelist'].checked=true;  }
  }

}

//____________________________________________________________________________

function ClearCategoryLists(IgnoreOptions)
{
  SetFormFieldnames();
  CurrentNumberOfOptions=0;
  if(categorycode.value=='')
  {
    NewCategoryCode = ReplaceChars(categoryname.value,"\'","");
    NewCategoryCode = ReplaceChars(NewCategoryCode,"alpha","");
    NewCategoryCode = NewCategoryCode.toLowerCase();
    categorycode.value = NewCategoryCode
  }

  if(!IgnoreOptions)
  {
    clearSubCatMenu();
    ThisOptionText = MainCatArray[0][0][0];
    ThisOptionValue = MainCatArray[0][0][1];
    ThisNewOption = new Option(ThisOptionText,ThisOptionValue,true,true);
    subcategoryID.options[i] = ThisNewOption
    if(categorycode.value!='' && categoryname.value!=''){CancelBlink=false;  blinkSubcatName(); }
  }
}

//____________________________________________________________________________

function clearSubCatMenu()
{
  itcheck=0;
  while(subcategoryID.options.length>0 && itcheck<300)
    {
    subcategoryID.options[0]=null
    itcheck = itcheck +1;
    }

  for(i=0; i<=CurrentNumberOfOptions; ++i)
    {
    subcategoryID.options[i]=null
    }
}

//____________________________________________________________________________

function CreateUndoState(Content,increment)
{
  if (Content=='')
  {
    SetFormFieldnames();
    Content = newcontent.value;
    Content = document.all['contentpreviewfield'].innerHTML
    if(Content=='<IMG src="images/pixel.gif" width=250>'){Content='';}
  }
  UndoState[CurrentUndoState] = Content;
  if(increment)
    {
    CurrentUndoState = CurrentUndoState + 1;
    MaxUndoState = MaxUndoState + 1
    }
}

//____________________________________________________________________________


function DeleteImagePermanently(ImageType,ImageName,ImageDiv)
{
  if(confirm('WARNING:\n\nYou are about to remove this image from your image library.\nIt will be gone forever.\n\n'))
  {window.open('admin_deleteimage.asp?ImageName=' + ImageName + '&ImageType=' + ImageType + '&ImageDiv=' + ImageDiv,'imagewindow','height=50,width=200,location=0,toolbar=0,status=0,resizable=1');}
}

//____________________________________________________________________________

function DeleteImagesPermanently()
{   
  PassedDeleteList = '';
  SetFormFieldnames();
  DeleteList = xform.elements['deletelist']
  for(i=0;i<DeleteList.length;++i)
  {
    if (DeleteList[i].checked)
    {
      if(PassedDeleteList.length>0){PassedDeleteList = PassedDeleteList  + ',';}
      ThisImageToDelete = DeleteList[i].value;
      PassedDeleteList = PassedDeleteList  + DeleteList[i].value;
    }
    if(i>59){i=DeleteList.length;}
  }
  if(confirm('WARNING:\n\nYou are about to remove ALL the images from your image library that you have placed a check next to in the right hand checkbox.\nThese images will be gone forever.\n\n(Note: This process will only delete a maximum of 60 images at a time.)'))
  {window.open('admin_deleteimages.asp?ImageList=' + PassedDeleteList + '&imageType=projects','imagewindow','height=50,width=200,location=0,toolbar=0,status=0,resizable=1');}
}

//____________________________________________________________________________

function GenerateProjectCode()
{
  SetFormFieldnames();
  projectTitle.value = ReplaceChars(projectTitle.value,"\'","");
  InputTitle = projectTitle.value
  suffix=1;
  ProjectTitleText = InputTitle.toLowerCase();

  while(UsedProjectTitles.indexOf(ProjectTitleText)>(-1))
  {
    suffix=suffix + 1; 
    InputTitle = ReplaceChars(InputTitle,"nonnumeric","");
    projectTitle.value = InputTitle + suffix;
    ProjectTitleText = projectTitle.value
    ProjectTitleText = ProjectTitleText.toLowerCase();
  }

  NewProjectCode = ReplaceChars(projectTitle.value,"alpha","");
  NewProjectCode = NewProjectCode.toLowerCase();

  suffix=1;
  projectCode.value = NewProjectCode;
  while(UsedProjectNames.indexOf(projectCode.value)>(-1))
  {
    suffix=suffix + 1; 
    projectCode.value = NewProjectCode + suffix;
  }
 
}

//____________________________________________________________________________

function GenerateSubcatCode()
{
  SetFormFieldnames();
  subcategoryname.value = ReplaceChars(subcategoryname.value,"\'","");
  Newsubcategorycode = ReplaceChars(subcategoryname.value,"alpha","");
  Newsubcategorycode = Newsubcategorycode.toLowerCase();
  subcategorycode.value = Newsubcategorycode;
}

//____________________________________________________________________________

function imageadd(NewImageName,WhichBlock)
{
  SetFormFieldnames();

  CurrentImageCount = imagecount.value
  ++CurrentImageCount;
  imagecount.value = CurrentImageCount;

  if(document.ContentEditor1)  {    InsertImage(NewImageName,WhichBlock);  } //THIS FUNCTION IS FOUND IN editorscript.js  
  else if(newcontent.value)  
  {
    CurrentText = newcontent.value;
    CreateUndoState(CurrentText,true);    
    ImageSyntax = '<img src="custom/content/' + NewImageName + '" name="ContentImage' + CurrentImageCount + '" align="left" onclick="imageAligner(\'ContentImage' + CurrentImageCount + '\',event);" ondblclick="imageDeleter(\'ContentImage' + CurrentImageCount + '\');">';
    newcontent.value = CurrentText  + "<!-- ContentImage" + CurrentImageCount + " start-->" + ImageSyntax + "<!-- ContentImage" + CurrentImageCount + " end-->";
    UpdateDisplay();  
  }
  
}


//____________________________________________________________________________

function imageAligner(WhichImage,e)
{
  SetFormFieldnames();

  ShiftDown = window.event.shiftKey; 
  CntrlDown = window.event.ctrlKey;
  if(ShiftDown){imageDeleter(WhichImage);}
  else
  {
    ImageStartString = "<!-- " + WhichImage + " start-->";
    ImageEndString = "<!-- " + WhichImage + " end-->";

    CurrentNewContent = newcontent.value

    NewContentBeforeImage = CurrentNewContent.split(ImageStartString)[0];
    NewContentAfterImage = CurrentNewContent.split(ImageEndString)[1];

    ImageStringStart = CurrentNewContent.indexOf(ImageStartString) + ImageStartString.length
    ImageStringEnd = CurrentNewContent.indexOf(ImageEndString)

    ImageString = CurrentNewContent.substring(ImageStringStart,ImageStringEnd)
    if (ImageString.indexOf('align="left"')>-0)  {    ImageString = ImageString.replace('align="left"','align="right"');  }
    else if (ImageString.indexOf('align="right"')>-0)  {    ImageString = ImageString.replace('align="right"','align="left"');  }
  //else if (ImageString.indexOf('align="center"')>-0)  {    ImageString = ImageString.replace('align="center"','align="right"');  }



    UpdatedContent = NewContentBeforeImage + ImageStartString + ImageString + ImageEndString + NewContentAfterImage;

    CreateUndoState(CurrentNewContent,true);

    newcontent.value = UpdatedContent;

    UpdateDisplay();  
  }
}

//____________________________________________________________________________

function imagecreate(WhichBlock)
{
  window.open('admin_singleimageupload.asp?WhichBlock=' + WhichBlock,'imagemaker','width=600,height=200,location=0,toolbar=0,status=1');
}

//____________________________________________________________________________

function imageDeleter(WhichImage)
{
  SetFormFieldnames();
  if(confirm('Deleting Image.\n Are you sure?'))
  {
    ImageStartString = "<!-- " + WhichImage + " start-->";
    ImageEndString = "<!-- " + WhichImage + " end-->";

    CurrentNewContent = newcontent.value

    NewContentBeforeImage = CurrentNewContent.split(ImageStartString)[0];
    NewContentAfterImage = CurrentNewContent.split(ImageEndString)[1];

    UpdatedContent = NewContentBeforeImage + " " + NewContentAfterImage;

    CreateUndoState(CurrentNewContent,true);

    newcontent.value = UpdatedContent;

    UpdateDisplay();  
  }
}

//____________________________________________________________________________

function keyhandler(e) 
{
StopLoop = false;
if(document.forms.length>0)  
  {

    if(MonitorTextArea)
    {
       MaxChars(MonitorTextArea,90);
    }
    else
    {

      if (document.layers){Key = e.which; ShiftDown = e.shiftKey; CntrlDown = e.ctrlKey;}
      else {Key = window.event.keyCode; ShiftDown = window.event.shiftKey; CntrlDown = window.event.ctrlKey;}
    //----------------------------------
      if (Key == '13' && !ShiftDown)
        {
        if (SubmitOnEnter)
        {
          for(WhichForm=0;WhichForm<document.forms.length;++WhichForm)
          {
            if(StopLoop==false)
            {
              for(WhichElement=0;WhichElement<document.forms[WhichForm].elements.length;++WhichElement)
              {
                if (document.forms[WhichForm].elements[WhichElement].type!='select-one' && document.forms[WhichForm].elements[WhichElement].defaultValue!=document.forms[WhichForm].elements[WhichElement].value && StopLoop==false)
                {
                  if (FormToSubmit!="")
                  {
                    document.forms[WhichForm].elements[WhichElement].focus(); 
                    WhichElement=document.forms[WhichForm].elements.length; 
                    StopLoop = true; 
                    if (AlternateCheckForm==""){formsubmit(FormToSubmit,SkipFormFields);}
                    else{eval(AlternateCheckForm);}
                  }
                  else{document.forms[WhichForm].submit(); StopLoop = true;}
                  StopLoop = true
                }
              }
            }
          }
        }
        }
    //----------------------------------
      else
        {
          if (UndoEnabled==true)
            {
            if (Key == '26' && CntrlDown){ ReturnToUndoState(); }
            if (Key == '26' && CntrlDown && ShiftDown){ RedoToUndoState(); }
            }
        }
    //----------------------------------
    }
  }
}

//____________________________________________________________________________

function linkadd()
{     
  SetFormFieldnames();
  LinkToAdd = xform.elements['link'].value;
  if(LinkToAdd.indexOf('galaxybuilders.com/')>(-1))
  {
	LinkToAdd = LinkToAdd.substring((LinkToAdd.indexOf('galaxybuilders.com/')+17),LinkToAdd.length);
  }
  else if(LinkToAdd.indexOf('http://')==(-1) && LinkToAdd.indexOf('@')==(-1))
  {
    LinkToAdd = "http://" + LinkToAdd
  }
  //TextToAdd = xform.elements['linkb'].value;
  currentField = newcontent 
  CurrentText = currentField.value;
  CurrentString = CurrentText.toString();

  //------- CLEAR ALL double spaces FROM THE FORM TEXT
  TempCurrentString = ReplaceChars(CurrentString,"  "," ");
  CurrentString = TempCurrentString;
  currentField.value = CurrentString;

  MailToCheck=LinkToAdd.split('@');

  if (MailToCheck.length > 1){MailPreFormatCheck=MailToCheck[0].split(':');}
  if (MailToCheck.length > 1 && MailPreFormatCheck.length<=1){MailTo="mailto:";}
  if (MailToCheck.length > 1 && MailPreFormatCheck.length>1){MailTo="";}
  if (MailToCheck.length <= 1){MailTo="";}


  FormatCheck=LinkToAdd.split(':');

  if (FormatCheck.length == 1 && MailTo=="") 
  {
	FormatCheck=new Array(2); FormatCheck[0]=''; FormatCheck[1]=LinkToAdd;
  }

  else if (FormatCheck.length > 1) 
  {
	FormatCheck=new Array(2); FormatCheck[0]=''; FormatCheck[1]=LinkToAdd;
  }

  if (FormatCheck.length <= 1 && MailTo=="mailto:") 
  {
	FormatCheck[1]="";
  }
 
  //------- CLEAR ALL double spaces FROM THE FORM TEXT
  TempCurrentString = ReplaceChars(CurrentString,"  "," ");
  CurrentString = TempCurrentString;
  currentField.value = CurrentString;

  //------- FIND CURRENT SELECTION BY USER
  SelectedText  = document.selection.createRange().text;
  SelectedString = SelectedText.toString();
  if (SelectedString.length>0)
  {
	  //------- CLEAR TRAILING AND LEADING SPACES FROM SELECTION
	  while(SelectedString.substring((SelectedString.length-1),SelectedString.length)==" ")
	  {SelectedString = SelectedString.substring(0,(SelectedString.length-1));}
	  while(SelectedString.substring(0,1)==" ")
	  {SelectedString = SelectedString.substring(1,SelectedString.length);}

	  //------- FIND POSITION OF SELECTION WITHIN THE TEXT FOR THIS BLOCK
	  StartPos = CurrentString.indexOf(SelectedString).toString();
	  if(StartPos>(-1))
		{
		StringLength = SelectedString.length.toString();
		EndPos = (1*StartPos) + (1*StringLength);
		BeforeNewLink = CurrentString.substring(0,(StartPos))
		AfterNewLink  = CurrentString.substring(EndPos,CurrentString.length)
		LinkSyntax = '<a href="' + MailTo + FormatCheck[0] + FormatCheck[1] + '" target="_new">' + SelectedString + '</a>';
		NewContent = BeforeNewLink + LinkSyntax + AfterNewLink
		}
	  else
		{
		BeforeNewLink = CurrentString.substring(0,(StartPos-1))
		AfterNewLink  = CurrentString.substring(EndPos,CurrentString.length)
		LinkSyntax = '<a href="' + MailTo + FormatCheck[0] + FormatCheck[1] + '" target="_new">' + TextToAdd + '</a>';
		NewContent = CurrentString + "\n" + LinkSyntax
		}

	  CreateUndoState(CurrentString,true);
	  newcontent.value=NewContent;
	  xform.elements['link'].value="";
	  //xform.elements['linkb'].value="";
	  UpdateDisplay();
  }
  else{alert('Before adding this link, please select the text that you would like to make into a link by clicking dragging your mouse over the text of your choice in the right-side preview area.');}
}

//____________________________________________________________________________

function MaxChars(WhichField,MaxLength)
{
  TextValue = WhichField.value
  if (TextValue.length>MaxLength)
  {
    alert('You have entered more than ' + MaxLength + ' characters into the text field. This value will automatically be truncated to 90 characters.');
    WhichField.value = TextValue.substring(0,90);
  }
}

//____________________________________________________________________________

function previewcontent()
{
  SetFormFieldnames();
  CurrentText = newcontent.value;
  CurrentString = CurrentText.toString();  
  CreateUndoState(CurrentString,true);

  document.all['contentpreviewfield'].innerHTML='';
  document.all['contentpreviewfield'].innerHTML=newcontent.value;
}

//____________________________________________________________________________

function RedoToUndoState()
{
if(CurrentUndoState<MaxUndoState)
  {
  SetFormFieldnames();
  TextToRestore  = UndoState[CurrentUndoState+1];
  newcontent.value=TextToRestore;
  UpdateDisplay();

  if(CurrentUndoState<MaxUndoState){CurrentUndoState = CurrentUndoState + 1}
  }
}

//____________________________________________________________________________

function RemoveUnusedTags(Tag)
{
  SetFormFieldnames();
  TempContent = document.all('contentpreviewfield').innerHTML;
  NewContent = ReplaceChars(TempContent,"  "," ");
  NewContent = ReplaceChars(NewContent,"</"+ Tag +"></"+ Tag +">","</"+ Tag +">");
  NewContent = ReplaceChars(NewContent,"</"+ Tag +"> </"+ Tag +">","</"+ Tag +">");
  NewContent = ReplaceChars(NewContent,"<"+ Tag +"></"+ Tag +">"," ");
  NewContent = ReplaceChars(NewContent,"<"+ Tag +"> </"+ Tag +">"," ");
  NewContent = ReplaceChars(NewContent,"> <","><");
  NewContent = ReplaceChars(NewContent,"<div></div>"," ");
  NewContent = ReplaceChars(NewContent,"<DIV></DIV>"," ");
  NewContent = ReplaceChars(NewContent,"  "," ");
  NewContent = ReplaceChars(NewContent," .",".");
  NewContent = ReplaceChars(NewContent," ,",",");
  NewContent = ReplaceChars(NewContent," ;",";");
  NewContent = ReplaceChars(NewContent," :",":");
  NewContent = ReplaceChars(NewContent," \"","\"");
  NewContent = ReplaceChars(NewContent,"\" ","\"");
  newcontent.value = NewContent;
  document.all('contentpreviewfield').innerHTML = newcontent.value;
}


//____________________________________________________________________________

function ReplaceImageText(ObsoleteDiv)
{
  ObsoleteDivs = ObsoleteDiv.split(',');
  for (i=0;i<ObsoleteDivs.length;++i)
  {
    document.all('imageContent1_' + ObsoleteDivs[i]).innerHTML = '';
    document.all('imageContent2_' + ObsoleteDivs[i]).innerHTML = '';
    document.all('imageContent3_' + ObsoleteDivs[i]).innerHTML = '';
    document.all('imageContent4_' + ObsoleteDivs[i]).innerHTML = '';
    //document.all('imageContent5_' + ObsoleteDivs[i]).innerHTML = '';
  }
}

//____________________________________________________________________________

function ReturnToUndoState()
{
	if(CurrentUndoState>0)
	  {
	  SetFormFieldnames();
	  TextToRestore  = UndoState[CurrentUndoState-1];

	  CurrentTextValue = newcontent.value
	  CurrentText = CurrentTextValue.toString();
	  CreateUndoState(CurrentText,false) //------- FOR REDOING ACTION

	  newcontent.value=TextToRestore;
	  UpdateDisplay();

	  if(CurrentUndoState>0){CurrentUndoState = CurrentUndoState - 1}
	  }
}

//____________________________________________________________________________

function SetFormFieldnames()
{
  xform = document.forms['adminform'];
  categoryID = xform.elements['category_id'];
  subcategoryID = xform.elements['subcategory_id'];
  categoryname = xform.elements['category_name'];
  categorycode = xform.elements['category_code'];
  subcategoryname = xform.elements['subcategory_name'];
  subcategorycode = xform.elements['subcategory_code'];
  projectTitle = xform.elements['projects_title'];
  projectCode = xform.elements['projects_name'];
  navmenu = xform.elements['nav_id'];
  imagecount = new Array()
  imagecount[1] = xform.elements['imagecount1'];
  imagecount[2] = xform.elements['imagecount2'];
  imagecount[3] = xform.elements['imagecount3'];
  imagecount[4] = xform.elements['imagecount4'];
  title1 = xform.elements['title1'];
  if(xform.elements['projects_text']){newcontent = xform.elements['projects_text'];}
  else {newcontent = xform.elements['content1'];}
}

//____________________________________________________________________________

function SetPageHeader()
{
  if(title1.value=='')
  {
    title1.value=categoryname.value
  }
}

//____________________________________________________________________________

function SetSelection(CheckBoxCount,e)
{
StopLoop = false;
SetFormFieldnames();
if(document.forms.length>0)
  {
    if (navigator.appName=='Netscape'){ShiftDown = e.shiftKey; CntrlDown = e.ctrlKey;}
    else {ShiftDown = window.event.shiftKey; CntrlDown = window.event.ctrlKey;}
    ThisBox = CheckBoxCount;
    if(PreviousBox!='' && (ShiftDown || CntrlDown))
    {
      FirstBox = ThisBox;
      LastBox = PreviousBox;
      if(ThisBox>PreviousBox){FirstBox = PreviousBox; LastBox = ThisBox}
      for(i=FirstBox;i<LastBox;++i)
      {
        PreviousBoxChecked = xform.elements['deletelist'][PreviousBox].checked
        if (i!=ThisBox && i!=PreviousBox ){xform.elements['deletelist'][i].checked=!(xform.elements['deletelist'][i].checked);}
      }
    }

    PreviousBox = ThisBox;
  }
}

//____________________________________________________________________________

function settype(y)
{
  SetFormFieldnames();

  currentField = newcontent;

  CurrentText = currentField.value;
  OriginalString = CurrentText
  CurrentString = CurrentText.toString();
  
  CreateUndoState(CurrentString,true);

  //------- CLEAR ALL double spaces FROM THE FORM TEXT
  TempCurrentString = ReplaceChars(CurrentString,"  "," ");
  CurrentString = TempCurrentString;
  currentField.value = CurrentString;

  TempCurrentString = ReplaceChars(CurrentString," \r\n","\r\n");
  TempCurrentString = ReplaceChars(CurrentString,"\r\n","");
  TempCurrentString = ReplaceChars(CurrentString," \n","\n");
  TempCurrentString = ReplaceChars(CurrentString,"\n","");
  CurrentString = TempCurrentString;
  currentField.value = CurrentString;
  CurrentText = currentField.value;
  //------- FIND CURRENT SELECTION BY USER
  currentField.blur();
  SelectedHTML  = document.selection.createRange().htmlText;
  SelectedText = document.selection.createRange().text;

  if(SelectedText==CurrentText)
  {
    alert('Please select the text you wish to format from within the preview window instead of the source text editing window.');
  }
  else
  {

    SelectedHTMLText = SelectedHTML.toString();
    SelectedText = SelectedText.toString();

    if (SelectedText.substring((SelectedText.length-1),SelectedText.length)==" "){SelectedText = SelectedText.substring(0,(SelectedText.length-1))}

    if(SelectedHTMLText.indexOf('<'+ y +'>')>(-1))
    {
    document.selection.createRange().pasteHTML('</'+ y +'>' + SelectedText + '<' + y + '>')
    }
    else
    {
    NewHTMLText = '<' + y + '>' + SelectedText + '</' + y + '> ';
    document.selection.createRange().pasteHTML(NewHTMLText)
    }

    RemoveUnusedTags(y);

    UpdateTextarea();
    UpdateDisplay();
  }
}

//____________________________________________________________________________

function UpdateDisplay()
{
  SetFormFieldnames();

  TempContent = newcontent.value;

  //---SET CASE TO CAPITAL
  TempContent = ReplaceChars(TempContent,"<br>","<BR>");
  TempContent = ReplaceChars(TempContent,"<ul>","<UL>");
  TempContent = ReplaceChars(TempContent,"</ul>","</UL>");
  TempContent = ReplaceChars(TempContent,"<li>","<LI>");
  TempContent = ReplaceChars(TempContent,"</li>","</LI>");

  TempContent = ReplaceChars(TempContent,"  "," ");
  TempContent = ReplaceChars(TempContent," \r\n","\r\n");
  TempContent = ReplaceChars(TempContent,"\r\n ","\r\n");
  TempContent = ReplaceChars(TempContent," \n","\n");
  TempContent = ReplaceChars(TempContent,"\n ","\n");

  TempContent = ReplaceChars(TempContent,"\r\n<LI>","<LI>");
  TempContent = ReplaceChars(TempContent,"<LI>\r\n","<LI>");
  TempContent = ReplaceChars(TempContent,"\n<LI>","<LI>");
  TempContent = ReplaceChars(TempContent,"<LI>\n","<LI>");

  TempContent = ReplaceChars(TempContent,"\r\n","<BR>");
  TempContent = ReplaceChars(TempContent,"\n","<BR>");

  TempContent = ReplaceChars(TempContent," <BR>","<BR>");
  TempContent = ReplaceChars(TempContent,"<BR> ","<BR>");
  TempContent = ReplaceChars(TempContent,"<BR>\r\n","<BR>");
  TempContent = ReplaceChars(TempContent,"\n<BR>","<BR>");


  TempContent = ReplaceChars(TempContent,"<LI> ","<LI>");
  TempContent = ReplaceChars(TempContent," <LI>","<LI>");
  TempContent = ReplaceChars(TempContent,"</LI> ","</LI>");
  TempContent = ReplaceChars(TempContent," </LI>","</LI>");
  TempContent = ReplaceChars(TempContent,"<LI><BR>","<LI>");
  TempContent = ReplaceChars(TempContent,"</LI><BR>","</LI>");


  TempContent = ReplaceChars(TempContent,"<BR><UL>","<UL>");
  TempContent = ReplaceChars(TempContent,"<BR></UL>","</UL>");
  TempContent = ReplaceChars(TempContent,"<UL><BR>","<UL>");
  TempContent = ReplaceChars(TempContent,"</UL><BR>","</UL>");
  TempContent = ReplaceChars(TempContent,"\r\n","");
  TempContent = ReplaceChars(TempContent,"\n","");

  NewContent = TempContent 
  newcontent.value = NewContent
  document.all('contentpreviewfield').innerHTML = NewContent;
}

//____________________________________________________________________________

function UpdateNavDisplay()
{
  SetFormFieldnames();
  SelectedNav = navmenu.selectedIndex
  WhichNavID = navmenu.options[SelectedNav].value
  NavHTML = NavArray[WhichNavID]
  NavHTML = unescape(NavHTML);
  NavHTML = unescape(NavHTML);
  document.all['bottomnavdisplay'].innerHTML='';
  document.all['bottomnavdisplay'].innerHTML=NavHTML;
}

//____________________________________________________________________________

function UpdateOptions()
{
SetFormFieldnames();
CancelBlink=true;
categoryname.value='';
categorycode.value='';
subcategoryname.value='';
subcategorycode.value='';

WhichOption = categoryID.selectedIndex;
WhichArray = categoryID.options[WhichOption].value;

CurrentNumberOfOptions = subcategoryID.options.length;
NumberOfNewOptions = MainCatArray[WhichArray].length;

clearSubCatMenu();

for(i=0; i<NumberOfNewOptions; ++i)
  {
    ThisOptionText = MainCatArray[WhichArray][i][0];
    ThisOptionValue = MainCatArray[WhichArray][i][1];
    ThisNewOption = new Option(ThisOptionText,ThisOptionValue,true,true);
    subcategoryID.options[i] = ThisNewOption
  }
}

//____________________________________________________________________________

function UpdateTextarea(Block)
{
  SetFormFieldnames();
  TempContent = document.all('contentpreviewfield').innerHTML;
  NewSource = ReplaceChars(TempContent," \r\n","\r\n");
  NewSource = ReplaceChars(TempContent,"\r\n ","\r\n");
  NewSource = ReplaceChars(TempContent,"\r\n"," ");
  NewSource = ReplaceChars(TempContent,"  "," ");
  newcontent.value = NewSource;
}

//____________________________________________________________________________

function WriteAllLayers()
{
  LayerNames = new Array('aboutus','services','hud','multifamily','commercial','assistedliving','adaptive','current','careers','contactus')
  LayerPositions = new Array('110','146','186','182','235','277','307','337','360','350')

  //------- Dimensions are required if different from the standard dimensions. (top;left;width;height)

  LayerFormat = '<div id="sLayerName" style="position:absolute; visibility:hidden; top:PosTop; left:156; width:98; height:74;">';
  CloseLayer = '</div>';
  if (NN4)
  {  
    LayerFormat = '<layer name="sLayerName" visibility=hidden top=PosTop left=160 width=94 height=70 onmouseout="ClearLayers();">'
    CloseLayer = '</layer>';
  }

  //-------Layer 1 has different dimensions, so write it out seperately
  ThisLayerString = LayerFormat;
  ThisLayerString = ReplaceChars(ThisLayerString,'"sLayerName"','layer1');
  ThisLayerString = ReplaceChars(ThisLayerString,'top:PosTop; left:156; width:98; height:74;','top:110; left:156; width:98; height:74;');
  ThisLayerString = ReplaceChars(ThisLayerString,'top=PosTop left=160 width=94 height=70','top=110 left=160 width=94 height=70');

  document.writeln(ThisLayerString + '\n');
  document.writeln('<div align="right">');
  document.writeln('<img src="<%=dirPath%>images/mouseover_aboutus.jpg" usemap="#mouseover_aboutus" border="0">');
  document.writeln('</div>')
  document.writeln(CloseLayer + '\n');

  //------- WRITE OUT ALL LAYERS WITH THE SAME MEASUREMENTS
  for(CurrentLayer=1;CurrentLayer<TotalLayers;++CurrentLayer)
  {
    ThisLayerString = LayerFormat;
    ThisLayer = CurrentLayer + 1;
    ThisLayerString = ReplaceChars(ThisLayerString,'sLayerName','layer' + ThisLayer);
    ThisLayerString = ReplaceChars(ThisLayerString,'PosTop',LayerPositions[CurrentLayer]);

    document.writeln(ThisLayerString + '\n');
    document.writeln('<div align="right">');
    document.writeln('<img src="<%=dirPath%>images/mouseover_'+ LayerNames[CurrentLayer] +'.jpg" usemap="#mouseover_'+ LayerNames[CurrentLayer] +'" border="0">');
    document.writeln('</div>')
    document.writeln(CloseLayer + '\n');

  }
}

//WriteAllLayers();