
/**
* set a cookie
*
* @param string cookie name
* @param string cookie value
* @param string [optional] number of hours to expire
* @param string [optional] url path to restrict to
* @param string [optional] domain name to restrict to
* @param string [optional] whether to force SSL cookies or not
*/
function setCookie (sName, sValue)
{
    // test for optional parameters
    var bHours	= (arguments.length >= 3) ? true : false;
    var bPath	= (arguments.length >= 4) ? true : false;
    var bDomain	= (arguments.length >= 5) ? true : false;
    var bSecure	= (arguments.length >= 6) ? true : false;

    // if applicable, set optional parameters
    var sHours	= (bHours) ? arguments[2] : null;
    var sPath	= (bPath) ? arguments[3] : null;
    var sDomain	= (bDomain) ? arguments[4] : null;
    var sSecure	= (bSecure) ? arguments[5] : null;

    // set local variables
    var sDate	= null;
    var sCookie	= null;

    if(bHours)
    {
        if ( (typeof(sHours) == 'string') && Date.parse(sHours) )
        {
            // already a Date string
            sDate = sHours;
        }
        else if (typeof(sHours) == 'number')
        {
            // calculate Date from number of hours
            sDate = (new Date((new Date()).getTime() + sHours * 3600000)).toGMTString();
        }
    }

    // create the cookie string, adding any parameters that were specified
    sCookie = sName + '=' + escape(sValue);
    sCookie += ((sDate != null) ? (';expires=' + sDate) : '');
    sCookie += ((bPath) ? (';path=' + sPath) : '');
    sCookie += ((bDomain) ? (';domain=' + sDomain) : '');
    sCookie += ((bSecure) ? (';secure=' + sSecure) : '');

    // set the cookie
    document.cookie = sCookie;
}

/**
* read a cookie value
*
* @param string cookie name
* @return string cookie value (empty string if not found)
*/
function readCookie(sName)
{
    if(document.cookie == '')
    {
        // there's no cookie, so go no further
        return("");
    }
    else
    {
        // there is a cookie
        var sFirstChar;
        var sLastChar;
        var sCookieValue = document.cookie;

        // find the start of 'sName'
        sFirstChar = sCookieValue.indexOf(sName);

        if (sFirstChar != -1)
        {
            // if you found the cookie

            // skip 'sName' and '='
            sFirstChar += sName.length + 1;

            // Find the end of the value string (i.e. the next ';').
            sLastChar = sCookieValue.indexOf(';', sFirstChar);

            if(sLastChar == -1) lastChar = theBigCookie.length;

            return(unescape(sCookieValue.substring(sFirstChar, sLastChar)));
        }
        else
        {
            // If there was no cookie of that sName, return false.
            return("");
        }
    }
}

/**
* kill a cookie
*
* @param string cookie name
* @param string url path the cookie was restricted to
* @param string domain name the cookie was restricted to
*/
function killCookie(sName)
{
    // test for optional parameters
    var bPath	= (arguments.length >= 2) ? true : false;
    var bDomain	= (arguments.length >= 3) ? true : false;

    // if applicable, set optional parameters
    var sPath	= (bPath) ? arguments[1] : null;
    var sDomain	= (bDomain) ? arguments[2] : null;

    // We need the value to kill the cookie
    var sCookieValue = readCookie(sName);

    if(sCookieValue != '')
    {
        // create the already-expired cookie string, adding any parameters that were specified
        sCookie = sName + '=' + escape(sValue);
        sCookie += ';expires=Fri, 13-Apr-1970 00:00:00 GMT';
        sCookie += ((bPath) ? (';path=' + sPath) : '');
        sCookie += ((bDomain) ? (';domain=' + sDomain) : '');

        // set the cookie
        document.cookie = sCookie;
    }
}


