/*
        File: formScripts.js
     Version: 1.0

     Created: 25/06/09
    Modified: 25/06/09
   Copyright: Matthew Cassidy 2005
       Email: cassidym@bne.catholic.net.au

     Purpose: Provides form validation and other form usage scripts
        Note: None

     License: For use solely by the Catholic Archdiocese of Brisbane. 
*/

var button_clicked;

/*--------------------------*/
/* Function: check_form(f)  */
/* Performs form validation */
/*--------------------------*/
function check_form(f) {
  var error_report = "";
  switch (f.frm.value) {
    case "cart-add":
      error_report += (f.v.value == "-") ? "* You must choose a product type to order.\n" : "";
      error_report += (f.q.value == "") ? "* You must enter a quantity to purchase.\n" : "";
      error_report += (f.q.value != "" && isNaN(f.q.value)) ? "* The quantity must be a number.\n" : "";
      error_report += (f.q.value != "" && !isNaN(f.q.value) && f.q.value <= 0) ? "* The quantity must be a positive number.\n" : "";
      if (f.q.value != "" && !isNaN(f.q.value) && f.q.value > 0) {
        for (i=0; i<f.q.value.length; i++) {
          error_report += (f.q.value.charAt(i) < "0" || f.q.value.charAt(i) > "9") ? "* The quantity must be a whole number.\n" : "";
        }
        error_report += (parseInt(f.l.value) > 0 && f.q.value < parseInt(f.l.value)) ? "* This product has a minimum purchase level. You must order "+f.l.value+" or more products.\n" : "";
        error_report += (parseInt(f.h.value) > 0 && f.q.value > parseInt(f.h.value)) ? "* This product has a maximum purchase level. You must order "+f.h.value+" or less products.\n" : "";
      }
      break;

    case "cart-update":
      error_report += (f.c.value == "Australia" && f.s.value == 0) ? "* Please choose your state within Australia.\n" : "";
      error_report += (f.c.value == "Australia" && f.p.value == "") ? "* Please enter your postcode.\n" : "";
      error_report += (!f.d[0].checked && !f.d[1].checked && !f.d[2].checked && !f.d[3].checked) ? "* Please chose a preferred delivery option.\n" : "";
      break;

    case "place-order":
      error_report += (f.fname.value == "") ? "* You must enter your first name.\n" : "";
      error_report += (f.lname.value == "") ? "* You must enter your last name.\n" : "";
      error_report += (f.address.value == "") ? "* You must enter your address.\n" : "";
      error_report += (f.suburb.value == "") ? "* You must enter your suburb.\n" : "";
      error_report += (f.state.value == "") ? "* You must enter your state.\n" : "";
      error_report += (f.postCode.value == "") ? "* You must enter your post code.\n" : "";
      error_report += (f.country.value == "") ? "* You must enter your country.\n" : "";
      break;

    case "pres":
      if (button_clicked == 'submit') {
        error_report += (f.fname.value == "") ? "* You must enter your first name.\n" : "";
        error_report += (f.lname.value == "") ? "* You must enter your last name.\n" : "";
        error_report += (f.tagName.value == "") ? "* You must enter your tag name.\n" : "";
        error_report += (f.address.value == "") ? "* You must enter your address.\n" : "";
        error_report += (f.town.value == "") ? "* You must enter your town or city.\n" : "";
        error_report += (f.state.value == "") ? "* You must enter your state.\n" : "";
        error_report += (f.postCode.value == "") ? "* You must enter your post (zip) code.\n" : "";
        error_report += (f.country.value == "") ? "* You must enter your country.\n" : "";
        error_report += (f.email.value == "") ? "* You must nominate your preferred email address.\n" : "";
        error_report += (f.photo.value == "" && f.photo_current.value == "FALSE") ? "* You must choose a photo to attach.\n" : "";
        if (f.t.value != 0) {
          error_report += (!f.transport[0].checked && !f.transport[1].checked && !f.transport[2].checked) ? "* You must nominate your transport intentions.\n" : "";        
        }
        if (f.a.value != 0) {
          error_report += (!f.accommodation[0].checked && !f.accommodation[1].checked && !f.accommodation[2].checked) ? "* You must nominate your accommodation intentions.\n" : "";        
          error_report += (f.accommodation[1].checked && !f.accomm_7_7.checked && !f.accomm_8_7.checked && !f.accomm_9_7.checked && !f.accomm_10_7.checked) ? "* You must choose the nights you are likely to require accommodation.\n" : "";        
        }
        if (f.keynote_count.value > 0) {
          error_report += (f.keynote_bio.value == "") ? "* You must enter you biographical note for keynotes.\n" : "";
          error_report += (!f.keynote_is_record[0].checked && !f.keynote_is_record[1].checked) ? "* You must nominate whether your keynote presentations may be recorded and reproduced.\n" : "";
          keynoteErr = false;
          for (i=1; i<=f.keynote_count.value; i++) {
            (eval("f.keynote_"+i).value == "") ? keynoteErr = true : "";
          }
          error_report += (keynoteErr) ? "* Please ensure all keynote descriptions have been entered.\n" : "";
        }
        if (f.workshop_count.value > 0) {
          error_report += (f.workshop_bio.value == "") ? "* You must enter you biographical note for workshop presentations.\n" : "";
          workshopErr = false;
          for (i=1; i<=f.workshop_count.value; i++) {
            (eval("f.workshop_"+i).value == "") ? workshopErr = true : "";
          }
          error_report += (workshopErr) ? "* Please ensure all workshop presentation descriptions have been entered.\n" : "";
        }
        if (error_report == "") {
          if (confirm('Are you sure you want to submit your information to Pray 2010?\nOnce submitted you cannot change the information online.')) {
            return true;
          }
          else {
            return false;
          }
        }
      }
      break;

    case "pres-2":
      if (button_clicked == 'submit') {
        error_report += (f.fname.value == "") ? "* You must enter your first name.\n" : "";
        error_report += (f.lname.value == "") ? "* You must enter your last name.\n" : "";
        error_report += (f.tagName.value == "") ? "* You must enter your tag name.\n" : "";
        error_report += (f.address.value == "") ? "* You must enter your address.\n" : "";
        error_report += (f.town.value == "") ? "* You must enter your town or city.\n" : "";
        error_report += (f.state.value == "") ? "* You must enter your state.\n" : "";
        error_report += (f.postCode.value == "") ? "* You must enter your post (zip) code.\n" : "";
        error_report += (f.country.value == "") ? "* You must enter your country.\n" : "";
        error_report += (f.email.value == "") ? "* You must nominate your preferred email address.\n" : "";
        error_report += (f.keynote_count.value > 0 && f.keynote_bio.value == "") ? "* You must nominate biographical notes for your keynote introductions.\n" : "";
        error_report += (f.workshop_bio.value == "") ? "* You must nominate biographical notes for your workshop introductions.\n" : "";
        if (f.t.value != 0) {
          error_report += (!f.transport[0].checked && !f.transport[1].checked && !f.transport[2].checked) ? "* You must nominate your transport intentions.\n" : "";        
        }
        if (f.a.value != 0) {
          error_report += (!f.accommodation[0].checked && !f.accommodation[1].checked && !f.accommodation[2].checked) ? "* You must nominate your accommodation intentions.\n" : "";        
          error_report += (f.accommodation[1].checked && !f.accomm_7_7.checked && !f.accomm_8_7.checked && !f.accomm_9_7.checked && !f.accomm_10_7.checked) ? "* You must choose the nights you are likely to require accommodation.\n" : "";        
        }
        if (f.keynote_count.value > 0) {
          keyErr = false;
          for (i=1; i<=f.keynote_count.value; i++) {
            if (eval("f.see_partner_k_"+i).checked) {
            }
            else {
              (eval("f.is_handout_k_"+i)[0].checked || eval("f.is_handout_k_"+i)[1].checked) ? "" : keyErr = true;
            }
          }
          error_report += (keyErr) ? "* Please ensure you nominate whether you will be using handounts for all keynote presentations.\n" : "";
        }
        if (f.workshop_count.value > 0) {
          workshopErr = false;
          workshopErr2 = false;
          workshopErr3 = false;
          for (i=1; i<=f.workshop_count.value; i++) {
            if (eval("f.see_partner_"+i).checked) {
            }
            else {
              (eval("f.is_chairs_"+i)[0].checked || eval("f.is_chairs_"+i)[1].checked) ? "" : workshopErr = true;
              if (eval("f.is_chairs_"+i)[1].checked) {
                (eval("f.chairs_style_"+i)[0].checked || eval("f.chairs_style_"+i)[1].checked || eval("f.chairs_style_"+i)[2].checked || eval("f.chairs_style_"+i)[3].checked || eval("f.chairs_style_"+i)[4].checked) ? "" : workshopErr2 = true;
              }
              (eval("f.is_handout_"+i)[0].checked || eval("f.is_handout_"+i)[1].checked) ? "" : workshopErr3 = true;
            }
          }
          error_report += (workshopErr) ? "* Please ensure you choose room setup options for all workshops.\n" : "";
          error_report += (workshopErr2) ? "* Please ensure you choose chair layout options for all workshops.\n" : "";
          error_report += (workshopErr3) ? "* Please ensure you nominate whether you will be using handounts for all workshops.\n" : "";
        }
        if (error_report == "") {
          if (confirm('Are you sure you want to submit your information to Pray 2010?\nOnce submitted you cannot change the information online.')) {
            return true;
          }
          else {
            return false;
          }
        }
      }
      break;

    case "vol":
      error_report += (f.fname.value == "") ? "* You must enter your first name.\n" : "";
      error_report += (f.lname.value == "") ? "* You must enter your last name.\n" : "";
      error_report += (f.tagName.value == "") ? "* You must enter your tag name.\n" : "";
      error_report += (f.address.value == "") ? "* You must enter your address.\n" : "";
      error_report += (f.town.value == "") ? "* You must enter your town or city.\n" : "";
      error_report += (f.postCode.value == "") ? "* You must enter your post code.\n" : "";
      error_report += (f.day_phone.value == "") ? "* You must nominate your daytime phone number.\n" : "";
      error_report += (f.email.value == "") ? "* You must nominate your email address.\n" : "";
      error_report += (f.org.value == "") ? "* You must nominate your parish / school / agency / organisation.\n" : "";
      error_report += (!f.set_up_mon.checked && !f.set_up_tue.checked && !f.set_up_wed.checked && !f.set_up_sun.checked && f.wed_task[0].checked && f.thu_task[0].checked && f.fri_task[0].checked && f.sat_task[0].checked) ? "* You must choose which task/s you will be available for.\n" : "";
      break;

    case "vol-pr":
      error_report += (f.fname.value == "") ? "* You must enter your first name.\n" : "";
      error_report += (f.lname.value == "") ? "* You must enter your last name.\n" : "";
      error_report += (f.tagName.value == "") ? "* You must enter your tag name.\n" : "";
      error_report += (f.address.value == "") ? "* You must enter your address.\n" : "";
      error_report += (f.town.value == "") ? "* You must enter your town or city.\n" : "";
      error_report += (f.postCode.value == "") ? "* You must enter your post code.\n" : "";
      error_report += (f.day_phone.value == "") ? "* You must nominate your daytime phone number.\n" : "";
      error_report += (f.email.value == "") ? "* You must nominate your email address.\n" : "";
      error_report += (f.org.value == "") ? "* You must nominate your parish / school / agency / organisation.\n" : "";
      error_report += ((f.t_9.value == "" || f.t_9.value == "0") && (f.t_10.value == "" || f.t_10.value == "0") && (f.t_11.value == "" || f.t_11.value == "0") && (f.t_12.value == "" || f.t_12.value == "0") && (f.t_1.value == "" || f.t_1.value == "0") && (f.t_2.value == "" || f.t_2.value == "0") && (f.t_3.value == "" || f.t_3.value == "0") && (f.t_4.value == "" || f.t_4.value == "0") && (f.t_5.value == "" || f.t_5.value == "0") && (f.t_6.value == "" || f.t_6.value == "0") && (f.f_9.value == "" || f.t_9.value == "0") && (f.f_10.value == "" || f.f_10.value == "0") && (f.f_11.value == "" || f.f_11.value == "0") && (f.f_12.value == "" || f.f_12.value == "0") && (f.f_1.value == "" || f.f_1.value == "0") && (f.f_2.value == "" || f.f_2.value == "0") && (f.f_3.value == "" || f.f_3.value == "0") && (f.f_4.value == "" || f.f_4.value == "0") && (f.f_5.value == "" || f.f_5.value == "0") && (f.f_6.value == "" || f.f_6.value == "0") && (f.s_9.value == "" || f.s_9.value == "0") && (f.s_10.value == "" || f.s_10.value == "0") && (f.s_11.value == "" || f.s_11.value == "0") && (f.s_12.value == "" || f.s_12.value == "0") && (f.s_1.value == "" || f.s_1.value == "0") && (f.s_2.value == "" || f.s_2.value == "0") && (f.s_3.value == "" || f.s_3.value == "0") && (f.s_4.value == "" || f.s_4.value == "0") && (f.s_5.value == "" || f.s_5.value == "0") && (f.s_6.value == "" || f.s_6.value == "0")) ? "* You must choose which times you will be available for praying in the Prayer Room.\n" : "";
      error_report += (isNaN(f.t_9.value) || isNaN(f.t_10.value) || isNaN(f.t_11.value) || isNaN(f.t_12.value) || isNaN(f.t_1.value) || isNaN(f.t_2.value) || isNaN(f.t_3.value) || isNaN(f.t_4.value) || isNaN(f.t_5.value) || isNaN(f.t_6.value) || isNaN(f.f_9.value) || isNaN(f.f_10.value) || isNaN(f.f_11.value) || isNaN(f.f_12.value) || isNaN(f.f_1.value) || isNaN(f.f_2.value) || isNaN(f.f_3.value) || isNaN(f.f_4.value) || isNaN(f.f_5.value) || isNaN(f.f_6.value) || isNaN(f.s_9.value) || isNaN(f.s_10.value) || isNaN(f.s_11.value) || isNaN(f.s_12.value) || isNaN(f.s_1.value) || isNaN(f.s_2.value) || isNaN(f.s_3.value) || isNaN(f.s_4.value) || isNaN(f.s_5.value) || isNaN(f.s_6.value)) ? "* You must enter only numbers into the availability table.\n" : "";
      break;

    case "vol-pr-off":
      error_report += (f.fname.value == "") ? "* You must enter your first name.\n" : "";
      error_report += (f.lname.value == "") ? "* You must enter your last name.\n" : "";
      error_report += (f.address.value == "") ? "* You must enter your address.\n" : "";
      error_report += (f.town.value == "") ? "* You must enter your town or city.\n" : "";
      error_report += (f.postCode.value == "") ? "* You must enter your post code.\n" : "";
      error_report += (f.email.value == "") ? "* You must nominate your email address.\n" : "";
      error_report += (!f.t_9.checked && !f.t_10.checked && !f.t_11.checked && !f.t_12.checked && !f.t_1.checked && !f.t_2.checked && !f.t_3.checked && !f.t_4.checked && !f.t_5.checked && !f.t_6.checked && !f.f_9.checked && !f.f_10.checked && !f.f_11.checked && !f.f_12.checked && !f.f_1.checked && !f.f_2.checked && !f.f_3.checked && !f.f_4.checked && !f.f_5.checked && !f.f_6.checked && !f.s_9.checked && !f.s_10.checked && !f.s_11.checked && !f.s_12.checked && !f.s_1.checked && !f.s_2.checked && !f.s_3.checked && !f.s_4.checked && !f.s_5.checked && !f.s_6.checked) ? "* You must nominate when you will be praying for Pray 2010" : "";
      break;

    case "reg-step-1":
      if (!f.is_meal_only.checked && !f.wed_eve.checked && !f.thu_mor.checked && !f.thu_aft.checked && !f.thu_eve.checked && !f.fri_mor.checked && !f.fri_aft.checked && !f.fri_eve.checked && !f.sat_mor.checked && !f.sat_aft.checked && !f.sat_eve.checked) {
        error_report += "* You must choose at least one session to attend.\n";
      }
      else if (!f.is_meal_only.checked && !f.wed_eve.checked && !f.thu_mor.checked && !f.thu_aft.checked && !f.thu_eve.checked && !f.fri_mor.checked && !f.fri_aft.checked && !f.fri_eve.checked && !f.sat_mor.checked && !f.sat_aft.checked && f.sat_eve.checked) {
        //error_report += "* You must attend at least one session other than the closing session.\n";
      }
      error_report += (!f.group[0].checked && !f.group[1].checked) ? "* You must nominate whether you are registering on behalf of a group.\n" : "";
      error_report += (f.fname.value == "") ? "* You must enter your first name.\n" : "";
      error_report += (f.lname.value == "") ? "* You must enter your last name.\n" : "";
      error_report += (f.tagName.value == "") ? "* You must enter the name you would like on your name tag.\n" : "";
      error_report += (f.address.value == "") ? "* You must enter your address.\n" : "";
      error_report += (f.suburb.value == "") ? "* You must enter your suburb.\n" : "";
      error_report += (f.town.value == "") ? "* You must enter your town.\n" : "";
      error_report += (f.state.value == "") ? "* You must enter your state.\n" : "";
      error_report += (f.postCode.value == "") ? "* You must enter your post code.\n" : "";
      error_report += (f.country.value == "") ? "* You must enter your country.\n" : "";
      error_report += (f.phone1.value == "") ? "* You must enter your phone number.\n" : "";
      error_report += (f.email.value == "") ? "* You must enter your email address.\n" : "";
      error_report += (!f.concession[0].checked && !f.concession[1].checked && !f.concession[2].checked && !f.concession[3].checked) ? "* You must nominate whether you are eligible for a concession.\n" : "";
      error_report += (f.payment[1].checked && f.presCode.value == "") ? "* You must enter your presenter code provided by Pray 2010.\n" : "";
      error_report += (f.payment[2].checked && f.volCode.value == "") ? "* You must enter your volunteer code provided by Pray 2010.\n" : "";
      break;

    case "pres-login":
      error_report += (f.presenter_name.value == "0") ? "* You must choose your name.\n" : "";
      error_report += (f.password.value == "") ? "* You must enter your password.\n" : "";
      break;

    case "pres-register":
      error_report += (f.name.value == "0") ? "* You must enter your name.\n" : "";
      error_report += (f.town.value == "0") ? "* You must enter your town.\n" : "";
      error_report += (f.country.value == "0") ? "* You must enter your country.\n" : "";
      error_report += (f.intention.value == "0") ? "* You must enter your prayer intention.\n" : "";
      break;

    case "register-story":
      error_report += (f.name.value == "0") ? "* You must enter your name.\n" : "";
      error_report += (f.town.value == "0") ? "* You must enter your town.\n" : "";
      error_report += (f.country.value == "0") ? "* You must enter your country.\n" : "";
      error_report += (f.story.value == "0") ? "* You must enter your Pray 2010 story.\n" : "";
      break;
  }
  if (error_report != "") {
    alert("There were errors in the completion of form.\n\n"+error_report);
    return false;
  } 
  else {
    return true;
  }
}


