this.tooltip = function(){

	$('.inputL, .inputSmallL').live('mouseenter', function(e){
		if (!$(this).find('img').hasClass('error')) return false;

		this.elmt = $(this).find('img.error');
		this.t = this.elmt .attr('alt');

		if((this.t == '') || this.t == undefined ) return false;
		
		this.elmt.attr('alt', '');

		$(this).children().append('<div id="tooltip" ><div class="tooltipInner"><div class="tooltipContent"><p>'+ l10n.text.error +'</p><span></span></div></div></div>');
		
		$('#tooltip span').append(this.t);
		
		$("#tooltip").show();
		
		$("#tooltip").css("left", $(this).width() -18);
		
	}).live('mouseleave', function(e){
		if((this.t == '') || this.t == undefined ) return false;
		else this.elmt.attr('alt', this.t);
		$('#tooltip span').text('');
		$('#tooltip').remove();
	});	

};

this.tooltipInfo = function(){

	$('.help').live('mouseenter', function(e){
			$(this).parent().append('<div id="tooltip" ><div class="tooltipInner"><div class="tooltipContent"><p>'+ l10n.text.bulleTitle +'</p><span>'+ l10n.text.bulle +'</span></div></div></div>');
			$("#tooltip").show();
			$("#tooltip").css('left',($(this).position().left+3)+'px');
			$("#tooltip").css('bottom',($(this).position().top+20)+'px');
		}).live('mouseleave', function(e){
			$('#tooltip').remove();
	});	
};


var regExpEmail = /^\s*[\w-]+(\.[\w-]+)*@([\w-]+\.)+[A-Za-z]{2,7}\s*$/;

function fieldIsNotEmpty (elmt) {
	var validity = true;
	if (elmt.value == '') validity = false;
	if (!validity) addErrorPicto(elmt, l10n.required.textEmpty, 'error');
	return validity;
}

function verifEmail (elmt){
	var validity = true;
    if (!regExpEmail.test(elmt.value)) validity = false;
    if (!validity) addErrorPicto(elmt, l10n.error.email, 'error');
    else addErrorPicto(elmt, l10n.text.imgAlt, 'valid');
    return validity;
}

function verifLength (elmt, max) {
	var validity = true;
	 if (elmt.value.length > max) validity = false;
	 return validity;
}

function verifLengthEqual (elmt, nb) {
	var validity = true;
	 if (elmt.value.length != nb) validity = false;
	 return validity;
}

function addErrorPicto (elmt, msg, result) {
	var errorImg;
	$elmtparent = $("#"+elmt.id).parent();
	if ($elmtparent.hasClass('inputR')) errorImg = '<img id="'+elmt.id+'FormError" class="msgError '+result+'" alt="'+msg+'" src="'+l10n.picto[result]+'\.png" />';
	else errorImg = '<img id="'+elmt.id+'FormError" class="msgError '+result+'" alt="'+msg+'" src="'+l10n.picto[result]+'-small\.png" />';	
	if ($elmtparent.children('img').attr('alt') != msg) {
		$elmtparent.children('img').remove();
		$($elmtparent).append(errorImg);
	}
}

function verifCompare (elmt1, elmt2) {
	var validity = true;
	if (elmt1.value != elmt2.value) validity = false;
	return validity;
}

//GFI BEGIN
function verifCheckBox (val)
{
	return document.getElementById(val).checked;
}

function verifRadioBox (val)
{
	return document.getElementById(val).checked;
}
//GFI END

function verifLoginInForm () {
	
	if (!before_verifLoginInForm ()) return;
		
	var frm = document.forms['loginInForm'];

	var email = frm.elements['email'];
	var password = frm.elements['password'];
	
	var validityEmail = fieldIsNotEmpty(email);
	var validityPassword = fieldIsNotEmpty(password);
	
	if (validityPassword) addErrorPicto(password, l10n.text.imgAlt, 'valid');
	
	if (validityEmail) validityEmail = verifEmail(email);
	
	if ((validityEmail == false) || (validityPassword == false)) return false;
	else return true;

	if (!after_verifLoginInForm ()) return;
}

