var current					= document.location.href;
var urlSite					= current.substring( 0 ,current.lastIndexOf( "/" ) + 1 );

function makeObject()
{
	var x;
	
	if (window.ActiveXObject)
	{
		x = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) 
	{
		x = new XMLHttpRequest();
	}

	return x;
}

var request_d = makeObject();

function post_dispo( action )
{
	var ok = document.getElementById( 'ModifierDispoCeSoir' ).value;

    request_d.open( 'POST', urlSite, true );
    request_d.onreadystatechange = function() {
		if( request_d.readyState == 4 ) {
			var answer = request_d.responseText;

			if ( action == "Dispo")	{		document.getElementById( "nonDispoCeSoir" ).style.display = "none"; document.getElementById("dispoCeSoir").style.display = "block";		}
			if ( action == "nonDispo") {	document.getElementById( "dispoCeSoir" ).style.display = "none"; document.getElementById("nonDispoCeSoir").style.display = "block";		}	  
		}
	}

	request_d.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var data = "ModifierDispoCeSoir="+ok+"&Action="+action;
    request_d.send(data);
}

var request_f = makeObject();

function post_favoris(action)
{
	var video  = document.getElementById('Video').value;

    request_f.open( 'POST', urlSite, true );
    request_f.onreadystatechange = function() {
		if( request_f.readyState == 1 ) {
			document.getElementById( "chargementFavoris" ).style.display = "block";
			if ( action == "Ajouter")	document.getElementById( "ajouterFavoris" ).style.display = "none";
			if ( action == "Supprimer")	document.getElementById( "supprimerFavoris" ).style.display = "none";
		}

	   if( request_f.readyState == 4 ) {
			var answer = request_f.responseText;
			document.getElementById( "chargementFavoris" ).style.display = "none";
			if ( action == "Ajouter")	document.getElementById( "supprimerFavoris" ).style.display = "block";
			if ( action == "Supprimer")	document.getElementById( "ajouterFavoris" ).style.display = "block";				  
		}
	}

	request_f.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
	var data = "Favoris=" + action + "&idVideoFavoris=" + video;
    request_f.send( data );
}

var request_s = makeObject();

function post_selection(action)
{
	var membre  = document.getElementById('Membre').value;

    request_s.open('POST', urlSite, true);
    request_s.onreadystatechange = function() {

		if( request_s.readyState == 1 ) {
			document.getElementById( "ajouterSelection" ).style.display			= "none";
			document.getElementById( "supprimerSelection" ).style.display		= "none";
			document.getElementById( "chargementSelection" ).style.display		= "block";	
		}

	   if(request_s.readyState == 4) {
			var answer = request_s.responseText;
			document.getElementById( "chargementSelection" ).style.display		= "none";
			
			if ( action == "Ajouter") {
				document.getElementById( "ajouterSelection" ).style.display		= "none";
				document.getElementById( "supprimerSelection" ).style.display	= "block";
			}
			else {
				document.getElementById( "supprimerSelection" ).style.display	= "none";
				document.getElementById( "ajouterSelection" ).style.display		= "block";  
			}
		}
	}

	request_s.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var data = "Selection="+action+"&idMembre="+membre;
    request_s.send(data);
}

var ajout_photo = makeObject();

function post_ajoutPhoto( titrePhoto, nomFichier, idMembre, albumId )
{
	ajout_photo.open('POST', urlSite + 'Gestion_photos_utilisateur' , true);

	ajout_photo.onreadystatechange = function() {
		if( ajout_photo.readyState == 4 ) { 
			var retour				= ajout_photo.responseText;
			document.location.href	= urlSite + 'Gestion_photos_utilisateur?albumId=' + albumId + "&addPhoto=1" ;
		}
	};

	ajout_photo.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var data = "GestionAction=AjouterPhoto&idMembre=" + idMembre + "&nomFichier=" + nomFichier + "&titrePhoto=" + titrePhoto + "&albumId=" + albumId;
	ajout_photo.send(data);
}

var videoReq = makeObject();

function post_ajoutVideo(titreVideo, nomFichier, idMembre, descriptionVideo, priveVideo, pswdVideo)
{
    videoReq.open('POST', urlSite + 'Gestion_videos_utilisateur', true);

	videoReq.onreadystatechange = function() {
	   if(videoReq.readyState == 4)
		{
		    var answer = videoReq.responseText;
			document.location.href = urlSite + 'Gestion_videos_utilisateur' ;
		}
	}
	
	videoReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var data = "GestionAction=AjouterVideo&idMembre="+idMembre+"&nomFichier="+nomFichier+"&titreVideo="+titreVideo+"&descriptionVideo="+descriptionVideo+"&priveVideo="+priveVideo+"&pswdVideo="+pswdVideo;
    videoReq.send(data);
}

var videoSup = makeObject();

function ajaxVideoSupprimer(videoASupprimer)
{
	var GestionAction				= document.getElementById('GestionAction').value;
	var divChargement				= "chargement_"+videoASupprimer;
	var divAffichageConfirmation	= "affichageConfirmation_"+videoASupprimer;
	var divGlobalVideo				= "globalVideo_"+videoASupprimer;
	
    videoSup.open('POST', urlSite + 'Gestion_videos_utilisateur', true);

	videoSup.onreadystatechange = function() {
		if(videoSup.readyState == 1)
		{
			document.getElementById(divAffichageConfirmation).style.display = "none";
			document.getElementById(divChargement).style.display = "block";
		}

	   if(videoSup.readyState == 4)
		{
			var answer = videoSup.responseText;
			document.getElementById(divChargement).style.display = "none";
			document.getElementById(divGlobalVideo).style.display = "none";
			rafraichir(GestionAction);
		}
	}

	videoSup.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var data = "GestionAction="+GestionAction+"&Num="+videoASupprimer;
    videoSup.send(data);
}

