// Copyright (c) 2002-2003 by SAS Institute Inc., Cary, NC 27513 USA. All Rights Reserved.
// Copyright (c) 2003 by SAS Institute Inc., Cary, NC 27513 -->
//
// Portal UI javascript functions
// @version 1.0

function isInternetExplorer() {
   var appVer = navigator.appVersion.toLowerCase();
   return (appVer.indexOf('msie') > -1);
      
}

// disable button if no check box items are selected 
function chkDisableButtonStatus(noOfElems,disableButton,disableButtonId) 
{
   var topBoxElem = document.getElementById(disableButtonId);
   var noneChecked = true;

   if (topBoxElem != null && noOfElems > 0) {
      for (var k=1; k<=noOfElems; k++) {
          var chkboxname = "chkBoxName" + k;
          var chkboxelem = document.getElementById(chkboxname);

          if (chkboxelem != null && chkboxelem.checked) {
             noneChecked = false;
          }
      } 	

   }

   var disableButtonCheck = document.getElementById(disableButton);
   
   if (disableButtonCheck != null ) {
       if (noneChecked) {
          disableButtonCheck.disabled = true;
       } else {
          disableButtonCheck.disabled = false;
       }
   }
}

function calcOptionsBoxSize(iframeWindow)
{
  var optionsWindow = iframeWindow.document.getElementById("optionsWindow");
  var menuHeight = optionsWindow.offsetHeight;
  var menuWidth = optionsWindow.offsetWidth;

  document.getElementById('optionsBox').style.height=menuHeight;
  document.getElementById('optionsBox').style.width=menuWidth;
}

function resizeViewerFrame()
{
    var bannerSpan = document.getElementById("banner");
    var bannerSpanHeight = bannerSpan.offsetHeight;
    parent.document.body.rows = bannerSpanHeight + ',*';
}

function resizeBannerWidth()
{
    var bannerSpan = document.getElementById("banner");
    var mainContentSpan = document.getElementById("mainContent");
    if (mainContentSpan != null && bannerSpan != null)
    {
        bannerSpan.style.width = mainContentSpan.offsetWidth;
    }
}

function moveContent()
{
    var contentSpan = document.getElementById("mainContent");
    if (contentSpan != null)
    {
        var bannerSpan = document.getElementById("banner");
        var tabSpan = document.getElementById("portalTabArea");
        var tabSpanHeight = 0;
        if (tabSpan != null)
        {
            tabSpanHeight = tabSpan.offsetHeight;
        }
        var bannerSpanHeight = bannerSpan.offsetHeight;
        contentSpan.style.top = bannerSpanHeight - tabSpanHeight;
        contentSpan.style.visibility = 'visible';
    }
}

function moveOptionsMenu()
{
    var optionsDiv = document.getElementById("optionsBox");
    var optionsAnchor = document.getElementById("optionsAnchor");
    if ((optionsDiv != null) && (optionsAnchor != null))
    {
        var optionsAnchor = document.getElementById("optionsAnchor");
        var left = optionsAnchor.offsetLeft;
        var top = optionsAnchor.offsetTop;
        var bottom = top + optionsAnchor.offsetHeight;
        optionsDiv.style.top = bottom;
        optionsDiv.style.left = left;
    }
}

function moveHelpMenu()
{
    var helpDiv = document.getElementById("helpLinks");
    var helpAnchor = document.getElementById("helpAnchor");

    if ((helpDiv != null) && (helpAnchor != null))
    {

        var right = helpAnchor.offsetWidth;
        var left = helpAnchor.offsetLeft;

        var top = helpAnchor.offsetTop;
        var bottom = top + helpAnchor.offsetHeight;
        helpDiv.style.top = bottom;
        helpDiv.style.left = left + right - helpDiv.offsetWidth;
    }
}
function moveSearchResultsActionsLinks()
{
    var moveSearchResultsActionsDiv = document.getElementById("searchResultsActionsLinks");
    if (moveSearchResultsActionsDiv != null)
    {
        var actionsAnchor = document.getElementById("actionsAnchor");

        if (actionsAnchor != null) {
	    var left = actionsAnchor.offsetLeft;
            var top = actionsAnchor.offsetTop;
            var bottom = top + actionsAnchor.offsetHeight - 1;
            moveSearchResultsActionsDiv.style.top = bottom;
            moveSearchResultsActionsDiv.style.left = left + 2;
        }
   }
}


