tboks.webexpert.reg_login={}; 


/**
 * 用户点击Connection开始登录程序
 */
 //2010-03-05 1700
tboks.webexpert.reg_login.goLogin=function()
{
	/*不再使用这个记录
	if($("#HLoginButton").attr("value") == 1)
		return;
	buttonSetter('HLoginButton',1);
	*/
	var account = tboks.webexpert.reg_login.checkLoginAccount();
	var password = tboks.webexpert.reg_login.checkLoginPassword();
	if(account&&password){
		tboks.webexpert.logs.doDurRequestLog();
		tboks.webexpert.popup.loadingShowPopup();
		tboks.webexpert.reg_login.doLogin();
	}else{
		jAlert('Identifiant ou mot de passe invalide. Merci de réessayer.',"Tboks");
		buttonSetter('HLoginButton',0); // Fonction défini nule part ! ><
	}
}
/**
 * 真正开始登录过程
 */
tboks.webexpert.reg_login.doLogin=function()
{
	var url = window.location.href.split('/');
		if (url[3] == "en" || url[3] == "EN")
		{
			var languageID = 1;
		}
		else
		{
			var languageID = 2;
		}

	var origin = $("#HOrigin").attr("value");
	var par = "method=LOGIN&account="+$("#LoginAccount").attr("value")+"&pwd="+$("#LoginPassword").attr("value")+"&ori="+origin+"&languageID="+languageID;
	
	$.ajax({
		url:"/commun/ws/Account.ws.php",
		type:"POST",
		data:par,
		success:tboks.webexpert.reg_login.doLoginResult,
		error:tboks.webexpert.reg_login.onError
	});
}
/**
 * 登录过程返回函数
 * @param {Object} result
 */
tboks.webexpert.reg_login.doLoginResult=function(result){
	var r = $.trim(result);
	if(r.length){
		var rf = r.substring(0,2);
		var rm = r.substring(2);
		switch(rf)
		{
			case "SU":
			document.getElementById("HSessionID").value = rm;
			tboks.webexpert.expert.sendRequest();
			break;
			
			case "FA":
			jAlert(rm,"Tboks");
			tboks.webexpert.popup.loadingClosePopup();
			break;
		}
	}else{
		jAlert("Une erreur est survenue lors de l'envoi de votre demande. Merci de réessayer à nouveau","Tboks");
	}
}