var ajax = makeObject();

function ajaxSupprimer(albumASupprimer)
{
	var GestionAction = document.getElementById('GestionAction').value;
	
	var divChargement = "chargementAlbum_"+albumASupprimer;
	var divAffichageConfirmation = "affichageConfirmation_"+albumASupprimer;
	var divGlobalAlbum = "globalAlbum_"+albumASupprimer;
	
    ajax.open('POST', urlSite + 'Gestion_photos_utilisateur' , true);

	ajax.onreadystatechange = function() {
		if(ajax.readyState == 1)
		{
			document.getElementById(divAffichageConfirmation).style.display = "none";
			document.getElementById(divChargement).style.display = "block";

		}

	   if(ajax.readyState == 4)
		{
			var answer = ajax.responseText;
			document.getElementById(divChargement).style.display = "none";
			document.getElementById(divGlobalAlbum).style.display = "none";
			rafraichir(GestionAction,answer);

		}
	}

	ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var data = "GestionAction="+GestionAction+"&Num="+albumASupprimer;
    ajax.send(data);
}

var raf = makeObject();

function rafraichir(zone, album){

    if( ( zone == "SupprimerPhoto" ) || ( zone == "SupprimerAlbum" ) )	url = urlSite + 'Gestion_photos_utilisateur?albumId=' + album ;
	else																url = urlSite + 'Gestion_videos_utilisateur' ;

    raf.open('POST',url, true);

    raf.onreadystatechange = function() {
		if(raf.readyState == 4){
			var answer = raf.responseText;
			document.getElementById('zoneARafraichir').innerHTML = answer;
			document.getElementById('zoneARafraichir').style.display = "block";
		}
	}

	raf.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	
	var data = "Rafraichir="+zone;
    raf.send(data);
}
   


function changeDisplay(Afficher, Cacher)
{
	document.getElementById(Cacher).style.display = "none";
	document.getElementById(Afficher).style.display = "block";
}

function AllerA(targ,selObj){
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'" );	
}

function pushSelectStyle()
{
	$( 'divProfil' ).style.display	= "none" ;
	$( 'divStyle' ).style.display	= "block" ;
}

function changeInput( val )
{
	$( 'skin' ).value	= val ;
	$( 'divStyle' ).style.display	= "none" ;
	$( 'divProfil' ).style.display	= "block" ;	
	
	if ( val == 0 )	$( 'btnStyle' ).innerHTML	= "FEUILLE DE STYLE : Moderne" ;
	if ( val == 1 )	$( 'btnStyle' ).innerHTML	= "FEUILLE DE STYLE : Boudoir" ;
	if ( val == 2 )	$( 'btnStyle' ).innerHTML	= "FEUILLE DE STYLE : Peace and Love" ;
	if ( val == 3 )	$( 'btnStyle' ).innerHTML	= "FEUILLE DE STYLE : Blackout" ;
	if ( val == 4 )	$( 'btnStyle' ).innerHTML	= "FEUILLE DE STYLE : Poupées russes" ;
	if ( val == 5 )	$( 'btnStyle' ).innerHTML	= "FEUILLE DE STYLE : Venise" ;

	if ( $( 'avtStyle' ) )	$( 'avtStyle' ).style.display	= "block" ;	
}

function verifLog()
	{
		var options = { 
						method: "post" ,
						postBody: 'Action=verifLog' ,
						onSuccess: function (xhr)
						{
							switch( xhr.responseText ) 
							{
								case "LOG_KO" :
									
									ouvreBoite( 'formInscription', false, true ) ;

								break ;

								case "LOG_OK" :
									
									ouvreBoite( 'formCommentaire', false, true ) ;

								break ;
							}
						}
					  } 

		var ajaxCall = new Ajax.Request( "add_Commentaires.php", options );
	}

	function addCommentaires()
	{

		var options = { 
						method: "post" ,

						postBody: 'texte=' + encodeURIComponent( $F('comTexte') ) + '&Mod=' + $F('Module') + '&El=' + $F('Element') + '&Action=addCommentaires' , 

						onSuccess: function (xhr)
									{
										switch( xhr.responseText )
										{
											case "LOG_KO" :
												
												$('erreurForm').innerHTML = "Vous devez être connecté pour poster un commentaire." ;
												Effect.Appear( 'erreurForm' ); 

											break ;

											case "ADD_KO" :
												
												$('erreurForm').innerHTML = "Echec de l'ajout, veuillez réessayer." ;
												Effect.Appear( 'erreurForm' ); 

											break ;

											case "ADD_OK" :
												
												$('comTexte').value = "" ;
												getCommentaires( 1, true ) ;
												
											break ;
												
										}
										
									}
					  } 

		if ( $F('comTexte').length > 10 )
		{
			var ajaxCall = new Ajax.Request( "add_Commentaires.php", options );
		}
		else
		{
			$('erreurForm').innerHTML = "Un peu plus de texte svp ..." ;
			Effect.Appear( 'erreurForm' ); 
		}
	}
