function validForm(frm) {
	var tags = new Array('input','textarea','select');
	for (var k=0; k<tags.length; k++){
		if (typeof(frm) != 'undefined') 
			var sfElems = frm.getElementsByTagName(tags[k]);
		else 
			var sfElems = document.getElementsByTagName(tags[k]);
		
		for (var i = 0; i < sfElems.length; i++) {
			if (sfElems[i].value == "" && sfElems[i].getAttribute('obg')=="true") {
				alert(sfElems[i].getAttribute('err'));
				sfElems[i].focus();
				return(false);
			}
			else if (sfElems[i].getAttribute('obg')=="check" && sfElems[i].checked == false) {
				alert(sfElems[i].getAttribute('err'));
				sfElems[i].focus();
				return(false);
			}
			
			switch(sfElems[i].getAttribute("special")){
				
				case null : isValid = true;
							break;
				case "email" : 	isValid = (sfElems[i].getAttribute("obg") != "true" && sfElems[i].value == '') ? true : validMail(sfElems[i].value);
								oComments = "\nEx.: nom@site.com";
								break;
				case "date" :  	isValid = (sfElems[i].getAttribute("obg") != "true" && sfElems[i].value == '') ? true :  validDate(sfElems[i].value);
								oComments = "\nEx.: 1982-03-25";
								break;
				case "nospecial" :  isValid = (sfElems[i].getAttribute("obg") != "true" && sfElems[i].value == '') ? true :  validSpecial(sfElems[i].value);
									oComments = "\nEx.: a-z, A-Z, 0-9";
									break;
				case "numeric" :  	isValid = (sfElems[i].getAttribute("obg") != "true" && sfElems[i].value == '') ? true :  validNumeric(sfElems[i].value);
									oComments = '';
									if (isValid && sfElems[i].getAttribute('minval') != null &&  parseFloat(sfElems[i].value) < parseFloat(sfElems[i].getAttribute('minval'))) {
										isValid=false;	
									}
									if (isValid && sfElems[i].getAttribute('maxval') != null &&  parseFloat(sfElems[i].value) > parseFloat(sfElems[i].getAttribute('maxval'))) {
										isValid=false;	
									}
									
									break;
				case "mustequal" : 
								oComments = '';
								if (sfElems[i].value != document.getElementById(sfElems[i].getAttribute('depend_id')).value) isValid=false;
								else isValid=true;
								break;	
				case "tel" : 	isValid = (sfElems[i].getAttribute("obg") != "true" && sfElems[i].value == '') ? true :  validTel(sfElems[i].value);
								oComments = "\nEx.: 819-379-8614";
								break;
					
			}
			if(!isValid){
				alert(sfElems[i].getAttribute("errorspecial")+oComments);
				sfElems[i].focus();
				return false;
			}
			if (sfElems[i].getAttribute("minlength") != null && sfElems[i].value.length < sfElems[i].getAttribute("minlength")) {
				alert(sfElems[i].getAttribute("errorlength"));
				sfElems[i].focus();
				return false;
			}
			
		}
	}
	return(true);
}

function validDate(dateaaaammjj) {
	var dt=dateaaaammjj.split("-"),date=new Date(dt[0],dt[1]-1,dt[2]);
	return date.getDate()==dt[2]&&date.getMonth()+1==dt[1]&&date.getFullYear()==dt[0]?date:false;
}

function validSpecial(fStr){
	myReg = new RegExp("[A-Za-z0-9]+");
	if(myReg.exec(fStr)!=fStr){return false};
	return true;
}

function validNumeric(fStr){
	myReg = new RegExp("[0-9.]+");
	if(myReg.exec(fStr)!=fStr){return false};
	return true;
}


function checkDependency(obg) {
	if (obg.getAttribute('depend')!="")
	{
		var sfElems = document.getElementsByTagName('input');
		for (var i = 0; i < sfElems.length; i++) {
			if (obg.getAttribute('depend')== sfElems[i].name && obg.value=="" && sfElems[i].checked==true && sfElems[i].value=="1") {
				//alert(obg.getAttribute('err'));
				return(false);
			}
		}
		
	}
	return(true);
}

function validMail(email) {
	var result = false
	var theStr = new String(email)
	var index = theStr.indexOf("@");
	if (index > 0) {
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1)) result = true;
	}
	return result;
}

