
var lastMouseX;
var lastMouseY;

var win = null;
var helpWindow = null;
var curPopupWindow = null;

var FocusNeeded = true; // set a global flag
var isIE = navigator.appName.indexOf("Microsoft") != -1;
var isIE5 = navigator.userAgent.indexOf('MSIE 5.0') > 0;

//This is used to set the default control of a page.
function DefaultCtrl(ctrl)
{
	// Set the focus to the 1st screen field
   if(FocusNeeded) 
   {
      ctrl.focus();
      FocusNeeded = false;
   }
}

function setLastMousePosition(e) 
{
    if(isIE)
      e = window.event;
    
    lastMouseX = e.screenX;
    lastMouseY = e.screenY;
}

function openHelpWin(url)
{
   openPopupFocus(url, "Help", 550, 400, "width=550,height=400,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=yes,scrollbars=yes", true, true);
}

function openDateWin(url)
{
   openPopupFocus(url, "Date", 225, 189, "toolbar=no,status=no,directories=no,menubar=no,resizable=no,scrollable=no,scrollbars=no", true, true);
}

function openWin(url, width, height)
{
   var temp = "toolbar=no,status=no,directories=no,menubar=no,resizable=no,scrollable=no,scrollbars=no";
   
   temp = temp + ",width=" + width;
   temp = temp + ",height=" + height;
   
   openPopupFocus(url, "Pick", width, height, temp, true, true);
}

function openScrollableWin(url, width, height)
{
   var temp = "toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=yes,scrollbars=yes";
   
   temp = temp + ",width=" + width;
   temp = temp + ",height=" + height;
   
   openPopupFocus(url, "Pick", width, height, temp, true, true);
}

/**
 * Handles popup windows.
 * If snapToLastMousePosition is true, then the popup will open up near the mouse click.
 * If closeOnLoseFocus is true, then it will close when the user clicks back into the browser window that opened it.
 */
function openPopupFocus(url, name, pWidth, pHeight, features, snapToLastMousePosition, closeOnLoseFocus)
{
    closePopup();

    if(snapToLastMousePosition)
    {
        if(lastMouseX - pWidth < 0) 
        {
            lastMouseX = pWidth;
        }
        
        if(lastMouseY + pHeight > screen.height)
        {
            lastMouseY -= (lastMouseY + pHeight + 50) - screen.height;
        }
        
        lastMouseX -= pWidth;
        lastMouseY += 10;
        features += ",screenX=" + lastMouseX + ",left=" + lastMouseX + ",screenY=" + lastMouseY + ",top=" + lastMouseY;
    }

    if(closeOnLoseFocus)
    {
        curPopupWindow = window.open(url, name, features, false);
        curPopupWindow.focus();
    }
    else
    {
        // assign the open window to a dummy var so when closePopup() is called it won't be assigned to curPopupWindow
        win = window.open(url, name, features, false);
        win.focus();
    }
}

function closePopup()
{
   if(curPopupWindow != null) 
   {
      if(!curPopupWindow.closed)
      {
         curPopupWindow.close();
      }
      
      curPopupWindow = null;
    }
}

function getSelectedItem(listBox)
{
   for(var i = 0; i < listBox.options.length; i++)
   {
      if(listBox.options[i].selected == true)
         return listBox.options[i];
   }
   
   return null;
}

function findByValue(listBox, value)
{
   for(var i = 0; i < listBox.options.length; i++)
      if(listBox.options[i].value == value)
         return i;
         
   return -1;
}

function findByText(listBox, text)
{
   for(var i = 0; i < listBox.options.length; i++)
      if(listBox.options[i].text == text)
         return i;
         
   return -1;
}

function selectIndex(listBox, index)
{
   for(var i = 0; i < listBox.options.length; i++)
   {
      listBox.options[i].selected = false;
      
      if(i == index)
         listBox.options[i].selected = true;
   }
}

function selectByValue(listBox, value)
{
   var index = -1;
   
   for(var i = 0; i < listBox.options.length; i++)
   {
      listBox.options[i].selected = false;
   
      if(listBox.options[i].value == value)
      {
         index = i;
         listBox.options[i].selected = true;
      }
   }

   return index;
}

function options2String(listBox)
{
   var output = "~";
   
   for(var i = 0; i < listBox.options.length; i++)
   {
      output += listBox.options[i].text;
      output += ",";
      output += listBox.options[i].value;
      output += "~";
   }
   
   return output;
}

function string2Options(listBox, str)
{
   listBox.innerHTML = "";
   
   var entries = str.split("~");
   for(var i = 1; i < entries.length - 1; i++)
   {
      var values = entries[i].split(",");
      
      if(values[1].indexOf(":") != -1)
      {
         listBox.options[listBox.length] = new Option(values[0], values[1].substring(1));
         listBox.selectedIndex = listBox.length - 1;
      }
      else
         listBox.options[listBox.length] = new Option(values[0], values[1]);
   }
}

function object2String(obj)
{
   var val, output = "";
 
   if(obj)
   {
      output += "{";
      for(var i in obj)
      {
         val = obj[i];
         switch(typeof val)
         {
         case("object"):
            if(val[0])
            {
               output += i + ":" + array2String(val) + ",";
            }
            else
            {
               output += i + ":" + object2String(val) + ",";
            }
            break;
         case("string"):
            output += i + ":'" + escape(val) + "',";
            break;
         default:
            output += i + ":" + val + ",";
         }
      }
      output = output.substring(0, output.length-1) + "}";
   }
   return output;
}

function array2String(array)
{
   var output = "";
   if(array)
   {
      output += "[";
      for(var i in array)
      {
         val = array[i];
         switch(typeof val)
         {
         case("object"):
            if(val[0])
            {
               output += array2String(val) + ",";
            }
            else
            {
               output += object2String(val) + ",";
            }
            break;
         case("string"):
            output += "'" + escape(val) + "',";
            break;
         default:
            output += val + ",";
         }
      }
      output = output.substring(0, output.length-1) + "]";
   }
   return output;
}
 
function string2Object(string)
{
   eval("var result = " + string);
   return result;
}

function string2Array(string)
{
   eval("var result = " + string);
   return result;
}

//For the check box columns on Gridviews
function ChangeCheckBoxState(id, checkState)
{
   var cb = document.getElementById(id);
   if(cb != null)
      cb.checked = checkState;
}

function ChangeAllCheckBoxStates(checkState, checkboxes)
{
   // Toggles through all of the checkboxes defined in the CheckBoxes array
   // and updates their value to the checkState input parameter
   if(checkboxes != null)
   {
      for(var i = 1; i < checkboxes.length; i++)
         ChangeCheckBoxState(checkboxes[i], checkState);
   }
}

function ChangeHeaderAsNeeded(checkboxes)
{
    // Whenever a checkbox in the GridView is toggled, we need to
    // check the Header checkbox if ALL of the GridView checkboxes are
    // checked, and uncheck it otherwise
    if(checkboxes != null)
    {
        // check to see if all other checkboxes are checked
        for(var i = 1; i < checkboxes.length; i++)
        {
            var cb = document.getElementById(checkboxes[i]);
            if(!cb.checked)
            {
                // Whoops, there is an unchecked checkbox, make sure
                // that the header checkbox is unchecked
                ChangeCheckBoxState(checkboxes[0], false);
                return;
            }
        }
        
        // If we reach here, ALL GridView checkboxes are checked
        ChangeCheckBoxState(checkboxes[0], true);
    }
}
