var Formulaire = Class.create();

Formulaire.prototype = {
	initialize : function() {
	},
	
	send: function(){
	ajustDimension('exec','contact_form',40,30);
	Effect.Appear('exec');
	document.getElementById('exec_data').innerHTML = '<img src=\'images/loading.gif\' />';
	protoAjax("contact.php","POST","exec_data","contact","loading","");
	},
	
	closeAndClear: function(){
	document.getElementById('contact').reset();
	Effect.Fade('exec');
	}
	

}

var Formulaire = new Formulaire();

//AGENDA

function nextDate(){
	if(document.getElementById('month_select').value < 12)
		{
		document.getElementById('month_select').selectedIndex += 1; 
		}
	else{
		document.getElementById('month_select').selectedIndex = 0;
		document.getElementById('year_select').selectedIndex += 1;
		}
	document.getElementById('calendar_form').submit();
}

function previewDate(){
	if(document.getElementById('month_select').value > 1)
		{
		document.getElementById('month_select').selectedIndex -= 1; 
		}
	else{
		document.getElementById('month_select').selectedIndex = 11; 
		document.getElementById('year_select').selectedIndex -= 1;
		}
	document.getElementById('calendar_form').submit();
}


/*Slideshow*/

var slideShow = Class.create();
var idObjet = 0;  

slideShow.prototype = {
	initialize : function(dest,timing,pictures) {
		idObjet++; 
		this.idRef = "CONSTRUCTEUR" + idObjet; 
  		window[this.idRef ] = this;  
		this.dest = dest;
		this.timing = Math.floor(Math.random()*10)+timing;
		this.pictures = pictures;
		this.totalPictures = this.pictures.length;
		this.currentPicture = 0;
		this.preload = new Image();
		this.nextPicture = (this.totalPictures > 1)? this.currentPicture + 1 : 0;
		
	},

	startSlideShow : function(){
		if(this.totalPictures > 1)
		this.LoadPicture();
		
	},
	
	LoadPicture : function() {
		$(this.dest).src = this.pictures[this.currentPicture];
		$(this.preload).src = this.pictures[this.nextPicture];
		$(this.preload).style.display ="none";
		new Effect.Appear($(this.dest), {duration:1});
		this.Display();
	},
	
	Display : function() {
	setTimeout("window."+this.idRef+".ChangePicture()",this.timing * 1000);  

	},
	
	ChangePicture : function(){
		
		this.currentPicture++;
		this.nextPicture = this.currentPicture + 1 ;
		// si c’est la dernière
		if (this.currentPicture == this.totalPictures)
			{
			this.currentPicture = 0;
			this.nextPicture = 1;
			}
		new Effect.Fade($(this.dest), {duration:1});
		this.Wait();
	},
	
	Wait : function () {
		setTimeout("window."+this.idRef+".LoadPicture()", 1000);
	}
}

var Reservation = Class.create();

