//Function générale et de validation des données
if (document.all){
	var IE =true;
	var NS = false;
}else{
	var IE= false;
	if (document.layers){
		var NS = true;
	}else{
		var NS = false;
	}
}


//fonction de controle de date valide
function IsValidDate(Jour, Mois, Annee){
	if (parseInt(Jour) == NaN){
		return false;
	}
	if (parseInt(Mois) == NaN){
		return false;
	}
	if (parseInt(Annee) == NaN){
		return false;
	}
	
	if (Mois == 1 || Mois == 3 || Mois == 5 || Mois == 7 || Mois == 8 || Mois == 10 || Mois == 12){
		if (parseInt(Jour) > 31){
			return false;
		}
	}else if (Mois == 2){
		if (parseInt(Annee) % 4 == 0){
			if (parseInt(Jour) > 29){
				return false;
			}
		}else{
			if (parseInt(Jour) > 28){
				return false;
			}
		}
	}else{
		if (parseInt(Jour) > 30){
			return false;
		}
	}
	return true;
}

function emailCheck (emailStr) {
	// Verifie le format d'une adresse emeil
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		alert("L'adresse E-Mail ne semble pas valide.")
		return false
	}

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

	// See if "user" is valid 
	if (user.match(userPat)==null) {
	    // user is not valid
	    alert("Le nom de l'utilisateur ne semble pas valide.")
	    return false
	}

	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("L'adresse IP n'est pas valide.")
			return false
		    }
	    }
	    return true
	}

	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		alert("Le nom de domaine ne semble pas valide.")
		return false
	}

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	    domArr[domArr.length-1].length>3) {
	   alert("L'adresse doit se terminer par un domaine à 2 ou 3 lettres.")
	   return false
	}

	if (len<2) {
	   alert("Il manque la fin du domaine dans cette adresse!")
	   return false
	}
	
	return true;
}

function getEmplCode(Empl, Height, Width){
	returnCode = '<!--DEBUT PUB-->\n<script language="JavaScript">\n<!--\n';
	returnCode += '\tNumEmpl = ' + Empl + ';\n';
	returnCode += '\tHeight = ' + Height + ';\n\tWidth = ' + Width + ';\n//-->\n</script>\n';
	returnCode += '<script language="JavaScript" src="http://clk.yatoocar.com/js/affbanni.js">\n</script>\n';
	returnCode += '<noscript>\n\t<iframe src="http://regie.tricault.com/aff_banni.php?NumEmpl=' + Empl + '" height="' + Height + '" width="' + Width + '" Marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0"  scrolling="no" bordercolor="#000000">\n';
	returnCode += '\t\t<a href="http://regie.tricault.com/click_banni.php">\n';
	returnCode += '\t\t\t<img src="http://regie.tricault.com/aff_imgbanni.php?NumEmpl=' + Empl + '" border="0">\n';
	returnCode += '\t\t</a>\n';
	returnCode += '\t</iframe>\n';
	returnCode += '</noscript>\n<!--FIN PUB-->';
	
	return returnCode;
}

function getID(){
	var tabSearch = window.location.search.substr(1, window.location.search.length).split("=");
	
	for (var i=0; i <= tabSearch.length ; i += 2){
		if (tabSearch[i] == "ID"){
			return (tabSearch[i+1]);
		}
	}
	return;
}

var allCalk = new Array();
function replaceCalk(){
	if (IE){
		if (document.all["ImgCentre"]){
			var LeftPos = getAbsoluteLeft(document.all["ImgCentre"]);
			var TopPos = getAbsoluteTop(document.all["ImgCentre"]);
			for (var i=0; i< allCalk.length; i++){
				if (document.all[allCalk[i]]){ //Celui-ci existe
					document.all[allCalk[i]].style.left = LeftPos;
					document.all[allCalk[i]].style.top = TopPos;
				}
			}
		}
	}
}

function getAbsoluteLeft(MyObject){
	var centerWidth = 0;
	if (typeof(MyObject.offsetParent) == "object"){
		if(MyObject.offsetParent.tagName != "BODY"){
			centerWidth = MyObject.offsetLeft;
			centerWidth +=  getAbsoluteLeft(MyObject.offsetParent);
		}
	}
	return centerWidth;
}

