
var submitted = false;

function radio_check(obj)
{
// set var radio_choice to false
var radio_choice = false;

// Loop from zero to the one minus the number of radio button selections
for (counter = 0; counter < obj.length; counter++)
{
// If a radio button has been selected it will return true
// (If not it will return false)
if (obj[counter].checked)
radio_choice = true; 
}

if (!radio_choice)
{
return (false);
}
return (true);
}

  function checkBonus(f) {

    var obj = document.getElementById('bonus_details');
    if(f.value == 1) {
      obj.style.display="";
    } else {
      obj.style.display="none";
    }

  }


  function checkChecking(f) {

    var obj = document.getElementById('savings');
    if(f.value == 'SAVINGS') {
      obj.style.display="";
    } else {
      obj.style.display="none";
    }
  }

  function doFocus(focus,obj) {
	  if(!focus) {
	    obj.focus();
		return true;
      } else {
	    return focus;
	  }
	}
	
	function validate(f) {


	  if(submitted) {
	    alert('\nPlease wait, we are processing your application\n');
	    return false;
	  }

	  if(f.name == "form1") 
	    return validate1(f);
	  else if(f.name == "form2")
            return validate2(f);
	  else if(f.name == "form3")
            return validate3(f);
	  else {
	    alert('error');return false;
	  }
	}
	
  function validate1(f) {

	  
	
	  var str = '';
	  var focus = false;


	  if(f.salutation.value == '') {
	    str += 'Please Select your proper Salutation (mr,mrs,ms)\n';
		focus = doFocus(focus,f.salutation);
		
	  } 

	  if(f.fname.value == '' || f.fname.value.length < 2) {
	    str += 'Please enter your First Name\n';
		focus = doFocus(focus,f.fname);
		
	  } 
	  
	  if(f.lname.value == '' || f.lname.value.length < 2) {
	    str += 'Please enter your Last Name\n';
		focus = doFocus(focus,f.lname);
	  }
	  /*
	  if(f.address.value == '') {
	    str += 'Please enter your Street Address\n';
		focus = doFocus(focus,f.address);
	  }
	  */
	  if(f.zip.value == '' || f.zip.value.length != 5) {
	    str += 'Please enter your Zip Code\n';
		focus = doFocus(focus,f.zip);
	  }

	  else if(!IsNumeric(f.zip.value)) {
	    str += 'Please fix your Zip Code. Please make sure its just numbers\n';
		focus = doFocus(focus,f.zip);
	  }
	  
	  if(f.phone1_1.value == '' || f.phone1_2.value == '' || f.phone1_3.value == '' ||
	       f.phone1_1.value.length != 3 || f.phone1_2.value.length != 3 || f.phone1_3.value.length != 4 ||
		!IsNumeric(f.phone1_1.value) || !IsNumeric(f.phone1_2.value) || !IsNumeric(f.phone1_3.value)) {
	    str += 'Please enter your Home Phone Number\n';
		focus = doFocus(focus,f.phone1_1);
	  }
	  
	  if(f.email.value == '') {
	    str += 'Please enter your Email\n';
		focus = doFocus(focus,f.email);
	  }

	  else if(!isValidEmail(f.email.value)) {
	    str += 'Please enter a valid Email Address\n';
		f.email.value = '';
		focus = doFocus(focus,f.email);
	  }

	  if(f.dobM.value == '') {
	    str += 'Please enter the Month you were Born\n';
		focus = doFocus(focus,f.dobM);
	  }
	  if(f.dobD.value == '') {
	    str += 'Please enter the Day you were Born\n';
		focus = doFocus(focus,f.dobD);
	  }
	  if(f.dobY.value == '') {
	    str += 'Please enter the Year you were Born\n';
		focus = doFocus(focus,f.dobY);
	  }
	  
	  

/*
	  if(f.employee.value == '') {
	    str += 'Please enter the company where you are employed\n';
		focus = doFocus(focus,f.employee);
	  }
	  if(f.income.value == '') {
	    str += 'Please enter how much you make a year\n';
		focus = doFocus(focus,f.income);
	  }
	  

	  if(f.howpaid.value == '') {
	    str += 'Please Select how you get Paid\n';
		focus = doFocus(focus,f.howpaid);
	  }
	  if(f.payfreq.value == '') {
	    str += 'Please Select how often you get Paid\n';
		focus = doFocus(focus,f.payfreq);
	  }
*/


	  if(f.citizen.value == '') {
	    str += 'Please Select Whether you are a US Citizen\n';
		focus = doFocus(focus,f.citizen);
	  }

	  else if(f.citizen.value != '1') {
	    str += 'You must be a US Resident to be consitered for a payday loan\n';
		focus = doFocus(focus,f.citizen);
	  }




	  if(str != '') {
	    alert(str);
		return false;
		
	  } else {
	    submitted = true;
  	    f.submit.value = '   Processing ...   ';

	    return true;
	  }
	}


  function checkPayDate(f) {

    var t0 = new Date()
    var t1 = new Date();
    t1.setFullYear(f.nextpay1Y.value,f.nextpay1M.value-1,f.nextpay1D.value);

    var t2 = new Date();
    t2.setFullYear(f.nextpay2Y.value,f.nextpay2M.value-1,f.nextpay2D.value);

    var focus = false;
    var str = ''; 

    if(t1 < t0) {
      str += 'The value of your next pay day is in the past.\n';
      focus = doFocus(focus,f.nextpay1M);
    }


    if(t2.valueOf() <= t1.valueOf()) {
      str += 'The value of your second next pay day should be after your next payday.\n';
      str += '\nFor example, if you are paid weekly, and the next time you are paid is on Thursday the ';

      var payday = new Date();

      payday.setDate(payday.getDate() + 4  -t0.getDay());
      var payday7 = payday.getDate() + 7;
      payday2 = new Date();
      payday2.setDate(payday.getDate() + 7);
      str += payday.getDate() + ' then your second next payday should be the Thursday the ' + payday2.getDate();

      focus = doFocus(focus,f.nextpay2M);

    }

    if(str != '') {
      alert(str);
      return false;
    } else {
      return true;
    }
  }


  function validate2(f) {
	
	  var str = '';
	  var focus = false;

	  if(f.phone2_1.value == '' || f.phone2_2.value == '' || f.phone2_3.value == '' ||
	       f.phone2_1.value.length != 3 || f.phone2_2.value.length != 3 || f.phone2_3.value.length != 4 ||
		!IsNumeric(f.phone2_1.value) || !IsNumeric(f.phone2_2.value) || !IsNumeric(f.phone2_3.value)) {
	    str += 'Please enter your Work Phone Number\n';
		focus = doFocus(focus,f.phone2_1);
	  }



          if(f.besttime.value == '') {
            str += 'Please enter the Best time to Call\n';
                focus = doFocus(focus,f.besttime);
          }

	  if(f.income_type.value == '') {
	    str += 'Please Select Where Your Income Comes From (employment,benefits,etc)\n';
		focus = doFocus(focus,f.income_type);
		
	  } 


	  if(f.employee.value == '') {
	    str += 'Please enter the company where you are employed\n';
		focus = doFocus(focus,f.employee);
	  }
	  if(f.income.value == '') {
	    str += 'Please enter how much you make a year\n';
		focus = doFocus(focus,f.income);
	  }

	  else if(f.income.value.length < 4) {
 	    str += 'Are you sure you entered the correct number? It should be your annual income.\n';
		focus = doFocus(focus,f.income);
	    
 	  }

	  if(f.howpaid.value == '') {
	    str += 'Please Select how you get Paid\n';
		focus = doFocus(focus,f.howpaid);
	  }
	  if(f.payfreq.value == '') {
	    str += 'Please Select how often you get Paid\n';
		focus = doFocus(focus,f.payfreq);
	  }

	  if(f.nextpay1M.value == '') {
	    str += 'Please Select the Month of your next pay day\n';
		focus = doFocus(focus,f.nextpay1M);
	  }
	  if(f.nextpay1D.value == '') {
	    str += 'Please Select the Day of your next pay day\n';
		focus = doFocus(focus,f.nextpay1D);
	  }
	  if(f.nextpay1Y.value == '') {
	    str += 'Please Select the Year of your next pay day\n';
		focus = doFocus(focus,f.nextpay1Y);
	  }

	  if(f.nextpay2M.value == '') {
	    str += 'Please Select the Month of your 2nd next pay day\n';
		focus = doFocus(focus,f.nextpay2M);
	  }
	  if(f.nextpay2D.value == '') {
	    str += 'Please Select the Day of your 2nd next pay day\n';
		focus = doFocus(focus,f.nextpay2D);
	  }
	  if(f.nextpay2Y.value == '') {
	    str += 'Please Select the Year of your 2nd next pay day\n';
		focus = doFocus(focus,f.nextpay2Y);
	  }
	  


	  if(f.employerlength.value == '') {
	    str += 'Please Select how long you\'ve been employed at your current place of work\n';
		focus = doFocus(focus,f.employerlength);
	  }


          if(!radio_check(f.military)) {
	    str += 'Please select whether you or your spouse are a member of the Armed Forces\n';
//	    focus = doFocus(focus,f.military);
	  }

          if(f.address.value == '') {
            str += 'Please enter your Street Address\n';
                focus = doFocus(focus,f.address);
          }

	  if(f.ownrent.value == '') {
	    str += 'Please Select Whether you Own or Rent your Home\n';
		focus = doFocus(focus,f.ownrent);
		
	  } 

	  if(f.address_length.value == '') {
	    str += 'Please Select How Long You\'ve Been at Your Current Home\n';
		focus = doFocus(focus,f.address_length);
		
	  } 


          if(f.ref1_name.value == '') {
            str += 'Please enter a name for your 1st Reference\n';
                focus = doFocus(focus,f.ref1_name);
          }

          if(f.ref1_phone_1.value == '' || f.ref1_phone_2.value == '' || f.ref1_phone_3.value == '' ||
		f.ref1_phone_1.value.length != 3 || f.ref1_phone_2.value.length != 3 || f.ref1_phone_3.value.length != 4) {
            str += 'Please enter a phone number for your 1st Reference\n';
                focus = doFocus(focus,f.ref1_phone_1);
          }

          if(f.ref1_relationship.value == '') {
            str += 'Please select how you know your 1st Reference\n';
                focus = doFocus(focus,f.ref1_relationship);
          }

          if(f.ref2_name.value == '') {
            str += 'Please enter a name for your 2nd Reference\n';
                focus = doFocus(focus,f.ref2_name);
          }

          if(f.ref2_phone_1.value == '' || f.ref2_phone_2.value == '' || f.ref2_phone_3.value == '' ||
		f.ref2_phone_1.value.length != 3 || f.ref2_phone_2.value.length != 3 || f.ref2_phone_3.value.length != 4) {
            str += 'Please enter a phone number for your 2nd Reference\n';
                focus = doFocus(focus,f.ref2_phone_1);
          }

          if(f.ref2_relationship.value == '') {
            str += 'Please select how you know your 2nd Reference\n';
                focus = doFocus(focus,f.ref2_relationship);
          }
/*
         if(!radio_check(f.extra_opt)) {
            str += 'Please select whether you want to improve your Credit Score with Fast Financial Coach\n';
            focus = doFocus(focus,f.extra_opt[0]);
          }
*/

	  if(str != '') {
	    alert(str);
		return false;
		
	  } else {
	    var res = checkPayDate(f);
	    if(res) {
   	      submitted = true;
	      f.submit.value = '   Processing ...   ';

	      return true;
	    } else return false;
	  }
	}


  function validate3(f) {
	
	  var str = '';
	  var focus = false;
	  
	  if(f.bankname.value == '') {
	    str += 'Please enter your Bank\'s Name\n';
		focus = doFocus(focus,f.bankname);
	  }
	  if(f.bank_type.value == '') {
	    str += 'Please Select whether your Bank Account is Checking or Savings\n';
		focus = doFocus(focus,f.bank_type);
	  }
	
	  if(f.bank_type.value == 'CHECKING') {
	    f.has_checking.value = '';
	  }

	  if(f.aba.value == '' || f.aba.value.length != 9) {
	    str += 'Please Enter Your Account\'s ABA\n';
	    focus = doFocus(focus,f.aba);
	  } else if(!IsNumeric(f.aba.value)) {
            str += 'Please Enter a Valid ABA Number. It should only be numbers\n';
    	    focus = doFocus(focus,f.aba);
          }


	  if(f.bankaccount.value == '' || f.bankaccount.value.length < 2 || f.bankaccount.value.length > 17) {
	    str += 'Please Enter your Bank Account\'s Number\n';
	    focus = doFocus(focus,f.bankaccount);
	  } else if(!IsNumeric(f.bankaccount.value)) {
            str += 'Please Enter a Valid Bank Account Number. It should only be numbers\n';
    	    focus = doFocus(focus,f.bankaccount);
          }

	  if(f.ssn.value == '' || f.ssn.value.length != 9) {
	    str += 'Please Enter your SSN\n';
		focus = doFocus(focus,f.ssn);
	  } else if(!IsNumeric(f.ssn.value)) {
	      str += 'Please Enter only the numbers of your SSN, (no dashes)\n';
	      focus = doFocus(focus,f.ssn);

          }


	  if(f.licence.value == '') {
	    str += 'Please Enter your Drivers Licence or State ID Number\n';
		focus = doFocus(focus,f.licence);
	  }

	  if(!f.legal_opt.checked) {
	    str += 'You must accept the terms and conditions to be eligible for this payday loan\n';
		focus = doFocus(focus,f.legal_opt);
	  }

	  


	  if(str != '') {
	    alert(str);
		return false;
		
	  } else {
            submitted = true;
            f.submit.value = '   Processing ... (may take 60s)   ';

	    return true;
	  }
	}




  function tabber(from,to,length) {
  	if(document.getElementById(from).value.length >= length)
    	document.getElementById(to).focus();
  }


 function mypopup(url,w,h)
 {
  var mywindow = window.open (url,  "mywindow","location=1,status=1,scrollbars=1, width=" + w + ",height=" + h);
//  mywindow.moveTo(0,0);
 } 
/*
function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  strEmail = document.forms[0].email.value;

   // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1) 
    {
      return false;
    } 
    return true; 
}
*/
function isValidEmail(email){ 
    var RegExp = /^((([A-Z,a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([A-Z,a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([A-Z,a-z]|[0-9])([A-Z,a-z]|[0-9]|\-){0,61}([A-Z,a-z]|[0-9])\.))*([A-Z,a-z]|[0-9])([A-Z,a-z]|[0-9]|\-){0,61}([A-Z,a-z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/ 
    if(RegExp.test(email)){ 
        return true; 
    }else{ 
        return false; 
    } 
} 

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}
