/********************************************************************
   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???
********************************************************************/

var windowHandle = null;
function IsPopUpOpen()  //This function checks if the window is open
{
  if (windowHandle != null)
  {
      if (windowHandle.closed)
         return 0
      else
        return 1
  }
  else
    return 0
}

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;
	
	if (IsPopUpOpen())
    windowHandle.focus()
	else
	windowHandle=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;
}


function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keycode;
else if (e) keycode = e.which;
else return true;
if (keycode == 13)
{
validateForm();
return false;
}
else
return true;
}








