// Fonction de stockage des scripts à charger onload
FuncOL = new Array(); 
FuncOUL = new Array();

function StkFunc(Obj) { 
	if(typeof(Obj) == "function")
	{
	    FuncOL[FuncOL.length] = Obj; 
	}
} 

function StkFuncOnUnLoad(Obj) { 
	if(typeof(Obj) == "function")
	{
	    FuncOUL[FuncOL.length] = Obj; 
	}
} 

function BodyOnLoad()
{
	for(i=0; i<FuncOL.length; i++) 
        {
			FuncOL[i]();
		} 
}

function BodyOnUnLoad()
{
	for(i=0; i<FuncOUL.length; i++) 
        {
			//alert(FuncOUL[i]);
			//FuncOUL[i]();
		} 
}

function redirect( url )
{
	self.location.href = url; 
}

//##############################################################
//function SendRequest(fichier)
//##############################################################
function SendRequest(fichier)
 {
	 if(window.XMLHttpRequest) // FIREFOX
		  xhr_object = new XMLHttpRequest(); 
	 else if(window.ActiveXObject) // IE
		  xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
	 else 
		  return(false); 
	 xhr_object.open("GET", fichier, false); 
	 xhr_object.send(null); 
	 if(xhr_object.readyState == 4) return(xhr_object.responseText);
	 else return(false);
 }
 
 
//##############################################################
//	function GetPostAjaxContent(url,param,idDivDestination)
// 	Fonction ajax générique
//##############################################################
	function GetPostAjaxContent(url,params,idDivDestination,onloadfunct)
	{
		 if(window.XMLHttpRequest) // FIREFOX
			  var http = new XMLHttpRequest(); 
		 else if(window.ActiveXObject) // IE
			  var http = new ActiveXObject("Microsoft.XMLHTTP"); 
		 else 
			  return(false); 

		http.open("POST", url, true);
		//Send the proper header infomation along with the request
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
	
		http.onreadystatechange = function() {//Call a function when the state changes.
		//alert(url + '?' + params +' - '+ http.readyState);
			if(http.readyState == 4 && http.status == 200) {
//				alert(url + http.responseText);
				document.getElementById(idDivDestination).innerHTML  = http.responseText;
				if(pageTracker)
				{
				//	pageTracker._trackPageview( 'PostAjax_' + url );
				}
//				if(pageurl != '') ajaxpageloadaction(pageurl);
				//alert(typeof(onloadfunct));
				if(typeof(onloadfunct) == "function"){
					//alert('ok');
					onloadfunct();
				}
			}
		}
		http.send(params);
		return true; 
	}


//##############################################################
//function GetSubmitFormValues(form)
//##############################################################
function GetSubmitFormValues(form)
{
	var sub = form.getElementsByTagName('input');
	query = new Array();
	for(var i = 0 ; i < sub.length ; ++ i )
	{
		var node = sub.item(i);
		//alert(node.name + '/' + node.type);
		if(node.name)
		{
			//alert(node.type);
			switch (node.type)
			{
			   case 'text':
			   case 'password':
					query.push(node.name + '=' + encodeURIComponent(node.value));
					break;
			   case 'hidden':
					query.push(node.name + '=' + encodeURIComponent(node.value) );
//					alert(sub[i].name + '/' + sub[i].value);
					break;
			   case 'checkbox':
			   case 'radio':
					//alert(node.name + '/' + node.type + '/' + node.checked);
					if(node.checked)
					{
						query.push(node.name + '=' + node.value);
					}
					else
					{
						//query.push(node.name + '=0');
					}
					break;
			}
		}		
	}
	var post = '';
	var sub = form.getElementsByTagName("select");
	for(var i = 0 ; i < sub.length ; i++)
	{ 
		var node = sub.item(i);
		if(node.multiple == false)
		{
			post += '&' + node.name + '=' + node.options[node.options.selectedIndex].value;
		}
		else
		{
			optionsSelect = node.options;
			for(var j = 0 ; j < optionsSelect.length ; j++)
			{
				if(optionsSelect[j].selected)
				{
					post += '&' + node.name + '=' + optionsSelect[j].value;
				}
			}
		}
	}

	var sub = form.getElementsByTagName("textarea");
	for(var i = 0 ; i < sub.length ; i++)
	{ 
		var node = sub.item(i);
		//alert(node.name + '/' + node.type);
		if(node.name)
		{
			switch (node.type)
			{
			   case 'textarea':
			   		text = node.value;
					query.push(node.name + '=' + encodeURIComponent(node.value) );
//					query.push(node.name + '=' + text.replace('\n','\\n' ) );
//					query.push(node.name + '=' + encodeURIComponent(node.value) );
					break;
			}
		}
	}
	//alert(post);
	query = query.join('&'); 
	params = query + '&' + post;

	return params;
}

