﻿function openDialog(page, offset) {
    // Giving the arguments to the dialog
    var width = 800;
    var height = 600;
    var dummy = 0;
    if (offset != null) {
        dummy = 30 * offset;
    }
    window.open(page, '', 'modal=yes,width=' + width + ',height=' + height + ',scrollbars=yes, resizable=no,top=' + ((screen.availHeight / 2) - (height / 2) + dummy) + ',left=' + ((screen.availWidth / 2) - (width / 2) + dummy) + ')');
}
function showDocument(page, offset) {
    // Giving the arguments to the dialog
    var width = 800;
    var height = 600;
    var dummy = 0;
    if (offset != null) {
        dummy = 30 * offset;
    }
    window.open(page, '', 'modal=no,location=no,width=' + width + ',height=' + height + ',scrollbars=yes, resizable=yes,top=' + ((screen.availHeight / 2) - (height / 2) + dummy) + ',left=' + ((screen.availWidth / 2) - (width / 2) + dummy) + ')');
}
function showReport(page, offset) {
    // Giving the arguments to the dialog
    /*
    var width = 800;
    var height = 600;
    var dummy = 0;
    if (offset != null) {
        dummy = 30 * offset;
    }
    */
    //window.open(page, '', 'modal=no,location=no,width=' + width + ',height=' + height + ',scrollbars=yes, resizable=yes,top=' + ((screen.availHeight / 2) - (height / 2) + dummy) + ',left=' + ((screen.availWidth / 2) - (width / 2) + dummy) + ')');
    window.open(page, '_blank', '');
}
function showSaveCookie(page, offset) {
    // Giving the arguments to the dialog
    var width = 300;
    var height = 100;
    var dummy = 0;
    if (offset != null) {
        dummy = 30 * offset;
    }
    window.open(page, '', 'modal=no,width=' + width + ',height=' + height + ',scrollbars=yes, resizable=yes,top=' + ((screen.availHeight / 2) - (height / 2) + dummy) + ',left=' + ((screen.availWidth / 2) - (width / 2) + dummy) + ')');
}
function openDialogWithArguments(page, argumentName, argumentIdValue, offset) {
    var url = page + '?' + argumentName + '=' + document.getElementById(argumentIdValue).value;
    openDialog(url, 2);
}

function confirmClient(msg) {
    var x = window.confirm(msg);
    if (x) {
        return true;
    }
    else {
        return false;
    }
}
function expandcollapse(obj, row) {
    var div = document.getElementById(obj);
    var img = document.getElementById('img' + obj);
    if (div != null) {
        if (div.style.display == "none") {
            div.style.display = "block";
            if (row == 'alt') {
                img.src = "../images/minus.gif";
            }
            else {
                img.src = "../images/minus.gif";
            }
            img.alt = "Clicca per chiudere";
        }
        else {
            div.style.display = "none";
            if (row == 'alt') {
                img.src = "../images/plus.gif";
            }
            else {
                img.src = "../images/plus.gif";
            }
            img.alt = "Clicca per espandere";
        }
    }
}
function doPostBackAsync(eventName, eventArgs) {
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    if (!Array.contains(prm._asyncPostBackControlIDs, eventName)) {
        prm._asyncPostBackControlIDs.push(eventName);
    }
    if (!Array.contains(prm._asyncPostBackControlClientIDs, eventName)) {
        prm._asyncPostBackControlClientIDs.push(eventName);
    }
    __doPostBack(eventName, eventArgs);
}

function modalPopupMessage(isError, title, body, infoIconUrl, errorIconUrl) {
    if (isError)
        document.getElementById('imgMessageIcon').src = errorIconUrl;
    else
        document.getElementById('imgMessageIcon').src = infoIconUrl;
    document.getElementById('spanMasterMessageTitle').innerHTML = title;
    document.getElementById('spanMasterMessageBody').innerHTML = body;
}

function addRemoveCheckID(hiddenControlId, checkBoxId) {
    var hiddenField = document.getElementById(hiddenControlId);
    var content = hiddenField.value;
    var arrayIDs = content.split(";").sort();
    if (content == "")
        arrayIDs = new Array();
    var elementFound = false;
    var index = -1;
    for (var i = 0; i < arrayIDs.length; i++) {
        if (arrayIDs[i] == checkBoxId) {
            elementFound = true;
            index = i;
        }
    }
    if (!elementFound) {
        if (content!="")
            hiddenField.value = content + ";" + checkBoxId;
        else
            hiddenField.value = checkBoxId;
    }
    else {
        arrayIDs.splice(index, 1);
        hiddenField.value = arrayIDs.join(";");
    }
    //alert(hiddenField.value);
    return true;
}

function clearCheckedId(hiddenControlId) {
    document.getElementById(hiddenControlId).value = '';
    return true;
}


function CheckUncheckAll(tabella, checkselectall) {
    var tabdoc = document.getElementById(tabella)
    var stato = document.getElementById(checkselectall).checked;
    var controlli = tabdoc.getElementsByTagName("input");
    for (i = 1; i < controlli.length; i++) {
        var current = controlli[i];
        if (current.getAttribute("type") == "checkbox") {
            //la selezione la devo fare richiamando il click client
            //e non settando semplicemente l'attributo "checked" perche il click
            // registra anche l'id nel carrello della spesa
            //inoltre va fatta solo nei seguenti casi, per non incasinare gli id nel carrello
            if ((stato == true) && (current.checked == false)) {
                //se devo selezionare tutto, spunto solo quelli non ancora checkccati
                current.click();
            }
            else if ((stato == false) && (current.checked == true)) {
                //se devo deselezionare tutto , spunto solo quelli gia ceccati
                current.click();
            }
        }
    }
    return true
}