function verifRegisterInForm () {
	
	if (!before_verifRegisterInForm ()) return;
	
	var frm = document.forms['registerInForm'];
	
	var email = frm.elements['email'];
	var password = frm.elements['password'];
	var passwordConfirm = frm.elements['passwordConfirm'];
	var textCaptcha = frm.elements['textCaptcha'];

	var validityEmail = fieldIsNotEmpty(email);
	var validityPassword = fieldIsNotEmpty(password);
	var validityPasswordConfirm = fieldIsNotEmpty(passwordConfirm);
	var validityTextCaptcha = fieldIsNotEmpty(textCaptcha);
	
	var validityCheckBox = verifCheckBox('acceptConditions');
	if (!validityCheckBox) popin('#cguNok');
	
	if (validityPassword) addErrorPicto(password, l10n.text.imgAlt, 'valid');
	if (validityTextCaptcha) addErrorPicto(textCaptcha, l10n.text.imgAlt, 'valid');
	
	if (validityPassword) validityPassword = verifCompare(password, passwordConfirm);
	if (!validityPassword) addErrorPicto(passwordConfirm, l10n.error.passwordConfirm, 'error');
	else addErrorPicto(passwordConfirm, l10n.text.imgAlt, 'valid');

	if (validityEmail) {
		validityEmail = verifEmail(email);
		if (validityEmail) {
			validityEmail = verifLength(email, l10n.number.mail);
			if (!validityEmail) {
				addErrorPicto(email, l10n.compareLongueur(l10n.number.mail), 'error');
			}
		}
	}	

	if ((validityEmail == false) || (validityPassword == false) || (validityPasswordConfirm == false) || (validityTextCaptcha == false) || (validityCheckBox == false)) return false;
	else return true;
	
	if (!after_verifRegisterInForm ()) return;
}

function verifEditInfosPersosForm () {
	
	if (!before_verifEditInfosPersosForm ()) return;
	
	var frm = document.forms['editInfosPersosForm'];

	var lastName = frm.elements['lastName'];
	var name = frm.elements['name'];
	var zip = frm.elements['zip'];
	
	var validityLastName = fieldIsNotEmpty(lastName);
	var validityName = fieldIsNotEmpty(name);
	var validityZip = fieldIsNotEmpty(zip);
	
	if (validityLastName) addErrorPicto(lastName, l10n.text.imgAlt, 'valid');
	if (validityName) addErrorPicto(name, l10n.text.imgAlt, 'valid');
	if (validityZip) addErrorPicto(zip, l10n.text.imgAlt, 'valid');
	
	if ((validityName == false) || (validityZip == false)) return false;
	else return true;
	
	if (!after_verifEditInfosPersosForm ()) return;
}

function verifEditTransportForm () {
	
	if (!before_verifEditTransportForm ()) return;
	
	var frm = document.forms['editTransportForm'];
	
	// var validityCheckBox = verifCheckBox('choiceTransport');

	// if (validityCheckBox) document.location.href = l10n.url.checkboxTransportTrue;
	// else document.location.href = l10n.url.checkboxTransportFalse;
	// if (validityCheckBox == false) {
	// 	return false;
	// }
	// else {
	// 	return true;
	// }
	
	if (!after_verifEditTransportForm ()) return;
}

function verifEditHeatingModeForm () {
	
	if (!before_verifEditHeatingModeForm ()) return;
	
	var frm = document.forms['editInfosHeatingForm'];
	
	var validityRadioBox = verifRadioBox('choiceHeating');

	if (validityRadioBox == false) {
		if (!$('p.error').length ) $('#editHeatingModeForm fieldset').append('<p class="error">'+ l10n.error.heatingMode +'</p>');	
		return false;
	} else {
		return true;
	}
	
	if (!after_verifEditHeatingModeForm ()) return;
}