function moveSearchResultsItemActionsLinks(itemActionDivId, itemActionAchorId)
{
    var moveSearchResultsItemActionsDiv = document.getElementById(itemActionDivId);
    if (moveSearchResultsItemActionsDiv != null)
    {
        var actionsAnchor = document.getElementById(itemActionAchorId);

        if (actionsAnchor != null) {
	    var left = actionsAnchor.offsetLeft;
            var top = actionsAnchor.offsetTop;
            var bottom = top + actionsAnchor.offsetHeight - 1;
            moveSearchResultsItemActionsDiv.style.top = bottom;
            moveSearchResultsItemActionsDiv.style.left = left + 2;
        }
   }
}


//
// Select or unselect all values in all the select lists
//
function moveListContents(fboxName, tboxName)
{
      var selBoxMoveFrom = document.getElementById(fboxName);
      var selBoxMoveTo = document.getElementById(tboxName);
      var i = 0;
      while (selBoxMoveFrom.options.length != 0) {
            var thisItem = selBoxMoveFrom.options[i];
            selBoxMoveTo.options[selBoxMoveTo.length] = new Option( thisItem.text, thisItem.value );
            selBoxMoveFrom.options[i] = null;
      }
      pageChanged(true);
}


function moveControls()
{
    moveContent();
    moveOptionsMenu();
    moveHelpMenu();
    moveSearchResultsActionsLinks();

    if (window.event != null) {
	    if (window.event.type != "resize") {
		document.onclick = hideMenus;
	    }
    }
    else {
	document.onclick = hideMenus;
    }
}

function hideOptions()
{
    var optionsDiv = document.getElementById("optionsBox");
    if (optionsDiv != null)
    {
        optionsDiv.style.visibility = 'hidden';
    }
}

function hideMenus(clickEvent)
{
    var target;
    if (!clickEvent) var clickEvent = window.event;
    if (clickEvent.target) target = clickEvent.target;
    else if (clickEvent.srcElement) target = clickEvent.srcElement;
    if (target.id != "optionsAnchor") {
        hideOptions();
    }
    if (target.id != "helpAnchor") {
        hideHelp();
    }
}

function hideHelp()
{
    var helpDiv = document.getElementById("helpLinks");
    helpDiv.style.visibility = 'hidden';
}

    
function toggleMenu()
{
    var optionsDiv = document.getElementById("optionsBox");
    var visibility = optionsDiv.style.visibility;
    if (visibility == 'visible')
    {
        optionsDiv.style.visibility = 'hidden';
    }
    else
    {
        optionsDiv.style.visibility = 'visible';
    }
}

function toggleHelp()
{
    var helpDiv = document.getElementById("helpLinks");
    var visibility = helpDiv.style.visibility;
    if (visibility == 'visible')
    {
        helpDiv.style.visibility = 'hidden';
    }
    else
    {
        helpDiv.style.visibility = 'visible';
    }
}

function hideSearchResultsActionsLinks()
{
    var searchResultsActionsDiv = document.getElementById("searchResultsActionsLinks");
    var popuptimeelem = document.getElementById("popuptime");
    var d = new Date();
    if (d.getTime() - popuptimeelem.value > 500)
        searchResultsActionsDiv.style.visibility = 'hidden';

}



function hideSearchResultsItemActionsLinks()
{
    var popuptimeelem = document.getElementById("popuptime");
    var lastDisplayedElem = document.getElementById("lastDisplayed");
    searchResultsItemActionsDivId = lastDisplayedElem.value;
    if (searchResultsItemActionsDivId != null && searchResultsItemActionsDivId.length > 0)
    {
        var searchResultsItemActionsDiv = document.getElementById(searchResultsItemActionsDivId);
        var popuptimeelem = document.getElementById("popuptime");
        var d = new Date();
        if (d.getTime() - popuptimeelem.value > 500)
            searchResultsItemActionsDiv.style.display = 'none';
    }    

    // Hide all other item action divs
    for (var i=0; i<50; i++)
    {
        var divid = "portalItemActionDiv_" + i;
        if (divid != searchResultsItemActionsDivId)
        {
           var divElem = document.getElementById(divid);
           if (divElem != null)
             divElem.style.display = 'none';  
        }
    }
}


