
order = {
	
	/* Main function initiation
	--------------------------------------------------------------*/
	init: function(){
		if(this._INIT == null){
			this._INIT = true;
        
			this.ajaxLdr = $$('.ajaxLoader')[0];
			this.txMsg   = $$('.form_submit h6')[0];
		
			this.pNorm  = 3;
			this.pMini  = 1.40;
			
			this.qNorm    = 0;
			this.qMini    = 0;
			this.qNormFld = $$('.quant_st')[0];
			this.qMiniFld = $$('.quant_sm')[0];
			
			this.total  = 0.00;
			this.ttlFld = $$('.total')[0];
		
			this.arNorm = $$('.quFld .fld_blk .fld_st');
			this.arMini = $$('.quFld .fld_blk .fld_sm');
			
			this.qAr = new Array();
			
			for (i=0; i<=17; i++) {
				this.qAr.push([0,0]);
			};
		};
		
		this.startup();
	},
	
	
	/* Startup function
	--------------------------------------------------------------*/
	startup: function(){
		this.initFields();
		this.addEvents();
	},
	
	
	/* Initialize field contents
	--------------------------------------------------------------*/
	initFields: function(){
		this.ttlFld.innerHTML = this.total + '.00$';
		this.qNormFld.innerHTML = this.qNorm;
		this.qMiniFld.innerHTML = this.qMini;
		
		this.arNorm.each(function(e){
			e.value = '';
		});
		
		this.arMini.each(function(e){
			e.value = '';
		});
	},
	
	
	/* Calculate new quantity for each field change of value
	--------------------------------------------------------------*/
	calculateQuantity: function(type){
		var self = this;
		
		if (type == 'st') var tyID = 0
		else var tyID = 1;
		
		var qt   = 0;
		var arLt = this.qAr.length;
		
		for (i=0; i<arLt; i++) {
			qt = qt + self.qAr[i][tyID];
		}
		
		if (type == 'st') this.qNorm = qt
		else this.qMini = qt;
		
		return qt.toInt();
	},
	
	
	/* Calculate new total
	--------------------------------------------------------------*/
	calculateTotal: function(){
		var self = this;
		
		// Calculate standard & mini cupcake quantities
		var tt = 0;
		var ttSt = this.qNorm * this.pNorm;
		var ttSm = this.qMini * this.pMini;
		
		// Add both quantities for the main total
		tt = ttSt + ttSm;
		
		// Send total
		this.total = tt.toFloat().toFixed(2);
		return tt.toFloat().toFixed(2);
	},
	
	
	/* Add events on fields
	--------------------------------------------------------------*/
	addEvents: function(){
		var self = this;
		
		// Loop standard cake fields
		this.arNorm.each(function(e){
			var prt = e.getParent('.quFld');
			var id = prt.id.substring(4);
			
			e.addEvent('blur', function(){
				var el = e.value;
				
				if (el == '' || el <= 0 || parseInt(el) != el - 0) {
					
					self.qAr[id][0] = 0;
					e.value = '';
					
				} else {
				
					self.qAr[id][0] = e.value.toInt();
				
				}
				
				self.qNormFld.innerHTML = self.calculateQuantity('st');
				self.ttlFld.innerHTML = self.calculateTotal() + '$';
			});
		});
		
		// Loop min-cake fields
		this.arMini.each(function(e){
			var prt = e.getParent('.quFld');
			var id = prt.id.substring(4);
			
			e.addEvent('blur', function(){
				var el = e.value;
				
				if (el == '' || el <= 0 || parseInt(el) != el - 0) {
					
					self.qAr[id][1] = 0;
					e.value = '';
					
				} else {
				
					self.qAr[id][1] = e.value.toInt();
				
				}
				
				self.qMiniFld.innerHTML = self.calculateQuantity('sm');
				self.ttlFld.innerHTML = self.calculateTotal() + '$';
			});
		});
	},
	
	
	/* Send form to ajax
	--------------------------------------------------------------*/
	sendFormToAjax: function(){
		var self = this;
		
        //Vanille
		var van_st  = $$('.fld_st')[0].value;
        var van_sm  = $$('.fld_sm')[0].value;
        
        // Poire vanille
        var pvan_st = $$('.fld_st')[1].value;
        
        // Classique choco
        var cch_st = $$('.fld_st')[2].value;
        var cch_sm = $$('.fld_sm')[1].value;
        
        // Whippet
        var whi_st = $$('.fld_st')[3].value;
        
        //Truffes
        var tru_st = $$('.fld_st')[4].value;
        
        //Poire choco
        var pch_st = $$('.fld_st')[5].value;
        
        // Ban.sucre creme
        var bsc_st = $$('.fld_st')[6].value;
        var bsc_sm = $$('.fld_sm')[2].value;
		
        // Ban.sucre choco
        var bsc2_st = $$('.fld_st')[7].value;
        var bsc2_sm = $$('.fld_sm')[3].value;
        
        // CS Framboise
        var csf_st = $$('.fld_st')[8].value;
        var csf_sm = $$('.fld_sm')[4].value;
        
        // Carottes
        var car_st = $$('.fld_st')[9].value;
        var car_sm = $$('.fld_sm')[5].value;
        
        // Coconut bleu
        var coc_st = $$('.fld_st')[10].value;
        var coc_sm = $$('.fld_sm')[6].value;
        
        // Cafe
        var caf_st = $$('.fld_st')[11].value;
        var caf_sm = $$('.fld_sm')[7].value;
        
        // Tarte citron
        var cit_st = $$('.fld_st')[12].value;
        var cit_sm = $$('.fld_sm')[8].value;
        
        // Agrumes
        var agr_st = $$('.fld_st')[13].value;
        var agr_sm = $$('.fld_sm')[9].value;
        
        // The, canneberge violette
        var can_st = $$('.fld_st')[14].value;
        var can_sm = $$('.fld_sm')[10].value;
        
        // Ganache
        var gan_st = $$('.fld_st')[15].value;
        
        // choco coco passion
        var chc_st = $$('.fld_st')[16].value;
        var chc_sm = $$('.fld_sm')[11].value;
        
        // Chevre, bleuets
        var chb_st = $$('.fld_st')[17].value;
        
		// Customer information
        var day = $$('.day')[0].value;
        var mth = $$('.month')[0].value;
        var yr  = $$('.year')[0].value;
        var tim = $$('.time')[0].value;
        var lname = $$('.lastname')[0].value;
        var fname = $$('.firstname')[0].value;
        var cie = $$('.cie')[0].value;
		var mail = $$('.email')[0].value;
        var adr = $$('.adr')[0].value;
        var tel = $$('.phone')[0].value;
        var cel = $$('.cel')[0].value;
        var msg = $$('.msg')[0].value;
		var tot = this.total;
        
		// Prepare the request
		var request = new Request({
			method: 'post', 
			url: '../ajax/sendForm.php',
			
			onRequest: function(){
				self.txMsg.setStyle('display','none');
				self.ajaxLdr.setStyle('display','block');
		    },
			
			onSuccess: function(response){
				self.ajaxLdr.setStyle('display','none');
				self.txMsg.setStyle('display','block');
				window.location.href = 'commandes_confirmation.php';
		    }
		});
		
		// Send the request
		request.send('van_st=' +van_st+ '&van_sm=' +van_sm+ '&pvan_st=' +pvan_st+ '&cch_st=' +cch_st+ '&cch_sm=' +cch_sm+ '&whi_st=' +whi_st+ '&tru_st=' +tru_st+ '&pch_st=' +pch_st+ '&bsc_st=' +bsc_st+ '&bsc_sm=' +bsc_sm+ '&bsc2_st=' +bsc2_st+ '&bsc2_sm=' +bsc2_sm+ '&csf_st=' +csf_st+ '&csf_sm=' +csf_sm+ '&car_st=' +car_st+ '&car_sm=' +car_sm+ '&coc_st=' +coc_st+ '&coc_sm=' +coc_sm+ '&caf_st=' +caf_st+ '&caf_sm=' +caf_sm+ '&cit_st=' +cit_st+ '&cit_sm=' +cit_sm+ '&agr_st=' +agr_st+ '&agr_sm=' +agr_sm+ '&can_st=' +can_st+ '&can_sm=' +can_sm+ '&gan_st=' +gan_st+ '&chc_st=' +chc_st+ '&chc_sm=' +chc_sm+ '&chb_st=' +chb_st+ '&day=' +day+ '&month=' +mth+ '&year=' +yr+ '&hour=' +tim+ '&lastname=' +lname+ '&firstname=' +fname+ '&company=' +cie+ '&email=' +mail+ '&adress=' +adr+ '&tel=' +tel+ '&cel=' +cel+ '&message=' +msg+ '&total=' +tot);
	},
	
	
	/* Use may vary
	--------------------------------------------------------------*/
	empty: function(){
	},

	
	/* Handles window events
	--------------------------------------------------------------*/
	onWindowDomReady: function(){
        this.init();
	},
	
	onWindowLoad: function(){
	}
};


window.addEvent('domready', function(){
	order.onWindowDomReady();
});

window.addEvent('load', function(){
	order.onWindowLoad();
});




