

var div = false;
var loading = '<img src = "/images/ajax-loader.gif" />'; 
var loading2 = '<img src = "/images/ajax-loader-2.gif" />'; 
var loading3 = '<img src = "/images/ajax-loader-3.gif" />'; 
var loading4 = '<img src = "/images/ajax-loader-4.gif" />'; 
var loading5 = '<img src = "/images/ajax-loader-5.gif" />'; 


function searchContact( t, prof )
{
	var type = parseInt( t );
	if( $('email').value.trim() != '' &&  $('passwd').value.trim() != '' &&  $('provider').value.trim() != '')
	{
		if( t == 0 ){
			$('loading').set( 'html', loading3 );
		}else{
			$('loading').set( 'html', loading );
		}
		new Request({
			url: '/ajax/ajax_register.php',
			method: 'post',
			data: {
				cmd: 'searchContact',
				email: $('email').value,
				passwd: $('passwd').value,
				provider: $('provider').value,
				profil: prof,
				id: $('id').value,
				type: t
			},
			
			
			onSuccess: function( result ) {
				if( t == 1 ){
					$('loading').set( 'html', result );
				}else{
					box.update( result );
				}

			}
		}).send();
	}
}


function displayExp( value )
{
	if( value == 1 ){
		$('aff').style.display='block';
		$('pot_name').style.display='block';
	}else{
		if( value == 2 || value == '' ) {
			$('aff').style.display='none';
			$('pot_name').style.display='none';
		}
	}
}


function displayCalendar( id, language ){
	new DatePicker(
   	 '.'+id,
   	{ 
   		 pickerClass: 'datepicker_jqui', 
   		 positionOffset: { x: 0, y: 5 },
   		inputOutputFormat  :  language == 'fr ' ? 'd-m-Y' : 'Y-m-d',
   		months : returnMonth( language ),
   		days: returnDay( language )
   	}
	);
}


function returnMonth( language )
{
	var months = null;
	if ( language == 'fr' )
	{
		months = ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre']
	}else{
		months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
	}
	return months;
}

function returnDay( language )
{
	var days = null;
	if ( language == 'fr' )
	{
		days = ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi']
	}else{
		days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
	}
	return days;
	
}


function showDiv(id){
	if( $(id) ){
		if($(id).style.display=='block'){
			$(id).style.display='none';
		}else{
			$(id).style.display='block';
		}
	}
}


function showDiv2(id){
	document.getElementById(id).style.display='block';
}
function hideDiv2(id){
	document.getElementById(id).style.display='none';
}

function sendForm(form)
{
	$(form).set('send', {
		onSuccess: function(msg) {
			window.location.reload();
		}
	}).send();
}



function sendFormInvite(form)
{
	
	$(form).set('send', {
		onRequest: function(){
			showDiv( 'loading' );
		},
		onSuccess: function( response ) {
			if( $('result') ){
				$('result').set( 'html', result );
			}else
			{
				var div = new Element( 'div', { id : 'result' } ).injectAfter( $('loading') );
				div.set( 'html', result );
			}
		}
	}).send();
}

function sendFormPassword(form)
{
	
	$(form).set('send', {
		onRequest: function(){
			showDiv( 'loading' );
		},
		onSuccess: function( response ) {
			$('ajaxbox').setStyle( 'background', '#fff' );
			box.update(response);
		}
	}).send();
}

function passwordForget()
{
	box.show();
	$('ajaxbox').setStyle( 'background', 'none' );
	box.update( loading4 );
	new Request({
		url: '/ajax/ajax.php',
		method: 'get',
		data: {
			cmd: 'showPassword'
		},
		
		
		onSuccess: function(msg) {
			$('ajaxbox').setStyle( 'background', '#fff' );
			box.update(msg);
		}
	}).send();
}

function passwordForget()
{
	box.show();
	$('ajaxbox').setStyle( 'background', 'none' );
	box.update( loading4 );
	new Request({
		url: '/ajax/ajax.php',
		method: 'get',
		data: {
			cmd: 'clubJoined'
		},
		
		
		onSuccess: function(msg) {
			$('ajaxbox').setStyle( 'background', '#fff' );
			box.update(msg);
		}
	}).send();
}

function anotherOne(){
	$('dcaptcha').setProperty('src', '/captcha.php?x='+$time() );
}


