/*  McCloud1.js
 *
 *  Purpose:
 *  ----------------------------------------------------------------------------
 *  This defines the JavaScript functions called throughout the site.
 *
 *  functions:
 *  ----------------------------------------------------------------------------
 *  function on_click_All_Services(opt)
 *  function on_click_McC_Find_Help(flag)
 *  function on_click_McC_Find_Text(what)
 *  function on_submit_McC_Find_Test()
 *  function on_click_McC_Home(j)
 *  function on_keyup_McC_Form()
 *  function on_click_Text_Size()
 *  function read_cookie(name)
 *  function show_hide(id)
 *  window.onload = function()
 *
 *  History:
 *  ----------------------------------------------------------------------------
 *  02-Jun-2009.0  Changed re "All Services" changed.
 *  19-Mar-2009.0  Changed re "All Services" pictures.
 *  10-Mar-2009.0  Changed re "All Services" picture.
 *  11-Dec-2008.0  Changed re "function show_hide(id)".
 *  19-Nov-2008.0  Changed re " // Home page links to All Services".
 *  18-Nov-2008.0  Changed.
 *  15-Nov-2008.0  Changed re class "id=" cannot start with "_" (underscore).
 *  13-Nov-2008.0  Changed re cookie expires.
 *  10-Nov-2008.0  Changed re "function on_click_All_Services(opt)".
 *  07-Nov-2008.0  Changed re "read_cookie(size)".
 *  06-Nov-2008.0  Changed re "function on_click_Text_Size(size)".
 *  03-Oct-2008.0  Changed re "document.forms[0]" now "document.forms["McC_?"]".
 *  02-Jul-2008.0  Changed re "function on_keyup_McC~Form()".
 *  19-May-2008.1  Changed re "Preload menu images": return!
 *  19-May-2008.0  Changed re "Preload menu images" was 404!
 *  18-May-2008.0  Changed.
 *  07-Mar-2008.0  Created.
 *  ----------------------------------------------------------------------------
 */

/*
 *  Global Constants and Variables
 */
var prel = true;

function on_click_All_Services(opt) {
/*
 *  on_click_All_Services()
 */
    var opts = new Array("ASD","BBM","BME","EDC","FSA","FA","IPM","OCP","SC","SCF","TIM","VM");
    for (var i=0; i<opts.length; i++) {
        var cls1 = "";
        var cls2 = "hide";
        if (opt == opts[i]) {
            cls1 = "pick";
            document.getElementById("All_Services").src = "images/McC_7" + opts[i] + ".jpg";
        }
        if (opt == opts[i] || opt == "+") cls2 = "show";
        document.getElementById("id_" + opts[i] + "_").className = cls1;
        document.getElementById("id_" + opts[i]).className = cls2;
    }
}

function on_click_McC_Find_Help(flag) {
/*
 *  on_click_McC_Find_Help()
 */
    var what = "";
    if (flag == 1) {
        what += "Search for:\n\n";
        what += "Enter a search word or phrase.\n\n";
        what += "Separate words/phrases with ' + '\n";
        what += "to find pages with all words/phrases.\n\n";
        what += "Extra spaces are automatically removed.";
    } else if (flag == 2) {
        what += "Match case?\n\n";
        what += "Check the checkbox for a 'case-sensitive' search;\n";
        what += "otherwise, the search is 'case-insensitive'.\n\n";
        what += "For example, a search on 'mccloud services' returns:\n";
        what += "zero matches if the search is case-sensitive but\n";
        what += "many matches if the search is case-insensitive\n";
        what += "because 'McCloud Services' is always captialized.";
    }
    alert(what);
}

function on_click_McC_Find_Text(what) {
/*
 *  on_click_McC_Find_Text()
 */
    var form = document.forms["McC_Find"];
    if (what == 0) {
        form.SearchText.value = " ";
    } else {
        form.SearchText.value = form.SearchLast.value
    }
}

