// JavaScript Document

//Open help screen
function openHelp(pageName){
	helpLink = window.open(pageName , "HelpPage","width=640,height=480,scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes");
	helpLink.focus();
}

function openQuick(pageName){
	helpLink = window.open(pageName , "DownloadPage","width=640,height=400,scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes");
	helpLink.focus();
}

//Open print friendly page
function openPrinterFriendly(url){
	var settings = "width=800,height=600,scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes";
	pfLink = window.open(url, 'PrintPage', settings);
	pfLink.focus();
}

//Open download page
function openDownload(disclosureName){
	var settings = "width=441,height=322,scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no";
	disclosureLink = window.open(disclosureName, "DownloadPage", settings);
	disclosureLink.focus();
}

//Open disclosure page
function openDisclosure(disclosureName){
	var settings = "width=450,height=400,scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes";
	disclosureLink = window.open(disclosureName, "DisclosurePage", settings);
	disclosureLink.focus();
}

//Open Check page
function openCheck(disclosureName){
	var settings = "width=800,height=399,scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes";
	disclosureLink = window.open(disclosureName, "DisclosurePage", settings);
	disclosureLink.focus();
}

//Open Print Friendly page
function openPrint(disclosureName){
	var settings = "width=850,height=600,scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes";
	disclosureLink = window.open(disclosureName, "DisclosurePage", settings);
	disclosureLink.focus();
}


//Used to display row of HTML if radiobutton is checked
function isCheckedExternalTransfer(menu){
				
	for(var c=0; c < currentMenuTokens.length; c++){
		currentMenu = document.getElementById(currentMenuTokens[c]);
		currentMenu.style.display="none";
	}
				
	var newMenuTokens = menu.tokenize(";", "", true);
		
	currentMenuTokens = newMenuTokens;
		
	for(var i=0; i<newMenuTokens.length; i++){
		newMenuDisplay = document.getElementById(newMenuTokens[i]);
		newMenuDisplay.style.display="";
	}
	
	
}//End isCheckedExternalTransfer Function

//Function used in the navigation menu. Allows the sub navigation menu that is selected,
//to stay visiable when items contained in that sub menu are selected. The function checks the 
//value appended to the url which corresponds to a specific sub menu, and displays that sub menu.
function continueDisplay(){

	if(location.search.substring(1) != '')
		isCheckedExternalTransfer(location.search.substring(1));
}

//Function is used in specific tables that have a check all option, when the parent check box is
//checked the children will then be checked.
function checkUncheckAll(theElement) {
     var theForm = theElement.form, z = 0;
	 for(z=0; z<theForm.length;z++){
      if(theForm[z].type == 'checkbox' && theForm[z].name != 'chkParent'){
	  theForm[z].checked = theElement.checked;
	  }
    }
}//checkAll Function

//Function used to determine which page to link the user to 
//recon,ach,edi,or deposit files depending on the 
//selection.
function switchPage(){
	
	//local Variables
	var selection;
	
	selection = getElement("SubModuleId");
	
	if(selection.value == "RECON") 
		parent.location.href='download_file_recon.html?file_download';
	if(selection.value == "DEPOSIT") 
		parent.location.href='download_file_deposit.html?file_download';
	if(selection.value == "ACH") 
		parent.location.href='download_file_ach.html?file_download';
	if(selection.value == "EDI")
		parent.location.href='download_file_edi.html?file_download';
	if(selection.value == "ACCOUNT")
		parent.location.href='download_file_account.html?file_download';
	if(selection.value == "CORPORATE")
		parent.location.href='download_file_corporate.html?file_download';
	if(selection.value == "**")
		parent.location.href='download_file.html?file_download';			
	
}//switch page function

//Fucntion use in the funds transfer and express transfer to direct the client to different pages
//determined by if the "submit for processing check box is checked
function isProcess(isChecked){
	 
	//local Variables
	var theObject = getElement(isChecked);
		
	if(theObject.checked)
		parent.location.href = nextPage(isChecked, theObject.checked);
	else
		parent.location.href = nextPage(isChecked, theObject.checked);
	
}//end isProcessFucntion

