//se la cartella contenente lo script cambia nome cambiare anche la url dell'img loading.gif
//esempio <a href="#" onclick="ajax_getz('zzz.htm','risultato')">qui</a>
// 
function ajax_get_url(urlz,dest_div,modo,effect_fade,startcolor,endcolor) {
	if (modo == null){
	   modo = "GET";
	 }
	 if (effect_fade == null){
	   effect_fade = "0"; ///disattivato di default, manda la cpu al 100%
	 } 
	if (startcolor == null){
	   startcolor = "#F2A057";
	 } 
	if (endcolor == null){
	   endcolor = "#FFFFFF";
	 } 	 
	 
	var myConn = new XHConn();	
	if (!myConn) alert("XMLHTTP non rilevato! ATTENZIONE, questo sito usa nuove tecnologie non supportate dal tuo browser, per accedervi è necessario aggiornare il browser!");
	

	//loading nel div
	with (document) if (getElementById && ((obj=getElementById(dest_div))!=null))
   with (obj) innerHTML = "<div style=\"position:relative;top:10%;text-align:center;\">loading<br><img src=\"js_ajax/loading.gif\"></div>";

	var fnWhenDone = function (oXML) { 
		//alert(oXML.responseText);  
		
		
	if (effect_fade=="1") {
		//effetto fade del div, si può levare se si vuole
		//Fat.fade_element(target ID, frames per second, fade duration, fade color, final color)
		Fat.fade_element(dest_div, 10, 1000, startcolor, endcolor);
	}		
		with (document) if (getElementById && ((obj=getElementById(dest_div))!=null))
		with (obj) innerHTML = unescape(oXML.responseText);	

		};	
		var dummy ="";
		var url_check = urlz.indexOf("dummy="); //se ne la url non c'è già il mio dummy lo aggiungo
		if ( url_check == -1 ) { 
   			//trichitrucco per evitare la cache
			var dummy = "dummy=" + new Date().getTime()
		} 

		
		//var dummy = "dummy=" + new Date().getTime();
		//alert(urlz);
		//alert(dummy);
		//dummy=""; //fix cache disabilitato
		myConn.connect(urlz, modo, dummy, fnWhenDone);
}
//Solo per form POST
//esempio <form  id="mioform" name="mioform" method="post" onsubmit="javascript:ajax_post_form('mioform','app.asp?pro=zzzzz','contenuto_centro');return false;">
function ajax_post_form(form_name,urlz,dest_div,effect_fade,startcolor,endcolor) {
	 if (effect_fade == null){
	   effect_fade = "1";
	 } 
	if (startcolor == null){
	   startcolor = "#F2A057";
	 } 
	if (endcolor == null){
	   endcolor = "#FFFFFF";
	 } 	 
	 
	var myConn = new XHConn();	
	if (!myConn) alert("XMLHTTP non rilevato! ATTENZIONE, questo sito usa nuove tecnologie non supportate dal tuo browser, per accedervi è necessario aggiornare il browser!");
	
	if (effect_fade=="1") {
	//effetto fade del div, si può levare se si vuole
	//Fat.fade_element(target ID, frames per second, fade duration, fade color, final color)
	Fat.fade_element(dest_div, 10, 1000, startcolor, endcolor);
	}
	//loading nel div
	with (document) if (getElementById && ((obj=getElementById(dest_div))!=null))
    with (obj) innerHTML = "<center>loading<br><img src=\"js_ajax/loading.gif\"></center>";

	var fnWhenDone = function (oXML) { 
	//alert(oXML.responseText);  
	with (document) if (getElementById && ((obj=getElementById(dest_div))!=null))
    with (obj) innerHTML = unescape(oXML.responseText);	
	};	
	
	var serializer = new Form.Serializer(form_name);
    var qs = serializer.queryString('&');
	myConn.connect(urlz, "POST", qs, fnWhenDone);
}

/////


////vera funzione ajax

/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        if (sVars) {
		//mio
		var sep = "?";
		var url_check = sURL.indexOf("&"); 
		if ( url_check != -1 ) { 
   			sep = "&";
		} 
		//end mio
			xmlhttp.open(sMethod, sURL+sep+sVars, true); 
		} else {
			xmlhttp.open(sMethod, sURL, true); 
		}
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}


// @name      The Fade Anything Technique
// @namespace http://www.axentric.com/aside/fat/
// @version   1.0-RC1
// @author    Adam Michela

