var textInfoBulle ;
var url_envoi     = '/php/verifInscription.php';
var submitform = false;
var objCible ;

function destroyimagecivil(){
	if( $chk( $('alertCivilMr') ) ){
		$('alertCivilMr').destroy();
	}
	$('bulleInfo').setStyles({'display':'none'});
}

function verifLoginPseudoDiff(){
	var valpseudo = $('pseudo').value.toLowerCase().trim();
	var valLogin  = $('login1').value.toLowerCase().trim();
	
	if(valpseudo == valLogin ){
		createBulle($('pseudo'),'Votre identifiant de connexion et votre pseudo doivent être différents');
	}else{
		new Request({url: url_envoi,
				method: 'post',
				async:false,
				onSuccess:function(txt){
					if( txt != 'ok'){
						createBulle($('pseudo'),txt);
						submitform =  true;
					}else{
						submitform =  false;
					}
					
		}}).send('id=pseudo&val='+valpseudo);
	}
}

function verifcivil(){
	current = $('CivilMr');
	id      = 'CivilMr';
	
	if( $chk($('alertCivilMr'))){
		$('alertCivilMr').destroy();
	}
	
	if(!$("CivilMlle").checked && !$("CivilMme").checked && !$("CivilMr").checked){
		new Request({url: url_envoi,
				method: 'post',
				async:false,
				onSuccess:function(txt){
					if( txt != 'ok'){
						createBulle(current,txt);
						$('alertCivilMr').addEvent('mouseover',createBulleAide.bind($('CivilMr')));
						submitform =  true;
					}else{
						submitform =  false;
					}
					
		}}).send('id='+id+'&err=err16');
	}
}

function VerifReglement(){
	current = $('regle');
	id      = 'regle';
	
	if( $chk($('alertregle'))){
		$('alertregle').destroy();
	}
	
	if(!$("regle").checked){
		new Request({url: url_envoi,
				method: 'post',
				async:false,
				onSuccess:function(txt){
					if( txt != 'ok'){
						createBulle(current,txt);
						//$('alertregle').addEvent('mouseover',createBulleAide.bind($('regle')));
						submitform =  true;
					}else{
						submitform =  false;
					}
					
		}}).send('id='+id+'&err=err19');
	}
}

function verifdatenaissance(current){
	var id    = current.id; 
	var DateJ = document.formulaire.jour.value;
	var DateM = document.formulaire.mois.value;
	var DateA = document.formulaire.annee.value;
	
	if( $chk($('alertjour')) ) $('alertjour').destroy();
	
	if(DateJ =="" || DateM =="" || DateA =="")
	{
		new Request({url: url_envoi,
				method: 'post',
				async:false,
				onSuccess:function(txt){
					if( txt != 'ok'){
						createBulle(current,txt);
						$('alertannee').addEvent('mouseover',createBulleAide.bind($('annee')));
						submitform =  true;
					}else{
						submitform =  false;
					}
					
		}}).send('id='+id+'&err=err17');
	}
	else {
		var aujour = new Date() ;
		var annee18ans = aujour.getFullYear() - 18 ;
		
		if (DateA > annee18ans){
			new Request({url: url_envoi,
					method: 'post',
					async:false,
					onSuccess:function(txt){
						if( txt != 'ok'){
							createBulle(current,txt);
							$('alertannee').addEvent('mouseover',createBulleAide.bind($('annee')));
							submitform =  true;
						}else{
							submitform =  false;
						}
						
			}}).send('id='+id+'&err=err29');			
		}
		else {
			if (DateA == annee18ans){
				var moisAujour = aujour.getMonth() + 1 ;    
				if (DateM > moisAujour){
					new Request({url: url_envoi,
							method: 'post',
							async:false,
							onSuccess:function(txt){
								if( txt != 'ok'){
									createBulle(current,txt);
									$('alertannee').addEvent('mouseover',createBulleAide.bind($('annee')));
									submitform =  true;
								}else{
									submitform =  false;
								}
								
					}}).send('id='+id+'&err=err29');
					
				}
				else if (DateM == moisAujour){
					if (DateJ > aujour.getDate()){
						new Request({url: url_envoi,
								method: 'post',
								async:false,
								onSuccess:function(txt){
									if( txt != 'ok'){
										createBulle(current,txt);
										$('alertannee').addEvent('mouseover',createBulleAide.bind($('annee')));
										submitform =  true;
									}else{
										submitform =  false;
									}
									
						}}).send('id='+id+'&err=err29');								
					}
				}
			}
		}
		
		var datenaiss = new Date(DateA,(DateM - 1),DateJ);
		if (DateA != datenaiss.getFullYear() || DateM != (datenaiss.getMonth()+1) || DateJ != datenaiss.getDate()){
			new Request({url: url_envoi,
					method: 'post',
					async:false,
					onSuccess:function(txt){
						if( txt != 'ok'){
							createBulle(current,txt);
							$('alertannee').addEvent('mouseover',createBulleAide.bind($('annee')));
							submitform =  true;
						}else{
							submitform =  false;
						}
						
			}}).send('id='+id+'&err=err29');
		}	
	}
}

