/* 2009 Rabobank America: sitewide.js */

/* IMPORTANT: This file loads functions for every page. Please restrict file content to functions required site-wide. -- AA */

/* Contents: */

/* [01] Window.onload event loading (Rabobank America search, Collage contribution, Account login selection) */
/* [02] Rabobank America search methods */
/* [03] Account login selection (hide/show + cookie functions, also uses [01] onload functions) */
/* [04] Branch/ATM search methods */
/* [05] Agent search methods */
/* [06] Helper methods */

/* =========================================== */

/* [01] Window.onload event loading (Rabobank America search, Collage contribution, Account login selection) */

// AA: 2009Jan23: Adds onload events that won't override Serena's onload code in the edit contribution window
function addLoadEvent(func)
{
	// Get the old onload events
	var oldonload = window.onload;
	
	// Check to see if there are currently events in the onload
	if (typeof window.onload != 'function') {
		
		// No other events so just add the passed method
		window.onload = func;
	} 
	else // An event was found in the onload
	{
		// Set the new onload event
		window.onload = function()
		{
			// Call the old onload events if they exist first
			if (oldonload) { oldonload(); }
			
			// Call the function that was passed
			func();
		}
	}
}

addLoadEvent(function()
{
/* Start: add onload events */
	
	try
	{
		// Check to see if the login username element exists
		if (typeof document.getElementById("login_username") != "undefined")
		{
			// Set the submit, focus, and blur events on the search form and text box
			document.getElementById("login_username").onfocus = function() { loginSelect(document.getElementById("login_username")); }
			document.getElementById("login_username").onblur = function() { loginDeselect(document.getElementById("login_username")); }
		}
	}
	catch (ex) {}
	
	try
	{
		// Check to see if the search form exists
		if (typeof document.getElementById("searchForm") != "undefined")
		{
			// Set the submit, focus, and blur events on the search form and text box
			document.getElementById("searchForm").onsubmit = function() { return searchSubmit(document.getElementById("searchForm").q); }
			document.getElementById("searchForm").q.onfocus = function() { searchSelect(document.getElementById("searchForm").q); }
			document.getElementById("searchForm").q.onblur = function() { searchDeselect(document.getElementById("searchForm").q); }
		}
	}
	catch (ex) {}
	
	try
	{
		// Check to see if the branch/atm search form exists
		if (typeof document.getElementById("locationForm") != "undefined")
		{
			// Set the submit, focus, and blur events on the branch/atm search form and text box
			document.getElementById("locationForm").onsubmit = function() { return locationSubmit(document.getElementById("locationForm").location); }
			document.getElementById("locationForm").location.onfocus = function() { locationSelect(document.getElementById("locationForm").location); }
			document.getElementById("locationForm").location.onblur = function() { locationDeselect(document.getElementById("locationForm").location); }
		}
	}
	catch (ex) {}
	
	try
	{
		// Check to see if the agent search form exists
		if (typeof document.getElementById("agentForm") != "undefined")
		{
			// Set the submit, key press, focus, and blur events on the agent search form and text box
			document.getElementById("agentForm").onsubmit = function() { return agentSubmit(document.getElementById("agentForm").agent_zip); }
			document.getElementById("agentForm").agent_zip.onfocus = function() { agentSelect(document.getElementById("agentForm").agent_zip); }
			document.getElementById("agentForm").agent_zip.onblur = function() { agentDeselect(document.getElementById("agentForm").agent_zip); }
			document.getElementById("agentForm").agent_zip.onkeypress = function() { return zipcodeKeyPress(document.getElementById("agentForm").agent_zip); }
		}
	}
	catch (ex) {}
	
/* End: add onload events */
});

/* =========================================== */

/* [02] Rabobank America search methods */

// Controls the "enter search text here" default text when selected
function searchSelect(textbox)
{
	// Check to see if the textbox has the default text
	if(textbox.value == "Search")
	{
		// Clear the text and set the class
		textbox.value = "";
	}
}

