var Communes = Class.create({
	initialize:function(cp){
		var _self = this;
		new Ajax.Request('/actions,getVilles.php',{
			method:'post',
			parameters:'cp='+cp,
			onSuccess:function(xhr){_self.exec(xhr);}
		});
	},
	exec:function(xhr){
		var jso = xhr.responseJSON;
		if(jso.error)
			return;
		
		if(jso.communes){
			if(jso.communes.length == 0){
				var d = this.createBox();
				var p = new Element("p").update("Code postal inconnu");
				p.setStyle("color:red;font-weight:bold;");
				d.insert(p);
			}else
			if(jso.communes.length == 1){
				var d = this.createBox();
				var ihide = new Element("input",{'type':'hidden','name':'commune','value':jso.communes[0].id});
				var p = new Element("p").update(jso.communes[0].commune);
				d.insert(ihide);
				d.insert(p);

			}else{
				var select = new Element("select",{'name':'commune','class':'input250'});
				for(a=0;a<jso.communes.length;a++){
					var opt = new Element("option",{'value':jso.communes[a].id}).update(jso.communes[a].commune);
					select.insert(opt);

				}
				var d = this.createBox();
				d.insert(select);
			}
			var cont = $('codepos');
			Element.insert(cont,{'after':d});
		}
	},
	createBox:function(){
		if($('div_commune'))
			$('div_commune').remove();
		var Div1 = new Element('div',{'class':'formContener ligneBG','id':'div_commune'});
		var pleft = new Element('p',{'class':'formL'}).update("<b>Commune(s) :</b>");
		Div1.update(pleft);
		return Div1;
	}
});


var updatePays = Class.create({
	initialize:function(Obj,cat,idcat){
		var _self = this;
		this.obj = Obj;
		this.cat = cat;
		var params = 'do='+cat+"&id="+idcat;
		new Ajax.Request('/actions,getPays.php',{
			method:'post',
			parameters:params,
			onSuccess:function(xhr){_self.show(xhr);}
		});
	},
	show:function(xhr){
		var json = xhr.responseJSON;
		if(json.error){
			alert(json.error);
		}else{
			
			if(this.cat == "list"){
				for(var a in json.pays){
					var op = new Element('option',{'value':a}).update(json.pays[a]);
					this.obj.insert(op);
				}
			}else{
			var opt = new Element('option',{'value':""}).update("[Selectionner]");
			this.obj.insert(opt);
			for(var a=0;a<json.pays.length;a++){
				var op = new Element('option',{'value':a}).update(json.pays[a]);
				this.obj.insert(op);
			}
			}
			this.obj.show();
		}
	}
});


var overlay = Class.create({
	initialize:function(){
		this.body = document.getElementsByTagName("body")[0];
		
		this.overlay = new Element("div",{id:'overlay','class':'overlay'});
		this.box = new Element("div",{
			id:'overlayBox',
			'class':'overlayBox'
		}).update("Hello World");;
		this.overlay.insert(this.box);

		this.body.insert({'top':this.overlay});

		var boxWidth = Number(this.box.getWidth());
		var boxHeight = Number(this.box.getHeight());
		var screenWidth = Number(this.overlay.getWidth());
		var screenHeight = Number(this.overlay.getHeight());
		
		this.box.style.left = ((screenWidth/2)-(boxWidth/2))+"px";
		this.box.style.top = ((screenHeight/2)-(boxHeight/2))+"px";
	},
	show:function(){
		document.getElementsByTagName("html")[0].style.overflow = 'hidden';
		this.overlay.style.display = 'block';			
	},
	hide:function(){
		document.getElementsByTagName("html")[0].style.overflow = 'auto';
		this.overlay.hide();
	}
});

/*document.observe("dom:loaded",function(){
	var over = new overlay();
	over.show();
});*/

function getRegion(id){
	if(id != 0){
		$('regionCont').hide();
		$('depCont').hide();
		$('regionSelect').update();
		$('depSelect').update();
		return;
	}
	var dt = new Date();
	new Ajax.Request("/actions,getRegion.php?v="+dt.getTime(),{
		method:'post',
		parameters:'id='+id,
		onSuccess:function(xhr){
			var json=xhr.responseJSON;
			if(json.type = 1){
				var selectRegion = $('regionSelect');
				if(!selectRegion)
					return;
				var opt = new Element('option',{'value':""}).update("Toutes");
				selectRegion.update(opt);
				for(var a=0;a<json.regionFR.length;a++){
					var opt = new Element('option',{'value':a}).update(json.regionFR[a]);
					selectRegion.insert(opt);
				}
				$('regionCont').show();	
			}
		}
	});
}

function getDepartement(id){
	var dt = new Date();
	new Ajax.Request("/actions,getDepartement.php?v="+dt.getTime(),{
		method:'post',
		parameters:'id='+id,
		onSuccess:function(xhr){
			var json=xhr.responseJSON;
			var selectRegion = $('depSelect');
			if(!selectRegion)
				return;
			var opt = new Element('option',{'value':""}).update("Tous");
			selectRegion.update(opt);
			for(var a in json.depFR){
				var opt = new Element('option',{'value':a}).update(json.depFR[a]);
				selectRegion.insert(opt);
			}
			$('depCont').show();	
		}
	});
}
