//-----------------------------------------------------------------------------

function ValidateRegForm(loForm)
{
	return ValidateRegForm2(loForm, false)
}
//-----------------------------------------------------------------------------
function ValidateDuplicate(oLoginForm)
{
	var oLoginForm, oPassword, sPassword, oPasswordConfirm, sPasswordConfirm, bValidLogin;
	var sError;
	oLoginForm = (oLoginForm || 'duplicate');
	oLoginForm = GetForm(oLoginForm);
	oPassword = GetField(oLoginForm, 'Password')
	sPassword = FieldValue(oLoginForm, oPassword);
	
	oPasswordConfirm = GetField(oLoginForm, 'ConfirmPassword')
	sPasswordConfirm = FieldValue(oLoginForm, oPasswordConfirm);
	
	if (sPassword.length == 0)
	{
		oPassword.focus();
		sError = 'Please enter a password';
	}
	else
	{
	    if (sPasswordConfirm.length == 0)
	    {
	        oPasswordConfirm.focus();
		    sError = 'Please enter password confirmation';
	    }
	    else if(sPassword!=sPasswordConfirm)
	    {
	        oPassword.focus();
		    sError = 'Password confirmation does not match';
	    }
	}
	
	if (sError)
	{
		bValidLogin = false;
		alert(sError + '.');
	}
	else
	{
		bValidLogin = true;
		document.body.style.cursor = 'wait';
		setTimeout('document.body.style.cursor = \'wait\';', 2000)
	}
	return(bValidLogin);
}
//-----------------------------------------------------------------------------
function populateAddress(loAddressForm, loAddressList)
{
	var strAddressList, arrAddressList, strAddressList, strAddressHtml, iSelected;

	if (NS6)
	{
		elAddress = document.getElementById('elAddress');
	}
	else if (NS4)
	{
		elAddress = document.elAddress;
	}

	loAddressForm = GetForm(loAddressForm);
	loAddressList = GetField(loAddressForm, loAddressList);
	if (loAddressList)
	{
		strAddressList = loAddressList.value;

		arrAddressList = strAddressList.split("|");
		strAddressHtml = arrAddressList.slice(0,2).join("<br>");
		strAddressHtml = strAddressHtml + '<br>' + FieldValue(loAddressForm, 'Town')
			+ '<br>' + FieldValue(loAddressForm, 'Postcode');

		strAddressHtml = strAddressHtml.replace(/(<br>)+/g,'<br>');
		strAddressHtml = '<p class="description">' + strAddressHtml 
				+ '<br><br>';
				
		if (strAddressList != "-1")
		{
			if (NS4)
			{
				//elAddress.document.write(strAddressHtml);
				//elAddress.close();
			}
			else
			{
				elAddress.innerHTML = strAddressHtml;
			}

			loAddressForm.Address1.value = arrAddressList[0]
			loAddressForm.Address2.value = arrAddressList[1];
			loAddressForm.Address3.value = arrAddressList[2];
			loAddressForm.PafStatusID.value = arrAddressList[3];
			loAddressForm.postkey.value = arrAddressList[4];
			loAddressForm.DPS.value = arrAddressList[5]; 
			loAddressForm.StreetNumber.value = arrAddressList[6]; 
		}
		else
		{
			loAddressForm.Address1.value="";
			loAddressForm.Address2.value="";
			loAddressForm.Address3.value="";
			loAddressForm.PafStatusID.value="";
			loAddressForm.postkey.value="";
			loAddressForm.DPS.value = "";
			loAddressForm.StreetNumber.value = ""; 
		}
	} //loAddressList
}
		
//-----------------------------------------------------------------------------

