/* javascript support for St. John's spiritual gifts survey
   written by: Russ Brown
   Copyright (c) St. John's Lutheran Church, Brookfield, WI
*/

// TITLES OF EACH GIFTS CATEGORY - ORDER IS IMPORTANT
var categoryTitleArray = new Array ("Administration", "Artistry", "Discernment", "Evangelism",
                            "Exhortation", "Faith", "Giving", "Hospitality", "Intercession",
                            "Knowledge", "Leadership", "Mercy", "Music-Vocal", "Music-Instrumental",
                            "Pastoral", "Service", "Skilled Craft", "Teaching", "Wisdom",
                            "Writing");

// ABBREVIATION FOR EACH CATEGORY - MUST BE IN SAME ORDER AS TITLES
var categoryArray = new Array ("ADMI", "ARTI", "DISC", "EVAN",
                            "EXHO", "FAIT", "GIVE", "HOSP", "INTE",
                            "KNOW", "LEAD", "MERC", "MUSV", "MUSI",
                            "PAST", "SERV", "SKIL", "TEAC", "WISD",
                            "WRIT");

// TOOLTIP "FLOATOVER" HELP FOR EACH CATEGORY ON RESULTS DISPLAY
// INDEXED BY ABBREVIATION
var tooltipHash = new Object;
tooltipHash.ADMI="Administration";
tooltipHash.ARTI="Artistry";
tooltipHash.DISC="Discernment";
tooltipHash.EVAN="Evangelism";
tooltipHash.EXHO="Exhortation";
tooltipHash.FAIT="Faith";
tooltipHash.GIVE="Giving";
tooltipHash.HOSP="Hospitality";
tooltipHash.INTE="Intercession";
tooltipHash.KNOW="Knowledge";
tooltipHash.LEAD="Leadership"; 
tooltipHash.MERC="Mercy";
tooltipHash.MUSV="Music-Vocal";
tooltipHash.MUSI="Music-Instrumental";
tooltipHash.PAST="Pastoral";
tooltipHash.SERV="Service";
tooltipHash.SKIL="Skilled Craft";
tooltipHash.TEAC="Teaching";
tooltipHash.WISD="Wisdom";
tooltipHash.WRIT="Writing"; 

// OPPORTUNITY & CONTACT LIST FOR EACH CATEGORY ON RESULTS DISPLAY
// INDEXED BY ABBREVIATION (+= appends new information, \n is a newline)
var contacts = new Object;
contacts.ADMI = "";
contacts.ADMI += "Suggestions\n"	 
               + "VBS Team Leader; Church Historian;" + "Church Council Boards and Committees; Small Group Leader\n"
               + "Organizer of Special Events; Financial Secretary/Treasurer; Infant Cradle Roll\n";

contacts.ARTI = "";
contacts.ARTI += "Suggestions\n"
+ "Vacation Bible School Crafts\n"
               + "Liturgical Arts Committee\n"
			   + "Teach a unit in Sunday School\n";

contacts.DISC = "";
contacts.DISC += "Suggestions\n"
+ "Church Council Opportunities\n"
               + "Walking With Christ, Strengthening Lives Vision Team\n";

contacts.EVAN = "";
			contacts.EVAN	+= "Suggestions\n"
				+ "Evangelism Committee\n"
				+"Small Group Leader\n";

contacts.EXHO = "";
contacts.EXHO+= "Suggestions\n"
+"Prayer Team, Small Groups\n"

contacts.FAIT = "";
contacts.FAIT+= "Suggestions\n"
+ "Walking With Christ, Strengthening Lives Teams\n"
+ "Outreach Committee\n";

contacts.GIVE = "";
contacts.GIVE+= "Suggestions\n" +"Stewardship Committee, Outreach Committee, Walking with Christ, Strengthening Lives Teams\n";

contacts.HOSP = "";
contacts.HOSP+= "Suggestions\n" +"Fellowship Committee, Welcomer, Outreach Committee\n";

contacts.INTE = "";
contacts.INTE+= "Suggestions\n" +"Prayer Team, Healing Service Team\n";

contacts.KNOW = "";
contacts.KNOW+="Suggestions\n" +"Education Committee, Small Group Bible study Leader, Adult Education\n";

contacts.LEAD = "";
contacts.LEAD+="Suggestions\n" +"Walking With Christ, Strengthening Live's Vision Team, Church Council, Small Group Leader\n";

contacts.MERC = "";
contacts.MERC+="Suggestions\n" +"Home Visitor, Prayer Ministry, Healing and Wellness Trips to El Salvador\n";

contacts.MUSV = "";
contacts.MUSV+="Suggestions\n" +"Choir, Special Music, Folk Choir\n";