function toggleSearchResultsItemActionsLinks(itemDivId, itemAchorId)
{

    moveSearchResultsItemActionsLinks(itemDivId, itemAchorId);
    var searchResultsItemActionsDiv = document.getElementById(itemDivId);
    var lastDisplayedElem = document.getElementById("lastDisplayed");
    var popuptimeelem = document.getElementById("popuptime");

    if (searchResultsItemActionsDiv != null) {

        var visibility = searchResultsItemActionsDiv.style.display;

        if (visibility == 'none')
        {
            searchResultsItemActionsDiv.style.display = 'block';
            var d = new Date();
            popuptimeelem.value = d.getTime();
            if (lastDisplayedElem != null)
                lastDisplayedElem.value = itemDivId;
        }
        else
        {
            searchResultsItemActionsDiv.style.display = 'none';
            if (lastDisplayedElem != null)
                lastDisplayedElem.value = "";
        }
    }    
    var searchResultsActionsDiv = document.getElementById("searchResultsActionsLinks");
    searchResultsActionsDiv.style.visibility = 'hidden';
}

function toggleSearchResultsActionsLinks()
{
    moveSearchResultsActionsLinks();
    var searchResultsActionsDiv = document.getElementById("searchResultsActionsLinks");
    if (searchResultsActionsDiv != null) {
        var popuptimeelem = document.getElementById("popuptime");
        var visibility = searchResultsActionsDiv.style.visibility;
        if (visibility == 'visible')
        {
            searchResultsActionsDiv.style.visibility = 'hidden';
        }
        else
        {
            searchResultsActionsDiv.style.visibility = 'visible';
            var d = new Date();
            popuptimeelem.value = d.getTime();
        }
    }    
    // Hide all other item action divs
    for (var i=0; i<50; i++)
    {
        var divid = "portalItemActionDiv_" + i;
        var divElem = document.getElementById(divid);
        if (divElem != null)
          divElem.style.display = 'none';  
    }
}

function toggleDiv(name) {
    var element = document.getElementById(name);
	element.style.display = (element.style.display == "none" ) ? "block" : "none";
}

// utility function for hiding a DIV element
function hideDiv(name)
{
    var element = document.getElementById(name);
    if (element)
        element.style.display = "none";
}


// utility function for showing a DIV element
function showDiv(name)
{
    var element = document.getElementById(name);
    if (element)
        element.style.display = "block";
}


// call when page is loaded, to render minimized portlets
function renderPortlets()
{
    for (var i = 0; i < parent.minimizedPortlets.length; i++)
    {
        hideDiv(parent.minimizedPortlets[i]);
        var min = "min"+parent.minimizedPortlets[i];
        var max = "max"+parent.minimizedPortlets[i];
        hideDiv(min);
        showDiv(max);
    }
}


// call when page is loaded, to render maximized collections
function renderCollections()
{
         for (var i = 0; i < parent.maximizedCollections.length; i++)
        {
            showDiv(parent.maximizedCollections[i]);
            var min = "min"+parent.maximizedCollections[i];
            var max = "max"+parent.maximizedCollections[i];
            hideDiv(min);
            showDiv(max);
        }
}


// call to minimize or maximize a Portlet
function togglePortlet(name)
{
    var index = -1;

    // first, find if the portlet is already minimized
    for (var i = 0; ((i < parent.minimizedPortlets.length) && (index == -1)); i++)
    {
        if (parent.minimizedPortlets[i] == name)
        {
            index = i;
        }
    }

    // if it is, remove it from the array and show it
    if (index != -1)
    {
        var arrayCopy = new Array(parent.minimizedPortlets.length - 1);
        var j = 0;
        for (var i = 0; i < arrayCopy.length; i++)
        {
            if (i != index)
            {
                arrayCopy[j++] = parent.minimizedPortlets[i];
            }
        }
        parent.minimizedPortlets = arrayCopy;
        showDiv(name);
        var max="max"+name;
        var min="min"+name;
        showDiv(min);
        hideDiv(max);
    }
    else
    // if it is not, add it to the array and hide it
    {
        parent.minimizedPortlets[parent.minimizedPortlets.length] = name;
        hideDiv(name);
        var max="max"+name;
        var min="min"+name;
        showDiv(max);
        hideDiv(min);
    }
    return false;
}