function on_submit_McC_Find_Test() {
/*
 *  function on_submit_McC_Find_Test()
 */
    var spc1 = false;
    var spc2 = false;
    var what = document.forms["McC_Find"].SearchText.value;
    if (what.length > 1) {
        if (what.charAt(0) == " ") spc1 = true;
        if (what.charAt(what.length-1) == " ") spc2 = true;
    }
    var rex1 = /\s{2,}/g;                               // replace spaces
    var rex2 = /^(\s*)([\W\w]*)(\b\s*$)/;               // trim spaces
        what = what.replace(rex1," ");
        what = what.replace(rex2,"$2");
    if (spc1) what = " " + what;
    if (spc2) what = what + " ";
    document.forms["McC_Find"].SearchText.value = what;
    var test = what.replace(/\</g,"[");
        test = test.replace(/\>/g,"]");
    if (what != "" && what == test && what.length > 2) return true;
    alert("'Search for' is either missing or invalid!");
    return false;
}

function on_click_McC_Home(j) {
/*
 *  on_click_McC_Home()
 */
    var boo = false;
    var cls;
    var sav = document.getElementById("id_"+j).className;
    for (var i=0; i<13; i++) {
        if (i == j) {
            cls = "div2 show";
            if (cls == sav) {
                boo = true;
                cls = "div2 hide";
            }
        } else {
            cls = "div2 hide";
        }
        document.getElementById("id_"+i).className = cls;
    }
    if (boo) document.getElementById("id_0").className = "div2 show";
}

function on_keyup_McC_Form() {
/*
 *  on_keyup_McC_Form()
 */
    var com = document.getElementById("Comments").value;
    var sav = document.getElementById("Commentz").value;
    if (com == sav) return;
    document.getElementById("Comment_").style.visibility = "visible";
}

function on_click_Text_Size(size) {
/*
 *  on_click_Text_Size()
 */
    if (document.getElementById("textSize").innerHTML != "") {
        if (size == 0 || size == "small") {
            document.getElementById("font_sm").src = "images/font_sm1.gif";
            document.getElementById("font_md").src = "images/font_md0.gif";
            document.getElementById("font_lg").src = "images/font_lg0.gif";
            document.getElementById("fontSize").className = "fnt0";
        } else if (size == 2 || size == "large") {
            document.getElementById("font_sm").src = "images/font_sm0.gif";
            document.getElementById("font_md").src = "images/font_md0.gif";
            document.getElementById("font_lg").src = "images/font_lg1.gif";
            document.getElementById("fontSize").className = "fnt2";
        } else {  // default to size == "medium"
            document.getElementById("font_sm").src = "images/font_sm0.gif";
            document.getElementById("font_md").src = "images/font_md1.gif";
            document.getElementById("font_lg").src = "images/font_lg0.gif";
            document.getElementById("fontSize").className = "fnt1";
        }
        document.cookie = "size=" + size + "; expires=Sat, 31-Dec-2039 23:59:59 GMT";
    }
}

function read_cookie(name) {
/*
 *  read_cookie()
 */
    var pref = name + "=";
    var pair = document.cookie.split(';');
    for (var i=0; i<pair.length; i++) {
        var what = pair[i];
        while (what.charAt(0) == " ") what = what.substring(1,what.length);
        if (what.indexOf(pref) == 0) return what.substring(pref.length,what.length);
    }
    return "";
}

function show_hide(id) {
/*
 *  show_hide()
 */
    var what = document.getElementById(id).className;
    (what == "hide") ? what = "show" : what = "hide";
    document.getElementById(id).className = what;
}

window.onload = function() {
/*
 *  window.onload
 */
 // Text_Size 
    if (document.cookie) {
        document.getElementById("textSize").className = "noprint show";
        document.getElementById("showHide").className = "hide";
        var size = read_cookie("size");
        if (size == "") size = 0;
        on_click_Text_Size(size);
        document.getElementById("showHide").className = "show";
    }
 // Preload menu images
    if (document.images && prel) {
        document.preload = new Array();
        var img = 0;
        var jpg = ["images/McC_Ind1.jpg",
                   "images/McC_Ind2.jpg",
                   "images/McC_Ind3.jpg",
                   "images/McC_Ind4.jpg",
                   "images/McC_Ind5.jpg",
                   "images/McC_Ind6.jpg",
                   "images/McC_Ind7.jpg"];
        for (var i=0; i<jpg.length; i++) {
            img++;
            document.preload[img] = new Image();
            document.preload[img].src = jpg[i];
        }
        prel = false;
    }
 //  Home page links to All Services
    if (document.title == "McCloud Services - All Services") {
        var code = location.search.substr(1);
        on_click_All_Services(code);
    }
}
