function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;	if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	 if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_changeProp(objName,x,theProp,theValue) { //v6.0
	var obj = MM_findObj(objName);
	if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
		if (theValue == true || theValue == false)
			eval("obj."+theProp+"="+theValue);
		else eval("obj."+theProp+"='"+theValue+"'");
	}
}

function leavingWebsite()
{
	return confirm("You are leaving the 1st Financial Bank website! Are you sure you would like to continue?");
}

function fdicMessage()
{
	return confirm("Deposits at FDIC-insured institutions are now insured up to at least $250,000 per depositor through December 31, 2013. On January 1, 2014, the standard insurance amount will return to $100,000 per depositor for all account categories except for IRAs and other certain retirement accounts which will remain at $250,000 per depositor. (This supersedes the October 3, 2008 changes.)\r\n\r\nThe extension announced on May 20,2009, does not apply to the Transaction Account Guarantee Program. The unlimited coverage under the Transaction Account Guarantee Program is only in effect for depositors at participating institutions through December 31,2009.");
}

function changeClass(divID,className) {
	var currElem = document.getElementById(divID);
		currElem.setAttribute("class",className);
		currElem.setAttribute("className",className);
		return;
}

function option_submit_form(form_name, select)
{
	var item = select.options[select.selectedIndex].value;
	var form = document.getElementById(form_name);
	form.action = form.action + "/" + item;
	form.submit();
}

function search_for(form)
{
	var search_text = document.getElementById("search");
	form.action = form.action + "/" + search_text.value.replace(" ","/");
	form.submit();
	return true;
}

function check_cart(form)
{
	var one_filled_out = false;
	for (i=0; i<form.elements.length; i++) {
		if (form.elements[i].id != "") {
			if (!form.elements[i].disabled && form.elements[i].value == "") {
				alert("Please enter a value for the enabled items...");
				return false;
			} else if (!form.elements[i].disabled) {
				one_filled_out = true;
			}
		}
	}
	
	if (!one_filled_out) {
		alert("Please select an item to add to the cart...");
		return false;
	}
	return true;
}

function add_to_order(prod_id)
{
	// Enable the correct box
	var box = document.getElementById(prod_id);
	var div = document.getElementById(prod_id + "_div");
	if (box.disabled) {
		box.disabled = false;
		div.className = "selectorBox_checkOn";
	} else {
		box.disabled = true;
		div.className = "selectorBox_checkOff";
	}
}

function measure_window(box, id)
{
	// Find width / height boxes
	var width = document.getElementById(id + "_Width");
	var height = document.getElementById(id + "_Height");
	if (box.checked) {
		width.disabled = false;
		height.disabled = false;
	} else {
		width.disabled = true;
		height.disabled = true;
	}
}

function copy_billing(checkbox)
{
	var fields = new Array("name", "address1","address2","city","zip");
	if (checkbox.checked) {
		for (var i=0; i<fields.length; i++) {
			document.getElementById("s_"+ fields[i] +"").value = document.getElementById("b_"+ fields[i] +"").value;
		}
		document.getElementById("s_state").selectedIndex = document.getElementById("b_state").selectedIndex;
	} else {
		for (var i=0; i<fields.length; i++) {
			document.getElementById("s_"+ fields[i] +"").value = "";
		}
		document.getElementById("s_state").selectedIndex = 0;
	}
}

// Copied from http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256C720080D723
function isArray(obj){
    if (obj == undefined || obj.constructor.toString().indexOf("Array") == -1) 
        return false;
    else 
        return true;
}

// REGEX: http://regexlib.com/REDetails.aspx?regexp_id=607
// Copied from http://www.zparacha.com/phone_number_regex/
function isPhoneNumber(num)
{
   if(num.search(/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/)==-1)
   {
		return false;
   }
   else
   {
   		return true;
   }
}

// Copied from http://javascript.internet.com/forms/currency-format.html
function formatCurrency(num){
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num)) 
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10) 
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) 
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
        num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}

// Email Validation Javascript
// copyright 23rd March 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function validateEmail(addr,man,db) {
if (addr == '' && man) {
   if (db) alert('email address is mandatory');
   return false;
}
if (addr == '') return true;
var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
for (i=0; i<invalidChars.length; i++) {
   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
      if (db) alert('email address contains invalid characters');
      return false;
   }
}
for (i=0; i<addr.length; i++) {
   if (addr.charCodeAt(i)>127) {
      if (db) alert("email address contains non ascii characters.");
      return false;
   }
}