function validTel(val) {
	if (val.match(/\(?\d{3}\)?([-\/\.])\d{3}\1\d{4}/) || val.match(/\d{10}/)) return true;
}
function validCodePostal(code,message) {
	code = code.toUpperCase();
	if (code.match(/^[A-Z][0-9][A-Z]$/)) return true;
	alert(message);
	return false;
} 
function changeTitre(TexteTitre) {
	document.getElementById('MenuTitre').removeAttribute('style');
	document.getElementById('MenuTitre').innerHTML = TexteTitre;
}
function getFormValues(form) {
	var formValues ='';
	var arrInput = form.getElementsByTagName('input');
	for (var i = 0; i < arrInput.length; i++) {
		if (arrInput[i].name != 'Submit') formValues = formValues + '&' + arrInput[i].name + '=' + arrInput[i].value;
	}
	var arrSelect = form.getElementsByTagName('select');
	for (var i = 0; i < arrSelect.length; i++) {
		formValues = formValues + '&' + arrSelect[i].name + '=' + arrSelect[i].value;
	}
	return(formValues);
}
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function counterUpdate(countedTextBox, countBody, opt_maxSize) {
        var maxSize = opt_maxSize ? opt_maxSize : 1024;
	
        var field = document.getElementById(countedTextBox);

        if (field && field.value.length >= maxSize) {
                field.value = field.value.substring(0, maxSize);
        }
        var txtField = document.getElementById(countBody);
                if (txtField) { 
                txtField.innerHTML = field.value.length;
        }
}

function atLeastOne(form,Prefixe) {
	arrInput = document.getElementsByTagName('input');
	for (i=0; i < arrInput.length; i++) {
		if (arrInput[i].name.substr(0,Prefixe.length) == Prefixe && arrInput[i].name != Prefixe + '0') {
			if (arrInput[i].checked) return(true);
		}
	}
	alert('Veuillez choisir au moins une catégorie');
	return(false);
}

function chooseAgent() {
	var Flushed = '';
	if (document.getElementById('PersonneID').value == getCheckedValue(document.formChoix.PersonneIDChoix)) { 
		for(var i = 0; i < document.formChoix.PersonneIDChoix.length; i++) {
			if(!document.formChoix.PersonneIDChoix[i].checked) Flushed = Flushed + document.formChoix.PersonneIDChoix[i].value + '|';
		} 
	}
	document.getElementById('FlushedAgents').value = Flushed;
	document.getElementById('PersonneID').value = getCheckedValue(document.formChoix.PersonneIDChoix);
	document.form.submit();
}

function verifSubmit() {
	if (document.getElementById('MessageBox').innerHTML.indexOf('Veuillez patientez') != -1) {
		document.form.submit();
	}
}

function valid_down(message, lan) {
	//return(true);
	var arrMnt = [];
	var arrDown = [];
	var arrInput = document.getElementsByTagName('input');
	for (var i = 0; i < arrInput.length; i++) {
		if (arrInput[i].name.indexOf('sale_price') != -1) {
			if (lan == 'fr') mnt = parseFloat(arrInput[i].value.replace(' ','').replace(',','.')); else mnt = parseFloat(arrInput[i].value.replace(',',''));
			ext = arrInput[i].name.substr(10);
			if (ext == '') ext = 0;
			arrMnt[ext] = mnt;
		}
		if (arrInput[i].name.indexOf('down_percent') != -1) {
			if (lan == 'fr') down = parseFloat(arrInput[i].value.replace(' ','').replace(',','.')); else down = parseFloat(arrInput[i].value.replace(',',''));
			ext = arrInput[i].name.substr(12);
			if (ext == '') ext = 0;
			arrDown[ext] = down;
		}
	}
	
	for (i=0; i < arrMnt.length; i++) {
		if (arrMnt[i]/20 > arrDown[i]) {
			alert(message);
			return false;
		}
	}
	return true;
}

var defaultToken = '30886eec-a8bf-11dc-8314-0800200c9a66';
var URLRech = "http://www.toutpourlimmobilier.com/recherche_cio/recherche.php?mode=Resultats&dispatch=srch&match.typeBatiment=&cat=&match.indLocation=false&locale=ZLANZ&match.dateCreationFrom=&match.superficieTerrainTo=&mode=Resultats&token=ZTOKENZ&match.superficieHabitableTo=&match.prixDemandeFrom=&match.nombreSalleBainTo=&match.nombreChambreFrom=&match.nombreChambreType=NB_CHAMBRES&match.superficieHabitableType=&match.superficieHabitableFrom=&match.genrePropriete=&match.caracteristiques=&match.prixDemandeTo=&match.superficieTerrainType=&match.categoriePropriete=UNI&match.nombreChambreTo=&match.noMLS[0]=ZNOSIAZ&match.nombreSalleBainFrom=&match.superficieTerrainFrom=&nbParPages=1&currentPage=0";
canMove = true;

