// standard js for getapproved

//global functions
function show(Id){
  document.getElementById(Id).style.display = 'block';
}
function hide(Id){
  document.getElementById(Id).style.display = 'none';
}


//change tabs
function changeTab(tabId){
         if(validatenowpersonal() == true)  {
            dijit.byId('multi-TabContainer').selectChild( 'page' + tabId + '-ContentPane' );
         }
  }


function startApp(url){
    window.open(url,'ga-app','width=1000,height=700,menubar=no,status=yes,location=no,toolbar=no,scrollbars=yes');
}

//append to current class
function appendClass(Id, theClass){
   document.getElementById(Id).className = document.getElementById(Id).className + theClass;
}
//replace class altogether
function changeClass(Id, theClass){
   document.getElementById(Id).className = theClass;
}





// task specific functions


//select application type
function basicAppType(type){
  var default_id = 'company_data';

  if(type == 'company'){
     show(default_id);
  } else {
    hide(default_id);
  }
}


//add applicatants and garantors to an application
function changeApplicants(num){
  var max = 2, i = 1, default_id = 'app_';
  for(i=max;i>=1;i--){
    var e = document.getElementById(default_id + i + '_email_wrap');
    var n = document.getElementById(default_id + i + '_name');
     // if show all the elements up to the number set and hide the rest
    var reqflds = new Array("_name_first", "_name_last", "_email");
    var f, max = reqflds.length;


        if(num >= i){
           e.style.display = 'block';
           n.style.display = 'block';
           for(f = 0; f < max; f++) {
              dijit.byId('page1-' + default_id + i + reqflds[f]).attr('required', true);
           }
        } else {
           e.style.display = 'none';
           n.style.display = 'none';
           for(f = 0; f < max; f++) {
              dijit.byId('page1-' + default_id + i + reqflds[f]).attr('required', false);
           }

        }
  } //end for
}  // end function


//add garantors
function changeGuar(num){
  var max = 2, i = 1, default_id = 'gua_';
  for(i=max;i>=0;i--){
    var e = document.getElementById(default_id + i + '_email_wrap');
    var n = document.getElementById(default_id + i + '_name');
    var reqflds = new Array("_name_first", "_name_last", "_email");
    var f, max = reqflds.length;

     // if show all the elements up to the number set and hide the rest
      //alert(default_id + i + '_emailid');
    if(e!=null & n!=null){
        if(num >= i){
           e.style.display = 'block';
           n.style.display = 'block';
           for(f = 0; f < max; f++)  {
              dijit.byId('page1-' + default_id + i + reqflds[f]).attr('required', true);
           }
        } else {
           e.style.display = 'none';
           n.style.display = 'none';
           for(f = 0; f < max; f++) {
              dijit.byId('page1-' + default_id + i + reqflds[f]).attr('required', false);
           }
        }
   }
  } //end for
}  // end function



 //clear the name text boxes
 function clearTB(val){
   if (val.value == 'first name' | val.value == 'middle name' | val.value == 'surname') {
         val.value = '';
   }
 }


   //clear the name text boxes
 function checkTB(val){
   if (val.value == '') {
         val.value = 'middle name';
   }
 }




//add vehichle asset
function changeMDL(val){

        if(val==false){
           hide('mdl');
         /*       dijit.byId('page1-dlnumber').attr('required', false);
           dijit.byId('page1-dlstate').attr('required', false);
           dijit.byId('page1-dlexpiry').attr('required', false);  */

         } else {
            show('mdl');
        /*    dijit.byId('page1-dlnumber').attr('required', true);
            dijit.byId('page1-dlstate').attr('required', true);
            dijit.byId('page1-dlexpiry').attr('required', true);   */
         }

}



function checkCreditHistory(val){
     if(val == 'Good' | val == 'Fantastic'){
        document.getElementById('further').style.display = 'none';
     } else {
        document.getElementById('further').style.display = 'block';
     }
}



