/*Global declaration*/
var m_FieldValidated = new Array();

var m_NumberOfField = 0;

/* Used on the submit of the form to verify that there is no error */

function asError()
{	
	var m_asError = false;
	if (isEmpty(document.frmInscription.username.value))
	{		
		document.getElementById("usernameMessage").innerHTML = MSG_USERNAME_LENGTH;
		m_asError = true;			
	}
	else if (trim(document.frmInscription.username.value).indexOf(" ") >= 0)
	{
		document.getElementById("usernameMessage").innerHTML = MSG_USERNAME_FORMAT;
		m_asError = true;
	}
	
	if (isEmpty(document.frmInscription.password.value))
	{
		document.getElementById("passwordMessage").innerHTML = MSG_PASSWORD_LENGTH;
		m_asError = true;
	}
	else if (trim(document.frmInscription.password.value).indexOf(" ") >= 0)
	{
		document.getElementById("passwordMessage").innerHTML = MSG_PASSWORD_FORMAT;
		m_asError = true;
	}		
	
	if (isEmpty(document.frmInscription.confirmpassword.value))
	{
		document.getElementById("passwordConfirmMessage").innerHTML = MSG_PASSWORD_CONFIRM_EMPTY;
		m_asError = true;
	}
	else if (trim(document.frmInscription.confirmpassword.value) != trim(document.frmInscription.password.value))
	{
		document.getElementById("passwordConfirmMessage").innerHTML = MSG_PASSWORD_CONFIRM_WRONG;
		m_asError = true;
	}		
			
	return !m_asError
}

/* Used to check if username is Message */

function checkAvailability()
{
	var param = new Array();
	param["username"] = document.frmInscription.username.value
	param["lang"] = document.frmInscription.lang.value;		
	var bindArgs = {
		content:		param,
  	url:        "/checkIfUsernameAvailable.asp",
  	mimetype:   "text/html",
  	error:      function(type, errObj){
      errorFound(type, errObj);
  	},
  	load:      function(type, data, evt){
      userAvailability(type, data, evt);
  	}
	};
	var requestObj = dojo.io.bind(bindArgs);
}

/* Used to received data from checkAvailability */

function userAvailability(type, data, evt)
{
	var m_asError = false;
	if (isEmpty(document.frmInscription.username.value))
	{		
		document.getElementById("usernameMessage").innerHTML = MSG_USERNAME_LENGTH;
		m_asError = true;			
	}
	else if (trim(document.frmInscription.username.value).indexOf(" ") >= 0)
	{
		document.getElementById("usernameMessage").innerHTML = MSG_USERNAME_FORMAT;
		m_asError = true;
	}
	
	if (!m_asError)
	{
		document.getElementById("usernameMessage").innerHTML = data;
		document.getElementById("usernameMessage").className = ""
	}	
}

/* Used to validate username format and length */

function validateUsername()
{		
	var param = new Array();
	param["username"] = document.frmInscription.username.value
	param["lang"] = document.frmInscription.lang.value;	
	var bindArgs = {
		content:		param,
  	url:        "/validate_username.asp",
  	mimetype:   "text/html",
  	error:      function(type, errObj){
      errorFound(type, errObj);
  	},
  	load:      function(type, data, evt){
      userValidated(type, data, evt);
  	}
	};
	var requestObj = dojo.io.bind(bindArgs);
}

/* Used to received data from validateUsername */

function userValidated(type, data, evt)
{				
	document.getElementById("usernameMessage").innerHTML = data;
	document.getElementById("usernameMessage").className = "errorMessage"	
}

/* Used to validate password format and length */

function validatePassword()
{		
	var param = new Array();
	param["password"] = document.frmInscription.password.value
	param["lang"] = document.frmInscription.lang.value;	
	var bindArgs = {
		content:		param,
  	url:        "/validate_password.asp",
  	mimetype:   "text/html",
  	error:      function(type, errObj){
      errorFound(type, errObj);
  	},
  	load:      function(type, data, evt){
      passwordValidated(type, data, evt);
  	}
	};
	var requestObj = dojo.io.bind(bindArgs);
}

/* Used to received data from validatePassword */

function passwordValidated(type, data, evt)
{				
	document.getElementById("passwordMessage").innerHTML = data;
	document.getElementById("passwordMessage").className = "errorMessage"	
}

/* Used to validate confirm password format and length */

function validateConfirmPassword()
{		
	var param = new Array();
	param["password"] = document.frmInscription.password.value
	param["confirmpassword"] = document.frmInscription.confirmpassword.value
	param["lang"] = document.frmInscription.lang.value;	
	var bindArgs = {
		content:		param,
  	url:        "/validate_confirm_password.asp",
  	mimetype:   "text/html",
  	error:      function(type, errObj){
      errorFound(type, errObj);
  	},
  	load:      function(type, data, evt){
      confirmPasswordValidated(type, data, evt);
  	}
	};
	var requestObj = dojo.io.bind(bindArgs);
}