//##############################################################
//function SrcReplaceReload(iItem)
//##############################################################
function SrcReplaceReload(iItem)
{
	var h = document.getElementById(iItem);
	h.src="/img2/recaploader.gif";
}

//##############################################################
//function iniAutoCompleterCity()
//##############################################################
function iniAutoCompleterCity()
{
	new  Ajax.Autocompleter(
	'autoCompleteCity',
	'autoCompleteMenuCity',
	'/ajax/boMainCity.php?gere=autocomplet',
	{
		ignoreCase:true,
		minChars: 2,frequency:.2,
		afterUpdateElement :function(input, li)
		{
			//document.location.href = 'bo-societe-' + li.id + '.html';
			var tmp = li.id;
			var val = tmp.split('|');
			
			document.getElementById('idville').value = val[0];
			document.getElementById('cp').value = val[1];
		}
	}
	);
}


function submitfrmRecupByMail(form)
{
//	var form = document.getElementById('frmidentification');
	if(form.email.value != '')
	{
		var url = "/ajax/box_member.php";
		query = new Array();
		query.push('gere=' + form.gere.value);
		query.push('email=' + form.email.value);
		query = '?' + query.join('&'); 
		retour = SendRequest(url + query);
		document.getElementById('BXIDENT_content').innerHTML  = retour;		
	}
}

function submitfrmidentification(form)
{
	var url = "/ajax/box_member.php";
	var params = GetSubmitFormValues(form);
 	retour = SendRequest(url + '?' + params);
	document.getElementById('BXIDENT_content').innerHTML  = retour;		
}

function recupecompte()
{
		var url = "/ajax/box_member.php";
		var query = "?gere=drawformrecupcpt";
		retour = SendRequest(url + query);
		document.getElementById('BXIDENT_content').innerHTML  = retour;		
}

function inipagecompte()
{
	var url = "/ajax/main_user.php";
	var query = "?gere=drawformnew";
 	retour = SendRequest(url + query);
	document.getElementById('Main_Content').innerHTML  = retour;		
}

function ShowBlocCircuit( idCircuit )
{
	var url = "/ajax/GetContentCircuits.php";
	var params = 'gere=showfiche&id=' + idCircuit;
	var idDivDestination = 'Main_Content';
	var onloadfunct = '';
	var onloadfunct = init_frontOngletFicheCircuit;
	GetPostAjaxContent(url,params,idDivDestination,onloadfunct);
}

function init_frontOngletFicheCircuit()
{
	var OngletFicheCircuit=new ddajaxtabs("OngletFicheCircuittabs", "OngletFicheCircuitdivcontainer")
	OngletFicheCircuit.setpersist(true)
	OngletFicheCircuit.setselectedClassTarget("link") //"link" or "linkparent"
	OngletFicheCircuit.init()

	OngletFicheCircuit.onajaxpageload = function(pageurl)
	{
		//alert( pageurl );
		if(pageurl.indexOf("sheet") != -1)
		{
			init_MapFicheCircuit();
		}
		if(pageurl.indexOf("schedule") != -1)
		{
			
		}
	}
}

