/*
        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" : "";
      error_report += (f.phone1.value == "") ? "* You must enter your phone number.\n" : "";
      break;

    case "register":
      error_report += (f.title.value == "") ? "* You must enter your title.\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.parish.value == "") ? "* You must enter your parish.\n" : "";
      error_report += (f.parishAddress.value == "") ? "* You must enter your parish address.\n" : "";
      error_report += (f.phone.value == "") ? "* You must enter your phone number.\n" : "";
      error_report += (f.role.value == "") ? "* You must enter your role at the parish.\n" : "";
      break;

    case "cm_survey":
      if (button_clicked == 'submit') {
        error_report += (f.title.value == "") ? "* You must enter your title.\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.parish.value == "" || f.parish.value == "0") ? "* You must choose your parish.\n" : "";
        if (!f.liturgy[0].checked && !f.liturgy[1].checked) {
          error_report += "* Please nominate whether your parish conducts children\'s Liturgy at weekend Mass.\n";
        }
        else {
          error_report += (f.liturgy[0].checked && f.liturgy_leaders.value == "") ? "* Please enter the number of Children\'s Liturgy volunteers you have.\n" : "";
        }
        if (!f.sacramental[0].checked && !f.sacramental[1].checked) {
          error_report +=  "* Please nominate whether your parish has a Sacramental Co-ordinator.\n";
        }
        else {
          error_report += (f.sacramental[0].checked && !f.sacramental_team[0].checked && !f.sacramental_team[1].checked) ? "* Please nominate whether your Sacramental Co-ordinator has a team.\n" : "";
          error_report += (f.sacramental[0].checked && f.sacramental_team[0].checked && f.sacramental_leaders.value == "") ? "* Please enter the number of members in your Sacramental Team.\n" : "";
        }
        error_report += (!f.ssre_teacher[0].checked && !f.ssre_teacher[1].checked) ? "* Please nominate whether your parish has any State School RE teachers.\n" : "";
        error_report += (!f.ssre_coord[0].checked && !f.ssre_coord[1].checked) ? "* Please nominate whether your parish has a State School RE Co-ordinator.\n" : "";
        if (f.ssre_teacher[0].checked || f.ssre_coord[0].checked) {
          error_report += (f.ssre_volunteers.value == "") ? "* Please nominate the number of State School RE Volunteers you have.\n" : "";
          error_report += (!f.ssre_catholic[0].checked && !f.ssre_catholic[1].checked) ? "* Please nominate whether your State School RE team teaches Catholic children only.\n" : "";
          error_report += (!f.ssre_christian[0].checked && !f.ssre_christian[1].checked) ? "* Please nominate whether your State School RE team teach co-operatively with other Christian churches.\n" : "";
        }
        error_report += (!f.cp_osh[0].checked && !f.cp_osh[1].checked) ? "* Please nominate whether your parish runs out of school hours catechetical programs.\n" : "";
        error_report += (!f.cp_holiday[0].checked && !f.cp_holiday[1].checked) ? "* Please nominate whether your parish runs catechetical holiday programs.\n" : "";
        error_report += (f.cp_osh[0].checked && f.cp_osh_team.value == "") ? "* Please nominate the number of team members in your out of school hours team.\n" : "";
        error_report += (f.cp_holiday[0].checked && f.cp_holiday_team.value == "") ? "* Please nominate the number of team members in your catechetical holiday programs team.\n" : "";
        if (f.cp_osh[0].checked || f.cp_holiday[0].checked) {
          error_report += (!f.cp_coord[0].checked && !f.cp_coord[1].checked) ? "* Please nominate whether you have a co-ordinator for your Catechetical Programs.\n" : "";
        }
      }
      else {
        error_report += (f.title.value == "") ? "* You must enter your title.\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.email.value == "") ? "* To save for later you must enter your email address.\n" : "";
      }
      break;
  }
  if (error_report != "") {
    alert("There were errors in the completion of form.\n\n"+error_report);
    return false;
  } 
  else {
    return true;
  }
}


/*------------------------------------------------------------*/
/* 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") {
    document.order.d[2].checked = true;
    if (c.value != "New Zealand") {
      document.getElementById("seamail").style.display = "block";
      document.getElementById("seamail_note").style.display = "block";
    }
    else {
      document.getElementById("seamail").style.display = "none";
      document.getElementById("seamail_note").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.d[0].checked = true;
    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";
    document.getElementById("seamail_note").style.display = "none";
  }
}


/*--------------------------------------*/
/* Function: set_button(value)          */
/* Sets the global button clicked value */
/*--------------------------------------*/
function set_button(value) {
  button_clicked = value;
}