function ValidateEmailSuggested(loForm)
	{
		var sErrors, bErrors;
		try
		{

			loForm = GetForm(loForm);
			sErrors = RequiredField(loForm, "NameTitle", "Name Title")
				+ ValidField(loForm, "name", "NameFirst")
				+ ValidField(loForm, "name", "NameLast")
				+ validDate(loForm, "Birth", 18)
				+ RequiredField(loForm, "NumOfChildrenID", "Number Of Children")
				+ RequiredField(loForm, "HouseholdIncomeID", "Household Income")
				+ RequiredField(loForm, "MaritalStatusID", "Marital Status")
				+ RequiredField(loForm, "HomeOwnerID", "Home Owner")
				+ ValidField(loForm, "email", "Email", "Email");
			
			// only validate new email if has been confirmed
			if(FieldValue(loForm, loForm.confirmemail, null) == 2)
			{
				sErrors += ValidField(loForm, "email", "newEmail", "Email");
			}
			
			// only validate new password if its been entered
			if (FieldValue(loForm, "NewPassword").length > 0 )
			{	
				sErrors += validPassword(loForm, "NewPassword", "ConfirmNewPassword")
			}
			
			bErrors = Boolean(sErrors);
			
			if (bErrors)
			{
				sErrors = "Please complete the following in your form:\n\n"
					+ sErrors;
				alert(sErrors);
			}
			else
			{
				document.body.style.cursor='wait';
			}
		}
		catch (eError)
		{
			alert('eError: ' + eError.description);
			bErrors = true;
		}
		return (!bErrors);
	}
	
//-----------------------------------------------------------------------------

function ValidateEmailConfirmation(loForm)
{
	var sErrors, bErrors;
	
	try
	{
		if(FieldValue(loForm, loForm.doicode, null).length != 5)
		{
			sErrors = 'Invalid Activation Code'
		}
		bErrors = Boolean(sErrors);
			
		if (bErrors)
		{
			sErrors = "Please complete the following in your form:\n\n"
				+ sErrors;
			alert(sErrors);
		}
		else
		{
			document.body.style.cursor='wait';
		}
	}
	catch (eError)
	{
		alert('eError: ' + eError.description);
		bErrors = true;
	}
	return (!bErrors);

}

//-----------------------------------------------------------------------------

function DOIReminder(oLoginForm)
{
	var sEmail, oEmail;
	if (!oLoginForm)
	{
		oLoginForm = 'confirmdoubleoptin';
	}
	oLoginForm = GetForm(oLoginForm);
	oEmail = GetField(oLoginForm, 'Email')
	sEmail = FieldValue(oLoginForm, oEmail);
	
	if (confirm('Send activation reminder email to ' + sEmail + '?'))
	{
		SetValueSubmitForm(oLoginForm, 'activationreminder', 1);
	}
	
	return(false);
} // ForgotPass

//-----------------------------------------------------------------------------
/************** this function has been copied from incCommonCode.js and adapt to the french myoffers****************/
function makeHomePage_FR(elem, lsRootFolder)
{
	return makeHomePage2_FR(elem, lsRootFolder, 'http://www.butineo.com');
}

