﻿// 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(dateChooser)
{
    if (dateChooser.getValue() == null || dateChooser.getValue().getYear() == 0)    
    {
        dateChooser.setValue(new Date(takeYear(new Date()), 0, 1));
    }
}

function setEmptyDateNull(dateChooser, newText, oEvent)
{
	if (dateChooser.getText() == dateChooser.getNullDateLabel())
	{
		dateChooser.setValue(null);
	}
}

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;
	}
}

// function used by the directory grid
function gridHighlight(el)
{
    try
    {
        if (el.className == "RowDetail")
        {
            el.className = "RowHighlighted";
        }
    }
    catch (err)
    {
        alert(err.description);
    }
}

// function used by the directory grid
function gridUnHighlight(el)
{
    try
    {
        if (el.className == "RowHighlighted")
        {
            el.className = "RowDetail";
        }
    }
    catch (err)
    {
        alert(err.description);
    }
}

// function used by the directory grid
function gridItemClick(el, id, elRowIDName)
{
    try
    {
		var elRowID = window.document.forms[0].elements[elRowIDName];

        // deselect the previously selected row
        var selectedRows = document.getElementsByTagName("tr");
        for (var i = 0; i < selectedRows.length; i++)
        {
            if (selectedRows[i].className == "RowSelected")
            {
	            selectedRows[i].className = "RowDetail";
			}
			
            if (selectedRows[i].className == "RowAltSelected")
            {
	            selectedRows[i].className = "RowAlternate";
			}
        }
     
        // Change the row's style to reflect the user's click
        switch (el.className)
        {
            case "RowSelected":
            {
	            el.className = "RowDetail";

	            // Remove the selected row id from the hidden input, so that
	            // the server can determine which rows have been selected
	            elRowID.value = -1;
            }
            break;
            case "RowDetail":
            case "RowHighlighted":
            {
	            el.className = "RowSelected";
		
	            // Add the selected row id to the hidden input, so that
	            // the server can determine which rows have been selected
		        elRowID.value = id;
            }
            break;
        }
    }
    catch (err)
    {
        alert(err.description);
    }
}