function createBulle(champ,txt){
	var papa = champ.getParent();
	var imgAlerte;
	var scrImg ;
	var topImg;
	var leftImg = null;
	var positionImg;
	
	if(champ.id == 'login1'){
		scrImg      = '/images_new/inscription/champ_error_rose.gif' ;
		topImg      = '8px' ;
		leftImg     = $('login1').getWidth().toInt()+140+'px'; 
		positionImg = 'top';
		
		if( Browser.Engine.webkit ) {// correction safari
			leftImg = '567px'; 
		}
	}else if( champ.id == 'code_pin' ){
		scrImg      = '/images_new/inscription/champ_error_beige.gif' ;
		topImg      = '15px' ;
		leftImg     = '535px'; 
		positionImg = 'bottom';
	}else if(champ.id == 'code') {
		scrImg      = '/images_new/inscription/champ_error_beige.gif' ;
		topImg      = '10px' ;
		leftImg     = '570px'; 
		positionImg = 'bottom';
	}else if(champ.id == 'regle'){
		scrImg      = '/images_new/inscription/champ_error_beige.gif' ;
		topImg      = '-3px' ;
		leftImg     = '310px'; 
		positionImg = 'bottom';
	}else if(champ.id == 'CivilMr'){
		scrImg      = '/images_new/inscription/champ_error_beige.gif' ;
		topImg      = '-2px' ;
		leftImg     = '475px'; 
		positionImg = 'bottom';
		if( Browser.Engine.webkit ) {// correction safari
			leftImg = '511px'; 
		}
	}else{
		
		imgAlerte = new Element('img',{src:'/images_new/inscription/champ_error_beige.gif',id:'alert'+champ.id,alt:txt}); 
		imgAlerte.setStyles({position:'absolute',top:'-2px'});
		imgAlerte.inject(papa,'bottom');
	}
	
	if( leftImg != null){
		imgAlerte = new Element('img',{src:scrImg,id:'alert'+champ.id,alt:txt});
		imgAlerte.setStyles({position:'absolute',left:leftImg,top:topImg});
		imgAlerte.inject(papa,positionImg);
	}
}

