﻿// Master js lib
// Antares Servicios Informáticos 2010

function _mstload(p, replace) {
    if (replace)
        window.location.replace(p)
    else
        window.location = p;
}

function _mst_post(t, p, a) { // template, event + params, action
    var mfr = document.forms['_mst_form'];
    var srcid = (event ? (event.srcElement ? event.srcElement.id : 'undefined') : 'undefined');
    if (!mfr) mfr = document._mst_form;
    if (a) {
        mfr.action = a;
        mfr._mst_event.value = '__redir__|' + t + '|' + srcid + '|' + p;
    }
    else {
        mfr._mst_event.value = t + '|' + srcid + '|' + p;
    }
    mfr.submit();
}

function _mst_ajax(t, p, d, f) { // template, event + params, 'selector jquery' destino si html, callback(data,type)
    var mfr = document.forms['_mst_form'];
    var srcid = (event ? (event.srcElement ? event.srcElement.id : 'undefined') : 'undefined');
    if (!mfr) mfr = document._mst_form;
    mfr._mst_event.value = t + '|' + srcid + '|' + p;
    return $.ajax({
        'url': 'ax/' + t,
        'data': $('form').serialize(),
        'type': 'POST',
        'error': function(request, status, a) {
            if (!f(status, 'error'))
                alert(data);
        },
        'success': function(data, status, request) {
            stype = request.getResponseHeader("content-type");
            if (stype.indexOf('html') > 0) {
                stype = 'html';
                if (d) $(d).html(data);
                $('#_mst_state').val($('#_mst_newstate').val());
                if (f) f(data, stype);
                _mst_ready(t);
            }
            else if (stype.indexOf('xml') > 0) {
                stype = 'xml';
                if (f) f(data, stype)
            }
            else if (stype.indexOf('plain') > 0) {
                stype = 'text';
                if (f) f(data, stype)
            }
            else {
                stype = 'error';
                if (f) {
                    if (!f(data, stype)) alert(data);
                }
                else alert(data);
            }
        }
    });
}

function _mstquery(q, c, f) {   // query, criterios, f(data, type) --> llama sin estado mantiene sesión
    return $.ajax({             // para llamadas con estado usar _mstajax
        'url': 'qx/mst',
        'data': '_mst_event=||' + q + ' ' + c,
        'type': 'POST',
        'error': function(request, status, a) {
            if (f)
                if (!f(status, 'error')) alert(data);
        },
        'success': function(data, status, request) {
            stype = request.getResponseHeader("content-type");
            if (stype.indexOf('html') > 0)
                stype = 'html';
            else if (stype.indexOf('xml') > 0)
                stype = 'xml';
            else if (stype.indexOf('plain') > 0)
                stype = 'text';
            else
                stype = 'error';
            if (f) f(data, stype);
        }
    });
}

//function _mst_dlg(t, p) {
//    $('form').append('<div class="_mst_overlay"></div><div class="_mst_dlg"><a href="#" onclick="_mst_dlg_close();return false;">close</a><div id="' + p + '"></div></div>');
//    $('#' + p).text('Cargando panel' + p)
//        .load(bizone + 'dlg/' + p, function() {
//        });
//}
//function _mst_dlg_close() {
//    $(this.event.srcElement)
//        .parent('._mst_dlg')
//        .prev()
//        .andSelf()
//        .remove();
//}


// Check si bloqueo mayusculas está pulsado

function mst_check_capson() {

    var e = event;
    e = (e) ? e : window.event;

    var charCode = false;
    if (e.which) {
        charCode = e.which;
    } else if (e.keyCode) {
        charCode = e.keyCode;
    }
    var character = String.fromCharCode(charCode);
    var shifton = false;
    if (e.shiftKey) {
        shifton = e.shiftKey;
    } else if (e.modifiers) {
        shifton = !!(e.modifiers & 4);
    }

    if (character >= 'a' && character <= 'z')
        return shifton;
    else if (character >= 'A' && character <= 'Z')
        return !(shifton);

}


/**
* getPageSize() by quirksmode.com
* @return Array Return an array with page width, height and window width, height
*/

function ___getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {	// all except Explorer
        if (document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
    return arrayPageSize;
};

/**
* getPageScroll() by quirksmode.com
* @return Array Return an array with x,y page scroll values.
*/

function ___getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;
    }
    arrayPageScroll = new Array(xScroll, yScroll);
    return arrayPageScroll;
};



