var global_valfield;

function _trim(s){
	return s.replace(/^\s+|\s+$/g, '');
}
function _isdate (s)
{
	return (!isNaN(new Date(s).getYear()));
}
function _isphone(s){
	var ph = s.replace(/[^0-9]+/g,'');
	var rx = /^5{3,}|^[0-9]{3}5{3,}/;
	return (ph.length==10 && !rx.test(ph));
}
function _isrent(s){
	var rnt = s.replace(/[^0-9\.\-]+/g,'');
	var rx = /^\-?[0-9]+(\.[0-9]{0,2})?$/;
	var f;
	if(rx.test(rnt)){
		f=parseFloat(rnt);
		return(!isNaN(f)&&f>=0&&f<32767); 
	}
	return false;
}
function _isssn(s1,s2,s3){
	return (s1.replace(/[^0-9]+/g,'').length==3 &&
			s2.replace(/[^0-9]+/g,'').length==2 &&
			s3.replace(/[^0-9]+/g,'').length==4);
}
function _iszip(s){
	var z = s.replace(/[^0-9]+/g,'');
	var rx = /^[0-9]{5}([0-9]{4})?$/;
	return rx.test(z);
}
function _isemail(s){
	var em = _trim(s);
	var rx = /^[a-z0-9_\-\.]+\@[a-z0-9][a-z0-9_\-\.]+\.[a-z]{2,4}$/i;
	return rx.test(em);
}
function _isvalid(s){
	var f = _trim(s);
	return (f.length>0);
}
function _isname(s){
	var n = _trim(s);
	var rx = /^[a-zA-Z][a-zA-Z'\.\- ]+$/;
	return rx.test(n);
}
function _istime(y,m){
	var v1 = parseInt(_trim(y),10);
	var v2 = parseInt(_trim(m),10);
	if(isNaN(v1))
		v1=0;
	if(isNaN(v2))
		v2=0;
	return ((v1+v2)>0);
}
function _ts(id,b){
	var s = document.getElementById(id);
	s.style.display=(b) ? "none" : "";
}
function qdtchk(t,s){
	_ts(s,_isdate(t.value));	
}
function qnmchk(t,s){
	_ts(s,_isname(t.value));
}
function qemchk(t,s){
	_ts(s,_isemail(t.value));
}
function qfchk(t,s){
	_ts(s,_isvalid(t.value));
}
function qmpchk(t,s){
	_ts(s,_isrent(t.value));
}
function qphchk(t,s){
	_ts(s,_isphone(t.value));
}
function qschk(sel,s){
	if(sel.selectedIndex>0)
		_ts(s,1);
	else
		_ts(s,0);
}
function qssnchk(s1,s2,s3,s){
	_ts(s,_isssn(s1.value,s2.value,s3.value));
}
function qtmchk(t1,t2,s){
	_ts(s,_istime(t1.value,t2.value));
}
function qzchk(t,s){
	_ts(s,_iszip(t.value));
}
function chkzip(s){
	if(_iszip(s))
		return 1;
	alert('Please enter your zip code.');	
	return 0;
}
function chkname(s,w){
	if(_isname(s))
		return 1;
	alert('Please enter your '+w+' name.');	
	return 0;
}
function chkfield(s,w){
	if(_isvalid(s))
		return 1;
	alert('Please enter your '+w+'.');	
	return 0;
}
function chkselect(s,w){
	if(s.selectedIndex>0)
		return 1;
	alert('Please enter your '+w+'.');	
	return 0;
}
function chkrent(s){
	if(_isrent(s))
		return 1;
	alert('Please enter your monthly housing payment (i.e. $500).');	
	return 0;
}
function chkphone(s){
	if(_isphone(s))
		return 1;
	alert('Please enter your phone number, including the area code.\n(This information is only used to complete your loan approval)');
	return 0;
}
function chkemail(s){
	if(_isemail(s))
		return 1;
	alert('Please enter a valid email address.\n(This information is only used to send you a loan approval)');	
	return 0;
}

function setfocus(valfield){
	global_valfield = valfield;
	setTimeout( 'setFocusDelayed()', 100 );
}
function setFocusDelayed(){
	global_valfield.select();
	global_valfield.focus();
}
function validate(f){
	if(!chkname(f.firstname.value,'first')){
		setfocus(f.firstname);
		return false;
	}
	if(!chkname(f.lastname.value,'last')){
		setfocus(f.lastname);
		return false;
	}
	if(!chkemail(f.email.value)){
		setfocus(f.email);
		return false;
	}
	if(!chkzip(f.zip.value)){
		setfocus(f.zip);
		return false;
	}
	return true;
}
function validate2(f){
	if(!chkname(f.firstname.value,'first')){
		setfocus(f.firstname);
		return false;
	}
	if(!chkname(f.lastname.value,'last')){
		setfocus(f.lastname);
		return false;
	}
	if(!chkemail(f.email.value)){
		setfocus(f.email);
		return false;
	}
	if(!chkfield(f.address.value,'residence address')){
		setfocus(f.address);
		return false;
	}
	/*if(!chkfield(f.city.value,'residence city')){
		setfocus(f.address);
		return false;
	}
	if(!chkselect(f.state,'residence state')){
		f.state.focus();
		return false;
	}*/
	if(!chkzip(f.zip.value)){
		setfocus(f.zip);
		return false;
	}
	if(!chkphone(f.phonenumber.value)){
		setfocus(f.phonenumber);
		return false;
	}
	if(!chkrent(f.rent.value)){
		setfocus(f.rent);
		return false;
	}
	if(!_istime(f.homeyears.value, f.homemonths.value)){
		alert('Please enter the amount of time you have lived at your current address.');	
		setfocus(f.homeyears);
		return false;
	}
	return true;
}
function showcalc(){
	var w=window.open('/loan-application/hourlywagecalc.html','calc','location=0,status=0,scrollbars=0,resizable=0,toolbar=0,directories=0,width=360,height=300');
	w.focus();
	return false;
}
function ssnnext(t){
	var f=document.frmdetails;
	if(t.id	=="ssn1"){
		if(t.value.length == 3){
			f.ssn2.select();
			return;
		}
	}
	if(t.id	=="ssn2"){
		if(t.value.length == 2){
			f.ssn3.select();
			return;
		}
	}
}
function shlenders(s){
	var w=window.open(s,'lenders','location=0,status=0,scrollbars=1,resizable=1,toolbar=0,directories=0,width=360,height=300');
	w.focus();
	return false;
}