function verifEditInfosCardForm () {
	
	if (!before_verifEditInfosCardForm ()) return;
	
	var frm = document.forms['editInfosCardForm'];
	var validityNumberCardClub = true, validityCodeCardClub = true;
	var cardClub = frm.elements['choiceCheckCard1'];
	var numberCardClub = frm.elements['numberCardClub'];
	var codeCardClub = frm.elements['codeCardClub'];
	
	if ($(cardClub).is(':checked')) {
		if (numberCardClub.value != '') {
			if(isFinite(numberCardClub.value)) {
				validityNb = verifLength(numberCardClub, l10n.number.numberCardClub);
				if (!validityNb) {
					addErrorPicto(numberCardClub, l10n.compareLongueur(l10n.number.numberCardClub), 'error');
					validityNumberCardClub = false;
				}
				else {
					addErrorPicto(numberCardClub, l10n.text.imgAlt, 'valid');
				}
			}
			else {
				addErrorPicto(numberCardClub, l10n.error.numberCardClub, 'error');
				validityNumberCardClub = false;
			}
			
			var validityCodeCardClub = fieldIsNotEmpty(codeCardClub);
		
			if (validityCodeCardClub) {
				if(isFinite(codeCardClub.value)) {
					validityNb = verifLengthEqual(codeCardClub, l10n.number.codeCardClub);
					if (!validityNb) {
						addErrorPicto(codeCardClub, l10n.getLongueur(l10n.number.codeCardClub), 'error');
						validityCodeCardClub = false;
					}
					else {
						addErrorPicto(codeCardClub, l10n.text.imgAlt, 'valid');
					}
				}
				else {
					addErrorPicto(codeCardClub, l10n.error.codeCardClub, 'error');
					validityCodeCardClub = false;
				}
			}
			else {
				validityCodeCardClub = false;
			}
		}
	}
	
	if ((validityNumberCardClub == false) || (validityCodeCardClub == false)) return false;
	else return true;
	
	if (!after_verifEditInfosCardForm ()) return;
}

function verifEditHeatingEnergyForm () {
	
	if (!before_verifEditHeatingEnergyForm ()) return;
	
	var frm = document.forms['editHeatingEnergyForm'];
	
	var validityCheckBox = verifCheckBox('choiceEnergy');
	
	if (validityCheckBox == false) {
		if (!$('p.error').length ) $('#editHeatingEnergyForm fieldset').append('<p class="error">'+ l10n.error.heatingEnergy +'</p>');	
		return false;
	} else {
		return true;
	}
	
	if (!after_verifEditHeatingEnergyForm ()) return;
}

function verifPswForgetForm () {
	
	if (!before_verifPswForgetForm ()) return;
	
	var frm = document.forms['psdForgetForm'];
	
	var email = frm.elements['pswForget'];
	
	var validityEmail = fieldIsNotEmpty(email);
	
	if (validityEmail) validityEmail = verifEmail(email);
	
	if (validityEmail == false) return false;
	else return true;
	
	if (!after_verifPswForgetForm ()) return;
}


var treatment = {
	unchecked : function(input, picto, label) {
		input.attr('checked', false);
        picto.removeClass('checked');
        label.removeClass('on-checked');
	},	
    checked : function(input, picto, label) {
    	input.attr('checked', true);
        picto.addClass('checked');
        label.addClass('on-checked');
    },
    checkHideOnShow : function(element, display) {
		if (element.parent().hasClass('checkHideOnShow')) {
	    	$elemt = element.parent().next();
	    	$elemt.css('display', display);
    	}
    },
    checkCardNone : function (element) {
		if (element.parent().hasClass('checkCard')) {
			$elemt = element.parent().next();
			$elemt.find('label').css('color', '#e5901f');
			$elemt.find('.inputSmallL').addClass('opacity');
			$elemt.find('input').attr("disabled", "disabled");
		}
    },
    checkCardDisplay : function (element) {
		if (element.parent().hasClass('checkCard')) {
			$elemt = element.parent().next();
			$elemt.find('label').css('color', '#7f5311');
			$elemt.find('.inputSmallL').removeClass('opacity');
			$elemt.find('input').attr("disabled", "");
		}
    }
}
		
