function mk_dmsg_sleep(m)
{
    var start = new Date().getTime();
    alert(start);
}
function mk_dmsg()
{
	var dd = document.createElement('div');
		dd.style.cssText = 'display:none; position:absolute; float:left;padding:0px; background-color:white; border:1px solid silver;';
		dd.innerHTML = '<iframe frameborder="0" src="" style="width:100%; height:100%; position:absolute; z-index:-1;"></iframe>';
        dd.ifr = dd.firstChild;
        dd.resize_ifr = function()
        {
            this.ifr.style.height=(((this.offsetHeight-2)>0)?(this.offsetHeight-2):0)+'px';
        }
	    dd.form = document.createElement('form');
        dd.form.dd = dd;
		dd.form.action='';
		dd.cnt = true;
		dd.form.submit = function()
		{
			if (this.action.length==0)
			{
				alert('Indirizzo non impostato');
				return false;
			}
			server.set_form(this);
			server.call(this.action);
			this.innerHTML = server.get_text();
            this.dd.resize_ifr();
            //mk_dmsg_sleep(100);
			return false;
		}
		dd.form.onsubmit = function(){return this.submit();}
		dd.appendChild(dd.form);
		dd.view = function(cmp,lnk)
		{
            if (dd.style.display == '')
            {
                dd.hide();
            }
            
			var x = findPosX(cmp);
			var y = findPosY(cmp)+cmp.offsetHeight;
            if (document.all) x+=4;
			dd.style.top = y+'px';
			dd.style.left = x+'px';
			dd.style.display = '';
			this.cmp = cmp;
			this.set_action(lnk);
//            return;
//            alert('sub');
			dd.form.submit();
            var inp = dd.form.getElementsByTagName('input');
            this.resize_ifr();
            for(var i=0; i<inp.length; i++)
            {
                // focus al primo elemento testuale
                if (inp[i].type.toLowerCase()=='text')
                {
                    inp[i].focus();
                    break;
                }
            }
		}
		dd.hide = function()
		{
			if (this.fmouse)
			{
				this.fmouse = false;
				return;
			}
            if (this.cmp && this.cmp.dmsg_hide)
            {
                this.cmp.dmsg_hide();
            }
			this.style.display = 'none';
			this.form.innerHTML = '';
		}
		dd.close = function()
		{
            if (this.cmp && this.cmp.dmsg_close)
            {
                this.cmp.dmsg_close();
            }
			this.style.display = 'none';
			this.form.innerHTML = '';
		}
		dd.select = function(dval)
		{
            if (this.cmp.dmsg_select)
            {
                this.cmp.dmsg_select(dval);
            }
            else
            {
			    this.cmp.value = dval;
            }
			this.hide();
		}
		dd.onmousedown = function()
		{
			this.fmouse = true;
		}
		dd.set_action = function(lnk)
		{
			dd.form.action = lnk;
		}
		document.body.appendChild(dd);
		return dd;
}

function xcnt(elem)
{
	if (!elem) return null;
	if (elem.cnt) return elem;
	return xcnt(elem.parentNode);
}

document.onmouseup = function()
{
    if (dmsg) dmsg.hide();
}

window.onload = function(){dmsg = mk_dmsg();}

var dmsg = null;

// posizione del mouse durante il click
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function get_parent_node(elem,tn)
{
    if (elem==document) return false;
    if (!elem.tagName) return false;
    if (elem.tagName.toUpperCase() == tn.toUpperCase()) return elem;
    return get_parent_node(elem.parentNode,tn);
}
function get_child(node,n,tag)
{
    var j = 0;
    for(var i=0; i<node.childNodes.length; i++)
    {
        if (node.childNodes[i].nodeType==1)
        {
            if (tag && tag.length && 
                tag.toUpperCase()!= node.childNodes[i].tagName.toUpperCase())
            {
                continue;
            }
            if (n==j++) return node.childNodes[i];
        }
    }
    return node;
}

function dump_node(nd,nameTag)
{
    if (!nd) return '[no node]';
    // text
    if (nd.nodeType==3) return nd.nodeValue;
    
    // not node
    if (nd.nodeType!=1) return'';

    // scrittura del nodo    
    var str = '';
    if (nameTag==true)
    {
        // scrittura del tag name
        str += '<'+nd.tagName;
        for(var ia=0; ia < nd.attributes.length; ia++)
        {
            str += ' '+nd.attributes[ia].nodeName+'="'+
                       escape(nd.attributes[ia].nodeValue)+'"'; 
        }
    }
    if (nd.childNodes.length>0)
    {
        if (nameTag==true)
        {
            str += '>';
        }
        //scrittura figli
        for(var i=0; i<nd.childNodes.length; i++)
        {
            str += dump_node(nd.childNodes[i],true);
        }
        if (nameTag==true)
        {
            str += '</'+nd.tagName+'>';
        }
    }
    else
    {
        if (nameTag==true)
        {
            str += '/>';
        }
    }
    return str;
}

