var theoxmlhttp;
function Ajax(){
	
	this.xmlhttp = false;
	this.action = function(){alert('no action define');};
	
	
	this.make = function(){
		/* Compilation conditionnelle d'IE */
		/*@cc_on
		@if (@_jscript_version >= 5)
		 try
		 {
		    theoxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		 }
		 catch (e)
		 {
		    try
		    {
		       theoxmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		    }
		    catch (E)
		    {
		       theoxmlhttp = false;
		    }
		 }
		@else
			theoxmlhttp = false;
		@end @*/
		
		/* on essaie de cr?er l'objet si ce n'est pas d?j? fait */
		if (!theoxmlhttp && typeof XMLHttpRequest != 'undefined')
		{
		 try
		 {
		    theoxmlhttp = new XMLHttpRequest();
		 }
		 catch (e)
		 {
		    theoxmlhttp = false;
		 }
		}
		
		this.xmlhttp = theoxmlhttp;
	}
	
	this.setAction = function(func,params)
	{
		if (theoxmlhttp)
		{
			 theoxmlhttp.onreadystatechange=function(){
				if (theoxmlhttp.readyState == 4) /* 4 : ?tat "complete" */
				{
				   if (theoxmlhttp.status == 200) /* 200 : code HTTP pour OK */
					  if(params == null)
						  func.call(this);
					  else
						  func.call(this,params);
							  
				}
			 }
		}
	}
	
	/**
	* Envoie des donn?es ? l'aide d'XmlHttpRequest?
	* @param string methode d'envoi ['GET'|'POST']
	* @param string url
	* @param string donn?es ? envoyer sous la forme var1=value1&var2=value2...
	*/
	this.sendData = function(method, url, thedata,act,params){
		this.make();
		if(params == 'null')
			this.setAction(act);
		else
			this.setAction(act,params);
			
		if (!this.xmlhttp)
		    return false;
		    
		if(method == "GET")
		{
			if(thedata == 'null')
			    this.xmlhttp.open("GET", url, true); //ouverture asynchrone
			else
			    this.xmlhttp.open("GET", url+"?"+thedata, true);
			this.xmlhttp.send(null);
		}
		else if(method == "POST")
		{
		    this.xmlhttp.open("POST", url, true); //ouverture asynchrone
		    this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			this.xmlhttp.send(thedata);
		}
		return true;
	}
	
	this.sendDataSync = function(method, url, thedata){
		this.make();
	
		if (!this.xmlhttp)
		    return false;
		    
		if(method == "GET")
		{
			if(thedata == 'null')
			    this.xmlhttp.open("GET", url, false); //ouverture asynchrone
			else
			    this.xmlhttp.open("GET", url+"?"+thedata, false);
			this.xmlhttp.send(null);
		}
		else if(method == "POST")
		{
		    this.xmlhttp.open("POST", url, false); //ouverture asynchrone
		    this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			this.xmlhttp.send(thedata);
		}
		//Debug.write(this.xmlhttp.responseText);
		return this.xmlhttp.responseText;
	}
	
	this.sendDataSyncXml = function(method, url, thedata){
		this.make();
			
		if (!this.xmlhttp)
		    return false;
		    
		if(method == "GET")
		{
			if(thedata == 'null')
			    this.xmlhttp.open("GET", url, false); //ouverture asynchrone
			else
			    this.xmlhttp.open("GET", url+"?"+thedata, false);
			this.xmlhttp.send(null);
		}
		else if(method == "POST")
		{
		
		    this.xmlhttp.open("POST", url, false); //ouverture asynchrone
		    this.xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			this.xmlhttp.send(thedata);
		}
		//Debug.write(this.xmlhttp.responseText);
		return new XMLObject(this.xmlhttp.responseXML.documentElement);
	}
	
}

