$(function(){
	//gestion des textes par d?faut dans formulaire de connexion
        $("input[class*='autoEmpty']").each(function(){

            $(this).focus(
                function(){

                    var text = $(this).attr('rel');
                    if($(this).val() == text) {
                        $(this).val("");
                    }

                }
            );

            $(this).blur(
                function(){
                    var text = $(this).attr('rel');
                    if($(this).val() == '') {
                        $(this).val(text);
                     }
                }
            );
        });
});


function envoiMail()
{
    
    var isOk = true;
    var town  = $('#jumpMenu option:selected').text();

    var phone = $('#phone').val();
    var mail = $('#mail').val();
    var name = $('#name').val();
    var firstname = $('#firstname').val();
    var surface = $('#surface').val();
    var service = $('#service').val();
    var type = $('#type').val();
    var callBackBy = $('input:radio[name=radio]:checked').val();


    var testPhone = isPhoneValid(phone);
    
    
    if(testPhone == 1)
    {
        isOk = false;
        alert('Attention, le«'+phone+'» n’est pas un numéro de téléphone valide.');
    }
    if(testPhone == 2)
    {
        isOk = false;
        alert('Attention, le champs téléphone doit contenir au moins 10 chiffres.');
    }
    if(testPhone == 3)
    {
        alert('Attention votre numéro de téléphone comporte plus de 10 chiffres, merci de le ressaisir. Si vous appelez de l’étranger merci de nous rappelez aux heures ouvrables au 0810 26 25 24. ');
    }

    if(isOk)
    {
        //MM_openBrWindow('adomo_vous_remercie.html','merci','width=300,height=280');
        $.ajax(
        {
            type: 'post',
            url: "mail.php",
            cache: false,
            data: {
                "phone":phone,
                "mail":mail,
                "name":name,
                "firstname":firstname,
                "surface":surface,
                "type":type,
                "town":town,
                "callBackBy ":callBackBy,
                "service":service
            },
            success: function(data)
            {

                $('#submitContactForm').fadeOut(function(){
                                    $('#contactFormSended').fadeIn();
                            });
            }
        });
    }
    
}


function isPhoneValid(fld) {
    var phoneWithoutSpace = str_replace(' ','',fld);
    
    if(isNaN(parseInt(phoneWithoutSpace * 1)))
        return 1;
    
    if(phoneWithoutSpace.length<10 && fld.length>10)
        return 1;
    if(phoneWithoutSpace.length<10)
        return 2;
    
    if(phoneWithoutSpace.length>10)
        return 3;
    
   return 0;
}


function isOnlyNumber(fld)
{
    var regex = new RegExp(/^(01|02|03|04|05|06|08)[0-9]{8}/gi);
	
    // Definition de la variable booleene match
    var match = false;

    // Test sur le motif
    if(regex.test(fld) && fld.length==10)
    {
            match = true;
    }
      else
    {
            match = false;
    }

    // On renvoie match
    return match;
}

function str_replace (search, replace, subject, count) {
    // Replaces all occurrences of search in haystack with replace  
    // 
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/str_replace
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order
    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
    var i = 0,
        j = 0,
        temp = '',
        repl = '',
        sl = 0,
        fl = 0,
        f = [].concat(search),
        r = [].concat(replace),
        s = subject,
        ra = r instanceof Array,
        sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
 
    for (i = 0, sl = s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j = 0, fl = f.length; j < fl; j++) {
            temp = s[i] + '';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length - s[i].length) / f[j].length;
            }
        }
    }
    return sa ? s : s[0];
}
