// JavaScript Document
// Validation de la date
var dtCh = "/";

// Détermine l'année à prendre en compte lors de la résérvation :
// l'année en cours + 1
var toDay = new Date();
minDay = toDay.getDate()
minMonth = toDay.getMonth() + 1
minYear = toDay.getYear()
var maxYear = minYear + 1

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Vérifie si le caractère actuel est un numérique
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // Tous les caractères sont numériques.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	
	var daysInMonth = DaysArray(12)
	var pos1 = dtStr.indexOf(dtCh)
	var pos2 = dtStr.indexOf(dtCh,pos1+1)
	//var strMonth = dtStr.substring(0,pos1)
	//var strDay = dtStr.substring(pos1+1,pos2)
	var strDay = dtStr.substring(0,pos1)
	//alert(strDay)
	var strMonth = dtStr.substring(pos1+1,pos2)
	//alert(strMonth)
	var strYear = dtStr.substring(pos2+1)
	//alert(strYear)
	strYr = strYear
	
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}

	day = parseInt(strDay)
	month = parseInt(strMonth)
	year = parseInt(strYr)
	
	if (pos1==-1 || pos2==-1){
		alert("Entrez une date valable au format jj/mm/aaaa")
		return false
	}
	if (strMonth.length < 1 || month < 1 || month > 12){
		alert("Le mois est incorrect")
		return false
	}
	if (strDay.length < 1 || day < 1 || day > 31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Le jour est incorrect")
		return false
	}
	if (strYear.length != 4 || year==0 || year < minYear || year > maxYear){
		alert("Entrez une date comprise entre le "+ minDay +"/"+ minMonth +"/"+ minYear +" et "+ minDay +"/"+ minMonth +"/"+ maxYear)
		return false
	}
	if (day < minDay && month < minMonth && year <= minYear){
		alert("Entrez une date comprise entre le "+minDay+"/"+ minMonth +"/"+ minYear +" et "+ minDay +"/"+ minMonth +"/"+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Entrez une date valable au format jj/mm/aaaa")
		return false
	}
	return true
}

function CheckDate(field){
	var fld = field
//	alert (fld)
	if (fld == 'jourdepart'){
		if (document.devis.jourdepart.value != ""){
			if (isDate(document.devis.jourdepart.value)==false){
				document.devis.jourdepart.focus()
				document.devis.jourdepart.select()
				return false}}
		}
	else{
		if (document.devis.jourretour.value != ""){
			if (isDate(document.devis.jourretour.value)==false){
				document.devis.jourretour.focus()
				document.devis.jourretour.select()
				return false}}
		}
	return true
}

function CheckEmail(val){
	with (val) {
		apos = val.indexOf("@")
		dotpos = val.lastIndexOf(".")
		if (apos<1||dotpos-apos<2){
			alert("'"+ val + "' n'est pas une adresse électronique valable !");
			devis.email.focus();
			devis.email.select;
			return false;
			}
		else {return true;}
		}
	}
 
function CheckThis(){
	
	if (devis.noms.value == ""){
		alert("Veuillez introduire vos noms");
		devis.noms.focus();
		return false;
		}
	
	if (devis.adresse.value == ""){
		alert("Veuillez introduire une adresse");
		devis.adresse.focus();
		return false;
		}
		
	if (devis.codepostal.value == ""){
		alert("Veuillez introduire un code postale valable");
		devis.codepostal.focus();
		return false;
		}
		
	if (devis.localite.value == ""){
		alert("Veuillez introduire une localité");
		devis.localite.focus();
		return false;
		}
	
	if (devis.pays.value == ""){
		alert("Veuillez sélectionner un pays");
		devis.pays.focus();
		return false;
		}
		
	if (devis.telephone.value == ""){
		alert("Veuillez introduire un numéro de téléphone");
		devis.telephone.focus();
		return false;
		}
	
	if (devis.email.value == ""){
		alert("Veuillez introduire une adresse électronique valable");
		devis.email.focus();
		return false;
		}
	return true;
	}
