function checkMin(minimum, formname, fieldName, fieldDesc) {
    var error = '';
    var current = eval('document.' + formname + '.' + fieldName + '.value');
    if (current != '') {
        if (parseInt(current) < minimum) {
            error += '- ' + fieldDesc + ' must be greater than ' + minimum + '\n';
        }
    }
    return error;
}


function check(formname) {
  var minimumGrossMonthly = 1200;
   
  var errors = '';
  errors += checkText(formname, 'first_name', 'First name');
  errors += checkText(formname, 'last_name', 'Last name');
  errors += checkText(formname, 'address', 'Address');
  errors += checkText(formname, 'city', 'City');
  errors += checkText(formname, 'state_province', 'State');
  errors += checkText(formname, 'zip_postal_code', 'Zip code');
  errors += checkZIP(formname, 'zip_postal_code', 'Zip code');
  errors += checkText(formname, 'home_phone', 'Home phone');
  errors += checkUSPhone(formname, 'home_phone', 'Home phone');
//  if(document.getElementById('work_phone').value.length > 0) {
//	errors += checkUSPhone(formname, 'work_phone', 'Work phone');
//  }
//  if(document.getElementById('mobile_phone').value.length > 0) {
//	errors += checkUSPhone(formname, 'mobile_phone', 'Cell phone');
//  }
  errors += checkText(formname, 'email', 'E-mail'); 
  errors += checkEmail(formname, 'email', 'E-mail');


  if ((checkNum(formname, 'years_at_res', 'How long at this address?') != '') && (checkNum(formname, 'months_at_res', 'How long at this address?') != ''))
  {
    errors += checkNum(formname, 'years_at_res', 'How long at this address?');
  }
  if ((checkText(formname, 'years_at_res', 'How long at this address?') != '') && (checkText(formname, 'months_at_res', 'How long at this address?') != ''))
  {
    errors += checkText(formname, 'years_at_res', 'How long at this address?');
  }
  errors += checkRadio(formname, 'type', 'Rent or own?');
  errors += checkText(formname, 'monthly_payment', 'Monthly payment');
  errors += checkNum(formname, 'monthly_payment', 'Monthly payment');
  errors += checkText(formname, 'bmonth', 'Date of birth - Month'); 
  errors += checkText(formname, 'bday', 'Date of birth - Day');
  errors += checkText(formname, 'byear', 'Date of birth - Year');   
  errors += checkDOB(formname, 'bmonth', 'bday', 'byear', 'Date of birth');
//  errors += checkRadio(formname, 'gender', 'Gender');
  errors += checkText(formname, 'ssn', 'Social Security #');
  errors += checkSSN(formname, 'ssn', 'Social Security #');
  errors += checkRadio(formname, 'bankruptcy', 'Declared bankruptcy?');
  errors += checkRadio(formname, 'cosigner_available', 'Cosigner available?');
  errors += checkText(formname, 'employer_name', 'Employer name');
  errors += checkText(formname, 'employer_phone', 'Employer phone');
  errors += checkUSPhone(formname, 'employer_phone', 'Employer phone');
  errors += checkText(formname, 'job_title', 'Your job title');
  errors += checkText(formname, 'years_at_emp', 'How long with this employer');
  errors += checkNum(formname, 'years_at_emp', 'How long with this employer');

  if ((checkNum(formname, 'years_at_emp', 'How long with this employer') != '') && (checkNum(formname, 'months_at_emp', 'How long with this employer') != ''))
  {
    errors += checkNum(formname, 'years_at_emp', 'How long with this employer');
  }
  if ((checkText(formname, 'years_at_emp', 'How long with this employer') != '') && (checkText(formname, 'months_at_emp', 'How long with this employer') != ''))
  {
    errors += checkText(formname, 'years_at_emp', 'How long with this employer');
  }

  errors += checkText(formname, 'gross_monthly_income', 'Monthly income');
  errors += checkNum(formname, 'gross_monthly_income', 'Monthly income');
  errors += checkMin(minimumGrossMonthly, formname, 'gross_monthly_income', 'Gross Monthly Income');
  if (errors == '')
  {
	return true;
  }	
  alert(errors);
  return false;
}
function checkText(t,L,n){var B='';if(d(eval('document.'+t+'.'+L+'.value'))==''){B='- '+n+' is Required.\n';}return B;};
function checkNum(t,L,n){var B='';if(isNaN(eval('document.'+t+'.'+L+'.value'))){B='- '+n+' Should Be A Number With No Spaces.\n';}return B;};
function checkSelect(t,I,n){var B='';if(eval('document.'+t+'.'+I+'.selectedIndex')==0){B='- '+n+' is Required.\n';}return B;};
function checkRadio(t,H,n){var B='';var G='';for(var i=0;i<eval('document.'+t+'.'+H+'.length');i++){if(eval('document.'+t+'.'+H+'[i].checked')){G+='-';}}if(G==''){B='- '+n+' is Required.\n';}return B;};
function d(s){while((s.substring(0,1)==' ')||(s.substring(0,1)=='\n')||(s.substring(0,1)=='\r')){s=s.substring(1,s.length);}while((s.substring(s.length-1,s.length)==' ')||(s.substring(s.length-1,s.length)=='\n')||(s.substring(s.length-1,s.length)=='\r')){s=s.substring(0,s.length-1);}return s;};
function checkZIP(t,L,n){var B='';var O=d(eval('document.'+t+'.'+L+'.value'));if(O.search(/\d{5}$|\d{5}-\d{4}$/)==-1)B+='- Invalid '+n+'.\n';return B;};
function checkSSN(t,L,n){var B='';var J=d(eval('document.'+t+'.'+L+'.value'));if(J.search(/^\d{3}-?\d{2}-?\d{4}$/)==-1)B+='- Invalid '+n+'.\n';return B;};
function e(c){return(c>='0'&&c<='9');};
function checkUSPhone(t,L,n){var B='';var F=d(eval('document.'+t+'.'+L+'.value'));var m='';for(i=0;i<F.length;i++){if(e(F.charAt(i))){m+=F.charAt(i);}}if(m.length!=10){B='- '+n+' must contain only 10 digits\n';}else{var j=m.slice(0,3);var M=m.slice(3,6);var z=m.slice(6,10);var v='900.911.411.456.500.222.333.444.555.666.777.999';if((v.indexOf(j)!=-1)||(j.charAt(0)=='0')||(j.charAt(0)=='1'))B+='- Invalid area code in '+n+'\n';var w='411.555.611.911';if((w.indexOf(M)!=-1)||(M.charAt(0)=='0')||(M.charAt(0)=='1'))B+='- Invalid exchange in '+n+'\n';var A=M+z;var g=true;for(i=1;i<A.length;i++)if(A.charAt(i)!=A.charAt(0)){g=false;break;}if(g)B+='- Repeating digits in '+n+' are not allowed.\n';}return B;};
function checkEmail(t,L,n){var B='';var r=d(eval('document.'+t+'.'+L+'.value'));if(r.search(/([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/)==-1)B+='- Invalid '+n+'.\n';return B;};
function checkDOB(t,C,l,N,n){var B='';var o=eval('document.'+t+'.'+C+'.value')+'/'+eval('document.'+t+'.'+l+'.value')+'/'+eval('document.'+t+'.'+N+'.value');if(isNaN(Date.parse(o))){B+='- Invalid '+n+'.\n';}else{var D=new Date();var k=new Date(o);var f=D.getFullYear()-k.getFullYear();if(f<18)B+='- You must be 18 or older - you are only '+f+' according to your '+n+'.\n';}return B;} 