/*  Copyright 2008 Dynacor Media Group  */

/* ############################## ROLL ############################## */

////==== change image off/on
function roll(id, oo) {
    var img = document.getElementById(id);

    var dname = img.src.substring(0, img.src.lastIndexOf("/"));
    var fname = img.src.substring(img.src.lastIndexOf("/") + 1);
    var sec = fname.substring(0, fname.lastIndexOf("_"));

    img.src = dname + "/" + sec + "_" + oo + ".gif";
}

/* ############################## MAIN NAV MENU ############################## */

////==== show/hide main nav menu
function mnav(num, oo) {
    var mlink = document.getElementById("mlink_" + num);
    var snav = document.getElementById("snav_" + num);

    if (oo) {
        mlink.className = appcho_word(mlink.className, true, "on");
        snav.style.display = "block";

        // find total number of links
        var total = 0;

        for (var i = 0; snav.childNodes[i]; i++) {
            if (snav.childNodes[i].nodeName.toUpperCase() == "A") {
                total++;
            }
        }

        for (var i = 1; i <= total; i++) {
            var thirdsnav = document.getElementById("thirdsnav_" + num + "_" + i);

            // right align third level nav
            if (thirdsnav) {
                if ((snav.offsetLeft + snav.offsetWidth + thirdsnav.offsetLeft + thirdsnav.offsetWidth) > 940) {
                    thirdsnav.style.right = "100%";
                    thirdsnav.style.left = "auto";
                }
            }
        }

        // right align second level nav
        if ((snav.offsetLeft + snav.offsetWidth) > 940) {
            snav.style.left = ((mlink.offsetLeft + mlink.offsetWidth + 0.1) - snav.offsetWidth) + "px";
        }
    }
    else {
        mlink.className = appcho_word(mlink.className, false);
        snav.style.display = "none";
    }
}

/* ############################## DROP-OUT MENU ############################## */

////==== show/hide drop-out menu
function tsnav(num, oo) {
    var tmlink = document.getElementById("tmlink_" + num);
    var thirdsnav = document.getElementById("thirdsnav_" + num);

    if (oo) {
        tmlink.className = appcho_word(tmlink.className, true, "on");
        thirdsnav.style.display = "block";
        //alert("it works!");
    }
    else {
        tmlink.className = appcho_word(tmlink.className, false);
        thirdsnav.style.display = "none";
    }
}

/* ############################## PHOTO ############################## */

////==== load photo image
function load_photo(num) {
    var photo_image = document.getElementById("photo_image");
    var photo_link = document.getElementById("photo_link");

    var photo_link_list = new Object();
    photo_link_list[1] = "index.php";
    photo_link_list[2] = "http://www.homewardtrust.ca/uploads/2008%20Final%20Homeless%20Count%20Report.pdf";
    photo_link_list[3] = "index.php?page=projects";
    photo_link_list[4] = "http://www.homewardtrust.ca/homeless-connect-edmonton/";
    photo_link_list[5] = "index.php?page=advertising";

    if (num == undefined) {
        num = Math.ceil(Math.random() * 5);
    }

    photo_image.src = "images/photo_" + num + ".jpg";
    photo_link.href = photo_link_list[String(num)];
}

////==== load photo image (home)
function load_photo_home() {
    load_photo(1);
}

/* ############################## CONTACT FORM ############################## */

////==== initialize form
function initialize_contact_us_form() {
    // get query string
    var args = location.search.substring(1).toLowerCase().split("&");

    // second argument given
    if (args[1] != undefined) {
        var action = args[1].split("=")[1];

        // sent
        if (action == "sent") {
            alert("Thank you! Your message has been sent.");
            location.replace("index.php?page=contact");
        }
    }
}

////==== check form for malformed input
function check_contact_us_form() {
    // trim and get all necessary field values that need to be checked
    var var_full_name = document.contact_us_form.full_name.value = trim_string(document.contact_us_form.full_name.value);
    var var_phone_number = document.contact_us_form.phone_number.value = trim_string(document.contact_us_form.phone_number.value);
    var var_email_address = document.contact_us_form.email_address.value = trim_string(document.contact_us_form.email_address.value);
    var var_subject = document.contact_us_form.subject.value = trim_string(document.contact_us_form.subject.value);

    // full_name
    if (var_full_name.length == 0) {
        alert("Please fill in your full name!");
        document.contact_us_form.full_name.focus();
        return false;
    }

    // phone_number
    if (var_phone_number.length == 0) {
        alert("Please fill in your phone number!");
        document.contact_us_form.phone_number.focus();
        return false;
    }

    // email_address
    if (var_email_address.length == 0) {
        alert("Please fill in your email address!");
        document.contact_us_form.email_address.focus();
        return false;
    }

    // subject
    if (var_subject.length == 0) {
        alert("Please fill in the subject of inquiry!");
        document.contact_us_form.subject.focus();
        return false;
    }

    return true;
}

////==== submit form
function submit_contact_us_form() {
    // check form
    if (check_contact_us_form()) {
        // disable submit button
        document.contact_us_form.submit_button.disabled = true;

        // grey out form
        document.contact_us_form.full_name.className += " disabled";
        document.contact_us_form.phone_number.className += " disabled";
        document.contact_us_form.email_address.className += " disabled";
        document.contact_us_form.subject.className += " disabled";
        document.contact_us_form.message.className += " disabled";
        document.contact_us_form.submit_button.className += " disabled";

        // submit form
        document.contact_us_form.submit();
    }
}

/* ############################## PROJECTS ############################## */

////==== toggle
function toggle(obj) {
    var el = document.getElementById(obj);

    if (el.style.display != 'inline') {
        el.style.display = 'inline';
    }
    else {
        el.style.display = 'none';
    }
}

/* ############################## 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;
}

////==== append/chop word to/from given string
function appcho_word(str, appcho, word) {
    // split
    var parts = str.split(" ");

    // append
    if (appcho) {
        parts.push(word);
    }
    // chop
    else {
        parts.pop();
    }

    return parts.join(" ");
}

/* ############################## GENERAL ############################## */

////==== delegate window.onload calls
function delegate_onload(function1, function2) {
    return function() {
        if (function1)
            function1();
        if (function2)
            function2();
    }
}