/**
 * Shop form class
 *
 * Class of shop form, for all calculations and displaying the correct form values
 * @author BigBase - D. Tiems (dennis@bigbase.nl)
 * @version 1.0
 * @package OdesiShop
 * @copyright Copyright 2009 Odesi
 */

shopForm = function ( c ) {

	// Set default vars
	var main = this;
	var undefined;
	var config = c;

	// Set configuration default values
	var defaultConfig = ({
		id : 'form',
		price : 0,
		transportprice : 0,
		downpayment : 0,
		tax : 0
	});

	// Check and correct complete configuration
	if ( config['id'] !== undefined )             defaultConfig['id'] = config['id'];
	if ( config['price'] !== undefined )          defaultConfig['price'] = config['price'];
	if ( config['transportprice'] !== undefined ) defaultConfig['transportprice'] = config['transportprice'];
	if ( config['downpayment'] !== undefined )    defaultConfig['downpayment'] = config['downpayment'];
	if ( config['tax'] !== undefined )            defaultConfig['tax'] = config['tax'];

	// Set correct values to main config
	config = defaultConfig;
		
	// Set calculated values
	this.calculated_price_single   = 0;
	this.calculated_price          = 0;
	this.calculated_transportprice = 0;
	this.calculated_transportprice_single = 0;
	this.calculated_downpayment    = 0;
	this.calculated_description    = 0;
	this.calculated_downpayment_price = 0;

	// Start the calculation
	this.refill = function () {
		calculate();
		updateForm();
	}

	// Calculate and set the prices
	function calculate () {

		// Get count
		count = 1;
		if ( document.getElementById('product_count') ) {
			count = document.getElementById('product_count').options[document.getElementById('product_count').selectedIndex].value;
		}

		// Get price
		addPrice = 0;
		if ( document.getElementById('product_dimension') ) addPrice = addPrice + Math.floor ( parseInt ( document.getElementById('product_dimension').options[document.getElementById('product_dimension').selectedIndex].value ) );
		if ( document.getElementById('product_material') )  addPrice = addPrice + Math.floor ( parseInt ( document.getElementById('product_material').options[document.getElementById('product_material').selectedIndex].value ) );

		// Get description
		newDescription = document.getElementById('product_name').value;
		if ( document.getElementById('product_dimension') ) newDescription = newDescription + '; ' + document.getElementById('product_dimension').options[document.getElementById('product_dimension').selectedIndex].text;
		if ( document.getElementById('product_material') )  newDescription = newDescription + '; ' + document.getElementById('product_material').options[document.getElementById('product_material').selectedIndex].text;
		if ( document.getElementById('product_showroom') ) {
			if ( document.getElementById('product_showroom').checked ) {
				newDescription = newDescription + '; ' + ' showroom thuis service';
			}
		}

		// Calculate new prices
		main.calculated_tax_multiplier = ( 100 + config['tax'] ) / 100;
		main.calculated_price_single   = Math.floor ( config['price'] * main.calculated_tax_multiplier ) + Math.floor ( addPrice * main.calculated_tax_multiplier );
		main.calculated_price          = main.calculated_price_single * count;
		main.calculated_transportprice_single = Math.floor ( config['transportprice'] * main.calculated_tax_multiplier );
		main.calculated_transportprice = main.calculated_transportprice_single * count;
		if ( config['downpayment'] == 0 ) config['downpayment'] = 100;
		main.calculated_downpayment    = config['downpayment'] / 100;
		if ( document.getElementById('product_showroom') ) {
			if ( document.getElementById('product_showroom').checked ) {
				main.calculated_downpayment = 0;
			}
		}
		main.calculated_downpayment_price = Math.floor ( main.calculated_downpayment * main.calculated_price ) + main.calculated_transportprice;
		main.calculated_description    = newDescription;
	}

	function updateForm () {

		// Update hidden elements
		if ( document.getElementById('product_price') ) document.getElementById('product_price').value = main.calculated_price_single;
		if ( document.getElementById('product_description') ) document.getElementById('product_description').value = main.calculated_description;
		if ( document.getElementById('product_downpayment') ) document.getElementById('product_downpayment').value = 100 * main.calculated_downpayment;
		if ( document.getElementById('product_transportprice') ) document.getElementById('product_transportprice').value = main.calculated_transportprice_single;

		// Update visible elements
		if ( document.getElementById('calculated_price_no_transport') ) document.getElementById('calculated_price_no_transport').innerHTML = '\u20ac ' + main.calculated_price + ',-';
		if ( document.getElementById('calculated_price') ) document.getElementById('calculated_price').innerHTML = '\u20ac ' + ( parseInt(main.calculated_price) + parseInt(main.calculated_transportprice) ) + ',-';
		if ( document.getElementById('calculated_transportprice') ) {
			if ( main.calculated_transportprice != 0 ) {
				document.getElementById('calculated_transportprice').innerHTML = '\u20ac ' + main.calculated_transportprice + ',-';
			}
		}
		if ( document.getElementById('calculated_downpayment') ) document.getElementById('calculated_downpayment').innerHTML = '\u20ac ' + parseInt(main.calculated_downpayment_price) + ',-';
		if ( document.getElementById('calculated_delivery_payment') ) document.getElementById('calculated_delivery_payment').innerHTML = '\u20ac ' + ( parseInt(main.calculated_price) + parseInt(main.calculated_transportprice) - parseInt(main.calculated_downpayment_price) ) + ',-';
		
	}
	
}
