﻿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) {

    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');
}

