
Proc="";
Addparameters="";
result="";
   var http_request = false;
   function makePOSTRequest(url, parameters) {
	   parameters+=Addparameters;
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
			  
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            result = http_request.responseText;
//		alert(Proc);    
			if (Proc!="") eval(Proc);    
			//alert(result);
				

         } else {
            //alert('There was a problem with the request.');
         }
      }
   }
   
   
      function PostForm(obj,NextProc) {
      var poststr = "";
		  Proc=NextProc;
	   for(var i=0,objs=obj.elements;i<objs.length;i++) 
	   {
		  try {
				 TagName=objs[i].tagName;
				 TagType=objs[i].type;
				}
			catch (errore_verificato) 
			{
			window.alert("Si è verificato l'errore \"" + errore_verificato.name + "\"\n(" + errore_verificato.description + ")");
			}

	  //alert(TagName);
		 if (TagName == "INPUT") {
            if ((TagType == "button") || (TagType == "text") || (TagType == "hidden") || (TagType == "password"))
			{
               poststr += objs[i].name + "=" + objs[i].value + "&";
            }
            if (TagType == "checkbox") {

               if (objs[i].checked) {
                  poststr += objs[i].name + "=" + objs[i].value + "&";
               } else {
                  poststr += objs[i].name + "=&";
               }
            }
            if (TagType == "radio") {
               if (objs[i].checked) {
                  poststr += objs[i].name + "=" + objs[i].value + "&";
               }
            }
         }   
         if (TagName == "SELECT") {
            var sel = objs[i];
            poststr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
         }
         if (TagName == "TEXTAREA") {
            var sel = objs[i];
            poststr += sel.name + "=" + encodeURI( objs[i].value ) + "&";
			
         }
         
      }
      makePOSTRequest('/source/Call.php?FLE=GAR', poststr);
   }


     function GetAllFieldForm(obj,returnobj) {
      var poststr = "";
	  var postobj = [];
	  var TagName;
	  var TagType;
	  var objName;
	  
	   for(var i=0,objs=obj.elements;i<objs.length;i++) 
	   {
		  try {
				 TagName=objs[i].tagName;
				 TagType=objs[i].type;
				 objName=objs[i].name;
				}
			catch (errore_verificato) 
			{
			window.alert("Si è verificato l'errore \"" + errore_verificato.name + "\"\n(" + errore_verificato.description + ")");
			}
	  //alert(TagName);
		 if (TagName == "INPUT") {
            if ((TagType == "button") || (TagType == "text") || (TagType == "hidden") || (TagType == "password"))
			{
               poststr += objs[i].name + "=" + objs[i].value + "&";
			   postobj[objName]=objs[i].value;
            }
            if (TagType == "checkbox") {
               if (objs[i].checked) 
				   {
					  poststr += objs[i].name + "=" + objs[i].value + "&";
					  postobj[objName]=objs[i].value;
				   } else {
					  poststr += objs[i].name + "=&";
					  postobj[objName]="";
				   }
            }
            if (TagType == "radio") {
               if (objs[i].checked) {
                  poststr += objs[i].name + "=" + objs[i].value + "&";
				  postobj[objName]=objs[i].value;
               }
            }
         }   
         if (TagName == "SELECT") {
            var sel = objs[i];
            poststr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
			postobj[sel.name]=sel.options[sel.selectedIndex].value;
         }
         if (TagName == "TEXTAREA") {
            var sel = objs[i];
            poststr += sel.name + "=" + encodeURI( objs[i].value ) + "&";
			postobj[sel.name]=encodeURI( objs[i].value );
			
         }
         
      }
      if (returnobj) return postobj; else poststr;
   }



function SendDataForm(obj,Span,Tmpl)
{	
	Addparameters="&PROC=LoadForm&TMPLCODE="+Tmpl;
	PostForm(obj,"ScriveRisultato('"+Span+"',result)");
	


}

function LoadDataAjax(obj,Span,Tmpl,Par)
{	
	Addparameters="&PROC=LoadForm&TMPLCODE="+Tmpl+Par;
	Proc="ScriveRisultato('"+Span+"',result)";
	makePOSTRequest('/source/Call.php?FLE=GAR', "");
	
	


}

