/**
 * js_forms.js
 * A function library for Acrobat plug-in checking and cookie manipulation
 */

var osmWinParams = "width=600,height=365,resizable=yes,scrollbars=yes";

/**
 * Determine whether the user has the Acrobat Reader ActiveX control by
 * attempting to instantiate it
 *
 * @return true if the ActiveX control was successfully instantiated,
 * false otherwise
 *
 */
function tryActiveXAcro() {
        try {
                acro = new ActiveXObject("PDF.PdfCtrl.1");
                if(!acro) return;
                acro = null;
                return true;
        } catch (e) { return false; }
}

function checkAcrobat(url, doPopup) {
        if(getCookie('HasAcrobat') != 1) {
                if(tryActiveXAcro()) {
                        setThreeYearCookie('HasAcrobat');
                        window.location = url;
                } else {
                        var acroUrl = "/acrobat_help.jsp?url=" + escape(url);
                        if(doPopup) {
                                var zwindow = window.open(acroUrl, "usePolicy", osmWinParams);
                                zwindow.focus();
                        } else
                                window.location = acroUrl + "&popup=no";
                }
        } else window.location = url;
}

function checkAcrobatAlt(url, alt, doPopup) {
        if(getCookie('HasAcrobat') == 1) window.location = url;
        else if(getCookie('HasAcrobat') == 0) window.location = alt;
        else {
                if(!document.layers && tryActiveXAcro()) {
                        setThreeYearCookie('HasAcrobat');
                        window.location = url;
                } else {
                        var acroUrl =
                                "/acrobat_help.jsp?url=" + escape(url) + "&alt=" + escape(alt);
                        if(doPopup) window.open(acroUrl, "usePolicy", osmWinParams).focus();
                        else window.location = acroUrl + "&popup=no";
                }
        }
}

function changeCookie (name, isChecked) {
        if (isChecked) setThreeYearCookie(name);
        else deleteCookie(name);
}

function setThreeYearCookieVal(name, value) {
        var expireDate = new Date();
        expireDate.setTime(expireDate.getTime() + 1000*60*60*24*365*3);
        document.cookie = name + "=" + value + ";expires="+expireDate.toGMTString();
}

function setThreeYearCookie(name) {
        setThreeYearCookieVal(name, 1);
}

function deleteCookie (name) {
        var oldDate = new Date();
        oldDate.setFullYear(1970); // the expiredate is set to Jan. 1st 1970
        document.cookie = name + "=0" + "; expires="+oldDate.toGMTString();
}

/**
 * read a cookie value
 */
function getCookie (name) {
        var cookieString = document.cookie;
        var startIndex = cookieString.indexOf(name);
        if(startIndex == -1) return (-1);       // the cookie doesn't exist!
        startIndex += name.length + 1;
        endIndex = cookieString.indexOf(";", startIndex);
        if (endIndex == -1) endIndex = cookieString.length;
        return unescape(cookieString.substring(startIndex, endIndex));
}