Reservation.prototype = {
  initialize: function() {
  },
  
  reserveSelected: function() {
   document.getElementById('calendar_form').action = "reservation_reserver.html";
   document.getElementById('calendar_form').submit();
  },
  
  clearSelected: function(){
	  document.getElementById('do').value = "clear";
	  document.getElementById('calendar_form').submit(); 
  },
  
  formuleInfos: function(){
	  Lightview.show({href: 'reservation.php',rel: 'ajax',title: '',
	  options: {autosize: true,topclose: true,ajax: {method: 'POST',
		  parameters: 'action=formuleInfos',
		  onComplete: function (){}
		}
	  }
	});
  },
  
  record: function(){
	  //Formater le formulaire
	  alphaOnly('firstname');Uppercase('firstname');
	  alphaOnly('name');ucfirst('name');
	  streetOnly('street');
	  alphaNumericOnly('cp');Uppercase('cp');
	  cityOnly('city');Uppercase('city');
	  alphaOnly('country');Uppercase('country');
	  phoneOnly('tel');
	  phoneOnly('gsm');
	  emailOnly('email');
	  
	  ajustDimension('exec','reservation_coordonnee',5,5);
	  Effect.Appear('exec');
	  document.getElementById('exec_data').innerHTML = '<img src=\'images/loading.gif\' />';
	  document.getElementById('action').value = "record";
	  protoAjax("reservation.php","POST","exec_data","reservation_coordonnee","loading","");
	  
  },
  
  cancel: function(){
	  document.getElementById('reservation_coordonnee').reset();
	  refreshCaptcha();
	  document.getElementById('do').value = "clear";
	  document.getElementById('reservation_coordonnee').action = "reservation_calendrier.html";
      document.getElementById('reservation_coordonnee').submit();
	  
  },
  
  edit: function(room_id,thisdate){
	  document.getElementById('calendar_form').action = "admin_reservation_edit.html";
	  document.getElementById('room_id').value = room_id;
	  document.getElementById('date_selected').value = thisdate;
	  document.getElementById('calendar_form').submit();
  },
  
  edit_exec: function(id){
	  document.getElementById('action').value = "edit_exec";
	  ajustDimension('exec','edit_table',5,5);
	  Effect.Appear('exec');
	  document.getElementById('exec_data').innerHTML = '<img src=\'images/loading.gif\' />';
	  protoAjax("admin/reservation.php","POST","exec_data","edit_reservation","loading","okOrNo('hrefLocation(\"admin_reservation_edit_"+id+".html\")','',3000)");
  },
  
  view_reserved: function(){
	  document.getElementById('action').value = "view_reserved";
	  protoAjax("admin/reservation.php","POST","reserved_date","edit_reservation","loading","");
  },
  
  del_dates: function(){
	  document.getElementById('action_date').value = "del_dates";
	  ajustDimension('exec_date','right_col',5,5);
	  Effect.Appear('exec_date');
	  document.getElementById('exec_data_date').innerHTML = '<img src=\'images/loading.gif\' />';
	  protoAjax("admin/reservation.php","POST","exec_data_date","form_date","loading","");
  },
  
  
  del_dates_exec: function(){
	  document.getElementById('action_date').value = "del_dates_exec";
	  document.getElementById('exec_data_date').innerHTML = '<img src=\'images/loading.gif\' />';
	  protoAjax("admin/reservation.php","POST","exec_data_date","form_date","loading","okOrNo('Reservation.view_reserved()','',3000)");
  },
  
  add_date: function(id){
	   hrefLocation('admin_reservation_adddate_'+id+'.html');
  },
  
  add_date_exec: function(id){
	  document.getElementById('action').value = "add_date_exec";
	  lightAjax('admin/reservation.php','','POST','add_date',false,'okOrNo("hrefLocation(\'admin_reservation_edit_'+id+'.html\')","",3000)');
  },
  
  changeStatut: function(reservation,id){
	  Lightview.show({href: 'admin/reservation.php',rel: 'ajax',title: '',
	  options: {autosize: true,topclose: true,ajax: {method: 'POST',
		  parameters: 'action=change_statut&id='+id+'&reservation='+reservation,
		  onComplete: function (){}
		}
	  }
	});
  },
  
   changeStatut_exec: function(reservation,id){
	   document.getElementById('action_statut').value = "change_statut_exec";
	   lightAjax('admin/reservation.php','','POST','statut_change',false,'okOrNo("hrefLocation(\'admin_reservation_calendrier.html\')","",3000)');
   },
   
    changeStatut_cancel: function(reservation,id){
		document.getElementById('statut_'+reservation).value = id;
		Lightview.hide();
   },
   
   del: function(){
	  	document.getElementById('action').value = 'del';
	    ajustDimension('exec','left_col',25,5);
	  	Effect.Appear('exec');
	  	document.getElementById('exec_data').innerHTML = '<img src=\'images/loading.gif\' />';
	  	protoAjax("admin/reservation.php","POST","exec_data","edit_reservation","loading","");
   },
   
   del_exec: function(){
	   document.getElementById('action').value = 'del_exec';
	   protoAjax("admin/reservation.php","POST","exec_data","edit_reservation","loading","okOrNo('hrefLocation(\'admin_reservation_calendrier.html\');','',3000)");
   },
   
   edit_formule: function(){
	   var textareas = document.getElementsByTagName('textarea');
		for(var i=0;i<textareas.length;i++)
			{
			if(textareas[i].className == "tiny")
				{
				var txt = ajaxSave(textareas[i].name);
				document.getElementById(textareas[i].name+'_input').value = txt;
				}
			}
	   lightAjax('admin/reservation.php','','POST','edit_formule',false,'okOrNo("hrefLocation(\'admin_reservation_formules.html\')","",3000)'); 
   },
   
   add_formule: function(){
	   var textareas = document.getElementsByTagName('textarea');
		for(var i=0;i<textareas.length;i++)
			{
			if(textareas[i].className == "tiny")
				{
				var txt = ajaxSave(textareas[i].name);
				document.getElementById(textareas[i].name+'_input').value = txt;
				}
			}
	   lightAjax('admin/reservation.php','','POST','add_formule',false,'okOrNo("hrefLocation(\'admin_reservation_formules.html\')","",3000)'); 
   },
   
   del_formule: function(id){
	   Lightview.show({href: 'admin/reservation.php',rel: 'ajax',title: '',
	  options: {autosize: true,topclose: true,ajax: {method: 'POST',
		  parameters: 'action=del_formule&id='+id,
		  onComplete: function (){
			  }
		}
	  }
	});
   },
   
   del_formule_exec: function(id){
	   Lightview.show({href: 'admin/reservation.php',rel: 'ajax',title: '',
	  options: {autosize: true,topclose: true,ajax: {method: 'POST',
		  parameters: 'action=del_formule_exec&id='+id,
		  onComplete: function (){
			  okOrNo('hrefLocation("admin_reservation_formules.html")','',3000)
			  }
		}
	  }
	});
   },
   
   add_statut: function(){
	   var variable = "Form.serialize('add_statut')" ;
	   Lightview.show({href: 'admin/reservation.php',rel: 'ajax',title: '',
	  options: {autosize: true,topclose: true,ajax: {method: 'POST',
		  parameters: eval(variable),
		  onComplete: function (){
			  okOrNo('hrefLocation("'+getValue("redir")+'")','',3000);
			  }
		}
	  }
	});
	},
   
   edit_statut: function(){
	   var variable = "Form.serialize('edit_statut')" ;
	   Lightview.show({href: 'admin/reservation.php',rel: 'ajax',title: '',
	  options: {autosize: true,topclose: true,ajax: {method: 'POST',
		  parameters: eval(variable),
		  onComplete: function (){
			  okOrNo('hrefLocation("'+getValue("redir")+'")','',3000);
			  }
		}
	  }
	});
	},
	
	del_statut: function(id){
		Lightview.show({href: 'admin/reservation.php',rel: 'ajax',title: '',
	  options: {autosize: true,topclose: true,ajax: {method: 'POST',
		  parameters: 'action=del_statut&id='+id,
		  onComplete: function (){
			  }
		}
	  }
	});
	},
	
	del_statut_exec: function(){
		 lightAjax('admin/reservation.php','','POST','del_statut',false,'okOrNo("hrefLocation(\'admin_reservation_statuts.html\')","",3000)');
	},
	
	add_dispo:function(){
		lightAjax('admin/reservation.php','','POST','add_dispo',false,'okOrNo("hrefLocation(\'admin_reservation_disponibilite.html\')","",3000)');
	},
	
	del_dispo:function(id){
		Lightview.show({href: 'admin/reservation.php',rel: 'ajax',title: '',
	  options: {autosize: true,topclose: true,ajax: {method: 'POST',
		  parameters: 'action=del_dispo&id='+id,
		  onComplete: function (){
			 			  }
		}
	  }
	});
	},
	
	del_dispo_exec:function(){
		lightAjax('admin/reservation.php','','POST','del_dispo',false,'okOrNo("hrefLocation(\'admin_reservation_disponibilite.html\')","",3000)');
	},
	
	edit_dispo:function(){
		lightAjax('admin/reservation.php','','POST','edit_dispo',false,'okOrNo("hrefLocation(\'admin_reservation_disponibilite.html\')","",3000)');
	}
};