function getAbsoluteTop(MyObject){
	var centerTop = MyObject.offsetTop;
	if (typeof(MyObject.offsetParent) == "object"){
		if(MyObject.offsetParent.tagName != "BODY"){
			centerTop +=  getAbsoluteTop(MyObject.offsetParent);
		}
	}
	return centerTop;
}

function gotoSearch(aSelect, aPage, aForm){
	if (! aPage){
		aPage = "";	
	}
	if (! aForm){
		aForm = document.formTypeAn;	
	}
	if (aSelect.options[aSelect.selectedIndex].value != ""){
		aForm.action = "http://annonces.yatoocar.com/" + aSelect.options[aSelect.selectedIndex].value + "/" + aPage;
		aForm.submit();
	}
}

function verifyImg(MyInputFileImg, MaxSize){
	//TheImg = document.imgTest;
	TheImg = new Image();
	TheSrc = TheImg.src;
	TheImg.src = "file://" + MyInputFileImg.value;
	var finAttente = Date.UTC(new Date()) + 2000; // On attent max 2 secondes sinon, on le check s'effectue coté serveur
	var fileExists = true;
	while (! TheImg.complete){
		if (Date.UTC(new Date()) > finAttente){
			//Plus de 1/2 seconde, on fait comme si le fichier existait et comme si la taille était bonne
			//Les controles vont s'effectués coté serveur	
			fileExists = false;
			break;
		}
	}
	if (fileExists){
		Filesize = TheImg.fileSize;
		TheImg.src = TheSrc;
		if (Filesize < 0){
			alert("Le format de l'image ne semble pas correct");
			return false;	
		}
		
		if (Filesize > MaxSize * 1024){
			alert("L'image est trop lourde (>" + MaxSize + "Ko)");
			return false;	
		}
	}
	
	return true;
}

function Diaporama(TheImagesSrc, TheImage){
	this.IDTime;
	this.Tempo=3000;
	this.curSrc=0;
	this.ImagesSrc = new Array();
	var sizeRatio = 0;
	for (var i=0; i < TheImagesSrc.length; i++){
		this.ImagesSrc[i] = new Image();
		this.ImagesSrc[i].src = TheImagesSrc[i];
	}
	this.Image = TheImage;
	return this;
}

function PlayDiaporama(){
	var Pas = 1;
	if (MyDiapo.curSrc + Pas < 0){
		MyDiapo.curSrc = MyDiapo.ImagesSrc.length + Pas;
	}else if (MyDiapo.curSrc + Pas >= MyDiapo.ImagesSrc.length){
		MyDiapo.curSrc = -1 + Pas;
	}else{
		MyDiapo.curSrc += Pas;
	}
	
	if (MyDiapo.curSrc < 0){
		MyDiapo.curSrc = 0;
	}else if (MyDiapo.curSrc >= MyDiapo.ImagesSrc.length){
		MyDiapo.curSrc = MyDiapo.ImagesSrc.length - 1;
	}
	
	MyDiapo.Image.src = MyDiapo.ImagesSrc[MyDiapo.curSrc].src;
	if (MyDiapo.ImagesSrc[MyDiapo.curSrc].height > MyDiapo.ImagesSrc[MyDiapo.curSrc].width){
		sizeRatio = 300 / MyDiapo.ImagesSrc[MyDiapo.curSrc].height;
	}else{
		sizeRatio = 300 / MyDiapo.ImagesSrc[MyDiapo.curSrc].width;
	}
	MyDiapo.Image.height = MyDiapo.ImagesSrc[MyDiapo.curSrc].height * sizeRatio;
	MyDiapo.Image.width = MyDiapo.ImagesSrc[MyDiapo.curSrc].width * sizeRatio;
	
	
	// On retrouve le numéro de l'image
	var NumDiapo = MyDiapo.Image.src.substr(MyDiapo.Image.src.indexOf("num") + 3, 1);
	if (NumDiapo == 1){
		NumDiapo = "Votre première photo";
	}else if(NumDiapo == 2){
		NumDiapo = "Votre deuxième photo";
	}else{
		NumDiapo = "Votre troisième photo";
	}
	MyDiapo.IDTime = setTimeout(PlayDiaporama, MyDiapo.Tempo);
}

function SetCookie (name, value) {
	var argv=SetCookie.arguments;
	var argc=SetCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=name+"="+escape(value)+
		((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
		((path==null) ? "" : ("; path="+path))+
		((domain==null) ? "" : ("; domain="+domain))+
		((secure==true) ? "; secure" : "");
}