function ShowBlocOrganisateur( id )
{
	var url = "/ajax/GetContentOrganisateur.php";
	var params = 'gere=showfiche&id=' + id;
	var idDivDestination = 'Main_Content';
	var onloadfunct = '';
	GetPostAjaxContent(url,params,idDivDestination,onloadfunct);
}


function espaceonclick(value,url)
{
	document.cookie = "typeportail="+value+";domain=oorouler.com"; //cookie value is domain wide (path=/)
	document.location.href = url;
}

function SubmitFormAddEvent( form , div )
{
	var url = "/ajax/MainEvent.php";
	var idDivDestination = div; //'divContentAddDate';
	var params = GetSubmitFormValues(form);

	var onloadfunct = init_Calendar_FormAddDate;
	GetPostAjaxContent(url,params,idDivDestination,onloadfunct);
}

function btnDateSupprimerOnclick( div )
{
	if(confirm('Supprimer ?'))
	{
		form = document.getElementById("frmDateEvent");
		form.gere.value='deldate';
		var url = "/ajax/MainEvent.php";
		var idDivDestination = div; //'divContentAddDate';
		var params = GetSubmitFormValues(form);
	
		var onloadfunct = '';
		GetPostAjaxContent(url,params,idDivDestination,onloadfunct);
	}
}



function init_Calendar_FormAddDate()
{
	if( document.getElementById("field_datestart") )
	{
		Calendar.setup({
			inputField     :    "field_datestart",     // id of the input field
			ifFormat       :    "%d/%m/%y",      // format of the input field
			button         :    "f_trigger_start",  // trigger for the calendar (button ID)
			align          :    "Bl",           // alignment (defaults to "Bl")
			singleClick    :    true,
			dateStatusFunc :    function (date) { // disable weekend days (Saturdays == 6 and Subdays == 0)
									 var yesterday = new Date(); 
									 yesterday.setDate(yesterday.getDate()-1) 
									if (date <= yesterday) {
										//alert(dFin +" <= "+ oDateDeb);
										return true;
									}
									else
										return false;
									}							
		});
	
/*		Calendar.setup({
			inputField     :    "field_dateend",     // id of the input field
			ifFormat       :    "%d/%m/%y",      // format of the input field
			button         :    "f_trigger_end",  // trigger for the calendar (button ID)
			align          :    "Bl",           // alignment (defaults to "Bl")
			singleClick    :    true,
			cache			:	true,
	//				dateStatusFunc	: dateStatusHandler
			dateStatusFunc :    function (date) { // disable weekend days (Saturdays == 6 and Subdays == 0)
									 var yesterday = new Date(); 
									 yesterday.setDate(yesterday.getDate()-1) 
									if (date <= yesterday) {
										//alert(dFin +" <= "+ oDateDeb);
										return true;
									}
									else
										return false;
									}
		});
*/
	}
}
	
function dateStatusHandler( date, y, m, d ) {
//						if (document.getElementById("field_datestart").value.length==8) {
				var dateDeb	= document.getElementById("field_datestart").value.split("/");
				var DateDeb = new Date();
				DateDeb.setDate(dateDeb[0]);
				DateDeb.setMonth(dateDeb[1]-1);
				DateDeb.setFullYear("20" + dateDeb[2]);
				DateDeb.setHours(0,0,0);
				//alert(oDateDeb + " / " + date );
				date.setHours(0,0,0);
				if (date < DateDeb) {
					//alert(date +" < "+ DateDeb);
					diff = date.getTime()-DateDeb.getTime();

					alert( diff);
					return true;
				}
				else
					return false;
//						}
		}
		
//###################################################################
//	Page Compte Utilisateur