//helper function of "isProcess" use to determine the correct URL to link to.
function nextPage(theLocation, theValue){
	if(theLocation == "chkProcessFund"){
		if(theValue)
			return "funds_entry_submit_process.html?funds_transfer";
		else
			return "funds_entry_submit.html?funds_transfer";	
	}//outer IF
	else{
		if(theValue)
			return "express_entry_submit_process.html?express_transfer";
		else
			return "express_entry_submit.html?express_transfer";	
	}//outer Else
	
}//end nextPage function


//function used to display the day, month and year so they can be displayed,
//formatted together were needed. diff is equal to the number of day you are subtracting from the
//the current date cannot exceed 31 days. display format: (mm/dd/yyyy)
function theDate(diff){
	
	//local variables
	var subDate = theDateHelper(diff);
	var month, day, year;
	
	//dividing the substring created from theDate function
	month = subDate.substr(0,2);
	day = subDate.substr(2,2);
	year = subDate.substr(4,4);
	
	document.write(month +"/" +day+"/" +year);
	
}//end theDate

//function used to separate the day, month and year so they can be displayed,
//individually were needed. diff is equal to the number of day you are subtracting from the
//the current date cannot exceed 31 days. which has to be either "month" "day" "year" each
//will ouput that specific value.
function theDateSub(diff,which)
{
	//local variables
	var subDate = theDateHelper(diff);
	var month, day, year;
	
	//dividing the substring created from theDate function
	month = subDate.substr(0,2);
	day = subDate.substr(2,2);
	year = subDate.substr(4,4);
	
	if(which == "month")
		document.write(month);
	if(which == "day")
		document.write(day);
	if(which == "year")
		document.write(year);
	
}//end theDateSub function

//function will move the date on day into the future and write that date
//to the html page. 'which' will either be 'month', 'day' or 'year and 'which will be
//writen to the screen. If 'which' is NULL mm/dd/yyy will be the format of the output 
function theDateFuture(which)
{
	//localVariables
	var subDate = theDateHelper(0);
	var month, day, year;
	myArray = new Array(31,28,31,30,31,30,31,31,30,31,30,31); //array of month lengths
	
	//dividing the substring created from theDate function
	month = subDate.substr(0,2);
	day = subDate.substr(2,2);
	year = subDate.substr(4,4);
	
	day ++;
	
	if(myArray[month - 1] < day){
		if(month == 12){
			month = "01";
			year ++;
			day = 1;
		}//if
		else{
			day = 1;
			month ++;
		}//else
	}//if	
	
	//format day	
	if (day<10)
		day="0"+day;
	
	
	if(which == null){
		document.write(month +"/" +day+"/" +year);
	}//if
   if(which == "month")
		document.write(month);
	if(which == "day")
		document.write(day);
	if(which == "year")
		document.write(year);
}//end theDateFuture

//Helper Function to "theDate" and "theDateSub" Functions returns 
//the current day minus a diff if entered
function theDateHelper(diff)
{
	//local variables
	var mydate=new Date();
	var year=mydate.getYear();
	var month=mydate.getMonth()+1;
	var day=mydate.getDate();
	var subValue = (day - diff); //value that will be subracted from previous month if value is negative
	myArray = new Array(31,28,31,30,31,30,31,31,30,31,30,31); //array of month lengths
	
	//format the year
	if (year < 1000)
		year+=1900;
	
	
	//We will be subtracting the value 'diff' from days.
	//But we need to determine if we need to go back a month
	if(subValue <= 0){ 
				
		//if month == 1 which is january we will move back to december
		//as well as move the year back one.
		if(month == 1){
			month = 12;
			year -= 1;
		}
		//if month isn't one just subtract a month
		else{
			month -= 1; 
		}//else
		
		//find the day in the previous month, looks in the array creates with the month
		//lengths and adds subValue to that number. subValue is negative that is why we 
		//are adding in this case.
		day = myArray[month - 1] + subValue;
	}//end if
	//if day - diff isn't less than or equal to zero just subtract.
	else{
		day -= diff;
	}//else
	
	//format month and day
	if (month<10)
		month="0"+month;
	if (day<10)
		day="0"+day;
	
	//return the month day and year in string format
	return month.toString() + day.toString() + year.toString();

}//end theDateHelper


