
$(document).ready(function(){
	var date = new Date().asString();
	$("#daterappel").datepicker($.extend({}, 
		$.datepicker.regional["fr"], { 
		    dateFormat: "dd/mm/yy",
			firstDay: 1,
			startDate: "03/17/2009",
			dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'],
			dayNamesMin: ['Di', 'Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa'],
			dayNamesShort: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
			monthNames: ['Janvier','Fevrier','Mars','Avril','Mai','Juin','Juillet','Aout','Septembre','Octobre','Novembre','Decembre'],
			monthNamesShort: ['Jan','Fev','Mar','Avr','Mai','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
			minDate : new Date(),
			hideIfNoPrevNext: true
		})).val(new Date().asString()).trigger('change');		
});


function str_replace(cherche,remplace,texte){
	while(texte.indexOf(cherche) > -1){
		texte = texte.replace(cherche,remplace);
	}
	return texte;
}


function supanimal(num){
	$("#animal"+num).remove();
	var string = $("#numanimal").val();
	var nouvelle = str_replace(num+"-","",string);	
	$("#numanimal").val(nouvelle);
}



function afficheprod(num){
		
		$.ajax({
			type:"GET",
			url:"prod_call.php",
			data : "id_rubrique="+document.getElementById("prod"+num).value+"&num="+num,
			success : function(html){
				$("#selectrace"+num).html(html.split("|")[0]); 
			}
		})
	}
	
	
function ajouter_animal(){
	var num = parseInt($("#numcourant").val());

	
	$.ajax({
		type : "GET",
		url : "ajoutanimal.php",
		data : "num="+num,
		success : function(html){
			$("#animalplus").html(html);
			$("#numcourant").val(num+1);
			$("#numanimal").val($("#numanimal").val()+(num+1)+"-");
			$("#animalplus").removeAttr("id")
			$(".animalplustemp").attr("id","animalplus");
			$("#animalplus").removeClass();
		}
	})
}

function verifcat(num){
	$.ajax({
		type:"GET",
		url:"verifcat.php",
		data:"id="+document.getElementById("race"+num).value,
		success:function(html){
			$("#categorie"+num).html(html.split("|")[0]);
			$("#dangereux"+num).val(html.split("|")[1]);
		}
	})
}

function verifcompte(){
	var mail1 = $("#email1").val();
	var mail2 = $("#email2").val();
	
	if(mail1 != mail2){
		$("#info").html("adresse mail différente");
	}
	else{
		$.ajax({
			method:"GET",
			url:"verifcompte.php",
			data:"mail="+mail1,
			success:function(html){
				if(html == 1){
					$("#info").html(" ");
					$("#LienDeValidation").removeAttr("style");
				}
				else{
					$("#info").html(" ");
					$("#verifpass").removeAttr("style");
				}
			}
		})
	
	
	
	}

}

function identification(num,valeur){
	if(valeur == "3"){
		document.getElementById("typecache"+num).style.display="inline";
	}
	else{
		document.getElementById("typecache"+num).style.display="none";
	}
}

function verifpass(){
	$.ajax({
		type:"GET",
		url:"verifpass.php",
		data:"pass="+$("#pass2").val()+"&mail="+$("#email2").val(),
		success:function(html){
			if(html == "1"){
				alert("mot de passe valide");
				tb_remove();
				$("#LienDeValidation").removeAttr("style");
			}
			else{
				alert("mot de passe invalide, réessayer");
			}
		}
	})


}

function longpass(){
	if($("#pass1").val() == $("#pass2").val()){
		$("#validpassword").removeAttr("style");
	}
}


function trim (str, charlist) {   
    var whitespace, l = 0, i = 0;
    str += '';
    
    if (!charlist) {
        // default list        
        whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
    } else {
        // preg_quote custom list
        charlist += '';
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');    }
    
    l = str.length;
    for (i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {            str = str.substring(i);
            break;
        }
    }
        l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;        }
    }
    
    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}




function valider(){
	var nbanim =  parseInt($("#numcourant").val());
	var valide = 1;
	var danger=0;
	var reg= /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
	for(i=1;i<=nbanim;i++){
		if($("#journaissance"+i) && $("#moisnaissance"+i) && $("#anneenaissance"+i)){
			var jour = $("#journaissance"+i).val();
			if(jour<10) jour ="0"+jour;
			var mois = $("#moisnaissance"+i).val();
			if(mois<10) mois ="0"+mois;
			var today = new Date();
			var dateachat = new Date($("#anneenaissance"+i).val(),mois,jour);
			var an = (today.getFullYear()-dateachat.getFullYear())*365;
			var moisdiff = ((today.getMonth()-dateachat.getMonth())+1)*30.41;
			var	jourdiff = (today.getDate()-dateachat.getDate());
			var res = an+moisdiff+jourdiff;
			
			if(res <=90){
				alert("Votre animal "+$("#nom"+i).val()+" devra \352tre ag\351 de plus de 3 mois au démarrage du contrat");
			}
			else if(res >=2920){
				alert("Votre animal "+$("#nom"+i).val()+" doit \352tre ag\351 de moins de 8 ans");
				valide=0
			}
		}
		
		if($("#prod"+i).val() == "" || !$("#race"+i) || $("#race"+i).val() == "" || trim($("#nom"+i).val()) == ""){
			valide = 0;
			alert("veuillez vérifier les informations de votre animal numéro "+i);
		}	
	} 
	
	
	for(i=1;i<=nbanim;i++){
		if($("#dangereux"+i) && $("#dangereux"+i).val() == "1"){
			danger=1;
			valide=0;
		}
	}
	
	if(trim($("#nom").val()) == "" || trim($("#prenom").val()) == "" || trim($("#adresse1").val()) == "" || trim($("#cpostal").val()) == "" || trim($("#ville").val()) == "" || reg.test($("#email1").val())==false || trim($("#email1").val()) != trim($("#email2").val()) || trim($("#telfixe").val()) == ""){
		valide=0;
		alert("vérifier les champs obligatoires de vos coordonnées");
	}
	
	if(danger){
		alert("Les chiens de première catégorie ne peuvent pas être assurés");
	}
	
	if(valide){
		$("#formulaire").submit();
	}

}



