function autoCompleteTag(sType,sInput,sDestination, iLimit, sAction ) {

	var oInput = document.getElementById(sInput);
	var iLastPos = oInput.value.lastIndexOf(",");
	var sSearch = oInput.value.substring(iLastPos);
	
	if (sAction==undefined || sAction == "") {
		sAction = "replacevalueToInput";
	}
	
	if (sSearch.length >3) {
		Updater( "../ajax/ajax_autocomplete.ajax.php", sDestination,"type="+sType+"&search="+sSearch+"&limit="+iLimit+"&destination="+sInput+"&action="+sAction);
	}
	
}

function addvalueToInput(sValue, sDestination) {
	var oInput = document.getElementById(sDestination);
	oInput.value += sValue;
}
function replacevalueToInput(sValue, sDestination) {
	var oInput = document.getElementById(sDestination);
	oInput.value = sValue;
}
function setInnerHtml(sValue,sDestination) {
	var oElement = document.getElementById(sDestination);
	if (oElement != undefined) {
		oElement.innerHTML = sValue;
	}
	else {
		alert("Erreur id rechercher non trouvé :"+sDestination);
	}
}

/* ------------------------------   AJAX --------------------------------------- */

function GetXmlHttpObject()
{ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest)
		objXMLHttp=new XMLHttpRequest();
	else if (window.ActiveXObject)
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	return objXMLHttp;
} 


function Updater( sUrl, sDiv, sParam)
{
	
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert("Votre navigateur ne supporte pas le javascript.");
		return false;
	} 
	var url=sUrl;

	xmlHttp.onreadystatechange = function (){
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
		{
			
			setInnerHtml(xmlHttp.responseText,sDiv);
		}
	}
	
	var params = sParam;
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	xmlHttp.send(params);
	//xmlHttp = "";
	
	return true;
	
} 

function getDataFromForm(sForm) {
	
	var data="";
	var key=0;
	oForm = document.getElementById(sForm);
	
	if (oForm != undefined && oForm !=null) {
		for (key=0;key<oForm.elements.length;key++) {
			if (oForm.elements[key].type=="checkbox" ) {
				if (oForm.elements[key].checked) {
					data+=escape(oForm.elements[key].name)+"="+escape(oForm.elements[key].value)+"&";
				}
			}
			else {
				data+=escape(oForm.elements[key].name)+"="+escape(oForm.elements[key].value)+"&";
			}
		}
		data = data.substr(0, data.length-1);
		return data ;
	}else {
		alert("Erreur : "+ sForm + " "+ oForm);
		return false;
	}
}

function SendAjaxForm ( sUrl, sDivReturn, sForm)
{
	
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert("Votre navigateur ne supporte pas le javascript.");
		return false;
	} 
	
	var url=sUrl;
	
	xmlHttp.onreadystatechange = function (){
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
		{		
					
				setInnerHtml(xmlHttp.responseText,"divmessage")
				DiplayAnHidePopup("divhide","inline");
		}
	}
	
	var params = getDataFromForm(sForm) ;
	
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	xmlHttp.send(params);
	
	return true;
} 

function SendAjaxFormAndEval ( sUrl, sForm)
{
	
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert("Votre navigateur ne supporte pas le javascript.");
		return false;
	} 
	
	var url=sUrl;
	
	xmlHttp.onreadystatechange = function (){
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
		{		
				eval (xmlHttp.responseText);
		}
	}
	
	var params = getDataFromForm(sForm) ;
	
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	xmlHttp.send(params);
	
	return true;
} 

function GetEvalData ( sUrl, sParam)
{
	
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert("Votre navigateur ne supporte pas le javascript.");
		return false;
	} 
	
	var url=sUrl;
	var params = sParam ;
		
	xmlHttp.onreadystatechange = function (){
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
		{		
				eval (xmlHttp.responseText);
		}
	}
	
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	xmlHttp.send(params);
	
	return true;
} 

function DiplayAnHidePopup(sElement,sDisplay) {
	
	oElement = document.getElementById(sElement);
	
	var my_width  = 0;
    var my_height = 0;

    if ( typeof( window.innerWidth ) == 'number' ){
        my_width  = window.innerWidth;
        my_height = window.innerHeight;
    }else if ( document.documentElement && 
             ( document.documentElement.clientWidth ||
               document.documentElement.clientHeight ) ){
        my_width  = document.documentElement.clientWidth;
        my_height = document.documentElement.clientHeight;
    }
    else if ( document.body && 
            ( document.body.clientWidth || document.body.clientHeight ) ){
        my_width  = document.body.clientWidth;
        my_height = document.body.clientHeight;
    }

    var scrollY = 0;

    if ( document.documentElement && document.documentElement.scrollTop ){
        scrollY = document.documentElement.scrollTop;
    }else if ( document.body && document.body.scrollTop ){
        scrollY = document.body.scrollTop;
    }else if ( window.pageYOffset ){
        scrollY = window.pageYOffset;
    }else if ( window.scrollY ){
        scrollY = window.scrollY;
    }
    
    oElement.style.height= my_height + scrollY + "px";
    
    if (oElement.style.display!=sDisplay) {
		oElement.style.display=sDisplay;
	}else {
		oElement.style.display="none";
	}
	
}

function ChangeShowedPicture (sId) {

	var oShowPict = $('showpicture');
	var aDiv = oShowPict.getElements("div");
	
	aDiv.each(function(oDiv) {
		oDiv.style.display='none';
	});
	var oDivView = document.getElementById(sId);
	
	if (oDivView) { 
		oDivView.style.display='inline';
	}
	
}
