/*
* This array contains the following in order:
* id,
* body type,
* gender,
* characteristic
*/

var symptoms = new Array ();
/* questions 1 */
symptoms[0]  = new Array (1,  'THYROID', 'both',   'Crave Sweets or Breads');
symptoms[1]  = new Array (2,  'ADRENAL', 'both',   'Crave Salty Foods');
symptoms[2]  = new Array (3,  'LIVER',   'both',   'Crave Deep-Fried Foods');
symptoms[3]  = new Array (4,  'OVARY',   'female', 'Crave Ice Cream');
/* questions 2 */
symptoms[4]  = new Array (5,  'THYROID', 'both',   'Feel Down or Hopeless');
symptoms[5]  = new Array (6,  'ADRENAL', 'both',   'A Worrier or Nervous');
symptoms[6]  = new Array (7,  'LIVER',   'both',   'Grouchy in the Morning');
symptoms[7]  = new Array (8,  'OVARY',   'female', 'Irritable at That Time of the Month');
/* questions 3 */
symptoms[8]  = new Array (9,  'THYROID', 'both',   'Feel Better When Eating Fruits');
symptoms[9]  = new Array (10, 'Adrenal', 'both',   'Need Coffee to Wake Up');
symptoms[10] = new Array (11, 'LIVER',   'both',   'Tightness on Right Side of Abdomen');
symptoms[11] = new Array (12, 'OVARY',   'female', 'Constipation during Menstruation');
/* questions 4 */
symptoms[12] = new Array (13, 'THYROID', 'both',   'Brittle Nails with Vertical Ridges');
symptoms[13] = new Array (14, 'ADRENAL', 'both',   'Poor Immune System');
symptoms[14] = new Array (15, 'LIVER',   'both',   'Tightness in Right Shoulder');
symptoms[15] = new Array (16, 'OVARY',   'female', 'Lower Back/Hip Pain');
/* questions 5 */
symptoms[16] = new Array (17, 'THYROID', 'both',   'Weight More Evenly Distributed');
symptoms[17] = new Array (18, 'ADRENAL', 'both',   'Sagging or Protruding Belly');
symptoms[18] = new Array (19, 'LIVER',   'both',   'Potbelly-Shaped Abdomen');
symptoms[19] = new Array (20, 'OVARY',   'female', 'Excess Hip Fat or Saddlebags');
/* questions 6 */
symptoms[20] = new Array (21, 'THYROID', 'both',   'Dry Skin on Heels or Elbows');
symptoms[21] = new Array (22, 'ADRENAL', 'both',   'Socks Leave Creases on Ankles');
symptoms[22] = new Array (23, 'LIVER',   'both',   'Flaky Skin in Eyebrows and Scalp');
symptoms[23] = new Array (24, 'OVARY',   'female', 'Menstrual Cycle Hair Loss');
/* questions 7 */
symptoms[24] = new Array (25, 'THYROID', 'both',   'Difficulty Making Decisions');
symptoms[25] = new Array (26, 'ADRENAL', 'both',   'Dizzy If I Get Up Quickly');
symptoms[26] = new Array (27, 'LIVER',   'both',   'Low Endurance');
symptoms[27] = new Array (28, 'OVARY',   'female', 'Hot Flashes');
/* questions 8 */
symptoms[28] = new Array (29, 'THYROID', 'both',   'Hair Loss on the Outer Eyebrows');
symptoms[29] = new Array (30, 'ADRENAL', 'both',   'Excess Facial Hair');
symptoms[30] = new Array (31, 'LIVER',   'both',   'Hot Feet Especially at Night');
symptoms[31] = new Array (32, 'OVARY',   'female', 'Menstrual Cycle Irritability');
/* questions 9 */
symptoms[32] = new Array (33, 'THYROID', 'both',   'Cold Feet or Hands');
symptoms[33] = new Array (34, 'ADRENAL', 'both',   'Chronic Soreness in Body');
symptoms[34] = new Array (35, 'LIVER',   'both',   'Feeling of Brain Fog in Morning');
symptoms[35] = new Array (36, 'OVARY',   'female', 'Excessive Menstrual Bleeding');
/* questions 10 */
symptoms[36] = new Array (37, 'THYROID', 'both',   'Puffiness around Eyes');
symptoms[37] = new Array (38, 'ADRENAL', 'both',   'Out of Breath Climbing Stairs');
symptoms[38] = new Array (39, 'LIVER',   'both',   'Small Red Dots on Skin');
symptoms[39] = new Array (40, 'OVARY',   'female', 'Excessive Cellulite on Hips');
/* questions 11 */
symptoms[40] = new Array (41, 'THYROID', 'both',   'Excessive Skin-Sagging under Arms');
symptoms[41] = new Array (42, 'ADRENAL', 'both',   'Twitching in Left Eyelid');
symptoms[42] = new Array (43, 'LIVER',   'both',   'Excessive Bloating after Eating');
symptoms[43] = new Array (44, 'OVARY',   'female', 'Bloating during Menstrual Period');
/* questions 12 */
symptoms[44] = new Array (45, 'THYROID', 'both',   'General Hair Loss');
symptoms[45] = new Array (46, 'ADRENAL', 'both',   'Wake Up in the Middle of the Night');
symptoms[46] = new Array (47, 'LIVER',   'both',   'Bad Breath');
symptoms[47] = new Array (48, 'OVARY',   'female', 'Upper Body Slimmer Than Lower');
/* questions 13 */
symptoms[48] = new Array (49, 'THYROID', 'both',   'Hair Is Dull and Dry');
symptoms[49] = new Array (50, 'ADRENAL', 'both',   'Cramps in the Calves at Night');
symptoms[50] = new Array (51, 'LIVER',   'both',   'Itching in the Soles of Feet');
symptoms[51] = new Array (52, 'OVARY',   'female', 'Monthly Water Retention');
/* questions 14 */
symptoms[52] = new Array (53, 'THYROID', 'both',   'Real Sluggish in the Morning');
symptoms[53] = new Array (54, 'ADRENAL', 'both',   'Have Cravings for Chocolate');
symptoms[54] = new Array (55, 'LIVER',   'both',   'Eye Redness, Especially in the AM');
symptoms[55] = new Array (56, 'OVARY',   'female', 'Acne during Menstruation');
/* questions 15 */
symptoms[56] = new Array (57, 'THYROID', 'both',   'Lack of Get-Up-and-Go (vitality)');
symptoms[57] = new Array (58, 'ADRENAL', 'both',   'Excessive Thinking 24/7');
symptoms[58] = new Array (59, 'LIVER',   'both',   'Moodiness If I Skip a Meal');
symptoms[59] = new Array (60, 'OVARY',   'female', 'Can\'t Lose Weight after Pregnancy');
/* questions 16 */
symptoms[60] = new Array (61, 'THYROID', 'both',   'History of Yo-Yo Dieting');
symptoms[61] = new Array (62, 'ADRENAL', 'both',   'Low Tolerance to Stress, Impatient');
symptoms[62] = new Array (63, 'LIVER',   'both',   'Stiffness in the Right Side of Neck');
symptoms[63] = new Array (64, 'OVARY',   'female', 'Stiff Low Back during Menstruation');

