﻿

var wp_clsGlobalObject = new Class({
    initialize: function(RootPath) {
        this.RootPath = RootPath;
    },
    getRootPath: function() { return this.RootPath; },
    getWaitImagePath: function(index) {
        if (index == null)
            index = 0;
        return this.getRootPath() + '/WebPlusResources/Other/Immagini/ajax-loader/0.gif';

    },
    getWaitImage: function(index) {
        return new Element('img',
            { 'src': this.getWaitImagePath(index), 'styles': { 'border': '0'} })
    },
    getHelpImagePath: function(index) {
        if (index == null)
            index = 0;
        return this.getRootPath() + '/WebPlusResources/Other/Immagini/help/0.gif';

    },
    getHelpImage: function(index) {
        return new Element('img',
            { 'src': this.getHelpImagePath(index), 'styles': { 'border': '0'} })
    }
});


function getXmlHTTP() {
    var xmlhttp = false;
    /*@cc_on@*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    @end@*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && window.createRequest) {
        try {
            xmlhttp = window.createRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }

    return xmlhttp;
}


function SetChildrenMaxHeight() {

    setTimeout('SetChildrenMaxHeight_delayed()', 2000);

}

function SetChildrenMaxHeight_delayed() {
    //cerco max
    var maxheight = -1;

    var i = 0;

    while (true) {
        var el = $('div_' + i.toString());
        if (el != null) {
            if (el.getSize().y > maxheight)
                maxheight = el.getSize().y;
        }
        else
            break;

        i += 1;
    }

    var heightToApply = maxheight.toString() + 'px';

    i = 0;
    while (true) {
        var el = $('div_' + i.toString());
        if (el != null)
            $('div_' + i.toString()).setStyle('height', heightToApply);
        else
            break;

        i += 1;
    }
}


//Ad ogni submit, la pagina esegue _onSubmit che in origine effettuava sempre una validazione lato client
//A volte è opportuno bypassarla (es: ricerca comune che dato comune trova codistat, che deve funzionare)
//anche se mancano delle validazioni.
//Per saltare la convalida, impostare __SkipClientValidation=true;
var __SkipClientValidation = false;
function _onSubmit() {

    if (__SkipClientValidation) {
        __SkipClientValidation = false;
        showWaitImage();
    }

    else {
        if (typeof (Page_Validators) != 'undefined')
            Page_ClientValidate();

        if (typeof (Page_IsValid) == 'undefined') {
            showWaitImage();
        }
        else {
            if (Page_IsValid)
                showWaitImage();
            else
                hideWaitImage();
        }

    }
}

function showWaitImage() {
    var div_wait = document.getElementById('div_wait');
    if (div_wait != null)
        div_wait.style.display = '';
}
function hideWaitImage() {
    var div_wait = document.getElementById('div_wait');
    if (div_wait != null)
        div_wait.style.display = 'none';
}



//todo spostabile in dll
function ActiveTabChanged(sender, e) {


    try {
        PreActiveTabChanged(sender, e);
    }
    catch (e) {
    }



    //sistemazione EntityList
    initEntityList();

    var ch_indici = document.getElementById('campohidden_tabcontaineractivetabindexes');
    var ActiveTab_indici = ch_indici.value.split('_');

    var currentContainerIndex = document.getElementById(sender.get_id()).getAttribute('realindex');
    var currentActiveTabIndex = sender.get_activeTab().get_tabIndex();

    var i;
    var arr_indici = new Array();
    for (i = 0; i < ActiveTab_indici.length; i++) {
        if (i == parseInt(currentContainerIndex))
            arr_indici[i] = currentActiveTabIndex;
        else
            arr_indici[i] = parseInt(ActiveTab_indici[i]);
    }

    //renderizzo nuovamente nel campi nascosti
    ch_indici.value = '';

    for (i = 0; i < arr_indici.length; i++) {
        if (i == 0)
            ch_indici.value = arr_indici[i];
        else
            ch_indici.value = ch_indici.value + '_' + arr_indici[i];
    }

}

//numeric text box
function ND_NTB(c, eventObj, allowDecimals) {



    var keyCode;

    // Check For Browser Type
    if (document.all) {
        keyCode = eventObj.keyCode
    }
    else {
        keyCode = eventObj.which
    }

    if (keyCode != 37 && keyCode != 39 && keyCode != 8) //freccia sx e dx, backspace
    {
        var conMeno;
        if (Left(c.value, 1) == '-' || (keyCode == 189 && Left(c.value, 1) == '-'))
            conMeno = true;
        else
            conMeno = false;

        if (allowDecimals == 0)
            c.value = c.value.replace(/\D/ig, '');
        else {
            c.value = c.value.replace('.', ',');
            c.value = c.value.replace(/[^0-9,]/ig, '');
        }


        if (conMeno) {
            c.value = '-' + c.value
        }

    }


}

function Left(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0, n);
}
function Right(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else {
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}

//solo se la modalità di visualizzazione delle risorse è CloeableByToolbarButtons
function ResourceContainerTabIndexChanged(type, DivContentIDList, ButtonHeaderIDList) {

    //type = 0 per ImageEntity
    //type = 1 per FileEntity
    //type = 2 per NoteEntity

    //nascondo tutti i div del contenuto
    var i = 0;
    for (i = 0; i < DivContentIDList.length; i++) {
        if (DivContentIDList[i] != '') {
            var ctl = document.getElementById(DivContentIDList[i]);
            if (ctl != null) {
                ctl.style.display = 'none';
                if (isNaN(type)) type = i;
            }



        }
    }
    //rendo visibile solo il "div contenuto" attivo
    document.getElementById(DivContentIDList[type]).style.display = 'block';
    //lo imposto come attivo per manterenere l'informazione anche durante un postback
    document.getElementById('campohidden_ResourceContainerActiveIndex').value = type;

    //resetto lo stile di tutti i bottoni degli header
    for (i = 0; i < ButtonHeaderIDList.length; i++) {
        var ctl = document.getElementById(ButtonHeaderIDList[i]);
        if (ctl != null)
            ctl.className = 'ResourceContainerNormalButtonHeader';

    }
    //applico uno stile particolare solo al pulsante header attivo
    document.getElementById(ButtonHeaderIDList[type]).className = 'ResourceContainerActiveButtonHeader';

}

function SetResourceContainerVisibility(defaultValue) {
    var ctl = document.getElementById('campohidden_ResourceContainerVisibility');
    if (ctl.value == '') ctl.value = defaultValue.toString();

    var divContainer = document.getElementById('ResourceContainer');

    if (ctl.value.toString() == '0')
        divContainer.style.display = 'none';
    else
        divContainer.style.display = 'block';
}

function ChangeResourceContainerVisibility() {
    var ctl = document.getElementById('ResourceContainer');
    if (ctl.style.display == 'none') {
        ctl.style.display = '';
        document.getElementById('campohidden_ResourceContainerVisibility').value = '1';
    }
    else {
        ctl.style.display = 'none';
        document.getElementById('campohidden_ResourceContainerVisibility').value = '0';
    }

}



/*Effetto allarga testo*/

function restoreText(ctl) {
    var lnk = $(ctl);
    lnk.setAttribute('enlarging', '0');
    lnk.setStyle('letterSpacing', '1px');
}
function enlargeText(ctl) {
    var lnk = $(ctl);
    lnk.setAttribute('enlarging', '1');
    var initialValue = parseInt(lnk.getStyle('letterSpacing'));
    initialValue = initialValue + 1;
    var gap = 30;
    var i = 0;
    for (i = 0; i < 5; i++) {
        window.setTimeout('enlargeTextStep(\'' + lnk.id + '\',' + initialValue.toString() + ')', gap * (i + 1))
        initialValue = initialValue + 1;
    }
}

function enlargeTextStep(lnkID, value) {
    var lnk = $(lnkID);
    if (lnk.getAttribute('enlarging') == '1')
        lnk.setStyle('letterSpacing', value.toString() + 'px');
}


/*
Imposta a true o false il check di tutti i checkbox trovati nela pagina
il cui attributo "tipo" via valorizzato alla variabile 'tipo' passata come 
argomento.
*/
function SetCheckBoxesValueByTipo(tipo, OnOff) {
    var list = $$('input');
    var i = 0;
    for (i = 0; i < list.length - 1; i++) {
        var chk = list[i];
        if (chk.getProperty('type') == 'checkbox')
            if (chk.getProperty('tipo') == tipo) {
            //solo se il checkbox è abilitato
            if (chk.getProperty('disabled') == false) {
                if (OnOff) {
                    chk.setProperty('checked', true);
                }
                else {
                    chk.setProperty('checked', false);
                }
            }

        }
    }
}

function getCheckBoxes() {

    var list = $$('input');
    var res = new Array;
    var i = 0;
    var j = 0;
    for (i = 0; i < list.length - 1; i++) {
        var chk = list[i];
        if (chk.getProperty('type') == 'checkbox') {
            res[j] = chk;
            j += 1;
        }
    }
    return res;

}

function GetCheckBoxesByTipo(tipo) {
    var list = GetCheckBoxes();
    var res = new Array;
    var i = 0;
    var j = 0;
    for (i = 0; i < list.length - 1; i++) {
        var chk = list[i];
        if (chk.getProperty('tipo') == tipo) {
            res[j] = chk;
            j += 1;
        }
    }
    return res;
}


function CountSelectedCheckBoxesByTipo(tipo, value) {
    var list = $$('input');
    var i = 0;
    var tot = 0;
    for (i = 0; i < list.length - 1; i++) {
        var chk = list[i];
        if (chk.getProperty('type') == 'checkbox') {
            if (chk.getProperty('tipo') == tipo) {
                if (chk.getProperty('checked') == value) {
                    tot += 1;
                }
            }
        }
    }
    return tot;
}

function SerializeCollection(list, sep, itemEnv) {
    if (list.length == 0) return '';
    var res = '';
    var i = 0;
    for (i = 0; i < list.length; i++) {

        res += sep + itemEnv + list[i] + itemEnv;
    }

    if (sep != '') { res = res.substring(sep.length); }

    return res;
}

function downloadTempFileByName(filename) {

    location.href = 'WebPlusResources/ajaxServices/downloadTempFileByName.ashx?filename=' + filename;

}


function createWaitDiv(message, imageIndex, messageWidth, messageHeight) {

    if (messageWidth == null)
        messageWidth = 200;

    if (messageHeight == null)
        messageHeight = 80;

    var extDivID = 'wp_div_wait_' + Math.random().toString();
    var extDiv = new Element('div', { 'id': extDiv, 'styles': { 'width': '100%', 'height': '100%', 'position': 'fixed', 'top': '0', 'left': '0', 'z-index': '10'} });
    extDiv.addEvent('dblclick', function() { this.destroy(); });

    var divGrayID = 'wp_div_wait_gray_' + Math.random().toString();
    var divGray = new Element('div', { 'id': divGrayID, 'styles': { 'width': '100%', 'height': '100%', 'text-align': 'center', 'background-color': 'gray', 'position': 'fixed', 'top': '0', 'left': '0', 'z-index': '20', 'filter': 'alpha(opacity=40)', 'opacity': '0.4'} });


    var divMessageID = 'wp_div_wait_message_' + Math.random().toString();
    var divMessage = new Element('div', { 'id': divMessageID, 'styles': { 'background-color': 'white', 'position': 'fixed', 'padding': '4px', 'width': '100%', 'border-bottom': 'solid 1px black', 'z-index': '30'} });

    var img = wp_GlobalObject.getWaitImage(imageIndex)
    img.setStyle('float', 'left');
    img.inject(divMessage);
    var spanMessage = new Element('div', { 'html': message, 'styles': { 'float': 'left', 'position': 'relative', 'top': '0px'} });
    spanMessage.inject(divMessage);



    extDiv.inject(document.body);
    divGray.inject(extDiv);

    divMessage.inject(extDiv);


    return extDiv;

}


function getImageInfo(filename, ecn, res) {

    var xmlhttp = getXmlHTTP();
    var url = 'WebPlusResources/ajaxServices/getImageInfo.ashx?filename=' + filename + '&ecn=' + ecn + '&rnd=' + Math.random();
    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
    if (xmlhttp.status == 200)
        return xmlhttp.responseText; 

}