// Controls the "enter search text here" default text when deselected
function searchDeselect(textbox)
{
	// Check to see if the textbox is empty
	if(trim(textbox.value) == "" || textbox.value == "Search")
	{
		// Set the class and text
		textbox.value = "Search";
	}
	else // There is valid text in the textbox
	{
		// Trim the search text and put it back
		textbox.value = trim(textbox.value);
	}
}

// Makes sure the search text is not blank when submitting
function searchSubmit(textbox)
{
	// Check to see if the textbox is empty
	if(trim(textbox.value) == "" || textbox.value == "Search")
	{
		// Inform the user and cancel the submit
		alert("You must enter something to search");
		textbox.focus();
		return false;
	}
	else // The textbox is not empty
	{
		// Allow the form to submit
		return true;
	}
}

/* =========================================== */

/* [03] Account login selection (hide/show + cookie functions, also uses [01] onload functions) */

// Creates a cookie with the values passed
function createCookie(name, value, days) {
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

// Clears the cookie name that is passed
function eraseCookie(name) { createCookie(name,"",-1); }

// Validate the dropdown form
function validateLoginGo(submitForm)
{
	// Declare local variables
	loginOptions = submitForm.login_options;
	selectedURL = loginOptions[loginOptions.selectedIndex].value;
	
	// Check to see if an option was selected
	if (selectedURL != "")
	{
		// Allow the form to post to the selected URL
		submitForm.action = selectedURL;
		rememberSelection(document.getElementById("login_remember"));
		return true;
	}
	else // No selected URL was found
	{
		// Warn the user and cancel the submit
		alert("Please select an account.");
		return false;
	}
}

// Validate the username login form
function validateLoginUsername(submitForm)
{
	// Declare local variables
	loginUsername = submitForm.UserID.value;
	
	// Check to see if login id was entered
	if (loginUsername != "" && loginUsername != "Enter Login ID")
	{
		// Allow the form to post to the personal banking URL
		submitForm.action = "https://online.rabobankamerica.com/RaboBank/Login.aspx";
		rememberSelection(document.getElementById("login_remember"));
		return true;
	}
	else // No login id was entered
	{
		// Warn the user and cancel the submit
		alert("Please enter a Login ID");
		return false;
	}
}

// Saves the currently selected options to a cookie if selected
function rememberSelection(checkbox)
{
	// Declare local variables
	selectedIndex = document.getElementById("login_options").selectedIndex;
	
	// Check to see if the box was checked
	if (checkbox.checked == true)
	{
		// Create the cookie with the selected index option for seven days
		createCookie("login_selection", selectedIndex, 7);
		
		// Check to see if the selected option is personal banking
		if (selectedIndex == 1)
		{
			//Pending Approval: username = document.getElementById("login_username");
			//Pending Approval: createCookie("login_username", username, 7);
		}
	}
	else // The box was not checked
	{
		// Clear the cookies
		try { eraseCookie("login_selection"); } catch (ex) {}
		try { eraseCookie("login_username"); } catch (ex) {}
	}
}

// Cancels the enter key and clicks the button (Bug workaround for S1)
function check_submit(evt)
{
	// Check to see if the enter key was pressed
	if (evt.keyCode == 13)
	{
		// Click the submit button
		document.getElementById("submit_logon").click();
		
		// Cancel the enter key
		return false;
	}
	else // This is not the enter key
	{
		// Allow button
		return true;
	}
}

// Controls the "Enter Login ID" default text when selected
function loginSelect(textbox)
{
	// Check to see if the textbox has the default text
	if(textbox.value == "Enter Login ID")
	{
		// Clear the text and set the class
		textbox.value = "";
	}
}

// Controls the "Enter Login ID" default text when deselected
function loginDeselect(textbox)
{
	// Check to see if the textbox is empty
	if(trim(textbox.value) == "" || textbox.value == "Enter Login ID")
	{
		// Set the class and text
		textbox.value = "Enter Login ID";
	}
	else // There is valid text in the textbox
	{
		// Trim the search text and put it back
		textbox.value = trim(textbox.value);
	}
}

/* =========================================== */

/* [04] Branch/ATM search methods */

// Controls the "Enter Zip Code or City" default text when selected
function locationSelect(textbox)
{
	// Check to see if the textbox has the default text
	if(textbox.value == "Enter Zip Code or City")
	{
		// Clear the text and set the class
		textbox.value = "";
	}
}

// Controls the "Enter Zip Code or City" default text when deselected
function locationDeselect(textbox)
{
	// Check to see if the textbox is empty
	if(trim(textbox.value) == "" || textbox.value == "Enter Zip Code or City")
	{
		// Set the class and text
		textbox.value = "Enter Zip Code or City";
	}
	else // There is valid text in the textbox
	{
		// Trim the search text and put it back
		textbox.value = trim(textbox.value);
	}
}

// Makes sure the location text is not blank when submitting
function locationSubmit(textbox)
{
	// Check to see if the textbox is empty
	if(trim(textbox.value) == "" || textbox.value == "Enter Zip Code or City")
	{
		// Inform the user and cancel the submit
		alert("You must enter a location to search");
		textbox.focus();
		return false;
	}
	else // The textbox is not empty
	{
		// Allow the form to submit
		return true;
	}
}

/* =========================================== */

/* [05] Agent search methods */

// Controls the "Enter Zip Code " default text when selected
function agentSelect(textbox)
{
	// Check to see if the textbox has the default text
	if(textbox.value == "Enter Zip Code")
	{
		// Clear the text and set the class
		textbox.value = "";
	}
}

// Controls the "Enter Zip Code" default text when deselected
function agentDeselect(textbox)
{
	// Check to see if the textbox is empty
	if(trim(textbox.value) == "" || textbox.value == "Enter Zip Code")
	{
		// Set the class and text
		textbox.value = "Enter Zip Code";
	}
	else // There is valid text in the textbox
	{
		// Trim the search text and put it back
		textbox.value = trim(textbox.value);
	}
}

// Makes sure the agent text is not blank when submitting
function agentSubmit(textbox)
{
	// Check to see if the textbox is empty
	if(trim(textbox.value) == "" || textbox.value == "Enter Zip Code")
	{
		// Inform the user and cancel the submit
		alert("You must enter a zip code to search");
		textbox.focus();
		return false;
	}
	else // The textbox is not empty
	{
		// Allow the form to submit
		return true;
	}
}

/* =========================================== */

/* [06] Helper methods */

// Trims whitespace off of the front and back of a string
function trim(str)
{
	// Remove spaces using regular expression
	return str.replace(/^\s+|\s+$/g, '');
}

// Function that opens a new window to display a flash demo
function viewDemo(url)
{
	// Open the url in a new window
	window.open(url, "VIEWWINDOW", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, copyhistory=no, resizable=yes, screenx=0, screeny=0, left=0, top=0, width=" + (screen.availWidth - 12) + ", height=" + (screen.availHeight - 30));
	
	// Cancel the link navigation
	return false;
}

// Function that only allows numbers to be pressed
function zipcodeKeyPress(obj)
{
	// Loop through each charater
	for (i=0; i<=obj.value.length; i++)
	{
		// Check to see if invalid character was pressed
		if (!(window.event.keyCode == 13 || window.event.keyCode == 48 || window.event.keyCode == 49 ||
			  window.event.keyCode == 50 || window.event.keyCode == 51 || window.event.keyCode == 52 ||
			  window.event.keyCode == 53 || window.event.keyCode == 54 || window.event.keyCode == 55 ||
			  window.event.keyCode == 56 || window.event.keyCode == 57)) { return false; }
	}
}
/* =========================================== */
/* END */