// netbankJS.js

function popup(){
		print();
		}
		
function go()
{
    top.window.close()
}

//Disables a dependent control ie a Button if there a checkbox is checked/not checked
function disableDependentControlByCheckBox(dependentControlCSSId, checkBoxCSSId, disableIfChecked)
{
  var dependentControl = getElementWithClass(document, '*', dependentControlCSSId);
  var checkBox = getElementWithClass(document, '*', checkBoxCSSId);

  //Disable
  dependentControl.disabled = (checkBox.checked == disableIfChecked);

  //Build a Method for the OnKeyDown Event of text box
  var sMethod = "getElementWithClass(document, '*', '" + dependentControlCSSId + "').disabled =";
  sMethod += " (this.checked == " + disableIfChecked  + ");";

  //Assign Code to events to trap all cases, copy-paste with mouse etc
  checkBox.onkeyup = new Function(sMethod);
  checkBox.onblur = new Function(sMethod);
  checkBox.onfocus = new Function(sMethod);
  checkBox.onclick = new Function(sMethod);
  checkBox.onmouseover = new Function(sMethod);
}

//Disables any number of dependent controls, passed as string array, if the given checkbox is checked/not checked
function disableDependentControlsByCheckBox(dependentControlCSSIdArray, checkBoxCSSId, disableIfCheckedArray)
{
  var checkBox = getElementWithClass(document, '*', checkBoxCSSId);  
  var dependentControl = null;
  var sMethod = '';

  for(var i = 0; i < dependentControlCSSIdArray.length; i++)
  { 
    //Disable controls            
    dependentControl = getElementWithClass(document, '*', dependentControlCSSIdArray[i]);      
    dependentControl.disabled = (checkBox.checked == disableIfCheckedArray[i]);
    
    //Build a Method for the OnKeyDown Event of text box
    sMethod += "getElementWithClass(document, '*', '" + dependentControlCSSIdArray[i] + "').disabled =";
    sMethod += " (this.checked == " + disableIfCheckedArray[i] + ");";  
  }

  //Assign code to click event  (note: in most cases this is the only event used, 
  // and assigning to only one event greatly improves the efficincy of this function
  checkBox.onclick = new Function(sMethod);
}

//Disables a dependent control ie a Button if there is nothing in a textbox.
function disableDependentControl(dependentControlCSSId, textBoxCSSId)
{
  var dependentControl = getElementWithClass(document, '*', dependentControlCSSId);
  var textBox = getElementWithClass(document, '*', textBoxCSSId);

  //Disable if no value
  dependentControl.disabled = !(textBox.value.length > 0);
  
  //Build a Method for the OnKeyDown Event of text box
  var sMethod = "getElementWithClass(document, '*', '" + dependentControlCSSId + "').disabled =";
  sMethod += " !(this.value.length > 0);";

  //Assign Code to events to trap all cases, copy-paste with mouse etc
  textBox.onkeyup = new Function(sMethod);
  textBox.onblur = new Function(sMethod);
  textBox.onfocus = new Function(sMethod);
  textBox.onclick = new Function(sMethod);
  textBox.onmouseover = new Function(sMethod);

}

//Disables/Enables a control based on there CSS Id
//Avoids naming issues we have with fields for mid tier
function disable(cssId, disable)
{
    var elements = getElementsWithClass(document, '*', cssId);

    for(var i = 0; i < elements.length; i++)
        elements[i].disabled = disable;
}

function setDollarValue(dollarValSpan, percentValSpan)
{
	eval("document.all." + dollarValSpan + ".innerHTML='$';");			
	eval("document.all." + percentValSpan + ".innerHTML='';");
}

function setPercentValue(dollarValSpan, percentValSpan)
{
	eval("document.all." + dollarValSpan + ".innerHTML='';");			
	eval("document.all." + percentValSpan + ".innerHTML='%';");
}

/* This function is used to allow the user to enable/disable or toggle between enabled and disabled
 * fields.
 * 
 * 
 * fieldNames	String[]	name of the fields to be modified
 * behaviour	String		Behaviour of the function input "enable", "disable", "toggle"
*/ 
function toggleFields(fieldNames, behaviour)
{
	var fieldCounter = 0;
	if (behaviour == 'disable')
	{	
		while(fieldNames[fieldCounter] != null)
		{
			fieldNames[fieldCounter].disabled = true;
			fieldCounter++;
		}//end while loop
	}//end if
	
	else if (behaviour == 'enable')
	{
		while(fieldNames[fieldCounter] != null)
		{
			fieldNames[fieldCounter].disabled = false;
			fieldCounter++;
		}//end while loop
	}//end if		
	
	else if (behaviour == 'toggle')
	{
		while(fieldNames[fieldCounter] != null)
		{
			if(fieldNames[fieldCounter].disabled == true)
			{
				fieldNames[fieldCounter].disabled = false;
			}//end if
			else
			{
				fieldNames[fieldCounter].disabled = true;
			}//end else
			
			fieldCounter++;
		}//end while
	}//end if		
}//end toggleFields

