﻿/// <reference path="OWD_ClientSide.js" />
/*********************************************
{
        ObjRGCookie = {
            JS: 
            [
                { 
                    id: "task1", 
                    script: encodeURIComponent("fnOpenDataRWindow('dlgFinAño',null, null, 630, 425,  '<img src=\\\'\/App_Images\/Others\/Tarjeta-Fin-de-Año-2010.png\\\' alt=\\\'Feliz 2011\\\' \\>', 'normal', null, null, null)"), 
                    ToShow: 2, 
                    desde: new Date(2010, 11, 20), 
                    hasta: new Date(2011, 0, 6) 
                } , {
                     id: "task2", 
                     script: "fnalert(2)", 
                     ToShow: 1, 
                     desde: new Date(2010, 11, 21), 
                     hasta: new Date(2010, 11, 22)
                }
            ]
        }
        El ToShow es un numero que corresponde a la cantidad de veces que se quiere que se ejecute el script en el periodo marcado por el desde - hasta.
*********************************************/
function set_cookie(cookie_name, cookie_value) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + 30);
    document.cookie = cookie_name +
                       "=" + encodeURIComponent(cookie_value) +
                        ";expires=" + exdate.toUTCString();
}

function get_cookie(cookie_name) {
    var cookie_string = document.cookie;
    var ObjRegExp = new RegExp('(^|;)[\\s]*' + cookie_name + '=([^;]*)');
    if (ObjRegExp.test(cookie_string)) {
        var cookie_value = ObjRegExp.exec(cookie_string);
        return decodeURIComponent(cookie_value[2]);
    }
    return '';
}

function delete_cookie(cookie_name) {
    document.cookie = cookie_name + '= ';
}

function fnCompareJSs(cookie_name) {
    if (typeof (ObjRGCookie) != "object") {
        delete_cookie("ObjRGC", null);
        return;
    }
    else if (typeof (cookie_name) != "string") {
        return;
    }
    else {
        var strObject = get_cookie(cookie_name);
        if (strObject == "") strObject = "{}"
        var TObj = eval("(" + strObject + ")");
        if (typeof (TObj) == "object") {
            var tsr = ObjRGCookie.JS
            for (var i in tsr) {
                var tjs = TObj.JS
                for (var e in tjs) {
                    if (tjs[e].id == tsr[i].id) {
                        tsr[i].ToShow = tjs[e].ToShow;
                        break;
                    }
                }
            }
            //TObj.JS = ObjRGCookie.JS;
        }
        var tsr = ObjRGCookie.JS
        for (var i in tsr) {
            if (tsr[i].ToShow > 0) {
                var Hoy = new Date();
                var desde = tsr[i].desde;
                var hasta = tsr[i].hasta;
                //le configuro para las 24 hs asi no es excluyente el hasta.
                hasta.setHours(24, 0, 0, 0)
                if (desde <= Hoy && Hoy <= hasta) {
                    if (typeof (tsr[i].script) == "string") eval(decodeURIComponent(tsr[i].script));
                    else if(typeof (tsr[i].script) =="function") tsr[i].script();
                    tsr[i].ToShow -= 1
                }
            }
        }
        TObj = ObjRGCookie;
        var toStr = new print_JSon();
        set_cookie("ObjRGC", toStr.recorrer(TObj), null, null);
    }
}

function print_JSon() {
    this.a = '';
    this.recorrer = function(o) {
        if (o.constructor == Array)
            this.a += '[';
        if (o.constructor == Object)
            this.a += '{';
        for (var i in o) {
            if (o.constructor != Array)
                this.a += i + ':';
            if (o[i].constructor == Object) {
                this.recorrer(o[i]);
            } else if (o[i].constructor == Array) {
                this.recorrer(o[i]);
            } else if (o[i].constructor == String || o[i].constructor == Date) {
                this.a += '"' + o[i] + '",';
            } else {
                this.a += o[i] + ',';
            }
        }
        if (o.constructor == Object)
            this.a += '},';
        if (o.constructor == Array)
            this.a += '],';
        return this.a.substr(0, this.a.length - 1).split(',}').join('}').split(',]').join(']');
    }
}