function checkField( id, value, type  )
{
	var cmd = 'checkField';
	
	if( type ) cmd = 'checkEmail';
	
	new Request({
			url: '/ajax/ajax_register.php',
			method: 'post',
			data: {
				cmd: cmd,
				value: value
			},
			
			onSuccess: function( result ) {
				var exist = $( id).getNext('span' );
				if( exist )
				{
					
					exist.destroy();
					var div = new Element( 'div', { id :  'result_'+id } ).injectAfter( $(id) );
					div.set( 'html', result );
					
				}
				else 
				{
					if( result.trim() != '' )
					{
						if(  $('result_'+id ) ){
							 $('result_'+id ).set( 'html', result );
						}else
						{
							var div = new Element( 'div', { id :  'result_'+id } ).injectAfter( $(id) );
							div.set( 'html', result );
						}
					}
					else
					{
						if(  $('result_'+id ) ){
							$('result_'+id ).destroy();
						}
					}
				}
			}
		}).send();
	
}


function showMemberVote(id, type)
{
	box.show();
	$('ajaxbox').setStyle( 'background', 'none' );
	box.update( loading4 );
	new Request({
		url: '/ajax/ajax.php',
		method: 'get',
		data: {
			cmd: 'showMemberVote',
			id: id,
			type: type
		},
		
		
		onSuccess: function(msg) {
			$('ajaxbox').setStyle( 'background', '#fff' );
			box.update(msg);
		}
	}).send();
}



function showMemberExtra(id)
{
	box.show();
	$('ajaxbox').setStyle( 'background', 'none' );
	box.update( loading4 );
	new Request({
		url: '/ajax/ajax.php',
		method: 'get',
		data: {
			cmd: 'showMemberExtra',
			id: id
		},
		onSuccess: function(msg) {
			$('ajaxbox').setStyle( 'background', '#fff' );
			box.update(msg);
		}
	}).send();
}

function showBox()
{
	box.show();
	$('ajaxbox').setStyle( 'background', 'none' );
	box.update( loading4 );
	new Request({
		url: '/ajax/ajax.php',
		method: 'get',
		data: {
			cmd: 'showBox'
		},
		onSuccess: function(msg) {
			$('ajaxbox').setStyle( 'background', '#fff' );
			box.update(msg);
		}
	}).send();
}


function clubRestricted( id )
{
	box.show();
	$('ajaxbox').setStyle( 'background', 'none' );
	box.update( loading4 );
	new Request({
		url: '/ajax/ajax.php',
		method: 'get',
		data: {
			cmd: 'ClubRestricted',
			id: id
		},
		onSuccess: function(msg) {
			$('ajaxbox').setStyle( 'background', '#fff' );
			box.update(msg);
		}
	}).send();
}

function showPoliceMsg(id)
{
	box.show();
	
	new Request({
		url: '/ajax/ajax.php',
		method: 'get',
		data: {
			cmd: 'showPoliceMsg',
			id: id
		},
		onSuccess: function(msg) {
			box.update(msg);
		}
	}).send();
}


function showClubVote(id)
{
	box.show();
	$('ajaxbox').setStyle( 'background', 'none' );
	box.update( loading4 );
	new Request({
		url: '/ajax/ajax.php',
		method: 'get',
		data: {
			cmd: 'showClubVote',
			id: id
		},
		onSuccess: function(msg) {
			$('ajaxbox').setStyle( 'background', '#fff' );
			box.update(msg);
		}
	}).send();
}


function goToInviteForm( form )
{
	box.show();
	$('ajaxbox').setStyle( 'background', 'none' );
	box.update( loading4 );
	/*new Request({
		url: form.action,
		method: form.method,
		data: {
			cmd: 'goToInviteForm'
		},
		onSuccess: function(msg) {
			$('ajaxbox').setStyle( 'background', '#fff' );
			box.update(msg);
		}
	}).send();*/
	$(form).set('send', {
		url: '/ajax/ajax_register.php',
		method: 'post',
		onSuccess: function( response ) {
			$('ajaxbox').setStyle( 'background', '#fff' );
			box.update( response );
		}
	}).send();
}


function showClubExtra(id)
{
	box.show();
	$('ajaxbox').setStyle( 'background', 'none' );
	box.update( loading4 );
	new Request({
		url: '/ajax/ajax.php',
		method: 'get',
		data: {
			cmd: 'showClubExtra',
			id: id
		},
		onSuccess: function(msg) {
			$('ajaxbox').setStyle( 'background', '#fff' );
			box.update(msg);
		}
	}).send();
}