function createBulleAide(){
	
	var myValues      = this.getCoordinates();
	var idbullealerte = 'alert'+this.id;
	objCible = this;
	
	if( $chk( this.getPrevious() )){
		var frereLabel    = this.getPrevious(); 
		var nomChamp      = frereLabel.innerHTML;
	}
	
	if( this.id == 'jour' ){
		nomChamp = $('labeldateNaissance').innerHTML;
		myValues = $('annee').getCoordinates();
	}
	
	var hauteur       = myValues.top.toInt()-20+'px';
	var left          = myValues.left.toInt()+myValues.width.toInt()+26+'px';
	nomChamp = nomChamp.toLowerCase();

	if( this.id == 'CivilMr' ){
		left = '670px';
		if( Browser.Engine.webkit ) {// correction safari
			left = '701px'; 
		}
	}	
	
	if( this.id == 'regle' ){
		left = '330px';
	}
	
	if( Browser.Engine.trident ){ // corrections du placement des bulles d'info pour ie
		hauteur = myValues.top.toInt()+55+'px';
		
		if( this.id == 'pass1' || this.id == 'pass2' || this.id == 'code_pin' ){
			hauteur = myValues.top.toInt()-20+'px';
		}
		
		if( this.id == 'login1'){
			hauteur = myValues.top.toInt()+25+'px';
			left    = myValues.left.toInt()+myValues.width.toInt()+30+'px';
		}
		
		if( this.id == 'code'){
			hauteur = myValues.top.toInt()+873+'px';
		}
	}

	if( $chk($(idbullealerte)) ){
		var texte = $(idbullealerte).getProperty('alt');
		
		if( texte == '' ){
			texte = textInfoBulle+'&nbsp;'+nomChamp;
			
			if( this.id == 'login1' || this.id == 'pass2' ){
				texte = nomChamp ;
			}
		}
	}else{
		var texte = textInfoBulle+'&nbsp;'+nomChamp;
		if( this.id == 'login1' || this.id == 'pass2' || this.id == 'code'){
			texte = nomChamp ;
		}
		if( this.id == 'code' ){
			texte = $("labelcode").innerHTML ;
		}
		createBulle(this,texte);
	}
	
	$('paraBulleInfo').innerHTML = texte ;
	$('bulleInfo').setStyles({'display':'block',top:hauteur,left:left,zindex:'20'});
}

function gestionMessageAttente(){
	$('boutonsubmit').fade(0);
	$('message_attente').fade(1);
	
	fsubmit.delay(500);
}

function verifChamp(){
	var val           = this.value;
	var id            = this.id;
	var current       = this;
	var idbullealerte = 'alert'+id;
	
	$('bulleInfo').setStyles({'display':'none'});
	
	if( $chk($(idbullealerte)) ){
		$(idbullealerte).destroy();
	}
	
	if( id == 'tel2'){	
			if( $chk($('alerttel2')) ){
				$('alerttel2').destroy();
			}
			
			new Request({url: url_envoi,
					method: 'post',
					async:false,
					onSuccess:function(txt){
						if( txt != 'ok'){
							createBulle(current,txt);
							submitform =  true;
						}else{
							submitform =  false;
						}
						
			}}).send('id='+id+'&val='+val+'&pays=FR');
	}else if( id == 'annee'){
		verifdatenaissance(this);
	}else if( id == 'pass2'){
		var pass1 = $('pass1').value;
		
		new Request({url: url_envoi,
				method: 'post',
				async:false,
				onSuccess:function(txt){
					if( txt != 'ok'){
						createBulle(current,txt);
						submitform =  true;
					}else{
						submitform =  false;
					}
		}}).send('id='+id+'&val='+val+'&pass1='+pass1);
		
	}else if(id == 'pseudo'){
		verifLoginPseudoDiff();
	}else{
		new Request({url: url_envoi,
				method: 'post',
				async:false,
				onSuccess:function(txt){
					if( txt != 'ok'){
						createBulle(current,txt);
						submitform =  true;
					}else{
						submitform =  false;
					}
					
		}}).send('id='+id+'&val='+val);
	}
}

function fsubmit(){ 	
	
	if ( verifForm() ){
		document.getElementById('idForm').submit();
	} else {
		$('boutonsubmit').fade(1);
		$('message_attente').fade(0);
	}
} 