//add extra address if homeown dif to residential
function changeHome(val){
         if(val==false){
            show('p_add_addr');
         } else {
            hide('p_add_addr');
         }
}


//show homeowner res option
function changeHomeRes(val){
         if(val=='homeowner' | val=='homebuyer'){
            show('is_property_res_wrap');
           show('hp_wrap');
           hide('rent_wrap');

           dijit.byId('page1-homepurchase').attr('required', true);
            dijit.byId('page1-rentamt').attr('required', false);
            dijit.byId('page1-ll_name').attr('required', false);
            dijit.byId('page1-ll_phone_phone').attr('required', false);


         } else {
            hide('is_property_res_wrap');
            hide('hp_wrap');
            show('rent_wrap');

            dijit.byId('page1-homepurchase').attr('required', false);
            dijit.byId('page1-rentamt').attr('required', true);
            dijit.byId('page1-ll_name').attr('required', true);
            dijit.byId('page1-ll_phone_phone').attr('required', true);
         }
}



//change employ start date
function changeMovedInDate(val){
    // 3 years ago


    var myDate = new Date;
    myDate.setDate(myDate.getDate()-1095);



   var mcon = new Array() ;
       mcon["Jan"]=0;
       mcon["Feb"]=1;
       mcon["Mar"]=2 ;
       mcon["Apr"]=3;
       mcon["May"]=4;
       mcon["Jun"]=5;
       mcon["Jul"]=6;
       mcon["Aug"]=7;
       mcon["Sep"]=8;
       mcon["Oct"]=9;
       mcon["Nov"]=10;
       mcon["Dec"]=11;

    var mymonth = document.getElementById('page1-movedin_month'),
        myyear = document.getElementById('page1-movedin_year');

       if(mymonth.value != 0 &&  myyear.value != 0 ) {

        var smonth = mcon[mymonth.value];
        var syear = myyear.value;
        thedate = new Date;
        thedate.setFullYear(syear,smonth,01);


            if(thedate > myDate)
             {
                   //  alert('Time at address is less than 6 months, please add your previous address');
                   show('prev_add_addr');
                   show('pmovedin_wrap');
                    dijit.byId('page1-prev_add_address_street_num').attr('required', true);
                    dijit.byId('page1-prev_add_address_street').attr('required', true);
                    dijit.byId('page1-prev_add_address_suburb').attr('required', true);
                    dijit.byId('page1-prev_add_address_pc').attr('required', true);
                    dijit.byId('page1-prev_add_address_state').attr('required', true);
                    dijit.byId('page1-prev_add_address_country').attr('required', true);
            }  else {
                 // alert('Time at address is MORE than 6 months');
                 hide('prev_add_addr');
                 hide('pmovedin_wrap');
                    dijit.byId('page1-prev_add_address_street_num').attr('required', false);
                    dijit.byId('page1-prev_add_address_street').attr('required', false);
                    dijit.byId('page1-prev_add_address_suburb').attr('required', false);
                    dijit.byId('page1-prev_add_address_pc').attr('required', false);
                    dijit.byId('page1-prev_add_address_state').attr('required', false);
                    dijit.byId('page1-prev_add_address_country').attr('required', false);
            }

      }
}




