﻿// JavaScript Document
function check(){
	var msg = "";
	
	if (document.inscription.nom.value == ""){
		msg += "Veuillez indiquer votre nom\n";
	}
	if (document.inscription.prenom.value == ""){
		msg += "Veuillez indiquer votre prénom\n";
	}
	if (document.inscription.date_1.value == ""){
		msg += "Veuillez indiquer votre date de naissance\n";
	}
	else if (isDate(document.inscription.date_1.value)==false){
		msg += "Merci de bien vouloir corriger votre date de naissance!\n";
	}
	else if(!checkAge("date_1")){
		msg += "Désolé, vous n'avez pas l'âge requis pour participer\n";
	}
	
	if (document.inscription.adresse.value == ""){
		msg += "Veuillez indiquer votre adresse\n";
	}
	
	if (document.inscription.code_postal.value == ""){
		msg += "Veuillez indiquer votre code postal\n";
	}
	else if(!verif_cp(document.inscription.code_postal.value)){
		msg += "Votre code postal est incorrect (format:75000) \n";
	}
	
	if (document.inscription.ville.value == ""){
		msg += "Veuillez indiquer votre ville\n";
	}
	if (document.inscription.telephone.value == ""){
		msg += "Veuillez indiquer votre téléphone\n";
	}
	else if(Verifier_Numero_Telephone(document.inscription.telephone.value) == false){
		msg += "Votre téléphone est incorrect (format:0612345678)\n";
	}
	
	if (document.inscription.email.value == ""){
		msg += "Merci de bien vouloir indiquer votre email\n";
	}
	else {
		indexAroba = document.inscription.email.value.indexOf('@');
		indexPoint = document.inscription.email.value.indexOf('.');
		if ((indexAroba < 0) || (indexPoint < 0))		{
			msg += "Merci de bien vouloir corriger votre email\n";
		}
	}
	if (!document.inscription.age.checked){
		msg += "Merci de bien vouloir certifier que vous avez + de 16 ans \n";
	}
	if (!document.inscription.cgv.checked){
		msg += "Merci de bien vouloir accepter le règlement du concours\n";
	}
		
	//if ((msg != "" || (msg == "" && document.inscription.mail.value == "")) && !checkPhone(document.inscription.phone.value)){
	/*if (document.inscription.phone.value!='' && !checkPhone(document.inscription.phone.value)){
		msg += "Invalid phone number\n";
	}
	*/
	if (msg == "") return(true);
	else{
		alert(msg);
		return(false);
	}
}