var atPos = addr.indexOf('@',0);
if (atPos == -1) {
   if (db) alert('email address must contain an @');
   return false;
}
if (atPos == 0) {
   if (db) alert('email address must not start with @');
   return false;
}
if (addr.indexOf('@', atPos + 1) > - 1) {
   if (db) alert('email address must contain only one @');
   return false;
}
if (addr.indexOf('.', atPos) == -1) {
   if (db) alert('email address must contain a period in the domain name');
   return false;
}
if (addr.indexOf('@.',0) != -1) {
   if (db) alert('period must not immediately follow @ in email address');
   return false;
}
if (addr.indexOf('.@',0) != -1){
   if (db) alert('period must not immediately precede @ in email address');
   return false;
}
if (addr.indexOf('..',0) != -1) {
   if (db) alert('two periods must not be adjacent in email address');
   return false;
}
var suffix = addr.substring(addr.lastIndexOf('.')+1);
if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
   if (db) alert('invalid primary domain in email address');
   return false;
}
return true;
}

function valid_characters(str, valid_chars)
{
    var valid = true;
    if (str == "") {
        return false;
    }
    for (var i = 0; i <= str.length - 1; i++) {
        if (valid_chars.indexOf(str.charAt(i)) == -1) {
            valid = false;
        }
    }
    return valid;
}

// Function to determine if the element should be an error
function check_all(conditional, value, optional, length)
{
	if (optional && value.length<1) {
		return true;
	} else {
		// We must check this entry for validity
		if (value.length>=length && conditional) {
			return true;
		} else {
			return false;
		}
	}
}

var error_array = new Array();
var error_str = "";

