var img = null;

var flag = false;
var current_region = null;

function get(id){
	return document.getElementById(id);
}

function prechargimg() {
	var doc=document;
	if(doc.images){
		if(!doc.precharg) doc.precharg=new Array();
		var i,j=doc.precharg.length,x=prechargimg.arguments;
		for(i=0; i<x.length; i++)
		if (x[i].indexOf("#")!=0){
			doc.precharg[j]=new Image;
			doc.precharg[j++].src=x[i];
		}
	}
}

function reset_map(){
	if(flag == true) return;
	get('map_image').src='images/france/carte_france.gif';
	get('text_region').style.visibility="hidden";

	current_region = null;
}

function select_map(region,left,top,nom){
	flag = true;
	get('map_image').src='images/france/'+region+'.gif';
	get('text_region').style.top=top+'px';
	get('text_region').style.left=left+'px';
	get('text_region').innerHTML=nom;
	get('text_region').style.visibility="visible";

	current_region = region;
}

function switch_menu(menu_id,nb){
	var height=16;
	var style = get('menu_'+menu_id).style;

	if(style.height == (nb*height)+'px'){
		close('menu_'+menu_id,(nb*height));
	}
	else if(style.height == min_height+'px' || style.height == ''){
		for( i = 0 ; i < nb_menus ; i++){
			if(i != menu_id){
				if(get('menu_'+i).style.height != min_height+'px' &&  get('menu_'+i).style.height != '') close('menu_'+i,(nb*height));
			}
			else open_menu('menu_'+i,min_height,(nb*height));
		}
	}
}

function select_menu(menu_id,nb){
	var height=16;
	var style = get('menu_'+menu_id).style;
	open_menu('menu_'+menu_id,min_height,(nb*height),1000);
}

function open_menu(menu_id,cur_height,max_height,step){
	if(step == null) step = 16;
	cur_height+=step;
	if(cur_height >= max_height){
		get(menu_id).style.height = max_height+'px';
	}else{
		get(menu_id).style.height = cur_height+'px';
		setTimeout ("open_menu('"+menu_id+"',"+cur_height+","+max_height+")",40);
	}
}

function close(menu_id,cur_height,step){
	if(step == null) step = 16;
	cur_height-=step;
	if(cur_height <= min_height){
		get(menu_id).style.height = min_height+'px';
	}else{
		get(menu_id).style.height = cur_height+'px';
		setTimeout ("close('"+menu_id+"',"+cur_height+")",40);
	}
}

function switch_select(select_id, nb){
	var height=13;
	var style = get('select_'+select_id).style;

	if(style.height == (nb*height)+'px'){
		close('select_'+select_id,(nb*height),32);
	}
	else if(style.height == min_height+'px' || style.height == ''){
		open_menu('select_'+select_id,min_height,(nb*height),32);
	}
}

function select(select_id, value){
	get('select_'+select_id+'_value').value = value;
	close('select_'+select_id,get('select_'+select_id).style.height.substr(0,get('select_'+select_id).style.height.length - 2));
}

function selectEcole(ecole_id){
	window.location='index.php?part=content&action=details&ecole_id='+ecole_id;
}

function search(zip){
	if(zip) window.location='index.php?part=content&action=search&reset=1&zip='+zip;
	else window.location='index.php?part=content&action=search';
}

