/* Dynamic Application Form by Peter Naudus (LinuxLefty) */

// Note: ll_appForm is defined below the <body> tag

/*
   program
      * package
      * duration
      * time
   package
      * accomodation
   accomodation
      * duration
*/

function ll_formInit() {

   if (typeof(goFormGo) == "undefined")
      return

   var selectArray = [
      ll_appForm.course,
      ll_appForm.program,
      ll_appForm.package,
      ll_appForm.accomodation,
      ll_appForm.duration,
      ll_appForm.time
   ];

   ll_formStoreValues();

   ll_formClear(selectArray);

   ll_formInitCourse();
   ll_formInitProgram();

   ll_formAddActions();

   ll_formLoadValues();
}

function ll_formOptionsInit(field, optionsVal, optionsTxt) {
   var options = field.options
   options.length = 0;
   for (var i = 0; i < optionsVal.length; i++) {
      options[options.length] = new Option(optionsTxt[i], optionsVal[i]);
   }
   // TODO: automatically set the current value to the optionsVal[0]
}

function ll_formInitCourse() {
   var optionsVal = [
      null,
      'general',
      'business',
      'other'
   ];
   var optionsTxt = [
      '',
      'General Chinese',
      'Business Chinese',
      'Other - add to notes'
   ];
   var field = ll_appForm.course;
   ll_formOptionsInit(field, optionsVal, optionsTxt);
}

function ll_formInitProgram() {
   var optionsVal = [
      null,
      'term',
      'flexible',
      'termIntensive',
      'flexIntensive',
      'private',
      'online',
      'other'
   ];
   var optionsTxt = [
      '',
      'Term',
      'Flexible',
      'Term-Intensive',
      'Flexible-Intensive',
      'Private',
      'Online',
      'Other - add to notes'
   ];
   var field = ll_appForm.program;
   ll_formOptionsInit(field, optionsVal, optionsTxt);
   ll_formProgramAction()
   ll_formPackageAction()
}

/* This function removes all current set options */
function ll_formClear(selectArray) {

   for (var i=0; i < selectArray.length; i++) {
      var options = selectArray[i].options;
      options.length = 0;
   }
}

function ll_formAddActions() {
   ll_appForm.course.onchange = ll_formCourseAction
   ll_appForm.program.onchange = ll_formProgramAction
   ll_appForm.package.onchange = ll_formPackageAction
   ll_appForm.accomodation.onchange = ll_formAccomodationAction
}

function ll_formCourseAction() {
   ll_formProgramAction();
}

function ll_formProgramAction() {
   var optionsVal;
   var optionsTxt;
   var field;
   var skip = false;
   var value = ll_appForm.program.value;

   // Package
   switch(value) {
      case "term":
      case "flexible":
      case "termIntensive":
      case "flexIntensive":
      case "private":
      case "other":
         optionsVal = [null,'gold','silver','bronze'];
         optionsTxt = ['','Gold','Silver','Bronze'];
         break;
      case "online":
         optionsVal = ['bronze'];
         optionsTxt = ['Bronze'];
         break;
      default:
         optionsVal = optionsTxt = [];
   }
   field = ll_appForm.package;
   ll_formOptionsInit(field,optionsVal,optionsTxt);
   ll_formPackageAction();

   // Duration
   switch(value) {
      case "termIntensive":
         optionsVal = [null,'fall','spring'];
         optionsTxt = ['','Fall Semester','Spring Semester'];
         break;
      case "term":
         optionsVal = [null,'fall','spring','year','summer'];
         optionsTxt = ['','Fall Semester','Spring Semester','Academic Year','Summer Term'];
         break;
      case 'flexible':
      case 'flexIntensive':
         ll_formFlexAccomAction();
         ll_formFlexDurationAction();
         skip = true;
         break;
      case 'online':
         optionsVal = [null,'2hrs','3hrs','5hrs','10hrs','15hrs'];
         optionsTxt = ['','2 hour online','3 hour online','5 hour online','10 hour online','15 hour online'];
         break;
      case 'private':
         optionsVal = [null,'40hrs','60hrs','80hrs','100hrs','other'];
         optionsTxt = ['','40 Hours Private','60 Hours Private','80 Hours Private','100 Hours Private','Other - add to notes'];
         break;
      case 'other':
         optionsVal = [
            null,'4wks','5wks','6wks','7wks',
            '8wks','9wks','10wks','12wks','other'
         ];
         optionsTxt = [
            '','4 weeks','5 weeks','6 weeks','7 weeks','8 weeks',
            '9 weeks','10 weeks','12 weeks','Other - add to notes'
         ];
         break;
      default:
         optionsVal = optionsTxt = [];
   }
   if(!skip) {
      field = ll_appForm.duration;
      ll_formOptionsInit(field,optionsVal,optionsTxt);
   }

   // Time
   switch(value) {
      case 'termIntensive':
      case 'flexIntensive':
         optionsVal = ['30h/w'];
         optionsTxt = ['Mon-Fri 8:30-12:30 & 13:30-15:30 (30 h/w)'];
         break;
      case 'online':
         optionsVal = ['custom'];
         optionsTxt = ['Online (Custom)'];
         break;
      case 'private':
         optionsVal = ['custom'];
         optionsTxt = ['Private (Custom)'];
         break;
      case 'term':
         optionsVal = [null,'morning','midday','other'];
         optionsTxt = ['',
            'Mon-Fri 8:30-12:30 (20 h/w)',
            'Mon-Fri 14:00-18:00 (20 h/w)',
            'Other - add to notes'
         ]
         break;
      case 'flexible':
      case 'other':
         optionsVal = [null,
            'morning','midday',
            'monWed','tuesThurs','wkndMorning',
            'wkndMidday','wkndBoth','other'
         ];
         optionsTxt = ['',
            'Mon-Fri 8:30-12:30 (20 h/w)',
            'Mon-Fri 14:00-18:00 (20 h/w)',
            'Mon & Wed 18:30-20:30 (4 h/w)',
            'Tues & Thurs 19:30 - 21:30 (4 h/w)',
            'Sat & Sun 8:30-12:30 (8 h/w)',
            'Sat & Sun 14:00-18:00 (8 h/w)',
            'Sat & Sun 8:30-12:30 & 14:00-16:00 (12 h/w)',
            'Other - add to notes'
         ]
         break;
      default:
         optionsVal = optionsTxt = [];
   }
   field = ll_appForm.time;
   ll_formOptionsInit(field,optionsVal,optionsTxt);
}

