function AjaxCall(url, parameters, container, win, theForm) {
    new Ajax_DefaultUpdater(container, url, parameters, win, theForm);
}

function AjaxCallDevise(url, parameters_string, container, win, theForm) {

    if ( typeof withAlphaDevies != 'undefined' && withAlphaDevies != 'undefined' && withAlphaDevies ) {

        if ( $('once').value == '1' ) { return false; }
        $('once').value = '1';
        changerAlpha('attente',0.2);

        if ($('attente2')) {
            $('attente2').style.display = 'block';
            changerAlpha('attente2',0.6);
        }
    }

    var parameters = parameters_string.toQueryParams();

    if ( traceActive ) {
	    trace('parameters: '+parameters_string);
    }

    new Ajax_DefaultUpdater(container, url, parameters, win, theForm);
}

function AjaxCallS(url, parameters_string, container, win, theForm, withoutAlpha) {

    withAlpha = false;

    if ( typeof withoutAlpha == 'undefined' || withoutAlpha != 0 ) {
        withAlpha = true;
    }

    if (withAlpha) {

        if ( $('once').value == '1' ) { return false; }
        $('once').value = '1';
        changerAlpha('attente',0.2);

        if ($('attente2')) {
            $('attente2').style.display = 'block';
		    changerAlpha('attente2',0.6);
        }
    }

    var parameters = parameters_string.toQueryParams();

    if ( traceActive ) {
        trace('parameters: '+parameters_string);
    }

    new Ajax_DefaultUpdater(container, url, parameters, win, theForm);
}