/**
* formats money into a presentable string
*
* @param string money value
* @return string formatted money string
*/
function formatMoney(s)
{
    if (s == "")
    {
        s = "0";
    }

    var p = s.indexOf(".");

    if (p < 0)
    {
        // No decimal present.
        s += ".00";
        p = s.indexOf(".");
    }

    if(p == 0)
    {
        // Decimal at position 0
        s = "0" + s;
        p = s.indexOf(".");
    }

    s += (p == s.length-1) ? "00" : (p == s.length-2) ? "0" : "";

    return "$" + s;
}

/**
* Update the color property of a style
*
* @param string the element id
* @param string the color value
*/
function swapColor(id, value)
{
    if (document.getElementById)
    {
        if (document.getElementById(id))
        {
            document.getElementById(id).style.color = value;
        }
    }
}

/**
* Swap out an image src property via the DOM (alias for swapSrc)
*
* @param string the element id of the image (not name)
* @param string the src property of the image
* @param string [optional] the alt property
*/
function swapImage(id, src)
{
    swapSrc(id, src);

    // the alt can be passed as well but is optional
    if (arguments.length > 2)
    {
        document.getElementById(id).setAttribute('alt', arguments[2]);
    }
}

/**
* Swap out the src property via the DOM
*
* @param string the element id of the image (not name)
* @param string the src property of the image
*/
function swapSrc(id, src)
{
    if (document.getElementById)
    {
        document.getElementById(id).setAttribute('src', src);
    }
}

/**
* Swap out an elements html code
*
* @param string the element id
* @param string the html code to swap in
*/
function swapHtml(id, html)
{
    if (document.getElementById)
    {
        if (document.getElementById(id))
        {
            document.getElementById(id).innerHTML = html;
        }
    }
}

/**
* hide an element by setting visibility to hidden
*
* @param string the element id
*/
function hideElement(id)
{
    if (document.getElementById)
    {
        if (document.getElementById(id))
        {
            document.getElementById(id).style.visibility = 'hidden';
        }
    }
}

/**
* show an element by setting visibility to visible
*
* @param string the element id
*/
function showElement(id)
{
    if (document.getElementById)
    {
        if (document.getElementById(id))
        {
            document.getElementById(id).style.visibility = 'visible';
        }
    }
}

/**
* move an element to x, y coordinates
*
* @param string the element id
* @param string x position
* @param string y position
*/
function moveElement(id, x, y)
{
    if (document.getElementById)
    {
        document.getElementById(id).style.left = x + 'px';
        document.getElementById(id).style.top = y + 'px';
    }
}

/**
* Find all matching a element onmouseover and onmouseout properties and
* strip out the image sources for preloading (this function should be
* called from the onload event, never before)
*/
function autoPreload()
{
    if (document.getElementById)
    {
        var aPreloadOver = new Array();
        var aPreloadOut = new Array();
        var aLink = document.getElementsByTagName('a');

        for (var i = 0; i < aLink.length; i++)
        {
            if (aLink[i].getAttribute('onmouseover') && aLink[i].getAttribute('onmouseout'))
            {
                // find the correct mouseover calls
                var re = /^javascript:swapImage\(('|").*('|")(.*)('|")\);$/;

                // grab the mousover properties
                var sTempOver	= aLink[i].getAttribute('onmouseover');
                var sTempOut	= aLink[i].getAttribute('onmouseout');

                if (re.test(sTempOver) && re.test(sTempOut))
                {
                    // strip the properties down to nothing but the
                    // image that we're loading
                    var sOver	= sTempOver.replace(re, "$3");
                    var sOut	= sTempOut.replace(re, "$3");

                    // preload the over image
                    aPreloadOver[i] = new Image();
                    aPreloadOver[i].src = sOver;

                    // preload the out image
                    aPreloadOut[i] = new Image();
                    aPreloadOut[i].src = sOut;
                }
            }
        }
    }
}

var oIframe; // our IFrame object

