/////////////////////////////////////////////////////
//// JAVASCRIPT DE LA PARTIE MEMBRE / PROFIL
/////////////////////////////////////////////////////

// VERIFICATION DE LA CONNEXION
// VERIFICATION DU FORMULAIRE DE CONNEXION MY MUSIQUE RADIO
function verif_connexion(p_login, p_mdp, p_verif)
{
	var msg = "";
	
	// LOGIN
	if(document.getElementById(p_login).value == "")   
	{
		msg += "Merci de saisir votre login\n";
		document.getElementById(p_login).style.backgroundColor = "#E2C4C4";
	}
	
	// @
	if(document.getElementById(p_mdp).value == "")   
	{
		msg += "Merci de saisir votre mot de passe\n";
		document.getElementById(p_mdp).style.backgroundColor = "#E2C4C4";
	}
	
	if (msg == "")
	{
		document.getElementById(p_verif).value = "1";
		return(true);
	}
	else 
	{
		  alert(msg);
		  return(false);
	}
}

// CONNEXION MY MUSIQUE RADIO 
function connexion_my_musique_radio(p_refresh) 
{	
	if(document.getElementById('checked').value == 1)
	{		
		if(document.getElementById('ins_cookies').checked)
			cookies = 1;
		else
			cookies = 0;
			
		verif_xhr();
		
		xhr_object.open("POST", "../pg/resultats_profil.php", true);
		
		xhr_object.onreadystatechange = function() 
		{
			if(xhr_object.readyState == 4 && xhr_object.status == 200)
			{
				//tb_remove();
				reponse = xhr_object.responseText;
				
				if(reponse == '-1')
				{
					document.getElementById('message_erreur_connexion').innerHTML = "Erreur d'identification, vous n'avez pas valid&eacute; votre inscription";
					document.getElementById('message_erreur_connexion').style.display = '';
				}
				else if(reponse == '-2')
				{
					document.getElementById('message_erreur_connexion').innerHTML = "Erreur d'identification, &ecirc;tes-vous s&ucirc;r d'avoir un compte my MusiqueRadio ?";
					document.getElementById('message_erreur_connexion').style.display = '';
				}
				else if(reponse == '1')
				{
					location.reload(true)
				}
				else
				{
					var profil = reponse.split('[***]');
					document.getElementById('message_erreur_connexion').style.display = 'none';
					document.getElementById('zone_non_connecte').style.display = 'none';
					document.getElementById('zone_connecte').style.display = '';
					document.getElementById('login_connexion').innerHTML = profil[1];
				}
			}
		}

		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhr_object.send("action=connexion_my_musique_radio&login="+document.getElementById('ins_login').value+"&password="+document.getElementById('ins_pass').value+"&rafraichissement="+p_refresh+"&cookies="+cookies);
	}
}

// DECONNEXION MY MUSIQUE RADIO 
function deconnexion_my_musique_radio(p_refresh) 
{		
	verif_xhr();
	
	xhr_object.open("POST", "../pg/resultats_profil.php", true);
	
	xhr_object.onreadystatechange = function() 
	{
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			reponse = xhr_object.responseText;
			if(reponse == '-1')
			{
				document.getElementById('message_erreur_connexion').style.display = 'none';
				document.getElementById('zone_non_connecte').style.display = '';
				document.getElementById('zone_connecte').style.display = 'none';
				document.getElementById('login_connexion').innerHTML = '';
			}
			else if(reponse == '1')
			{
				location.reload(true)
			}
		}
	}
	
	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr_object.send("action=deconnexion_my_musique_radio&rafraichissement="+p_refresh);
}