function makeHomePage2_FR(elem, lsRootFolder, lsUrl)
{
	try
	{
		elem.style.behavior = 'url(#default#homepage)';
		elem.setHomePage(lsUrl);
	}
	catch(e)
	{
		alert('Nous n\'avons pas pu configurer votre page de démarrage car :\n\n' + e.message);
	}	
	if (lsRootFolder)
	{
		location.href = lsRootFolder;
	}
	return (false);

}
/**************************************************************/
function ValidateRegForm2(loForm, lbRedirectAge)
{
	var sErrors, bErrors;
	var dTomorrow;
	var sCookieName;
	
	sCookieName = "Underage";

	try
	{
	    if(GetCookie(sCookieName) != null)
	    {
		    document.body.style.cursor='wait';
		    bErrors = true;
	        alert( "Désolé, il faut être âgé de plus de 18 ans pour souscrire à Butineo ; et vous venez de déclarer que vous avez moins de 18 ans." );
			window.location = "/butineo/index.asp";
	    }
	    else
	    {
		    gbFieldFocus = false;
		    loForm = GetForm(loForm);
		    sErrors = RequiredField(loForm, "Addressline", "Address Chooser")
			    + RequiredField(loForm, "NameTitleID", "Titre")
			    + ValidField(loForm, "name", "NameFirst", "Prénom")
			    + ValidField(loForm, "name", "NameLast", "Nom")
			    /*+ ValidField(loForm, "StreetNumber", "StreetNumber", "No de maison/appartement")*/
			    + ValidField(loForm, "postcode", "Postcode", "Code postal")
			    + ValidField(loForm, "email", "Email", "Adresse email")
			    + ValidField(loForm, "email", "Email2", "Adresse email alternative")
			    + ValidField(loForm, "mobilenumber", "MobileNumber", "Numéro de téléphone mobile", true)
			    + validPassword(loForm, "Password", "ConfirmPassword")
    			+ ValidField(loForm, "address1", "Address1", "Adresse")
			    + ValidField(loForm, "town", "Town", "Ville")
			    + ValidField(loForm, "pseudo", "Pseudo", "Pseudo")
			    if(lbRedirectAge) 
			    {
				    sErrors = sErrors + validDate(loForm, "Birth", 0)
			    }
			    else
			    {
				    sErrors = sErrors + validDate(loForm, "Birth", 18)
			    }
    			
		    sErrors = sErrors + RequiredField(loForm, "NumOfChildrenID", "Nombre d'enfants")
			    + RequiredField(loForm, "MaritalStatusID", "Statut marital")
			    + RequiredField(loForm, "HouseholdIncomeID", "Revenus du ménage")
			    + RequiredField(loForm, "HomeOwnerID", "Propriétaire du logement")
			    + RequiredField(loForm, "ResidentialStatusID", "Statut résidentiel")
			    + RequiredField(loForm, "webbrandsoptin", "Would you like to join Web Brands?")
			    + RequiredField(loForm, "TermsConditions", "Terms and Conditions")
		    bErrors = Boolean(sErrors);
		    //bErrors = true;
		    if (bErrors)
		    {
			    sErrors = "Veuillez compléter les champs ci-dessous:\n\n"
				    + sErrors;
			    alert(sErrors);
		    }
		    else
		    {
			    document.body.style.cursor='wait';
    		
			    if(validDate(loForm, "Birth", 18).indexOf("18") >= 0 && lbRedirectAge)
			    {
				    bErrors = true;
				    dTomorrow = DayAdd(new Date(), 1);
				    alert("Désolé, il faut être âgé de plus de 18 ans pour souscrire à Butineo, nous ne pouvons donner suite à votre requête.");
				    SetCookie(sCookieName, "true", '/', dTomorrow);
				    window.location = "/butineo/index.asp";
			    }
		    }
	    }
	}
	catch (eError)
	{
		alert('eError: ' + eError.description);
		bErrors = true;
	}
	return (!bErrors);
}

function ForgotPass(oLoginForm)
{
	var sEmail, oEmail;
	if (!oLoginForm)
	{
		oLoginForm = 'login';
	}
	oLoginForm = GetForm(oLoginForm);
	oEmail = GetField(oLoginForm, 'Email')
	sEmail = FieldValue(oLoginForm, oEmail);
	//alert('sEmail: ' + sEmail);
	if (validEmail(sEmail))
	{
		if (confirm('Envoyer un rappel de mot de passe ' + sEmail + '?'))
		{
			SetValueSubmitForm(oLoginForm, 'forgotpass', 1);
		}
	}
	else
	{
		alert('Saisir l\'adresse email sous laquelle vous êtes enregistré pour recevoir un email de rappel.');
		oEmail.focus();
	}
	return(false);
} // ForgotPass

//-----------------------------------------------------------------------------

