
/*****************************************************************************************
************** FONCTIONS DEDIEES A LA LOGIN POUR LA HOME DE GARNIER CLUB *****************
*****************************************************************************************/
function checkemailgarnierclub(emailgarnierclub){

	var at="@";
	var dot=".";
	var lat=emailgarnierclub.indexOf(at);
	var lstr=emailgarnierclub.length;
	var ldot=emailgarnierclub.indexOf(dot);
	if (emailgarnierclub.indexOf(at)==-1){
	   return false;
	}
	if (emailgarnierclub.indexOf(at)==-1 || emailgarnierclub.indexOf(at)==0 || emailgarnierclub.indexOf(at)==lstr){
	   return false;
	}
	if (emailgarnierclub.indexOf(dot)==-1 || emailgarnierclub.indexOf(dot)==0 || emailgarnierclub.indexOf(dot)==lstr){
		return false;
	}
	if (emailgarnierclub.indexOf(at,(lat+1))!=-1){
		return false;
	}
	if (emailgarnierclub.substring(lat-1,lat)==dot || emailgarnierclub.substring(lat+1,lat+2)==dot){
		return false;
	}
	if (emailgarnierclub.indexOf(dot,(lat+2))==-1){
		return false;
	}
	if (emailgarnierclub.indexOf(" ")!=-1){
		return false;
	}
	return true;					
}

function connection_homegarnierclub(url_redirect){
	var loginhomegarnierclub = document.getElementById('login-ajax-home-garnier-club');
	var passwordhomegarnierclub = document.getElementById('password-ajax-home-garnier-club');
	var errormessagehomegarnierclub = document.getElementById('form_errors-home-garnier-club');
	
	if(!checkemailgarnierclub(loginhomegarnierclub.value)){
		loginhomegarnierclub.style.border = '1px darkred solid';
		passwordhomegarnierclub.style.border = '1px darkred solid';
		errormessagehomegarnierclub.innerHTML = "Wrong format email";
		return false;
	}
	
	var customerlogin_homegarnierclub = new Customer(null,false);
	customerlogin_homegarnierclub.CustomerLogin.call(loginhomegarnierclub.value, passwordhomegarnierclub.value);
	// Si appel ajax termine
	$("").ajaxComplete(function(ev, xhr, s) {	
		// Et qu'il s'est bien deroule
		if(xhr.status == 200 && s.url.indexOf("CustomerLogin")>-1){
			// Si le webservice renvoie une erreur
			if(customerlogin_homegarnierclub.CustomerLogin.getError()!=""){	
				loginhomegarnierclub.style.border = '1px darkred solid';
				passwordhomegarnierclub.style.border = '1px darkred solid';
				errormessagehomegarnierclub.innerHTML = errorSignInValidationMessage;
			}else{
				//window.location.replace(window.location);
				if(url_redirect != "" && url_redirect.length > 0){
					if(url_redirect.substring(0,1) == "#" || url_redirect.indexOf("#") >= 1){
						location.reload(true);
					}else{
						window.location.href = url_redirect;
					}
				}else{
					var sURL = unescape(window.location.pathname);
					window.location.href = sURL;
				}
			}
		}
	});
}
