﻿// Gets a starnet control from a generated page. the starnetID is the BlockName (with spaces replaced with underscores)
// followed by an underscore, followed by the FieldName (with spaces replaced with underscores)
// For RadioButtons type fields, this will return the table wrapping the radio buttons, which you can pass to the 
// radio button helper functions below
function getStarNetControl(starnetID)
{
    var obj = null;
    if (obj == null) obj = findStarNetControl(starnetID, 'input');
    if (obj == null) obj = findStarNetControl(starnetID, 'textarea');
    if (obj == null) obj = findStarNetControl(starnetID, 'select');
    if (obj == null) obj = findStarNetControl(starnetID, 'table');
    if (obj == null) obj = findStarNetControl(starnetID, 'span');
    if (obj == null) obj = findStarNetControl(starnetID, 'div');
    
    if (obj == null)
    {
        //alert('Couldn\'t find ' + starnetID);
    }
    return obj;
}

function setDateDefault(oDropDown)
{
    if (oDropDown.getValue() == null || oDropDown.getValue().getYear() == 0)    
    {
        oDropDown.setValue(new Date(takeYear(new Date()) - 20, 0, 1));
    }
}
function takeYear(theDate) 
{
    x = theDate.getYear();
    var y = x % 100;
    y += (y < 38) ? 2000 : 1900;
    return y;
}
        
// this is the internal function used by the above to find the starnet field.
function findStarNetControl(starnetID, tagName)
{
   col = document.getElementsByTagName(tagName);
    
    for(i=0; i<col.length; i++)
    {
        var control = col[i];
        if (control.getAttribute('starnetid') == starnetID)
        {
            return control;
        }
    }
    return null;
}

// Gets the date from an infragistics date picker as an actual JavaScript date object
function getDatePickerDate(datePicker)
{
    return datePicker.Object.getValue();
}

// Disable/Enable all elements from a radio button list. Pass in the table that wraps them (from getStarNetControl above)
function disableRadioList(radioList, disabled)
{
      col = radioList.getElementsByTagName('input');

      for(i=0; i<col.length; i++)
      {
         col[i].disabled = disabled;
      }
}


// Set the checkstate of a radio button list, by value. Pass in the table that wraps them (from getStarNetControl above)
function setRadioListByValue(radioList, value)
{
      col = radioList.getElementsByTagName('input');

      for(i=0; i<col.length; i++)
      {
        if (col[i].value == value)
        {
          col[i].checked = true;
          return;
        }
      }
}

// Set the checkstate of a radio button list, by index. Pass in the table that wraps them (from getStarNetControl above)
function setRadioListByIndex(radioList, index)
{
      col = radioList.getElementsByTagName('input');

      for(i=0; i<col.length; i++)
      {
        if (i == index)
        {
          col[i].checked = true;
          return;
        }
      }
}

// Get the value of the currently selected radio button. Pass in the table that wraps them (from getStarNetControl above)
function getRadioListValue(radioList)
{
      var done = false;

      for (var i = 0; !done; i++)
      {
             var elItem = document.getElementById(radioList.getAttribute('ID') + '_' + i);
             if (elItem == null)
             {
                   done = true;
             }
             else
             {
                   if (elItem.checked)
                   {
                          return elItem.value;
                   }
             }
      }
      
      return '';
}

// Get the index of the currently selected radio button. Pass in the table that wraps them (from getStarNetControl above)
function getRadioListIndex(radioList)
{
      var done = false;

      for (var i = 0; !done; i++)
      {
             var elItem = document.getElementById(radioList.getAttribute('ID') + '_' + i);
             if (elItem == null)
             {
                   done = true;
             }
             else
             {
                   if (elItem.checked)
                   {
                          return i;
                   }
             }
      }
      
      return -1;
}
function disableControl(evt) 
{        
    alert('Your application process is now complete and you cannot change any data at this time.');  
    evt = (evt) ? evt : ((window.event) ? window.event : null)
    if (evt) 
    {
        var elem = (evt.target) ? evet.target : ((evt.srcElement) ? evt.srcElement : null)
        if (elem) 
        {
            elem.blur();
            return true;
        }
    }    
}

// open a popup window
function openPopup(url, name, width, height)
{
    return windowOpen(url, name, 'width=' + width + ',height=' + height);
}

// detect pop-up blocker blocker
function windowOpen(url, windowName, windowFeatures)
{
    var tableWin = window.open(url, windowName, windowFeatures);
    if (tableWin) 
    {
        tableWin.focus();
        return false;
    }
    else
    {
	    alert("Please turn off your pop-up blocking software");
        return false;
    }
}

// displays or hides the element 'id'
function showHideContent(id, show)
{
    var elem = document.getElementById(id);
    if (elem) 
    {
      if (show) 
      {
        elem.style.display = 'block';
        elem.style.visibility = 'visible';
      } 
      else
      {
        elem.style.display = 'none';
        elem.style.visibility = 'hidden';
      }
    }
}            

// displays the div named 'divStartsWith' and hides all the other divs that start with 'divStartsWith'
function showHideDiv(divStartsWith, divToShow) 
{
    var divs = document.getElementsByTagName('div');
    for (i=0; i < divs.length; i++)
    {
        if ( divs[i].id.match(divStartsWith) )
        {
            if (divs[i].id.match(divToShow))
            {
                divs[i].style.display = 'block';
                divs[i].style.visibility = 'visible';
            } 
            else 
            {
                divs[i].style.display = 'none';
                divs[i].style.visibility = 'hidden';
            }
        }
    }
}

function LimitText(field, maxLimit)
{
	if (field.value.length > maxLimit)
	{
		field.value = field.value.substring(0, maxLimit);
		return false;
	}
	else
	{
		return true;
	}
}

//
// menu methods 
//
function menu(allitems,thisitem,startstate){ 
  callname= "gl"+thisitem;
  divname="subglobal"+thisitem;  
  this.numberofmenuitems = allitems;
  this.caller = document.getElementById(callname);
  this.thediv = document.getElementById(divname);
  this.thediv.style.visibility = startstate;
}

function ehandler(event,theobj){
  for (var i=1; i<= theobj.numberofmenuitems; i++){
    var shutdiv =eval( "menuitem"+i+".thediv");
    shutdiv.style.visibility="hidden";
  }
  theobj.thediv.style.visibility="visible";
}
				
function closesubnav(event){
  if ((event.clientY <48)||(event.clientY > 107)){
    for (var i=1; i<= numofitems; i++){
      var shutdiv =eval('menuitem'+i+'.thediv');
      shutdiv.style.visibility='hidden';
    }
  }
}

//
// end menu methods
//
