Event.addOnLoad(roundCorners);

if (document.getElementById('contact')) { 
    Event.addOnLoad(function() {document.getElementById('contact').onsubmit = validerContact;});
}

var xmlHttp = new XmlHttpReq();

function emailTraite() {
    var rep = xmlHttp.getReponse();
    if (rep == 'OK') {
        alert('Votre message a bien été envoyé. Merci de votre intérêt pour YanaWare');

        document.getElementById('nom').value = '';
        document.getElementById('email').value = '';
        document.getElementById('societe').value = '';
        document.getElementById('message').value = '';
        
    } else {
        alert("Une erreur s'est produite lors de l'envoi de votre message. Merci de ré-essayer.");
    }
}

function roundCorners() {
    if (!NiftyCheck()) {
        return;
    }
    
    Rounded("div#about","top","white","#ABBCC8");
    Rounded("div#about","bottom","white","#DFE5EA");
    Rounded("div.section","top","white","#ABBCC8");
    Rounded("div.section","bottom","white","#DFE5EA");
    Rounded("div#overview","top","white","#ABBCC8");
    Rounded("div#overview","bottom","white","#DFE5EA");
    Rounded("div#pricing","top","white","#ABBCC8");
    Rounded("div#pricing","bottom","white","#DFE5EA");
    Rounded("div#menu","top","white","#ABBCC8");
    Rounded("div#menu","bottom","white","#DFE5EA");
    Rounded("div.services","top","white","#ABBCC8");
    Rounded("div#services","bottom","white","#DFE5EA");
    Rounded("div#software","top","white","#ABBCC8");
    Rounded("div#software","bottom","white","#DFE5EA");
    Rounded("div#contact","top","white","#ABBCC8");
    Rounded("div#contact","bottom","white","#DFE5EA");
    Rounded("div#branding","top","#e6edf1","#718FA4");
    Rounded("div#siteinfo-legal","bottom","#e6edf1","#718FA4");
    Rounded("label","all","#DFE5EA","#FFF","small");
}

function validerContact() {
    var nom = document.getElementById('nom');
    
    if (isEmpty(nom)) {
        alert('Veuillez saisir votre nom');
    
        nom.focus();
        
        return false;
    }
    
    var email = document.getElementById('email');

    if (isNotEmail(email)) {
        alert('Veuillez saisir une adresse email valide');
    
        email.focus();
        
        return false;
    }
    
    var message = document.getElementById('message');
    
    if (isEmpty(message)) {
        alert('Veuillez saisir votre message');
    
        message.focus();
        
        return false;
    }
    
    var nom = document.getElementById('nom');
    var email = document.getElementById('email');
    var societe = document.getElementById('societe');
    var message = document.getElementById('message');

    xmlHttp.executer('contact.php?nom=' + encodeURIComponent(nom.value) 
                     + '&email=' + encodeURIComponent(email.value) 
                     + '&societe=' + encodeURIComponent(societe.value) 
                     + '&message=' + encodeURIComponent(message.value), emailTraite);

    return false; // pas de soumission en post mais utilisation de XmlHttpRequest
}

function isEmpty(elem) {
    var str = elem.value;
    return str == null || str.length == 0;
}

function isNotEmail(elem) {
    var str = elem.value;
    str = str.toLowerCase( );
    if (str.indexOf("@") > 1) {
        var addr = str.substring(0, str.indexOf("@"));
        var domain = str.substring(str.indexOf("@") + 1, str.length);
        // at least one top level domain required
        if (domain.indexOf(".") == -1) {
            //alert("Verify the domain portion of the email address.");
            return true;
        }
        // parse address portion first, character by character
        for (var i = 0; i < addr.length; i++) {
            oneChar = addr.charAt(i).charCodeAt(0);
            // dot or hyphen not allowed in first position; dot in last
            if ((i == 0 && (oneChar == 45 || oneChar == 46))  || 
                (i == addr.length - 1 && oneChar == 46)) {
                //alert("Verify the user name portion of the email address.");
                return true;
            }
            // acceptable characters (- . _ 0-9 a-z)
            if (oneChar == 45 || oneChar == 46 || oneChar == 95 || 
                (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
                continue;
            } else {
                //alert("Verify the user name portion of the email address.");
                return true;
            }
        }
        for (i = 0; i < domain.length; i++) {
            oneChar = domain.charAt(i).charCodeAt(0);
            if ((i == 0 && (oneChar == 45 || oneChar == 46)) || 
                ((i == domain.length - 1  || i == domain.length - 2) && oneChar == 46)) {
                //alert("Verify the domain portion of the email address.");
                return true;
            }
            if (oneChar == 45 || oneChar == 46 || oneChar == 95 || 
                (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
                continue;
            } else {
                //alert("Verify the domain portion of the email address.");
                return true;
            }
        }
        return false;
    }
    //alert("The email address may not be formatted correctly. Please verify.");
    return true;
}
            