// call to minimize or maximize a Collection
function toggleCollection(name)
{
    var index = -1;

        for (var i = 0; ((i < parent.maximizedCollections.length) && (index == -1)); i++)
        {
            if (parent.maximizedCollections[i] == name)
            {
                index = i;
            }
        }

    // if it is, remove it from the array and hide it
    if (index != -1)
    {

        var j = 0;
            var arrayCopy = new Array(parent.maximizedCollections.length - 1);
            for (var i = 0; i < arrayCopy.length; i++)
            {
                if (i != index)
                {
                    arrayCopy[j++] = parent.maximizedCollections[i];
                }
            }
            parent.maximizedCollections = arrayCopy;
        hideDiv(name);
        var max="max"+name;
        var min="min"+name;
        hideDiv(min);
        showDiv(max);
    }
    else
    // if it is not, add it to the array and show it
    {
            parent.maximizedCollections[parent.maximizedCollections.length] = name;
        showDiv(name);
        var max="max"+name;
        var min="min"+name;
        hideDiv(max);
        showDiv(min);
    }
}


// call this function when the user clicks a link, to make the options disappear or show
function toggleOptions()
{
    parent.isOptionsVisible = !parent.isOptionsVisible;
    renderOptions();
}


// call this function when the page is loaded. It will render the options box
// depending on if it is visible or hidden
function renderOptions()
{
    if (parent.isOptionsVisible)
    {
        hideDiv("showOptionsText");
        showDiv("hideOptionsText");
        showDiv("optionsBox");
    }
    else
    {
        showDiv("showOptionsText");
        hideDiv("hideOptionsText");
        hideDiv("optionsBox");
    }
}


function submitThisForm(thisForm)
{
    var aForm = thisForm;
    if (navigator.appName.indexOf("Netscape") != -1)
        document.aForm.submit();
}


// used for validating a new user's registration. It makes sure they have a valid password
function validatePassword(pw0, pw1, pw2, oldmissingtxt, newmissingtxt, reenterpasswordtxt, invalidpasswordtxt)
{
        var oldpw = document.getElementById(pw0);
        var newpw = document.getElementById(pw1);
        var reenterpw = document.getElementById(pw2);
        var validated = true;
	
        if ( (oldpw.value.length > 0) || (newpw.value.length > 0) || (reenterpw.value.length > 0) )
        {
            if (oldpw.value.length <= 0)
            {
                alert(oldmissingtxt);
                validated=false;
            }
             else if (newpw.value.length <= 0)
            {
                alert(newmissingtxt);
                validated=false;
            }
            else if (reenterpw.value.length <= 0)
            {
                alert(reenterpasswordtxt);
                validated=false;
            }
        }
         return validated;
}

//
// Rearrange the items in a SELECT LIST by moving it up or down
//
function changeOrder(selectBoxName, moveUp)
{
  var selectBox = document.getElementById(selectBoxName);
  var srcText = srcValue = destText = destValue = "";
  var srcIndex = destIndex = 0;
  srcIndex = selectBox.selectedIndex;

   if (srcIndex >= 0)
   {
       pageChanged(true);

       switch (moveUp)
      {
           case false:
              increment = 1
              if (srcIndex +1 == selectBox.length)
                 return;
              destIndex = srcIndex + increment;
              break;
         case true:
            increment = -1
            if (srcIndex < 1) return;
                destIndex = srcIndex + increment;
            break;
       }

      with (selectBox)
      {
        srcText  = options[srcIndex].text;
        srcValue = options[srcIndex].value;

        destText  = options[destIndex].text;
        destValue = options[destIndex].value;

        options[destIndex].text = srcText;
        options[destIndex].value = srcValue;

        options[srcIndex].text = destText;
        options[srcIndex].value = destValue;

        selectedIndex = destIndex;
       }
   }
   else
       pageChanged(false);


}


