﻿function AQCmsJsManager(){
    this.div;
    this.oForm = document.forms[0];
    this.idMenu;
    this.idImgPreload='myImgPreload';
    this.caller;
    this.callerBackColor='';
}
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;
//            }
//        }
        /*if (response != ''){
            alert(response);
        }*/
        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;
    }
};
AQCmsJsManager.prototype.setToDebug = function(msg){
    if (location.hostname.indexOf('localhost') != -1) {
        if (document.getElementById('divDebug')) document.getElementById('divDebug').innerText=msg;
    }
};
AQCmsJsManager.prototype.expandcollapse = function(idCaller, _idMenu)
{
    this.idMenu=_idMenu;
    var img = document.getElementById(idCaller);
    this.caller = img;
    var srcPart=img.src.substring(0,img.src.lastIndexOf('/')+1);
    var newDiv = document.getElementById('newDiv_'+_idMenu);
    if (!newDiv){
        newDiv=document.createElement('div');
        newDiv.id='newDiv_'+_idMenu;
        newDiv.style.display='none';
        newDiv.style.position='absolute';
        newDiv.innerHTML='<span style="color:#FFFFFF;background-color:#0000aa;padding:5px;font-size:13px;">Loading...</span>';
        this.oForm.appendChild(newDiv);  
        this.div=newDiv;      
    }
    if (newDiv.style.display == "none")
    {
        img.parentNode.style.backgroundColor=this.callerBackColor;  //il td
        newDiv.style.display = "block";
        newDiv.style.left = (15+this.getLeft(idCaller))+"px";
        newDiv.style.top = (25+this.getTop(idCaller))+"px";
        img.alt = "Click to close";
        img.src = srcPart + "minus.gif";
        this.callSrv(true);
    }
    else
    {
        img.parentNode.style.backgroundColor=''; //il td
        this.setToDebug('nascondo ' +newDiv.id + ' suo padre --> ' + newDiv.parentNode.nodeName);
        this.oForm.removeChild(newDiv);
    for(var i=0;i<document.getElementsByTagName('div').length;i++){
        this.setToDebug('\r\n\r\n'+document.getElementsByTagName('div')[i].id);
    }        
        img.src = srcPart+ "plus.gif";
        img.alt = "Expand to view";
    }
};
AQCmsJsManager.prototype.setEdit = function(idTable, idRiga){
    var x=0;
    var tbl = document.getElementById(idTable);
    var obj;
    //cicla e disabilita tutte le textbox della tabella
    var arrTxts = tbl.getElementsByTagName('input');
    for(x=0;x < arrTxts.length;x++){
        obj = arrTxts[x];
        if (obj.id.indexOf('Mod') != -1) {
            obj.disabled= !(obj.id.indexOf('_'+idRiga+'_') != -1);
            obj.style.backgroundColor = (!obj.disabled) ? '#aaff00' : '';
        }
    }
    
    
    //cicla e disabilita tutte le immagini Add della tabella
    for(x=0;x<tbl.getElementsByTagName('img').length;x++){
        obj = tbl.getElementsByTagName('img')[x];
        if (obj != null && obj.id.indexOf('Add_') != -1) {
            obj.style.display = 'none';   //nasconde tutti gli img Add
            if (obj.id.indexOf('Add_'+idRiga+'_') != -1) {obj.style.display = '';} //obj.src='/AQCMS/images/loading.gif';visualizza solo quello voluto
        }
    }
};
AQCmsJsManager.prototype.getError = function(arg,context){
    alert(arg);
};
AQCmsJsManager.prototype.setModify = function(isMod, idTable, idMenu,idRiga){
    var vals = '';
    var tbl = document.getElementById(idTable);
    for (var x=0;x<tbl.getElementsByTagName('input').length;x++){
        var obj = tbl.getElementsByTagName('input')[x];
        if (obj.id.indexOf('_'+idRiga+'_') != -1) {
            if (vals != '') vals += '|';
            vals += obj.value;
        }
    }
    //this.preLoad(true);
    if (vals == '') {alert('Non è stato inserito alcun valore');}
    else{
        if (isMod) callServer('mod,'+idMenu+','+vals);
        else callServer('add,'+idMenu+','+vals);
    }
};
AQCmsJsManager.prototype.setDel = function(idDiv,idMenu){
    if (confirm('Cancellare la voce selezionata?')){
        this.div = document.getElementById('newDiv_'+idDiv);
        callServer('del,'+idMenu+',null');
    }
}
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.tagName != "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.tagName != "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));
};