function goPage(isAdh,path)
{
	if(isAdh==0){
		box.show();
		new Request({
			url: '/ajax/ajax.php',
			method: 'get',
			data: {
				cmd: 'alertPage'
			},
			onSuccess: function(msg) {
				box.update(msg);
			}
		}).send();
	}else{
		window.location.replace(path);
	}
}


function goPaypal(isConnected)
{
	if(isConnected==0){
		box.show();
		new Request({
			url: '/ajax/ajax.php',
			method: 'get',
			data: {
				cmd: ''
			},
			onSuccess: function(msg) {
				box.update(msg);
			}
		}).send();
	}else{
		var checked=false;
		var form = document.forms.formBank;
		for (i=0 ; i<= form.length-1 ; i++){
			if (form[i].type == 'radio' && form[i].checked){
				var checked=true;				
			}
		}
		if(checked){
			document.getElementById('formBank').submit();
		}
	}
}

function showMemberReport(item, type)
{
	box.show();
	$('ajaxbox').setStyle( 'background', 'none' );
	box.update( loading4 );
	new Request({
		url: '/ajax/ajax.php',
		method: 'get',
		data: {
			cmd: 'showMemberReport',
			item: item,
			type: type
		},
		onSuccess: function(msg) {
			$('ajaxbox').setStyle( 'background', '#fff' );
			box.update(msg);
		}
	}).send();
}




function showMemberComment( item, type  )
{
	box.show();
	$('ajaxbox').setStyle( 'background', 'none' );
	box.update( loading4 );
	new Request({
		url: '/ajax/ajax.php',
		method: 'get',
		data: {
			cmd: 'showMemberComment',
			item: item,
			type: type
		},
		onSuccess: function(msg) {
			$('ajaxbox').setStyle( 'background', '#fff' );
			box.update(msg);
		}
	}).send();
}

function showMemberRegistred( link )
{
	
	box.show();
	
	new Request({
		url: '/ajax/ajax.php',
		method: 'post',
		data: {
			link: (link == null || link == undefined ) ? null : link 
		},
		onSuccess: function(msg) {
			box.update(msg);
		}
	}).send();
}

function showClubRegistred(id)
{
	box.show();
	$('ajaxbox').setStyle( 'background', 'none' );
	box.update( loading4 );
	
	new Request({
		url: '/ajax/ajax.php',
		method: 'get',
		data: {
			cmd: 'showRegistredClub',
			id: id
		},
		onSuccess: function(msg) {
			$('ajaxbox').setStyle( 'background', '#fff' );
			box.update(msg);
		}
	}).send();
}

function showAddCampaign()
{
	var typeToSend;
	type = document.choix.type_campaign;
	var setType = false;
	for (var i=0; i<type.length; i++)
	{
		if(type[i].checked == true){
			typeToSend=i;
			setType = true;
		}
	}
	
	if(setType) 
	{
		box.show();
		$('ajaxbox').setStyle( 'background', 'none' );
		box.update( loading4 );
		new Request({
			url: '/ajax/ajax.php',
			method: 'get',
			data: {
				cmd: 'showAddCampaign',
				type: typeToSend
			},
			onSuccess: function(msg) {
				$('ajaxbox').setStyle( 'background', '#fff' );
				box.update(msg);
			}
		}).send();
	}
}

// V�rifie que la valeurs pass�e est bien num�rique
function checkNum(num)
{
	if(isNaN(num)) return false;
	else return true;
}

// Retourne le type de choix de selection
function checkChoice()
{
	var choice = false;
	for(var i=1;i<4;i++)
	{
		if($('choice_'  + i).checked == true) choice = i;
	}
	return choice;
}

// Mets � jour les nombres dans les libell�s
function numberEmailing(val)
{
	if(checkNum(val))
	{
		$('number').removeClass('error');
		
		if(val == '') val = 0;
		
		for(var i=1;i<4;i++)
		{
			$('ch' + i).set('text',val);
		}
		
		if(checkChoice() != false) chooseEmailing(checkChoice());
		if($('type').value != '' || $('number').value > 0 )
		{
			showEmailing('false');
			uncheckEmailing();
		}
	}
	//else $('number').addClass('error');
}

