
function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start += c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
    // todo: should you use null here instead?
}

function setCookie(c_name, c_value) {
    var today = new Date();
    var expire = new Date();
    expire.setTime(today.getTime() + 3600 * 24 * 365 * 10);
    // expires in 10 years
    document.cookie = escape(c_name) + "=" + escape(c_value) + "; expires=" + expire.toGMTString() + "; path=/; domain=.sutrosoftware.com";
}

function validJava() {
    var valid = true;
    if (!getCookie("validJava")) {
        var javaDetect = PluginDetect.isMinVersion('Java', '1.5', '/assets/dhtml/getJavaInfo.jar');
        switch (javaDetect) {
            case 1:
                setCookie("validJava", "true");
                break;
            case 0:
                alert('Please ensure that you have Java version 1.5 \nor greater installed on your system.');
                break;
            case -1:
                alert('Please ensure that you have Java version 1.5 \nor greater installed on your system.');
                valid = false;
                break;
            case -2:
                alert('ActiveX disabled in Internet Explorer. \nUnable to detect your version of Java. \n'
                        + 'Please ensure that you have Java version 1.5 \nor greater installed on your system.');
                valid = false;
                break;
        }
    }
    return valid;
}

function doLaunch() {
    var serialno = getCookie("serialno");
    if (!serialno) {
        ticket = getCookie("ticket");
        if (ticket) {
            setCookie("serialno", ticket);
            serialno = ticket;
        }
    }
    if (serialno) {
        if (validJava()) {
            //			alert('http://local.sutrosoftware.com/webstart/' + ticket + '/PortalSpy.jnlp');
            window.location = "http://" + hostname + "/webstart/" + serialno + "/PortalSpy.jnlp";
        } else {
            window.open("http://jdl.sun.com/webapps/getjava/BrowserRedirect?locale=en&host=java.com", "needdownload");
        }
    } else {
        doSignIn();
    }
}


function doSignIn() {
    $.fn.colorbox({href:"/scripts/signin.php", scrolling:false, width:500, height:600, iframe:true});
}

if (launchPage) {
    $(document).ready(function(){
        doLaunch();
    });
}
