/* ############################## MAIN NAV MENU ############################## */

////==== preload mnav images
function preload_mnav() {
    mnav_1 = new Image();
    mnav_1.src = "mnav_about_alan_on.gif";
    mnav_2 = new Image();
    mnav_2.src = "mnav_integrated_safety_on.gif";
    mnav_3 = new Image();
    mnav_3.src = "mnav_publications_on.gif";
    mnav_4 = new Image();
    mnav_4.src = "mnav_courses_on.gif";
    mnav_5 = new Image();
    mnav_5.src = "mnav_resources_on.gif";
    mnav_6 = new Image();
    mnav_6.src = "mnav_contact_on.gif";
}

////==== change mnav image off/on
function roll_mnav(num, oo) {
    var img = document.getElementById("mnav_" + num);

    var fname = img.src.substring(img.src.lastIndexOf("/") + 1);
    var sec = fname.substring(0, fname.lastIndexOf("_"));

    img.src = sec + "_" + oo + ".gif";
}

/* ############################## SUB NAV MENU ############################## */

////==== preload snav images
function preload_snav() {
    snav_1 = new Image();
    snav_1.src = "snav_about_joe_on.gif";
    snav_2 = new Image();
    snav_2.src = "snav_archives_on.gif";
    snav_3 = new Image();
    snav_3.src = "snav_articles_on.gif";
    snav_4 = new Image();
    snav_4.src = "snav_components_on.gif";
    snav_5 = new Image();
    snav_5.src = "snav_consulting_coaching_on.gif";
    snav_6 = new Image();
    snav_6.src = "snav_crsp_courses_on.gif";
    snav_7 = new Image();
    snav_7.src = "snav_e_newsletter_on.gif";
    snav_8 = new Image();
    snav_8.src = "snav_free_info_downloads_on.gif";
    snav_9 = new Image();
    snav_9.src = "snav_ism_works_on.gif";
    snav_10 = new Image();
    snav_10.src = "snav_key_messages_on.gif";
    snav_11 = new Image();
    snav_11.src = "snav_news_on.gif";
    snav_12 = new Image();
    snav_12.src = "snav_related_links_on.gif";
    snav_13 = new Image();
    snav_13.src = "snav_safety_book_on.gif";
    snav_14 = new Image();
    snav_14.src = "snav_safety_dvds_on.gif";
    snav_15 = new Image();
    snav_15.src = "snav_speaking_presentations_on.gif";
    snav_16 = new Image();
    snav_16.src = "snav_training_courses_on.gif";
    snav_17 = new Image();
    snav_17.src = "snav_training_on.gif";
}

////==== change snav image off/on
function roll_snav(num, oo) {
    var img = document.getElementById("snav_" + num);

    var fname = img.src.substring(img.src.lastIndexOf("/") + 1);
    var sec = fname.substring(0, fname.lastIndexOf("_"));

    img.src = sec + "_" + oo + ".gif";
}

/* ############################## ISM ############################## */

// content - quilley-ism
ism_content_q = new Array();
ism_content_q.push("A collection of elements is not a safety management system any more than a collection of food ingredients is a meal - and rating one element as more important than another is like saying that the reason a six-course meal was wonderful is that the chef put garlic in the potatoes.");
ism_content_q.push("When people want to do a good job, positive feedback is the thing that lets them know they're doing it.");
ism_content_q.push("Most things that go wrong in safety, go wrong because nobody was held accountable.");
ism_content_q.push("Measure it and they'll do it. Add a reward, and they'll do it with enthusiasm.");
ism_content_q.push("A safety policy posted on the wall means nothing. Activities mean it's happening. Accountability means it's happening.");
ism_content_q.push("If we increase the quality and quantity of our safety discussions, we're going to be very happy with the results.");
ism_content_q.push("One secret to successfully selling safety is to ceaselessly emphasize that safety isn't in competition with production. Safety is not a goal, it's a value; it's a feature of how you produce. We need safe production.");
ism_content_q.push("Safety is a value, not a goal or a priority. A company's priorities change, and its goals change accordingly. Its values, however, are constant. Values are the way we do things while pursuing our goals in accordance with our priorities. Safety, like quality, is a value.");
ism_content_q.push("Safety doesn't just happen... It comes as a result of intelligent planning, thoughtful activity, skilful execution, constant measurement and continual adjustment.");

// content - testimonial
ism_content_t = new Array();
ism_content_t.push("\"Alan's approach helps even the most seasoned safety professionals re-examine their traditional thinking and inspires them to explore new ideas and concepts.\"");
ism_content_t.push("\"Alan's passion, wealth of knowledge and vast experience in OH&S has placed him on par with the world's leaders. His delivery style is unique and refreshing - he has the uncanny ability to make safety interesting and fun, while at the same time drives safety concepts and principles home!\"");
ism_content_t.push("\"What an INTERVENTION FOR OH&S MANAGEMENT! Alan's book will finally open eyes and start motivating people to stop using common and out-of-date OH&S management systems.\"");
ism_content_t.push("\"I'm about half-way through your book... what can I say, WOW! You've got a real winner here - easy read and highly entertaining. I think the first time I sat down to read it, I almost read 100 pages.\"");
ism_content_t.push("\"Alan is a great instructor, and extremely knowledgeable about the subject matter. He brings personal experience to illustrate the concepts he is explaining. Great class.\"");