//change employ start date
function changeEmployDate(val, num){
    // 3 years ago
	var days = 1095;
	var days = 1;


    var empfields = new Array("_company",
                              "_position",
                              "c_add_address_street_num",
                              "c_add_address_street",
                              "c_add_address_street",
                              "c_add_address_suburb",
                              "c_add_address_pc",
                              "c_add_address_state",
                              "c_add_address_country",
                              "company_phone",
                              "payroll_phone",
                              "payroll_contact");

   var mcon = new Array() ;
       mcon["Jan"]=0;
       mcon["Feb"]=1;
       mcon["Mar"]=2 ;
       mcon["Apr"]=3;
       mcon["May"]=4;
       mcon["Jun"]=5;
       mcon["Jul"]=6;
       mcon["Aug"]=7;
       mcon["Sep"]=8;
       mcon["Oct"]=9;
       mcon["Nov"]=10;
       mcon["Dec"]=11;

    var mymonth = document.getElementById('page2-employ_' + num + '_commenced_month'),
        myyear = document.getElementById('page2-employ_' + num + '_commenced_year');



       if(mymonth.value != 0 &&  myyear.value != 0 ) {

        var smonth = mcon[mymonth.value];
        var syear = myyear.value;
        thedate = new Date;
        thedate.setFullYear(syear,smonth,01);

            var myDate = new Date;
            myDate.setDate(myDate.getDate()-days);
            var i, max = empfields.length, base = 'page2-employ_' + (num + 1), myid ;

            if(thedate > myDate)
             {
                     //alert('Time at job is less than 3 years, please add your previous employment');

                     if(num < 5){
                        show('employ_' + (num + 1) );

                        //set all to req
                        for(i=0; i < max; i++) {
                             myid = base +  empfields[i];
                             dijit.byId( myid).attr('required', true);


                        }
                     }
            }  else {
                     hide('employ_' + (num + 1) );
                        //set all to not req
                        for(i=0; i < max; i++) {
                           myid = base +  empfields[i];
                           dijit.byId( myid).attr('required', false);

                        }
            }


        }  // end if not == month/year  both selected





}





//altenate salary type
function changeSalary(val){
         if(val==false){
            show('income_ammount_wrap');
            hide('income_net_ammount_wrap');
         } else {
            show('income_net_ammount_wrap');
            hide('income_ammount_wrap');
         }
}


//add accountant fieldds
function changeAccountant(val){
         if(val==false){
           hide('isaccountant');
         } else {
           show('isaccountant');
         }
}

//add shares fields
function changeShares(val){
         if(val==false){
           hide('shares');
         } else {
           show('shares');
         }
}

//add investment property
function changeIP(val){
         if(val==false){
           hide('iprop_wrap');
         } else {
           show('iprop_wrap');
         }
}


//add vehichle asset
function changeVA(val){

   var max = 5, i = 1, default_id = 'veh_';
  for(i=max;i>=0;i--){
    var e = document.getElementById(default_id + i + '_wrap');
     // if show all the elements up to the number set and hide the rest
    if(e!=null){
        if(val >= i){
           e.style.display = 'block';
        } else {
           e.style.display = 'none';
        }
   }
  } //end for



}



//add personal loan
function changePL(val){
         if(val==false){
           hide('loan_1');
         } else {
           show('loan_1');
         }
}

//add other loan
function changeOL(val){
         if(val==false){
           hide('loan_2');
         } else {
           show('loan_2');
         }
}


//change amount of cc listed
function changeCC(val){

  var max = 10, i = 1, default_id = 'card_';
  for(i=max;i>=0;i--){
    var e = document.getElementById(default_id + i + '_wrap');
     // if show all the elements up to the number set and hide the rest
    if(e!=null){
        if(val >= i){
           e.style.display = 'block';
        } else {
           e.style.display = 'none';
        }
   }
  } //end for

}

//employment source
function changeEmp(val, fld){

        //if self employed
        if(val == 'self'){
         show(fld + 'selfemp_abn_wrap');
        }   else  {
         hide(fld + 'selfemp_abn_wrap');
        }
        /*if unemployed
        if(val != 'unemployed'){
         hide(fld + '_commenced_wrap');
        }  else  {
         show(fld + '_commenced_wrap');
        }
          */
}

//add another income source
function addIncome(val){

         show('src'+val);

}