function ll_formPackageAction() {
   var optionsVal;
   var optionsTxt;
   var field;
   var value = ll_appForm.package.value;
   var skip = false;

   // Accomodation
   switch(value) {
      case "gold":
      case "silver":
         optionsVal = [null,
            'homestay','privateApt+','privateApt',
            'sharedApt+','sharedApt','other'
         ];
         optionsTxt = ['',
            'Homestay','Private Apt - Plus',
            'Private Apt - Stnd','Shared Apt - Plus',
            'Shared Apt - Stnd','Other - add to notes'
            ];
         break;
      case "bronze":
         switch (ll_appForm.program.value) {
            case 'flexible':
            case 'flexIntensive':
               ll_formFlexDurationAction();
               break;
         }
         optionsVal = ['n/a']
         optionsTxt = ['Not Applicable']
         break;
      default:
         optionsVal = optionsTxt = [];
   }
   if (!skip) {
      field = ll_appForm.accomodation;
      ll_formOptionsInit(field,optionsVal,optionsTxt);
   }
}

// Only call this if program.value is flexible or flexIntensive
function ll_formFlexDurationAction() {
   var optionsVal;
   var optionsTxt;
   var field = ll_appForm.duration;
   var value = ll_appForm.package.value;

   switch(value) {
      case "bronze":
         optionsVal = [null,
            '4wks','5wks','6wks','7wks','8wks',
            '9wks','10wks','12wks','other'
         ];
         optionsTxt = ['',
            '4 weeks','5 weeks','6 weeks','7 weeks','8 weeks',
            '9 weeks','10 weeks','12 weeks','Other - add to notes'
         ];
         break;
      case "gold":
      case "silver":
         // It should never run here, but just in case
         optionsVal = ['n/a'];
         optionsTxt = ['Not Applicable'];
         break;
      default:
         optionsVal = optionsTxt = [''];
         break;
   }
   ll_formOptionsInit(field,optionsVal,optionsTxt);

}

// Only call this if program.value is flexible or flexIntensive
function ll_formFlexAccomAction() {
   var optionsVal;
   var optionsTxt;
   var field;
   var value = ll_appForm.accomodation.value;

   // Accomodation
   switch(value) {
      case "homestay":
      case "other":
         optionsVal = [null,
            '4wks','5wks','6wks','7wks','8wks',
            '9wks','10wks','12wks','other'
         ];
         optionsTxt = ['',
            '4 weeks','5 weeks','6 weeks','7 weeks',
            '8 weeks','9 weeks','10 weeks','12 weeks',
            'Other - add to notes'
         ];
         break;
      case "privateApt+":
      case "privateApt":
      case "sharedApt+":
      case "sharedApt":
         optionsVal = [null,'8wks','9wks','10wks','12wks','other'];
         optionsTxt = ['',
            '8 weeks','9 weeks','10 weeks',
            '12 weeks','Other - add to notes'
         ];
         break;
      case "null":
      case "":
         optionsVal = optionsTxt = [];
         break;
      default:
         optionsVal = ['other'];
         optionsTxt = ['Other - add to notes'];
   }
   field = ll_appForm.duration;
   ll_formOptionsInit(field,optionsVal,optionsTxt);
}

function ll_formAccomodationAction() {
   if (
      ll_appForm.program.value == "flexible" ||
      ll_appForm.program.value == "flexIntensive"
   ) {
      ll_formFlexAccomAction();
   }
}

function ll_formStoreValues() {
   ll_formStoredValues = [
      ll_appForm.course.value,
      ll_appForm.program.value,
      ll_appForm.package.value,
      ll_appForm.accomodation.value,
      ll_appForm.duration.value,
      ll_appForm.time.value
   ];
}

function ll_formLoadValues() {
   console.log(ll_formStoredValues);
   ll_appForm.course.value = ll_formStoredValues[0];
   ll_formCourseAction();

   ll_appForm.program.value = ll_formStoredValues[1];
   ll_formProgramAction();

   ll_appForm.package.value = ll_formStoredValues[2];
   ll_formPackageAction();

   ll_appForm.accomodation.value = ll_formStoredValues[3];
   ll_formAccomodationAction();

   ll_appForm.duration.value = ll_formStoredValues[4];
   ll_appForm.time.value = ll_formStoredValues[5];
}
