﻿$(document).ready(function() {
	$('#onlinebanking').change(function() {
		$('#personalbanking').hide();
		$('#businessbanking').hide();
		$('#merchantcapture').hide();
		$('#onlinebanking option:selected').each(function() {
			if ($(this).val() == 'P') {
				$('#personalbanking').fadeIn();
				$('#p_username').focus();
			}
			else if ($(this).val() == 'B') {
				$('#businessbanking').fadeIn();
				$('#b_username').focus();
			}
			else {
				$('#merchantcapture').fadeIn();
				$('#c_username').focus();
			}
		});
		closeWarningBox();
	});

	$('#personalbanking :input').keydown(function(e) {
		if (e.which == 13) {
			$('#p_submit').click();
			return false;
		}
	});

	$('#businessbanking :input').keydown(function(e) {
		if (e.which == 13) {
			$('#b_submit').click();
			return false;
		}
	});

	$('#merchantcapture :input').keydown(function(e) {
		if (e.which == 13) {
			$('#c_submit').click();
			return false;
		}
	});

	$('#p_submit').click(function() {
		// Perform validation on Access ID textbox
		//
		if ($('#p_username').val() == '') {
			displayWarningBox('Please enter Access ID then click Submit to login to Internet Banking.');
			$('#p_submit').focus();
		}
		else {
			post_to_url('https://www.thefarmersbank.biz/pbi_pbi1961/pbi1961.asp?WCI=RemoteLogin&Rt=074902503&LogonBy=Connect3',
				{
					'AccessID': $('#p_username').val() 
				}, 'post', '_self');
			$('#p_username').val('');
		}

		return false;
	});

	$('#b_submit').click(function() {
		// Perform validation on username/password
		//
		if ($('#b_username').val() == '') {
			displayWarningBox('Please enter Access ID then click Submit to login to Business Connexion.');
			$('#b_submit').focus();
		}
		else {
			post_to_url('https://www.businessconnexion.com/EBC_EBC1961/EBC1961.ASP?WCI=Process&WCE=RemoteLogon&IRL=T',
				{
					'nmRTN': '074902503',
					'nmUID': $('#b_username').val()
				}, 'post', '_self');
			$('#b_username').val('');
		}

		return false;
	});

	$('#c_submit').click(function() {
		// Perform validation on username/password
		//
		{
			post_to_url('https://mscweb.thefarmersbank.com/',
				{}, 'get', '_self');
		}

		return false;
	});
});

function post_to_url(path, params, method, target) {
	method = method || "post";		// Set method to post by default, if not specified.
	target = target || "_blank";	// Set target to blank by default, if not specified
	
	// The rest of this code assumes you are not using a library.    
	// It can be made less wordy if you use one.    
	var form = document.createElement("form");    
	form.setAttribute("method", method);
	form.setAttribute("action", path);
	form.setAttribute("target", target);
	for(var key in params) {        
		var hiddenField = document.createElement("input");        
		hiddenField.setAttribute("type", "hidden");        
		hiddenField.setAttribute("name", key);        
		hiddenField.setAttribute("value", params[key]);        
		form.appendChild(hiddenField);    
	}
	document.body.appendChild(form);
	form.submit();
}