// VERIFICATION INSCRIPTION / MODIFICATION
// VERIFICATION DE L'EMAIL
function verification_mail(p_mail, p_compare)
{
	if (!p_mail) 
		 return false;
	
	if(p_mail == document.form_profil.last_email.value)
		return false;
	
	if(p_compare == 1)
	{
		if(p_mail == document.form_profil.ins_email.value)
		{
			document.form_profil.ins_email_confirmation.style.background = "#FFFFFF url('/images/saisie_ok.gif') no-repeat 100% 0%";
			document.form_profil.email_compare.value = "0";
			return true;
		}
		else
		{
			document.form_profil.ins_email_confirmation.style.background = "#E2C4C4 url('/images/saisie_no.gif') no-repeat 100% 0%";
			document.form_profil.email_compare.value = "1";
			return false;
		}
	}

	indexAroba = p_mail.indexOf('@');
	indexPoint = p_mail.indexOf('.');
	
	if ((indexAroba < 0) || (indexPoint < 0))      
		 document.form_profil.ins_email.style.background = "#E2C4C4 url('/images/saisie_no.gif') no-repeat 100% 0%";
	else
	{
		verif_xhr();
		
		xhr_object.open("POST", "pg/resultats_profil.php", true);
		
		xhr_object.onreadystatechange = function() 
		{
			if(xhr_object.readyState == 4 && xhr_object.status == 200)
			{
				if(xhr_object.responseText == "1")
				{ 
					document.form_profil.ins_email.style.background = "#E2C4C4 url('/images/saisie_no.gif') no-repeat 100% 0%";
					document.form_profil.email_exist.value = "1";
					return false;
				}
				else
				{
					document.form_profil.email_exist.value = "0";
					document.form_profil.ins_email.style.background = "#FFFFFF url('/images/saisie_ok.gif') no-repeat 100% 0%";
					return true;
				}
			}
		}
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");			
		xhr_object.send("action=verif_mail&mail="+p_mail);
	}
}

// VERIFICATION DU LOGIN
function verification_login(p_login)
{
	if (!p_login) 
		 return;
	
	if(p_login == document.form_profil.last_login.value) 
		 return;
	
	if (p_login.length >= 3) 
	{
		verif_xhr();
		
		xhr_object.open("POST", "pg/resultats_profil.php", true);
		
		xhr_object.onreadystatechange = function() 
		{
			if(xhr_object.readyState == 4 && xhr_object.status == 200)
			{
				if(xhr_object.responseText == "2")
				{ 
					document.form_profil.ins_pseudo.style.background = "#E2C4C4 url('/images/saisie_no.gif') no-repeat 100% 0%";
					document.form_profil.login_exist.value = "1";
				}
				else if (xhr_object.responseText == "1")
				{
					document.form_profil.ins_pseudo.style.background = "#E2C4C4 url('/images/saisie_no.gif') no-repeat 100% 0%";
				}
				else
				{
					document.form_profil.login_exist.value = "0";
					document.form_profil.ins_pseudo.style.background = "#FFFFFF url('/images/saisie_ok.gif') no-repeat 100% 0%";
				}
			}
		}
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");			
		xhr_object.send("action=verif_login&login="+p_login);
	}
	else
	{
		document.form_profil.ins_pseudo.style.background = "#E2C4C4 url('/images/saisie_no.gif') no-repeat 100% 0%";
	}
}

// VERIFICATION DU MOT DE PASS
function verification_mdp(p_mdp)
{
	if (!p_mdp) 
		 return;
	
	if (p_mdp.length < 4) 
		document.form_profil.ins_password.style.background = "#E2C4C4 url('/images/saisie_no.gif') no-repeat 100% 0%";
	else
		document.form_profil.ins_password.style.background = "#FFFFFF";
}

// VERIFICATION DE LA DATE DE NAISSANCE
function verification_naissance(p_date)
{
	if (!p_date) 
		 return;
	
	if (p_date.length == 10)
	{
		verif_xhr();
		
		xhr_object.open("POST", "pg/resultats_profil.php", true);
		
		xhr_object.onreadystatechange = function() 
		{
			if(xhr_object.readyState == 4 && xhr_object.status == 200)
			{
				if(xhr_object.responseText == "1")
				{ 
					document.form_profil.ins_naissance.style.background = "#E2C4C4 url('/images/saisie_no.gif') no-repeat 100% 0%";
					document.form_profil.naissance_correcte.value = "1";
				}
				else
				{
					document.form_profil.naissance_correcte.value = "0";
					document.form_profil.ins_naissance.style.background= "#FFFFFF url('/images/saisie_ok.gif') no-repeat 100% 0%";;
				}
			}
		}
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");			
		xhr_object.send("action=verif_naissance&naissance="+p_date);
	}
	else
	{
		document.form_profil.ins_naissance.style.background = "#E2C4C4 url('/images/saisie_no.gif') no-repeat 100% 0%";
		document.form_profil.naissance_correcte.value = "1";
	}
}