function verifForm(){      
	verifcivil.attempt(0,$('CivilMr')); 
	verifChamp.attempt(0,$('Nom'));
	verifChamp.attempt(0,$('Pnom'));
	verifChamp.attempt(0,$('Adr'));
	verifChamp.attempt(0,$('CodeP'));
	verifChamp.attempt(0,$('Ville'));
	verifChamp.attempt(0,$('annee'));  
	verifChamp.attempt(0,$('email'));
	verifChamp.attempt(0,$('login1'));
	verifChamp.attempt(0,$('pass1'));
	verifChamp.attempt(0,$('pass2'));    
	VerifReglement.attempt(0,$('regle'));
	verifChamp.attempt(0,$('code'));  
	verifChamp.attempt(0,$('pseudo'));
	verifChamp.attempt(0,$('tel2'));
	
	if( $chk($('alertNom')) ||
	$chk($('alertPnom')) ||
	$chk($('alertAdr')) ||
	$chk($('alertCodeP')) ||
	$chk($('alertVille')) ||
	$chk($('alertemail')) ||
	$chk($('alertlogin1')) ||
	$chk($('alertpass1')) ||
	$chk($('alertpass2')) ||
	$chk($('alertregle')) ||
	$chk($('alertCivilMr')) ||
	$chk($('alertpseudo')) ||
	$chk($('alerttel2')) ||
	$chk($('alertcode')) 
	){ 
		return false;
	}else{
		return true;
	}
}

function essaiResizeWindow(){
	createBulleAide.attempt(0,objCible);
}

function init(){
	$('boutonsubmit').addEvent('click',gestionMessageAttente);
	$('message_attente').setStyle('opacity','0');
	textInfoBulle = $('paraBulleInfo').innerHTML;
	
	/******* Initialisation des action onBlur **********/
	$('Nom').addEvent('blur',verifChamp.bind($('Nom')));
	$('Pnom').addEvent('blur',verifChamp.bind($('Pnom')));
	$('Adr').addEvent('blur',verifChamp.bind($('Adr')));
	$('CodeP').addEvent('blur',verifChamp.bind($('CodeP')));
	$('Ville').addEvent('blur',verifChamp.bind($('Ville')));
	$('email').addEvent('blur',verifChamp.bind($('email')));
	$('login1').addEvent('blur',verifChamp.bind($('login1')));
	$('pass1').addEvent('blur',verifChamp.bind($('pass1')));
	$('pass2').addEvent('blur',verifChamp.bind($('pass2')));
	$('regle').addEvent('click',VerifReglement.bind($('regle')));
	$('code').addEvent('blur',verifChamp.bind($('code')));
	$('tel2').addEvent('blur',verifChamp.bind($('tel2')));
	
	$('annee').addEvent('change',verifChamp.bind($('annee')));
	$('jour').addEvent('change',verifChamp.bind($('annee')));
	$('mois').addEvent('change',verifChamp.bind($('annee')));
	
	$('pseudo').addEvent('blur',verifChamp.bind($('pseudo')));
	
	/****************** Bulle d'aide sur les focus *******/
	$('Nom').addEvent('focus',createBulleAide.bind($('Nom')));
	$('Pnom').addEvent('focus',createBulleAide.bind($('Pnom')));
	$('CodeP').addEvent('focus',createBulleAide.bind($('CodeP')));
	$('Adr').addEvent('focus',createBulleAide.bind($('Adr')));
	$('Ville').addEvent('focus',createBulleAide.bind($('Ville')));
	$('email').addEvent('focus',createBulleAide.bind($('email')));
	$('pass1').addEvent('focus',createBulleAide.bind($('pass1')));
	$('pass2').addEvent('focus',createBulleAide.bind($('pass2')));
	$('code').addEvent('focus',createBulleAide.bind($('code')));
	$('login1').addEvent('focus',createBulleAide.bind($('login1')));
	$('jour').addEvent('focus',createBulleAide.bind($('jour')));
	$('tel2').addEvent('focus',createBulleAide.bind($('tel2')));
	
	$('CivilMlle').addEvent('click',destroyimagecivil);
	$('CivilMme').addEvent('click',destroyimagecivil);
	$('CivilMr').addEvent('click',destroyimagecivil);
	
	$('pseudo').addEvent('focus',createBulleAide.bind($('pseudo')));
	
	window.addEvent('resize',essaiResizeWindow);
}	
window.addEvent('domready',init);
