var http       = getHTTPObject();

function handleHttpResponse(http,myid) {

	if (http.readyState == 4) {

	  if (http.responseText != null) {
		  if (http.responseText.indexOf('invalid') == -1) {
				results = http.responseText;
				document.getElementById(myid).innerHTML = results;
				isWorking = false;
			}
	  } else {
			document.getElementById(myid).innerHTML = '';
			isWorking = false;
		}

  } else {
	  isWorking = true;
	}

}

function linkme(link_url,myid) {

  var isWorking  = false;
	if (!isWorking && http) {
		var outdata = myid;  
		http.open("POST", link_url , true);
		http.onreadystatechange = function () { handleHttpResponse(http,myid); }
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.send(outdata);
	
		if (link_url != '') {
			//	debug('link me BEFORE ' + link_url + ' > ' + actiontype);
			//	dhtmlHistory.add(link_url + '|' + outdata);
			//	debug('link me AFTER ' + link_url + ' > ' + actiontype);
		}
		isWorking = true;
	}
}

function selectme(name,myid,alertxt) {

	var tp = document.getElementById(name).value.split(';;');

	document.getElementById(name).selectedIndex = 0;

	if (tp[0] == 1) {
		var alertme =  confirmdelete(alertxt);
	} else {
		var alertme = true;	
	}
	
	if (alertme == true) {
		var link_url = 	tp[1];
			if (link_url != '') {
   		 var isWorking  = false;
		   if (!isWorking && http) {
				var outdata = myid;  
				http.open("POST", link_url , true);
				http.onreadystatechange = function () { handleHttpResponse(http,myid); }
				http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				http.send(outdata);
				isWorking = true;
		 }
   }
 }
}

function submitproceed(link,form_name,myid) {

	var params = treatpostinput(form_name);
	alert(params);
	new Ajax.Request(link,{
			method: 'post',
			postBody : params,
			onComplete : function (xhr) {
								 	   if (xhr.status == 200) {
												$(myid).innerHTML = xhr.responseText;
										 } else {
											  $(myid).innerHTML = xhr.status;
										}
			}
	});
	alert($(myid).innerHTML);
}

function treatpostinput(form_name) {
				 
	var ret = '';  

	for (i=0; i<document.forms[form_name].elements.length; i++) {
		
		  // CheckBox Issues
			if (document.forms[form_name].elements[i].type == 'checkbox') {
				 if (document.forms[form_name].elements[i].checked == true) {
			       document.forms[form_name].elements[i].value = "on";
				 } else {
					   document.forms[form_name].elements[i].value = "";
				 }
			}
			// ending patch
		  
			retstr = document.forms[form_name].elements[i].value;
			if (retstr.match('&')) {
          alert('& IS A FORBIDDEN CHARACTER');
				  exit;
			} else {
					if (i == (document.forms[form_name].elements.length-1)) { var et = '';  } else { et = '&';  }
			    ret += document.forms[form_name].elements[i].name+'='+document.forms[form_name].elements[i].value+et;
			}
  }

	return ret;

}


function confirmdelete(msg) {
	var agree=confirm(msg);
	if (agree)
	return true ;
	else
	return false ;
}


// SetInnerHTML Securise
 
    // Renvoie le texte de l'objet ActiveXObject le plus recent depuis une liste
    var pickRecentProgID = function (idList){
	    // found progID flag
        var bFound = false;
        for(var i=0; i < idList.length && !bFound; i++){
            try{
                var oDoc = new ActiveXObject(idList[i]);
                o2Store = idList[i];
                bFound = true;
            }catch (objException){
                // trap; try next progID
            };
        };
        if (!bFound)
		    throw ("Aucun ActiveXObject n'est valide sur votre ordinateur, pensez a mettre a jour votre navigateur");
        idList = null;
        return o2Store;
    }
 
    // Retourne un nouvel objet XmlHttpRequest
    var GetXmlHttpRequest_AXO=null
    var GetXmlHttpRequest=function () {
	    if (window.XMLHttpRequest) {
		    return new XMLHttpRequest()
	    }
	    else if (window.ActiveXObject) {
		    if (!GetXmlHttpRequest_AXO) {
			    GetXmlHttpRequest_AXO=pickRecentProgID(["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]);
		    }
		    return new ActiveXObject(GetXmlHttpRequest_AXO)
	    }
	    return false;
    }

// setInnerHTML Securise
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;
}

 
function getFileContent(url) {

       var Xhr=GetXmlHttpRequest();
       Xhr.open("GET",url,false);
       Xhr.send(null);
       return Xhr.responseText;

}

function showModule(module,myscope) {
 new Effect.Appear($(module), { duration: 0.7, from:0.0, to:1.0 , queue: { position:'front', scope: myscope, limit:2}});
}

function hideModule(module,myscope) { 
 new Effect.Fade($(module), { duration: 0.7, from:0.9, to:0.0 , queue: { position:'end', scope: myscope, limit:2} });
}


function displayText(text,target,mytime) {
  $(target).innerHTML = text;
	new Effect.Appear($(target), { duration: mytime, from:0.0, to:1.0 , queue: { position:'front', scope: 'textscope', limit:2}});
	new Effect.Fade($(target),   { duration: mytime, from:0.9, to:0.0 , queue: { position:'end', scope: 'textscope', limit:2}});

}