//
// Delete an item from a SELECT LIST.
//
function deleteOption(selectBoxName, removeConfirmMsg)
{
  var retval = confirm(removeConfirmMsg);
  if (!retval) {
     pageChanged (false);
  } else {
     var selectBox = document.getElementById(selectBoxName);
     if (selectBox != null && selectBox.options.length > 0) {
         for (var i = 0; i < selectBox.length; i++) {
             if (selectBox.options[i].selected) {
                 selectBox.options[i]=null;
                 pageChanged(true);
                 i--;
             }
         }
     }
  }  
  return retval;
}


//
// Concatenate the items in a SELECT LIST and store it in a hidden field
// which will be processed when the form is submitted.
//
function doSelect(selectBoxId, inputBoxId)
{
   var selectBox = document.getElementById(selectBoxId);
   var inputBox = document.getElementById(inputBoxId);

    if (selectBox.options.length > 0)
    {
          for (var i = 0; i < selectBox.length; i++)
          {
// We do not have entity ID's yet, so we use the text and not the value for now
              if ( inputBox.value == "")
    	      inputBox.value = selectBox.options[i].text;
              else
                     inputBox.value = inputBox.value + "," + selectBox.options[i].text;
           }
     }
}


// Function to move a selected value from one SELECT LIST to another
function selSwitch(fboxName,tboxName)
{
   var i= 0;
   var fbox = document.getElementById(fboxName);
   var tbox = document.getElementById(tboxName);
   srcIndex = fbox.selectedIndex;
   if (srcIndex >= 0)
   {
       with (fbox)
       {
             for (i = 0; i < length; i++)
             {
                   with (options[i])
                   {
                      if (options[i].selected)
                      {
                            tbox.options[tbox.length] = new Option( text, value );
                            options[i] = null;
                            i--;
                      }
                   }
             } // end for loop
       }
       pageChanged(true);
   }
   else
      pageChanged(false);

}


// Function to check for empty fields and alert the user to enter a value
function validate(object,text)
{
    if (object.value.length > 0)
        return true;
    else
    {
        alert(""+text);
        if (navigator.appName.indexOf('Netscape') > -1)
       {
            object.focus();
        }
        return false;
    }
}


function formvalidate(fieldArray, errorArray)
{
    var validated = true;
    if (fieldArray!= null && fieldArray.length > 0) {
        for (var i = 0; i < fieldArray.length; i++) {
            if (i < errorArray.length) {
   	        var fieldName = document.getElementById(fieldArray[i]);
   	        if (fieldName != null) {
                    if (!validate(fieldName,errorArray[i]))
                        validated = false;
                }        
            }
        }
    }    
    return validated;
}


function changeColLayout(tbox, fbox1, fbox2, columnField, columnNumber)
{
   var i= 0;

   if (fbox1 != null)
   {
        with (fbox1)
       {
             while (length != 0)
            {
	  i=0;
                 with (options[i])
                {
                      options[i].selected = true;
	                  selSwitch(fbox1,tbox);
                 }
             }
        }
   }
   if (fbox2 != null)
   {
       with (fbox2)
       {
             while (length != 0)
            {
	  i=0;
                 with (options[i])
                 {
                      options[i].selected = true;
                      selSwitch(fbox2,tbox);
                  }
             }
        }
    }
    columnField.value = columnNumber;

}


function toggleColumns(radioBox,colNumber)
{
	if (colNumber == 1)
    {
	    hideDiv("rightImage1");
	    hideDiv("rightImage2");
	    hideDiv("rightImage3");
	    hideDiv("leftImage1");
	    hideDiv("leftImage2");
	    hideDiv("leftImage3");
	    showDiv("portletBox1");
	    hideDiv("portletBox2");
	    hideDiv("portletBox3");
    }
	else if (colNumber == 2)
    {
	    showDiv("rightImage1");
	    hideDiv("rightImage2");
	    hideDiv("rightImage3");
	    showDiv("leftImage1");
	    hideDiv("leftImage2");
	    hideDiv("leftImage3");
	    showDiv("portletBox1");
	    showDiv("portletBox2");
	    hideDiv("portletBox3");
    }
	else if (colNumber == 3)
    {
	    showDiv("rightImage1");
	    showDiv("rightImage2");
	    hideDiv("rightImage3");
	    showDiv("leftImage1");
	    showDiv("leftImage2");
	    hideDiv("leftImage3");
	    showDiv("portletBox1");
	    showDiv("portletBox2");
	    showDiv("portletBox3");
    }
}

