﻿/// <reference path="Cookies.js" />
/// <reference path="swfobject.js" />
/// <reference path="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" />

var ChecksEnum = {
    None : 0,
    Cookies : 1,
    SessionCookies : 2,
    Flash : 4,
    AdobeReader: 8
    //,Https : 16
}

function flashVersion(){
    this.major = 0;
    this.minor = 0;
    this.revision = 0;
};

flashVersion.prototype.version = function (){
    return this.major + '.' + this.minor + '.' + this.revision;
}

function BrowserCheck(checks){
    this.completionRedirectUrl = null;
    //this.httpsCheckUrl = null;
    this.Checks = checks;
    this.ChecksPassed = ChecksEnum.None;
    this.FlashVersion = new flashVersion();

};

BrowserCheck.prototype.IsCheckFailed = function(enumValue){
    return (this.Checks & enumValue) && !(this.ChecksPassed & enumValue)
};

BrowserCheck.prototype.SetComplete = function(){
    var bcCookie = get_bcCookie();
    bcCookie.Success = true;
    bcCookie.store();
};

BrowserCheck.prototype.CheckComplete = function(){
    var bcCookie = get_bcCookie();
    bcCookie.load();
    return bcCookie.exists() && bcCookie.Success;
};

function get_bcCookie(){
    var cookie = new Cookie(document, 'BrowserCheckComplete');
    //cookie.$secure = true;
    return cookie;
}

BrowserCheck.prototype.RunChecks = function(completionCallback){

    if (this.Checks & ChecksEnum.Cookies) this.RunCookieCheck();
    if (this.Checks & ChecksEnum.SessionCookies) this.RunSessionCookieCheck();
    if (this.Checks & ChecksEnum.Flash) this.RunFlashCheck();
    if (this.Checks & ChecksEnum.Https) this.RunHttpsCheck();
    if (this.Checks & ChecksEnum.AdobeReader) this.RunAdobeReaderCheck();
    
    if(completionCallback){
        completionCallback(this);
    }
    
    return this.ChecksPassed;
}; 

BrowserCheck.prototype.RunCookieCheck = function(){
    var cookie = new Cookie(document, 'testCookie', 120);
    cookie.TestValue = 'value';
    cookie.store();
    
    var exists = cookie.exists();
    this.ChecksPassed = SetFlag(this.ChecksPassed, ChecksEnum.Cookies, exists);
    cookie.remove();
    return exists;

};

BrowserCheck.prototype.RunSessionCookieCheck = function(){
    var cookie = new Cookie(document, 'testSessionCookie');
    cookie.TestValue = 'STORE-ME';
    cookie.store();
    
    var exists = cookie.exists();
    this.ChecksPassed = SetFlag(this.ChecksPassed, ChecksEnum.SessionCookies, exists);
    cookie.remove();
    return exists;
};

BrowserCheck.prototype.RunFlashCheck = function(){
    var hasFlash = swfobject.hasFlashPlayerVersion(this.FlashVersion.version());
    this.ChecksPassed = SetFlag(this.ChecksPassed, ChecksEnum.Flash, hasFlash);
    return hasFlash;
};

BrowserCheck.prototype.RunAdobeReaderCheck = function() {
    var ret = false;
    if (navigator.plugins && navigator.plugins.length > 0) {
        for (i = 0; i < navigator.plugins.length; i++) {
            if (navigator.plugins[i].name.indexOf('Acrobat') >= 0 || navigator.plugins[i].description.indexOf('Acrobat') >= 0) {
                ret = true;
            }
        }
    } else {
        try {
            var dummyObj = new ActiveXObject('AcroExch.Document');
            if (dummyObj) ret = true;
        } catch (e) {
            ret = false;
        }
    }
    this.ChecksPassed = SetFlag(this.ChecksPassed, ChecksEnum.AdobeReader, ret);
    return ret;
};

BrowserCheck.prototype.RunHttpsCheck = function(){
//    var url = this.httpsCheckUrl;
//    var httpsCheckResult = null;

//    if(!url){ throw 'The https check url must be set to a valid url to perform the https check.'; }
////    if(url.indexOf('?') == -1) { url += '?' }
////    //append this just in case, so if the coder points at the page hosting the check it can ignore the request
////    url += '&httpsCheck=true'; 

//    try{
//        $.get(url, null, function(data, status){
//            $("div.#httpsResults").append(data);
//            if(status.toLowerCase() == 'success'){
//                httpsCheckResult = true;
//            }else{
//                httpsCheckResult = false;
//            }
//        });
//    }catch(e){
//        alert('Error testing for https: ' + e);
//    }
//    return httpsCheckResult;
    return true; //hard coding this to true atm, as there are several problems with trying to detect this.
};

function SetFlag(pEnum, pEnumValue, flag){
    if(flag){
        pEnum |= pEnumValue;
    }else{
        pEnum &= ~(pEnumValue);
    }
    return pEnum;
}

