/**
* Gère l'affichage du formulaire de réservation.
*
* @param Rental rental Réservation en cours
* @param String	lang Code langue pour l'affichage des messages
* @param String	urlSite URL du site
* @access public
* @since 1.0
*/
function RentalForm(urlSite, lang, rental) {
	/**
	 * URL du site.
	 *
	 * @var String
	 * @access protected
	 * @since 1.0
	 */
	this.urlSite = urlSite;
	
	/**
	 * Code langue pour l'affichage des messages.
	 *
	 * @var String
	 * @access protected
	 * @since 1.0
	 */
	this.lang = lang;
	
	/**
	 * Réservation en cours.
	 *
	 * @var Rental
	 * @access private
	 * @since 1.0
	 */
	this.rental = rental;
	
	/**
	 * Liste des categories disponibles.
	 *
	 * @var Array
	 * @access private
	 * @since 1.0
	 */
	this.categories = new Array('adult', 'child', 'all-mountain', 'enduro', 'freeride', 'protection');
	
	/**
	 * Listes de prix.
	 *
	 * @var Array
	 * @access private
	 * @since 1.0
	 */
	this.prices = new Array();
	
	/**
	 * Mini-formulaires de réservation.
	 *
	 * @var Array
	 * @access private
	 * @since 1.0
	 */
	this.forms = new Array();
	
	/**
	* Affiche les tarifs de location du produit sélectionné.
	*
	* @access public
	* @since 1.0
	*/
	this.init = function() {
		for(var i = 0; i < this.categories.length; i++) {
			price = new DisplayProductPrice(products[i]);
			price.display(this.categories[i] + '-price');
			this.prices.push(price);
			
			form = new DisplayProductRental(products[i]);
			form.display(this.categories[i] + '-form', i);
			this.forms.push(form);
		}
		
	}
	
	/**
	* Affiche le formulaire de réservation.
	*
	* @access public
	* @since 1.0
	*/
	this.display = function() {
		var html = '';
		
		html += '<table>';
		html += '<tr>';
		html += '<th class="name">' + messages['t-product'][this.lang] + '</th>';
		html += '<th class="quantity">' + messages['t-size'][this.lang] + '</th>';
		html += '<th class="selection">' + messages['t-quantity'][this.lang] + '</th>';
		html += '<th class="price">' + messages['t-price'][this.lang] + '</th>';
		html += '<th class="delete">' + messages['t-delete'][this.lang] + '</th>';
		html += '</tr>';
		for(var i = 0; i < this.rental.products.length; i++) {
			html += '<tr>';
			html += '<td class="name"><input type="hidden" name="product[' + i + '][name]" value="' + this.rental.products[i].product.name + '" />' + this.rental.products[i].product.name + '</td>';
			html += '<td class="selection"><input type="hidden" name="product[' + i + '][selection]" value="' + this.rental.products[i].selection + '" />' + this.rental.products[i].selection + '</td>';
			html += '<td class="quantity"><input type="hidden" name="product[' + i + '][quantity]" value="' + this.rental.products[i].quantity + '" />' + this.rental.products[i].quantity + '</td>';
			html += '<td class="price"><input type="hidden" name="product[' + i + '][price]" value="' + this.rental.products[i].getTotal() + '" />' + formatPrice(this.rental.products[i].getTotal()) + ' &euro;</td>';
			html += '<td class="delete"><a href="#" title="' + messages['t-delete'][this.lang] + '" onclick="rentalForm.clickDelete(' + i + ');" /></td>';
			html += '</tr>';
		}
		html += '<tr>';
		html += '<td class="total-label" colspan="3">' + messages['t-total'][this.lang] + '</td>';
		html += '<td class="total-value"><input type="hidden" name="total" value="' + this.rental.getTotal() + '" />' + formatPrice(this.rental.getTotal()) + ' &euro;</td>';
		html += '<td class="delete">&nbsp;</td>';
		html += '</tr>';
		html += '</table>';
		
		document.getElementById('rental-form-cart').innerHTML = html;
	}
	
	/**
	* Traite le clic sur un bouton de suppression.
	*
	* @param Integer i Identifiant du produit à supprimer
	* @return Boolean Réussite (true) ou échec (false) de l'opération
	* @access public
	* @since 1.0
	*/
	this.clickDelete = function(i) {
		this.rental.deleteProduct(i);
		this.display();
	}
	
	/**
	* Traite la séleciton d'une durée.
	*
	* @param Integer i Identifiant du produit à supprimer
	* @access public
	* @since 1.0
	*/
	this.changeDuration = function(duration) {
		for(var i = 0; i < this.rental.products.length; i++) {
			this.rental.products[i].setPrice(duration);
		}
		this.display();
	}
	
	/**
	* Vérifie la validité du formulaire d'envoi.
	*
	* @param HTMLFormElement form Formulaire à tester
	* @param Boolean antiSpamTest Vérifie (true) ou pas (false) le code anti-spam saisi
	* @return Boolean Le formulaire est valide (true) ou pas (false)
	* @access public
	* @since 1.0
	*/
	this.validate = function(form, antiSpamTest) {
		if(antiSpamTest == undefined) {
			antiSpamTest = true;
		}
		
		//if(form.product == undefined) {
		if(this.rental.products.length == 0) {
			alert(messages['e-product'][this.lang]);
			return false;
		}
		
		/*if(form.product.length == 0) {
			alert('Veuillez choisir un produit au moins.');
			return false;
		}*/
		
		if(form.arrival_date.value == '') {
			alert(messages['e-start'][this.lang]);
			return false;
		}
		
		if(form.duration.value == '') {
			alert(messages['e-duration'][this.lang]);
			form.duration.focus();
			return false;
		}
		
		if(form.name.value == '') {
			alert(messages['e-name'][this.lang]);
			form.name.focus();
			return false;
		}
		
		if(form.firstname.value == '') {
			alert(messages['e-firstname'][this.lang]);
			form.firstname.focus();
			return false;
		}
		
		if(form.phone.value == '') {
			alert(messages['e-phone'][this.lang]);
			form.phone.focus();
			return false;
		}
		
		if(!this.verifEmail(form.email.value)) {
			//alert(messages['e-email'][this.lang]);
			form.email.focus();
			return false;
		}
		
		if(form.email2.value != form.email.value) {
			alert(messages['e-email2'][this.lang]);
			form.email2.focus();
			return false;
		}
		
		if(antiSpamTest) {
			jQuery.ajax({
				type:    'POST',
				url:     this.urlSite + 'voucher/antispam.php?antispam=' + form.antispam.value,
				async:   false,
				success: function(data) {
					if(data == '0') {
						alert(messages['e-antispam'][rentalForm.lang]);
						form.antispam.focus();
						return false;
					}
					
					return rentalForm.validate(form, false);
				}
			});
		}
		else {
			form.submit();
			return true;
		}
		
		return false;
	}
	
	/**
	* Affiche la date de départ en fonction de la date d'arrivée et le nombre de nuit.
	*
	* @param String dateValue Valeur da la date sélectionnée
	* @access public
	* @since 1.0
	*/
	this.displayDepartureDate = function (dateValue) {
		if(document.getElementById('arrival_date').value != '') {
			if(dateValue == undefined) {
				dateValue = document.getElementById('arrival_date').value;
			}
			
			expression = /([0-9]{2})-([0-9]{2})-([0-9]{4})/;
			expression.exec(dateValue);
			//expression.exec(document.getElementById('arrival_date').value);
			
			dateD = parseInt(this.formatInfoDate(RegExp.$1));
			dateM = parseInt(this.formatInfoDate(RegExp.$2));
			dateY = parseInt(RegExp.$3);
			dateValue = (dateD <= 9 ? '0' + dateD : dateD) + '-' + (dateM <= 9 ? '0' + dateM : dateM) + '-' + dateY;
			departureDate = new Date(dateY, dateM - 1, dateD);
			
			if(document.getElementById('duration').value == '') {
				duration = 1;
			}
			else {
				duration = parseInt(document.getElementById('duration').value);
			}
			
			// Le javascript ne permettant pas d'initialiser une date à partir d'un temps UNIX, passer par PHP
			jQuery.ajax({
				type:    'GET',
				//url:     this.urlSite + 'index.php?page=form&tache=get-time&art=2&unixtimestamp=' + (Date.parse(departureDate) / 1000 + duration * 24 * 3600) + '&format=d-m-Y',
				url:     this.urlSite + 'get-time.php?unixtimestamp=' + (Date.parse(departureDate) / 1000 + duration * 24 * 3600) + '&format=d-m-Y',
				async:   false,
				success: function(data) {
					document.getElementById('arrival_date').value = dateValue;
					document.getElementById('arrival_date_span').innerHTML = dateValue;
					//document.getElementById('departure_date').value = data;
					//document.getElementById('departure_date_span').innerHTML = data;
				}
			});
		}
	}
	
	/**
	* Affiche la date sélectionnée.
	*
	* @param String dateValue Valeur da la date sélectionnée
	* @access public
	* @since 1.0
	*/
	this.displayDate = function (dateEntry, dateValue) {
		if(dateEntry.value != '') {
			if(dateValue == undefined) {
				dateValue = dateEntry.value;
			}
			
			document.getElementById(dateEntry.name).value = dateValue;
			document.getElementById(dateEntry.name + '_span').innerHTML = dateValue;
		}
	}
	
	/* Supprimer le 0 initial du mois ou du jour d'une date.
	*
	* @param String info_date Jour ou mois numérique (chaîne de caractères)
	* @return Jour / mois modifié
	* @access public
	* @since 1.0
	*/
	this.formatInfoDate = function(info_date) {
		if(info_date.length == 2 && info_date.charAt(0) == '0') {
			info_date = info_date.charAt(1);
		}
		
		return info_date;
	}
	
	/**
	* Vérifie la validité d'une adresse e-mail.
	*
	* @param String email Adresse e-mail à vérifier
	* @return Jour / mois modifié
	* @access public
	* @since 1.0
	*/
	this.verifEmail = function(email) {
		if(email.length < 6) {
			alert(email + ' : ' + messages['e-email-too-short'][this.lang]);
			return false;
		}
		if(email.length > 80) {
			alert(email + ' : ' + messages['e-email-too-long'][this.lang]);
			return false;
		}
		if(!email.match(/@/)) {
			alert(email + ' : ' + messages['e-email-no-arobase'][this.lang]);
			return false;
		}
		var caractere = '';
		if(caractere = email.match(/([^a-zA-Z0-9_\@\.\-])/i)) {
			alert(email + ' : ' + messages['e-email-forbidden-caracter'][this.lang] + ' (' + caractere[0] + ').');
			return false;
		}
		if(!email.match(/^([a-z0-9_\-\.])+@(([a-z0-9_\-])+\.)+[a-z]{2,4}$/i)) {
			alert(email + '  ' + messages['e-email-format'][this.lang]);
			return false;
		}
		
		return true;
	}
	
	/**
	* Renvoit les informations sur l'objet.
	*
	* @return String Information sur l'objet
	* @access public
	* @since 1.0
	*/
	this.toString = function() {
		return 'categories = ' + this.categories;
	}
}