var Fat = {
	make_hex : function (r,g,b) 
	{
		r = r.toString(16); if (r.length == 1) r = '0' + r;
		g = g.toString(16); if (g.length == 1) g = '0' + g;
		b = b.toString(16); if (b.length == 1) b = '0' + b;
		return "#" + r + g + b;
	},
	fade_all : function ()
	{
		var a = document.getElementsByTagName("*");
		for (var i = 0; i < a.length; i++) 
		{
			var o = a[i];
			var r = /fade-?(\w{3,6})?/.exec(o.className);
			if (r)
			{
				if (!r[1]) r[1] = "";
				if (o.id) Fat.fade_element(o.id,null,null,"#"+r[1]);
			}
		}
	},
	fade_element : function (id, fps, duration, from, to) 
	{
		if (!fps) fps = 30;
		if (!duration) duration = 3000;
		if (!from || from=="#") from = "#FFFF33";
		if (!to) to = this.get_bgcolor(id);
		
		var frames = Math.round(fps * (duration / 1000));
		var interval = duration / frames;
		var delay = interval;
		var frame = 0;
		
		if (from.length < 7) from += from.substr(1,3);
		if (to.length < 7) to += to.substr(1,3);
		
		var rf = parseInt(from.substr(1,2),16);
		var gf = parseInt(from.substr(3,2),16);
		var bf = parseInt(from.substr(5,2),16);
		var rt = parseInt(to.substr(1,2),16);
		var gt = parseInt(to.substr(3,2),16);
		var bt = parseInt(to.substr(5,2),16);
		
		var r,g,b,h;
		while (frame < frames)
		{
			r = Math.floor(rf * ((frames-frame)/frames) + rt * (frame/frames));
			g = Math.floor(gf * ((frames-frame)/frames) + gt * (frame/frames));
			b = Math.floor(bf * ((frames-frame)/frames) + bt * (frame/frames));
			h = this.make_hex(r,g,b);
		
			setTimeout("Fat.set_bgcolor('"+id+"','"+h+"')", delay);

			frame++;
			delay = interval * frame; 
		}
		setTimeout("Fat.set_bgcolor('"+id+"','"+to+"')", delay);
	},
	set_bgcolor : function (id, c)
	{
		var o = document.getElementById(id);
		o.style.backgroundColor = c;
	},
	get_bgcolor : function (id)
	{
		var o = document.getElementById(id);
		while(o)
		{
			var c;
			if (window.getComputedStyle) c = window.getComputedStyle(o,null).getPropertyValue("background-color");
			if (o.currentStyle) c = o.currentStyle.backgroundColor;
			if ((c != "" && c != "transparent") || o.tagName == "BODY") { break; }
			o = o.parentNode;
		}
		if (c == undefined || c == "" || c == "transparent") c = "#FFFFFF";
		var rgb = c.match(/rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/);
		if (rgb) c = this.make_hex(parseInt(rgb[1]),parseInt(rgb[2]),parseInt(rgb[3]));
		return c;
	}
}



//form serializer
//http://openjsan.org/doc/a/au/autarch/Form/Serializer/0.14/lib/Form/Serializer.html

if ( typeof Form == "undefined" ) {
    Form = {};
}

Form.Serializer = function (name) {
    return this._initialize(name);
};

Form.Serializer.VERSION = "0.14";

Form.Serializer.ElementTypes = [ "input", "textarea", "select" ];

Form.Serializer.prototype._initialize = function (form) {
    if ( typeof form == "object" ) {
        this.form = form;
        return;
    }

    this.form = document.getElementById(form);

    if ( ! this.form ) {
        for ( var i = 0; i < document.forms.length; i++ ) {
            if ( document.forms[i].name == form ) {
                this.form = document.forms[i];
                break;
            }
        }
    } 

    if ( ! this.form ) {
        throw new Error( "Cannot find a form with the name or id '" + name + "'" );
    }
};

Form.Serializer.prototype.pairsArray = function () {
    var pairs = new Array;

    for ( var i = 0; i < Form.Serializer.ElementTypes.length; i++ ) {
        var type = Form.Serializer.ElementTypes[i];
        var elements = this.form.getElementsByTagName(type);

        for ( var j = 0; j < elements.length; j++ ) {

            var p = eval( "this._serialize_" + type + "(elements[j])" );

            if (p) {
                for ( var k = 0; k < p.length; k++ ) {
                    pairs.push( p[k] );
                }
            }
        }
    }

    return pairs;
}

Form.Serializer.prototype._serialize_input = function (elt) {
    switch (elt.type.toLowerCase()) {
      case "hidden":
      case "password":
      case "text":
          return this._simple(elt);

      case "checkbox":  
      case "radio":
          return this._simple_if_checked(elt);

      default:
          return false;
    }
}

Form.Serializer.prototype._simple = function (elt) {
    return [ [ elt.name, elt.value ] ];
}

Form.Serializer.prototype._simple_if_checked = function (elt) {
    if ( ! elt.checked ) {
        return;
    }

    return this._simple(elt);
}

Form.Serializer.prototype._serialize_textarea = function (elt) {
    return this._simple(elt);
}

Form.Serializer.prototype._serialize_select = function (elt) {
    var options = elt.options;

    var serialized = new Array;
    for ( var i = 0; i < options.length; i++ ) {
        if ( options[i].selected ) {
            serialized.push( [ elt.name, options[i].value ] );
        }
    }
        
    return serialized;
}

Form.Serializer.prototype.queryString = function () {
    var pairs = this.pairsArray();

    var queryPairs = new Array;
    for ( var i = 0; i < pairs.length; i++ ) {
        queryPairs.push(   encodeURIComponent( pairs[i][0] )
                         + "=" 
                         + encodeURIComponent( pairs[i][1] ) );
    }

    var sep = arguments.length ? arguments[0] : ";";
    return queryPairs.join(sep);
}

Form.Serializer.prototype.keyValues = function (forceArray) {
    var pairs = this.pairsArray();

    var named = {};
    for ( var i = 0; i < pairs.length; i++ ) {
        var k = pairs[i][0];
        var v = pairs[i][1];

        if ( named[k] ) {
            if ( typeof named[k] == 'object' ) {
                named[k].push(v);
            }
            else {
                named[k] = [ named[k], v ];
            }
        }
        else {
            if (forceArray) {
                named[k] = [v];
            }
            else {
                named[k] = v;
            }
        }
    }

    return named;
}

/*

*/


