/**
* @version		$Id: rental.js 2009-10-22 Frederic $
* @package		Intersport
* @subpackage	Shop
*/


/**
* Traite le formulaire de location d'un magasin Intersport.
*
* @param HTMLForm form Formulaire à traiter
* @param String lang Langue en cours
* @access public
* @since 1.0
*/
function RentalForm(form, lang) {
	/**
	 * Formulaire à traiter.
	 *
	 * @var HTMLForm
	 * @access private
	 * @since 1.0
	 */
	this.form = form;
	
	/**
	 * Langue en cours.
	 *
	 * @var String
	 * @access private
	 * @since 1.0
	 */
	this.lang = lang;
	
	/**
	 * Magasin géré par le formulaire.
	 *
	 * @var IntersportShop
	 * @access private
	 * @since 1.0
	 */
	this.intersportShop = null;
	
	/**
	 * Date de début de location.
	 *
	 * @var String
	 * @access private
	 * @since 1.0
	 */
	this.start = '';
	
	/**
	 * Date de end de location.
	 *
	 * @var String
	 * @access private
	 * @since 1.0
	 */
	this.end = '';
	
	/**
	 * Durée de location.
	 *
	 * @var Integer
	 * @access private
	 * @since 1.0
	 */
	this.duration = 6;
	
	/**
	* Initialise la location avec le formulaire en cours.
	*
	* @access public
	* @since 1.0
	*/
	this.init = function () {
		/*this.start = this.form.dayStart.value + '/' + this.form.monthStart.value + '/' + this.form.yearStart.value;
		this.end   = this.form.dayEnd.value   + '/' + this.form.monthEnd.value + '/' + this.form.yearEnd.value;*/
		this.start = this.form.dateStart.value;
		this.end   = this.form.dateEnd.value;
		/*this.start = '30/04/2009';
		this.end   = '05/05/2009';*/
		this.setIntersportShop();
	}
	
	/**
	* Initialise la location avec le formulaire en cours.
	*
	* @access public
	* @since 1.0
	*/
	this.submit = function () {
		this.init(form);
		this.form.action = this.intersportShop.intersportRentLink.getShopURL();
		return this.form.submit();
	}
	
	/**
	* Modifie le formulaire à traiter.
	*
	* @param HTMLForm form Formulaire à traiter
	* @access public
	* @since 1.0
	*/
	this.setForm = function (form) {
		this.form = form;
	}
	
	/**
	* Modifie la langue en cours.
	*
	* @param String lang Langue en cours
	* @access public
	* @since 1.0
	*/
	this.setLang = function (lang) {
		this.lang = lang;
	}
	
	/**
	* Initialise le magasin géré par le formulaire.
	*
	* @return IntersportShop Magasin géré par le formulaire
	* @access public
	* @since 1.0
	*/
	this.setIntersportShop = function () {
		this.intersportShop = new IntersportShop(this.form.shop.value, this.lang);
		//this.setEndDate();
		this.intersportShop.intersportRentLink.setDate(this.formatDateForIntersportRent(this.start), this.formatDateForIntersportRent(this.end));
		
		return this.intersportShop;
	}
	
	/**
	* Formate une date saisie dans le formulaire magasin pour le formulaire Intersport Rent
	*
	* @param  String date Date au format du formulaire magasin
	* @return String Date au format Intersport Rent
	* @access public
	* @since 1.0
	*/
	this.formatDateForIntersportRent = function (date) {
		date2 = date.split('/');
		return date2[2] + date2[1] + date2[0];
	}
	
	/**
	* Initialise le magasin géré par le formulaire.
	*
	* @return IntersportShop Magasin géré par le formulaire
	* @access public
	* @since 1.0
	*/
	this.setEndDate = function () {
		this.intersportShop = new IntersportShop(this.form.value, this.lang);
		
		date = this.start.split('/');
		start = new Date(date[2], parseInt(date[1] < 10 ? date[1].replace(/0/, '') : date[1]) - 1, (date[0] < 10 ? date[0].replace(/0/, '') : date[0]), 0, 0, 0);
		
		end = new Date();
		end.setTime(Date.parse(start) + (this.duration - 1) * 24 * 3600 * 1000);
		
		this.end = (end.getDate() < 10 ? '0' : '') + end.getDate() + '/' + ((end.getMonth() + 1) < 10 ? '0' : '') + (end.getMonth() + 1) + '/' + end.getFullYear();
	}
	
	/**
	* Renvoit les informations sur l'objet.
	*
	* @return String Information sur l'objet
	* @access public
	* @since 1.0
	*/
	this.toString = function() {
		lines = new Array();
		
		lines.push(this.intersportShop);
		lines.push(this.start);
		lines.push(this.end);
		lines.push(this.duration);
		
		return lines.join('<br />');
	}
	
	//this.init();
}