function openInfo(imgObj,lan,token) {
	var token = (token === undefined) ? defaultToken : token;

	TheURL = URLRech.replace('ZTOKENZ',token)
	TheURL = TheURL.replace('ZNOSIAZ',imgObj.getAttribute('NoSIA'))
	TheURL = TheURL.replace('ZLANZ',lan);
	window.open(TheURL,'Mandat','scrollbars=yes,width=700,height=550');
}

function movePics(direction) {
	if (canMove) {
		canMove = false;
		if (direction == 'right') {
			// Movement and scaling of the slides
			toLoad = parseFloat($('img04').getAttribute('imgLoaded'));
			toLoad = toLoad + 1;
			if (toLoad == arrMandats.length) toLoad = 0;
			new Effect.Move('divImg01', { x:-125, y:0, mode:'relative', transition: Effect.Transitions.linear});
			new Effect.Scale('img01',67, {scaleMode: { originalWidth: 75, originalHeight: 52 }});
			new Effect.Move('divImg02', { x:-125, y:0, mode:'relative', transition: Effect.Transitions.linear});
			new Effect.Scale('img02',62, { scaleMode: { originalWidth: 120, originalHeight: 84 }});
			new Effect.Move('divImg03', { x:-125, y:0, mode:'relative', transition: Effect.Transitions.linear});
			new Effect.Scale('img03',160, { scaleMode: { originalWidth: 75, originalHeight: 52 }});
			new Effect.Move('divImg04', { x:-125, y:0, mode:'relative', transition: Effect.Transitions.linear});
			new Effect.Scale('img04',150, { scaleMode: { originalWidth: 50, originalHeight: 35 }});
			new Effect.Move('divImg00', { x:500, y:0, mode:'relative', transition: Effect.Transitions.full});
			// Renaming of the slides to prepare for the next move
			$('divImg00').id = 'tmpDiv';
			$('img00').id = 'tmpImg';
			for (i = 1; i<=4; i++) {
				$('divImg0' + i).id = 'divImg0' + (i-1);
				$('img0' + i).id = 'img0' + (i-1);
			}
			$('tmpDiv').id = 'divImg04';
			$('tmpImg').id = 'img04';
			// Loading of the new slide in the hidden div
			$('img04').src = arrMandats[toLoad].imgSrc;
			$('img04').setAttribute('imgLoaded',toLoad);
			//$('img04').setAttribute('onclick',"MM_openBrWindow('" + URLRech.replace('ZNOSIAZ',arrMandats[toLoad].NoSIA) + "','Mandat','scrollbars=yes,width=700,height=550');");
			$('img04').setAttribute('NoSIA',arrMandats[toLoad].NoSIA);
			$('img04').setAttribute('alt',arrMandats[toLoad].Adresse);
			$('img04').setAttribute('title',arrMandats[toLoad].Adresse);
			toLoad = toLoad - 4;
			if (toLoad < 0) toLoad = toLoad + arrMandats.length;
			$('img00').setAttribute('imgLoaded',toLoad);
			picInFront = parseFloat($('PicNo').innerHTML) + 1;
			if (picInFront > arrMandats.length) picInFront = 1;
			$('PicNo').innerHTML = picInFront;
		} else {
			// Movement and scaling of the slides
			toLoad = parseFloat($('img00').getAttribute('imgLoaded'));
			toLoad = toLoad - 1;
			if (toLoad == -1) toLoad = (arrMandats.length-1);
			new Effect.Move('divImg00', { x:125, y:0, mode:'relative', transition: Effect.Transitions.linear});
			new Effect.Scale('img00',150, {scaleMode: { originalWidth: 50, originalHeight: 35 }});
			new Effect.Move('divImg01', { x:125, y:0, mode:'relative', transition: Effect.Transitions.linear});
			new Effect.Scale('img01',160, { scaleMode: { originalWidth: 75, originalHeight: 52 }});
			new Effect.Move('divImg02', { x:125, y:0, mode:'relative', transition: Effect.Transitions.linear});
			new Effect.Scale('img02',62, { scaleMode: { originalWidth: 120, originalHeight: 84 }});
			new Effect.Move('divImg03', { x:125, y:0, mode:'relative', transition: Effect.Transitions.linear});
			new Effect.Scale('img03',67, { scaleMode: { originalWidth: 75, originalHeight: 52 }});
			new Effect.Move('divImg04', { x:-500, y:0, mode:'relative', transition: Effect.Transitions.full});
			// Renaming of the slides to prepare for the next move
			$('divImg04').id = 'tmpDiv';
			$('img04').id = 'tmpImg';
			for (i = 1; i<=4; i++) {
				$('divImg0' + (4-i)).id = 'divImg0' + (4-(i-1));
				$('img0' + (4-i)).id = 'img0' + + (4-(i-1));
			}
			$('tmpDiv').id = 'divImg00';
			$('tmpImg').id = 'img00';
			// Loading of the new slide in the hidden div
			$('img00').src = arrMandats[toLoad].imgSrc;
			$('img00').setAttribute('imgLoaded',toLoad);
			//$('img00').setAttribute('onclick',"MM_openBrWindow('" + URLRech.replace('ZNOSIAZ',arrMandats[toLoad].NoSIA) + "','Mandat','scrollbars=yes,width=700,height=550');");
			$('img00').setAttribute('NoSIA',arrMandats[toLoad].NoSIA);
			$('img00').setAttribute('alt',arrMandats[toLoad].Adresse);
			$('img00').setAttribute('title',arrMandats[toLoad].Adresse);
			toLoad = toLoad + 4;
			if (toLoad >= arrMandats.length) toLoad = toLoad - arrMandats.length;
			$('img04').setAttribute('imgLoaded',toLoad);
			picInFront = parseFloat($('PicNo').innerHTML) - 1;
			if (picInFront == 0) picInFront = arrMandats.length;
			$('PicNo').innerHTML = picInFront;
		}
		setTimeout('canMove=true;',800); //Click happy people protection
		return(false);
	} else {
		return(false);
	}
}

