﻿function AQCmsJsManager(){
    this.div;
    this.oForm = document.forms[0];
    this.idMenu;
    this.idImgPreload = 'imgLoading';
    this.caller;
    this.callerBackColor = '';
    this.divAdminCommands = '';
}

AQCmsJsManager.prototype.getResult = function(arg, context) {
    var content = arg.split('|');

    /*
    if (content[0]=='relog') {location.href = '/AQUEST/AQCMS/admin/login.aspx';return;}
    else {
    //        if (content[0]=='del'){
    //            for (var i=0;i<context.oForm.getElementsByTagName('div').length;i++){
    //                var el = context.oForm.getElementsByTagName('div')[i];
    //                alert(el.id + ' - ' + context.div.id);
    //                if (el && el.id == context.div.id) { alert(el.innerHTML); }//context.oForm.removeChild(el); break;
    //            }
    //        }
    context.div.style.display = '';
    var response=content[0];  //codice risposta

        var cnt = unescape(content[1]);
    var newText = '';
    for (idx=0;idx<cnt.length;idx++){
    if (cnt.charAt(idx) == '+' && 1 != 1) newText += ' ';
    else newText += cnt.charAt(idx);
    } 
    context.setToDebug(newText);
    context.div.innerHTML = newText;
        
        

    }
    */
    context.preLoad(false);
    if (content[0] != '') {
        context.div = document.getElementById(content[0]);
    }
    context.div.innerHTML = '';
    context.div.innerHTML = content[1];
    //context.setToDebug(content[1]);
};
AQCmsJsManager.prototype.setToDebug = function(msg) {
    //return;
    if (location.hostname.indexOf('localhost') != -1) {
        if (document.getElementById('divDebug')) document.getElementById('divDebug').innerText = msg;
    }
};

AQCmsJsManager.prototype.setSelect = function(idRiga, idMenu, idMenuPadre) {
    this.preLoad(true);
    callServer('sel$' + idRiga + '$' + idMenu + '$' + idMenuPadre);
};
AQCmsJsManager.prototype.setDeselect = function(idMenu, idMenuPadre) {
    this.preLoad(true);
    callServer('annulla$-1$' + idMenu + '$' + idMenuPadre);
};

AQCmsJsManager.prototype.setPubblica = function(idMenu, pubblica) {
this.preLoad(true);
    callServer('pubblica$' + idMenu + '$'+pubblica);
};
AQCmsJsManager.prototype.getError = function(arg,context){
    alert(arg);
};

AQCmsJsManager.prototype.setModify = function (idTable, idRiga, isNewMenu, idMenu, idMenuPadre) {
    this.preLoad(true);
    var txtVals = '', ckbVals = '', ddlVals = '';
    var tbl = document.getElementById(idTable);
    var row;
    if (tbl) {
        var rows = tbl.getElementsByTagName("tr");
        if (idRiga == -1) {
            //footer
            row = rows[rows.length - 1];
        } else {
            for (var x = 1; x < rows.length; x++) { //la posizione 0 è riservata all'header della table
                if (x != parseInt(idRiga)) continue;
                row = rows[x];
                break;
            }
        }
        if (row) {
            var col = row.getElementsByTagName('input');
            for (var x = 0; x < col.length; x++) {
                var obj = col[x];
                if (obj.type == 'checkbox') {
                    if (ckbVals != '') ckbVals += '|';
                    ckbVals += (obj.checked ? '1' : '0');
                }
                else if (obj.type == 'text') {
                    if (txtVals != '') txtVals += '|';
                    txtVals += obj.value;
                }
            }
            col = row.getElementsByTagName('select');
            for (var x = 0; x < col.length; x++) {
                var obj = col[x];
                var opt = obj.options[obj.selectedIndex].value;
                if (ddlVals != '') ddlVals += '|';
                if (opt == 'sel') opt = '';
                ddlVals += opt;
            }
        }
    }
    callServer('' + (isNewMenu ? 'add' : 'mod') + '$-1$' + idMenu + '$' + idMenuPadre + '$' + (txtVals + '|' + ckbVals + '|' + ddlVals));
};
AQCmsJsManager.prototype.setDel = function(idDiv, idMenu, idMenuPadre) {
    this.preLoad(true);
    if (confirm('Cancellare la voce selezionata?')){
        this.div = document.getElementById('newDiv_'+idDiv);
        callServer('del$-1$' + idMenu + '$' + idMenuPadre);
    }
}
AQCmsJsManager.prototype.doCreateHidden = function(namesValues){
    if (document.createElement){
        var arr = namesValues.split(',');
        for(i=0;i<arr.length;i++){
            var tokens=arr[i].split('=');
            var oldObj=document.getElementById(tokens[0]);
            if (oldObj){
                oldObj.value='';
                oldObj.value=tokens[1];
            }else{
                var newField = document.createElement("input");
                newField.type = "hidden";
                newField.name = tokens[0];
                newField.id = tokens[0];
                newField.value = tokens[1];
                document.forms[0].appendChild(newField);
            }
        }            
    } else{
        alert('Il browser in uso non supporta operazioni fondamentali per l\'uso di questo sito.')
    }
};
AQCmsJsManager.prototype.preLoad = function(show) {
    var obj = document.getElementById(this.idImgPreload);
    if (obj!=null) obj.style.display = (show==true) ? 'block' : 'none';
 };
