//TODO : delocaliser arrow_close/open => personal_space_arrow_close/open


//Lors du click sur 'espace client' utilisateur non identifie
function clickPersonalSpaceUserNotLogged()
{
var arrow_close  = '/images/espace_perso/fleche_deroule.gif';
var arrow_open   = '/images/fleche_marr.gif';
	
	switch( $('personal_space_state').firstChild.data )
	{
		case 'opened' :
		
		closeRegisterForm();

		break;
		
		case 'closed' :
		
		openRegisterForm();
	
		break;
	}
}

/**
 *
 * Try to authenticate an user
 */
function userRegisteredSuccess()
{

new Ajax.Updater(
'loginInfoSpace',
'/MySpace/GetMyMenu',
{
        method: 'get'
}
);

$('newsletter_space').innerHTML = 'Espace perso <strong><a  href="javascript:void(0)" onclick="clickPersonalSpace()" id="txt_espace_perso">Fermer</a></strong> <a href="javascript:void(0)" onclick="clickPersonalSpace()"><img src="/images/espace_perso/close_06.gif" width="16" height="16" border="0" align="absmiddle"></a>';
  	
personalSpaceLinkFollow( '/MySpace/MyCoords' );

	if( callbackFunctionAfterLogin != false )
	{
		callbackFunctionAfterLogin();
	}
}

//Lorsque l'utilisateur clicke sur "valider" dans le formulaire enregistrement
function registerNewUser( userdatas )
{
	
	var mandatory = new Array('contact_name',
							  'contact_first_name',
							   'contact_address',
							    'contact_post_code',
							     'city',
							      'contact_login',
							       'contact_country',
							        'contact_password',
							         'contact_email',
							         'contact_gsm');
	
	if( checkFormFieldsOk( 'formUserCoords', mandatory ) )
	{
		new Ajax.Request(
			    '/MyAccount/RegisterInAjax?'+userdatas,
			    {
			        method: 'get',
			        onSuccess: function( xx ){
			        	
				        if( xx.responseText.substring( 0, 7 ) == 'SUCCESS' )
				        {
				        	//index 0 = login, index 1 = pass
				        	var phrase = xx.responseText.substring( 8, xx.responseText.length ).split( '#' );
				        	userRegisteredSuccess();
				        }
	        			else
	        			{
	        				var errorPhrase = xx.responseText.substring( 6, xx.responseText.length );
	        					alert( errorPhrase );
	        			}
			        }
				}
			); 
	}
	else
	{
		alert('Vous devez remplir toutes les informations');
	}
}


function handleMyCoords( userdatas )
{
	var mandatory = new Array('contact_name', 'contact_first_name', 'contact_address', 'contact_post_code', 'city', 'contact_country', 'contact_email');
	
	if( checkFormFieldsOk( 'formUserCoords', mandatory ) )
	{
		new Ajax.Request(
			    '/MySpace/ModifyCoords?'+userdatas,
			    {
			        method: 'post',
			        onSuccess: function( xx ){
			        	
				        if( xx.responseText.substring( 0, 7 ) == 'SUCCESS' )
				        {
				        	//index 0 = login, index 1 = pass
				        	//var phrase = xx.responseText.substring( 8, xx.responseText.length ).split( '#' );
				        	personalSpaceLinkFollow( '/MySpace/MyCoords' );
				        }
	        			else
	        			{
	        				var errorPhrase = xx.responseText.substring( 6, xx.responseText.length );
	        					alert( errorPhrase );
	        			}
			        }
				}
			); 
	}
	else
	{
		alert('Vous devez remplir toutes les informations');
	}
}

//validateModifiedAddress( Form.serialize('formNewAddress') )
function validateModifiedAddress( lformdatas )
{
	var mandatory = new Array( 'address_name', 'name', 'first_name', 'address', 'country', 'city' );
	
	if( checkFormFieldsOk( 'formNewAddress', mandatory ) )
	{
		 new Ajax.Request(
			    '/MySpace/ModifyOneAddress?'+lformdatas,
			    {
			        method: 'post',
			        onSuccess: function( xx ){
			        	
				        if( xx.responseText.substring( 0, 7 ) == 'SUCCESS' )
				        {
				        	//index 0 = login, index 1 = pass
				        	//var phrase = xx.responseText.substring( 8, xx.responseText.length ).split( '#' );
				        	personalSpaceLinkFollow( '/MySpace/MyCoords' );
				        }
	        			else
	        			{
	        				var errorPhrase = xx.responseText.substring( 6, xx.responseText.length );
	        				alert( errorPhrase );
	        			}
			        }
				}
			); 
	}
	else
	{
		alert('Vous devez remplir toutes les informations');
	}
}