//Helper Function used for compatibility between IE to FF browsers
function getElement(id) 
{ if (document.getelementbyid) return document.getelementbyid(id) 
else return eval('document.all.' + id) 
}//end getElemnet()

//This function is used to dynamically create the main menu navigation. The function 
//will read from a text file the menu options that were selected to be present in the demo
//and display those modules. 
function showMenu(){
	
	//Read data from the text file modules.txt and store into any variable
	var current = eval("record1");
		
	//local array used to store the data from the text file
	menu = new Array();
	
	//iterate through the data from the text file and store into an array
	for(var i=0; i < record1.length; i++){
		menu[i] = current[i];
	}//for
	
	//iterate through the array create from the text file to display the selected modules
	for(var x=0; x < menu.length; x++){
		var temp;
		temp = getElement(menu[x]);
		temp.style.display = 'block';
	}//for
	
	padding = 19 - record1.length;
	
	for(var y=1; y <= padding; y++){
		var spacer = "spacer" + y;
		temp = getElement(spacer);		
		temp.style.display = 'block';
	}//end for
	
	
}//end function showMenu()


//displayRow Function
//Function takes a row "id" and displays that row or hides
//the row depending on the current state of the row 
function displayRow(theRow)
{
	temp = getElement(theRow);
	
	if(temp.style.display == "")
		temp.style.display = 'none';
	else
		temp.style.display = "";
		
}//end displayRow

//function radioButtonRedirect
//This function take two variable, both of which are desination
//urls. The function will test on a bool value and redirect to
//the appropriate URL variable.
function radioButtonRedirect(nextURL, altURL)
{
	
	var testValueBool = false;
	var testValueTwoBool = false;
	var testValue,testValueTwo;

	//Determine if the quickbook radio button is checked
	if(document.getElementById("quickbook"))
	{
		testValueBool = true;
		testValue = document.getElementById("quickbook").checked;
	}
	//Determine if the quicken radio button is checked
	if(document.getElementById("quicken"))
	{
		testValueTwoBool = true;
		testValueTwo = document.getElementById("quicken").checked;
	}
		
	//if one of the cased equal true redirect to the altURL
	if((testValue && testValueBool) || (testValueTwo && testValueTwoBool))
		openDownload(altURL);	
	else
		parent.location.href=nextURL;	

}//end radioButtonRedirect


//function getLogo
//This function will read the "logo" property from the 
//properties.txt file and return the value
function getLogo()
{
	//Read data from the text file modules.txt and store into any variable
	var current = eval("logo");
	return current[0];
}


//function writeHeaderLogo
//This function will write the <img> tag to the header
//This function calls the getLogo function which returns the image
//to be used as the header image
function writeHeaderLogo()
{
	var first,second,third;
	
	first = '<img src="..\/images\/';
	second = getLogo();
	third = '" name="main_banner" title="Visit the Bank Home Web Page" align="top" alt="Bank Logo" style="border-width:0px;border-style:None;"\/>	';
	
			
	document.write(first + second + third);

}