var selections = new Array ();
var selectionsCount = 0;
var gender = '';

function beginTest() {
    var strDisplay = '';

    strDisplay += '<div id="gender_top">Select your Gender to  begin the Body Type Quiz</div>';
    strDisplay += '<div id="gender_bottom">';
    strDisplay += '  <div id="gender_male">';
    strDisplay += '    <a href="javascript:setGender(\'male\');"><label style="cursor: pointer;"><span style="vertical-align: 40px;">Male</span>&nbsp;&nbsp;<img src="images/quiz/icon_male.jpg" width="118" height="121" alt="Male" /></label></a>';
    strDisplay += '  </div>';
    strDisplay += '  <div id="gender_female">';
    strDisplay += '    <a href="javascript:setGender(\'female\');"><label style="cursor: pointer;"><img src="images/quiz/icon_female.jpg" width="90" height="139" alt="Female" />&nbsp;&nbsp;<span style="vertical-align: 58px;">Female</span></label></a>';
    strDisplay += '  </div>';
    strDisplay += '</div>';

    document.getElementById('quiz').innerHTML = strDisplay;
}

function setGender(sex) {
    gender = sex;
    displayQuiz(1);
}

function buildGroup(page) {
	var strGroup = '<div id="questions">';
    strGroup += '<table width="760" align="center" cellspacing="3" cellpadding="1" border="0">';

    var x = 0;
    var loopLt = 0;

    if (page == 1) {
        x = 0;
        loopLt = 24;
    } else if (page == 2) {
        x = 24;
        loopLt = 48;
    } else if (page == 3) {
        x = 48;
        loopLt = symptoms.length;
    }

    while (x < loopLt) {
        // if condition changed to show all groups
        if (symptoms[x][2] == 'both' || symptoms[x][2] == gender) {
            strGroup += '  <tr>';
            var i = 0;

            while (i < 3) {
                if (x < loopLt) {
                    if (symptoms[x][2] == 'both' || symptoms[x][2] == gender) {
                        strGroup += '    <td align="right" width="5px">';
                        strGroup += '      <input type="checkbox" id=' + symptoms[x][0] + ' onclick="checkSymption(this);" />';
                        strGroup += '    </td>';
						strGroup += '    <td align="left" width="281px"><label for="' + symptoms[x][0] + '">&nbsp;' + symptoms[x][3] + '</label></td>';

                        i = i + 1;
                    }
                } else {
                    strGroup += '    <td align="right" width="5px">&nbsp;</td>';
                    strGroup += '    <td align="left" width="281px">&nbsp;</td>';

                    i = i + 1;
                }

                x = x + 1;
            }

            strGroup += '  </tr>';
        } else {
            x = x + 1;
        }
    }

    strGroup += '</table>';
	strGroup += '</div>';

    return strGroup;
}