//Verifie que le formulaire d'enregistrement a bien ete remplis
function checkFormFieldsOk( lformname, mandatory )
{

	var error_occur = false;
	
	for( var i =0; i< mandatory.length; i++ )
	{
		if( document.forms[lformname].elements[mandatory[i]].value == '' )
		{
			oldbordercolor = document.forms[lformname].elements[mandatory[i]].style.border;
			document.forms[lformname].elements[mandatory[i]].style.border ='1px solid red';
			document.forms[lformname].elements[mandatory[i]].onblur = function(){
				
				this.style.border = oldbordercolor;
				
			}
			error_occur = true;
		}
	}
	
	return !error_occur;
}


//Lors du click sur 'annuler' depuis formaulaire enregistrement
function closeRegisterForm()
{
	var arrow_open   = '/images/fleche_marr.gif';
	
	$('personal_space_state').firstChild.data = 'closed';
	$('arrow_espace_client').src   			  = arrow_open;
	Effect.BlindUp('personal_space_main_div');
}

//Ouverture formaulaire enregistrement
function openRegisterForm()
{
	if( $('personal_space_state').firstChild.data == 'closed' )
	{
		new Ajax.Updater(
		    'personal_space_contents',
		    '/MyAccount/GetRegisterForm',
		    {
		        method: 'get',
		        onSuccess: function( xx )
		        {
        		var arrow_close  = '/images/espace_perso/fleche_deroule.gif';

				$('personal_space_state').firstChild.data = 'opened';
				$('arrow_espace_client').src   			  = arrow_close;
				Effect.BlindDown('personal_space_main_div');
		        }
			}
		); 	
	}
}




//Lors du click sur 'espace client' utilisateur identifie
function clickPersonalSpace()
{
var arrow_close  = '/images/espace_perso/fleche_deroule.gif';
var arrow_open   = '/images/fleche_marr.gif';
	
	switch( $('personal_space_state').firstChild.data )
	{
		case 'opened' :
		
		closePersonalSpace();
		
		break;
		
		case 'closed' :
		
		openPersonalSpace();
		
		break;
	}
}

function openPersonalSpace()
{
	var arrow_close  = '/images/espace_perso/fleche_deroule.gif';

	$('personal_space_state').firstChild.data = 'opened';
	$('txt_espace_perso').firstChild.data     = 'fermer';
	$('arrow_espace_client').src   			  = arrow_close;
	Effect.BlindDown('personal_space_main_div');
}

function closePersonalSpace()
{
	var arrow_open   = '/images/fleche_marr.gif';
	
	$('personal_space_state').firstChild.data = 'closed';
	$('txt_espace_perso').firstChild.data     = 'ouvrir';
	$('arrow_espace_client').src   			  = arrow_open;
	Effect.BlindUp('personal_space_main_div');
}

function personalSpaceLinkFollow( lpath )
{	
	$('personal_space_current_url').firstChild.data = lpath;
	
	new Ajax.Updater(
	    'personal_space_contents',
	    lpath,
	    {
	        method: 'get',
	        onSuccess: function( xx )
	        {
	        	if( $('personal_space_state').firstChild.data == 'closed' )
	        	{
	        		openPersonalSpace();
	        		if(!reperePanierForPopUp){
						PopUpOldMember();
						}
	        	}
	        }
		}
	); 	
}


function mySpaceSeeImagePage( pagenum )
{
	new Ajax.Updater(
	'personal_space_contents',
	'/MySpace/MyPics',
		{
			method: 'get',
			parameters: {page:pagenum},
			onComplete: function( xxx ){
			
			}
		}
	);
}

function suppresionAdresse(argument){
		if(confirm('Etes-vous sur de vouloir supprimer cette adresse de livraison')){
				personalSpaceLinkFollow(argument);
		}// fin confirm
}// fin function