function LoadAjaxTemplate(Span,Tmpl,Par)
{	
	Addparameters="&TMPLCODE="+Tmpl+Par;
	Proc="ScriveRisultato('"+Span+"',result)";
	makePOSTRequest('/source/Call.php?FLE=LTM', "");
	
	


}

function LoadAjax(obj,ProcCall,Span,Par)
{	
	Addparameters="&PROC="+ProcCall;
	//alert(Addparameters);
	Proc="ScriveRisultato('"+Span+"',result)";
	if (obj!="") PostForm(obj,"ScriveRisultato('"+Span+"',result)"); else makePOSTRequest('/source/Call.php?FLE=GAR', "");

	//alert(http_request.responseText);
	


}


var DataAjax = function() {
	
	var ThisObject=this;
	this.multipart=0;
	   this.LoadPage = function (url, parameters)
	   {
      	ThisObject.http_request = false;
      	if (window.XMLHttpRequest) 
			{ // Mozilla, Safari,...
			 ThisObject.http_request = new XMLHttpRequest();
			 
			 if (ThisObject.http_request.overrideMimeType) 
			 	{
            		ThisObject.http_request.overrideMimeType('text/html');
         		}
      		} 
			else if (window.ActiveXObject) 
					{ // IE
         				try {
            				ThisObject.http_request = new ActiveXObject("Msxml2.XMLHTTP");
         					} catch (e) 
								{
            						try {
               							ThisObject.http_request = new ActiveXObject("Microsoft.XMLHTTP");
            							} 
									catch (e) {}
         						}
      				}
		  if (!ThisObject.http_request) 
		  {
			 alert('Cannot create XMLHTTP instance');
			 return false;
		  }
			  
      ThisObject.http_request.onreadystatechange = ThisObject.callbackFirst;
      ThisObject.http_request.open('POST', url, true);
	  //Attenzione il multipart non funziona
	  if (ThisObject.multipart==0) ThisObject.http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	
	  	else
		  ThisObject.http_request.setRequestHeader("Content-Type", "multipart/form-data");
      ThisObject.http_request.setRequestHeader("Content-length", parameters.length);
      ThisObject.http_request.setRequestHeader("Connection", "close");
      ThisObject.http_request.send(parameters);
   } // End Load Data Ajax


        this.LoadData = function(addParam) {
			  var poststr = "";
			  
			  if (addParam!="") addParam="&"+addParam;
			  poststr+=addParam;
			  ThisObject.LoadPage('/source/Call.php?FLE=GAR', poststr);
   } //End PostForm


        this.PostForm = function(obj,addParam) {
			  var poststr = "";
			  poststr=PreparaDati(obj,ThisObject.multipart);
			  if (addParam!="") addParam="&"+addParam;
			  poststr+=addParam;
			  ThisObject.LoadPage('/source/Call.php?FLE=GAR', poststr);
   } //End PostForm

   this.callbackFirst=function(res){};
   this.callback=function(res){};
   
   this.callbackFirst = function (res)
   {
	     if (ThisObject.http_request.readyState == 4) {
         if (ThisObject.http_request.status == 200) {
            result = ThisObject.http_request.responseText;
			//if (Proc!="") eval(Proc);    
			//alert(result);
			ThisObject.callback(result);
				

         } else {
            //alert('There was a problem with the request.');
         }
      }
	   }

}



//AJAX
function AJAXReq(method,url,bool,IDForm){
  if(window.XMLHttpRequest){
    myReq = new XMLHttpRequest();
  } else 
  
  if(window.ActiveXObject){
    myReq = new ActiveXObject("Microsoft.XMLHTTP");
    
    if(!myReq){
      myReq = new ActiveXObject("Msxml2.XMLHTTP");
    }
  }
  
  if(myReq){
    execfunc(method,url,bool,IDForm);
  }else{
    alert("Impossibilitati ad usare AJAX");
  }
}

function execfunc(method,url,bool,IDForm){
  myReq.onreadystatechange = handleResponse;
  myReq.open(method,url,bool);
  myReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
  stringa = PreparaDati(IDForm)
  myReq.send(stringa);
}

function handleResponse(){
  if(myReq.readyState == 4){
    if(myReq.status == 200){
      //alert(myReq.responseText);
    }else{
      alert("Niente da fare, AJAX non funziona :(");
    }
  }
}

