//general javascript functions

//if browser is IE6 or IE7, then use a "block" display type.  This is invalid but required.
isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
isIE7 = navigator.userAgent.toLowerCase().indexOf('msie 7') != -1;
var DISPLAYTYPE;
if (isIE6 || isIE7) {
	DISPLAYTYPE = "block";
} else {
	DISPLAYTYPE = "table-row";
}

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_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location=\'"+args[i+1]+"\'");
}


//validate email address
function validateEmail(email) {
	var emailTest = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$/i;
	return emailTest.test(email);  
}

//validate ABA routing number
function validateABA(routingnum) {
	var i, n, t, s;
	s = routingnum;

	// First, remove any non-numeric characters.
	t = "";
	for (i = 0; i < s.length; i++) {
		c = parseInt(s.charAt(i), 10);
		if (c >= 0 && c <= 9)
			t = t + c;
	}

	if (t.length != 9)
		return false;

	// Now run through each digit and calculate the total.
	n = 0;
	for (i = 0; i < t.length; i += 3) {
		n += parseInt(t.charAt(i),     10) * 3
		+  parseInt(t.charAt(i + 1), 10) * 7
		+  parseInt(t.charAt(i + 2), 10);
	}

	// If the resulting sum is an even multiple of ten (but not zero),
	// the aba routing number is good.
	if (n != 0 && n % 10 == 0)
		return true;
	else
		return false;
}

//Allowing dashes as well as numbers
function isNumeric(form_value) {
    //if (form_value.match(/^\d+$/) == null)
	if (form_value.match(/^[0-9\-]{1,20}$/) == null)
        return false; 
    else 
        return true; 
}

//Display co-signer on form
var cosigndisplay = 0;
function togglecosign() {
	if (cosigndisplay == 0) {
		cosigndisplay = 1;
		document.getElementById("coInfo").style.display = DISPLAYTYPE;
		document.getElementById("coName").style.display = DISPLAYTYPE;
		document.getElementById("coButton").style.display = "none";
	} else {
		cosigndisplay = 0;
		document.getElementById("coInfo").style.display = "none";
		document.getElementById("coName").style.display = "none";
		document.getElementById("coButton").style.display = DISPLAYTYPE;
	}
}


//Sets the payment date based on the amount
function setpaymentdate() {
	amount = document.getElementById("amountField").value;
	amount = amount.replace(/\,/g,'');
	if (document.getElementById("firstPaymentDate").value == "") {
		if (amount < 80000) {
			newdate = addDays(new Date(),45);
			var newday = newdate.getDate();
			var newmonth = newdate.getMonth() + 1;
			var newyear = newdate.getFullYear();
			document.getElementById("firstPaymentDate").value = newmonth + "/" + newday + "/" + newyear;
			//document.getElementById("firstPaymentDate").value = newdate;
		} else {
		}
	}
	if (amount > 80000) {
		showprincipal();
	}
}

function showprincipal() {
/*
	document.getElementById("principal_label").style.display = DISPLAYTYPE;
	document.getElementById("principal_amount").style.display = DISPLAYTYPE;
	document.getElementById("principal_button").style.display = "none";
*/
	$("#principal_label").show();
	$("#principal_amount").show();
	$("#principal_button").hide();
}

//add days to date
function addDays(myDate,days) {
    return new Date(myDate.getTime() + days*24*60*60*1000);
}

//Open text in a textbox in a new window and print
function printtext(elem) {
	elem = elem.replace(/\n/g, "<br />");
	popup = window.open('','popup','toolbar=no,menubar=no,width=200,height=150');
	popup.document.open();
	popup.document.write("<html><head></head><body onload='print()'>");
	popup.document.write(elem);
	popup.document.write("</body></html>");
	popup.document.close();
}

//Load result of AJAX validation
function loadingAjax(id, response) {
  $("#"+id+"Loading").hide();
  $("#"+id+"Result").html(unescape(response));
  $("#"+id+"Result").fadeIn();
} //finishAjax