/**
* Load a remote url into an iframe
*
* @param string the url
*/
function callToServer(sUrl)
{
    if (!document.createElement)
    {
        return(true);
    }

    var oIframeDoc;

    if (!oIframe && document.createElement)
    {
        // create the IFrame and assign a reference to the
        // object to our global variable oIframe.
        // this will only happen the first time
        // callToServer() is called
        try
        {
            var oIframeTemp = document.createElement('iframe');
            oIframeTemp.setAttribute('id','RSIFrame');
            oIframeTemp.style.border = '0px';
            oIframeTemp.style.width = '0px';
            oIframeTemp.style.height = '0px';
            /*
            oIframeTemp.style.top = '20px';
            oIframeTemp.style.left = '750px';
            oIframeTemp.style.border = '1px';
            oIframeTemp.style.width = '200px';
            oIframeTemp.style.height = '200px';
            */
            oIframe = document.body.appendChild(oIframeTemp);

            if (document.frames)
            {
                // this is for IE5 Mac, because it will only
                // allow access to the document object
                // of the IFrame if we access it through
                // the document.frames array
                oIframe = document.frames['RSIFrame'];
            }
        }
        catch(exception)
        {
            // This is for IE5 PC, which does not allow dynamic creation
            // and manipulation of an iframe object. Instead, we'll fake
            // it up by creating our own objects.
            iframeHTML = '\<iframe id="RSIFrame" style="';
            iframeHTML += 'border:0px;';
            iframeHTML += 'width:0px;';
            iframeHTML += 'height:0px;';
            /*
            iframeHTML += 'top:750px;';
            iframeHTML += 'left:20px;';
            iframeHTML += 'border:1px;';
            iframeHTML += 'width:200px;';
            iframeHTML += 'height:200px;';
            */
            iframeHTML += '"><\/iframe>';

            document.body.innerHTML += iframeHTML;

            oIframe = new Object();
            oIframe.document = new Object();
            oIframe.document.location = new Object();
            oIframe.document.location.iframe = document.getElementById('RSIFrame');
            oIframe.document.location.replace = function(location)
            {
                this.iframe.src = location;
            }
        }
    }

    /*
    alert('oIframe.contentDocument = ' + (oIframe.contentDocument ? 'true' : 'false') + '\n' +
        'oIframe.contentWindow = ' + (oIframe.contentWindow ? 'true' : 'false') + '\n' +
        'oIframe.document = ' + (oIframe.document ? 'true' : 'false') + '\n');
    return(false);
    */

    if (navigator.userAgent.indexOf('Gecko') !=-1 && !oIframe.contentDocument)
    {
        // we have to give NS6 a fraction of a second
        // to recognize the new IFrame
        setTimeout('callToServer()',10);
        return false;
    }

    if (oIframe.contentDocument)
    {
        // For NS6
        oIframeDoc = oIframe.contentDocument;
    }
    else if (oIframe.contentWindow)
    {
        // For IE5.5 and IE6
        oIframeDoc = oIframe.contentWindow.document;
    }
    else if (oIframe.document)
    {
        // For IE5
        oIframeDoc = oIframe.document;
    }
    else
    {
        return(true);
    }

    oIframeDoc.location.replace(sUrl);
    return(false);
}

/**
* a javascript collection object of all of the window popup options
*/
function popupOptions()
{
    this.height         = screen.availHeight / 2;
    this.width          = screen.availWidth / 2;
    this.top            = 0;
    this.left           = 0;
    this.toolbar        = false;
    this.location       = false;
    this.directories    = false;
    this.status         = false;
    this.menubar        = false;
    this.scrollbars     = false;
    this.resizable      = false;
    this.dependent      = false;

    this.build          = _buildOptions;
}