function myTotalSkinCheckbox() {
	if ($('.checkbox').length) {
		
		//click on checkbox
		$('.mytotal-picto-check').click(function() {
			var input = $(':checkbox', $(this).parent());
			picto = $(this);
			var label = $('label', $(this).parent());

			if (input.is(':checked')) {
		        treatment.unchecked(input, picto, label);
		        treatment.checkHideOnShow(picto, 'none');
		        treatment.checkCardNone(picto);

		    } else {
		        treatment.checked(input, picto, label);
		        treatment.checkHideOnShow(picto, 'block');
		        treatment.checkCardDisplay(picto);
		    }
		});
		
		//click on label
		$('.mytotal-label-check').click(function(e) {
			e.preventDefault();
			var input = $(':checkbox', $(this).parent());
		    var picto = $('.mytotal-picto-check', $(this).parent());
		    label = $(this);
		    
		    if (input.is(':checked')) {
	    		treatment.unchecked(input, picto, label);
	    		treatment.checkHideOnShow(label, 'none');
	    		treatment.checkCardNone(label);
		    } else {
		        treatment.checked(input, picto, label);
		        treatment.checkHideOnShow(label, 'block');
		        treatment.checkCardDisplay(label);
		    }
		});
   }
}

function skinRadioBox () {
	$('.radio-group label').click(function(){
		var thisToggle = $(this).prev();
		var thisToggleLabel = $(this);
		var checkBox = thisToggle.prev();
		checkBox.trigger('click');
		$('.picto_radiobox').removeClass('checked');
		thisToggle.addClass('checked');
		$('.radio-group label').removeClass('on-checked');
		thisToggleLabel.addClass('on-checked');
		return false;
	});
}

this.popin = function(id) {
	
	if (!before_popin ()) return;
	
	if (!$('#overlay').length) $("body").append('<div id="overlay"></div><div class="coreMiniPopin"><div class="coreMiniPopinInner"><div class="coreMiniPopinContent"><span class="close">close</span></div></div></div>');
	
	$('#overlay').css('display','block');
	$($(id)).appendTo(".coreMiniPopinContent").css('display', 'block');
	$(".coreMiniPopin").css('display', 'block');
	
	$(".coreMiniPopin").css({
		top : ($(window).height() - $(".coreMiniPopinInner").height()) / 2,
		left: ($(window).width() - $(".coreMiniPopinInner").width()) / 2
	});

	/* fermeture popin */
	$('.coreMiniPopin .close').live('click',function(){
		$($(this).next()).appendTo("body").css('display','none');
		$("#overlay, .coreMiniPopin").css({display: 'none', top:0, left:0});
	});
	
	if (!after_popin ()) return;
}

function popupLegals (page, largeur, hauteur, options) {
	var top=(screen.height-hauteur)/2;
	var left=(screen.width-largeur)/2;
	window.open(page,"","top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);
}

function popupStation(path){
		window.open	(path, 'TOTAL','width=662px,height=700px,scrollbars=yes');
		return false;
}


$(function() {
	
	tooltip();
	tooltipInfo();
	
	myTotalSkinCheckbox();
	skinRadioBox();
	
	if ($('.civility').length) $('.civility').selectbox();
	if ($('.supplierOil').length) $('.supplierOil').selectbox();
	if ($('.providerGazTank').length) $('.providerGazTank').selectbox();
	
	$('.energy .editHeatingEnergy .field .hide').css('display', 'none');
	
	$(".openProfil").fancybox({
		'height'			: 540,
		'width' 			: 859,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'margin'			: 0,
		'padding'			: 0,
		'scrolling'			: 'no'
	});
	
	$('#header .loggedIn .wrapLog').live('mouseenter', function(e){
		$('.loggedIn #logIn').show();
	}).live('mouseleave', function(e){
		$('.loggedIn #logIn').hide();
	});
	
	$('.psdForget').click(function(e) {
		e.preventDefault();
		($(this).attr('href'));
		popin($(this).attr('href'));
		
	})
	
	$('input:text, input:password').live('click',function() {
			// GFI3 BEGIN 
	//if (!$(this).next('img').hasClass('help')) {
		if (!$(this).next('img').hasClass('help')&&!$(this).next('img').hasClass('dojoComboBox')) {
			// GFI3 END
		
			$(this).next('img').remove();	
			$('#tooltip').remove();
		}
	});
	
	//popin('#psdForgetPopinTrue'); //Popin de confirmation d'envoi de password perdu
	//popin('#psdForgetPopinFalse'); //Popin d'erreur d'envoi de password perdu
	
});