function error_check_form()
{
	var heir = false;
	var array = error_array;
	var error = false;
	error_str = "";
    if (isArray(array)) {
        for (var i = 0; i < array.length; i++) {
			// Field is slot one and data requirement is field two
			var element = array[i]["element"];
			var type = array[i]["type"];
			var validation = array[i]["validation"];
			var name = array[i]["name"];
			var temp_other = array[i]["other"].split(",");
			var length = array[i]["length"];
			var optional = array[i]["optional"];
			var other = new Array();
			for (var j=0; j<temp_other.length; j++) {
				if (temp_other[j] != "") {
					other[j] = document.getElementById(temp_other[j]);
					if (other[j] == undefined) {
						alert("Other Field in " + name + " has an invalid field listed: " + temp_other[j]);
						return true;
					}
				}
			}
			var value = "";
			
			if (!element.disabled) {
				switch (type){
					case "password":
					case "text":
						value = element.value;
						break;
					case "select":
						value = element.options[element.selectedIndex].value;
						break;
				}
				
				switch (validation) {
					case "email":
						if (check_all(validateEmail(value, optional, false), value, optional, length)) {
							element.className = "input";
						}
						else {
							element.className = "error";
							if (length > 1) {
								error_str += name + " field requires a valid email with a miniumum length of "+ length +" <br />";
							} else {
								error_str += name + " field requires a valid email<br />";
							}
							error = true;
						}
						break;
					case "required":
						if (check_all(true, value, optional, length)) {
							element.className = "input";
						}
						else {
							element.className = "error";
							if (length > 1) {
								error_str += name + " field is required with a miniumum length of "+ length +" <br />";
							} else {
								error_str += name + " field is required<br />";
							}
							error = true;
						}
						break;
					case "password":
						if (check_all(value == other[0].value, value, optional, length)) {
							element.className = "input";
							other[0].className = "input";
						} else {
							element.className = "error";
							other[0].className = "error";
							if (length > 1) {
								error_str += name + " field is required with a minimum length of " + length + "and must match<br />";
							} else {
								error_str += name + " field is required and must match<br />";
							}
							error = true;
						}
						break;
					case "phone":
						if (check_all(isPhoneNumber(value), value, optional, length)) {
							element.className = "input";
						}
						else {
							element.className = "error";
							error_str += name + " requires a phone number entered as: 555-555-1234<br />";
							error = true;
						}
						break;
					case "radio":
						var other_elements = array[i][2];
						var one_checked = element.checked;
						
						// One of the elements must be selected or we have an error
						// Cycle through elements to find one selected
						for (var j = 0; j < other_elements.length; j++) {
							if ($(other_elements[j]).checked) {
								one_checked = true;
							}
						}
						
						// If one is selected were good, else we have an error
						if (one_checked) {
							element.className = "input";
							
							// Cycle through other elements to make as correct
							for (var j = 0; j < other_elements.length; j++) {
								$(other_elements[j]).className = "input";
							}
						}
						else {
							error = true;
							
							element.className = "error";
							error_str += name + " requires that a selection is made<br />";
							// Cycle through other elements to make as errors
							for (var j = 0; j < other_elements.length; j++) {
								$(other_elements[j]).className = "error";
							}
						}
						break;
					case "numbertext":
					case "number":
						if (check_all(!isNaN(value), value, optional, length)) {
							element.className = "input";
						}
						else {
							element.className = "error";
							if (length > 1) {
								error_str += name + " field is required to be numeric with a length of "+length+" numbers<br />";
							} else {
								error_str += name + " field is required to be numeric<br />";
							}
							error = true;
						}
						break;
					case "text":
						if (check_all(valid_characters(value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "), value, optional, length)) {
							element.className = "input";
						}
						else {
							element.className = "error";
							if (length > 1) {
								error_str += name + " field is required to be alphabetical with a length of "+length+" characters<br />";
							} else {
								error_str += name + " field is required to be alphabetical only<br />";
							}
							error = true;
						}
						break;
				}
			}
		}
    } else {
		alert("No valid error checking array...");
		return false;
	}
	var error_display = document.getElementById("error_display");
	
	if (error_display == undefined) {
		alert("Error Display needs to be added...");
		return false;
	}
	
    if (error && !heir) {
		error_display.innerHTML = error_str;
		//error_display.innerHTML = "Please enter the correct information for fields indicated in red below:";
    } else {
		error_display.innerHTML = "";
	}
    return !error;
}


	
function error_check_init(form) {
	var test = "";
	var num_errors_checked = 0;
	var test = error_array;
	for (var i=0; i<form.elements.length; i++) {
		// Find out custom attributes
		var element = form.elements[i];
		var display = "";
		var validation = "";
		var type = "";
		var optional = false;
		var other = "";
		var length = 1;
		for (var j=0; j<element.attributes.length; j++) {
			switch (element.attributes[j].nodeName) {
				case "display":
					display = element.attributes[j].nodeValue;
					break;
				case "validation":
					validation = element.attributes[j].nodeValue;
					break;
				case "type":
					type = element.attributes[j].nodeValue;
					break;
				case "optional":
					optional = true;
					break;
				case "other":
					other = element.attributes[j].nodeValue;
					break;
				case "minlength":
					length = element.attributes[j].nodeValue;
					break;
				default:
					break;
			}
		}
		
		if (type == "") {
			type = element.tagName.toLowerCase();
		}
		
		if (validation != "") {
			error_array[num_errors_checked] = new Array();
			error_array[num_errors_checked]["element"] = element;
			error_array[num_errors_checked]["type"] = type;
			error_array[num_errors_checked]["validation"] = validation;
			error_array[num_errors_checked]["optional"] = optional;
			error_array[num_errors_checked]["other"] = other;
			error_array[num_errors_checked]["length"] = length;
			if (display != "") {
				error_array[num_errors_checked]["name"] = display;
			} else {
				var name = element.name.replace("_", " ");
				
				// If the name is part of an array, clean it up...
				var char_at = name.indexOf("[");
				if (char_at != -1) { // Item is part of an array!
					name = name.replace("[", " ");
					var number = parseInt(name.charAt(char_at+1));
					name = name.substr(0,char_at+1);
					name += "#" + (number+1);
				}
				error_array[num_errors_checked]["name"] = name;
			}
			num_errors_checked += 1;
		}
	}
	if (num_errors_checked>0) {
		// Modify form to do check before submission
		form.onsubmit = function()
		 {
		 	if (typeof(other_checks) != "undefined") {
				if (error_check_form()) {
					return other_checks();
				} else {
					return false;
				}
			}
			else {
				return error_check_form();
			}
		}
	}
}