/*--------------------------------------*/
/* Function: set_button(value)          */
/* Sets the global button clicked value */
/*--------------------------------------*/
function set_button(value) {
  button_clicked = value;
}


/*--------------------------------------------------------------*/
/* Function: confirm_registration(f)                            */
/* Gives the user one last chance to back out of a registration */
/*--------------------------------------------------------------*/
function confirm_registration(f) {
  f.confirm.disabled = true; 
  if (confirm('Are you sure you want to finalise your Pray 2010 registration?\nOnce finalised you cannot change the sessions chosen.')) {
    return true;
  }
  else {
    f.confirm.disabled = false; 
    return false;
  }
}


/*------------------------------------------------------------*/
/* Function: update_state_and_postage(c)                      */
/* Updates required shipping fields depending on choices made */
/*------------------------------------------------------------*/
function update_state_and_postage(c) {
  document.order.d[0].checked = false;
  document.order.d[1].checked = false;
  document.order.d[2].checked = false;
  document.order.d[3].checked = false;
  if (c.value != "Australia") {
    if (c.value != "New Zealand") {
      document.getElementById("seamail").style.display = "block";
    }
    else {
      document.getElementById("seamail").style.display = "none";
    }
    document.order.s.value = 0;
    document.order.s.disabled = true;
    document.getElementById("local").style.display = "none";
    document.getElementById("international").style.display = "block";
  }
  else {
    document.order.s.value = "QLD";
    document.order.s.disabled = false;
    document.getElementById("local").style.display = "block";
    document.getElementById("international").style.display = "none";
    document.getElementById("seamail").style.display = "none";
  }
}