function research(){
	if(!get('select_1_value').value || get('select_1_value').value == '== Département =='){
		if(!get('keyword').value || get('keyword').value== 'Mot clé'){
			//if(get('permis').value < 1){
			alert('Vous devez renseigner au moins un critère parmis :\n  - Département\n  - Mot clé.'); //\n  - Permis
			return;
			//}
		}
	}


	var keyword = '';

	if(!get('keyword').value || get('keyword').value== 'Mot clé') keyword = '';
	else{
		keyword = escape(get('keyword').value);
		keyword = keyword.replace(/\//g," ");
		keyword = keyword.replace(/&/g," ");
		keyword = keyword.replace(/"/g," ");
		keyword = keyword.replace(/'/g," ");

		keyword='-'+ keyword;
	}

	var zip = get('select_1_value').value == '== Département ==' ? '' : get('select_1_value').value;

	var permis = get('permis').value == 0 ? '' :  get('permis').value;

	get('search_form').submit();
}


function home(){
	window.location='index.php';
}

function selectDoc(doc_id){
	window.location='index.php?part=content&action=doc&doc_id='+doc_id;
}

function selectAdvices(ecole_id){
	window.location='index.php?part=content&action=advice&ecole_id='+ecole_id;
}

function addAdviceForm(ecole_id){
	window.location='index.php?part=content&action=add_advice_form&ecole_id='+ecole_id;
}

function selectPermis(permis_id){
	get('permis').value = permis_id;
	get('permis_'+ permis_id).className='permis_selected';
	for(i = 1 ; i <= nb_permis ; i++){
		if(i != permis_id) get('permis_'+ i).className='permis_unselected';
	}
}

function addAdvice(){
	var err='';

	if(!get('name').value){
		err+='Vous devez renseigner le champs "Nom".\n';
	}

	if(!get('email').value){
		err+='Vous devez renseigner le champs "Email".\n';
	}else if(!testEmail(get('email').value)){
		err+='Vous correctement devez renseigner le champs "Email".\n';
	}

	if(!get('zip').value){
		err+='Vous devez renseigner le champs "Code Postal".\n';
	}else if(!testZip(get('zip').value)){
		err+='Vous correctement devez renseigner le champs "Code Postal".\n';
	}

	if(get('note').value == -1){
		err+='Vous devez indiquer une note.\n';
	}

	if(!get('text').value){
		err+='Vous devez renseigner le champs "Avis".\n';
	}

	if(err == ''){
		get('advice_form').submit();
	}else{
		alert(err);
	}
}

function submit_contact(){
	var err='';

	if(!get('ecole_name').value){
		err+='Vous devez renseigner le champs "Nom de l\'auto-école".\n';
	}

	if(!get('mail').value){
		err+='Vous devez renseigner le champs "Adresse".\n';
	}

	if(!get('zip').value){
		err+='Vous devez renseigner le champs "Code postal".\n';
	}else if(!testZip(get('zip').value)){
		err+='Le champs "Code postal" n\'est pas correctement renseigné.\n';
	}

	if(!get('city').value){
		err+='Vous devez renseigner le champs "Commune".\n';
	}

	if(!get('gender').value){
		err+='Vous devez renseigner le champs "Civilité".\n';
	}

	if(!get('name').value){
		err+='Vous devez renseigner le champs "Nom".\n';
	}

	if(!get('firstname').value){
		err+='Vous devez renseigner le champs "Prénom".\n';
	}

	if(!get('email').value && !get('phone').value){
		err+='Vous devez renseigner au moins un des champs parmis "Téléphone" et "Email".\n';
	}

	if(get('email').value && !testEmail(get('email').value)){
		err+='Le champs "Email" n\'est pas correctement renseigné.\n';
	}

	if(!get('subject').value){
		err+='Vous devez renseigner le champs "Objet".\n';
	}

	if(!get('body').value){
		err+='Vous devez renseigner le champs "Message".\n';
	}

	if(err != ''){
		alert(err);
	}else{
		get('contact_form').submit();
	}
}

function testEmail(string) {
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
	return true;
	else
	return false;
}

function testZip(zip){
	var reg = /[0-9]{5}/i;
	return reg.test(zip);
}

function findPos(obj) {
	var curleft = obj.offsetLeft || 0;
	var curtop = obj.offsetTop || 0;
	while (obj = obj.offsetParent) {
		curleft += obj.offsetLeft
		curtop += obj.offsetTop
	}
	return {x:curleft,y:curtop};
}

function display(photo_id,photo_alt){
	img = new Image();
	img.src= 'index.php?dphoto='+photo_id;
	setTimeout("_display("+photo_id+",'"+photo_alt.replace('\'','')+"')",200);
}

function _display(photo_id,photo_alt){
	if(!img.complete){
		setTimeout("_display("+photo_id+",'"+photo_alt+"')",200);
	}else{
		get('photo_img').src = 'index.php?dphoto='+photo_id;
		get('photo_img').alt = photo_alt;
		var dim = getDim();
		get('photo').style.left = (Math.round((dim.width - img.width)/2)) + 'px';
		get('photo').style.visibility='visible';
	}
}

function getDim(){
	if (document.body){
		var width = (document.body.clientWidth);
		var height = (document.body.clientHeight);
	}else{
		var width = (window.innerWidth);
		var height = (window.innerHeight);
	}
	return {width:width,height:height};
}

function selectMiniSite(ecole_id, part){
	window.location = 'index.php?part=content&action=details&ecole_id='+ecole_id+'&minisite='+part;
}

function getDimm(){

	 var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;

		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth;
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}

		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
}



