// Reference to this file is called in the iTransplantBasePage.aspx. 
// The following functions are global helper functions for all WebUI pages. 


function WindowOpen(url)
{
    window.open(url,"window");
    
}

function TextLimit(fieldId, maxlen)
{
	elem = document.getElementById(fieldId);
	if (elem.value.length > maxlen)
	{
		elem.value = elem.value.substring(0, maxlen);
		alert('Field limited to ' + maxlen + ' characters.');
	}
}
		
function TextCounter(control,counter,maxlength) 
{
	if (control.value.length > maxlength) 
		control.value = control.value.substring(0, maxlength);
	else
		counter.value = maxlength - control.value.length;
}

function TextLimitWithCounter(fieldId,counterId,maxlength) 
{
	field = document.getElementById(fieldId);
	counter = document.getElementById(counterId);

	if (field.value.length > maxlength) 
		field.value = field.value.substring(0, maxlength);
	else
		counter.value = maxlength - field.value.length;
}

function DropDownListEnablesOtherTextbox(DropDownList, TextBox)
{
	isSelected = (DropDownList.selectedIndex == DropDownList.options.length - 1);
	control = document.getElementById(TextBox);
	EnableControls(isSelected, control, true, false);
}    

function DropDownListEnablesControls(dropDownList, selectedValue, parentCtrlToDisableId, checkedEnables, recurseImmediateSibilings)
{
	isSelected = (dropDownList.value == selectedValue);

	control = document.getElementById(parentCtrlToDisableId);
	EnableControls(isSelected, control, checkedEnables, recurseImmediateSibilings);

	if (dropDownList.parentNode != null && dropDownList.parentNode.disabled != null)
		dropDownList.parentNode.disabled = false;

	dropDownList.selectedvalue = selectedValue;
	dropDownList.disabled = false;
	dropDownList.style.backgroundColor  = 'white';
}

// ** CheckBox enables a control (+ children) or an array of controls (+ children)

function CheckBoxEnablesControls(checkbox, parentCtrlToDisableId, checkedEnables, recurseImmediateSibilings)
{
	isChecked = checkbox.checked;
	control = document.getElementById(parentCtrlToDisableId);
	EnableControls(isChecked, control, checkedEnables, recurseImmediateSibilings);

	checkbox.checked = isChecked;
	checkbox.disabled = false;
	if (checkbox.parentNode != null && checkbox.parentNode.disabled != null)
		checkbox.parentNode.disabled = false;
}

function CheckBoxEnablesControlArray(checkbox, parentControlsToDisableIdArray, checkedEnables, recurseImmediateSibilings)
{
	isChecked = checkbox.checked;
	for (i = 0; i < parentControlsToDisableIdArray.length; i ++)
	{
		CheckBoxEnablesControls(checkbox, parentControlsToDisableIdArray[i], checkedEnables, recurseImmediateSibilings);
	}
	
	checkbox.checked = isChecked;
	checkbox.disabled = false;
	if (checkbox.parentNode != null && checkbox.parentNode.disabled != null)
		checkbox.parentNode.disabled = false;
}

// ** Radio button list enables a control (+ children) or an array of controls (+ children)

function RBListEnablesControls(rblName, parentCtrlToDisableId, trueVal, trueEnables, recurseImmediateSibilings)
{
	rbl = document.forms[0].elements[rblName];
	ctrl = document.getElementById(parentCtrlToDisableId);
	
	for (i = 0; i < rbl.length; i ++)
	{
		if (rbl[i].checked && rbl[i].value == trueVal)
		{
			EnableControls(true, ctrl, trueEnables, recurseImmediateSibilings);
			return;
		}
	}
	EnableControls(false, ctrl, trueEnables, recurseImmediateSibilings);
}

function RBListEnablesControlsArray(rblName, parentCtrlToDisableIdArray, trueVal, trueEnables, recurseImmediateSibilings)
{
	for (i = 0; i < parentCtrlToDisableIdArray.length; i ++)
	{
		RBListEnablesControls(rblName, parentCtrlToDisableIdArray[i], trueVal, trueEnables, recurseImmediateSibilings);
	}
}

// ** Enable controls

function EnableControls(condition, containerElement, trueConditionEnables, recurseContainerElementSiblings)
{
	var grayOutClass
	var current = containerElement;
	var sibling = current.nextSibling;
	var child = current.firstChild;
	
	n = current.nodeName;
	isTextArea = (n == "TEXTAREA");

	if ((condition && trueConditionEnables) || (!condition && !trueConditionEnables))
	{
		if (current.disabled != null)
		{
			if (n == 'INPUT' || n == 'TEXTAREA'|| n == 'SELECT')
			{
				if (current.type == 'text')
				{                                 
					 grayOutClass = 'GrayOutTextBoxUndo';
				}
				else if (n == 'TEXTAREA')
				{
					 grayOutClass = 'GrayOutTextAreaUndo';
				}
				else if (current.type == 'select-one')
				{
					 grayOutClass = 'GrayOutDropDownUndo';
				}
			}
			current.disabled = false;
			AppendGrayOutClass_1(current,grayOutClass);

		}
	}
	else 
	{
		if (n == 'INPUT' || n == 'TEXTAREA' || n == 'SELECT')
      {
			if (current.type == 'text')
			{
				grayOutClass = 'GrayOutTextBox';
			}
			else if (n == 'TEXTAREA')
			{
				grayOutClass = 'GrayOutTextArea';
			}
			else if (current.type == 'select-one')
			{
				grayOutClass = 'GrayOutDropDown';
			}
             
			current.disabled = true;
			AppendGrayOutClass_1(current,grayOutClass);

			if (n == 'TEXTAREA') // Handles some quirky error with textarea's...
				return;
		}	
	}
	if (sibling != null && recurseContainerElementSiblings) EnableControls(condition, sibling, trueConditionEnables, true);
	if (child != null) EnableControls(condition, child, trueConditionEnables, true);
}

function AppendGrayOutClass_1(current, grayOutClass)
{
  var className = current.className;

  if(className!=null || className!='')
  {
      var index = className.indexOf('GrayOut');
      if(index==-1)
      {
          current.className += ' ' + grayOutClass;
      }
      else
      {
          current.className = className.substring(0,index) + grayOutClass;
      }
  }
  else 
  {
      current.className = grayOutClass;
  }

}