/* This function is used to toggle the value of a hidden field, based on whether or not 
 * a given checkbox is currently checked or not, using values passed as parameters.
 * 
 * fieldName		String		name of field to be modified
 * checkBoxName		String		name of checkbox 
 * checkedValue		String		value when checked
 * uncheckedValue	String		value when not checked
*/ 
function toggleCheckBoxValue(fieldName, checkBoxName, checkedValue, uncheckedValue)
{
	
	if (checkBoxName.checked == true) 
	{
		fieldName.value = checkedValue; 
	}//end if
	else 
	{
		fieldName.value = uncheckedValue;
	}//end else
			
}//end toggleFieldValue



/* This function adds an option to the end of the select list passed in
 *
 * Inputs
 * dropdown	Select		This is the selectlist to be updated
 * display	String		This is the option text
 * value	String		This is the option value
 *
 */
function addOption(dropdown, display, value)
{
	var newOption = new Option(display, value);
	dropdown.options[dropdown.options.length] = newOption;
}//end addOption

/*
 * Selects a javascript option based on its value
 *
 */
function selectItemByValue(oDropdown, sValue)
{
	var nOptionCount = 0;
	while(nOptionCount < oDropdown.options.length)
	{
		if(oDropdown.options[nOptionCount].value == sValue)
		{
			oDropdown.options[nOptionCount].selected = true;
		}
		nOptionCount ++;
	}
}

/* This function checks the selectedValue passed in and populates a second selectlist with
 * appropriate values
 *
 * Inputs
 * dropDown			Select		Selectlist to be updated
 * selectedValue	String		selectedValue of the 1st selectlist
 * array			String		contains values to update 2nd selectlist
 * checkColumn		String		column index to compare selected value against
 * valueColumn		String		column index of value to be in the second dropdown option
 * displayColumn	String		column index of the field to display in the second dropdown
 *
 */
function populateDD(dropDown, selectedValue, array, checkColumn, valueColumn, displayColumn)
{
	var arrayCount=0;

	//remove all values except select
	dropDown.length = 1;

	//for all values in array, if selectedValue matches column in array, create new
	//option in dropDown
	while ( (array[arrayCount] != null ) && (array[arrayCount][checkColumn] != null) )
	{
		if(array[arrayCount][checkColumn] != null && array[arrayCount][checkColumn] == selectedValue)
		{
			addOption(dropDown, array[arrayCount][displayColumn], array[arrayCount][valueColumn]);
		}//end if
		arrayCount++;
	}//end while
}//end populateDD



/* This function returns a collection of all the elements with a certain CSS class
 *
 * Inputs
 * containingEl		String 		The containing element (for all, this is document)
 * tagName 			String		tag types (eg 'tr')
 * className		String		the CSS class to look for
 *
 */
function getElementsWithClass(containingEl, tagName, className)
{

    var returnedCollection = new Array(0);

    var collection  = (containingEl.all && tagName == "*") ? 
     containingEl.all : containingEl.getElementsByTagName(tagName);
     
    for(var i = 0; i < collection.length; i++)
        if(collection[i].className == className)
            returnedCollection[returnedCollection.length] = collection[i];

    return returnedCollection;
} //end getElementsWithClass

/*
 *
 * Calls getElementsWithClass(containingEl, tagName, className)
 * returns the first elment or null.
 *
 */
function getElementWithClass(containingEl, tagName, className)
{
    var controls = getElementsWithClass(containingEl, tagName, className);
    if(controls.length > 0)
    {
      return controls[0];
    }
    else
    {
       return null;
    }
}

/* This function concatenates source fields and populates a target field (with a separator if required)
 *
 * Inputs
 * targetFieldID    String 		The id attribute of the target field
 * sourceFieldIDs   [String]    array of id attributes of the source field (eg "new Array('KC_CARD_NUMBER_1', 'KC_CARD_NUMBER_2')" )
 * separator        String		separator string (if required)
 *
 *
 * Example usage:  onclick="concatenateFields('targetId', new Array('sourceID1','sourceID2'), '-')"
 * Example output: targetID.value = "sourceID1value-sourceID2value"
 *


 */
