$(document).ready(function(){
    //Preloading images
    $.cacheImage('http://www.genimmo.ch/assets/images/layout/bigger-input-bgr-false.png');
    $.cacheImage('http://www.genimmo.ch/assets/images/layout/login-input_x.png'); 
    $.cacheImage('http://www.genimmo.ch/assets/images/layout/textfield-bgr_x.png');

    //Removes the warning when "I agree to conditions" is checked"
    $("span.ui-checkbox").click(function(){
        $("div.condition_error").html("");
    });

});

function validateForm(){

        var error = false;

        if( $.trim($("#password").val()) != $.trim($("#rePassword").val()) ) {
            $("#password").parent().addClass('false');
            $("#rePassword").parent().addClass('false');
            error = true;
        }
        else {
            $("#password").parent().removeClass('false');
            $("#rePassword").parent().removeClass('false');
        }
        
        $(".required").each(function(){
            if( $.trim( $(this).val() ) == "" ){
                $(this).parent().addClass('false');
                error = true;
            }
            else{ 
                $(this).parent().removeClass('false');
            }
        });

        if(!validateEmail()){
            $("#email").parent().addClass('false');
            error = true;
        }else{
            $("#email").parent().removeClass('false');
        }
        
        return error;
}

function validateCondition(){

        var error = false;

        if( $("#password").val() == '' && $("#rePassword").val() == '' ) {
            $("#password").parent().addClass('false');
            $("#rePassword").parent().addClass('false');
            error = true;
        } 
        
        if(!$("span.ui-checkbox").hasClass('ui-checkbox-state-checked')){
            if($("input#language").val() == 'french'){
                 $("div.condition_error").html("<p style=\"color:yellow;\">Vous devez accepter les conditions d'utilisation!!!</p>");
            }else{
                 $("div.condition_error").html("<p style=\"color:yellow;\">You have to accept the terms of use!!!</p>");
            }
          error = true;
        }
        return error;
}

function validateContact() {
    
    var error = false;
    
    if( $.trim( $(".message_required").val() ) == "" ){
        $(".message_required").parent().removeClass('textField'); 
        $(".message_required").parent().addClass('falseBig');
        error = true;
    }
    else{ 
        $(".message_required").parent().removeClass('falseBig');
        $(".message_required").parent().addClass('textField'); 
    }
    return error;
    
}



function validateEmail() {

        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        return emailPattern.test($("#email").val());
}

function registerUser(){

       val1 = validateCondition();
       val2 = validateForm();

       if(!val1 && !val2){
            $("#registration").submit();
       }
}

function sendContact(){

        val1 = validateForm();
        val2 = validateContact();
    
        if(!val1 && !val2){
            $("#contactForm").submit();
        }
}

function updateUser(){

        if(!validateForm()){
            $("#account").submit();
        }

}