// VERIFICATION DU PROFIL COMPLET
function verif_profil(p_modification)
{
	var msg = "";
	
	// @
	if (document.form_profil.ins_email.value != "")   
	{
		if(document.form_profil.email_exist.value == "1")
		{
			document.form_profil.ins_email.style.backgroundColor = "#E2C4C4";
			msg += "L'adresse email est déjà utilisée\n";
		}
		else
		{
			indexAroba = document.form_profil.ins_email.value.indexOf('@');
			indexPoint = document.form_profil.ins_email.value.indexOf('.');
			
			if ((indexAroba < 0) || (indexPoint < 0))      
			{
				document.form_profil.ins_email.style.backgroundColor = "#E2C4C4";
				msg += "L'adresse email n'est pas valide\n";
			}
			
			if(document.form_profil.ins_pseudo.value != "" && document.form_profil.ins_pseudo.value == document.form_profil.ins_email.value)
			{
				document.form_profil.ins_pseudo.style.backgroundColor = "#E2C4C4";
				msg += "Votre login doit être différent de votre adresse email\n";
			}
		}
	} 
	else 
	{
		  document.form_profil.ins_email.style.backgroundColor = "#E2C4C4";
		  msg += "Merci de saisir votre email\n";
	}
	
	if(document.form_profil.ins_email_confirmation.value != "")   
	{
		if(document.form_profil.email_compare.value == "1")
		{
			document.form_profil.ins_email_confirmation.style.backgroundColor = "#E2C4C4";
			msg += "L'adresse email de confirmation est incorrecte\n";
		}
		else
		{
			indexAroba = document.form_profil.ins_email_confirmation.value.indexOf('@');
			indexPoint = document.form_profil.ins_email_confirmation.value.indexOf('.');
			if ((indexAroba < 0) || (indexPoint < 0))      
			{
				document.form_profil.ins_email_confirmation.style.backgroundColor = "#E2C4C4";
				msg += "L'adresse email de confirmation n'est pas valide\n";
			}
		}
	} 
	else 
	{
		  document.form_profil.ins_email_confirmation.style.backgroundColor = "#E2C4C4";
		  msg += "Merci de saisir l'email de confirmation\n";
	}
	
	// LOGIN
	if(document.form_profil.ins_pseudo.value == "")   
	{
		msg += "Merci de saisir un login\n";
		document.form_profil.ins_pseudo.style.backgroundColor = "#E2C4C4";
	}
	else if(document.form_profil.ins_pseudo.value.length < 3)   
	{
		msg += "Le login est trop court\n";
		document.form_profil.ins_pseudo.style.backgroundColor = "#E2C4C4";
	}
	else if (document.form_profil.login_exist.value == "1")   
	{
		msg += "le login existe déjà\n";
		document.form_profil.ins_pseudo.style.backgroundColor = "#E2C4C4";
	}
	
	// MDP
	if(document.form_profil.ins_password.value == "")   
	{
		msg += "Merci de saisir un mot de passe\n";
		document.form_profil.ins_password.style.backgroundColor = "#E2C4C4";
	}
	if(document.form_profil.ins_password.value.lenght <  4)   
	{
		msg += "Votre mot de passe est trop court\n";
		document.form_profil.ins_password.style.backgroundColor = "#E2C4C4";
	}
	
	// PRENOM
	if(/\w/.test(document.form_profil.ins_prenom.value) == false)   
	{
		msg += "Merci de saisir votre prénom\n";
		document.form_profil.ins_prenom.style.backgroundColor = "#E2C4C4";
	}
	
	// NOM
	if(/\w/.test(document.form_profil.ins_nom.value) == false)   
	{
		msg += "Merci de renseigner votre nom\n";
		document.form_profil.ins_nom.style.backgroundColor = "#E2C4C4";
	}
	
	if(p_modification == 1)
	{
		// EDITO
		if(/\w/.test(document.form_profil.ins_tit_presentation.value) == false && document.form_profil.ins_presentation.value != "")
		{
			msg += "Vous n'avez pas donné de titre à votre édito\n";
			document.form_profil.ins_tit_presentation.style.backgroundColor = "#E2C4C4";
		}
		else if(/\w/.test(document.form_profil.ins_presentation.value) == false && document.form_profil.ins_tit_presentation.value != "")
		{
			msg += "Vous n'avez pas écrit d'édito\n";
			document.form_profil.ins_presentation.style.backgroundColor = "#E2C4C4";
		}

		// SITE
		if (document.form_profil.ins_site.value != "")   
		{
			indexPoint = document.form_profil.ins_site.value.indexOf('.');
			indexarobase = document.form_profil.ins_site.value.indexOf('@');
			
			if (indexPoint < 0 || indexarobase > 0)     
			{
				document.form_profil.ins_site.style.backgroundColor = "#E2C4C4";
				msg += "l'url de votre site est incorrecte\n";
			}
		}
	}
	
	if (msg == "")
	{
		document.form_profil.checked.value="1";
		return(true);
	}
	else 
	{
		  alert(msg);
		  return(false);
	}
}

