function isRadioSelected(form) {
   var check = false;
   for (var i = 0; i < form.length && !check; i++) {
       if (form.elements[i].type == 'radio' && form.elements[i].checked)
           check = true;
   }
   if (!check)
      alert( "An option must be selected" );
   return check;
}

function trim( s ) {
    return s.replace(/(^\s*)|(\s*$)/g,'');
}


function wrongControl( id ) {
    document.getElementById( id ).style.backgroundColor="red";
    document.getElementById( id ).style.color="white";
    document.getElementById( id ).parentNode.style.border="1px solid red";
    document.getElementById( id ).focus();
}

function goodControl( id ) {
    document.getElementById( id ).style.backgroundColor="white";
    document.getElementById( id ).style.color="#444444";
    document.getElementById( id ).parentNode.style.border="none";
    document.getElementById( id ).blur();
}

/* verifie si un champs est null
    @return true si le champ est null sinon false
 */
function isNull( id ) {
    if ( trim(document.getElementById( id ).value) == "") {
         wrongControl( id );
         return true;
    }
    return false;
}


function isNullAndCorrect(id) {
    if ( isNull( id ) ) {
        wrongControl( id );
        return true;
    } else {
        goodControl( id );
        return false;
    }
}

function isEmailNotCorrect( id ) {
    var regex = /\w[\w\-\.]*@\w[\w\-\.]*\.[a-z]{2,}/i;
    var email = document.getElementById( id ).value;
    if ( email == "" || email == null || !regex.test(email) ) {
        wrongControl( id );
        return true;
    }
    else {
        goodControl( id );
        return false;
    }
}

function isNoOptionSelected(id) {
    var val = document.getElementById( id ).options[document.getElementById( id ).selectedIndex].value;
    if ( trim(val) == "") {
        wrongControl( id );
        return true;
    }
    else {
        goodControl( id );
        return false;
    }
}



function isTextFilled(form) {
   var found = -1;
   for (var i = 0; i < form.length && found == -1; i++) {
       if (form.elements[i].type == 'text' || form.elements[i].type == 'textarea')
           found = i;
   }
   var nc = !isNullAndCorrect(form.elements[found].id);
   return nc;
}

function checkContact() {
    if (isNullAndCorrect("ct_first_name")) {
        return false;
    }
    if (isNullAndCorrect("ct_last_name")) {
        return false;
    }
    if (isNoOptionSelected("ct_country")) {
        return false;
    }
    if (isNullAndCorrect("ct_phone")) {
        return false;
    }
    if (isNullAndCorrect("ct_subject")) {
        return false;
    }
    if (isNullAndCorrect("ct_question")) {
        return false;
    }
    if (isNullAndCorrect("ct_captcha")) {
        return false;
    }
    return !isEmailNotCorrect("ct_email");
}