/**
* a private javascript method for popupOptions which is used
* to build a popup option string
*/
function _buildOptions()
{
    var sTemp = "";

    sTemp += "height=" + this.height + ",";
    sTemp += "width=" + this.width + ",";
    sTemp += "top=" + this.top + ",";
    sTemp += "left=" + this.left + ",";
    sTemp += "scrollbars=" + ((this.scrollbars) ? "yes" : "no") + ",";
    sTemp += "toolbar=" + ((this.toolbar) ? "yes" : "no") + ",";
    sTemp += "location=" + ((this.location) ? "yes" : "no") + ",";
    sTemp += "directories=" + ((this.directories) ? "yes" : "no") + ",";
    sTemp += "status=" + ((this.status) ? "yes" : "no") + ",";
    sTemp += "menubar=" + ((this.menubar) ? "yes" : "no") + ",";
    sTemp += "resizable=" + ((this.resizable) ? "yes" : "no") + ",";
    sTemp += "dependent=" + ((this.dependent) ? "yes" : "no");

    return(sTemp);
}

/**
* creates a popup window
*
* @param string window url
* @param string window name
* @param string [optional] window width
* @param string [optional] window height
* @param boolean [optional] whether window is scrollable or not
* @param boolean [optional] whether window has a toolbar or not
* @param boolean [optional] whether window is resizable or not
*/
function popup(winlink, winname)
{
    var winwidth   	= (arguments.length >= 3) ? arguments[2] : screen.availWidth / 2;
    var winheight   = (arguments.length >= 4) ? arguments[3] : screen.availWidth / 2;
    var winscroll   = (arguments.length >= 5) ? ((arguments[4] == '1') ? true : false) : false;
    var wintoolbar  = (arguments.length >= 6) ? ((arguments[5] == '1') ? true : false) : false;
    var winresize   = (arguments.length >= 7) ? ((arguments[6] == '1') ? true : false) : false;

    var oWin    = null;
    var oOption = new popupOptions();

    oOption.width		= winwidth;
    oOption.height		= winheight;
    oOption.scrollbars	= winscroll;
    oOption.toolbar		= wintoolbar;
    oOption.resizable	= winresize;

    // these values are hard coded
    oOption.top			= ((screen.availHeight / 2) - (winheight / 2));
    oOption.left		= ((screen.availWidth / 2) - (winwidth / 2));
    oOption.location	= false;
    oOption.directories = false;
    oOption.status		= false;
    oOption.menubar		= false;
    oOption.dependent	= false;

    var sOptions = oOption.build();

    oWin = window.open(((typeof(winlink) == "string") ? winlink : winlink.href), winname, sOptions);
    oWin.focus();
//    return(false);
}

/**
* this is a custom function that corrects styles as necessary
* on the site (mostly to account for IE's lack of standards)
*/
function cssFormAdjust()
{
    /*if ((navigator.userAgent.toLowerCase().indexOf("mac") != -1) && document.all)
    {
        return(true);
    }
    else
    {*/
        var i = 0;

        var aInput		= document.getElementsByTagName('input');
        var aTextarea	= document.getElementsByTagName('textarea');

        // input tags
        for (i = 0; i < aInput.length; i++)
        {
            if (aInput[i].type == 'text')
            {
                aInput[i].style.width = '175px';
            }
            else if (aInput[i].type == 'button')
            {
                aInput[i].style.fontSize = '9px';
                aInput[i].style.padding = '0';
                aInput[i].style.margin = '0';
                aInput[i].style.color = '#666666';
                aInput[i].style.backgroundColor = '#cccccc';
                aInput[i].style.border = '1px solid #000000';
            }
        }

        // textarea tags
        for (i = 0; i < aTextarea.length; i++)
        {
            //alert("width = '" + aTextarea[i].style.width + "'");
            aTextarea[i].style.width = '175px';
        }

        return(true);
    //}
}


/**
* this is a custom function that manages the pop-up window size for the survey
*/

function popupMacTest(url)
{
    if (navigator.appVersion.indexOf("Mac") != -1)
    {
        popup(url, 'panelsurvey', 508, 520, 1 ,0 ,0);
    }
    else
    {
        popup(url, 'panelsurvey', 525, 520, 1 ,0 ,0);
    }
}