// VERIFICATION DE L'EMAIL DE RENVOI
function verification_mail_renvoi(p_mail)
{
	if (!p_mail) 
		 return false;

	indexAroba = p_mail.indexOf('@');
	indexPoint = p_mail.indexOf('.');
	
	if ((indexAroba < 0) || (indexPoint < 0))      
	{
		 document.form_renvoi.email_renvoi.style.background = "#BDCEDA url('/images/saisie_no.gif') no-repeat 100% 0%";
		 return false;
	}
	else
	{
		verif_xhr();
		
		xhr_object.open("POST", "pg/resultats_profil.php", true);
		
		xhr_object.onreadystatechange = function() 
		{
			if(xhr_object.readyState == 4 && xhr_object.status == 200)
			{
				if(xhr_object.responseText == "1")
				{ 
					document.form_renvoi.email_exist.value = "1";
					document.form_renvoi.email_renvoi.style.background = "#FFFFFF url('/images/saisie_ok.gif') no-repeat 100% 0%";
					return true;
				}
				else
				{
					document.form_renvoi.email_renvoi.style.background = "#BDCEDA url('/images/saisie_no.gif') no-repeat 100% 0%";
					document.form_renvoi.email_exist.value = "0";
					return false;
				}
			}
		}
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");			
		xhr_object.send("action=verif_mail&mail="+p_mail);
	}
}

function verif_mail_renvoi()
{
	var msg = "";
	
	// @
	if (document.form_renvoi.email_renvoi.value != "")   
	{
		if(document.form_renvoi.email_exist.value == "0")
		{
			document.form_renvoi.email_renvoi.style.backgroundColor = "#BDCEDA";
			msg += "L'adresse email ne correspond à aucun profil\n";
		}
		else
		{
			indexAroba = document.form_renvoi.email_renvoi.value.indexOf('@');
			indexPoint = document.form_renvoi.email_renvoi.value.indexOf('.');
			if ((indexAroba < 0) || (indexPoint < 0))      
			{
				document.form_renvoi.email_renvoi.style.backgroundColor = "#BDCEDA";
				msg += "L'adresse mail n'est pas valide\n";
			}
		}
	} 
	else 
	{
		  document.form_renvoi.email_renvoi.style.backgroundColor = "#BDCEDA";
		  msg += "Merci de saisir votre email\n";
	}
	
	if (msg == "")
	{
		document.form_renvoi.checked.value = "1";
		return(true);
	}
	else 
	{
		  alert(msg);
		  return(false);
	}
}

function affiche_programme_chaine(p_chain_id, p_chain_nom)
{
	verif_xhr();
	
	xhr_object.open("POST", "pg/resultats_profil.php", true);
	
	xhr_object.onreadystatechange = function() 
	{
		if(xhr_object.readyState == 4 && xhr_object.status == 200)			
			document.getElementById('contenu_chaine').innerHTML = xhr_object.responseText;
		else			
			document.getElementById('contenu_chaine').innerHTML = '<p class="chargement"><img src="../images/loadingAnimation.gif" /></p>';
	}

	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr_object.send("action=affiche_programme_chaine&chain_nom="+p_chain_nom+"&chain_id="+p_chain_id);
}

/* AFFICHAGE PLAYLIST DU PLAYER */
function modif_position(p_chain_id, p_pro_position, p_pro_new_position, p_chain_nom, p_pro_id) 
{		
	verif_xhr();
	
	xhr_object.open("POST", "pg/resultats_profil.php", true);
	
	xhr_object.onreadystatechange = function() 
	{
		if(xhr_object.readyState == 4 && xhr_object.status == 200)
		{
			affiche_programme_chaine(p_chain_id, p_chain_nom);
		}
		else
		{
			document.getElementById('contenu_chaine').innerHTML = '<p class="chargement"><img name="image" src="../images/loadingAnimation.gif" /></p>';
		}
	}
	
	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr_object.send("action=modif_position&chain_id="+p_chain_id+"&pro_position="+p_pro_position+"&pro_new_position="+p_pro_new_position+"&pro_id="+p_pro_id);
}