function displayQuiz(page) {
    var strDisplay = '';

    if (page == 1 || page == 2 || page == 3) {
        var strGroup = buildGroup(page);
        page++;

        strDisplay += '<div id="gender_top">Select all that apply to you</div>';
        strDisplay += '<div id="gender_bottom">';
        strDisplay += '  <div id="list_questions">';
        strDisplay += strGroup;
        strDisplay += '  </div>';
        strDisplay += '  <div id="next_button"><a href="#" onclick="displayQuiz('+page+');"></a></div>';
        strDisplay += '</div>';
    } else if (page == 4) {
        window.scroll(0, 0);

		strDisplay += '<div id="form_result_body">';
        strDisplay += '<div id="gender_top">Get your complete body type results</div>';
        strDisplay += '<div id="gender_bottom">';
        strDisplay += '  <div id="list_questions">';
        strDisplay += '    <div id="form_result">';
        strDisplay += '      <form id="form_result" name="form_result" method="post" action="index.php">';
        strDisplay += '      <input name="form_submit" type="hidden" id="form_submit" />';
		strDisplay += '      <input name="quizThyroidPercent" type="hidden" id="quizThyroidPercent" />';
        strDisplay += '      <input name="quizAdrenalPercent" type="hidden" id="quizAdrenalPercent" />';
        strDisplay += '      <input name="quizLiverPercent" type="hidden" id="quizLiverPercent" />';
		strDisplay += '      <input name="quizOvaryPercent" type="hidden" id="quizOvaryPercent" />';
        strDisplay += '      <input name="quizPrimaryType" type="hidden" id="quizPrimaryType" />';
        strDisplay += '      <table width="400" cellspacing="1" cellpadding="1" border="0">';
        strDisplay += '        <tr>';
        strDisplay += '          <td colspan="2" align="right" height="25">Complete the form below to see immediate test results:</td>';
        strDisplay += '        </tr>';
        strDisplay += '        <tr>';
        strDisplay += '          <td align="right" width="250" height="25">* First Name &nbsp;</td>';
        strDisplay += '          <td align="left" width="150">';
        strDisplay += '            <input name="quizFirstName" type="text" id="quizFirstName" size="20" />';
        strDisplay += '          </td>';
        strDisplay += '        </tr>';
        strDisplay += '        <tr>';
        strDisplay += '          <td align="right" height="25">* Last Name &nbsp;</td>';
        strDisplay += '          <td align="left">';
        strDisplay += '            <input name="quizLastName" type="text" id="quizLastName" size="20" />';
        strDisplay += '          </td>';
        strDisplay += '        </tr>';
        strDisplay += '        <tr>';
        strDisplay += '          <td align="right" height="25">* State &nbsp;</td>';
        strDisplay += '          <td align="left">';
        strDisplay += '            <select name="quizState" id="quizState" style="width: 150px;">';
        strDisplay += '              <option value="">Please choose</option>';
        strDisplay += '              <option value="AK">Alaska</option>';
        strDisplay += '              <option value="AL">Alabama</option>';
        strDisplay += '              <option value="AR">Arkansas</option>';
        strDisplay += '              <option value="AS">American Samoa</option>';
        strDisplay += '              <option value="AZ">Arizona</option>';
        strDisplay += '              <option value="CA">California</option>';
        strDisplay += '              <option value="CO">Colorado</option>';
        strDisplay += '              <option value="CT">Connecticut</option>';
        strDisplay += '              <option value="DC">District of Columbia</option>';
        strDisplay += '              <option value="DE">Delaware</option>';
        strDisplay += '              <option value="FL">Florida</option>';
        strDisplay += '              <option value="GA">Georgia</option>';
        strDisplay += '              <option value="GU">Guam</option>';
        strDisplay += '              <option value="HI">Hawaii</option>';
        strDisplay += '              <option value="IA">Iowa</option>';
        strDisplay += '              <option value="ID">Idaho</option>';
        strDisplay += '              <option value="IL">Illinois</option>';
        strDisplay += '              <option value="IN">Indiana</option>';
        strDisplay += '              <option value="KS">Kansas</option>';
        strDisplay += '              <option value="KY">Kentucky</option>';
        strDisplay += '              <option value="LA">Louisiana</option>';
        strDisplay += '              <option value="MA">Massachusetts</option>';
        strDisplay += '              <option value="ME">Maine</option>';
        strDisplay += '              <option value="MD">Maryland</option>';
        strDisplay += '              <option value="MH">Marshall Islands</option>';
        strDisplay += '              <option value="MI">Michigan</option>';
        strDisplay += '              <option value="MN">Minnesota</option>';
        strDisplay += '              <option value="MO">Missouri</option>';
        strDisplay += '              <option value="MS">Mississippi</option>';
        strDisplay += '              <option value="MT">Montana</option>';
        strDisplay += '              <option value="NC">North Carolina</option>';
        strDisplay += '              <option value="ND">North Dakota</option>';
        strDisplay += '              <option value="NE">Nebraska</option>';
        strDisplay += '              <option value="NH">New Hampshire</option>';
        strDisplay += '              <option value="NJ">New Jersey</option>';
        strDisplay += '              <option value="NM">New Mexico</option>';
        strDisplay += '              <option value="NV">Nevada</option>';
        strDisplay += '              <option value="NY">New York</option>';
        strDisplay += '              <option value="OH">Ohio</option>';
        strDisplay += '              <option value="OK">Oklahoma</option>';
        strDisplay += '              <option value="OR">Oregon</option>';
        strDisplay += '              <option value="PA">Pennsylvania</option>';
        strDisplay += '              <option value="PR">Puerto Rico</option>';
        strDisplay += '              <option value="PW">Palau</option>';
        strDisplay += '              <option value="RI">Rhode Island</option>';
        strDisplay += '              <option value="SC">South Carolina</option>';
        strDisplay += '              <option value="SD">South Dakota</option>';
        strDisplay += '              <option value="TN">Tennessee</option>';
        strDisplay += '              <option value="TX">Texas</option>';
        strDisplay += '              <option value="UT">Utah</option>';
        strDisplay += '              <option value="VI">Virgin Islands</option>';
        strDisplay += '              <option value="VT">Vermont</option>';
        strDisplay += '              <option value="VA">Virginia</option>';
        strDisplay += '              <option value="WA">Washington</option>';
        strDisplay += '              <option value="WI">Wisconsin</option>';
        strDisplay += '              <option value="WV">West Virginia</option>';
        strDisplay += '              <option value="WY">Wyoming</option>';
        strDisplay += '              <option value="other">Other</option>';
        strDisplay += '            </select>';
        strDisplay += '          </td>';
        strDisplay += '        </tr>';
        strDisplay += '        <tr>';
        strDisplay += '          <td align="right" height="25">* Phone &nbsp;</td>';
        strDisplay += '          <td align="left">';
        strDisplay += '            <input name="quizPhone" type="text" id="quizPhone" size="20" />';
        strDisplay += '          </td>';
        strDisplay += '        </tr>';
        strDisplay += '        <tr>';
        strDisplay += '          <td align="right" height="25">** Email &nbsp;</td>';
        strDisplay += '          <td align="left">';
        strDisplay += '            <input name="quizEmail" type="text" id="quizEmail" size="20" />';
        strDisplay += '          </td>';
        strDisplay += '        </tr>';
        strDisplay += '        <tr>';
        strDisplay += '          <td colspan="2" align="right" height="25">';
        strDisplay += '            <span style="font-size: 10px;">* Your information is secure and is only used to validate your identity<br />';
        strDisplay += '            should you forget your body type results and return at a later time.</span><br />';
        strDisplay += '            <span style="font-size: 12px; font-weight: bold; color: #F82121;">** Check your e-mail for a link to see your results.</span>';
        strDisplay += '          </td>';
        strDisplay += '        </tr>';
        strDisplay += '      </table>';
        strDisplay += '      </form>';
        strDisplay += '    </div>';
        strDisplay += '  </div>';
        strDisplay += '  <div id="result_button"><a href="#" onclick="submitForm();"></a></div>';
        strDisplay += '</div>';
		strDisplay += '</div>';
    }

    document.getElementById("quiz").innerHTML = strDisplay;
}