//
// Check or uncheck all the checkboxes in advanced search
//
function quickCheck(name, total, checkAll)
{

    if (checkAll)
    {
          for (var i = 1; i <= total; i++)
          {
                chkBox = document.getElementById(name + i);
	 chkBox.checked = true;
          }
    }
    else
    {
          for (var i = 1; i <= total; i++)
          {
                chkBox = document.getElementById(name + i);
	 chkBox.checked = false;
          }
    }
}


//
// Select or unselect all values in all the select lists
//
function quickSelect(name, total, selectAll)
{
    if (selectAll)
    {
        for (var i = 1; i <= total; i++)
        {
            selBox = document.getElementById(name + i);
            selectAllOptions(selBox);
        }
    } else {
        for (var i = 1; i <= total; i++)
        {
            selBox = document.getElementById(name + i);
            unSelectAllOptions(selBox);
        }
    }
}

//
// Select or values in a select list
//
function selectAllOptions(selBox)
{
     
    if (selBox != null && selBox.options.length > 0)
    {
        for (var j = 0; j < selBox.options.length; j++)
        {
            selBox.options[j].selected = true;
        }
    }
}

function selectAllItems(name)
{
     selBox = document.getElementById(name);
     selectAllOptions(selBox);
}

//
// UnSelect all values in a select list
//
function unSelectAllOptions(selBox)
{
    if (selBox != null && selBox.options.length > 0)
    {
        for (var j = 0; j < selBox.options.length; j++)
        {
            selBox.options[j].selected = false;
        }
    }
}


// call this function when the search page is loaded. It will render the advanced options box
// depending on if it is visible or hidden
function renderAdvancedSearch()
{
    if (parent.isAdvancedSearchVisible)
    {
        hideDiv("showAdvancedText");
        showDiv("hideAdvancedText");
        showDiv("advanced");
        showDiv("advancedButtons");
    }
    else
    {
        showDiv("showAdvancedText");
        hideDiv("hideAdvancedText");
        hideDiv("advanced");
        hideDiv("advancedButtons");
    }
}

// call this function when the user clicks a link, to make the options disappear or show
function toggleAdvancedSearch()
{
    parent.isAdvancedSearchVisible = !parent.isAdvancedSearchVisible;
    renderAdvancedSearch();
}

// call this function when the user clicks a link or button after changes were made to the Page
function checkForPageChanges(errorTxt)
{
    if (parent.isPageChanged)
    {
        alert(""+errorTxt);
        return false;
    }
    else
        return true;
}

// call this function when a from field changed when Editing a Page, or when Saving or Cancelling
function pageChanged(isChanged)
{
    parent.isPageChanged = isChanged;
}


// call this function when the user clicks the Edit Portlet image on a Page
function editPortlet(columnName, editPortletLocation, errorTxt, selErrorTxt)
{
   var i= 0;
   var selBox = document.getElementById(columnName);
    if (selBox.options.length > 0)
     {
          for (var j = 0; j < selBox.options.length; j++)
         {
	if (selBox.options[j].selected)
	 i++;
          }
     }

    if (parent.isPageChanged)
    {
        alert(""+errorTxt);
    }
    else if (i > 1)
    {
        alert(""+selErrorTxt);
    }
    else
    {
        var srcIndex = selBox.selectedIndex;
        if (srcIndex >= 0)
        {
            var newEditPortletLocation = editPortletLocation + "&com.sas.portal.ItemId=" + selBox.options[srcIndex].value;
            location.href=newEditPortletLocation;
         }
    }
}

