
// whitespace characters
var whitespace = " \t\n\r";


// Check whether string s is empty.
function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace (s) {
	
	var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.
    for (i = 0; i < s.length; i++) {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// WORKAROUND FUNCTION FOR NAVIGATOR 2.0.2 COMPATIBILITY.
//
// The below function *should* be unnecessary.  In general,
// avoid using it.  Use the standard method indexOf instead.
//
// However, because of an apparent bug in indexOf on 
// Navigator 2.0.2, the below loop does not work as the
// body of stripInitialWhitespace:
//
// while ((i < s.length) && (whitespace.indexOf(s.charAt(i)) != -1))
//   i++;
//
// ... so we provide this workaround function charInString
// instead.
//
// charInString (CHARACTER c, STRING s)
//
// Returns true if single character c (actually a string)
// is contained within string s.
function charInString (c, s) {
	for (i = 0; i < s.length; i++) {
		if (s.charAt(i) == c) return true;
    }
    return false
}



// Removes initial (leading) whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.
function stripInitialWhitespace (s) {
	var i = 0;
    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       i++;
    
    return s.substring (i, s.length);
}

function checkEmail(src)

{      

 if (src==null || src=='') 

   return true;

 else

 {

  var regex = /^[a-z0-9A-Z\_\.\-]{1,}[\@@]{1}[a-z0-9A-Z\_\.\-]*[a-z0-9A-Z]{1}[\.]{1}[a-zA-Z]{2,6}$/;

  return !(src.match(regex)==null);

 }

}

//return true if length of s is < textAreaLength
function isValidTextArea(input) {
	var textAreaLength = 2000;
	var s = input.value;
	return (s.length <= textAreaLength);
}

function isCorrectForm(lang) {
	var errorMsg = "";
    var errorNb = 0;
     
	var lastName = stripInitialWhitespace(document.fillForm.LastName.value);
	var firstName = stripInitialWhitespace(document.fillForm.FirstName.value);
    var email = stripInitialWhitespace(document.fillForm.Email.value);
	var confEmail = stripInitialWhitespace(document.fillForm.ConfEmail.value);
	var phone = stripInitialWhitespace(document.fillForm.Telephone.value);
	var remarks = document.fillForm.Remarks;

    if (isWhitespace(lastName)) {
		if(lang == 'fr') {
			errorMsg+="  - Le champ 'Nom' doit être renseigné\n";
		} else {
			errorMsg+="  - The field 'Last name' must be filled\n";
		}
       errorNb++;
     }

	if (isWhitespace(firstName)) {
		if(lang == 'fr') {
			errorMsg+="  - Le champ 'Prénom' doit être renseigné\n";
		} else {
			errorMsg+="  - The field 'First name' must be filled\n";
		}
       errorNb++;
     } 

	if(lastName == firstName) {
		if(lang == 'fr') {
			errorMsg+="  - Les champs 'Nom' et 'Prénom' sont identiques\n";
		} else {
			errorMsg+="  - The fields 'Last name' and 'First name' are identical\n";
		}
		errorNb++;
	 }

	if (isWhitespace(email)) {
		if(lang == 'fr') {
			errorMsg+="  - Le champ 'E-mail' doit être renseigné\n";
		} else {
			errorMsg+="  - The field 'E-mail' must be filled\n";
		}
		errorNb++;
	} 

	if (isWhitespace(confEmail)) {
		if(lang == 'fr') {
			errorMsg+="  - Le champ 'Confirmation E-mail' doit être renseigné\n";
		} else {
			errorMsg+="  - The field 'Confirm your E-mail' must be filled\n";
		}
		errorNb++;
	} 

	if (! checkEmail(email)) {
		if(lang == 'fr') {
			errorMsg+="  - Le champ 'E-mail' n'est pas une adresse correcte\n";
		} else {
			errorMsg+="  - The field 'E-mail' is not a correct address\n";
		}
		errorNb++;
	} else {
		if (email != confEmail) {
			if(lang == 'fr') {
				errorMsg+="  - Le champ 'Confirmation E-mail' n'est pas correct\n";
			} else {
				errorMsg+="  - The field 'Confirm your E-mail' is not correct\n";
			}
			errorNb++;
		}
	}

	if (isWhitespace(phone)) {
		if(lang == 'fr') {
			errorMsg+="  - Le champ 'Téléphone' doit être renseigné\n";
		} else {
			errorMsg+="  - The field 'Phone' must be filled\n";
		}
		errorNb++;
	} 

	if (! isValidTextArea(remarks)) {
		if(lang == 'fr') {
			errorMsg+="  - Le champ 'Commentaires' ne doit pas excéder 2000 caractères\n";
		} else {
			errorMsg+="  - The field 'Comments' must contain a text less than 2000 characters\n";
		}
		errorNb++;
	}

	if(! jcap()) {
		if(lang == 'fr') {
			errorMsg+="  - Le champ 'Mot à recopier' est incorrect\n";
		} else {
			errorMsg+="  - The field 'Word to enter' is not correct\n";
		}
		errorNb++;
	}

	switch(errorNb)
	{
		case 0 :
			result = true;
			break;
        case 1 :
			if(lang == 'fr') {
				errorMsg = "Ce formulaire contient 1 erreur : \n" + errorMsg;
			} else {
				errorMsg = "This form contains 1 error : \n" + errorMsg;
			}
            window.alert(errorMsg);
            result = false;
            break;
        default :
			if(lang == 'fr') {
				errorMsg = "Ce formulaire contient " + errorNb + " erreurs :\n" + errorMsg;
			} else {
				errorMsg = "This form contains " + errorNb + " errors :\n" + errorMsg;
			}
            window.alert(errorMsg);
            result = false;
            break;
     }
     return result;
}

function sendInfoForm(nbService, lang) {
	if (isCorrectForm(lang)) {
		
		f = "";

		for (i=0; i<nbService; i++) {
		  if (document.fillForm.Service[i].checked)
			  f += document.fillForm.Service[i].value + ",";
		}

		document.fillForm.ListServices.value = f;

		a = "Non";
		if (document.fillForm.ConditionsGenerales.checked)
			a = "Oui";
		document.fillForm.AccordConditionsGenerales.value = a;
		document.fillForm.action="formSendMail.jsp";
		document.fillForm.submit();
    }
}