/* Used to received data from validateConfirmPassword */

function confirmPasswordValidated(type, data, evt)
{				
	document.getElementById("passwordConfirmMessage").innerHTML = data;
	document.getElementById("passwordConfirmMessage").className = "errorMessage"	
}

/* Used to validate inscription step 2 */

function validateForm(fieldToValidate)
{					
	var param = new Array();
	
	if(document.getElementById("lang"))
		param["lang"] = document.frmInscription.lang.value
	
	if(document.getElementById("firstname"))
		param["firstName"] = document.frmInscription.firstname.value
		
	if(document.getElementById("lastname"))
		param["lastName"] = document.frmInscription.lastname.value
		
	if(document.getElementById("city"))
		param["city"] = document.frmInscription.city.value;	
	
	if(document.getElementById("province"))
		param["province"] = document.frmInscription.province.value;
		
	if(document.getElementById("pays"))
		param["pays"] = document.frmInscription.pays.value;
	
	if(document.getElementById("phone1"))
		param["phone"] = document.frmInscription.phone1.value + document.frmInscription.phone2.value + document.frmInscription.phone3.value;	
	
	if(document.getElementById("mobilephone1"))
		param["mobile"] = document.frmInscription.mobilephone1.value + document.frmInscription.mobilephone2.value + document.frmInscription.mobilephone3.value;	;
	
	if(document.getElementById("mobile_carrier"))
		param["mobile_carrier"] = document.frmInscription.mobile_carrier.value;
	
	if(document.getElementById("email"))
		param["email"]	= document.frmInscription.email.value;
	
	if(document.getElementById("ChoosenAvatarForServer"))
		param["ChoosenAvatarForServer"] = document.frmInscription.ChoosenAvatarForServer.value
	
	if(document.getElementById("myAvatar"))
		param["myAvatar"] = escape(document.frmInscription.myAvatar.value);		
	
	if(document.getElementById("password"))
		param["password"] = document.getElementById("password").value;
	if(document.getElementById("newPassword"))
		param["newPassword"] = document.getElementById("newPassword").value;
	if(document.getElementById("newPasswordConfirm"))
		param["newPasswordConfirm"] = document.getElementById("newPasswordConfirm").value;
	if(document.getElementById("nomUsager"))
		param["nomUsager"] = document.getElementById("nomUsager").value;
	
	param["fieldToValidate"] = fieldToValidate;
	var bindArgs = {
		content:		param,
  	url:        "/validateStep2.asp",
  	mimetype:   "text/plain",
  	error:      function(type, errObj){
      errorFound(type, errObj);
  	},
  	load: function(type, data, evt)
  	{
  		eval(data);
  		if (!m_FieldValidated["email"])
  			isEmailExist();
  	}  	
	};
	var requestObj = dojo.io.bind(bindArgs);
}

function validateAllProfil()
{
	var param = new Array();
	
	if(document.getElementById("lang"))
		param["lang"] = document.frmInscription.lang.value
	
	if(document.getElementById("firstname"))
		param["firstName"] = document.frmInscription.firstname.value
		
	if(document.getElementById("lastname"))
		param["lastName"] = document.frmInscription.lastname.value
		
	if(document.getElementById("city"))
		param["city"] = document.frmInscription.city.value;	
	
	if(document.getElementById("province"))
		param["province"] = document.frmInscription.province.value;
		
	if(document.getElementById("pays"))
		param["pays"] = document.frmInscription.pays.value;
	
	if(document.getElementById("phone1"))
		param["phone"] = document.frmInscription.phone1.value + document.frmInscription.phone2.value + document.frmInscription.phone3.value;	
	
	if(document.getElementById("mobilephone1"))
		param["mobile"] = document.frmInscription.mobilephone1.value + document.frmInscription.mobilephone2.value + document.frmInscription.mobilephone3.value;	;
	
	if(document.getElementById("mobile_carrier"))
		param["mobile_carrier"] = document.frmInscription.mobile_carrier.value;
	
	if(document.getElementById("email"))
		param["email"]	= document.frmInscription.email.value;
	
	if(document.getElementById("ChoosenAvatarForServer"))
		param["ChoosenAvatarForServer"] = document.frmInscription.ChoosenAvatarForServer.value
	
	if(document.getElementById("myAvatar"))
		param["myAvatar"] = escape(document.frmInscription.myAvatar.value);	
		
	if(document.getElementById("password"))
		param["password"] = document.getElementById("password").value;
	if(document.getElementById("newPassword"))
		param["newPassword"] = document.getElementById("newPassword").value;
	if(document.getElementById("newPasswordConfirm"))
		param["newPasswordConfirm"] = document.getElementById("newPasswordConfirm").value;
	if(document.getElementById("nomUsager"))
		param["nomUsager"] = document.getElementById("nomUsager").value;
		
	var bindArgs = {
		content:		param,
  	url:        "/validateAllProfil.asp",
  	mimetype:   "text/plain",
  	error:      function(type, errObj){
      errorFound(type, errObj);
  	},
  	load: function(type, data, evt)
  	{
  		dojo.byId('validationSummary').style.display="";
  		dojo.byId('validationSummary').innerHTML = data;
  		
  		if(data == "")
  			dojo.byId('frmInscription').submit();
  		else
  			dojo.byId('frmInscription').onSubmit = "return false;"
  	}  	
	};
	var requestObj = dojo.io.bind(bindArgs);
}

