/****************************************************************
 *                                                              *
 * Keating Magee                                                *
 * http://www.keatingmagee.com/                                 *
 *                                                              *
 * Script Name: scripts.js                                      *
 * Script Version: 1.0                                          *
 * Script Info: Basic Config/Load Scripts                       *
 *                                                              *
 ****************************************************************/

// Function to preload images.

function preLoad() {
	if (document.images) {
        logo = new Image();
        logo.src = "/img/commcare_logo.gif";

        aboutoff = new Image();
        aboutoff.src = "/img/nav/about_off.gif";
        complianceoff = new Image();
        complianceoff.src = "/img/nav/compliance_off.gif";
        contactoff = new Image();
        contactoff.src = "/img/nav/contact_off.gif";
        employmentoff = new Image();
        employmentoff.src = "/img/nav/employment_off.gif";
        facilitiesoff = new Image();
        facilitiesoff.src = "/img/nav/facilities_off.gif";
        homeoff = new Image();
        homeoff.src = "/img/nav/home_off.gif";
        resourcesoff = new Image();
        resourcesoff.src = "/img/nav/resources_off.gif";
        senior_care_kitoff = new Image();
        senior_care_kitoff.src = "/img/nav/senior_care_kit_off.gif";

        abouton = new Image();
        abouton.src = "/img/nav/about_on.gif";
        complianceon = new Image();
        complianceon.src = "/img/nav/compliance_on.gif";
        contacton = new Image();
        contacton.src = "/img/nav/contact_on.gif";
        employmenton = new Image();
        employmenton.src = "/img/nav/employment_on.gif";
        facilitieson = new Image();
        facilitieson.src = "/img/nav/facilities_on.gif";
        homeon = new Image();
        homeon.src = "/img/nav/home_on.gif";
        resourceson = new Image();
        resourceson.src = "/img/nav/resources_on.gif";
        senior_care_kiton = new Image();
        senior_care_kiton.src = "/img/nav/senior_care_kit_on.gif";
    }

    cookies = document.cookie;
    isCookie = document.cookie.indexOf("textsize=");
    if (isCookie != -1) {
        curCookies = cookies.split("; ");
        for (i = 0; i < curCookies.length; i++) {
            tmpStr = curCookies[i];
            if (tmpStr.indexOf("textsize=") != -1) {
                curCookie = curCookies[i];
            }
        }
        tmpAry = curCookie.split("=");
        setSize = tmpAry[1];
    } else {
        document.cookie = "textsize=11";
        setSize = 11;
    }

    var pageBody = document.getElementsByTagName("body").item(0);
    pageBody.style.fontSize = setSize + "px";
}

// Function to create dynamic pop up window, currently set to center window on screen.

function newWindow(url, name, wid, hgt, scr, res, stat, loc, mnu, tool, x, y) {
    if (screen) {
        widthOfScreen = screen.width;
        heightOfScreen = screen.height;
    }
    midX = widthOfScreen / 2;
    midY = heightOfScreen / 2;
    midWid = wid / 2;
    midHgt = hgt / 2;
    x = midX - midWid;
    y = midY - midHgt;
    windowOpts = "width=" + wid + ",height=" + hgt + ",scrollbars=" + scr + ",resizable=" + res + ",status=" + stat + ",location=" + loc + ",menubar=" + mnu + ",toolbar=" + tool + ",left=" + x + ",top=" + y;
    popUpWindow = window.open(url, name, windowOpts);
}

// Function to dynamically write out the flash code to work around the IE flash "white box" issue.

function flashBanner(name, width, height) {
    document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '" id="' + name + '" align="middle">');
    document.write('    <param name="allowScriptAccess" value="sameDomain" />');
    document.write('    <param name="movie" value="/swf/' + name + '.swf" />');
    document.write('    <param name="quality" value="high" />');
    document.write('    <param name="bgcolor" value="#ffffff" />');
    document.write('    <embed src="/swf/' + name + '.swf" quality="high" bgcolor="#ffffff" width="' + width + '" height="' + height + '" name="' + name + '" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" />');
    document.write('    </embed>');
    document.write('</object>');
}

// Contact form validation

function contactValidate(obj) {
    formId = document.getElementById(obj);

    if (formId.name.value.length < 1) {
	    alert("Please enter your name.");
	    formId.name.focus();
	    return false;
    }
    if ((formId.email.value.indexOf("@") == -1) || (formId.email.value.indexOf(".") == -1) || (formId.email.value.length < 5))  {
	    alert("Please enter a valid email.");
	    formId.email.focus();
	    return false;
    }

    return true;
}

function checkAll(num, act) {
    chk = document.getElementsByTagName("input");

    if (num == 0) {
        for (i = 0; i < chk.length; i++) {
            if (chk[i].className == "checkBox") {
                if (act == "check") {
                    chk[i].checked = true;
                } else if (act == "uncheck") {
                    chk[i].checked = false;
                }
            }
        }
    } else {
        section = "section" + num + "[]";

        for (i = 0; i < chk.length; i++) {
            if (chk[i].className == "checkBox") {
                if (chk[i].name == section) {
                    if (act == "check") {
                        chk[i].checked = true;
                    } else if (act == "uncheck") {
                        chk[i].checked = false;
                    }
                }
            }
        }
    }
}

// Senior Care Kit validation

function kitValidate(obj) {
    formId = document.getElementById(obj);

    if (formId.name.value.length < 1) {
	    alert("Please enter your name.");
	    formId.name.focus();
	    return false;
    }
    if ((formId.email.value.indexOf("@") == -1) || (formId.email.value.indexOf(".") == -1) || (formId.email.value.length < 5))  {
	    alert("Please enter a valid email.");
	    formId.email.focus();
	    return false;
    }
    if (formId.city.value.length < 1) {
	    alert("Please enter your city.");
	    formId.city.focus();
	    return false;
    }
    if (formId.state.value == "--") {
	    alert("Please enter your state.");
	    formId.state.focus();
	    return false;
    }
    if (formId.zip.value.length < 1) {
	    alert("Please enter your zip.");
	    formId.zip.focus();
	    return false;
    }

    chk = document.getElementsByTagName("input");
    counter = 0;

    for (i = 0; i < chk.length; i++) {
        if (chk[i].className == "checkBox") {
            if (chk[i].checked) {
                counter++;
            }
        }
    }

    if (counter == 0) {
        alert("Please select at least one PDF.");
        return false;
    } else {
        return true;
    }

}

function textSize(act) {
    var pageBody = document.getElementsByTagName("body").item(0);
    curSize = parseInt(pageBody.style.fontSize);
    if (act == 'up') {
        curSize++;
    } else if (act == 'down') {
        curSize--;
    }
    pageBody.style.fontSize = curSize + "px";
    document.cookie = "textsize=" + curSize;

}