// content
var ism_content = new Array(ism_content_q, ism_content_t);

// titles
var ism_title = new Array("QUILLEY-ISM", "TESTIMONIAL");

// flag
var ism_flag = Math.floor(Math.random() * ism_title.length);

////==== write ism title
function ism_write_title() {
    document.writeln(ism_title[ism_flag]);
}

////==== write ism content
function ism_write_content() {
    var i = Math.floor(Math.random() * ism_content[ism_flag].length)

    document.writeln(ism_content[ism_flag][i]);
}

////==== write ism image
function ism_write_image() {
    var i = Math.ceil(Math.random() * 6)

    document.writeln("<img src=\"fg_ism_" + i + ".gif\">");
}

/* ############################## SEARCH FORM ############################## */

////==== submit search form
function submit_search_form() {
    // check form
    if (check_search_form()) {
        // submit form
        document.search_form.submit();
    }
}

////==== check search form
function check_search_form() {
    // trim and get all necessary field values that need to be checked
    var val_search_query = document.search_form.search_query.value = trim_string(squeeze_string(document.search_form.search_query.value));

    // search query check
    if (val_search_query.length == 0) {
        document.search_form.search_query.select();
        return false;
    }

    // return
    return true;
}

/* ############################## NEWSLETTER FORM ############################## */

////==== submit newsletter form
function submit_newsletter_form() {
    // check form
    if (check_newsletter_form()) {
        // submit form
        document.newsletter_form.submit();
    }
}

////==== check newsletter form
function check_newsletter_form() {
    // trim and get all necessary field values that need to be checked
    var val_email_address = document.newsletter_form.email_address.value = trim_string(squeeze_string(document.newsletter_form.email_address.value));

    // search query check
    if (val_email_address.length == 0) {
        document.newsletter_form.email_address.select();
        return false;
    }

    // return
    return true;
}

/* ############################## NEWSLETTER MESSAGE ############################## */

////==== display appropriate message
function newsletter_alert() {
    // get query string
    var action = location.href.substring(location.href.indexOf("?") + 1);

    // error
    if (action == "error") {
        alert("Sorry! Please enter a valid email address.");
        location.replace("e_newsletter.html");
    }
    // in
    else if (action == "in") {
        alert("Thank you! You have been subscribed to the Safety Results newsletter.");
        location.replace("e_newsletter.html");
    }
    // out
    else if (action == "out") {
        alert("Thank you! You have been unsubscribed from the Safety Results newsletter.");
        location.replace("e_newsletter.html");
    }
}

/* ############################## E-COMMERCE ############################## */

function checkMaxQty(fld, value, orig) {
    if (parseInt(value) > 10) {
        alert("For quantities over 10, please contact us for a volume discount.");
        fld.value = orig;
        fld.focus();
    }
}

/* ############################## DISCLAIMER ############################## */

// disclaimer file
var disc_file;

// disclaimer interval id
var disc_id;

////==== download based on disclaimer agreement
function disc_download(filename) {
    // update file
    disc_file = filename;

    // cookie exists
    if (document.cookie.indexOf("disclaimer") >= 0) {
        // open document
        window.open("pdf/form/" + disc_file, "_blank");
    }
    // cookie doesn't exist
    else {
        // show disclaimer
        disc_window = window.open("disclaimer.html", "disc_window", "width=540,height=340,resizable=no,scrollbars=no");
        disc_window.focus();

        // stop waiting
        clearInterval(disc_id);

        // wait for cookie
        disc_id = setInterval("disc_wait()", 1000);
    }
}

////==== wait for disclaimer cookie
function disc_wait() {
    // cookie exists
    if (document.cookie.indexOf("disclaimer") >= 0) {
        // stop waiting
        clearInterval(disc_id);

        // open document
        window.open("pdf/form/" + disc_file, "_blank");
    }
}

////==== agree/disagree to disclaimer
function disc_agree(yes) {
    // agree
    if (yes) {
        // write cookie
        document.cookie = "disclaimer=yes";
    }
    // not agree
    else {
        // stop waiting
        clearInterval(disc_id);
    }

    // close window
    window.close();
}

/* ############################## STRINGS ############################## */

////==== trim given string of leading and trailing whitespace
function trim_string(str) {
    // trim leading
    str = str.replace(/^[\s]+/, "");

    // trim trailing
    str = str.replace(/[\s]+$/, "");

    return str;
}


////==== squeeze multiple spaces into one in given string
function squeeze_string(str) {
    // squeeze
    str = str.replace(/[\s]+/g, " ");

    return str;
}

function validate_required(field,alerttxt)
{
with (field)
  {
  if (value==null||value=="")
    {
    alert(alerttxt);return false;
    }
  else
    {
    return true;
    }
  }
}

function validate_form(thisform)
{
with (thisform)
  {
  if (validate_required(shipping,"Quantity was not selected")==false)
  {shipping.focus();return false;}
  }
}