function PreparaDati(IDForm,mp){
  stringa = "";
  var form = IDForm;
  var numeroElementi = form.elements.length;
  var StrSend = "";
  
  if (mp==1)
  	{
		
      StrSend = "";
      var boundary = "proto" + Math.random();
      for (var i = 0; i < numeroElementi; i++) 
	  {
		    StrSend += "--" + boundary + "\nContent-Disposition: form-data;name=\"" + form.elements[i].name + "\"\n";
			StrSend += "\n";
			StrSend += form.elements[i].value + "\n";
			StrSend += "\n";
		}
 		StrSend += "--" + boundary + "--";
		stringa=StrSend;
		}
 
	else
 	{
	  for(var i = 0; i < numeroElementi; i++){
	  TagType=form.elements[i].type;
      if (TagType == "checkbox") {
	   if (form.elements[i].checked) StrSend = form.elements[i].name + "= 1" ;
	   	else StrSend = form.elements[i].name + "= 0";
               
	}
	else
	   if (TagType == "radio") {
               if (form.elements[i].checked) {
                  StrSend = form.elements[i].name + "=" + form.elements[i].value ;
               }
            }
		else StrSend = form.elements[i].name+"="+encodeURIComponent(form.elements[i].value);		
    if(i < numeroElementi-1){
      stringa += StrSend + "&";
    }else{
      stringa += StrSend;
    }  
  }
	}	
  return stringa;
}




WPL_JSON=new (function(){
    var useHasOwn = {}.hasOwnProperty ? true : false;
    
    // crashes Safari in some instances
    //var validRE = /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/;
    
    var pad = function(n) {
        return n < 10 ? "0" + n : n;
    };
    
    var m = {
        "\b": '\\b',
        "\t": '\\t',
        "\n": '\\n',
        "\f": '\\f',
        "\r": '\\r',
        '"' : '\\"',
        "\\": '\\\\'
    };

    var encodeString = function(s){
        if (/["\\\x00-\x1f]/.test(s)) {
            return '"' + s.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                var c = m[b];
                if(c){
                    return c;
                }
                c = b.charCodeAt();
                return "\\u00" +
                    Math.floor(c / 16).toString(16) +
                    (c % 16).toString(16);
            }) + '"';
        }
        return '"' + s + '"';
    };
    
    var encodeArray = function(o){
        var a = ["["], b, i, l = o.length, v;
            for (i = 0; i < l; i += 1) {
                v = o[i];
                switch (typeof v) {
                    case "undefined":
                    case "function":
                    case "unknown":
                        break;
                    default:
                        if (b) {
                            a.push(',');
                        }
                        a.push(v === null ? "null" : Ext.util.JSON.encode(v));
                        b = true;
                }
            }
            a.push("]");
            return a.join("");
    };
    
    var encodeDate = function(o){
        return '"' + o.getFullYear() + "-" +
                pad(o.getMonth() + 1) + "-" +
                pad(o.getDate()) + "T" +
                pad(o.getHours()) + ":" +
                pad(o.getMinutes()) + ":" +
                pad(o.getSeconds()) + '"';
    };
    
    /**
     * Encodes an Object, Array or other value
     * @param {Mixed} o The variable to encode
     * @return {String} The JSON string
     */
    this.encode = function(o){
        if(typeof o == "undefined" || o === null){
            return "null";
        }else if(Ext.isArray(o)){
            return encodeArray(o);
        }else if(Ext.isDate(o)){
            return encodeDate(o);
        }else if(typeof o == "string"){
            return encodeString(o);
        }else if(typeof o == "number"){
            return isFinite(o) ? String(o) : "null";
        }else if(typeof o == "boolean"){
            return String(o);
        }else {
            var a = ["{"], b, i, v;
            for (i in o) {
                if(!useHasOwn || o.hasOwnProperty(i)) {
                    v = o[i];
                    switch (typeof v) {
                    case "undefined":
                    case "function":
                    case "unknown":
                        break;
                    default:
                        if(b){
                            a.push(',');
                        }
                        a.push(this.encode(i), ":",
                                v === null ? "null" : this.encode(v));
                        b = true;
                    }
                }
            }
            a.push("}");
            return a.join("");
        }
    };
    
    /**
     * Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws a SyntaxError.
     * @param {String} json The JSON string
     * @return {Object} The resulting object
     */
    this.decode = function(json){
        return eval("(" + json + ')');
    };
})();



	
  	