function check_jeux(){
	var msg = "";
	
	if (document.inscription.nom.value == ""){
		msg += "Veuillez indiquer votre nom\n";
	}
	if (document.inscription.prenom.value == ""){
		msg += "Veuillez indiquer votre prénom\n";
	}
	
	if (document.inscription.nom_rue.value == ""){
		msg += "Veuillez indiquer votre rue\n";
	}
	
	if (document.inscription.code_postal.value == ""){
		msg += "Veuillez indiquer votre code postal\n";
	}
	else if(!verif_cp(document.inscription.code_postal.value)){
		msg += "Votre code postal est incorrect (format:75000) \n";
	}
	
	if (document.inscription.ville.value == ""){
		msg += "Veuillez indiquer votre ville\n";
	}
	/*if (document.inscription.telephone.value == ""){
		msg += "Veuillez indiquer votre téléphone\n";
	}
	else if(Verifier_Numero_Telephone(document.inscription.telephone.value) == false){
		msg += "Votre téléphone est incorrect (format:0612345678)\n";
	}*/
	
	if (document.inscription.email.value == ""){
		msg += "Veuillez indiquer votre email\n";
	}
	else {
		indexAroba = document.inscription.email.value.indexOf('@');
		indexPoint = document.inscription.email.value.indexOf('.');
		if ((indexAroba < 0) || (indexPoint < 0))		{
			msg += "Veuillez corriger votre email\n";
		}
	}
	if (document.inscription.date_1.value == ""){
		msg += "Veuillez indiquer votre date de naissance\n";
	}
	else if (isDate(document.inscription.date_1.value)==false){
		msg += "Merci de bien vouloir corriger votre date de naissance!\n";
	}
	else if(!checkAge("date_1")){
		msg += "Désolé, vous n'avez pas l'âge requis pour participer\n";
	}
	if (!document.inscription.age.checked){
		msg += "Merci de bien vouloir certifier que vous êtes majeur avant le 13 juillet 2010 \n";
	}
	if (!document.inscription.cgv.checked){
		msg += "Merci de bien vouloir accepter le règlement du concours\n";
	}
		
	//if ((msg != "" || (msg == "" && document.inscription.mail.value == "")) && !checkPhone(document.inscription.phone.value)){
	/*if (document.inscription.phone.value!='' && !checkPhone(document.inscription.phone.value)){
		msg += "Invalid phone number\n";
	}
	*/
	if (msg == "") return(true);
	else{
		alert(msg);
		return(false);
	}
}
function check_festival(){
	var msg = "";
	
	if (document.inscription.num.value == ""){
		msg += "Veuillez indiquer le numéro de fabrication\n";
	}
	
	if (document.inscription.num.value.length!=12 || document.inscription.num.value <= 100000000000 || document.inscription.num.value >= 1000000000000){
		msg += "La première partie de votre numéro doit comporter 12 chiffres\n";
	}
	if (document.inscription.num2.value.length!=3 || isNaN(document.inscription.num2.value) || document.inscription.num2.value >= 999){
		msg += "La seconde partie de votre numéro doit comporter 3 chiffres\n";
	}
		
	//if ((msg != "" || (msg == "" && document.inscription.mail.value == "")) && !checkPhone(document.inscription.phone.value)){
	/*if (document.inscription.phone.value!='' && !checkPhone(document.inscription.phone.value)){
		msg += "Invalid phone number\n";
	}
	*/
	if (msg == "") return(true);
	else{
		alert(msg);
		return(false);
	}
}
function check2(){
	var msg = "";
	
	if (!document.myform.fichier_1 || document.myform.fichier_1.value == ""){
		msg += "Veuillez indiquer votre fichier musical\n";
	}
	if (document.myform.ligne_1.value == ""){
		msg += "Veuillez indiquer un titre de composition\n";
	}
	
	if (document.myform.select_1.value == ""){
		msg += "Veuillez indiquer un genre\n";
	}
		
	if (msg == ""){
		if (document.myform.ligne_4.value == ""){
			document.myform.ligne_4.value = document.myform.ligne_2.value+' '+document.myform.ligne_3.value;
		}
		document.myform.action = 'spip.php?page=merci2';
		return(true);
	}
	else{
		alert(msg);
		return(false);
	}
}
function check_voter(){
	var msg = "";
	
	if (document.inscription.nom.value == ""){
		msg += "Veuillez indiquer votre nom\n";
	}
	if (document.inscription.prenom.value == ""){
		msg += "Veuillez indiquer votre prénom\n";
	}
	if (document.inscription.date_2.value == ""){
		msg += "Veuillez indiquer votre date de naissance\n";
	}
	else if (isDate(document.inscription.date_2.value)==false){
		msg += "Merci de bien vouloir corriger votre date de naissance!\n";
	}
	else if(!checkAge("date_2")){
		msg += "Désolé, vous n'avez pas l'âge requis pour participer\n";
	}
	
	if (document.inscription.email.value == ""){
		msg += "Merci de bien vouloir indiquer votre email\n";
	}
	else {
		indexAroba = document.inscription.email.value.indexOf('@');
		indexPoint = document.inscription.email.value.indexOf('.');
		if ((indexAroba < 0) || (indexPoint < 0))		{
			msg += "Merci de bien vouloir corriger votre email\n";
		}
	}
	if (!document.inscription.age.checked){
		msg += "Merci de bien vouloir certifier que vous avez + de 16 ans \n";
	}
	if (!document.inscription.cgv.checked){
		msg += "Merci de bien vouloir accepter le règlement du concours\n";
	}
	
		
	//if ((msg != "" || (msg == "" && document.inscription.mail.value == "")) && !checkPhone(document.inscription.phone.value)){
	/*if (document.inscription.phone.value!='' && !checkPhone(document.inscription.phone.value)){
		msg += "Invalid phone number\n";
	}
	*/
	if (msg == "") return(true);
	else{
		alert(msg);
		return(false);
	}
}