function ajaxChangeContent(ajaxAction,params){
	aj = new Ajax(); 
	params = params == "noparams" ? "" : "&"+params;
	//alert(params);
	data = 'todo=ajaxAction&ajaxActionId='+ajaxAction+params;
	
	reps = aj.sendDataSyncXml(
		'POST',
		'index.php',
		data);
	//Debug.write(reps);
	meth = reps.getValue('methode');
	where = document.getElementById(reps.getValue('id'));
	dom = new Dom();
			
	switch(meth){
		case "ALERT":
			alert(reps.getValue('message'));
			break;
		case "INSERT_CONTENT":
			cont = reps.getValueObj("content");
			dom.removeAllChilds(where);
			for(i=0;i<cont.length;i++){
					dom.addChild(where,cont[i]);
			}
			break;
		case "CHANGE_CONTENT":
			dom.removeAllChilds(where);
		case "ADD_CONTENT":
			var cont = reps.getValueObj("content");
			//Debug.write_r(cont);						
			for(var i=0;i<cont.length;i++){
			
				dom.addChild(where,cont[i]);
			}
			break;
		case "REMOVE_CONTENT":
			dom.removeAllChilds(where);
		case "END":
			return;		
	}
	
}

function ajaxPopup(ajaxAction,params,ajaxTitle,ajaxClosable,ajaxMovable,selectId){
	aj = new Ajax(); 
	params = params == "noparams" ? "" : "&"+params;
	data = 'todo=ajaxAction&ajaxActionId='+ajaxAction+params;
	//alert(data);
	reps = aj.sendDataSyncXml(
		'POST',
		'index.php',
		data);
	cont = reps.getValueObj("content");
	
	pop = new TheoFrame();
	pop.setTitle(ajaxTitle);
	pop.setMovable(ajaxMovable);
	pop.setClosable(ajaxClosable);
	pop.setModal(true);
	pop.setClassName(ajaxAction);
	
	for(i=0;i<cont.length;i++){
		if(cont[i].xml)
			pop.append(cont[i].xml);
		else
			pop.appendChild(cont[i]);
	}

	di = pop.getFrame();
	if(!window.theoFrames)
		window.theoFrames = new Array();
		
	window.theoFrames[ajaxAction] = pop;
	
	isForm = di.getElementsByTagName("form");
	if(isForm.length != 0){
		if(selectId != null){
			sel = document.createElement("input");
			sel.type = "hidden";
			sel.value = selectId;
			sel.name = "insertSelectId";
			isForm[0].appendChild(sel);	
		}
		isForm[0].appendChild(document.createElement("br"));	
		ok = document.createElement("input");
		ok.type = "button";
		ok.value = "Valider";
		ok.name = pop.getId();
		ok.onclick = function(){
			ajaxForm(this.parentNode);	
			document.body.removeChild(document.getElementById(this.name));
			document.body.removeChild(document.getElementById("thedivm"+this.name));
		}
		cancel = document.createElement("input");
		cancel.type = "button";
		cancel.value = "Annuler";
		cancel.name = pop.getId();
		cancel.onclick = function(){
			document.body.removeChild(document.getElementById(this.name));
			document.body.removeChild(document.getElementById("thedivm"+this.name));
		}
		isForm[0].appendChild(ok);
		isForm[0].appendChild(cancel);
	}
	else{
		ok = document.createElement("input");
		ok.type = "button";
		ok.value = "Valider";
		ok.name = pop.getId();
		ok.onclick = function(){
			document.body.removeChild(document.getElementById(this.name));
			document.body.removeChild(document.getElementById("thedivm"+this.name));
		}
		di.appendChild(ok);
	}
	pop.display();
}

function getDoc(ifr){
		if (ifr.contentDocument)
    		return ifr.contentDocument; 
  		if (ifr.contentWindow)
    		return ifr.contentWindow.document;
  		if (ifr.document)
			return ifr.document;
	}

function ajaxEncode(leHTML){
	var tab = new Array();
	tab.push(233); //?
	tab.push(231); //?
	tab.push(232); //?
	tab.push(234); //?
	tab.push(238); //?
	tab.push(339); //oe
	tab.push(156); //oe
	tab.push(239); //?		
	tab.push(224); //?
	tab.push(226); //?
	tab.push(244); //?
	tab.push(249); //?
	tab.push(36); //$
	tab.push(39); //'
	tab.push(176); //?
//	tab.push(8217); //'
	tab.push(160); // (espace)
	tab.push(64); // @
	tab.push(8364); //?
	tab.push(33); //   !
	tab.push(34); //   !

	
	for(var i=0;i<tab.length;i++){
		leHTML = leHTML.replace(new RegExp(String.fromCharCode(tab[i]),"g"),"__"+tab[i]+"__");
	}
	return leHTML;
}

