//by Nasreddine BOUAFIF (nasr@lasouris.Fr)
//La Souris qui Colle au Tapis 
//Xuan Viet HOANG DON (xuanviet_hoangdon@yahoo.fr)
//Original:  Sandeep V. Tamhankar (stamhankar@hotmail.com)
//Please keep this information for further updates. Thank you very much!!!

function validatorEmail(emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

//alert("Email address seems incorrect (check @ and .'s)");
return false;
}

var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
//alert("Ths username contains invalid characters.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
//alert("Ths domain name contains invalid characters.");
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

//alert("The username doesn't seem to be valid.");
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
//alert("Destination IP address is invalid!");
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
//alert("The domain name does not seem to be valid.");
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
//alert("The address must end in a well-known domain or two letter " + "country.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
//alert("This address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;

}


function validatorDate(dateStr) {
	// Date validation function courtesty of 
	// Checks for the following valid date formats:
	// DD/MM/YY   DD/MM/YYYY   DD-MM-YY   DD-MM-YYYY
	
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year
	
	var matchArray = dateStr.match(datePat); // is the format ok?
		if (matchArray == null) {
		//	alert(dateStr + " Date is not in a valid format.")
		return false;
	}
	month = matchArray[3]; // parse date into variables
	day = matchArray[1];
	year = matchArray[4];
	if (month < 1 || month > 12) { // check month range
		//alert("Month must be between 1 and 12.");
		return false;
	}
	if (day < 1 || day > 31) {
		//alert("Day must be between 1 and 31.");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		//alert("Month "+month+" doesn't have 31 days!")
		return false;
	}
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			//alert("February " + year + " doesn't have " + day + " days!");
			return false;
		}
	}
	return true;
}


function compareDate(dateform){
		
	date1 = new Date();
	date2 = new Date();
	diff  = new Date();
	
	if (eval('document.'+dateform+'.date_arriv.value')) {
		date1temp = new Date(eval('document.'+dateform+'.date_arriv.value') );
		date1.setTime(date1temp.getTime());
	}
	if (eval('document.'+dateform+'.date_depart.value')) {
		date2temp = new Date(eval('document.'+dateform+'.date_depart.value') );
		date2.setTime(date2temp.getTime());
	}
	else return false; // otherwise exits
	if(date2.getTime() - date1.getTime()>= 0){
		return (true);		
	}else{
		return (false);
	}



}



function is_valid(formName, lang) {
	
	
	
	
	var message_fr = "Merci de bien vouloir vérifier les champs suivants : \n\n";
	var message_en = "Please check the fields : \n\n";
	var message = eval ('message_'+lang);
	
	var required = new Array('date_arriv', 'date_depart', 'nb_adultes',  'repas_soiree_arriv', 'nom', 'prenom', 'adresse', 'cp', 'ville', 'pays', 'email', 'tel');
	var type_required = new Array('date', 'date', 'text', 'text', 'text', 'text', 'text', 'text', 'text',  'text', 'email', 'text');
	
	var name_required_en = new Array('Arrival date', 'Departure date', 'Adults number', 'Dinner on arrival date', 'Surname', 'Name', 'Adress', 'Postal Code', 'City', 'Country', 'Email', 'Phone');
	var name_required_fr = new Array('Date d\'arrivée', 'Date de départ', 'Nombre des adultes', 'Repas la soirée d\'arrivée', 'Nom', 'Prénom', 'Adresse', 'Code Postal', 'Ville', 'Pays', 'Email', 'Téléphone');
	
	var msg_date_validation1_fr = "Merci de renseigner la date dans le format demandé";
	var msg_date_validation1_en = "Date is not in a valid format."
	
	var msg_date_validation2_fr = "La date d'arrivée doit être inférieure à la date de départ.";
	var msg_date_validation2_en = "The arrival date must be inferior to the departure date.";
	
	
	
	stop = 0;
	
	for (champs in required){
		//alert(required[champs])	;
		field = required[champs];
		name_field = eval('name_required_'+lang+'['+champs+']');
		type_field = type_required[champs];
		
		if(type_field == 'text'){
			if (document.forms[formName].elements[field].value == "") {
				message += "- "+name_field+"\n";
				stop ++;
			}
		}else if(type_field == 'email'){
			if (validatorEmail(document.forms[formName].elements[field].value) == false) {
				message += "- "+name_field+"\n";
				stop++;
			}
		
		}else if(type_field == 'date'){
			
			if (validatorDate(document.forms[formName].elements[field].value) == false) {
				message += "- "+name_field+"\n"+ eval('msg_date_validation1_'+lang)+"\n";
				stop++;
			}
			
		}
		
		
		
	}
	if(compareDate(formName) == false){
		message += eval('msg_date_validation2_'+lang)+"\n";
		stop ++;
	}

		
	if (stop > 0 && message != "") {
		alert(message);
		message = "";
		return false;
	}else 
		return true;
	
}

function ft_submit(lang){
	//lang = 'en' 
	var formName = 'labriqueterie_contact';
	
	if(is_valid(formName, lang)){
		document.forms[formName].submit();	
	}
}