function check_astro2(){
	var i = 0;
	var nb = 0;
	while(document.getElementById('qualite_' + i)){
		if(document.getElementById('qualite_' + i).checked){
			nb++;
		}
		i++;
	}
	
	if(nb==3){
		return true;
	}
	else{
		window.alert("Merci de sélectionner 3 qualités.");
		return false;
	}
}

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
	return true
}

function verif_cp(valeur){
	if(valeur>1000 && valeur<99999){
		return true;
	}
	else{
		return false;
	}
}
function check_file(valeur){
	tab_val=valeur.split('.');
	
	if(tab_val[tab_val.length - 1] == 'mp3'){
		document.getElementById('video_file').style.display = 'none';
		document.getElementById('mp4_file').style.display = 'none';
		document.getElementById('audio_file').style.display = 'block';
	}
	else if(tab_val[tab_val.length - 1] == 'mp4'){
		document.getElementById('video_file').style.display = 'none';
		document.getElementById('mp4_file').style.display = 'block';
		document.getElementById('audio_file').style.display = 'none';
	}
	else if(tab_val[tab_val.length - 1] == 'flv'){
		document.getElementById('video_file').style.display = 'block';
		document.getElementById('mp4_file').style.display = 'none';
		document.getElementById('audio_file').style.display = 'none';
	}
	else{
		document.getElementById('video_file').style.display = 'none';
		document.getElementById('mp4_file').style.display = 'none';
		document.getElementById('audio_file').style.display = 'none';
		document.getElementById('fichier_1').value = '';
		alert("Fichiers supportés : mp3, mp4, flv");
	}
}

function Verifier_Numero_Telephone(num_tel){
	// Definition du motif a matcher
	var regex = new RegExp(/^(01|02|03|04|05|06|08|09)[0-9]{8}/gi);
	// Definition de la variable booleene match
	var match = false;
	// Test sur le motif
	if(regex.test(num_tel))	{
		match = true;
	}
	else{
		match = false;
	}
	// On renvoie match
	return match;
} 

function affiche_cache(my_id){
	document.getElementById('content_106').style.display = 'none';
	document.getElementById('content_119').style.display = 'none';
	document.getElementById('content_120').style.display = 'none';
	document.getElementById('content_121').style.display = 'none';
	document.getElementById('content_122').style.display = 'none';
	document.getElementById('content_123').style.display = 'none';
	document.getElementById('content_124').style.display = 'none';
		
	document.getElementById(my_id).style.display = 'block';	
}

function affiche_pagination(){
	document.getElementById('voir').style.display = 'none';
	document.getElementById('affichage').style.display = 'block';	
}
function checkAge(my_id){
	/* the minumum age you want to allow in */
	var min_age = 18;
	tab_age = (document.inscription[my_id].value).split('/');
	
	/* change "age_form" to whatever your form has for a name="..." */
	var year = parseInt(tab_age[2]);
	var month = parseInt(tab_age[1])-1;
	var day = parseInt(tab_age[0]);

	var theirDate = new Date((year + min_age), month, day);
	var today = new Date(2010,07,13);

	if ( (today.getTime() - theirDate.getTime()) < 0) {
		return false;
	}
	else {
		return true;
	}
}