function validateAllInscription()
{
	var param = new Array();
	
	if(document.getElementById("lang"))
		param["lang"] = document.frmInscription.lang.value
	
	if(document.getElementById("firstname"))
		param["firstName"] = document.frmInscription.firstname.value
		
	if(document.getElementById("lastname"))
		param["lastName"] = document.frmInscription.lastname.value
		
	if(document.getElementById("city"))
		param["city"] = document.frmInscription.city.value;	
	
	if(document.getElementById("province"))
		param["province"] = document.frmInscription.province.value;
		
	if(document.getElementById("pays"))
		param["pays"] = document.frmInscription.pays.value;
	
	if(document.getElementById("phone1"))
		param["phone"] = document.frmInscription.phone1.value + document.frmInscription.phone2.value + document.frmInscription.phone3.value;	
	
	if(document.getElementById("mobilephone1"))
		param["mobile"] = document.frmInscription.mobilephone1.value + document.frmInscription.mobilephone2.value + document.frmInscription.mobilephone3.value;	;
	
	if(document.getElementById("mobile_carrier"))
		param["mobile_carrier"] = document.frmInscription.mobile_carrier.value;
	
	if(document.getElementById("email"))
		param["email"]	= document.frmInscription.email.value;
	
	if(document.getElementById("ChoosenAvatarForServer"))
		param["ChoosenAvatarForServer"] = document.frmInscription.ChoosenAvatarForServer.value
	
	if(document.getElementById("myAvatar"))
		param["myAvatar"] = escape(document.frmInscription.myAvatar.value);	
		
	var bindArgs = {
		content:		param,
  	url:        "/validateAllInscription.asp",
  	mimetype:   "text/plain",
  	error:      function(type, errObj){
      errorFound(type, errObj);
  	},
  	load: function(type, data, evt)
  	{
  		dojo.byId('validationSummary').style.display="";
  		dojo.byId('validationSummary').innerHTML = data;
  		
  		if(data == "")
  			dojo.byId('frmInscription').submit();
  		else
  			dojo.byId('frmInscription').onSubmit = "return false;"
  	}  	
	};
	var requestObj = dojo.io.bind(bindArgs);
}

/* used if any function return an error */

function isEmailExist()
{
	if (window.location.href.toString().indexOf("inscription_2.asp") != -1)
	{
		var param = new Array();
	
		param["email"] = document.getElementById("email").value;
		
		if (document.getElementById("lang"))
			param["lang"] = document.getElementById("lang").value;
		
		var bindArgs = {
			content:		param,
	  	url:        "/isEmailExist.asp",
	  	mimetype:   "text/plain",
	  	error:      function(type, errObj){
	      errorFound(type, errObj);
	  	},
	  	load: function(type, data, evt)
	  	{
	  		eval(data);	  		
	  	}  	
		};
		var requestObj = dojo.io.bind(bindArgs);
	}
}

function errorFound(type, errObj)
{
	alert(errObj.message);
	return;
}

/*Used to change avatar.*/
function changeAvatar(avatarCliqued)
{				
		for (i = 1; i <= 8; i++)
		{
			document.getElementById("avatar" + i).src = "/imgPhoto/avatar/avatar" + i + "_gris.gif"		
		}
		document.getElementById(avatarCliqued).src = "/imgPhoto/avatar/" + avatarCliqued + ".gif"				
		if (avatarCliqued == "avatar8")
		{
			document.getElementById("myAvatar").style.display=""
			//document.getElementById("ChoosenAvatarForServer").value = "";
			document.getElementById("ChoosenAvatarForServer").value = avatarCliqued;
		}
		else
		{
			document.getElementById("myAvatar").style.display = "none";
			document.getElementById("ChoosenAvatarForServer").value = avatarCliqued;
		}				
		validateForm("avatar");
}

function avatarChoosen()
{		
	validateForm("avatar");	
}

/* Used on the submit of the page */


