checkClubRegister = Class.create();
checkClubRegister.prototype = {
			
	initialize: function () {
		
		if ($('validClub')) Event.observe($('validClub'),"click",this.check.bind(this,true));		
		if ($('jourNaissanceClub')) {
			new Aheuh.SelectHtml({
				selectHtml:"jourNaissanceClub", selectWrite:"jourNaissanceSelectClub"
			}).init();
		}		
		if ($('moisNaissanceClub')) {
			new Aheuh.SelectHtml({
				selectHtml:"moisNaissanceClub", selectWrite:"moisNaissanceSelectClub"
			}).init();
		}		
		if ($('anneeNaissanceClub')) {
			new Aheuh.SelectHtml({
				selectHtml:"anneeNaissanceClub", selectWrite:"anneeNaissanceSelectClub"
			}).init();
		}
		
		if ( $('LSFoffers') ) {
			
			var check_date = new Date();
			check_time = check_date.getTime();
			
			var self = this;
			setTimeout( function( ) { self.check_LSF_offers_data(); }, retry_delay );
		}
        
	},
	
	check_LSF_offers_data: function(){
		
		var d1 = new Date(); var t1 = d1.getTime();
		if (window.get_LSF_offers_data || (t1 - check_time) > 4000) {
			
			var offers_data = get_LSF_offers_data();
			$('LSFoffers').innerHTML = offers_data;
			
			//# Encapsulation des inputs par des divs pour marquer les erreurs
			var oDiv = document.createElement("div");
			oDiv.innerHTML = offers_data;
			var inputs = oDiv.getElementsByTagName("input");			
			for ( i=0; i<inputs.length; i++ ) {
				input_id 		= inputs[i].id;
				input_id_length = input_id.length;
				
				divElt = document.createElement("div");
				divElt.setAttribute( 'id', input_id + '_div' );
				divElt.setAttribute( 'class', 'inputDiv' );
				$(input_id).wrap( divElt );
			}
			
			if (myPopHtmlClub) myPopHtmlClub._onWindowResize();
		}
		else {
			var self = this;
			setTimeout( function( ) { self.check_LSF_offers_data(); }, retry_delay );
		}
	},
	
	check: function(){
		
		var error           = false;
		var errorConfirm    = false;
		var errorCheck      = false;
		var errorLSF 		= false;
		
		if($F('sf_guard_user_last_name') == ""){
			Element.addClassName($('sf_guard_user_last_name'),"inpError");
			error = true;
		}
		else Element.removeClassName($('sf_guard_user_last_name'),"inpError");
		
		if($F('sf_guard_user_first_name') == ""){
			Element.addClassName($('sf_guard_user_first_name'),"inpError");
			error = true;
		}
		else Element.removeClassName($('sf_guard_user_first_name'),"inpError");
		
		if($F('sf_guard_user_email_client') == "" || !this.checkEmail($F('sf_guard_user_email_client'))){
			Element.addClassName($('sf_guard_user_email_client'),"inpError");
			error = true;
		}
		else Element.removeClassName($('sf_guard_user_email_client'),"inpError");
		
		if($F('sf_guard_user_username') == ""){
			Element.addClassName($('sf_guard_user_username'),"inpError");
			error = true;
		}
		else Element.removeClassName($('sf_guard_user_username'),"inpError");
		
		if($F('sf_guard_user_password') == ""){
			Element.addClassName($('sf_guard_user_password'),"inpError");
			error = true;
		}
		else Element.removeClassName($('sf_guard_user_password'),"inpError");
		
		if($F('sf_guard_user_password_again') == ""){
			Element.addClassName($('sf_guard_user_password_again'),"inpError");
			error = true;
		}
		else if ($F('sf_guard_user_password_again') != $F('sf_guard_user_password')) {
		    Element.addClassName($('sf_guard_user_password_again'),"inpError");
		    errorConfirm = true;
		}
		else Element.removeClassName($('sf_guard_user_password_again'),"inpError");
		
		if($('sf_guard_user_optin_tmc_cgv').checked == false){
			Element.addClassName($('TmcCgv'),"inpError");
			errorCheck = true;
		}
		else Element.removeClassName($('TmcCgv'),"inpError");
		
		//# Radio buttons checked
		if ( $('LSFoffers') ) {
			var oDiv = document.createElement("div");
			oDiv.innerHTML = $('LSFoffers').innerHTML;
			
			var inputs = oDiv.getElementsByTagName("input");
			
			for ( i=0; i<inputs.length; i++ ) {
				input_id 		= inputs[i].id;
				input_id_length = input_id.length;
				
				lastChar = input_id.substring(input_id_length - 1, input_id_length);
				if ( lastChar == 'y' ) otherChar = 'n';
				else if ( lastChar == 'n' ) otherChar = 'y';
				other_input_id = input_id.substring(0, input_id_length - 1) + otherChar;
				
				if ( $(input_id).checked == false && $(other_input_id).checked == false ) {
					Element.addClassName($(input_id + '_div'),"inpError");
					errorLSF = true;
				}
				else {
					Element.removeClassName($(input_id + '_div'),"inpError");
				}
			}
		}
		
		if(error) { 
		    $('errorFormClub').innerHTML = "Certains champs du formulaire sont incorrects.";
		} else if (errorConfirm) {
		    $('errorFormClub').innerHTML = "La confirmation de votre mot de passe est incorrecte.";
		} else if (errorCheck) {
		    $('errorFormClub').innerHTML = "Vous devez accepter la charte d'utilisation du club TMC";
		} else if (errorLSF) {
		    $('errorFormClub').innerHTML = "Vous devez nous indiquer (par 'oui' ou 'non'), si vous souhaitez profiter des propositions de nos partenaires.";
		} else {
			$('errorFormClub').innerHTML = "";
			this.send();
		}
	},
	
	send: function () {
		f = $('clubRegisterForm');
		
		var newAJAX = new Ajax.Request(
			f.action, {
			    method: 'POST',
			    postBody: f.serialize(),
			    onComplete: function(response) {
					if ( response.status == 200 ) {
						if (window.LSF_submit) LSF_submit();
					}
			        $("PopHtmlContent").innerHTML = response.responseText;
			        this.initialize();
			    } .bind(this)
			}
		);
	},
	
	
	checkEmail: function (email) {
		var reg = /^[A-Za-z0-9._-]+@[A-Za-z0-9.-]{2,}[.][A-Za-z]{2,3}$/;
  		return (reg.exec(email)!=null);
	}	
}

var myCheckClubRegister;
var retry_delay = 100, check_time;

window.LSF_send_data = function () {
	
	var form 	= document.LSF_form;
	var so 		= '';
    var nso 	= '';
	if (!form || !form.elements) { return true; }
	var params 	= '';

	if (form.LSFr_o1708[0].checked) { so += '1708,'; } else if (form.LSFr_o1708[1].checked) { nso += '1708,'; }
	if (form.LSFr_o1521[0].checked) { so += '1521,'; } else if (form.LSFr_o1521[1].checked) { nso += '1521,'; }

	nso = nso.substring(0, nso.length-1);
	params += '&nso=' + nso;
	so = so.substring(0, so.length-1);
	params += '&so=' + so;
		params += LSF_collect_data();
		LSF_submit_image.src = 'http://coreg.1x12.net?act=submitoffers&pubid=3031' + params;

	return true;	
}