function movePics2(direction,nbrMandats, paramString) {
	if (nbrMandats < 4) maxSlides = nbrMandats; else  maxSlides = 4;
	if (canMove) {
		canMove = false;
		if (direction == 'right') {
			// Movement and scaling of the slides
			toLoad = parseFloat($('img04').getAttribute('imgLoaded'));
			toLoad = toLoad + 1;
			if (toLoad == nbrMandats) toLoad = 0;
			new Effect.Move('divImg01', { x:-125, y:0, mode:'relative', transition: Effect.Transitions.linear});
			new Effect.Scale('img01',67, {scaleMode: { originalWidth: 75, originalHeight: 52 }});
			new Effect.Move('divImg02', { x:-125, y:0, mode:'relative', transition: Effect.Transitions.linear});
			new Effect.Scale('img02',62, { scaleMode: { originalWidth: 120, originalHeight: 84 }});
			new Effect.Move('divImg03', { x:-125, y:0, mode:'relative', transition: Effect.Transitions.linear});
			new Effect.Scale('img03',160, { scaleMode: { originalWidth: 75, originalHeight: 52 }});
			new Effect.Move('divImg04', { x:-125, y:0, mode:'relative', transition: Effect.Transitions.linear});
			new Effect.Scale('img04',150, { scaleMode: { originalWidth: 50, originalHeight: 35 }});
			new Effect.Move('divImg00', { x:500, y:0, mode:'relative', transition: Effect.Transitions.full});
			// Renaming of the slides to prepare for the next move
			$('divImg00').id = 'tmpDiv';
			$('img00').id = 'tmpImg';
			for (i = 1; i<=4; i++) {
				$('divImg0' + i).id = 'divImg0' + (i-1);
				$('img0' + i).id = 'img0' + (i-1);
			}
			$('tmpDiv').id = 'divImg04';
			$('tmpImg').id = 'img04';
			// Loading of the new slide in the hidden div
			if (paramString != '') theURL = '/includes/slider.php?' + paramString + '&toLoad=' + toLoad + '&imgID=04'; else theURL = '/includes/slider.php?toLoad=' + toLoad + '&imgID=04';
			new Ajax.Updater('divImg04', theURL, { method: 'get' });
			
			toLoad = toLoad - maxSlides;
			if (toLoad < 0) toLoad = toLoad + nbrMandats;
			$('img00').setAttribute('imgLoaded',toLoad);
			picInFront = parseFloat($('PicNo').innerHTML) + 1;
			if (picInFront > nbrMandats) picInFront = 1;
			$('PicNo').innerHTML = picInFront;
		} else {
			// Movement and scaling of the slides
			toLoad = parseFloat($('img00').getAttribute('imgLoaded'));
			toLoad = toLoad - 1;
			if (toLoad == -1) toLoad = (nbrMandats-1);
			new Effect.Move('divImg00', { x:125, y:0, mode:'relative', transition: Effect.Transitions.linear});
			new Effect.Scale('img00',150, {scaleMode: { originalWidth: 50, originalHeight: 35 }});
			new Effect.Move('divImg01', { x:125, y:0, mode:'relative', transition: Effect.Transitions.linear});
			new Effect.Scale('img01',160, { scaleMode: { originalWidth: 75, originalHeight: 52 }});
			new Effect.Move('divImg02', { x:125, y:0, mode:'relative', transition: Effect.Transitions.linear});
			new Effect.Scale('img02',62, { scaleMode: { originalWidth: 120, originalHeight: 84 }});
			new Effect.Move('divImg03', { x:125, y:0, mode:'relative', transition: Effect.Transitions.linear});
			new Effect.Scale('img03',67, { scaleMode: { originalWidth: 75, originalHeight: 52 }});
			new Effect.Move('divImg04', { x:-500, y:0, mode:'relative', transition: Effect.Transitions.full});
			// Renaming of the slides to prepare for the next move
			$('divImg04').id = 'tmpDiv';
			$('img04').id = 'tmpImg';
			for (i = 1; i<=4; i++) {
				$('divImg0' + (4-i)).id = 'divImg0' + (4-(i-1));
				$('img0' + (4-i)).id = 'img0' + + (4-(i-1));
			}
			$('tmpDiv').id = 'divImg00';
			$('tmpImg').id = 'img00';
			// Loading of the new slide in the hidden div
			if (paramString != '') theURL = '/includes/slider.php?' + paramString + '&toLoad=' + toLoad + '&imgID=00'; else theURL = '/includes/slider.php?toLoad=' + toLoad + '&imgID=00';
			new Ajax.Updater('divImg00', theURL, { method: 'get' });

			toLoad = toLoad + maxSlides;
			if (toLoad >= nbrMandats) toLoad = toLoad - nbrMandats;
			$('img04').setAttribute('imgLoaded',toLoad);
			picInFront = parseFloat($('PicNo').innerHTML) - 1;
			if (picInFront == 0) picInFront = nbrMandats;
			$('PicNo').innerHTML = picInFront;
		}
		setTimeout('canMove=true;',800); //Click happy people protection
		return(false);
	} else {
		return(false);
	}
}