//--------------------------------------------------------------------
//function submitCompteInfosConnexion( form )
//--------------------------------------------------------------------
function submitCompteInfosConnexion( form )
{
	if( CheckVide_CompteInfosConnexion( form ) )
	{
		var url = "/ajax/main_user.php";
		var params = GetSubmitFormValues(form);
		var idDivDestination = 'divContentCompte';
		var onloadfunct = '';
		GetPostAjaxContent(url,params,idDivDestination,onloadfunct);
	}
}

//--------------------------------------------------------------------
//function CheckVide_CompteInfosConnexion( form ) {
//--------------------------------------------------------------------
function CheckVide_CompteInfosConnexion( form )
{
	vide( form ,'username','- Identifiant');
	vide( form ,'email','- Adresse e-mail');
//	vide( form ,'password','- Votre mot de passe');
//	vide( form ,'confirm_password','- Confirmation du mot de passe');
	
	validiteOnSubmit( form ,'username','ANS',6,25,'- Identifiant');
	validiteOnSubmit( form ,'email','EMAIL',0,50,'- Adresse e-mail');
//	validiteOnSubmit( form ,'password','ANS',6,25,'- Votre mot de passe');
	
//	check_pwOnSubmit( form ,'password','confirm_password');
	
	return resultat(form);
}

//--------------------------------------------------------------------
//function submitCompteInfosConnexion( form )
//--------------------------------------------------------------------
function submitCompteInfos( form )
{
	var url = "/ajax/main_user.php";
	var params = GetSubmitFormValues(form);
	var idDivDestination = 'divContentCompte';
	var onloadfunct = '';
	GetPostAjaxContent(url,params,idDivDestination,onloadfunct);
}

//###################################################################
//	Popup Agenda liste dates

//--------------------------------------------------------------------
//function showpopupLstEvent( year,month,intday,day,typeportail )
//--------------------------------------------------------------------
function showpopupLstEvent( year,idmonth,intday,idday,typeportail,typeconsult,id )
{
//	alert('ok');
	document.getElementById('popupLstEvent').style.display='block';
	document.getElementById('popupLstEvent').innerHTML = '<div class="map-info-box" style="height:150px;width200px;background: #000000 url(/images/loading_2.gif) no-repeat center center;"></div>';
	var url = "/ajax/MainAgenda.php";
//	var params = 'gere=DrawPopup&year=' + year + '&idmonth=' + month + '&idday=' + day + '&intday=' + intday + '&typeportail=' + typeportail + '&typeportail=' + typeportail ;
	var params = new Array();
	params.push('gere=DrawPopup');
	params.push('year=' + year);
	params.push('idmonth=' + idmonth);
	params.push('intday=' + intday);
	params.push('idday=' + idday);
	params.push('typeportail=' + typeportail);
	params.push('typeconsult=' + typeconsult);
	params.push('id=' + id);
	params = params.join('&'); 

	var idDivDestination = 'popupLstEvent';
	var onloadfunct = '';
//	GetPostAjaxContent(url,params,idDivDestination,onloadfunct);
 	retour = SendRequest(url + '?' + params);
	document.getElementById(idDivDestination).innerHTML  = retour;		

}

//--------------------------------------------------------------------
//function HidepopupLstEvent()
//--------------------------------------------------------------------
function HidepopupLstEvent()
{
document.getElementById('popupLstEvent').style.display='none';
}

//--------------------------------------------------------------------
//function popupEventListRow_onclick( id )
//--------------------------------------------------------------------
function popupEventListRow_onclick( id )
{
	HidepopupLstEvent();
}

function AgendaFilter_onchange()
{
	var form = document.getElementById('frmAgendaFilter');
	var url = "/ajax/MainAgenda.php";
	var params = GetSubmitFormValues(form);
	var idDivDestination = 'divContentMain';
	var onloadfunct = '';
	GetPostAjaxContent(url,params,idDivDestination,onloadfunct);
}