tboks.webexpert.reg_login.checkNom=function()
{
  var nom = $("#registerNom").attr("value");
  var format=/^([a-zA-Z]|\s|\u0027|[-,é,è,à,ê,ç,î,ï,ë]){1,15}$/;

	if (format.exec(nom)) {
		document.getElementById('nomLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/okicon.png\"/>";
		return true;
		}else{
			document.getElementById('nomLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/warning_16.png\" title=\"1-15 caractères alphanumériques\" />";
			return false;
		}
	
}
tboks.webexpert.reg_login.checkPrenom=function()
{
  var nom = $("#registerPrenom").attr("value");
  var format=/^([a-zA-Z]|\s|\u0027|[-,é,è,à,ê,ç,î,ï,ë]){1,15}$/;

	if (format.exec(nom)) {
		document.getElementById('prenomLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/okicon.png\"/>";
		return true;
		}else{
			document.getElementById('prenomLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/warning_16.png\" title=\"1-15 caractères alphanumériques\" />";
			return false;
		}
	
}
/**
 * 用户点击开始注册过程
 */
tboks.webexpert.reg_login.sinscrireCheck=function()
{
    var ca = tboks.webexpert.reg_login.checkAccount();
	var ce = tboks.webexpert.reg_login.checkEmail();
	var cp = tboks.webexpert.reg_login.checkPassword();
	var ct = tboks.webexpert.reg_login.checkTel();
	var cz = tboks.webexpert.reg_login.checkZip();
	
	
	$("#HTboksCaptcha").val($("#captchainput").attr("value"));
	
	///////////////////////////
if(document.getElementById('registerNom'))
{ //alert ("check nom prenom");
		var cNom = tboks.webexpert.reg_login.checkNom();
		var cPrenom = tboks.webexpert.reg_login.checkPrenom();
		if(!document.getElementById('acceptCheck').checked)
	{
		jAlert("Merci de lire et de confirmer que vous acceptez les Conditions Générales d'Utilisation","Tboks");
		
		return
	}
	
	var RegisterCountry =$('#RegisterCountry').val();
	var cCity = tboks.webexpert.reg_login.checkInputCity();

	if (RegisterCountry=="70"){// alert("france");
								if(ca&&ce&&cp&&cz&ct&&cNom&&cPrenom)
											{
												tboks.webexpert.reg_login.newUserRegister();
											}
												else{
												//document.getElementById("HRegisterButton").value = 0;
												jAlert('Merci de renseigner tous les champs.',"Tboks");
												  } 		
							 }
	else {//alert ("not france");
			
					if(ca&&ce&&cp&&cCity&ct&&cNom&&cPrenom)
					{
						tboks.webexpert.reg_login.newUserRegister();
					}else{
						//document.getElementById("HRegisterButton").value = 0;
						jAlert('Merci de renseigner tous les champs.',"Tboks");
					     } 
		}
			
}
else {
				if(!document.getElementById('acceptCheck').checked)
			{
				jAlert("Merci de lire et de confirmer que vous acceptez les Conditions Générales d'Utilisation","Tboks");
				
				return
			}
				
	var RegisterCountry =$('#RegisterCountry').val();
	var cCity = tboks.webexpert.reg_login.checkInputCity();

	if (RegisterCountry=="70"){// alert("france");
	
						if(ca&&ce&&cp&&cz&ct)
						{
							tboks.webexpert.reg_login.newUserRegister();
						}else{
							//document.getElementById("HRegisterButton").value = 0;
							jAlert('Merci de renseigner tous les champs.',"Tboks");
						} 
				}else {
							if(ca&&ce&&cp&&cCity&ct)
							{
								tboks.webexpert.reg_login.newUserRegister();
							}else{
								//document.getElementById("HRegisterButton").value = 0;
								jAlert('Merci de renseigner tous les champs.',"Tboks");
							} 
						}
	  }

}


/**
 * 注册用户过程
 */
tboks.webexpert.reg_login.newUserRegister=function()
{
	var account = $("#registerAccount").attr("value");
	var email = $("#registerEmail").attr("value");
	var password = $("#registerPassword").attr("value");
	var zip = $("#registerZip").attr("value");
	var tel = $("#registerTel").attr("value");
	var country = $("#RegisterCountry").attr("value");
	var tcaptcha = $("#HTboksCaptcha").attr("value");
	var origin = $("#HOrigin").attr("value");

	var url = window.location.href.split('/');
		if (url[3] == "en" || url[3] == "EN")
		{
			var languageID = 1;
		}
		else
		{
			var languageID = 2;
		}
		
		
	if (country=="70")
		 {
			var zip = $("#registerZip").attr("value");
			// var city = $("#cityNameFrance").attr("value"); 
			 var city = ""; 
		 }else{
			 var zip =00000;
			 var city = $("#cityName").attr("value"); 
			 }

	
	var par = "method=REGISTERUSER&&account=" + account + "&&email=" + email + "&&password=" + password + "&&zip=" + zip + "&&city=" + city +"&&country=" + country +"&&tel="+ tel+ "&&tcaptcha=" + tcaptcha + "&&ori=" + origin+"&languageID="+languageID;
	
	
	tboks.webexpert.popup.loadingShowPopup();
	
	tboks.webexpert.logs.doDurRequestLog();
	
	$.ajax({
		url:"/commun/ws/Account.ws.php",
		type:"POST",
		data:par,
		success:tboks.webexpert.reg_login.onNewUserRegisterResult,
		error:tboks.webexpert.reg_login.onError
	});
}
/**
 * 注册用户返回函数
 */
tboks.webexpert.reg_login.onNewUserRegisterResult=function(result)
{
	var r = $.trim(result);
	
	if(r.length){
		var IDuser=r.search('IDuser');
		var user_registered='failed:'+r.substring(2);
		var rf = r.substring(0,2);
		if (IDuser!=-1)
		{
			rm=r.substring(2,IDuser);
			document.getElementById("HIDUser").value=r.substring(IDuser);
		}
		else
		{var rm = r.substring(2);}
		
		switch (rf)
		{
			case "FA":
				tboks.webexpert.popup.loadingClosePopup();
				//alert("Une erreur est survenue lors de l'envoi de votre demande. Merci de réessayer à nouveau");
				break;
			case "FR": //Error Register
				tboks.webexpert.popup.loadingClosePopup();
				jAlert(rm,"Tboks");
				//alert("L'enegistrement d'un compte avec ces données a échoué");
				break;
			case "FC": //Error Confirm
				tboks.webexpert.popup.loadingClosePopup();
				jAlert("\tLa confirmation de votre compte a échoué. \n Veuillez vérifier votre e-mail et confirmez votre compte","Tboks");
				break;
			default:
				document.getElementById("HSessionID").value = rm;
				tboks.webexpert.expert.sendRequest();
		}	
	}else{
		jAlert("Une erreur est survenue lors de l'envoi de votre demande. Merci de réessayer à nouveau","Tboks");
	}
	
}


/**
 * Tboks Captcha获取和设置
 */
tboks.webexpert.reg_login.tboksCaptcha=function()
{
	var url = window.location.href.split('/');
		if (url[3] == "en" || url[3] == "EN")
		{
			var languageID = 1;
		}
		else
		{
			var languageID = 2;
		}	

	var par = "method=CAPTCHA&languageID="+languageID;
	
	$.ajax({
		url:"/commun/ws/Account.ws.php",
		type:"POST",
		data:par,
		success:tboks.webexpert.reg_login.onTboksCaptchaResult,
		error:tboks.webexpert.reg_login.onError
	});
}
tboks.webexpert.reg_login.onTboksCaptchaResult=function(result)
{
	var source = result;
	document.getElementById("captchaIframe").src = source;
}


/**
 * 登录用户名输入合法性检查
 */
tboks.webexpert.reg_login.checkLoginAccount=function(){
	var inputAccount = $("#LoginAccount").attr("value");
	
	var format=/^(\w|\u0027|[-,.]){4,15}$/;
	
	if (format.exec(inputAccount)) {
		//document.getElementById('LoginAccountLabel').innerHTML = "";
		return true;
		}else{
			//document.getElementById('LoginAccountLabel').innerHTML = "1-15 caractères alphanumériques";
			return false;
		}
}
/**
 * 登录密码合法性检查
 */
tboks.webexpert.reg_login.checkLoginPassword=function(){
	var inputPassword = $("#LoginPassword").attr("value");
	
	//var format=/^(\w|\u0027|[é,è,à,ê,ç,î,ï,ë]){6,10}$/;
	var format=/^(.){6,15}$/;
	
	if (format.exec(inputPassword)) {
		//document.getElementById('LoginPasswordLabel').innerHTML = "";
		return true;
		}else{
			//document.getElementById('LoginPasswordLabel').innerHTML = "6-10 caractères alphanumériques";
			return false;
		}
}

//-----------
/**
 * 验证用户名
 */
tboks.webexpert.reg_login.checkAccount=function(){
	var inputAccount = $("#registerAccount").attr("value");
	
	var format=/^(\w|[-,.]){4,15}$/;
	
	if (format.exec(inputAccount)) {
		document.getElementById('accountLabel').innerHTML = "<img src=\"/commun/images/busy.gif\"/>";
		tboks.webexpert.reg_login.checkUniqueValue(document.getElementById('registerAccount'),"Username");
		return true;
		}else{
			document.getElementById('accountLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/warning_16.png\"  title=\"4-15 caractères alphanumériques\" />";;
			return false;
		}
}


/**
 * 验证邮箱
 */
tboks.webexpert.reg_login.checkEmail=function()
{
	
	var inputmailvalue = $("#registerEmail").attr("value");
	
	var format = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

	if (format.exec(inputmailvalue)) {
		document.getElementById('emailLabel').innerHTML = "<img src=\"/commun/images/busy.gif\"/>";
		tboks.webexpert.reg_login.checkUniqueValue(document.getElementById('registerEmail'),"Email");
		return true;
		}else{
			document.getElementById('emailLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/warning_16.png\" title=\"erreur de format d'email\"  />";
			return false;
		}
	
}
/**
 * 验证密码
 */
tboks.webexpert.reg_login.checkPassword=function(){
	var inputPassword = $("#registerPassword").attr("value");
	
	//var format=/^(\w|\u0027|[é,è,à,ê,ç,î,ï,ë]){6,10}$/;
	var format=/^(.){6,15}$/;
	
	if (format.exec(inputPassword)) {
		document.getElementById('passwordLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/okicon.png\"/>";
		return true;
		}else{
			document.getElementById('passwordLabel').innerHTML ="<img src=\"/commun/images/webtboks/icons/warning_16.png\"   title=\"6-15 caractères alphanumériques\" />";
			return false;
		}
}

tboks.webexpert.reg_login.checkInputCity=function(){
	var inputCity = $("#cityName").attr("value");	
	if (inputCity!="") {
		document.getElementById('cityLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/okicon.png\"/>";
		return true;
		}else{
			document.getElementById('cityLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/warning_16.png\"  title=\"5 caractères numériques\" />";
			return false;
		}
}
/**
 * 验证城市,改邮政
 */
tboks.webexpert.reg_login.checkCity=function(){
	var inputCity = $("#registerCity").attr("value");
	var format=/^(\w|\u0027|\s|[-,é,è,à,ê,ç,î,ï,ë]){1,50}$/;
	
	if (format.exec(inputCity)) {
		document.getElementById('cityLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/okicon.png\"/>";
		return true;
		}else{
			document.getElementById('cityLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/warning_16.png\"  title=\"1-50 caractères alphanumériques\" />";
			return false;
		}
}

/**
 * 用户名密码唯一性验证
 */
tboks.webexpert.reg_login.checkUniqueValue=function(thisNode,checkField)
{
	var url = window.location.href.split('/');
		if (url[3] == "en" || url[3] == "EN")
		{
			var languageID = 1;
		}
		else
		{
			var languageID = 2;
		}
	var fieldValue = thisNode.value;
	var fieldName = checkField;
	
	var par = "method=CHECKUNIQUEVALUE&checkUniqueFieldName=" + checkField + "&checkUniqueFieldValue=" + fieldValue+"&languageID="+languageID;
	
	$.ajax({
		url:"/commun/ws/Account.ws.php",
		type:"POST",
		data:par,
		success:tboks.webexpert.reg_login.doCheckUniqueValue,
		error:tboks.webexpert.reg_login.onError
	});
}

tboks.webexpert.reg_login.checkZip=function(){
	var inputZip = $("#registerZip").attr("value");
	var ZipLength= inputZip.length; 
	var format=/[0-9]{5}$/;
	if (format.exec(inputZip)&&(ZipLength==5)&&inputZip!=00000) {
	
		document.getElementById('zipLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/okicon.png\"/>";
		return true;
		}else{
			document.getElementById('zipLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/warning_16.png\" title=\"5 caractères numériques\" />";
			return false;
		}
}

tboks.webexpert.reg_login.checkTel=function(){
			
	//if(document.getElementById('registerTel').value){
	if(document.getElementById('registerTel') != null){


		var inputTel = document.getElementById('registerTel').value;
		var telLength= inputTel.length; 
		var pattern = /^[0][0-9]{9}/;
	
		if( (!isNaN(inputTel)) && (pattern.exec(inputTel)) &&(inputTel != "")&&(telLength==10) ){ 
		 // if( (!isNaN(inputTel)) && (pattern.exec(inputTel)) &&(telLength==10)||(inputTel == "") ){
	
			document.getElementById('telLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/okicon.png\"/>";
			return true;
		}
		else{
			document.getElementById('telLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/warning_16.png\" title=\"10 caractères alphanumériques\" />";
			return false;
		}
	}
}

tboks.webexpert.reg_login.doCheckUniqueValue=function(result)
{
	switch (result)
	{
		case "1Username":
			document.getElementById('accountLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/noicon.png\" onclick=\"jAlert('Pseudo a déjà été pris','Info')\" title=\"Pseudo a déjà été pris\" />";
			break;
		case "0Username":
			document.getElementById('accountLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/okicon.png\"/>";
			break;
		case "1Email":
			document.getElementById('emailLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/noicon.png\" onclick=\"jAlert('Cette email a déjà été enregistrée','Info')\" title=\"Cette email a déjà été enregistrée\" />";
			break;
		case "0Email":
			document.getElementById('emailLabel').innerHTML = "<img src=\"/commun/images/webtboks/icons/okicon.png\"/>";
			break;
	}
}


/**
 * 暂时没有定义Ajax错误的回调函数，目前先用onError代替
 */
tboks.webexpert.reg_login.onError=function(error)
{
	tboks.webexpert.popup.loadingClosePopup();
	jAlert("Une erreur est survenue lors de l'envoi, veuillez ressayer plus tard","Tboks");
}