function ValidateLogin(oLoginForm) {
	var oLoginForm, oEmail, sEmail, oPassword, sPassword, bValidLogin;
	var sError;
	oLoginForm = (oLoginForm || 'login');
	oLoginForm = GetForm(oLoginForm);
	oEmail = GetField(oLoginForm, 'Email')
	oPassword = GetField(oLoginForm, 'Password')
	sEmail = FieldValue(oLoginForm, oEmail);
	sPassword = FieldValue(oLoginForm, oPassword);
	if (sPassword.length == 0)
	{
		oPassword.focus();
		sError = 'un mot de passe valide';
	}
	if (!validEmail(sEmail))
	{
		sError = 'une adresse email valide' + (sError ? ' et ' + sError : '');
		oEmail.focus();
	}
	if (sError)
	{
		bValidLogin = false;
		alert('Veuillez saisir ' + sError + '.');
	}
	else
	{
		bValidLogin = true;
		document.body.style.cursor = 'wait';
		setTimeout('document.body.style.cursor = \'wait\';', 2000)
	}
	return(bValidLogin);
}

//-----------------------------------------------------------------------------

/*****************/

	 //updating lists via XMLHttpRequest
      function liste(f,q,t) {
         var l1    = f.elements["list"+(q-1)];
         var l2    = f.elements["list"+q];
         var index = l1.selectedIndex;

         for(i=q;i<=t;i++) 
			f.elements["list"+i].options.length = 0;

         if(index > 0) {
            var xhr_object = null;
            
            if(window.XMLHttpRequest) // Firefox
              xhr_object = new XMLHttpRequest();
            else if(window.ActiveXObject) // Internet Explorer
              xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
            else { // browser doesns't support XMLHttpRequest
              alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
              return;
            }

            xhr_object.open("POST", "http://" + window.location.host + "/butineo/FrenchTownAJAX.asp?q="+q, true);

            xhr_object.onreadystatechange = function() {
              if(xhr_object.readyState == 4)
              {
				eval(xhr_object.responseText);
				//alert(xhr_object.responseText);  debug mode
				
              }
            }

            xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            var data = "champ="+escape(l1.options[index].value)+"&form="+f.name+"&select=list"+q;
            xhr_object.send(data);
         }
      }

      // update of the selectindex
      function update(L) { 
 
         v = new Array;
         v[0] = "<%=adresse('Departement')%>";
         v[1] = "<%=adresse('Codepos')%>";
         v[2] = "<%=adresse('INSEE')%>";
         if(v[L]=="") return;
         f = document.forms.register;
         L0 = f.elements["list"+L];
         for(i=0;i<L0.options.length;i++)
            if(L0.options[i].value==v[L]) {
               L0.selectedIndex=i;
               if(L<2) liste(f,L+1,2);
               break;
            }
      }

//-----------------------------------------------------------------------------

function ValidateExistingUser(oLoginForm)
{
	var oLoginForm, oEmail, iExistingUserChoice, oPassword, sPassword, bValidLogin;
	var sError;
	try
	{
		oLoginForm = GetForm(oLoginForm);
		iExistingUserChoice = FieldValue(oLoginForm, 'existinguserchoice');
		oPassword = GetField(oLoginForm, 'Password');
		sPassword = FieldValue(oLoginForm, oPassword);
		if (!iExistingUserChoice)
		{
			sError = 'Choisir une des 3 options SVP.';
		}
		else if (iExistingUserChoice == 1 && sPassword.length == 0)
		{
			sError = 'Veuillez saisir votre mot de passe (4 caractères ou plus).';
			oPassword.focus();
		}
		if (sError)
		{
			bValidLogin = false;
			alert(sError);
		}
		else
		{
			bValidLogin = true;
			document.body.style.cursor = 'wait';
			setTimeout('document.body.style.cursor = \'wait\';', 2000)
		}
	}
	catch (eError)
	{
		alert('eError: ' + eError.description);
		bValidLogin = false;
	}
	return(bValidLogin);
}

//-----------------------------------------------------------------------------
	
	
 
