/**
 * Copyright 2005 Olivier Clavel
 *
 * This file is part of LCJ-Conseil website software.
 *
 * LCJ-Conseil website is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License,
 * or any later version.
 *
 * LCJ-Conseil website is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MediaNetLive portal software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @author Olivier Clavel <contact AT retiz DOT com>
 */

/**
 * function to fully check the contact form
 */
function completeCheck () {
    var form = document.getElementById('contactForm');
    var problems = new Array();
    if (form.email.value.length == 0) { 
        problems[problems.length] = 'Vous devez entrer un email';
    } else {
        if (!emailCheck(form.email.value)) {
            problems[problems.length] = 'Vous devez entrer une adresse email valide';
        }
    }
    
    if (form.fname.value.length == 0) {
        problems[problems.length] = 'Vous devez entrer un prénom';
    }
    
    if (form.lname.value.length == 0) {
        problems[problems.length] = 'Vous devez entrer un nom';
    }
    
    if (form.subject.value.length == 0) {
        problems[problems.length] = 'Vous devez entrer un objet';
    }
    
    if (form.message.value.length == 0) {
        problems[problems.length] = 'Vous devez entrer un message';
    }
    
    var nbPbs = problems.length;
    
    if (nbPbs > 0) {
        var errorPopup = window.open("", "errorPopup", "width=300,height=300,status=0,resizable=1,scrollbars=1");
        errorPopup.document.open();
        var errorText = '<html><head><title>Erreurs</title><style>* {font-family: arial, sans-serif;}</style></head><body>';
        errorText += '<p>Il y a ' + nbPbs + ' ';
        if (nbPbs > 1) {
            errorText += 'problèmes';
        } else {
            errorText += 'problème';
        }
        errorText += ' à régler dans les données que vous avez entrées:</p>';
        for (var i=0; i<nbPbs; i++) {
            errorText += '<p>' + (i+1) + ' - ' + problems[i] + '</p>';
        }
        errorText += '<form><center><input type="button" value="Fermer cette fenêtre" onClick="window.close()"></center></form>';
        errorText += '</body></html>';
        
        errorPopup.document.write(errorText);
        errorPopup.document.close();
    } else {
        form.submit();
    }
}

/**
 * Valid email check function
 * Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
function emailCheck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}