/*
 VALIDATION FUNCTIONS GO HERE
 */


 function checkform ( form )  {


 /*
  // ** START **
  if (document.getElementById('page1-privacycheck').checked == false) {
    alert( "Please agree to the terms." );
    return false ;
  }
  */


  // ** END **
  return true ;
}




    function validatenow() {


              //validation fields
                var form = dijit.byId("start");
                if (!form.validate()) {
                   alert('Please complete all the required fields');
                    return false;
                } else {
                //privacy check
                /*
                if(document.getElementById('page1-privacycheck').checked == false){
                     alert('You must agree to the privacy terms to continue');
                     return false;
                 }
                 */
                  return true;
                }

            }



  function dojo_validate(formid){
                var form = dijit.byId(formid);
               if(!form.validate()) {
                  return false;
                } else {
                  return true;
                }
  }


     function validateMulti() {
                //form in question... by id

                 var errors = new Array();

               //validation fields


                var d = document.getElementById('page1-dob_day'),
                    m = document.getElementById('page1-dob_month'),
                    y = document.getElementById('page1-dob_year');
                var gf = document.getElementById('page1-gender-female'), gm = document.getElementById('page1-gender-male');
                var rb = document.getElementById('page1-res-board'),
                    rhb = document.getElementById('page1-res-homebuyer'),
                    rr = document.getElementById('page1-res-rent'),
                    rho = document.getElementById('page1-res-homeowner');
                var mim = document.getElementById('page1-movedin_month'),
                    miy = document.getElementById('page1-movedin_year');
                var commonth = document.getElementById('page2-employ_1_commenced_month'),
                    commonth = document.getElementById('page2-employ_1_commenced_year') ;

                if(d != null){
                // if personal page   (determined by dob_day field being present)

                   if(mim.value == 'Month' | miy.value == 'Year'){
                      errors[3] = 'Invalid moved in date';
                      dijit.byId('page1-movedin_month').focus();
                   }
                   if(d.value == 'Day' | m.value == 'Month' | y.value == 'Year'){
                      errors[0] = 'Invalid Date of Birth';
                      dijit.byId('page1-dob_day').focus();
                   }
                   if(gm.checked == false && gf.checked == false){
                      errors[1] = 'Invalid Gender';
                      document.getElementById('page1-gender-male').focus();
                   }
                   if(rr.checked == false && rb.checked == false && rhb.checked == false && rho.checked == false){
                      errors[2] = 'Invalid Residential Status';
                      document.getElementById('page1-res-rent').focus();
                   }

                //end if personal page
                }
                if(commonth != null){
                // if employ page
                  for(i=1;i<=5;i++){
                  //check the commenced dates
                    if(document.getElementById('employ_' + i ).style.display != 'none'){
                        if(document.getElementById('page2-employ_' + i + '_commenced_month').value == 'Month'){
                              errors[0] = 'Invalid Commencement Date';
                              dijit.byId('page2-employ_' + i + '_commenced_month').focus();
                        }
                        if( document.getElementById('page2-employ_' + i + '_commenced_year').value == 'Year' ){
                              errors[0] = 'Invalid Commencement Date';
                              dijit.byId('page2-employ_' + i + '_commenced_year').focus();
                           }
                    }
                  }
                  //end if employ page
                }




                 //ERRORS ...
                  //errors exist
                   if(errors.length > 0){
                     var i, errormsg = 'The following errors have been detected: \n\r \n\r';
                     for(i=0;i<errors.length;i++){
                       if( errors[i] ){ errormsg =  errormsg + '- ' + errors[i] + ' \n\r'; }
                     }
                     errormsg =  errormsg + '\n\r' + 'Please correct the above then click Save \n\r' ;
                     alert(errormsg);
                     return false;   //return false on the form submit
                   }



               // this must be after the other errors due to it forcing a submit if returns true
               if( !dijit.byId('multi').validate() ) {
                   alert('Missing Required Fields');
                   return false;
                }

      }


     //basic validation
     function thisvalidatenow(formid) {
               var form = dijit.byId(formid);
                if (!form.validate()) {
                   alert('Please complete all the required fields');
                    return false;
                } else {

                  return true;
                }

     }