function concatenateFields(targetFieldID, sourceFieldIDs, separator)
{
    var sTemp = "";
    
    for(var i = 0; i < sourceFieldIDs.length; i++)
    {
        var sTempFieldID = sourceFieldIDs[i];

        sTemp = sTemp + document.getElementById(sTempFieldID).value;
        
        if ((i != (sourceFieldIDs.length -1)) && (separator != undefined))
        {
            sTemp = sTemp + separator;
        }
    }
    document.getElementById(targetFieldID).value = sTemp;       
}

/* This function takes the field name and a flag that indicates if the select All checkbox is checked
 * and selects or deselects all table values.
 * 
 * Inputs
 * selectAllFieldId		String 	The name of the select All check box
 * cssClass				String 	CSS class of the checkboxes to select/unselect
 *
 */
function selectAll(selectAllFieldId, cssClass)
{
	var isSelected = document.getElementById(selectAllFieldId).checked;

    var checkboxes = getElementsWithClass(document, "input", cssClass);

	for(j = 0; j < checkboxes.length; j++) 
	{
		checkboxes[j].checked = isSelected;
	}
	
}

/* When selecting a checkbox, this function checks if all values in the checkbox are checked
 * and checks/unchecks the select all checkbox
 *
 * Inputs
 * selectAllFieldId		String 	The name of the select All check box
 * cssClass				String 	CSS class of the checkboxes to select/unselect
 */
function checkSelectAll(selectAllFieldId, cssClass)
{
	var checkboxes = getElementsWithClass(document, "input", cssClass);
    var allSelected = true;
	
	for(j = 0; j < checkboxes.length; j++) 
	{
		if (checkboxes[j].checked == false)
        {
			allSelected = false;
        }
	}
	document.getElementById(selectAllFieldId).checked = allSelected;	
}

function writeDatePicker(sFieldId)
{
    document.write("<a href=\"javascript:show_calendar('" + sFieldId + "');\" onmouseover=\"window.status='Date Picker';return true;\" onmouseout=\"window.status='';return true;\" title=\"Opens a calendar popup window\"><img src=\"../images/calendar.gif\" alt=\"Launch calendar in new window\"></img></a>");
}

function setValueById(sFieldId, sValue)
{
    oField = document.getElementById(sFieldId);
    oField.value = sValue;
}

function setStringById(sFieldId, sValue)
{
    oField = document.getElementById(sFieldId);
    oField.innerHTML = sValue;
}
/* This function is used to set the initial focus to a specific form element on the page.  This element is 
 * is identified by its 'id' attribute. If a null argument is passed, the function will set the focus
 * to the first clickable form element found on the page.
 *
 * Inputs
 * sElementId		    String 	The id of the element that requires initial focus
 *
 */
function setInitialFocus(sElementId)
{

	if (sElementId != "null")
	{
		// set focus to field matching given elementId
		var element = document.getElementById(sElementId);
	 	
		if (element != null)
		{
			element.focus();
		}	
	}
	else
	{
		// otherwise set focus to first clickable field on the page
		for (var i = 0; i < document.forms.length; i++)
		{
			for(var j = 0; j < document.forms[i].length; j++)
			{            
            	if((document.forms[i].elements[j].type == "text") ||
	               (document.forms[i].elements[j].type == "textarea") ||            	
            	   (document.forms[i].elements[j].type == "select-one") ||
            	   (document.forms[i].elements[j].type == "checkbox") ||            	   
            	   (document.forms[i].elements[j].type == "radio") ||             	   
            	   (document.forms[i].elements[j].type == "submit"))
            	{
            		if(document.forms[i].elements[j].disabled == false)
            		{            		
	            		document.forms[i].elements[j].focus();
	            		return;
					}	            		
				}	            	
			}
		}	
	}	
}

var nKbSize = 1024;
function calculateFileSize()
{
	if (!document.fileSize)
	{
		return ('This script does not work in your browser.');
	}
	var size = (document.fileSize)*1;
	var y = document.images;
	var imglength = 0;
	for (i=0;i<y.length;i++)
	{
		imglength += (y[i].fileSize)*1;
	}
	var total = size + imglength;
	var writestring = 'File size HTML:&nbsp;&nbsp;&nbsp;' + Math.round(size/nKbSize) + ' kb';
	writestring += '<br />File size images:&nbsp;&nbsp;' + Math.round(imglength/nKbSize)  + ' kb';
	writestring += '<br />Total file size:&nbsp;&nbsp;' + Math.round(total/nKbSize)  + ' kb';
    return writestring;
}

//called on the onload of a page to logon as customer

function checkLogonAsCustomer(eventName, error){
if ((eventName == "Logon as customer") && (error == "false")){
document.logonAsCustomer.submit();
}
}