//
// Add values of selected items to url in Bookmarks portlet
//
function bookmarksSelect(name, total, bookmarkUrl, errormsg, removeBookmarks)
{
     var validated = false;
      for (var i = 1; i <= total; i++)
      {
            chkBox = document.getElementById(name + i);
            if (chkBox.checked)
            {
                 validated = true;
                 break;
             }
      }

     if (validated == true)
    {
          for (var i = 1; i <= total; i++)
          {
                chkBox = document.getElementById(name + i);
                if ( removeBookmarks )
                {
                    if (chkBox.checked == false)
                    {
                         bookmarkUrl = bookmarkUrl + "&Items=" + chkBox.value
                     }
                 }
                 else
                {
                    if (chkBox.checked == true)
                    {
                         bookmarkUrl = bookmarkUrl + "&Items=" + chkBox.value
                     }
                 }
          }
          if ( removeBookmarks )
            location.href=bookmarkUrl;
          else
            top.frames[1].location.href=bookmarkUrl;
      }
      else
      {
           alert(""+errormsg);
       }
      return validated;
}



function publishformvalidate(fieldArray, errorArray, channelBox, errmsg)
{
    var validated = true;
   var selBox = document.getElementById(channelBox);

    for (var i = 0; i < fieldArray.length; i++)
   {
        if (i < errorArray.length)
        {
   	var fieldName = document.getElementById(fieldArray[i]);
	if (!validate(fieldName,errorArray[i]))
                    validated = false;
        }
    }

    if (validated == true)
    {
        if (selBox.selectedIndex > 0)
        {
              alert(""+errmsg);
              validated = false;
        }
    }
    return validated;
}

//
// Sets the correct URL for editing an item.
//
function editItem(selectBoxName, editUrl, nothingselected, toomanyselected)
{
  var selectBox = document.getElementById(selectBoxName);
  var i= 0;
  if (selectBox.options.length > 0)
  {
      for (var j = 0; j < selectBox.options.length; j++)
      {
          if (selectBox.options[j].selected)
	      i++;
       }
   }

   if (i > 1)
   {
       alert(""+toomanyselected);
   }
   else
   {
       srcIndex = selectBox.selectedIndex;
       if (srcIndex >= 0)
       {
           editUrl = editUrl + "?com.sas.portal.ItemId=" + selectBox.options[srcIndex].value;
           submitDisableAllForms();
          location.href=editUrl;
       }
       else
           alert(""+nothingselected);
    }
}


// utility function to set focus on a form field
function setFocus(name)
{
    var element = document.getElementById(name);
    if (element != null) {
        if (!element.disabled)
            element.focus();
    }        
}

function cancelForm(btn, cancelUrl)
{
    btn.form.action = cancelUrl;
    return true;
}

function setAction(id, portletaction)
{
   var elem;
    elem = document.getElementById(id);
    elem.value = portletaction;	
}

function checkMe(name, total, topBoxId)
{
   var elem;
   elem = document.getElementById(topBoxId);
   if (elem != null && total > 0) {
       var isChecked = elem.checked;

       if (isChecked) {
           for (var i = 1; i <= total; i++) {
               chkBox = document.getElementById(name + i);
   	       chkBox.checked = true;
           }
        } else {
           for (var i = 1; i <= total; i++) {
               chkBox = document.getElementById(name + i);
	       chkBox.checked = false;
           }
        }
   }     
}


function isChannelSelected(channelBox, errmsg)
{
    var validated = true;
   var selBox = document.getElementById(channelBox);

   if (selBox.selectedIndex < 0)
   {
       alert(""+errmsg);
       validated = false;
   }
   else
        validated = true;

    return validated;
}

function submitDisableAllForms() {
    setTimeout("disableAllForms()", 100);
    return true;
}

function disableAllForms() {
    // Loop through each form on this page
    for (var i = 0; i < document.forms.length; i++) {
        // Loop through each element in the form
        var form = document.forms[i];
        for (var j = 0; j < form.length; j++) {
            // Disable each element
            var element = form.elements[j];
            element.disabled = true;
        }
    }
}

function submitDisableAllAnchors(callCount) {
    if (callCount == 0) {
        //setTimeout("disableAllAnchors()", 100);
        disableAllAnchors();
        return true;
    } else {
        return false;
    }
}

function disableAllAnchors() {
    // Loop through each anchor on this page
    for (var i = 0; i < document.links.length; i++) {
        // Disable each element
        var anchor = document.links[i];
        anchor.disabled = true;
    }
}


//*********************************
//
// Functions for Portal Search Page
//
//*********************************

