//=========================================================================================
//                           <<<< ILSR PMPS Survey Scripting >>>>
//               All scripting/logic is (c)2000, 2001 Jeremy-Chad Pulcifer
//  All scripting/logic was designed/written by Jeremy-Chad Pulcifer [ http://fly.to/dahc ]
//=========================================================================================

//Global Vars====
var noAnswerFlag = 0;  //No answer flag

function testingAnswer() {  // Set stuff
  for(i = 1; i < 10; ++i) {
   eval('document.pmpsform.num_0' + i + '.selectedIndex = 2;');
  }
  for(i = 10; i < 43; ++i) {
   eval('document.pmpsform.num_' + i + '.selectedIndex = 2;');
  }
}

function compute() {

  if(document.pmpsform.nameText.value == 'testing') {
    testingAnswer();
    document.pmpsform.nameText.value = 'TESTING DELETE ME';
    document.pmpsform.emailText.value = 'testingdeleteme@nowhere.com';
    if(confirm('Test fill complete.\nPoint results to \'emperordahc@hotmail.com\' instead?\n  (Clicking \'Cancel\' will point it to \'ILSR@prodigy.net\'.)')) {
      document.pmpsEmailForm.recipient.value = 'emperordahc@hotmail.com';
    }
    return;
  }

  //==VARS==
  score_print       = 0;
  score_aural       = 0;
  score_interactive = 0;
  score_visual      = 0;
  score_haptic      = 0;
  score_kinesthetic = 0;
  score_olfactory   = 0;

  noAnswerFlag = 0;  //Reset!

  num_01 = document.pmpsform.num_01.selectedIndex;
  num_02 = document.pmpsform.num_02.selectedIndex;
  num_03 = document.pmpsform.num_03.selectedIndex;
  num_04 = document.pmpsform.num_04.selectedIndex;
  num_05 = document.pmpsform.num_05.selectedIndex;
  num_06 = document.pmpsform.num_06.selectedIndex;
  num_07 = document.pmpsform.num_07.selectedIndex;
  num_08 = document.pmpsform.num_08.selectedIndex;
  num_09 = document.pmpsform.num_09.selectedIndex;
  num_10 = document.pmpsform.num_10.selectedIndex;
  num_11 = document.pmpsform.num_11.selectedIndex;
  num_12 = document.pmpsform.num_12.selectedIndex;
  num_13 = document.pmpsform.num_13.selectedIndex;
  num_14 = document.pmpsform.num_14.selectedIndex;
  num_15 = document.pmpsform.num_15.selectedIndex;
  num_16 = document.pmpsform.num_16.selectedIndex;
  num_17 = document.pmpsform.num_17.selectedIndex;
  num_18 = document.pmpsform.num_18.selectedIndex;
  num_19 = document.pmpsform.num_19.selectedIndex;
  num_20 = document.pmpsform.num_20.selectedIndex;
  num_21 = document.pmpsform.num_21.selectedIndex;
  num_22 = document.pmpsform.num_22.selectedIndex;
  num_23 = document.pmpsform.num_23.selectedIndex;
  num_24 = document.pmpsform.num_24.selectedIndex;
  num_25 = document.pmpsform.num_25.selectedIndex;
  num_26 = document.pmpsform.num_26.selectedIndex;
  num_27 = document.pmpsform.num_27.selectedIndex;
  num_28 = document.pmpsform.num_28.selectedIndex;
  num_29 = document.pmpsform.num_29.selectedIndex;
  num_30 = document.pmpsform.num_30.selectedIndex;
  num_31 = document.pmpsform.num_31.selectedIndex;
  num_32 = document.pmpsform.num_32.selectedIndex;
  num_33 = document.pmpsform.num_33.selectedIndex;
  num_34 = document.pmpsform.num_34.selectedIndex;
  num_35 = document.pmpsform.num_35.selectedIndex;
  num_36 = document.pmpsform.num_36.selectedIndex;
  num_37 = document.pmpsform.num_37.selectedIndex;
  num_38 = document.pmpsform.num_38.selectedIndex;
  num_39 = document.pmpsform.num_39.selectedIndex;
  num_40 = document.pmpsform.num_40.selectedIndex;
  num_41 = document.pmpsform.num_41.selectedIndex;
  num_42 = document.pmpsform.num_42.selectedIndex;

  //==CPU==
  
  //Print >>
  score_print += num_check(num_01,  4,  2, -2, -4);
  score_print += num_check(num_07, -2, -1,  1,  2);
  score_print += num_check(num_08,  4,  2, -2, -4);
  score_print += num_check(num_14, -2, -1,  1,  2);
  score_print += num_check(num_15,  4,  2, -2, -4);
  score_print += num_check(num_21, -2, -1,  1,  2);
  score_print += num_check(num_22,  4,  2, -2, -4);
  score_print += num_check(num_28, -2, -1,  1,  2);
  score_print += num_check(num_29,  4,  2, -2, -4);
  score_print += num_check(num_35, -2, -1,  1,  2);
  score_print += num_check(num_36,  4,  2, -2, -4);
  score_print += num_check(num_42, -2, -1,  1,  2);

  //Aural >>
  score_aural += num_check(num_01, -2, -1,  1,  2);
  score_aural += num_check(num_02,  4,  2, -2, -4);
  score_aural += num_check(num_11, -2, -1,  1,  2);
  score_aural += num_check(num_12,  4,  2, -2, -4);
  score_aural += num_check(num_19, -2, -1,  1,  2);
  score_aural += num_check(num_20,  4,  2, -2, -4);
  score_aural += num_check(num_27, -2, -1,  1,  2);
  score_aural += num_check(num_28,  4,  2, -2, -4);
  score_aural += num_check(num_31, -2, -1,  1,  2);
  score_aural += num_check(num_32,  4,  2, -2, -4);
  score_aural += num_check(num_37, -2, -1,  1,  2);
  score_aural += num_check(num_38,  4,  2, -2, -4);

  //Interactive >>
  score_interactive += num_check(num_02, -2, -1,  1,  2);
  score_interactive += num_check(num_03,  4,  2, -2, -4);
  score_interactive += num_check(num_08, -2, -1,  1,  2);
  score_interactive += num_check(num_09,  4,  2, -2, -4);
  score_interactive += num_check(num_17, -2, -1,  1,  2);
  score_interactive += num_check(num_18,  4,  2, -2, -4);
  score_interactive += num_check(num_26, -2, -1,  1,  2);
  score_interactive += num_check(num_27,  4,  2, -2, -4);
  score_interactive += num_check(num_34, -2, -1,  1,  2);
  score_interactive += num_check(num_35,  4,  2, -2, -4);
  score_interactive += num_check(num_39, -2, -1,  1,  2);
  score_interactive += num_check(num_40,  4,  2, -2, -4);

  //Visual >>
  score_visual += num_check(num_03, -2, -1,  1,  2);
  score_visual += num_check(num_04,  4,  2, -2, -4);
  score_visual += num_check(num_12, -2, -1,  1,  2);
  score_visual += num_check(num_13,  4,  2, -2, -4);
  score_visual += num_check(num_15, -2, -1,  1,  2);
  score_visual += num_check(num_16,  4,  2, -2, -4);
  score_visual += num_check(num_25, -2, -1,  1,  2);
  score_visual += num_check(num_26,  4,  2, -2, -4);
  score_visual += num_check(num_30, -2, -1,  1,  2);
  score_visual += num_check(num_31,  4,  2, -2, -4);
  score_visual += num_check(num_41, -2, -1,  1,  2);
  score_visual += num_check(num_42,  4,  2, -2, -4);

  //Haptic >>
  score_haptic += num_check(num_04, -2, -1,  1,  2);
  score_haptic += num_check(num_05,  4,  2, -2, -4);
  score_haptic += num_check(num_09, -2, -1,  1,  2);
  score_haptic += num_check(num_10,  4,  2, -2, -4);
  score_haptic += num_check(num_20, -2, -1,  1,  2);
  score_haptic += num_check(num_21,  4,  2, -2, -4);
  score_haptic += num_check(num_24, -2, -1,  1,  2);
  score_haptic += num_check(num_25,  4,  2, -2, -4);
  score_haptic += num_check(num_33, -2, -1,  1,  2);
  score_haptic += num_check(num_34,  4,  2, -2, -4);
  score_haptic += num_check(num_36, -2, -1,  1,  2);
  score_haptic += num_check(num_37,  4,  2, -2, -4);

  //Kinesthetic >>
  score_kinesthetic += num_check(num_05, -2, -1,  1,  2);
  score_kinesthetic += num_check(num_06,  4,  2, -2, -4);
  score_kinesthetic += num_check(num_13, -2, -1,  1,  2);
  score_kinesthetic += num_check(num_14,  4,  2, -2, -4);
  score_kinesthetic += num_check(num_18, -2, -1,  1,  2);
  score_kinesthetic += num_check(num_19,  4,  2, -2, -4);
  score_kinesthetic += num_check(num_23, -2, -1,  1,  2);
  score_kinesthetic += num_check(num_24,  4,  2, -2, -4);
  score_kinesthetic += num_check(num_29, -2, -1,  1,  2);
  score_kinesthetic += num_check(num_30,  4,  2, -2, -4);
  score_kinesthetic += num_check(num_38, -2, -1,  1,  2);
  score_kinesthetic += num_check(num_39,  4,  2, -2, -4);

  //Olfactory >>
  score_olfactory += num_check(num_06, -2, -1,  1,  2);
  score_olfactory += num_check(num_07,  4,  2, -2, -4);
  score_olfactory += num_check(num_10, -2, -1,  1,  2);
  score_olfactory += num_check(num_11,  4,  2, -2, -4);
  score_olfactory += num_check(num_16, -2, -1,  1,  2);
  score_olfactory += num_check(num_17,  4,  2, -2, -4);
  score_olfactory += num_check(num_22, -2, -1,  1,  2);
  score_olfactory += num_check(num_23,  4,  2, -2, -4);
  score_olfactory += num_check(num_32, -2, -1,  1,  2);
  score_olfactory += num_check(num_33,  4,  2, -2, -4);
  score_olfactory += num_check(num_40, -2, -1,  1,  2);
  score_olfactory += num_check(num_41,  4,  2, -2, -4);

  //Error Checking==
  if(noAnswerFlag == 1) {
    alert("You have (an) unanswered question(s).  Please answer them first.");
    return 0;
  }
  if(document.pmpsform.nameText.value == "") {
    alert("Please enter your name at the top.");
    return 0;
  }
  if(document.pmpsform.emailText.value == "") {
    alert("Please enter your e-mail address at the top.");
    return 0;
  }

  //==END==

  //Enable Submit?
  if(document.pmpsform.checkSendResults.checked == false) {
    if(confirm('Would you like ILSR to receive a copy of your scores?  (This is suggested.)')) {
      document.pmpsform.checkSendResults.checked = true;
    }
  }


  //Open/Start HTML Window==
  scorewindow = window.open("", "_blank", "height=400,width=275");
  scorewindow.document.writeln('<title>Perceptual Modality Preference Survey</title>');
  scorewindow.document.writeln('<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#0000FF" alink="#999999">');

  //Order Scores==

  score_array = new Array(8);        //Holds Scores
  score_array_temp = 0;              //Holds Scores Temp
  score_array_class = new Array(8);  //Holds Scores Class
  score_array_class_temp = '';       //Holds Scores Class Temp

  score_array[1] = score_print;
  score_array[2] = score_aural;
  score_array[3] = score_interactive;
  score_array[4] = score_visual;
  score_array[5] = score_haptic;
  score_array[6] = score_kinesthetic;
  score_array[7] = score_olfactory;

  score_array_class[1] = 'print';
  score_array_class[2] = 'aural';
  score_array_class[3] = 'interactive';
  score_array_class[4] = 'visual';
  score_array_class[5] = 'haptic';
  score_array_class[6] = 'kinesthetic';
  score_array_class[7] = 'olfactory';

  i = 1;  // Swap Info==
  while(i < 8) {
    ii = 0;
    while(ii < 8) {
      if(score_array[i] > score_array[ii]) {
        //Data
        score_array_temp = score_array[i];
        score_array[i] = score_array[ii];
        score_array[ii] = score_array_temp;
        //Classes
        score_array_class_temp = score_array_class[i];
        score_array_class[i] = score_array_class[ii];
        score_array_class[ii] = score_array_class_temp;
        //==
        ii = 1;
      }
      else {
        ++ii;
      }
    }
    ++i;
  }

  //Start Complete table
  scorewindow.document.writeln('<table border="0" vspace="0" hspace="0"><tr><td><div align="right">');

  //Start Header Bar table
  scorewindow.document.writeln('<table border="0" vspace="0" hspace="0"><tr><td><img src="images/scorewindow_%7Ebar_top.gif" width="240" height="20"></td></tr></table>');

  //Start Bar table
  scorewindow.document.writeln('<table width="249" cellspacing="0" cellpadding="0" border ="0"><tr valign="middle"><td><div align="right"><img src="images/scorewindow_%7Ebar_left.gif" width="14" height="200"></div></td><td nowrap background="images/scorewindow_~bar_background.gif"><div align="right">');

  //Scores
  for(i = 1; i < 8; ++i) {
    ii = score_array[i];   if(ii > 0) scorewindow.document.writeln('<img src="images/scorewindow_bar_' + score_array_class[i] + '.gif" width="30" height="' + (ii *  2.5) + '" vspace="0" hspace="0" align="bottom">'); else scorewindow.document.writeln('<img src="images/scorewindow_scorebar_blank.gif" width="30" height="100" align="bottom">');
  }
  scorewindow.document.writeln('<br>');
  for(i = 1; i < 8; ++i) {
    ii = score_array[i];   if(ii < 0) scorewindow.document.writeln('<img src="images/scorewindow_bar_' + score_array_class[i] + '.gif" width="30" height="' + (ii * -2.5) + '" vspace="0" hspace="0" align="top">'); else scorewindow.document.writeln('<img src="images/scorewindow_scorebar_blank.gif" width="30" height="100" align="top">');
  } 
  scorewindow.document.writeln('</div></td></tr></table>');

  //Scores Table
  scorewindow.document.writeln('<table border="0"  cellpadding="0"><tr align="center">');

  for(i = 1; i < 8; ++i) {
    scorewindow.document.writeln('<td width="32"><div align="center"><font size="2">' + score_array[i] + '</font></div></td>');
  }
  scorewindow.document.writeln('</tr></table>');

  //Score Labels
  for(i = 1; i < 8; ++i) {
    scorewindow.document.writeln('<img src="images/scorewindow_label_' + score_array_class[i] + '.gif" width="30" height="100">');
  }

  //Results
  scorewindow.document.writeln('<br><font size="2"><a href="javascript:window.close();">close</a></font>');
  
  //End Complete Table
  scorewindow.document.writeln('</div></td></tr></table>');

  //End 
  scorewindow.document.writeln('</body>');


  //Submit?
  if(document.pmpsform.checkSendResults.checked) {
    document.pmpsform.checkSendResults.checked = false;
    sendEmail();
  }


}