function checkSymption(el) {
    if (el.checked == true) {
        addSymption(el);
    } else {
        removeSymption(el);
    }
}

function addSymption(el) {
    var found = false;

    for (x=0; x<selections.length; x++) {
        if (selections[x] == el.id) {
            found = true;
        }
    }

    if (found == false) {
        selections[selectionsCount] = el.id;
        selectionsCount++;
    }
}

function removeSymption(el) {
    for (x=0; x<selections.length; x++) {
        if (selections[x] == el.id) {
            delete(selections[x]);
            selections.sort();
            selections.pop();
            selectionsCount--;
        }
    }
}

function submitForm() {
    if (checkForm()) {
        if (selections.length > 0) {
            var type = checkbodytype();

			document.form_result.quizThyroidPercent.value = String(type['thyroidpercent']).substr(0, 5);
            document.form_result.quizAdrenalPercent.value = String(type['adrenalpercent']).substr(0, 5);
			document.form_result.quizLiverPercent.value = String(type['liverpercent']).substr(0, 5);
            document.form_result.quizOvaryPercent.value = String(type['ovarypercent']).substr(0, 5);   
            document.form_result.quizPrimaryType.value = type['yourbodytype'];

            document.form_result.submit();
        } else {
            alert('No selections were made.');
        }
    }
}