var Reservation = new Reservation();

var Notification = Class.create();

Notification.prototype = {
  initialize: function() {
  },
  
  edit: function(){
	   var textareas = document.getElementsByTagName('textarea');
		for(var i=0;i<textareas.length;i++)
			{
			if(textareas[i].className == "tiny")
				{
				var txt = ajaxSave(textareas[i].name);
				document.getElementById(textareas[i].name+'_input').value = txt;
				}
			}
	  lightAjax('admin/notification.php','','POST','edit_notification',false,'okOrNo("history.back(-1)","",3000)');
  }
};

var Notification = new Notification();

function clientInfo(action,from){
	if(action == "autocomplete")
		{
		var link_name = (from == "firstname")? "name" : "firstname";
		var link_name = document.getElementById(link_name).value;
		new Ajax.Autocompleter(from, from+"_autocompleter", "admin/client.php",{indicator:'loading_'+from,
  minChars: 2,parameters: 'action='+action+'&link_name='+link_name, afterUpdateElement :setClientValues},{})
	
		}
}

function locationInfo(action,from){
	if(action == "autocomplete")
		{
		new Ajax.Autocompleter(from, from+"_autocompleter", "admin/localisation.php",{indicator:'loading_'+from,
  minChars: 2,parameters: 'action='+action, afterUpdateElement :setLocationValues},{})
		}
}