// Cherche les membres en fonction du th�me choisi
function chooseMembersEmailing()
{
	var number = $('number').value;
	var th = $('theme').value;
	var type = $('type').value;
	
	
	
	/*if(checkNum(number) && number != '' && number != '0')
	{*/
		//$('chooseMember').addClass('error');
		//$('chooseMember').set('html','Loading data...<br /><br /><img src="/images/loading.gif" alt="" />');
		$('chooseMember').set('html','<img src="/images/loading.gif" alt="" />');
		
		new Request({
			url: '/ajax/ajax.php',
			method: 'get',
			data: {
				cmd: 'chooseMembersEmailing',
				th: th,
				type: type
			},
			onSuccess: function(msg) {
				//$('chooseMember').removeClass('error');
				$('chooseMember').set('html',msg);
				if($('number').value == 0 )
				{
					uncheckEmailing( true );
				}
				//numberEmailing(number);
			}
		}).send();
	//}
}



function chooseEmailing()
{
	var number = $('number').value;
	var select = $('chooseMember').getElementsByTagName("input");
	var ch =  checkChoice();
	
	var rand = 0;
	var tot = 0;
	
	// On remets tout � z�ro
	for (var i=0; i<select.length; i++)
	{
		select[i].checked = false;
	}
	
	for (var i=0; i<select.length; i++)
	{
		// Coche les 'number' premiers
		if(ch == 2 && i < number) select[i].checked = true;
		
		// Coche les 'number' al�atoirement
		if(ch == 1)
		{
			if(tot < number)
			{
				rand = randEmailing();
				if(rand == 1)
				{
					select[i].checked = true;
					tot++;
				}
				else select[i].checked = false;
			}
		}
	}
}

function initShowEmailing()
{
	if($('number').value != '' || parseInt( $('number').value ) >= 0 ) 
	{
		//showEmailing('false');
		chooseMembersEmailing();
	}
}

function showEmailing(see)
{
	/*box.show();
	box.update('Loading data...<br /><br /><img src="/images/loading.gif" alt="" />');*/
	
	type = $('type').value;
	number = $('number').value;
	
	new Request({
		url: '/ajax/ajax.php',
		method: 'get',
		data: {
			cmd: 'showEmailing',
			type: type,
			number: number,
			msg: see
		},
		onSuccess: function(msg) {
			if(msg == 'false') box.hide();
			else
			{
				box.show();
				box.update(msg);
				$('number').value = 0;
				chooseEmailing();
			}
		}
	}).send();
}

function randEmailing()
{
	var x = Math.round(Math.random());
	return x;
}

function checkChooseMember(id)
{
	var select = document.getElementById('chooseMember').getElementsByTagName("input");
	var max = $('number').value;
	var num = 0;
	
	for (var i=0; i<select.length; i++)
	{
		if(select[i].checked == true) num ++;
	}
	
	if(num > max)
	{
		$('ckeck_' + id).checked = false;
		box.show();
		new Request({
			url: '/ajax/ajax.php',
			method: 'get',
			data: {
				cmd: 'checkChooseMember',
				number: max
			},
			onSuccess: function(msg) {
				box.update(msg);
			}
		}).send();
	}
}

function scroll()
{			
	if( $('divScroll') )
	{
		new Scroll({
			id: 'divScroll', // id de la div qui entourre le tout
			tag: 'div', // fait d�filer toutes les div � l'interrieur de divScroll
			top: true,
			offset: 150, // height de divScroll
			speed: 2,
			pause: 3000
		});
	}
}

//Function de pagination en ajax
function goPagination( path, div, limit )
{
	$(div).set( 'html',  '<center>'+loading5+'</center>' );
	new Request({
		url: path,
		method: 'get',
		data: {
			limit: limit
		},
		onSuccess: function(msg) {
			$(div).set( 'html',  msg );
		}
	}).send();
}

function showPollVote(id,value)
{
	box.show();
	$('ajaxbox').setStyle( 'background', 'none' );
	box.update( loading4 );
	new Request({
		url: '/ajax/ajax.php',
		method: 'get',
		data: {
			cmd: 'showPollVote',
			id: id,
			value : value
		},
		onSuccess: function(msg) {
			$('ajaxbox').setStyle( 'background', '#fff' );
			box.update(msg);
		}
	}).send();
}



var openFacebook = function (){
	window.open( '/facebook.php', '', 'width = 550, height = 506');
}


