/**
 * @author Pascal MARTIN
 * 
 * Copyright (c) 2008 Pascal MARTIN
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * TODO Pour choisir la version à utiliser, affecter en bas de ce script 
 * la bonne fonction à la variable "multiply"
 */

var multiply_noAjax = function () {
    var a = parseFloat($F('a'));
    var b = parseFloat($F('b'));
    $('result').value = a*b;
}; // multiply_noAjax



var multiply_ajaxBase = function (url) {
    var a = parseFloat($F('a'));
    var b = parseFloat($F('b'));
    var myAjax = new Ajax.Request(
    url,
    {
      method: 'get',
      parameters: {
        a: a,
        b: b
      },
      onSuccess: function (xhr, jsonValue) {
          if (jsonValue) {
            $('result').value = jsonValue;
          }
        }, // onSuccess
      onException: function (xhr, e) {
          $('msg').innerHTML = e;
        } // onException
    });
}; // multiply_ajaxBase



var multiply_ajaxLocal = function () {
    multiply_ajaxBase('./demo-ajaxdistant-1-server-xhr.php');
}; // multiply_ajaxLocal



var multiply_ajaxDistantErreur = function () {
    multiply_ajaxBase('http://distant.example.com/ajax-distant/demo-ajaxdistant-1-server-xhr.php');
} // multiply_ajaxDistantErreur



var multiply_scriptTag = function () {
    var a = parseFloat($F('a'));
    var b = parseFloat($F('b'));
    
    if ($('requestMultiplier')) {
        // Suppression de la balise si elle existait déjà
        // (pour faire un brin de ménage dans le DOM)
        $('requestMultiplier').remove();
    }
    
    // Création de la nouvelle balise :
    var script = document.createElement('script');
    script.src = 'http://distant.example.com/ajax-distant/demo-ajaxdistant-1-server-script.php?';
    script.src += 'func=resultFunction';
    script.src += '&a=' + a;
    script.src += '&b=' + b;
    
    script.id = 'requestMultiplier';
    script.type = 'text/javascript';
    
    // Et injection dans le DOM :
    document.body.appendChild(script);
}; // multiply_scriptTag

/**
 * Appelée au retour de l'appel "Ajax"
 */
var resultFunction = function (jsonValue) {
    if (jsonValue) {
        $('result').value = jsonValue;
    }
    else {
        $('msg').innerHTML = 'Erreur...';
    }
}; // resultFunction




// TODO Selon la situation, utiliser l'une de ces possibilités :
//var multiply = multiply_noAjax;

//var multiply = multiply_ajaxLocal;
//var multiply = multiply_ajaxDistantErreur;

var multiply = multiply_scriptTag;
