﻿function $(o) { 
    return document.getElementById(o)
}

function $ec(t) { 
    return encodeURIComponent(t)
}

function Find(n) {
    GoTo((n==1?'/search':'/busqueda')+'?s=' + $ec($('txtSearch').value))
}

function GoTo(a) {
    window.location.href = a;
    return false
}

function httpReq(method, url, params, s) {
    if (s == undefined) { s = true };
    if (s) {ShowModal('divProg', 200)};
    var h = httpReqObj();
    if (h == null) {return false};
    h.onreadystatechange = function() {
        if (h.readyState == 4) {
            AjaxParser(h.responseText);
            if (s) { HideModal('divProg') };
        }
    }

    if (method.toUpperCase() == 'POST') {
        h.open('POST', url, true);
        h.setRequestHeader("AsyncPost", "True");
        h.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        h.setRequestHeader("Content-Length", params.length);
        h.setRequestHeader("Connection", "close");
        h.send(params)
    } else {
        url = url.split('?')[0];
        h.open('GET', url + '?' + params, true);
        h.send(null)
    }
    
    return true
}

function httpReqObj() {
    var objs = [
	function() { return new XMLHttpRequest() }, // Firefox, Opera 8.0+, Safari
	function() { return new ActiveXObject("Msxml2.XMLHTTP") }, // Internet Explorer
	function() { return new ActiveXObject("Msxml3.XMLHTTP") }, // Internet Explorer
	function() { return new ActiveXObject("Microsoft.XMLHTTP") } ]; // Internet Explorer

    var h;
    for (var i = 0; i < objs.length; i++) {
        try {
            h = objs[i]();
        }
        catch (e) {
            continue;
        }
        break;
    }
    return h;
}

function Print(t) {
    var b = '<html><body style="font-family:verdana; width:1000px">' + t + '</div><div style="text-align:center">LasProfecias.com<br/>Rogelio Prestol</div></body></html>'
    
    var w = window.open('', 'Print');
    w.document.write(b);
    w.document.close();
    w.print();
    w.close();
}

function FocusThis(o) {
    try { o.focus() } catch (e) { }
}

function Params() {
    var na = new Array();
    var va = new Array()

    this.Add = function(n, v) {
        na.push(n);
        va.push(v)
    }
    this.Text = function() {
        var t = '';
        for (var i = 0; i < na.length; i++) {
            t = t + $ec(na[i]) + '=' + $ec(va[i]) + '&'
        }
        return t.substring(0, t.length - 1)
    }
}

function AjaxParser(t) {
    var a = t.split('|');
    for(var i = 0; i < a.length - 1; i = i + 4){
        switch (a[i]) {
            case "Acknowledge":
                $('divAckMsg').innerHTML = a[i+2];
                ShowModal('divAck');
                setTimeout('HideModal(\'divAck\')', 1000)
                break;
            case "Update":
                var obj = $(a[i + 1]);
                if (a[i + 3] != '') {
                    a[i + 2] = a[i + 2].replace(/a[i+3]/g, '|');
                }
                if (obj == null) { alert('Unable to find object: ' + a[i + 1]); return; }
                obj.innerHTML = a[i + 2];
                break;
            case "Alert":
                alert(a[i+2]);
                break;
            case "Redirect":
                document.location.href = a[i + 2];
                break;
            case "JScript":
                var func = new Function(a[i+2]);
                func();
                break;
        }
    }
}

function MaxChars(o, l, e, s) {
    var keynum;
    if(!e && window.event){e = window.event};
    if (e) { keynum = e.keyCode };

    $(s).innerHTML = 'Max (' + (parseInt(l) - o.value.length) + ')';
    if ((parseInt(l) - o.value.length) < 0) {
        $(s).style.color = '#ff0000'
    } else {
        $(s).style.color = '#666666'
    }

    if (!keynum) {
        return false
    };
    
    if (keynum < 32 || keynum == 46) {
        return true
    } else if (l <= o.value.length) {
        return false
    }
}

function TOver(o) {
    var c = $('divCats');
    c.style.display = 'inline';
    if (c.offsetLeft < 0) {
        c.style.left = '0px'
        c.style.marginLeft = '0px';   
    }
}

function TOut(o) {
    var c = $('divCats');
    c.style.display = 'none';
    c.style.left = '';
    c.style.marginLeft = '';
    
}

function ShowModal(n, i) {
    if (isNaN(i)) { i = 0 }
    Overlay(n,i);
    var o = $(n);

    o.style.position = 'fixed';
    o.style.left = '50%';
    o.style.top = '50%';
    o.style.zIndex = parseInt(i) + 1; 
    o.style.display = '';
    
    if (IsIE6()) {
        o.className = 'dialogie6';
        o.style.position = 'absolute';
        o.style.top = '';
        o.style.left = '';
    } else {
        o.style.postion = 'fixed';   
        o.style.marginLeft = -parseInt(o.offsetWidth / 2) + 'px';
        o.style.marginTop = -parseInt(o.offsetHeight / 2) + 'px';    
    }

}

function HideModal(n) {
    document.body.removeChild($(n + '_m'));
    Hide(n);
}

function Show(n) { 
    $(n).style.display = ''
}

function Hide(n) { 
    $(n).style.display = 'none'
}

function Overlay(n, i) {
    var e = document.createElement('div');
    e.setAttribute('class', 'overlay');
    e.setAttribute('id', n + '_m');
    document.body.appendChild(e);

    var o = $(n + '_m');
    o.className = 'overlay';
    o.style.display = 'block';
    o.style.zIndex = parseInt(i);
    
    if (IsIE6()) {
        o.className = 'overlayie6';
    }
}

function IsIE6(){
    var r = new RegExp(/MSIE 6/i);
    return r.test(navigator.userAgent)
}

function KeyPressFind(e,n) {
    if (e.keyCode == 13) { Find(n) }
}

function Logout() {
    var p = new Params();
    p.Add('target', 'logout');
    
    httpReq('POST', '/Async.aspx', p.Text())
}

function Hit() {
    try {
        var p = new Params();
        p.Add('target', 'hit');
        p.Add('width', screen.width);
        p.Add('height', screen.height);
        p.Add('url', window.location.href);
        p.Add('ref', document.referrer);
        httpReq('POST', '/Async.aspx', p.Text(), false);
        setTimeout('Online()',60000) 
    }catch(ex){}
}

function Online() {
    try {
        var p = new Params();
        p.Add('target', 'online');
        httpReq('POST', '/Async.aspx', p.Text(), false);
        setTimeout('Online()',60000)
    } catch(ex){}
}
