if( typeof( insertPanier ) != 'undefined' ){
	var extended = insertPanier;
}else
	var extended = _AjaxElements;

var gestionCoupons = Class.create( extended , {
	
	initialize:function($super){
		$super();
		this.tab = new Array();
		this.getTab();
	},

	getTab:function(){
		 var jsonElements = { 
					'url':'/AffichageAjax~getCoupon',
					'myClass':this,
					'myMethod':'createTab()'
				};

		 ajaxReq = new _AjaxElements( jsonElements );
		 ajaxReq.getAjaxInformations();
	},
	
	createClicks:function(){		
		var coupons = document.getElementsByClassName( 'inputCoupon' );
		var chekCado = document.getElementsByClassName( 'inputChekCado' );
		for( var i = 0 ; i < coupons.length ; i++ ){
			coupons[i].observe( 'click' , this.sumDiscountCode.bindAsEventListener( this , 'coupons' ) );
		}
		for( var j = 0 ; j < chekCado.length ; j++ ){
			chekCado[j].observe( 'click' , this.TotalMoinsChequeCado.bindAsEventListener( this ) );
		}
		
		$('validCoupon').observe( 'submit' , this.submitCoupon.bind(this) );
	},
	
	createTab:function(){
		if( isSet( this.json ) ){
			for (var i=0; i< this.json.codeCoupon.length; i++) {
				this.tab.push( this.json.codeCoupon[i]+'='+this.json.value[i] );
			}
		}
		this.createClicks();
	},
	
	sumDiscountCode:function( e , idP ) {
		
		var cbCoupon = e.element();
		var m = $('sousTot').firstChild.data;
		var regm = new RegExp("(\\d+\\.?\\d*)");
		var sumOutCoupon = ( $('sumOutCoupon').firstChild.data );
		var numStr = ( regm.exec(m)[0] - sumOutCoupon );
	  
		var getDiv = idP;
	  
		if( isSet( $('cc') ) )
			var getDiv = 'canChecked';

		var divPrincipale = $( getDiv );

		var checkcheck = divPrincipale.getElementsByTagName('input');
	  
		if ( !isSet( $("cumulage") ) ) {
			var cum = 0;
		}else {
			var cum = $('cumulage').innerHTML;
			cum = regm.exec(cum)[0];
		}

		if( cbCoupon.checked ){
			montant = ( parseFloat( cum ) + parseFloat( cbCoupon.value ) );
			this.tab.push( cbCoupon.name+'='+cbCoupon.value );
		}else {
			montant = ( parseFloat(cum) - parseFloat(cbCoupon.value) );
			this.tab.splice( this.tab.indexOf(cbCoupon.name+'='+cbCoupon.value),1 );
		}
		var montantCoup = montant;
	  
		if( isSet( $('chekKados') ) ){
			var cheks = $('chekKados').getElementsByTagName('input');
			for( var i = 0 ; i < cheks.length ; i++ ){
				if( cheks[i].checked == true ){
					montantCoup = ( ( parseFloat(montantCoup) * 100 ) - ( parseFloat(cheks[i].value) * 100 ) ) / 100;
				}
			}
		}
		
		if( ( parseFloat( cbCoupon.value ) == parseFloat( numStr ) || montantCoup == parseFloat( numStr  ) ) && cbCoupon.checked == true ) {
			for( i = 0;i<checkcheck.length;i++ ){
				if(checkcheck[i].type == 'checkbox' && checkcheck[i].checked == false){
					checkcheck[i].disabled = 'disabled';
				}
			}
		}else if( ( parseFloat( cbCoupon.value ) > parseFloat( numStr ) || montantCoup > parseFloat( numStr  ) ) && cbCoupon.checked == true ) {
			$('depassement').style.display = 'block';
			for( i = 0;i<checkcheck.length;i++ ){
				if(checkcheck[i].type == 'checkbox' && checkcheck[i].checked == false){
					checkcheck[i].disabled = 'disabled';
				}
			}
		}else if( ( montantCoup > parseFloat( numStr  ) ) && cbCoupon.checked == false  ) {
			$('depassement').style.display = 'block';
			for( i = 0;i<checkcheck.length;i++ ){
				if(checkcheck[i].type == 'checkbox' && checkcheck[i].checked == false){
					checkcheck[i].disabled = 'disabled';
				}
			}
		}else{
			$('depassement').hide();
			for( i = 0;i<checkcheck.length;i++ ){
				if(checkcheck[i].type == 'checkbox' && checkcheck[i].disabled == true){
					checkcheck[i].disabled = '';
				}
			}
		}

		if( this.shopid == 43 ) {
			var contentTot = 'Total &pound;';
			var parseFl = parseFloat(montant);
		}else {
			var contentTot = 'Total ';
			var parseFl = montant+'&euro;';
		}
		$('couponTotal').update( contentTot );
		$('couponTotal').insert( '<span id="cumulage">'+parseFl+'</span>' );
	  
		if( isSet( $('chekKados') ) )
			this.TotalMoinsChequeCado();
	},
	
	TotalMoinsChequeCado:function( e ){
		
		var cbCheck = isSet( e ) ? e.element() : null;
		var totCoup = 0;
		var trueTot = $('trueTotal').firstChild.data;
		var sousto = $('sousTot').firstChild.data;
		var coup = $('coupons').getElementsByTagName('input');
		var toDisable = true;
		var cc = $('cc');
		var regm = new RegExp("(\\d+\\.?\\d*)");
		 
		if( cbCheck != null ){
			if ( !isSet( $("cumulage") ) ) {
				var cum = 0;   
			}else{
				var cum = $('cumulage').innerHTML;
				cum = regm.exec(cum)[0];
				var parent = $('couponTotal');
				while(parent.hasChildNodes() )
					parent.removeChild(parent.firstChild);	
			}
		}

		for( var i = 0 ; i < coup.length ; i++ ){
			if( coup[i].checked == true ){
				toDisable = false;
				totCoup = parseFloat(totCoup) + parseFloat(coup[i].value);
			}
		}

		if( totCoup > 0 ){
			var calcul = ( ( parseFloat(trueTot) * 100 ) - parseFloat(totCoup) * 100 ) / 100;
			if( calcul < 0 )
				trueTot = ( ( parseFloat(trueTot) * 100 ) - ( parseFloat(sousto) * 100 ) ) / 100;
			else
				trueTot = calcul;
		}

		var cheks = $('chekKados').getElementsByTagName('input');
		for( var i = 0 ; i < cheks.length ; i++ ){
			if( cheks[i].checked == true ){
				trueTot = ( ( parseFloat(trueTot) * 100 ) - ( parseFloat( cheks[i].value ) * 100 ) ) / 100;
			}
		}

		if( trueTot == 0 ){
			if( toDisable == true ){
				for( var i = 0 ; i < coup.length ; i++ ){
					if( coup[i].type == 'checkbox' && coup[i].checked == false ){
						coup[i].disabled = 'disabled';
					}
				}
			}
			for( j = 0;j<cheks.length;j++ ){
				if( cheks[j].type == 'checkbox' && cheks[j].checked == false ){
					cheks[j].disabled = 'disabled';
				}
			}
		}else if( trueTot < 0 ){
			$('depassementChek').style.display = 'block';
			if( toDisable == true ){
				for( var i = 0 ; i < coup.length ; i++ ){
					if( coup[i].type == 'checkbox' && coup[i].checked == false ){
						coup[i].disabled = 'disabled';
					}
				}
			}
			for( j = 0;j<cheks.length;j++ ){
				if( cheks[j].type == 'checkbox' && cheks[j].checked == false ){
					cheks[j].disabled = 'disabled';
				}
			}
		}else{
			$('depassementChek').hide();
			if( toDisable == true ){
				for( var i = 0 ; i < coup.length ; i++ ){
					if( coup[i].type == 'checkbox' && coup[i].checked == false ){
						coup[i].disabled = '';
					}
				}
			}
			for( j = 0;j<cheks.length;j++ ){
				if( cc == null ){
					if(cheks[j].type == 'checkbox' && cheks[j].checked == false){
						cheks[j].disabled = '';
					}
				}
			}
		}

		var trueSousTot = parseFloat(sousto)-parseFloat(totCoup) > 0 ? parseFloat(sousto)-parseFloat(totCoup) : 0;
		 
		if( cbCheck != null ){
			if( !cbCheck.checked ) {
				montant = ( ( parseFloat( cum ) * 100 ) - ( parseFloat( cbCheck.value ) * 100 ) ) / 100;
				trueTot = ( ( parseFloat(trueTot) * 100 ) + ( parseFloat( cbCheck.value ) * 100 ) ) / 100;
				this.tab.splice( this.tab.indexOf( cbCheck.name+'='+cbCheck.value ) , 1 );
			}else{
				this.tab.push( cbCheck.name+'='+cbCheck.value );
				montant = ( ( parseFloat(cum) * 100 ) + ( parseFloat(cbCheck.value) * 100 ) ) / 100;
			}
		  
			var parseFl = parseFloat(montant)+'&euro;';
	  
			$('couponTotal').update( 'Total ' );
			$('couponTotal').insert( '<span id="cumulage">'+parseFl+'</span>' );
		}
	},
	
	submitCoupon:function() {
		if( this.tab.length > 0 ) {
			var mescoupons = '';
			for( var i = 0 ; i < this.tab.length ; i++ ){
				mescoupons += this.tab[i];
				if( i < ( this.tab.length - 1 ) )
					mescoupons +=',';
			}
			var elemntsFrame = {
					'htmlelmt' : 'input',
					'attributes' : { 'type' : 'hidden' , 'name' : 'CouponTab' , 'value' : mescoupons }
			   	};
			$('validCoupon').createFrame( elemntsFrame );
		}
	}
	
});