/********************************************************************
   Utilities 2.2                                           2003-01-03

	Collection of useful (?) functions.

   // Markus Gemstad
   gemstad@hotmail.com
   http://www.gemstad.com (references, samples etc)
********************************************************************/

/** Global settings *************************************************

  g_sDisableColor : Background color of ie SELECT fields on disabled.
  g_sEnableColor  : Background color of ie SELECT fields on enabled.
  g_sMsg...       : Messages used by the functions.
                                                                   */

var g_sDisableColor = "#CCCCCC";
var g_sEnableColor  = "#F7FAFF";

var g_sMsgAddOption_ValueExist       = "Värdet fanns redan!";
var g_sMsgSortSelectOption_SelectOne = "ðà áçø àú äùãä";
/*******************************************************************/


/** Description *****************************************************
  Sort option items in a select in alphabetic order. IE only.
********************************************************************/
function sortSelect(oSelect)
{
   var objSelect       = document.frmMain.lstSort;
   var arrOptionNames  = new Array();

   // Create array with arrays with values from options for each node
   for(var i = 0; i < oSelect.options.length; i++)
   {
      arrOptionNames[i] = new Array(oSelect.options[i].text, oSelect.options[i].value);
   }

   arrOptionNames.sort();

   // Change the values and names of the options
   for(i = 0; i < oSelect.options.length; i++)
   {
      oSelect.options[i].text  = arrOptionNames[i][0];
      oSelect.options[i].value = arrOptionNames[i][1];
   }
}


/** Description *****************************************************
   Get reference to an OPTION object by its value. IE only.
********************************************************************/
function getOptionByValue(oSelect, sValue)
{
   for(var i = 0; i < oSelect.options.length; i++)
   {
      if(oSelect.options[i].value == sValue)
         return i;
   }
   return null;
}


/** Description *****************************************************
   Sort selected OPTION in a SELECT list - up or down. IE only.
********************************************************************/
function sortSelectOption(oSelect, bUp)
{
   iSelect = oSelect.selectedIndex;
   if(iSelect == -1)
   {
      alert(g_sMsgSortSelectOption_SelectOne);
   }
   else
   {
      if(bUp && iSelect-1 != -1)
         oSelect.options[iSelect].swapNode(oSelect.options[iSelect-1]);
      else if(!bUp && iSelect+1 < oSelect.options.length)
         oSelect.options[iSelect].swapNode(oSelect.options[iSelect+1]);
   }
}


/** Description *****************************************************
   Enable or disable FORM objects. IE only.
********************************************************************/
function setEnabled(arrFormObj, bEnable)
{
   var bDisable = (bEnable) ? false : true;
   for(var i=0; i < arrFormObj.length; i++)
   {
      if(arrFormObj[i] != null)
      {
         arrFormObj[i].disabled = bDisable;
      
         var sColor, sCursor;
         bDisable ? sColor = g_sDisableColor : sColor = g_sEnableColor;
         bDisable ? sCursor = "default" : sCursor = "hand";
         
         // If this is a textedit box - change bg color
         if(arrFormObj[i].isTextEdit) arrFormObj[i].style.background = sColor;
         
         // If this is a input type image - change cursor
         if(arrFormObj[i].type == "image") arrFormObj[i].style.cursor = sCursor;
      }
   }
}


/** Description *****************************************************
   Toggle checkmark in a checkbox or radiobutton. IE4 or higher, NS4
   and NS6.
********************************************************************/
function toggleCheck(oFormObj)
{
   if(oFormObj.checked)
   {
      if(oFormObj.type != "radio")
         oFormObj.checked = false;
   }
   else
      oFormObj.checked = true;
   return oFormObj.checked;
}


/** Description *****************************************************
   Remove OPTION(S) in a SELECT list. IE only.
********************************************************************/
function removeOptions(oSelect, bSelectedOnly, sValue)
{
   for(var i = oSelect.options.length-1; i >= 0; i--)
   {
      if(sValue)
      {
         if(sValue == oSelect.options[i].value)
            oSelect.options.remove(i);
      }
      else
      {
         if(bSelectedOnly && oSelect.options[i].selected)
            oSelect.options.remove(i);
         else if(!bSelectedOnly)
            oSelect.options.remove(i);
      }
   }
}


/** Description *****************************************************
   Add a OPTION to a SELECT list. IE only.
********************************************************************/
function addOption(oSelect, sText, sValue, bUnique)
{
   var sErrorMsg = "";
   var bFound = false;
   if(bUnique)
   {
      for(var i = 0; i < oSelect.options.length; i++)
      {
         if(oSelect.options[i].value == sValue && oSelect.options[i].text == sText)
         {
            sErrorMsg = g_sMsgAddOption_ValueExist;
            bFound    = true;
         }
      }
   }
   if(!bFound)
      oSelect.options[oSelect.length] = new Option(sText, sValue);

   return sErrorMsg;
}


/** Description *****************************************************
   Copy selected options from one SELECT to another. IE only.
********************************************************************/
function copyOptions(oSelectFrom, oSelectTo, bUnique, bRemoveSource)
{
   var bFound = false;
   for(var i=0; i<oSelectFrom.options.length; i++)
   {
      bFound = false;
      if(oSelectFrom.options[i].selected)
      {
         if(bUnique)
         {
            for(var i2=0; i2<oSelectTo.options.length; i2++)
            {
               if(oSelectFrom.options[i].value == oSelectTo.options[i2].value && oSelectFrom.options[i].text == oSelectTo.options[i2].text)
                  bFound = true;
            }
         }
         if(!bFound)
            oSelectTo.options[oSelectTo.length] = new Option(oSelectFrom.options[i].text, oSelectFrom.options[i].value);
      }
   }
   if(bRemoveSource)
      removeOptions(oSelectFrom, true);
}


/** Description *****************************************************
   Open a window in the center of the users screen. IE only???
********************************************************************/
function openCenterWnd(sUrl, iWidth, iHeight, sFeatures, bRandomWndName, sWndName)
{
	var iTop, iLeft, sAllFeatures;
	
	if(bRandomWndName)
	{
		sWndName = "wnd" + (Math.random()+"").slice(2);
	}
	
	iTop  = (window.screen.height - iHeight)/2;
	iLeft = (window.screen.width  - iWidth)/2;
	
	sAllFeatures = "top=" + iTop + ",left=" + iLeft + ",width=" + iWidth + ",height=" + iHeight;
	if(sFeatures != null || sFeatures != "")
		sAllFeatures += "," + sFeatures;
	
	return window.open(sUrl, sWndName, sAllFeatures);
}


/** Description *****************************************************
   Insert a HTML into a selected area of a TEXTAREA. IE only.
********************************************************************/
function insertHtmlTag(sTag, sStartTag, sEndTag)
{
   var oTxtRng = document.selection.createRange();
   oTxtRng.text = sStartTag + oTxtRng.text + sEndTag;
}


