﻿
function validatePhone( strValue ) {
    var phone = new RegExp("^(\\d{3})( |-)?(\\d{3})( |-)?(\\d{4})$");
    return phone.test(strValue);
}

function validatecodepostalUSA( strValue ) {
    var validCodePostalUSA = new RegExp("^(\\d{5}(( |-)\\d{4})?)$");
    return validCodePostalUSA.test(strValue);
}

function validateIATA( strValue ) {
    var iata = new RegExp("^-?(\\d+(\\.|\\,)\\d*$)|(^-?\\d+$)|(^-?(\\.|\\,)\\d+$)");
    return iata.test(strValue);
}

function validateTexte ( strValue ) {
    var texte = new RegExp("^[a-zA-Z]+$");
    return texte.test(strValue);
}

function validateEmail( strValue ) {
     var validemail = new RegExp("^[\\w\\.=-]+@[\\w\\.-]+\\.[\\w\\.-]{2,4}$");
     return validemail.test(strValue);
}

function validateFirstnameRegex ( strValue ) {
    var firstname = new RegExp("^[a-zA-ZÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÃÑÕãñõÄËÏÖÜäëïöüçÇßØøÅåÆæÞþÐð \\-]+$");
    return firstname.test(strValue);
}

function validateLastnameRegex ( strValue ) {
    var firstname = new RegExp("^[a-zA-ZÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÃÑÕãñõÄËÏÖÜäëïöüçÇßØøÅåÆæÞþÐð \\-\\']+$");
    return firstname.test(strValue);
}

function valider(oi_form) {

    ///EMAIL
    if(oi_form.email.value == "")
    {
        alert('The e-mail address is not valid.');
        oi_form.email.focus();
        //window.scrollBy(0,-20);
        return false;
    }
    else
    {
        if (oi_form.email.value != "")
        {
            if(validateEmail(oi_form.email.value) == false)
            {
                alert('Please enter a valid email. (format: name@server.com)');
                oi_form.email.focus();
                //window.scrollBy(0,-20);
                return false;
            }
        }
    }

     ///FIRSTNAME
     if(oi_form.firstname.value == "")
     {
        alert('Please enter your first name.');
        oi_form.firstname.focus();
        //window.scrollBy(0,-20);
        return false;
     }

    if(oi_form.firstname.value.length < 2)
    {
        alert('Your first name must be longer than 2 characters and contain only letters or dashes');
        oi_form.firstname.focus();
        //window.scrollBy(0,-20);
        return false;
    }

    if(validateFirstnameRegex(oi_form.firstname.value) == false)
    {
        alert('Your first name must be longer than 2 characters and contain only letters or dashes');
        oi_form.firstname.focus();
        //window.scrollBy(0,-20);
        return false;
    }


    ///LASTNAME
    if(oi_form.lastname.value == "")
    {
        alert('Please enter your last name.');
        oi_form.lastname.focus();
        //window.scrollBy(0,-20);
        return false;
    }

    if(oi_form.lastname.value < 2 )
    {
        alert('Your last name must be longer than 2 characters and contain only letters, dashes or apostrophes');
        oi_form.lastname.focus();
        //window.scrollBy(0,-20);
        return false;
    }

    if(validateLastnameRegex(oi_form.lastname.value) == false)
    {
        alert('Your last name must be longer than 2 characters and contain only letters, dashes or apostrophes');
        oi_form.lastname.focus();
        //window.scrollBy(0,-20);
        return false;
    }


    ///IATA
     if(oi_form.iata.value == "")
     {
        alert('Please enter your iata number.');
        oi_form.iata.focus();
        //window.scrollBy(0,-20);
        return false;
     }

     if(oi_form.iata.value != "")
     {
         if(validateIATA(oi_form.iata.value) == false){
                alert('Your IATA number must contain only numbers.');
                oi_form.iata.focus();
                //window.scrollBy(0,-20);
                return false;
         }
     }

    ///PHONE
     if(oi_form.phone.value == "")
     {
        alert('Please enter your telephone number');
        oi_form.phone.focus();
        //window.scrollBy(0,-20);
        return false;
     }
     else
     {
        if(validatePhone(oi_form.phone.value) == false){
            alert('The telephone number is not entered in an acceptable format. Please re-enter the telephone number in this format: 555-555-5555');
            oi_form.phone.focus();
            //window.scrollBy(0,-20);
            return false;
        }
     }

     ///ADDRESS1
     if(oi_form.address1.value == ""){
            alert('Please enter your address.');
            oi_form.address1.focus();
            //window.scrollBy(0,-20);
            return false;
     }

    ///CITY
    if(oi_form.city.value == ""){
            alert('Please enter your city.');
            oi_form.city.focus();
            //window.scrollBy(0,-20);
            return false;
    }

     ///STATE
     if(oi_form.state.selectedIndex == 0) {
            alert("Please select the State.");
            oi_form.state.focus();
            //window.scrollBy(0,-20);
            return false;
      }

     ///ZIP
     if(oi_form.postalcode.value == ""){
            alert('Please enter a zip code.');
            oi_form.postalcode.focus();
            //window.scrollBy(0,-20);
            return false;
     }
     if(validatecodepostalUSA(oi_form.postalcode.value) == false){
            alert('The zipcode code should be entered in a zip (11111) or zip+4 (11111-1111)');
            oi_form.postalcode.focus();
            //window.scrollBy(0,-20);
            return false;
     }

 if ( (oi_form.gender[0].checked == false ) && ( oi_form.gender[1].checked == false ) ) { alert ( "Please choose your Gender: Male or Female" ); return false; }
 if ( (oi_form.wish_receive_cm[0].checked == false ) && ( oi_form.wish_receive_cm[1].checked == false ) ) { alert ( "Do you wish to receive newsletter from Club Med? Yes or No" ); return false; }
 if ( (oi_form.wish_receive_partners[0].checked == false ) && ( oi_form.wish_receive_partners[1].checked == false ) ) { alert ( "Do you wish to receive newsletter from Partners? Yes or No" ); return false; }

 ///BIRTHDATE
 if(oi_form.yearofbirth.selectedIndex == 0) {
        alert("Please select the year of birth.");
        oi_form.yearofbirth.focus();
        //window.scrollBy(0,-20);
        return false;
 }

 if(oi_form.monthofbirth.selectedIndex == 0) {
        alert("Please select the month of birth.");
        oi_form.monthofbirth.focus();
        //window.scrollBy(0,-20);
        return false;
 }

 if(oi_form.dayofbirth.selectedIndex == 0) {
        alert("Please select the day of birth.");
        oi_form.dayofbirth.focus();
        //window.scrollBy(0,-20);
        return false;
 }

if ((oi_form.dayofbirth.selectedIndex != 0) && (oi_form.monthofbirth.selectedIndex != 0)&&(oi_form.yearofbirth.selectedIndex != 0) ) {
        oi_form.dateofbirth.value = oi_form.yearofbirth.options[oi_form.yearofbirth.selectedIndex].value + '-' + oi_form.monthofbirth.options[oi_form.monthofbirth.selectedIndex].value + '-' + oi_form.dayofbirth.options[oi_form.dayofbirth.selectedIndex].value;
}
//alert(oi_form.dateofbirth.value);

}