AQCmsJsManager.prototype.callSrv = function (showPreload) {
    this.preLoad(showPreload);
    callServer('sel$'+this.idMenu+'$null');
};
AQCmsJsManager.prototype.doPostBack = function (where){
    this.oForm.action = where;
    this.oForm.submit();
};
AQCmsJsManager.prototype.getLeft = function(idCaller) {
    var oNode = document.getElementById(idCaller);
    var iLeft = 0;
    while (oNode) {
        if (oNode.tagName.toLowerCase() != 'body') {
            iLeft += oNode.offsetLeft;
            oNode = oNode.offsetParent;
            if (oNode == null) return iLeft;
        }
    }
    return iLeft;
};
AQCmsJsManager.prototype.getTop = function(idCaller) {
    var oNode = document.getElementById(idCaller);
    var iTop = 0;
    while (oNode) {
        if (oNode.tagName.toLowerCase() != 'body') {
            iTop += oNode.offsetTop;
            oNode = oNode.offsetParent;
            if (oNode == null) return iTop;
        }
    }
    return iTop;
};
AQCmsJsManager.prototype.conta = function (idTxt,maxLimit){
    var objTxt= document.getElementById(idTxt);
    if (!objTxt) return;
    var lblCounter = document.getElementById(idTxt+'_lbl');
    if (!lblCounter){
        lblCounter = document.createElement('span');
        lblCounter.id = idTxt+'_lbl';
        lblCounter.style.width='50px'
        lblCounter.style.padding='5px';
        lblCounter.style.position='relative';
        lblCounter.innerHTML='max';
        objTxt.parentNode.appendChild(lblCounter);
    }
    lblCounter.style.color = '#000';
    
    if (objTxt.value.length < 10) lblCounter.style.backgroundColor = '#0f0';
    else if (objTxt.value.length > 10 && objTxt.value.length <= 20) lblCounter.style.backgroundColor = '#ff0';
    else if (objTxt.value.length > 20 && objTxt.value.length <= maxLimit) {
        lblCounter.style.color = '#fff';
        lblCounter.style.backgroundColor = '#f00';
    }
    else if (objTxt.value.length > maxLimit) {
        objTxt.value = objTxt.value.substring(0,maxLimit);
    }
    lblCounter.innerHTML = " " + (30 - objTxt.value.length);
};
//gestione COOKIE
AQCmsJsManager.prototype.setCookie = function (name,value,expires){
    var cookieVal=this.getCookie(name);
    if (cookieVal){
        cookieVal = value;
        document.cookie = name + "=" + escape (cookieVal);
    } else {
        document.cookie = name + "=" + escape (value) +
        ((expires) ? '; expires=' + expires.toGMTString() : '');
    }
};
AQCmsJsManager.prototype.delCookie = function(name,path,domain){
    if (this.getCookie(name)) {
        document.cookie = name + "=" +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
};
AQCmsJsManager.prototype.getCookie = function (name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            return this.getCookieVal (j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
};
AQCmsJsManager.prototype.getCookieVal = function (offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) { endstr = document.cookie.length; }
    return unescape(document.cookie.substring(offset, endstr));
};
AQCmsJsManager.prototype.setTargetPopUp = function(ddl, idDiv, optWhen) {
    var arrPos = this.getObjectPosition(ddl);
    var opt = ddl.options[ddl.selectedIndex].value;
    var div = document.getElementById(idDiv);
    this.setCookie('ckTargetPopUp','');
    if (div) {
        div.style.display = (opt == optWhen ? '' : 'none');
        if (div.style.display == '') {
            //            var _top = (document.body.offsetHeight-150) / 2;
            //            var _left = (document.body.offsetWidth-150) / 2;
            //            alert(_top + ' ' + _left)
            //            div.style.left = _left + document.body.scrollLeft + 'px';
            //            div.style.top = _top + document.body.scrollTop + 'px';
            div.style.left = arrPos[0] + 'px';
            div.style.top = arrPos[1] + 'px';

            //            var inputs = div.getElementsByTagName('input');
        }
    }
};

AQCmsJsManager.prototype.setModificaPopUp = function(caller, idDiv, parametri) {
    var arrPos = this.getObjectPosition(caller);
    var div = document.getElementById(idDiv);
    if (div) {
        div.style.display = '';
        div.style.left = arrPos[0] + 'px';
        div.style.top = arrPos[1] + 'px';
    }
};

AQCmsJsManager.prototype.getObjectPosition = function(obj) {
    var curleft = 0;
    var curtop = 0;
      if (obj.offsetParent) {
            do {
                  curleft += obj.offsetLeft;
                  curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
      }
      return [curleft,curtop];
};
AQCmsJsManager.prototype.setHideNomiModuli = function (lblCaller, idDiv) {
    if (this.divAdminCommands == '') {
        var divs = document.getElementsByTagName("div");
        if (divs) {
            for (i = 0; i < divs.length; i++) {
                var div = divs[i];
                if (div.id.indexOf(idDiv) == -1) continue;
                this.divAdminCommands = div;
                break;
            }
        }
    }

    this.divAdminCommands.style.display = (this.divAdminCommands.style.display == 'none' ? '' : 'none');
    lblCaller.innerHTML = (this.divAdminCommands.style.display == 'none' ? 'visualizza nome moduli' : 'nascondi nome moduli');
    this.setCookie('ck_hide_edit_section', (this.divAdminCommands.style.display == 'none' ? '0' : '1'));

}  