function xserver()
{
       
    // istanziazione oggetto http   
    this.init_server = function()
    {
        this._server_remote = null;
        if (window.ActiveXObject)
        {
            this._server_remote = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else if (window.XMLHttpRequest)
        {
            this._server_remote = new XMLHttpRequest();
        }
    }
    
    this.init_param = function()
    {
        this._server_form   = null;
        this._server_par_nm = new Array();
        this._server_par_val = new Array();
    }
    
    this.init_server();
    this.init_param();
       
       
    // aggiunge i parametri da inviare al servizio
    this.add = function (nm,val)
    {
        this._server_par_nm [this._server_par_nm.length]  = nm;
        this._server_par_val[this._server_par_val.length] = val;
    }
   
    // imposta il form da cui andare a prelevare i parametri
    this.set_form = function (lfrm)
    {
        this._server_form = lfrm;
    }
   
    this.display_message = function (msg,container_typ,container)
    {
        if (msg != '')
        {                                             
            if (container == undefined || container == null)
            {
                container ='err_msg';
            }
            
            if (container_typ == undefined || container_typ == null)
            {
                container_typ = 'alert';
            }                          
            if (container_typ == 'div')
            {
                if (document.getElementById(container) == null)
                {
                    document.body.innerHTML += '<div id="'+container+'" style="position: absolute; left: 10%; top: 10%; background-color: white; border: 1px solid blue; display: none; font-family: Verdana; font-size: 12px; padding: 10px" onClick="this.style.display=\'none\';">&nbsp;</div>';
                }
                var cont = document.getElementById(container);
                cont.innerHTML = msg + '<br/><br/>' + '<input type="button" onClick="document.getElementById(\'' + container + '\').style.display = \'none\';" class="chiudi" value="Chiudi">';        
                cont.style.display = '';
            }
            else if(container_typ == 'popup')
            {
                var cont = dlgModal('','popup',300,400);
                cont.document.write('<html></head><link rel="stylesheet" href="/abile/style1.css"></head><body>');
                cont.document.write('<table cellpadding="0" cellspacing="0" width="100%"><tr><td>');
                cont.document.write('<table cellpadding="0" cellspacing="0"><tr><th bgcolor="#80a9e5" style="color:white; padding:0 10 0 15; font-size:12px; ">ERRORE</th><td><img src="/abile/images/icone/curva2.gif"/></td></tr></table>');
                cont.document.write('</td></tr><tr><td bgcolor="#f1f7f7" class="cell_top_bottom_left_right" style="padding:10px; height:230px; text-align:center"><table align="center"><tr><td style="font-size:14px;">');
                cont.document.write(msg);
                cont.document.write('</td></tr></table></td></tr>');
                cont.document.write('<tr><th bgcolor="#80a9e5" ><input type="button" onClick="window.close();" class="chiudi" value="Chiudi"></th></tr>');
                cont.document.write('</table></body></html>');
                cont.document.close();
            }
            else
            {
                alert(msg);
            }                                                                                                 
        
        }
    }
       
    this.call = function (srv,resp)
    {
        if (!this._server_remote)
        {
            alert('Chiamata as servizio '+srv+' impossibile.\nServer non inizializzato.');
            return;
        }
        var i;
        var str = '';
        var elem = null;     
        if (this._server_form)
        {
            // se è impostata un form di riferimento allora
            // allineo i parametri con i suoi valori
            for (var i = 0; i < this._server_form.length; i++)
            {
                elem = this._server_form.elements[i];
                switch(elem.type)
                {
                case 'checkbox':
                case 'radio':
                    // se non è impostato allora non lo inserisco come parametro
                    if(!elem.checked) continue;
                    break;
                }
                if (elem.disabled) continue;
                this.add(elem.name,
                         elem.value);
            }
        }
       
        if (this._server_par_nm.length>0)
        {
            for(i = 0; i < this._server_par_nm.length; i++)
            {
                str += this._server_par_nm[i]+'='+escape(this._server_par_val[i]) + '&';
            }
            str = str.substring(0,str.length-1);
        }
       
        // pulizia parametri 
        this.init_param();
        
        if (resp==undefined)
        {
            // sincrono
            try
            {
                this._server_remote.open("POST", srv, false);
                this._server_remote.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                this._server_remote.send(str);
            }
            catch(e)
            {
//                this.display_message(e.message,'div');
            }
            
            var xml = this.get_xml();
            
            if (xml && xml.tagName=='error')
            {
                return false;
            }
            if (xml && xml.tagName=='parsererror')
            {
                var ss = xml.getElementsByTagName('sourcetext');
                this.display_message(dump_node(ss[0]),'div');
                return false;
            }
        }
        else
        {
            // asincrono
            this._server_remote.open("POST", srv, true);
            this._server_remote.onreadystatechange = resp;
            this._server_remote.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            this._server_remote.send(str);
        }
   
        return true;
    }

    this.display_error = function()
    {
        this.display_message(dump_node(this.get_xml()));
    }
    this.get_text = function ()
    {
        return this._server_remote.responseText;
    }
       
    this.get_xml = function ()
    {
        if (this._server_remote.responseXML)
        {
            return this._server_remote.responseXML.documentElement;
        }
        return null;
    }
    
    return this;
}

var server = xserver(); 
