/**
* @version		$Id: quotation.js 2009-01-20 Frederic $
* @package		CMS
* @subpackage	Form
*/

/**
* Gère l'interface du formulaire.
*
* @param String url URL du script index.php
*
* @package		CMS
* @subpackage	Form
* @since		1.0
*/
function FormInterface(url) {
	/**
	* URL du script index.php.
	*
	* @var string
	*/
	this.url = url;
	
	/**
	* Traite la sélection d'une catégorie.
	*
	* @access public
	* @since 1.0
	*/
	this.changeCategory = function(entry) {
		if(document.getElementById('quotation').category.selectedIndex != 0) {
			$.ajax({
				type:    'GET',
				url:     this.url + 'index.php?page=form&art=quotation&tache=load-product&category=' + document.getElementById('quotation').category.value,
				async:   false,
				success: function(data) {
					document.getElementById('product').innerHTML = '';
					$('#product').append(data);
				}
			});
		}
	}
	
	/**
	* Traite la sélection d'un produit.
	*
	* @access public
	* @since 1.0
	*/
	this.changeProduct = function(entry) {
		if(document.getElementById('quotation').product.selectedIndex != 0) {
			document.location.href = this.url + 'index.php?page=form&art=quotation&product=' + document.getElementById('quotation').product.value + '&date=' + document.getElementById('quotation').date.value;
			//document.getElementById('quotation').action = this.url + 'index.php?page=form&art=quotation';
			//this.displayAllAttributeBlocks();
			//this.clearAttributeSelect();
		}
	}
	
	/**
	* Traite la sélection d'une période.
	*
	* @access public
	* @since 1.0
	*/
	this.changeDate = function() {
		if(document.getElementById('quotation').date.selectedIndex != 0) {
			document.location.href = this.url + 'index.php?page=form&art=quotation&product=' + document.getElementById('quotation').product.value + '&date=' + document.getElementById('quotation').date.value;
			//document.getElementById('quotation').action = this.url + 'index.php?page=form&art=quotation';
			//this.displayAllAttributeBlocks();
			//this.clearAttributeSelect();
		}
	}
	
	/**
	* Traite la sélection d'une caractéristique.
	*
	* @param HTMLFormEntry entry Entrée du formulaire sélectionnée
	* @access public
	* @since 1.0
	*/
	this.changeAttribute = function(entry) {
		$.ajax({
			type:    'GET',
			url:     this.url + 'index.php?page=form&art=quotation&tache=load-attribute-price&product=' + document.getElementById('quotation').product.value + '&date=' + document.getElementById('quotation').date.value + '&attribute=' + entry.name,
			async:   false,
			success: function(data) {
				document.getElementById(entry.name + '-price').innerHTML = formatNumber(parseFloat(entry.value) * parseFloat(data));
			}
		});
		this.changeTotal();
	}
	
	/**
	* Traite la sélection de l'assurance.
	*
	* @param HTMLFormEntry entry Entrée du formulaire sélectionnée
	* @access public
	* @since 1.0
	*/
	this.changeInsurance = function(entry) {
		this.changeTotal();
	}
	
	/**
	* Modifie l'affichage du total.
	*
	* @access public
	* @since 1.0
	*/
	this.changeTotal = function() {
		var personNumber = 0.00;
		var total = 0.00;
		$('fieldset span').each(function(itemIndex, itemObject) {
			if(itemObject.id != 'total-price' && itemObject.id != 'total-file' && itemObject.id != 'total-insurance' /*&& itemObject.id != 'total-insurance2'*/ && itemObject.id != 'total-price2') {
				if(itemObject.id == 'pc2cf0-price' || itemObject.id == 'pc2cf2-price' || itemObject.id == 'pc2cf4-price' || itemObject.id == 'pc1cf0-price' || itemObject.id == 'pc1cf1-price' || itemObject.id == 'pc1cf2-price' || itemObject.id == 'pc1cf4-price') {
					personNumber += parseFloat(document.getElementById(itemObject.id.replace(/\-price/, '-select')).value);
				}
				
				total += parseFloat(itemObject.innerHTML);
			}
		});
		
		insurance = document.getElementById('insurance').checked ? this.calculateInsurance(total, parseFloat(document.getElementById('insurance').value)) : 0.00;
		//insurance2 = document.getElementById('insurance').checked ? parseFloat(document.getElementById('insurance2').value) : 0.00;
		//document.getElementById('insurance2').checked = document.getElementById('insurance').checked;
		fileCharge = 30.00 * personNumber;
		total2 = total + fileCharge + insurance /*+ insurance2 * personNumber*/;
		
		document.getElementById('total-price').innerHTML = formatNumber(total);
		document.getElementById('total-file').innerHTML = formatNumber(fileCharge);
		document.getElementById('total-insurance').innerHTML = formatNumber(insurance);
		//document.getElementById('total-insurance2').innerHTML = formatNumber(insurance2 * personNumber);
		document.getElementById('total-price2').innerHTML = formatNumber(total2);
	}
	
	/**
	* Calcule le montant de l'assurance.
	*
	* @param Float total Total pour lequel on veut connaître le montant de l'assurance
	* @param Float insurance Pourcentage de l'assuranceassurance
	* @return Float Montant de l'assurance calculé sur le total
	* @access public
	* @since 1.0
	*/
	this.calculateInsurance = function(total, insurance) {
		return Math.round(total * insurance / 100.00, 2);
	}
	
	/**
	* Affiche tous les blocs de sélection de caractéristiques.
	*
	* @param String display Mode d'affichage 'display' ou 'bloc'
	* @access public
	* @since 1.0
	*/
	this.displayAllAttributeBlocks = function(display) {
		list = new Array('person', 'bicyle', 'car');
		
		for(i = 0; i < list.length; i++) {
			this.displayAttributeBlock(list[i]);
		}
	}
	
	/**
	* Affiche le bloc de sélection de caractéristiques.
	*
	* @param String blockName Nom du block à afficher
	* @param String display Mode d'affichage 'display' ou 'bloc'
	* @access public
	* @since 1.0
	*/
	this.displayAttributeBlock = function(blockName, display) {
		if(display == undefined) {
			display = 'block';
		}
		
		$(blockName).css('display', display);
	}
	
	/**
	* Affiche le bloc de sélection de caractéristiques.
	*
	* @access public
	* @since 1.0
	*/
	this.clearAttributeSelect = function() {
		productSelectedIndex = document.getElementById('quotation').product.selectedIndex;
		dateSelectedIndex = document.getElementById('quotation').date.selectedIndex;
		document.getElementById('quotation').reset();
		document.getElementById('quotation').product.selectedIndex = productSelectedIndex;
		document.getElementById('quotation').date.selectedIndex = dateSelectedIndex;
		$('fieldset span').each(function(itemIndex, itemObject) {
			itemObject.innerHTML = '0.00';
		});
		this.changeTotal();
	}
}