function setValue(element,attr) {
	var e = document.getElementById(element);
	if (e==null){return;}
	if (attr == -1) {e.value = '';} 
	else if (attr != null && attr != ' ' && attr != '') {e.value = attr;}
}

function setClientValues(text, li)
{
	if(document.getElementById('client_id'))
	setValue('client_id',li.getAttribute('client_id'));
	if(document.getElementById('society'))
	setValue('society',li.getAttribute('client_society'));
	if(document.getElementById('tva'))
	setValue('tva',li.getAttribute('client_tva'));
	if(li.getAttribute('client_tva') != "" || li.getAttribute('client_society') != "")
		{
		document.getElementById('society_enable').checked = true;
		if(document.getElementById('society_facturation'))
		displayChecked('society_enable','society_facturation');
		displayChecked('society_enable','society_info');
		}
	else{
		document.getElementById('society_enable').checked = false;
		if(document.getElementById('society_facturation'))
		displayChecked('society_enable','society_facturation');
		displayChecked('society_enable','society_info');
		document.getElementById('society').value = "";
		document.getElementById('tva').value = "";
		}
	if(document.getElementById('firstname'))
	setValue('firstname',li.getAttribute('client_firstname'));
	if(document.getElementById('name'))
	setValue('name',li.getAttribute('client_name'));
	if(document.getElementById('street'))
	setValue('street',li.getAttribute('client_street'));
	if(document.getElementById('localisation_id'))
	setValue('localisation_id',li.getAttribute('localisation_id'));
	if(document.getElementById('cp'))
	setValue('cp',li.getAttribute('client_cp'));
	if(document.getElementById('city'));
	setValue('city',li.getAttribute('client_city'));
	if(document.getElementById('country'));
	setValue('country',li.getAttribute('client_country'));
	if(document.getElementById('tel'))
	setValue('tel',li.getAttribute('client_tel'));
	if(document.getElementById('gsm'))
	setValue('gsm',li.getAttribute('client_gsm'));
	if(document.getElementById('email'))
	setValue('email',li.getAttribute('client_email'));
	if(document.getElementById('infos_client'))
	document.getElementById('infos_client').style.display = "block";
}

function setLocationValues(text, li)
{
	if(document.getElementById('localisation_id'))
	setValue('localisation_id',li.getAttribute('localisation_id'));
	if(document.getElementById('cp'))
	setValue('cp',li.getAttribute('localisation_cp'));
	if(document.getElementById('city'))
	setValue('city',li.getAttribute('localisation_city'));
	if(document.getElementById('country'))
	setValue('country',li.getAttribute('localisation_country'));
}

function changeLanguage(id,formu){
	document.getElementById('lang').value = id;
	document.getElementById(formu).submit();
}