var Ajax_DefaultUpdater = Class.create(Ajax.Request, {

    initialize: function($super, container, url, parameters, win, theForm) {

        if ( !win ) {
            this.win = window;
        } else {
            this.win = win;
        }

        if ( theForm != undefined && theForm != null ) {
            this.theForm = theForm;
        }
        else {
            this.theForm = null;
        }

        this.container = container;
        this.transport = Ajax.getTransport();

        options = { parameters: parameters, onComplete: this.updateContent.bind(this)};

        $super(url, options);
    },

    respond2error: function() {
        if (traceActive) {
            trace('43:Error on server call, server might be down, status code=' + this.transport.status);
        }
    },

    updateContent: function() {

        try {
            if (traceActive) {
                trace('52:Status: '+this.transport.status);
            }

            if ( this.success() ) {
                if ( this.theForm != undefined && this.theForm != null ) {
                    Form.enable(this.theForm);
                    this.theForm = null;
                }

                if (traceActive) {
                    trace('63:' + this.transport.responseText.length);
                    trace('63:' + this.transport.responseText.escapeHTML());
                }

                if ( window.ActiveXObject ) {

                    try {
                        var doc = new ActiveXObject('Microsoft.XMLDOM');
                        doc.async = 'false';
                        var a = this.transport.responseText;

                        var b = '';
                        aa = a.split('<!--');
                        b = aa[0];

                        for ( i = 1 ; i < aa.length ; i++ ) {
                            b += '<!--';
                            aaa = aa[i].split('-->');
                            b += aaa[0].replace(/[^-_a-zA-Z0-9"'(),\.<>:=;& \/{}\*\[\]+\?|\\#@%!\$]*/g,'');
                            b += '-->';
                            b += aaa[1];
                        }

                        var hasParse = doc.loadXML(b);

                        if (!hasParse) {
                            if (traceActive) {
                                trace('91:'+b.escapeHTML());
                                trace('92:Parsing: Invalid XML configuration..');
                            }

                            return;
                        }
                    }
                    catch (e) {
                        b = b.replace(/</g,'&lt;');
                        b = b.replace(/>/g,'&gt;');

                        if (traceActive) {
                            trace('102:'+b);
                            trace('103:Exception: Invalid XML configuration..');
                        }

                        return;
                    }

                    var xml = doc.xml;

/*D
                    if (traceActive) {
                        trace(doc.getElementsByTagName('action').length);
                        trace(xml);
                    }
D*/

                    var actions = doc.getElementsByTagName('action');

                    for (var i = 0; i < actions.length; i++) {

                        node = actions.item(i);
                        var value = ' ' + node.getElementsByTagName('content').item(0).firstChild.data;
                        var src = $(node.getAttribute('blockId'));

                        if (src) {
                            if ( node.getAttribute('replace') == null || (node.getAttribute('replace') != null && node.getAttribute('replace') == 'true') ) {
                                src.innerHTML = value;
                            }

                            var ss = src.getElementsByTagName('script');

                            for (var j = 0; j < ss.length ; j++) {
                                if (traceActive) {
                                    trace('130:eval[js]:'+ss.item(j).text);
                                }

                                eval(ss.item(j).text);
                            }

                            if ( node.getAttribute('cssBlock') != null && node.getAttribute('cssBlock') != '' ) {
                                $(node.getAttribute('blockId')).className = node.getAttribute('cssBlock');
                            }

                            if ( value.strip() == '' ) {
                                if (traceActive) {
                                    trace('none:'+node.getAttribute('blockId'));
                                }

                                src.style.display = 'none';
                            }
                            else {
                                if (traceActive) {
                                    trace('block:'+node.getAttribute('blockId'));
                                }

                                src.style.display = 'block';
                            }

                            if ( node.getAttribute('display') != null && node.getAttribute('display') != '' ) {
                                $(node.getAttribute('blockId')).style.display = node.getAttribute('display');
                            }
                        }

                    }

                    var scrs = doc.getElementsByTagName('script');

                    for (var i = 0; i < scrs.length; i++) {

                        try {
                            node = scrs.item(i);
                            var value = '' + node.getElementsByTagName('content').item(0).firstChild.data;

                            if (traceActive) {
                                trace('252:eval[js]:' + value);
                            }

                            this.win.eval(value);
                        }
                        catch (e) {
                            throw e;
                        }
                    }
                }
                else {
                    if (traceActive) {
                        trace('177:'+this.transport.responseXML);
                    }

                    var actions = this.transport.responseXML.getElementsByTagName('action');

                    for (var i = 0; i < actions.length; i++) {

                        node = actions.item(i);

                        var value = ' ' + node.getElementsByTagName('content').item(0).firstChild.data;

                        var src = $(node.getAttribute('blockId'));

                        if (src) {

                            if ( node.getAttribute('replace') == null || (node.getAttribute('replace') != null && node.getAttribute('replace') == 'true') ) {
                                src.innerHTML = value;
                            }

                            var ss = src.getElementsByTagName('script');

                            if (traceActive) {
                                trace('192:ss.length:'+ss.length);
                            }

                            for (var j = 0; j < ss.length ; j++) {
                                if ( navigator.userAgent.indexOf('AppleWebKit/') > -1 ) {

                                    if (traceActive) {
                                        trace('198:eval[js]:'+ss.item(j).innerHTML);
                                    }

                                    eval(ss.item(j).innerHTML);
                                }
                                else {
                                    if (traceActive) {
                                        trace('205:eval[js]:'+ss.item(j).text);
                                    }

                                    eval(ss.item(j).text);
                                }
                            }

                            if (node.getAttribute('cssBlock') != null && node.getAttribute('cssBlock') != '' ) {
                                $(node.getAttribute('blockId')).className = node.getAttribute('cssBlock');
                            }

                            if ( value.strip() == '' ) {
/*D
                                if (traceActive) {
                                    trace('none:'+node.getAttribute('blockId'));
                                }
D*/
                                src.style.display = 'none';
                            } else {
/*D
                                if (traceActive) {
                                    trace('block:'+node.getAttribute('blockId'));
                                }
D*/
                                src.style.display = 'block';
                            }

                            if ( node.getAttribute('display') != null && node.getAttribute('display') != '' ) {
                                $(node.getAttribute('blockId')).style.display = node.getAttribute('display');
                            }
                        }
                    }

                    var scrs = this.transport.responseXML.getElementsByTagName('script');

                    for (var i = 0; i < scrs.length; i++) {

                        try {
                            node = scrs.item(i);
                            var value = '' + node.getElementsByTagName('content').item(0).firstChild.data;
/*D*/
                            if (traceActive) {
                                trace('228:eval[js]:'+value);
                            }
/*D*/
                            this.win.eval(value);
                        }
                        catch (e) {
                            throw e;
                        }
                    }
                }
            }
            else {
/*D*/
                if (traceActive) {
                    trace('242:Une erreur est survenue');
                }
/*D*/
            }
        }
        catch (e) {
/*D*/
            if (traceActive) {
                trace('249:'+this.transport.responseText.escapeHTML());
                trace('250:ajax.js:updateContent():Error updating content ' + e);
            }
/*D*/
        }

        if ($('attente')) {
            changerAlpha('attente',1);
        }

        if ($('attente2')) {
            $('attente2').style.display='none';
        }

        if ($('once')) {
            $('once').value = '0';
        }

    }
});

function submitForm(id, container) {

    changerAlpha('attente',0.2);

    if ($('attente2')) {
        $('attente2').style.display = 'block';
        changerAlpha('attente2',0.6);
    }

/*D
    if (traceActive) {
        trace($(id));
    }
D*/

    var params = $(id).serialize(true);
    var url = $(id).readAttribute('action');

    AjaxCall(url, params, container);

    return false;
}

function submitMoteurIFrame(nomId, valueRemplace, table) {
    if ($('divCompletionIframe')) {
        $('divCompletionIframe').innerHTML = '';
    }

    document.forms.moteurLibre.produitChoixMoteurLibre.value = valueRemplace;
    document.forms.moteurLibre.produitChoixTable.value = table;
    document.forms.moteurLibre.saveWord.value = false;
    url = '/scripts/actions/rechercheMoteurLibreTrouver.php';
    if (project_bottle != 'undefined' && project_bottle == 'candry' && islabel != 'undefined' && islabel)
    {
      url = '/' + code_mrq + url;
    }
    document.forms.moteurLibre.action = url;
    return submitMoteur($(nomId), '');

}

function submitMoteur(id, container) {

	if (project_bottle != 'undefined' && project_bottle == 'candry')
	{
		submitMoteurCandry(id, container);
	}
	else
	{
		submitMoteurPvhd(id, container);
	}
}

function submitMoteurPvhd(id, container) {

	if ( !$('criteres') ||
		 !$('pagination_haut') ||
		 !$('pagination_bas') ||
		 !$('resultats') ) {
		return false;
	}

	if ( $('once').value == '1' ) { return false; }
	$('once').value = '1';
	changerAlpha('attente',0.2);

	if ($('attente2')) {
        $('attente2').style.display = 'block';
		changerAlpha('attente2',0.6);
	}

/*D
    if (traceActive) {
        trace('ici submitMoteur :'+id);
    }
D*/
    var params = id.serialize(true);
/*D
    if (traceActive) {
        trace('params:'+params);
    }
D*/
    var produits = '';
    var opts = $('produitCtrl');

    for (var i = 1; i < opts.length; i++) {
        produits += opts[i].value;
        if ( i != opts.length-1 ) { produits += '#'; }
    }

    params['produitChoix'] = produits;

    var url = id.readAttribute('action');

    AjaxCall(url, params, container);

    return false;
}

function remplirProduitChoixMoteurNoAjax(id, container) {

    var produits = '';
    var opts = $('produitCtrl');
    for (var i = 1; i < opts.length; i++) {
        produits += opts[i].value;
        if ( i != opts.length-1 ) {
            produits += '#';
        }
    }

    $('produitChoix').value = produits;

    return true;
}

function addParams(params) {
  if ( typeof code_mrq != 'undefined' ) { if ( params != '' ) { params += '&'; } params += 'code_mrq=' + code_mrq; }
  if ( typeof id_mrq != 'undefined' ) { if ( params != '' ) { params += '&'; } params += 'id_mrq=' + id_mrq; }
  if ( typeof ext_lang != 'undefined' ) { if ( params != '' ) { params += '&'; } params += 'ext_lang=' + ext_lang; }
  if ( typeof id_lang != 'undefined' ) { if ( params != '' ) { params += '&'; } params += 'id_lang=' + id_lang; }
  return params;
}

function pvhdAjaxCall(code,params) {

	if ( code == 'ge' ) {
		/* 'id_reg_pv=3' */
		url = '/scripts/actions/Geographique.php';
		params = addParams(params);
		AjaxCallS(url,params);
	}
	else if ( code == 'th' ) {
		/* 'id_thm=3' */
		url = '/scripts/actions/Thematique.php';
		params = addParams(params);
		AjaxCallS(url,params);
	}
	else if ( code == 'os' ) {
		/* '=3' */
		url = '/scripts/actions/Os.php';
		params = addParams(params);
		AjaxCallS(url,params);
	}
	else if ( code == 'multi' ) {
		/* aplusdecrit=yes */
		/* dateDepart=31/05/2007 */
		/* flex */
		/* duree */
		/* produitChoix */
		/* prix = 0#200 */
		url = '/scripts/actions/rechercheMoteur.php';
		params = addParams(params);
		AjaxCallS(url,params);
	}
	else {
		url = '/scripts/actions/index_reference.php';
		params = addParams(params);
		AjaxCallS(url,params);
	}

}

function submitMoteurCandry(id, container) {

	if ( $('once').value == '1' ) { return false; }
	$('once').value = '1';
	//changerAlpha('attente',0.2);

	/*if ($('attente2')) {
        $('attente2').style.display = 'block';
		changerAlpha('attente2',0.6);
	}*/

/*D
    if (traceActive) {
        trace('ici submitMoteur :'+id);
    }
D*/
    var params = id.serialize(true);
/*D
    if (traceActive) {
        trace('params:'+params);
    }
D*/
    var produits = '';
    var opts = $('produitCtrl');

    for (var i = 1; i < opts.length; i++) {
        produits += opts[i].value;
        if ( i != opts.length-1 ) { produits += '#'; }
    }

    params['produitChoix'] = produits;

    var url = id.readAttribute('action');

    AjaxCall(url, params, container);

    return false;
}