/************************************************************************/
/************************************************************************/
/******** ACH FUNCTIONS **********/
//function sumDiff 
//function that is used to compare two variables and direct the user to correct URL
//first = the id of the control input box should be id'ed as 'control'
//second = the id of the total input box should be id'ed as 'total'
//variance = the id of the variance input box shoulb be id'ed as 'variance'
//URL = the desination url
//account = the account name and number
//type = what type of account this is ach...ect
function sumDiff(first, second, variance, URL, account, type)
{
	//localVariables
	var firstValue = 0; 
	var secondValue = 0;
	var varianceValue = 0;
	var confirmValue = false;
	var checkVariance = false;
	
	//check if the id's are valid and the the brower understands getElementById
	if( document.getElementById(first) && document.getElementById(second) && document.getElementById(variance))
	{
		firstValue = document.getElementById(first).value; //get the value of first
		secondValue = document.getElementById(second).value; //get the value of second
		varianceValue = document.getElementById(variance).value; //get the value of third
		
		//call to the isVariance function the function returns a boolean
		checkVariance = isVariance(firstValue,secondValue,varianceValue); 
		
		//Test the value of the checkVariance variable which was set from the return from the isVariance function 
		//If there is a variance between the total and control we will proceed inside the if statement, but if there
		//is not variance between the two or their was no control entered at all procee to the else statement
		if( checkVariance )
			{
				//variable used to hold the diff between first and second variable
				var theDiff = diff(firstValue,secondValue); //call to the diff function
				theDiff = theDiff.toFixed(2); //format 'theDiff' variable to allow only 2 decimal places
				
				//Confirm statement output to client informing them of difference in values
				confirmValue = confirmDiff(secondValue,firstValue,theDiff); //call to the confirmDiff statement
				
				//Uses return from confirmDiff function, if the client doesn't mind the values are different then we will
				//proceed as normal. If the client has made an error; return the to current page.
				if(confirmValue)
					{
						confirmValue = confirmStatement(secondValue, account, type); //call to the confirmStatement function
						confirmLocation(confirmValue,URL); //call to the confirmLocation function
					}//end if
				else
					{
						confirmLocation(confirmValue,URL); //call to the confirmLocation function
					}
			}//end if
		//If there is no variance proceed as normal 
		else
			{
			confirmValue = confirmStatement(secondValue, account, type); //call to the confirmStatement function
			confirmLocation(confirmValue,URL); //call to the confirmLocation function
			}//else
	}//if
}//end function sum


//diff Function
//function takes two number variables and returns the absolute
//value between the two numbers.
function diff(firstValue, secondValue)
{
	//variable used to hold the diff between first and second variable
	var localDiff = 0;
	localDiff = Math.abs(firstValue - secondValue); //finds absolute value 
	return localDiff;
}//end diff Function

//confirmDiff Function
//This function will be fired when there is a difference between the control and the total
//function takes three variables secondValue and firstValue are positive numbers(int or double), 
//theDiff has to be the difference between those two numbers(positive number).
//the function will then return a confirm statement. This function will be
//used in the ACH module of the demo, and is a helper function of sumDiff function.
function confirmDiff(secondValue,firstValue,theDiff)
{
	//local variable
	var confirmValue = false;
	
	//confirm statement
	confirmValue = confirm('The total transfer amount of $' + secondValue + ' differs from the control total of $' + firstValue + ' by $' + theDiff + '. Do you wish to proceed with this transfer.');
	
	return confirmValue;
}//end confirmDiff()

//confirmStatement Function
//This function will be fired every time the submit is selected either after the confirmDiff
//is fired or in the case confirmDiff isn't fired at all this function will fire first.
//function takes three variables secondvalue is a positive number (int or double),
//account is the account in which the transaction will be acting on, and type
//is the type of transaction that will be taking place.
function confirmStatement(secondValue, account, type)
{
	//local variable
	var confirmValue = false;
	
	//confirm statement
	confirmValue = confirm('Please confirm the transfer of the amount of $' + secondValue + '\nvia ' + type + ' from account ' + account + ' ACCOUNT.');
	
	
	return confirmValue;
}//end confirmStatement


//confirmLocation Function
//Helper function which takes a boolean confirmValue and if true links to URL which is the
//second variable entered. If false return to the current page.
function confirmLocation(confirmValue,URL)
{
	if(confirmValue)
		return parent.location.href = URL; //link to the new page
	else
		return; //return to the current page
}//end confirmLocation