function checkForm() {
    var fname = document.getElementById('quizFirstName');

    if (fname.value <= '') {
        alert('Please enter your first name.');
        return false;
    }

    var lname = document.getElementById('quizLastName');

    if (lname.value <= '') {
        alert('Please enter your last name.');
        return false;
    }

    var state = document.getElementById('quizState');

    if (state.value <= '') {
        alert('Please enter your state.');
        return false;
    }

    var phone = document.getElementById('quizPhone');

    if (phone.value <= '') {
        alert('Please enter your phone number.');
        return false;
    }

    var email = document.getElementById('quizEmail');
    var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;

    if (!pattern.test(email.value)) {
        alert('Please enter a valid email address.');
        return false;
    }

    return true;
}

function checkbodytype() {
	var thyroidtotal = 0;
    var adrenaltotal = 0;
	var livertotal = 0;
    var ovarytotal = 0;
    
    var thyroidpercent = 0;
    var adrenalpercent = 0;
	var liverpercent = 0;
    var ovarypercent = 0;

    var tempType = '';
    var tempTypes = new Array ();

    for (x=0; x<selections.length; x++) {
        for (i=0; i<symptoms.length; i++) {
            if (selections[x] == symptoms[i][0]) {
                tempType = symptoms[i][1];

                // have to do this because some symptoms represent two body types
                // split the symptoms body type field and check for both symptoms
                tempTypes = tempType.split('|');

                if (tempTypes[0] == 'THYROID' || tempTypes[1] == 'THYROID'){
                    thyroidtotal++
                };

                if (tempTypes[0] == 'ADRENAL' || tempTypes[1] == 'ADRENAL'){
                    adrenaltotal++
                };

                if (tempTypes[0] == 'LIVER' || tempTypes[1] == 'LIVER'){
                    livertotal++
                };

                if (tempTypes[0] == 'OVARY' || tempTypes[1] == 'OVARY'){
                    ovarytotal++
                };
            }
        }
    }

    var totalCount = thyroidtotal + adrenaltotal + livertotal + ovarytotal;

    thyroidpercent = 100 * thyroidtotal / totalCount;
    adrenalpercent = 100 * adrenaltotal / totalCount;
	liverpercent = 100 * livertotal / totalCount;
    ovarypercent = 100 * ovarytotal / totalCount;

    var yourbodytype = '';

    if (ovarytotal > thyroidtotal && ovarytotal > livertotal && ovarytotal > adrenaltotal) {
        if (gender == 'male') {
            yourbodytype='ADRENAL';
        } else {
            yourbodytype='OVARY';
        }
    } else if (thyroidtotal > ovarytotal && thyroidtotal > livertotal && thyroidtotal > adrenaltotal) {
        yourbodytype = 'THYROID';
    } else if (livertotal > ovarytotal && livertotal > thyroidtotal && livertotal > adrenaltotal) {
        yourbodytype = 'LIVER';
    } else if (adrenaltotal > ovarytotal && adrenaltotal > thyroidtotal && adrenaltotal > livertotal) {
        yourbodytype='ADRENAL';
    } else if (ovarytotal == thyroidtotal) {
        if (gender == 'male') {
            yourbodytype = 'ADRENAL';
        } else {
            yourbodytype='OVARY';
        }
    } else if (ovarytotal == livertotal) {
        if (gender == 'male') {
            yourbodytype = 'ADRENAL';
        } else {
            yourbodytype = 'OVARY';
        }
    } else if (ovarytotal == adrenaltotal) {
        if (gender == 'male') {
            yourbodytype = 'ADRENAL';
        } else {
            yourbodytype = 'OVARY';
        }
    } else if (thyroidtotal == livertotal) {
        yourbodytype = 'LIVER';
    } else if (thyroidtotal == adrenaltotal) {
        if (gender == 'male') {
            yourbodytype = 'ADRENAL';
        } else {
            yourbodytype='OVARY';
        }
    } else if (livertotal == adrenaltotal) {
        yourbodytype = 'ADRENAL';
    }

    var result = {
		'thyroidpercent': thyroidpercent,
		'adrenalpercent': adrenalpercent,
		'liverpercent': liverpercent,
        'ovarypercent': ovarypercent,       
        'yourbodytype': yourbodytype
    };

     return result;
}