function num_check(num, selection1, selection2, selection3, selection4) {  //This does the checking
  if(num == 0) { noAnswerFlag = 1; return 0; }
  if(num == 1) return selection1;
  if(num == 2) return selection2;
  if(num == 3) return selection3;
  if(num == 4) return selection4;

  alert("A wacko list error has occured!");
  return 0;
}

//=========================

function sendEmail() {  //Sends Results

  //Copy email Data
  document.pmpsEmailForm.subject.value = 'Survey Results for \'' + document.pmpsform.nameText.value + '\'';
  

  list = '\n==============\n';
  list += '\n\n';

  //Copy personal info
  list += 'Name & Email\n';
  list += '============\n';
  list += '  ' + document.pmpsform.nameText.value + '\n';
  list += '  ' + document.pmpsform.emailText.value + '\n';
  list += '\n\n';

  //Copy Scores
  list += 'Scores\n';
  list += '======\n';
  list += '  Print: \t' + score_print + '\n';
  list += '  Aural: \t' + score_aural + '\n';
  list += '  Interactive: \t' + score_interactive + '\n';
  list += '  Visual: \t' + score_visual + '\n';
  list += '  Haptic: \t' + score_haptic + '\n';
  list += '  Kinesthetic: \t' + score_kinesthetic + '\n';
  list += '  Olfactory: \t' + score_olfactory;
  list += '\n\n';

  //Copy Answers
  list += 'Answers\n';
  list += '=======\n';
  list += '  01: ' + getAnswerWord(document.pmpsform.num_01.selectedIndex) + '\n';
  list += '  02: ' + getAnswerWord(document.pmpsform.num_02.selectedIndex) + '\n';
  list += '  03: ' + getAnswerWord(document.pmpsform.num_03.selectedIndex) + '\n';
  list += '  04: ' + getAnswerWord(document.pmpsform.num_04.selectedIndex) + '\n';
  list += '  05: ' + getAnswerWord(document.pmpsform.num_05.selectedIndex) + '\n';
  list += '  06: ' + getAnswerWord(document.pmpsform.num_06.selectedIndex) + '\n';
  list += '  07: ' + getAnswerWord(document.pmpsform.num_07.selectedIndex) + '\n';
  list += '  08: ' + getAnswerWord(document.pmpsform.num_08.selectedIndex) + '\n';
  list += '  09: ' + getAnswerWord(document.pmpsform.num_09.selectedIndex) + '\n';
  list += '  10: ' + getAnswerWord(document.pmpsform.num_10.selectedIndex) + '\n';
  list += '  11: ' + getAnswerWord(document.pmpsform.num_11.selectedIndex) + '\n';
  list += '  12: ' + getAnswerWord(document.pmpsform.num_12.selectedIndex) + '\n';
  list += '  13: ' + getAnswerWord(document.pmpsform.num_13.selectedIndex) + '\n';
  list += '  14: ' + getAnswerWord(document.pmpsform.num_14.selectedIndex) + '\n';
  list += '  15: ' + getAnswerWord(document.pmpsform.num_15.selectedIndex) + '\n';
  list += '  16: ' + getAnswerWord(document.pmpsform.num_16.selectedIndex) + '\n';
  list += '  17: ' + getAnswerWord(document.pmpsform.num_17.selectedIndex) + '\n';
  list += '  18: ' + getAnswerWord(document.pmpsform.num_18.selectedIndex) + '\n';
  list += '  19: ' + getAnswerWord(document.pmpsform.num_19.selectedIndex) + '\n';
  list += '  20: ' + getAnswerWord(document.pmpsform.num_20.selectedIndex) + '\n';
  list += '  21: ' + getAnswerWord(document.pmpsform.num_21.selectedIndex) + '\n';
  list += '  22: ' + getAnswerWord(document.pmpsform.num_22.selectedIndex) + '\n';
  list += '  23: ' + getAnswerWord(document.pmpsform.num_23.selectedIndex) + '\n';
  list += '  24: ' + getAnswerWord(document.pmpsform.num_24.selectedIndex) + '\n';
  list += '  25: ' + getAnswerWord(document.pmpsform.num_25.selectedIndex) + '\n';
  list += '  26: ' + getAnswerWord(document.pmpsform.num_26.selectedIndex) + '\n';
  list += '  27: ' + getAnswerWord(document.pmpsform.num_27.selectedIndex) + '\n';
  list += '  28: ' + getAnswerWord(document.pmpsform.num_28.selectedIndex) + '\n';
  list += '  29: ' + getAnswerWord(document.pmpsform.num_29.selectedIndex) + '\n';
  list += '  30: ' + getAnswerWord(document.pmpsform.num_30.selectedIndex) + '\n';
  list += '  31: ' + getAnswerWord(document.pmpsform.num_31.selectedIndex) + '\n';
  list += '  32: ' + getAnswerWord(document.pmpsform.num_32.selectedIndex) + '\n';
  list += '  33: ' + getAnswerWord(document.pmpsform.num_33.selectedIndex) + '\n';
  list += '  34: ' + getAnswerWord(document.pmpsform.num_34.selectedIndex) + '\n';
  list += '  35: ' + getAnswerWord(document.pmpsform.num_35.selectedIndex) + '\n';
  list += '  36: ' + getAnswerWord(document.pmpsform.num_36.selectedIndex) + '\n';
  list += '  37: ' + getAnswerWord(document.pmpsform.num_37.selectedIndex) + '\n';
  list += '  38: ' + getAnswerWord(document.pmpsform.num_38.selectedIndex) + '\n';
  list += '  39: ' + getAnswerWord(document.pmpsform.num_39.selectedIndex) + '\n';
  list += '  40: ' + getAnswerWord(document.pmpsform.num_40.selectedIndex) + '\n';
  list += '  41: ' + getAnswerWord(document.pmpsform.num_41.selectedIndex) + '\n';
  list += '  42: ' + getAnswerWord(document.pmpsform.num_42.selectedIndex) + '\n';
  list += '\n\n\n\n';

  //Credits
  list += '==========================================================\n';
  list += 'Survey coding by Jeremy-Chad Pulcifer.  http://fly.to/dahc\n';
  list += '==========================================================\n';


  //Copy all in
  document.pmpsEmailForm.SurveyResults.value = list;
  

  //Send!
  document.pmpsEmailForm.submit();
}

//=========================

function getAnswerWord(i) {
  if(i == 1) return 'Always';
  if(i == 2) return 'Usually';
  if(i == 3) return 'Seldom';
  if(i == 4) return 'Never';

  alert("A wacko list error has occured!");
  return 'A wacko list error has occured!';
}

//=========================

function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}