function ajaxForm(form){
	buffer = "";
	var fcks = document.getElementsByName("TheoFCK");
	for(i=0;i<fcks.length;i++){
		var name = fcks[i].id.substring(fcks[i].id.indexOf("theo")+4);
		var thefr = document.getElementById(name+"___Frame");
		var thedoc = getDoc(thefr);
		var fckcontainer = thedoc.getElementsByTagName("iframe")[0];
		buffer += "&"+fcks[i].id+"="+ajaxEncode(getDoc(fckcontainer).body.innerHTML);
	}
	elems = form.getElementsByTagName("input");	
	for(i=0;i<elems.length;i++){
		field = elems[i];
		if((field.type == "text" || field.type == "password" || 
			field.type == "hidden" || field.type == "file")
			&&  (trim(field.value).length > 0) )
			buffer += "&"+field.name+"="+field.value;
		if((field.type == "radio" || field.type == "checkbox") && field.checked)
			buffer += "&"+field.name+"="+field.value;
	}
	elems = form.getElementsByTagName("textarea");	
	for(i=0;i<elems.length;i++){
		field = elems[i];
		if(trim(field.value).length > 0)
			buffer += "&"+field.name+"="+field.value;
	}
	elems = form.getElementsByTagName("select");	
	for(i=0;i<elems.length;i++){
		field = elems[i];
		for (j = 0; j < field.options.length; j++)
    		if (field.options[j].selected)
				buffer += "&"+field.name+"="+field.options[j].value;
	}
	ajaxActionHidden = form.action;
	aj = new Ajax(); 
	params = buffer == "" ? "" : buffer;
	data = 'todo=ajaxAction&ajaxActionId='+ajaxActionHidden+buffer;
	reps = aj.sendDataSyncXml(
		'POST',
		'index.php',
		data);
	
	meth = reps.getValue('methode');
	dom = new Dom();
	//Debug.write("methode => "+meth);
	switch(meth){
		case "ALERT":
			alert(reps.getValue('message'));
			break;
		case "POPUP":
			cont = reps.getValueObj('content');
			pop = new TheoFrame();
			pop.setTitle(reps.getValue('title'));
			pop.setMovable(reps.getValue('movable'));
			pop.setModal(true);
			for(i=0;i<cont.length;i++)
				pop.append(cont[i].xml);
			pop.display();
			break;
		case "SELECT":
			selid = reps.getValue('selectId');
			val   = reps.getValue('value');
			text  = reps.getValue('text');
			thesel = document.getElementById(selid);
			opts = thesel.options;
			
			newops = new Array();
			
			var i=1;
			do{
				op = opts[i];
				i++;
			}while(i <= opts.length && op.text.toLowerCase()<text.toLowerCase());
			theopt = document.createElement("option");
			theopt.value = val;
			theopt.text = text;
			theopt.selected = true;
			
			var temp = new Array();
			for(j=i-1;j<opts.length;j++){
				temp.push(opts[j].cloneNode(true));
			}
			opts[i-1]=theopt;
			for(j=0;j<temp.length;j++){
				opts[i+j]=temp[j];
			}
			
			//Debug.write("to add in "+selid+" <option value='"+val+"'>"+text+"</option>");
			break;
		case "REDIRECT":
			u = document.URL;
			if(u.indexOf(reps.getValue('path')) != -1)
				window.location.reload();
			window.location.href = reps.getValue('path');
			break;
		case "INSERT_CONTENT":
			cont = reps.getValueObj("content");
			where = document.getElementById(reps.getValue('id'));
			dom.removeAllChilds(where);
			
			for(i=0;i<cont.length;i++){
					dom.addChild(where,cont[i]);
			}
			break;
		case "CHANGE_CONTENT":
			where = document.getElementById(reps.getValue('id'));
			dom.removeAllChilds(where);
		case "ADD_CONTENT":
			cont = reps.getValueObj("content");
			where = document.getElementById(reps.getValue('id'));			
			for(i=0;i<cont.length;i++){
					dom.addChild(where,cont[i]);
			}
			break;
		case "REMOVE_CONTENT":
			where = document.getElementById(reps.getValue('id'));
			dom.removeAllChilds(where);
		case "END":
			break;
	}	
}