﻿// JavaScript Document
function getXhr(){
	var xhr = null; 
	if(window.XMLHttpRequest) // Firefox et autres
	   xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){ // Internet Explorer 
	   try {
				xhr = new ActiveXObject("Msxml2.XMLHTTP"); // Les autres
			} catch (e) {
				xhr = new ActiveXObject("Microsoft.XMLHTTP"); // IE
			}
	}
	else { // XMLHttpRequest non supporté par le navigateur 
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	   xhr = false; 
	} 
	return xhr;
}

function getAjaxdiv(file, div_id, args){
	var element = document.getElementById(div_id)

	ajax_wait(element);	

	var xhr = getXhr();
	// On défini ce qu'on va faire quand on aura la réponse
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			//leselect = xhr.responseText;
			// On se sert de innerHTML pour rajouter les options a la liste
			//alert(''+args+'');
			element.innerHTML = eval('xhr.responseText');
			//setInnerHTML(element,xhr.responseText);
		}
	}

	// Ici on va voir comment faire du post
	//file='include/fonction/ajax_application_workflow.php';
	xhr.open("POST",file,true);
	// ne pas oublier ça pour le post
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("args="+args);
}


function ajax_wait(destination)
{
	destination.innerHTML = "<div style='postion:absolute; margin:0px 0px 0px 0px; padding:0px 0px 0px 0px; width:200px;'><table width='100%' height='100%'><tr><td align='left' valign='middle'>Patientez... <img src='./images/icone_waiting.gif' align='absmiddle' style='margin:0px 0px 0px 0px; padding:0px 0px 0px 0px;' /></td></tr></table></div>";	
}



// setInnerHTML Sécurisé permettant de faire fonctionner le javascript dans un appel en ajax
/*function setInnerHTML(divContent, HTML) {
    divContent.innerHTML=HTML; 
    try {
      var All=divContent.getElementsByTagName("*");
      for (var i=0; i<All.length; i++) {
        All[i].id=All[i].getAttribute("id")
        All[i].name=All[i].getAttribute("name")
        All[i].className=All[i].getAttribute("class")
      }
    } catch (ex) {}
    try {
      var AllScripts=HTML.extractTags("script");
      AllScripts.forEach(function (v) {
        eval(v);
      })
    } catch (ex) {}
    try {
      var AllStyles=HTML.extractTags("style");
      AllStyles.forEach(function (v) {
        var s=document.createStyleSheet()
        s.cssText=v;
        s.enabled=true;
      }, true)
    } catch (ex) {}
}
 
String.prototype.extractTags=function(tag) {
    var matchAll = new RegExp('(?:<'+tag+'.*?>)((\n|\r|.)*?)(?:<\/'+tag+'>)', 'img');
    var matchOne = new RegExp('(?:<'+tag+'.*?>)((\n|\r|.)*?)(?:<\/'+tag+'>)', 'im');
    return (this.match(matchAll) || []).map(function(scriptTag) {
      return (scriptTag.match(matchOne) || ['', ''])[1];
    });
  }
 
Object.prototype.forEach=function(delegate, ownpropertiesonly) {
        if (typeof(delegate)=="function") {
            if (this instanceof Array && typeof(ownpropertiesonly)=="undefined") {
                ownpropertiesonly=true;
            }
            for (key in this) {
                var ok = (!ownpropertiesonly);
                if (!ok) {
                    try {
                        ok=this.hasOwnProperty(key)
                    } catch (ex) {}
                }
                if (ok) {
                    try { delegate(this[key], key, this) } catch(e) {
                        // ...
                    }
                }
            }
        }
        return false;
    }
 
Object.prototype.map=function(iterator) {
    var results = [];
    this.forEach(function(value, index) {
      results.push(iterator(value, index));
    });
    return results;
  }
*/