function getContentTypeChkBoxes() {
    values = "";
    var i=1;
    while (true) { 
        id = "item" + i;
        var element = document.getElementById(id);
        if (element == null)
           break;
        if (i==1)
           values = values + element.checked;
        else
           values = values + "," + element.checked;
        i++;
    }
    var element2 = document.getElementById("selectedItemString");
    element2.value=values;
}

function toggleTopChkBox(itemname,total, topBoxId) {
   var elem;
   elem = document.getElementById(topBoxId);
   checkTopBox = true;
 
   if (elem != null && total > 0) {
       for (var i = 1; i <= total; i++) {
           chkBox = document.getElementById(itemname + i);
           if (!chkBox.checked) {
               checkTopBox = false;
               break;
           }
       }

       if (checkTopBox)
          elem.checked = true;
       else
          elem.checked = false;
   }        

}

function submitSort(btn, sortWhat, submitUrl) {
    var element = document.getElementById("sortBy");
    element.value=sortWhat;	
    element.form.action = submitUrl;
    element.form.submit();
}

function toggleSearchOptionsDiv() {
    var searchOptions = document.getElementById("searchOptions");
    var optionsDisplayStyle = document.getElementById("optionsDisplayStyle");
    var collapseImageDiv = document.getElementById("collapseSidebar");
    var expandImageDiv = document.getElementById("expandSidebar");
    var visibility = searchOptions.style.display;
    if (visibility == '') {
        searchOptions.style.display = 'none';
        optionsDisplayStyle.value = 'none';
        collapseImageDiv.style.display = '';
        expandImageDiv.style.display = 'none';
    } else {
        searchOptions.style.display = '';
        optionsDisplayStyle.value = '';
        collapseImageDiv.style.display = 'none';
        expandImageDiv.style.display = '';
    }
}

function setSearchOptionsDisplayStyle() {
    var searchOptions = document.getElementById("searchOptions");
    var optionsDisplayStyle = document.getElementById("optionsDisplayStyle");
    var visibility = optionsDisplayStyle.value;
    searchOptions.style.display = visibility;
    var collapseImageDiv = document.getElementById("collapseSidebar");
    var expandImageDiv = document.getElementById("expandSidebar");
    if (visibility == '') {
        collapseImageDiv.style.display = 'none';
        expandImageDiv.style.display = '';
    } else {
        collapseImageDiv.style.display = '';
        expandImageDiv.style.display = 'none';
    }
}


function submitChangeRows() {
    var element = document.getElementById("changeRows");
    submitForm(element);
}

function submitBookmark() {
    var element = document.getElementById("bookmark");
    submitForm(element);
}

function submitDeleteFromSearch() {
    var element = document.getElementById("delete");
    submitForm(element);
}

function submitPageSearch(pageUrl) {
      var element = document.getElementById("bookmark");
      element.form.action = pageUrl;
      element.form.submit();
}

function submitAddAction() {
    var element = document.getElementById("addAction");
    submitForm(element);
}

function submitForm(element) {
    element.form.action = element.value;
    element.form.submit();
}


function resizeSearchPageBannerDiv() {
    var bannerDiv = document.getElementById("bannerdiv");
    var resultsdiv = document.getElementById("resultsdiv");

    var resultsdivWidth = resultsdiv.offsetWidth;
    bannerDiv.style.width = resultsdivWidth;

    resizeSearchSidePanel();
}

function resizeSearchSidePanel() {
    var sidebartd = document.getElementById("sidebar");
    var searchOptions = document.getElementById("searchOptions");

    var sidebarWidth = sidebartd.offsetWidth;
    searchOptions.style.width = sidebarWidth;
}


function chkSelectedStatus(noOfResults, errorMessage) 
{
   var noneChecked = true;

   if (noOfResults > 0)
   {
       for (var k=1; k<= noOfResults; k++)
       {
           var rowChkboxName = "result" + k;
           var chkboxelem = document.getElementById(rowChkboxName);


           if (chkboxelem != null && chkboxelem.checked)
           {
              noneChecked = false;
           }
       } 	

       if (noneChecked) {
	    alert(errorMessage);
	    return false;
       } else {
            toggleSearchResultsActionsLinks();
	    return true;
       }     
    }
}

function closeMessageBox(elementname) {
    var messageBox = document.getElementById(elementname);
    if (messageBox != null) {
       messageBox.style.display = "none";
    } 
}