var showFormGmail = function( prof, id ){

	if( prof == undefined || prof == null ) var prof = 0; 
	if( id == undefined || id == null ) var id = 0; 
	box.show();
	$('ajaxbox').setStyle( 'background', 'none' );
	box.update( loading4 );
	
	new Request({
		url: '/ajax/ajax_register.php',
		method: 'get',
		data: {
			cmd: "showFormGmail",
			profil:  prof,
			id: id
		},
		onSuccess: function(msg) {
			$('ajaxbox').setStyle( 'background', '#fff' );
			box.update(msg);
		}
	}).send();
}



var checkAllCheckbox = function ( form ){
	var input = form.getElementsByTagName( 'input' );
	for( var i = 0; i < input.length ; i++ ){
		if( input[i].type == 'checkbox' ){
			if( input[i].checked == true ){
				input[i].checked = false;
			}else{
				input[i].checked = true;
			}
		}
	}
}


//coche ou decoche contact gmail, msn ...
var selectAll = function ( send ){
	
	var input = $('bloContact').getElementsByTagName( 'input' );
	for( var i = 0; i < input.length ; i++ ){
		if( input[i].type == 'checkbox' ){
			if( input[i].checked == true ){
				input[i].checked = false;
			}else{
				input[i].checked = true;
			}
		}
	}
}

//envoi mail, contact msn gmail...
var sendMailContact = function( id, profil, type )
{
	
	if( type == undefined || type == null ) var type = 0;
	if( !$('result') )
	{
		new Element( 'p', { 
			'id' :  'result',
			'style' : 'width:280px;padding-top:5px; padding-bottom:5px;float:left;'
			} 
		).injectAfter( 'green' );
		
		new Element( 'div', { 
			'class': 'clear'
			} 
		).injectAfter( 'result' );
	}
	
	/*if( id == 0 ){
		$('result').set( 'html', loading3 );
	}else{
		$('result').set( 'html', loading2 );
	}*/
	
	$('result').set( 'html', loading );
	
	var array = new Array();
	var input = $('bloContact').getElementsByTagName( 'input' );
	
	for( var i = 0; i < input.length ; i++ ){
		if( input[i].type == 'checkbox' ){
			if( input[i].checked == true ){
				array.push( input[i].value );
			}
		}
	}
	new Request({
		url: '/ajax/ajax_register.php',
		method: 'post',
		data: {
			cmd: profil == 0 ? 'sendMailContact' : 'sendProfilContact',
			data: JSON.encode( array ),
			id: id,
			type: type
		},
		onSuccess: function( result ) {
			$('result').set( 'html', result );
		}
	}).send();
}

var uncheckEmailing = function( type )
{
	type = type == '' || type == undefined ? false: true;
	var form = $('search');
	var input = form.getElementsByTagName( 'input' );
	var checked = 0;
	for( var i = 0; i < input.length ; i++ ){
		if( input[i].type == 'checkbox' ){
			input[i].disabled = type;
		}
	}
}




var checkPicture = function ( form, nb ){
	
	var input = form.getElementsByTagName( 'input' );
	

		var checked = 0;
		for( var i = 0; i < input.length ; i++ ){
			if( input[i].type == 'checkbox' ){
				if ( input[i].checked == true ){
					checked++;
				}
			}
		}
		
		if( checked > nb-1 ){
			for( var i = 0; i < input.length ; i++ ){
				if( input[i].type == 'checkbox' ){
					if ( input[i].checked == false ){
						input[i].disabled = true;
					}
				}
			}
		}else{
			for( var i = 0; i < input.length ; i++ ){
				if( input[i].type == 'checkbox' ){
					if ( input[i].checked == false ){
						input[i].disabled = false;
					}
				}
			}
		}
}


function displayTag( value, id, type )
{
	if( !div )
	{
		div = new Element( 'div',{
			id: 'tagresult'
		}).injectAfter( $(id) );
	}
	
	if( value != '' )
	{
		div.show();
		
		new Request({
			url: '/ajax/tag.php',
			method :'post',
			
			data:{
				tag: value,
				type: type
			},
			onRequest: function()
			{
				div.set( 'html', '' );
				div.set( 'html' , '<div id = "loading">'+loading+'</div>' );
			},
			
			onSuccess: function( data ) {
				div.set( 'html', data );
			}
			
		}).send();
	}else{
		div.hide();
	}
}


function addTag( value )
{
	var previous = div.getPrevious();
	
	var split = previous.value.split(',') ;
	
	split[split.length-1] = value;
	
	previous.value = split;
	
	div.hide();
}