contacts.MUSI = "";
contacts.MUSI+="Suggestions\n" +"Bell Choir, Special Music, Brass Choir\n";

contacts.PAST = "";
contacts.PAST+="Suggestions\n" +"Small Group Leader, Assisting Minister\n";

contacts.SERV = "";
contacts.SERV+="Suggestions\n"+"Janitorial Help, Sunday School Shepherd, Office Help, Snow Shoveling\n" +"Flower/Plant waterer, Flower beds\n";

contacts.SKIL = "";
contacts.SKIL +="Suggestions\n"+"Property Committee\n";

contacts.TEAC = "";
contacts.TEAC+="Suggestions\n"+"Small Groups, Sunday School, Adult Ed, Confirmation\n";

contacts.WISD = "";
contacts.WISD+="Suggestions\n"+"Church Council, Small Groups, Walking With Christ, Strengthening Lives Teams\n";

contacts.WRIT = "";
contacts.WRIT+="Suggestions\n"+"Newsletter, Web Content, Connections Content\n";


function sortPercentTuple(a,b)
{
  // inverse sort
  return b.p - a.p;
}

function displayContacts(category, categoryName){
  document.getElementById("contact-title").firstChild.nodeValue=categoryName+" Opportunities"
  document.getElementById("contacts").value=contacts[category];
}


function getResults (){

  resultsWindow = window.open("","resultsWindow","height=750,width=760");
  var totalsHash = new Object();
  var maxHash = new Object();
  var percentArray = new Array();
  var scaleFactor = 3;

  // get the selected radio button values.
  var numelems = document.giftssurvey.elements.length;
  var total = 0;
  for (i=0; i<numelems; i++) {
    if (document.giftssurvey.elements[i].checked) {
      var elemname = document.giftssurvey.elements[i].name;
      var category = elemname.substr(2,4);
      var elemvalue = parseInt(document.giftssurvey.elements[i].value);
      if (totalsHash[category]) {
        totalsHash[category] = totalsHash[category]+elemvalue;
        maxHash[category] = maxHash[category]+4;
      } else {
        totalsHash[category] = elemvalue;
        maxHash[category] = 4;
      }      
    }
  }

  // calculate percentage for each category and sort the array highest first
  for (i=0; i<categoryArray.length; i++) {
    // percentTuple holds p=percent and c=category index
    var percentTuple = new Object();
    percentTuple.c = i;
    if (totalsHash[categoryArray[i]]) {
      percentTuple.p = totalsHash[categoryArray[i]] * 100 / maxHash[categoryArray[i]];
    } else {
      percentTuple.p = 0;
    }
    percentArray[i] = percentTuple;
  }
  percentArray.sort(sortPercentTuple);

  // draw the graph
  htmlStr = "<html><head\><SCRIPT src=\"getresultssj.js\" type=\"text/javascript\"></script>\n";
  htmlStr+= "<LINK href=\"survey-styles.css\" rel=\"stylesheet\" type=\"text/css\"><body>\n";
  htmlStr += "<h1>Your Assessment Results:</h1>\n";
  htmlStr += "<table>\n";
  htmlStr += "<tr>\n";
  htmlStr += "<td><table>\n";
  
  for (i=0; i<categoryArray.length; i++) {
    htmlStr += "<tr>"
            + "<th title=\""+tooltipHash[categoryArray[percentArray[i].c]]+"\">"
            +  "<button type=button class=catbutton "
            +   "onclick=displayContacts(\""+categoryArray[percentArray[i].c]+"\",\""
            +   categoryTitleArray[percentArray[i].c]+"\")>"
            +   categoryTitleArray[percentArray[i].c]
            +   "</button>"
            + "</th>"
            + "<td><IMG SRC='sjgiftbar.jpg' width=" 
            + (percentArray[i].p * scaleFactor) + " height=10></td></tr>\n";
  }

  htmlStr+="</table></td>\n";
  htmlStr+="<td>\n";
  htmlStr+="<div id=contact-title>Instructions</div>\n";
  htmlStr+="<textarea id=contacts>Print your results.  Use the Explanation of Gifts to help discern where you might best serve in your congregation.</textarea>\n";
  htmlStr+= "<FORM NAME='xit'><INPUT TYPE='SUBMIT' VALUE='Close Window' onClick='self.close()'>\n" + 
                  "&nbsp;&nbsp;<INPUT TYPE='SUBMIT' VALUE='PRINT & EXIT' onClick='window.print();self.close()'>\n" +
                  "</FORM>\n";
htmlStr+="</td>\n";
  htmlStr+="</tr>\n";
  htmlStr+="</table>\n";
  htmlStr+="</body></html>\n";
  resultsWindow.document.write(htmlStr);
  resultsWindow.document.close();
}