function uncheckAll(Tous, Prefixe) {
	if (Tous) {
		arrInput = document.getElementsByTagName('input');
		for (i=0; i < arrInput.length; i++) {
			if (arrInput[i].name.substr(0,Prefixe.length) == Prefixe && arrInput[i].name != Prefixe + '0') arrInput[i].checked = false;
		}
	} else {
		$(Prefixe + '0').checked = false;
	}
}

function filterSlider() {
	if ($('Ville0').checked == true) {
		paramString = '';
	} else {
		paramString = '?Ville=';
		arrInput = document.getElementsByTagName('input');
		for (i=0; i < arrInput.length; i++) {
			if (arrInput[i].name.substr(0,5) == 'Ville' && arrInput[i].name != 'Ville0' && arrInput[i].checked) {
				paramString += arrInput[i].value + '|';
			}
		}
	}
	if ($('Type0').checked == false) {
		if (paramString == '') paramString = '?Type='; else paramString += '&Type=';
		arrInput = document.getElementsByTagName('input');
		for (i=0; i < arrInput.length; i++) {
			if (arrInput[i].name.substr(0,4) == 'Type' && arrInput[i].name != 'Type0' && arrInput[i].checked) {
				paramString += arrInput[i].value + '|';
			}
		}
	}
	new Ajax.Updater('SliderHolder', '/includes/slider.php' + paramString, { method: 'get' });
}

function setCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function unsetCookie(name) {
	setCookie(name,"",-1);
}

function initDrag() {
	dragCalc = new Draggable('Calculateur', { onEnd: function() {
		setCookie('calcLeft',Position.page($('CalculateurHeader'))[0],7);
		setCookie('calcTop',Position.page($('CalculateurHeader'))[1],7);
	}} );
}