//updateTotal Function
//function takes one variable "number" number is the number
//of table rows that will be added together. Each added element must be 
//named and id'ed "amount1", "amount2", "amount3"... and so on. The field 
//where the total value will be stored must be named and id'ed "total". The
//field where the control will be stored will be called and id'ed "control".
//The field where the variance will be stored will be called and id'ed "variance".
function updateTotal(number)
{
	//local variables
	var totalValue = 0;
	var tempValue, tempId;
	var regExp = /[0-9]*\.[0-9]{2}/; //regular express to extract valid dollar amounts
	
	//for loop will loop through all the input boxes labels amount1, amount2, ... and so on.
	//the values of those input boxes will be added together and displayed in the input box
	//id'ed 'total'
	for(var x=1; x <= number; x++)
	{
		tempId = "amount" + x; //creates table ID(amount1,amount2,amount3...	)	
		tempValue = document.getElementById(tempId).value.replace(/,/g,""); //returns element value w/out "," inserted
		tempValue = parseFloat(tempValue); //converts to a float
		tempValue = tempValue.toFixed(2); //formats the float to have only two decimal places
		
		//if statement to test the value 'tempValue' again the regular expression 'regExp'
		//to check if 'tempValue' is valid for currency.
		if(regExp.test(tempValue))
		{
			totalValue += parseFloat(tempValue); //parses to float and add to total
		}//if
		else
		{
			//alert the client that the number they have entered isn't a valid number, and then return from the function
			//leaving the 'total' value unchanged
			alert(document.getElementById(tempId).value + " is not a valid number.\nPlease enter a valid number.");	
			return; //return out of the function
		}//else
		
	}//end for
		
	//sets element "total" value and formates it to 2 decimal places
	document.getElementById('total').value = totalValue.toFixed(2);
	fillVariance('control','total'); //call the fillVariance function to update the 'control' and 'variance' input boxes

}//end updateTotal Function


//fillVariance Function
//This function will find the variance if any between the control and total and place that value
//in the 'variable' input box.
//This function takes two variables. 'control' is the id of the input box used to store the
//conrol variable, and 'total' is the id of the input box used to store the total value.
function fillVariance(control, total)
{
	//local variables
	var variance = 0;	
	var controlValue = 0;
	var totalValue = 0;
	
	//find and format 'control' value
	controlValue = document.getElementById(control).value.replace(/,/g,""); //returns element value w/out "," inserted
	controlValue = parseFloat(controlValue); //convert 'controlValue' to float
	
	//find and format 'total' value
	//'total' value will already be formatted from previous function so formatting 
	//will not be necessary.
	totalValue = document.getElementById(total).value; 
	
	//if controlValue is valid number
	if(!isNaN(controlValue))
	{
		//the value of 'control' will be changed to 'controlValue' formatted
		//to two decimal places.
		document.getElementById('control').value = controlValue.toFixed(2); 
	}
	
	//find the difference, if any between controlValue and totalValue.
	//that value will be stored in the variable 'variance'
	variance = diff(controlValue,totalValue); //call the function diff
	
	//the variable 'variance' will be used to set the input box
	//with the id 'variance'. If the variable 'variance' is not
	//a number set the input box with the id 'variance' to 0.00
	//if the variable 'variance' is a valid number format it to 
	//two decimal places and then set it as the value of the input
	//box with the id 'variance'
	if(isNaN(variance))
		document.getElementById('variance').value = "0.00";
	else
		document.getElementById('variance').value = variance.toFixed(2);

}//endfillVariance

//isVariance Function
//This function will return true is there is a variable in between the 
//total and the control, and false if there is not.
//This function will take three variables firstValue is the value 
//of the 'control' input box, secondValue is the value of the 'total'
//input box and varianceValue is the value of the 'variance' input box
function isVariance(firstValue,secondValue,varianceValue)
{
	firstValue = parseFloat(firstValue); //convert firstValue to float, secondValue and VarianceValue
													 //have been converted earlier and stored in the their respective
													 //input boxes
	
	//case the will have a false return value
	//meaning there is not a variance between control and total
	if(secondValue == firstValue.toFixed(2))
		return false;
	if(isNaN(firstValue))
		return false;
	if(secondValue == varianceValue)
		return false;
	if(isNaN(firstValue - secondValue))
		return false;
		
		//return true if there is a variance between control and total
		return true;
	
}//ed isVariance

/******  END ACH FUNCTIONS********************/
/************************************************************************/
/************************************************************************/
/************************************************************************/