var valid;
var order_after_close = false;    

Prototype.Browser.IE6=Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
Prototype.Browser.IE8=Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==8;
Prototype.Browser.IE9=Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==9;

Event.observe(window, 'unload', do_logoff, false);

function do_logoff(){

    var url = '/modules/ditshop/front/server_delete.php';
    //var params = $('form.module-resultaat.resultaat').serialize(true);
    var params=$H(
    {
        action:'delete_temp',
        sid:Math.random()
    }
    ).toQueryString();
        
    new Ajax.Request(
        url,
        {
          asynchronous: false,
          method: 'get',
          parameters: params
        }    
    );  

    //Event.unloadCache;
}

function checkDayInMonth(date){
    if(date.length() == 10){
        var d = Date.parse(date);

        var day = d.getDate();
        var max_days = dInM(d.getMonth(),d.getFullYear());

        if(day <= max_days){
            return date;
        }
        else{
            return max_days + "-" + d.getMonth() + "-" + d.getFullYear();
        }
    }
    else{
        if(date.length() == 0){
            return "";
        }
        else{
            var d = Date();
            return d.getDate() + "-" + d.getMonth() + "-" + d.getFullYear();
        }
    }
}    
        
function dInM(mon,Year){
    if (mon == 2){
    
        if(Math.round(Year/4) == Year/4){
            
            if(Math.round(Year/100) == Year/100){
                if(Math.round(Year/400) == Year/400)
                    return 29;
                else return 28;
            }
            else return 29;
        }
        return 28;
    }
    else if (mon == 4 || mon == 6 || mon == 9 || mon == 11){
        return 30;
    }
    return 31;
}  

function test_date(element){
   
      val = $F(element);
      var date_array = val.split('-');
      var day = date_array[0];

      // Attention! Javascript consider months in the range 0 - 11
      var month = date_array[1] - 1;
      var year = date_array[2];    
    
       // This instruction will create a date object
       source_date = new Date(year,month,day);

       if(year != source_date.getFullYear())
       {
          alert('Geen geldig jaar ingevuld!');
          return false;
       }

       if(month != source_date.getMonth())
       {
          alert('Geen geldige maand ingevuld!');
          return false;
       }

       if(day != source_date.getDate())
       {
          alert('Geen geldige dag ingevuld!');
          return false;
       }
 
 
        new Ajax.Request('/modules/ditshop/front/server_date.php?date='+val, {
          method:'get',
          onComplete: function(transport){
             var json = transport.responseText.evalJSON();

             if(json.success){
                //if($('delivery_desired_date').value != json.result){
                    
                    $('delivery_desired_date').value = json.result; 
                //}
             }
             else {
                //alert('wrong');
             }
             
           },
           onException: function(req,exception){  
                
           }
        });      
}  


var Cart = Class.create({

    initialize: function() {     
        this.url = '/modules/ditshop/front/server_cart.php';  
    },
	addToCartChoice: function(div_id,formId,choiceId,id,quantity){

    	if(isNaN(quantity) || /^\s+$/.test(quantity)){
       		quantity = 1;
    	}

    	if($RF(formId,choiceId) == 'fabrikant'){
        	this.addFabToCart(div_id,id,'',quantity);
    	}
    	else{
        	this.addLevToCart(div_id,id,'',quantity);   
    	}
	},
	addRelated: function(div_id){

    	var params=$H(
    		{
        		action: 'addRelated',
        		cart_mode: 'simple',
        		sid:Math.random()
    		}
    	).toQueryString();

    	params = params + "&" + $('form_related').serialize();

    	$(div_id).ajaxUpdate(
        	this.url,
        	{
          		method: 'post',
          		parameters: params,
          		onComplete: function(){
					this.showCompleteCart();
				}.bind(this)
        	}    
    	);    
	},
	addFabToCart: function(div_id,id,rel,quantity){
	    var cart_mode = 'simple';
	    if (div_id == 'winkelmand_container') {
			cart_mode = 'extended';
		}
	    
	    var params=$H(
	    {
	        action: 'addFabToCart',
	        article_id:id,
	        relatie_code:rel,
	        cart_mode: cart_mode,
	        quantity:quantity,
	        sid:Math.random()
	    }
	    ).toQueryString();
	
	    if(div_id == 'winkelmand_container'){
	        $(div_id).ajaxUpdate(
	            this.url,
	            {
	              method: 'get',
	              parameters: params,
	              onComplete: function(){
	                 //resize(); 
	              }
	            }    
	        );
	    }
	    else{
	        $(div_id).ajaxUpdate(
	            this.url,
	            {
	              method: 'get',
	              parameters: params,
	              onComplete: function(){
				  	this.showCompleteCart();
				  }.bind(this)
	            }    
	        );    
	    }
	},
	addLevToCart: function(div_id,id,rel,quantity){

	    var cart_mode = 'simple';
	    if (div_id == 'winkelmand_container') {
			cart_mode = 'extended';
		}
	        
	    var params=$H(
	    {
	        action: 'addLevToCart',
	        article_id:id,
	        relatie_code:rel,
	        cart_mode: cart_mode, 
	        quantity:quantity,
	        sid:Math.random()
	    }
	    ).toQueryString();
	
	    
	    if(div_id == 'winkelmand_container'){
	        $(div_id).ajaxUpdate(
	            this.url,
	            {
	              method: 'get',
	              parameters: params,
	              onComplete: function(){
	                    //resize();  
	                    
	              }.bind(this)        
	            }    
	        );
	    }
	    else{
	        $(div_id).ajaxUpdate(
	            this.url,
	            {
	              method: 'get',
	              parameters: params,
	              onComplete: function(){
				  	this.showCompleteCart();
				  }.bind(this)
	            }    
	        );    
	    }
	},
	removeFromCart: function(id,article_id,cart_id){
		
	    var params=$H(
	    {
	        action: 'remove',
	        article_id: article_id,
	        cart_id: cart_id,
	        sid:Math.random()
	    }
	    ).toQueryString();
	
	    
	    $(id).ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: params,
	          onComplete: function(){
	
	               // resize();
	          }          
	          
	        }    
	    );
	},
	updateToCart: function(id,article_id,quantity,cart_id){


	    var params=$H(
	    {
	        action: 'update',
	        article_id: article_id,  
	        cart_id: cart_id,
	        quantity:quantity,
	        sid:Math.random()
	    }
	    ).toQueryString();
	
	    
	    $(id).ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: params,
	          onComplete: function(){
	
	            //resize(); 
	          }            
	        }    
	    );
	},
	refreshCart: function(id){
	    var params=$H(
	    {
	        action: 'refresh',
	        sid:Math.random()
	    }
	    ).toQueryString();
	
	    
	    $(id).ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: params
	        }    
	    );
	},
	clearCart: function(){
	    var params=$H(
	    {
	        action: 'clearCart',
	        sid:Math.random()
	    }
	    ).toQueryString();
	
	    
	    $('winkelmand_container').ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: params,
	          onComplete: function(){
	
	            //resize(); 
	          }
	        }    
	    );
	},
	saveCart: function(id,name){

	    var params=$H(
	    {
	        action: 'save_cart',
	        cart_id: id,
	        cart_name: name,
	        sid:Math.random()
	    }
	    ).toQueryString();
	
	    
	    $('winkelmandje-storebox').ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: params,
	          onComplete: function(){
	            alert('Bestellijst is opgeslagen');
	          }
	        }    
	    );
	},										
	loadSavedCart: function(id){

	    var params=$H(
	    {
	        action: 'load_saved_cart',
	        cart_id: id,
	        sid:Math.random()
	    }
	    ).toQueryString();
	
	    
	    $('header_cart').ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: params,
	          onComplete: function(){
	            this.showCompleteCart();
	          }.bind(this)
	
	        }    
	    );
	
	},
	deleteSavedCart: function(id){
	
	    var params=$H(
	    {
	        action: 'delete_saved_cart',
	        cart_id: id,
	        sid:Math.random()
	    }
	    ).toQueryString();
	
	    
	    $('order_list_overview').ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: params
	        }    
	    );
	},
	goto_checkout: function(step,substep,action){

	    
	    var params=$H(
	    {
	        action: action,
	        step: step,
	        substep: substep,
	        sid:Math.random()
	    }
	    ).toQueryString();
	
	    
	    $('popup_module-resultaat').ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: params,
	          onComplete: function(){
					refresh_lv();
					

	                if($('form_checkout')){
	                    valid = new Validation('form_checkout', {immediate : true, onFormValidate : formCallback,useTitles:true});          
	                }

					
					
					
	          }.bind(this)
	        }    
	    );  
	
	},   
    checkout_goto: function(step){
    	this.goto_checkout(step,'','show');
	},
	checkout_goto_advanced: function(step){
    	this.goto_checkout(step,'','advancedShow');
	},
	checkout_sub_goto: function(step,substep){
    	this.goto_checkout(step,substep,'advancedShow');
	},
	checkout_confirm_form: function(form,nextstep){
	        $(form).request({
	            onComplete: function(req){ 
	                    if(dialogCounter == 1) 
	                        hideProcessing();  
	                    $('popup_module-resultaat').innerHTML = req.responseText; 
	                    
	                    if($('checkout_final')){ 
	                        order_after_close = true;
	                    }
	                    
	                    
	                    if ($('form_checkout')) {
							valid = new Validation('form_checkout', {
								immediate: true,
								onFormValidate: formCallback,
								useTitles: true
							});
						}
	                    
	                /*if($('winkelmandje-overzicht')){
	                
	                    height = $('lightview').getHeight();
	                    width  = $('lightview').getWidth(); 
	                    
	                    $('winkelmandje-overzicht').setStyle({'width':width-15+'px', 'height':(height*0.45)-5+'px'});  
	                } */                   
	                    
	            }.bind(this)
	        }); 
	    return false;
	},
	showCompleteCart: function(){

        var autosize =  false;
		var fullscreen = true;
		if (Prototype.Browser.IE6) {
			hideBackground();
			var autosize =  false;
			var fullscreen = true;
		}
		
		
	    var params=$H(
	    {
	        action:'show',
	        sid:Math.random()
	    }
	    ).toQueryString();



	    Lightview.show({
	      href: this.url,
	      rel: 'ajax',
		  viewport:true,
	      options: {
	        menubar: false,
	        innerPreviousNext:false,
	        overflow:'hidden',
	        topclose: true,
	  		autosize: autosize,
			fullscreen:fullscreen,   
	        ajax: {
	          method: 'get',
	          parameters: params, 
	          onComplete: function(){ 
	           
	           // $('lightview').fire('foo:bar', 'foobarbaz');
	             refresh_lv();
	

	
	          }
	        }
	      }
	    });
    },
	showAdvancedCart: function(){

        var autosize =  false;
		var fullscreen = true;
		if (Prototype.Browser.IE6) {
			hideBackground();
			var autosize =  false;
			var fullscreen = true;
		}

	    var params=$H(
	    {
	        action:'advancedShow',
	        sid:Math.random()
	    }
	    ).toQueryString();
	
	    //obj.bfx = obj.fx.bindAsEventListener(obj);
	    //Event.observe($('lightview'),'foo:bar', obj.bfx);    
	
	    Lightview.show({
	      href: this.url,
	      rel: 'ajax',
		  viewport:true,
	      options: {
	        menubar: false,
	        innerPreviousNext:false,
	        overflow:'hidden',
	        topclose: true,
	        autosize: autosize, 
			fullscreen:fullscreen, 
	        ajax: {
	              method: 'get',
	              parameters: params, 
	              onComplete: function(){ 
	                    if($('form_checkout')){  
	                        valid = new Validation('form_checkout', {immediate : true, onFormValidate : formCallback,useTitles:true});          
	                    }
	              }.bind(this)
	        }
	      }
	    });    
	},
	change_select_delivery: function(select_id){

	    if ($F(select_id) == -1) {
			$('delivery.custom_delivery').show();
		}
		else {
			$('delivery.custom_delivery').hide();
		}
	}	
});

var Payment = Class.create({   
    initialize: function() {   
    
    }
	
});

var Login = Class.create({   

});

var Shop = Class.create({   
    
    initialize: function(options) {     
        this.url 		= '/modules/ditshop/front/server.php';  
        this.cart 		= new Cart();
        this.payment 	= new Payment();
        this.login 		= new Login();
        this.req_parameters = "";
        this.product_detail_fullscreen = false;   
        
        this.request = null;
        this.requestQueue = [];    

		this.isExtended = 1;
    },
	getExtended: function(){
		return this.isExtended;
	},
	setExtended:function(value){
		this.isExtended = value;
	},
	showCompleteCart: function(action){
        this.cart.showCompleteCart(action);
    },
	getCart: function(){    
        return this.cart;
    },
	getLogin: function(){    
        return this.login;
    },
	getPayment: function(){    
        return this.payment;
    },
	updateCategory: function(catid){

    	$('module-resultaat.breadcrumb').style.display = 'block'; 
    	this.updateCategoryForElement(catid,'tree','module-infoContentsInner');   
    	this.updateCategoryForElement(catid,'breadcrumb','module-resultaat.breadcrumb');   
    	this.updateCategoryForElement(catid,'gallery','module-resultaat.resultaat');   
    	this.updateFilterList();
    	//ditshopApp.promtForNew(catid);              
	},
	showProductDetail: function(){
    
	    if ($('filters')) {
			$('filters').style.display = 'none';
		}                                                    
	    
	    if($('module-resultaat.resultaat')){
	
	        $('module-resultaat.resultaat').style.display = 'none';
	    }
	    
	    if($('module-resultaat.detail')){
	 
	        $('module-resultaat.detail').style.display = 'block';                                                    
	    }
        
	},
	closeProductDetail: function(){
    	this.showProductSearch();   
	},
	showProductSearch: function(){

	    if($('product-details') ){
	            $('product-details').style.display = 'none';                                                     
	    }
	    
	    if($('module-resultaat.detail') ){ 
	        $('module-resultaat.detail').style.display = 'none';                                                     
	    }
	    
	    if($('filters') ){ 
	        $('filters').style.display = 'block';                                                    
	    }
	    
	    if($('module-resultaat.resultaat') ){ 
	        $('module-resultaat.resultaat').style.display = 'block'; 
	    }
	},
	changeTab: function(artikelCode,relatieCode,tab,tab_selected){
    
    	var params=$H(
    		{
        		action:'tabchange',
        		artikelCode : artikelCode,
        		relatieCode : relatieCode,        
        		tab_selected: tab_selected,
        		sid:Math.random()
    		}
    	).toQueryString();
     
    	req_parameters = removeValuePairs(req_parameters,'sid','');
    	req_parameters = removeValuePairs(req_parameters,'action','');  
    	req_parameters = removeValuePairs(req_parameters,'artikelCode','');  
    	req_parameters = removeValuePairs(req_parameters,'relatieCode','');  
    	req_parameters = removeValuePairs(req_parameters,'tab_selected','');  
    	req_parameters = req_parameters + "&" + params;     
        
    	$(tab).ajaxUpdate(
        	this.url,
        	{
	          	method: 'get',
          		parameters: params
        	}    
    	);    
    
	},
	changeViewType: function(){
    
    	this.showProductSearch();
    
    	var params=$H(
    		{
        		action:'typeSelection',
        		type: $F('weergave'),
        		sid:Math.random()
    		}
    	).toQueryString();
     
    	req_parameters = removeValuePairs(req_parameters,'sid','');
    	req_parameters = removeValuePairs(req_parameters,'action','');  
    	req_parameters = removeValuePairs(req_parameters,'type','');             
    	req_parameters = removeValuePairs(req_parameters,'nav_page','');           
    	req_parameters = req_parameters + "&" + params;
    
	    $('module-resultaat.resultaat').ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: req_parameters,
	          onComplete: function(){
	            Lightview.updateViews();
	          }
	        }    
	    ); 
    
    	window.location.hash='#top';        
	},
	changeSort: function(column,direction){
    
    	this.showProductSearch();
    
	    if(!column){
	        pair = $F('sortering').split("|");
	        column = pair[0];
	        direction = pair[1];
	    }

	    var params=$H(
	    {
	        action:'sort',
	        column:column,
	        direction:direction,
	        sid:Math.random()
	    }
	    ).toQueryString();
	    
	    req_parameters = removeValuePairs(req_parameters,'sid','');  
	    req_parameters = removeValuePairs(req_parameters,'action','');  
	    req_parameters = removeValuePairs(req_parameters,'sort',''); 
	    req_parameters = removeValuePairs(req_parameters,'direction',''); 
	    req_parameters = removeValuePairs(req_parameters,'column','');
	    req_parameters = req_parameters + "&" + params;
	    
	    $('module-resultaat.resultaat').ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: req_parameters,
	          onComplete: function(){
	            Lightview.updateViews();  
	          }
	        }    
	    );   
	    
	    window.location.hash='#top';     
	},
	removeFilter: function(key,value){

    	this.showProductSearch();

	    var params=$H(
	    {
	        action:'removefilter',
	        nav_page: 1,
	        sid:Math.random()
	        
	    }
	    ).toQueryString();
	    
	    
	    req_parameters = removeValuePairs(req_parameters,'column','');
	    req_parameters = removeValuePairs(req_parameters,'direction','');
	    if (key == 'f_UBIM') {
			req_parameters = removeAllFilters(req_parameters);
		}    
	    
	    req_parameters = removeValuePairs(req_parameters,key,value) + "&" + params; 
	    
	
	        
	    //alert(req_parameters);
	    
	    $('module-resultaat.resultaat').ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: req_parameters,
	          onComplete: function(){
	            Lightview.updateViews();  
	          }
	        }    
	    );    
	    
	    this.updateFilterList();   
	},
	addFilter: function(key,value){
   
	    this.showProductSearch();

	    req_parameters = removeValuePairs(req_parameters,'action','');
	    req_parameters = removeValuePairs(req_parameters,'sid','');
	    req_parameters = removeValuePairs(req_parameters,'nav_page','');
	    
	    var params=$H(
	    {
	        action:'addfilter',
	        nav_page: 1,
	        sid:Math.random() 
	        
	    }
	    ).toQueryString();
	    
	    
	    req_parameters = removeValuePairs(req_parameters,'column','');
	    req_parameters = removeValuePairs(req_parameters,'direction','');    
	    
	    req_parameters = req_parameters + '&' + key + '=' + value + "&" + params;
	    
	    $('module-resultaat.resultaat').ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: req_parameters,
	          onComplete: function(){
	            Lightview.updateViews();  
	          }
	        }    
	    );    
	    
	    this.updateFilterList();
	},
	getProductPrice: function(artikelCode, relatieCode){

	    var params=$H(
	    {
	        action:'productprice',
	        artikelCode : artikelCode,
	        relatieCode : relatieCode,
	        sid:Math.random()                                      
	    }
	    ).toQueryString();
	    
	    $('product_prijs_klant').ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: params,
	          onComplete:function(){ 
	
	          }
	        }    
	    );      
	},
	getProductMainStock: function(artikelCode, relatieCode){
 
	    var params=$H(
	    {
	        action:'productmainstock',
	        artikelCode : artikelCode,
	        relatieCode : relatieCode,
	        sid:Math.random()                                      
	    }
	    ).toQueryString();
	    
	    $('mainvoorraad').ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: params,
	          onComplete:function(){ 
	
	          }
	        }    
	    );      
	},
	subproductDetails: function(artikelCode,relatieCode,parentArtikelCode,parentRelatieCode){
		this.productDetail(artikelCode, relatieCode,parentArtikelCode,parentRelatieCode,1,0);
	},
	returnToParentProductDetails: function(artikelCode,relatieCode){
    	this.productDetail(artikelCode, relatieCode,'','',0,1);
	},
	productDetails: function(artikelCode, relatieCode){
    	this.productDetail(artikelCode, relatieCode,'','',0,0);
	},
	productDetail: function(artikelCode, relatieCode,parentArtikelCode,parentRelatieCode,sub,backfromsub){
    
    	this.showProductDetail();


	    var params=$H(
	    {
	        action:'details',
	        artikelCode : artikelCode,
	        relatieCode : relatieCode,
	        parentArtikelCode : parentArtikelCode, 
	        parentRelatieCode : parentRelatieCode,
	        sub: sub,
	        backfromsub: backfromsub,
	        sid:Math.random()                                      
	    }
	    ).toQueryString();
	
	    $('module-resultaat.detail').ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: params,
	          onComplete:function(req){ 
	               
	              Lightview.updateViews();
	              this.getProductPrice(artikelCode,relatieCode);
	              this.getProductMainStock(artikelCode,relatieCode);  
	          }.bind(this)
	        }    
	    );      
	    
	    return false;
	},
	searchProducts: function(clean){

	    this.showProductSearch();
	    searchTerms = $F('searchterms');
	    exact = $F('exact');

	    var params=$H(
	    {
	        action:'search',
	        searchTerms : searchTerms,
	        exact: exact,
	        sid:Math.random()                                      
	    }
	    ).toQueryString();
	    
	    if(clean){
	        req_parameters = params;
	    }
	    else{
	        req_parameters = removeValuePairs(req_parameters,'searchTerms',''); 
	        req_parameters = removeValuePairs(req_parameters,'sid','');         
	        req_parameters = removeValuePairs(req_parameters,'exact','');         
	        req_parameters = req_parameters + '&' + params;
	    }

	    $('module-resultaat.resultaat').ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: req_parameters,
	          onComplete:function(){ 
	
	                $('module-resultaat').removeClassName('module-resultaat');
	                $('module-resultaat').addClassName('module-resultaat-full');
	                
	                if ($('menu_titel')) {
						$('menu_titel').addClassName('menu_titel_collapse');
					}  
	                    
	                $('module-info').style.width =  '22px';
	                $('module-infoContents').style.display = 'none';   
	
	                
	                $('module-resultaat.breadcrumb').style.display = 'none';
	                
	                this.isExtended = 0; 
	                if($('filters')){   
	 
	                    this.updateFilterList();
	                }
	          }.bind(this)
	        }    
	    );       
	},
	updateFilterList: function(){
    
    	this.showProductSearch();
    
	    var params=$H(
	    {
	        action:'filter',
	        sid:Math.random()
	        
	    }
	    ).toQueryString();
	    
	    req_parameters = removeValuePairs(req_parameters,'sid',''); 
	    req_parameters = removeValuePairs(req_parameters,'action',''); 
	    req_parameters = req_parameters + "&" + params;
	    
	    $('filters').ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: req_parameters
	        }    
	    );  
	},
	updateProductListPage: function(page){
	
	    this.showProductSearch();

	    var params=$H(
	    {
	        action:'navigate',
	        nav_page:page,
	        sid:Math.random()
	    }
	    ).toQueryString();
	    
	    
	    req_parameters = removeValuePairs(req_parameters,'action',''); 
	    req_parameters = removeValuePairs(req_parameters,'nav_page',''); 
	    req_parameters = removeValuePairs(req_parameters,'sid','');     
	    req_parameters = req_parameters + "&" + params;
	
	    $('module-resultaat.resultaat').ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: req_parameters,
	          onComplete: function(){
	            Lightview.updateViews();  
	          }
	        }    
	    );      
	    window.location.hash='#top';  
	    
	},
	updateCategoryForElement: function(catid,action,elementId){

	    var params=$H(
	    {
	        catid:catid,
	        action:action,
	        sid:Math.random()
	    }
	    ).toQueryString();
	
	    req_parameters = params; 
	    
	    $(elementId).ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: params
	        }    
	    );
	},
	
	
	myCallBackOnStart: function(obj){

	   $('module-info').style.width =  '240px'; 
	   $('menu_titel').removeClassName('menu_titel_collapse');        
	   $('menu_titel').addClassName('menu_titel');  
	   
	   $('module-resultaat').removeClassName('module-resultaat-full');
	   $('module-resultaat').addClassName('module-resultaat');
    },
	myCallBackOnFinish: function(obj){
 
   		$('module-resultaat').removeClassName('module-resultaat');   
   		$('module-resultaat').addClassName('module-resultaat-full'); 
   		$('menu_titel').addClassName('menu_titel_collapse');  
   		$('module-info').style.width =  '22px';
	},
	slideSideBar: function(){


   	    new Effect.toggle('module-infoContents', 'blind', {scaleX: 'true', scaleY: 'false;', scaleContent: true});

	    if(this.isExtended==0){
	         
	        new Effect.Fade('module-infoContents',
				{ 
					duration:1.0, from:0.0, to:1.0,
	        		beforeStart: this.myCallBackOnStart.bind(this)
				}
			); 
	        
	        this.isExtended++;
	    }
	    else{                                                                                                                   

	        new Effect.Fade('module-infoContents',
				{ 
					duration:1.0, from:1.0, to:0.0,
	       			afterFinish: this.myCallBackOnFinish.bind(this)
				}
			); 
	
	        this.isExtended=0;
	    }
    
    	return false;
	}	
		
});

var PPage = Class.create({
    
    initialize: function() {   
        this.url = '/modules/ditshop/front/server_ppage.php'; 
    },
	changeValidation: function(val){
 
        valid.reset();
        switch(val){
            case 'DE':
                    $('company_btwnummer').removeClassName('required');
                    break;        
            case 'NL':
                    $('company_btwnummer').addClassName('required');
                    break;
            case 'BE':
                    $('company_btwnummer').removeClassName('required');
                    break;        
        }                        
	},  
	sameCheckValue: function(){

	    var boxStatus;
	    boxStatus = eval($('place1').checked);
	    if(boxStatus == true){
	        $('company_delivery_address').value = $F('company_address'); 
	        $('company_delivery_housenumber').value = $F('company_housenumber');      
	        $('company_delivery_zipcode').value = $F('company_zipcode');    
	        $('company_delivery_city').value = $F('company_city');      
	    }
	},            
	submitResultaat: function(form,action){
	    
	
	    $(form).request({
	        onComplete: function(req){ 
	                if(dialogCounter == 1) 
	                    hideProcessing(); 
	                    
	                $('module-resultaat.resultaat').innerHTML = req.responseText; 
	               
	                if($('form_subscription'))   
	                    valid = new Validation('form_subscription', {immediate : true, onFormValidate : formCallback,useTitles:true});
	                 
	                 this.orderHistoryOverviewFilter();
	
	                               
	        }.bind(this)
	    });    
	    return false;
	},   
	orderHistoryOverviewFilter: function(){
	    if($('form_overview_filter')){
	        if($F('action') == 'order_history_overview'){
	                
	                var status = -1;
	                var nr = "";
	                
	                if($('orderhistory_nummer'))
	                    nr = $F('orderhistory_nummer').toLowerCase();
	                
	                if($('orderhistory_status'))
	                    status = $F('orderhistory_status').toLowerCase();
	
	                if(nr || status > -1){
	
	          
	                    $$('#orderhistory_table tr:not([class~=orderhistory_header])').each(function(el){
	                       el.hide();
	                    });                            
	                    
	                    if(status > -1){
	                        
	                        if(nr){
	                            $$('#orderhistory_table tr.status_'+status).each(function(el){
	                                if(el.hasClassName(nr)){
	                                    el.show(); 
	                                }
	                            });                        
	                        }
	                        else{
	                            $$('#orderhistory_table tr.status_'+status).each(function(el){
	                                el.show(); 
	                            });
	                            
	                        }
	                    }
	                    else{
	                        $$('#orderhistory_table tr.'+nr).each(function(el){
	                             el.show(); 
	                        });                    
	                    }
	                
	                }
	        }
	    }  
	}, 
	updateResultaatDeleteId: function(action,id){
	
	    if (confirm('Gebruiker verwijderen?')){      

	        var params=$H(
	        {
	            action:action,
	            res_id:id,
	            sid:Math.random() 
	            
	        }
	        ).toQueryString();

	        $('module-resultaat.resultaat').ajaxUpdate(
	            this.url,
	            {
	              method: 'get',
	              parameters: params,
	              onComplete: function(){
	                    if($('form_subscription')){ 
	                        valid = new Validation('form_subscription', {immediate : true, onFormValidate : formCallback,useTitles:true});
						}                   
	              }.bind(this)
	            }    
	        );  
	    }
	},
	updateResultaatId: function(action,id){
	
	    var params=$H(
	    {
	        action:action,
	        res_id:id,
	        sid:Math.random() 
	        
	    }
	    ).toQueryString();

	    $('module-resultaat.resultaat').ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: params,
	          evalScripts:true,
	          onComplete: function(){
	                
	                if ($('form_subscription')) {
						valid = new Validation('form_subscription', {
							immediate: true,
							onFormValidate: formCallback,
							useTitles: true
						});
					}
	                                       
	          }.bind(this)
	          
	        }    
	    );  
	},
	updateResultaat: function(action){
		
		
		
		if(typeof $('module-resultaat.resultaat') == "undefined" ){
		    return true;
		}
		else{
		    $('module-resultaat').removeClassName('module-resultaat');
		    $('module-resultaat').addClassName('module-resultaat-full');
		    
		    if(typeof $('module-info') != "undefined" ){
		        
		        $('module-info').style.display = 'none';
		        $('locatie-informatie').innerHTML = '<a href=\'\'>Mijn Dumoulin</a><span id="module-resultaat.breadcrumb" style="margin: 0px; padding: 0px; float: left;"></span>'; 
		        $('module-infoContentsInner').style.display = 'none';   
		        $('module-resultaat.breadcrumb').style.display = 'none';
		    }
		   

		    var params=$H(
		    {
		        action:action,
		        sid:Math.random() 
		        
		    }
		    ).toQueryString();
		    
		
		    
		    $('module-resultaat.resultaat').ajaxUpdate(
		        this.url,
		        {
		          method: 'get',
		          parameters: params,
		          evalScripts:true,
		          onComplete: function(){
		                //eval(transport.responseText); 
		                if($('form_subscription')){
		                    valid = new Validation('form_subscription', {immediate : true, onFormValidate : formCallback,useTitles:true});
		                }
		                
		                if($('form_icm_upload_advanced')){
		                
		                    // Create an instance of the multiSelector class, pass it the output target and the max number of files
		                    var multi_selector = new MultiSelector( $( 'files_list' ), 15 );
		                    // Pass in the file element
		                    multi_selector.addElement( $( 'my_file_element' ) ); 
		                }                    
		                this.orderHistoryOverviewFilter();                        
		          }.bind(this)
		          
		        }    
		    );  
		    return false;
		}
	},
   	conditie_bestand_request: function(){

	    var params=$H(
	    {
	        action:'conditie_bestand_request',
	        sid:Math.random() 
	        
	    }
	    ).toQueryString();
	    
	
	    
	    $('conditie_bestand_response').ajaxUpdate(
	        this.url,
	        {
	          method: 'get',
	          parameters: params,
	          onComplete: function(){    
	                $('conditie_bestand_button').hide();
	          }
	        }    
	    ); 
	},   
	showICMUpload: function(filename){

	    var params=$H(
	    {
	        action:'shopICMUpload',
	        filename: filename,
	        sid:Math.random()
	    }
	    ).toQueryString();
	    
	    Lightview.show({
	      href: this.url,
	      rel: 'ajax',
		  viewport:true,
	      options: {
	        menubar: false,
	        innerPreviousNext:false,
	        overflow:'auto',
	        topclose: true,
	        autosize: false,  
			fullscreen:true,   
	        ajax: {
	          method: 'get',
	          parameters: params, 
	          onComplete: function(){ 
	           
	            //$('lightview').fire('foo:bar', 'foobarbaz');
	         
	
	          }
	        }
	      }
	    });    
	}	
});

var shop = new Shop(); 
var ppage = new PPage();
     


function removeAllFilters(params){

   
     if (params[0] == "?") {
	 	params = params.substr(1);
	 }
        
     var pairs = params.split("&");  
     var splitPair;  
     
     res = "";
     // Load the key/values of the return collection  
     for (var i = 0; i < pairs.length; i++) {  
         splitPair = pairs[i].split("=");  
         found = splitPair[0].indexOf('f_');
         
         if(found == -1){
 
             if (res.length == 0) {
			 	res = res + "?";
			 }
			 else {
			 	res = res + "&";
			 }
             
             res = res + splitPair[0] + "="  + splitPair[1] ;  
         }

     }  
   
     return res;   
}

function removeValuePairs(params, key,value){
   
     if(params[0] == "?")
        params = params.substr(1);
        
     var pairs = params.split("&");  
     var splitPair;  
     
     res = "";
     // Load the key/values of the return collection  
     for (var i = 0; i < pairs.length; i++) {  
         splitPair = pairs[i].split("=");  

         if(key != splitPair[0]){
 
             if(res.length == 0)
                res = res + "?";
             else
                res = res + "&";
             
             res = res + splitPair[0] + "="  + splitPair[1] ;  
         }

     }  
   
     return res;        
}


function checkNumber(elId,afname){
    v = $F(elId);
    
    if(isNaN(v) || /^\s+$/.test(v)){
       $(elId).value = afname;
    }
    else{
        if(v == 0){
            $(elId).value = afname;
            v = afname;
        }
            
        if((v % afname) > 0){
           alert('Minimale afname is ' + afname + '. U kunt alleen hier meervouden van kiezen.');
           $(elId).value = afname;
        }
    }
}

function addQuantity(elId,afname){
    $(elId).value = parseInt($F(elId)) + afname;
    
    checkNumber(elId,afname);
}

function removeQuantity(elId,afname){

    temp = parseInt($F(elId)) - afname;
    if(temp < afname)
        $(elId).value = afname;
    else
        $(elId).value = temp;
    
    checkNumber(elId,afname);
}


function refresh_lv(){
	
	return;
	
}

last_height = 0;
last_width  = 0;

function resize(document){

		var height = parseInt(document.viewport.getHeight()) - 80;
		var width = parseInt(document.viewport.getWidth()) - 80;
		
		if(last_height == height && last_width == width)
			return;

		if (Prototype.Browser.IE6) {
		

    		$('lightview').style.marginTop = '0px';
    		$('lightview').style.top = '30px';		

			var el = $$('.lv_contentTop')[0];
			if (el) {
	
				el.setStyle({
					'width': (width - 4) + 'px',
					'height': (height - 4) + 'px'
				});
				
			}
			
			last_width = width;
			last_height = height;			
			
			Lightview.resize({
				width: width,
				height: height
			});
			
	
		
		}
		else {
		
			var height = parseInt(document.viewport.getHeight()) - 80;
			var width = parseInt(document.viewport.getWidth()) - 80;
			
			
			var el = $$('.lv_contentTop')[0];
			if (el) {
				el.setStyle({
					'width': (width - 4) + 'px',
					'height': (height - 4) + 'px'
				});
				
			}
			
			last_width = width;
			last_height = height;
					
			Lightview.resize({
				width: width,
				height: height
			});
		}
		

		
}

Event.observe(document.onresize ? document : window, "resize", function() {

		resize(document); 
 
}); 

function hideBackground(){
	
	  	var body = document.body;
	  	body = $(body);
		var container = body.down('div.container');
		container.setStyle({height:'0px'});
		body.setStyle({height:'0px'});	
		container.hide();
		

}

function showBackground(){
    
	last_height = 0;
	last_width  = 0;
			
	var body = document.body;
    body = $(body);     
  
    body.down('div.container').show();    	
	
}

document.observe('lightview:opened', function(event) {
  if (!event.target.href) {

		if(!Prototype.Browser.IE6)
			hideBackground();


  }
});

document.observe('lightview:hidden', function(event) {
  if(!event.target.href){
  
  	showBackground();

  
    if ($('header_cart')) {
        shop.getCart().refreshCart('header_cart'); 
    }   
                                
    if($('order_list_overview') != null ){ 
       //ppage.updateResultaat('order_list_overview'); 
    }  
  }
});



document.observe("dom:loaded", function () {
        if($('login')){
            $('login').observe('keydown', function (e) {

                    if ( e.keyCode == 13 ) {
                            //event.findElement('form').submit();
                            $('login').submit();
                    }
            });
        }
}, false);


document.observe('dom:loaded', function() {
  $$('a[rel]').each(function(element) {      
    if(element.rel.indexOf('cloneMe_') > -1){  
        new Tip(element, $(element.rel).cloneNode(true), { 
                    title: "Help",
                    target: element.up('div'),
                    showOn: 'click',
                    hideOn: { element: 'closeButton', event: 'click' },
                    stem: 'topRight',
                    hook: { mouse:true, tip: 'topRight' },
                    width: 'auto',
                    viewport:false  
                });
    }
  });
});

document.observe('foo:inithelp', function() {
  

  $$('a[rel]').each(function(element) {
    if(element.rel.indexOf('cloneMe_') > -1){
        new Tip(element, $(element.rel).cloneNode(true), { 
                    title: "Help",
                    target: element.up('div'),
                    showOn: 'click',
                    hideOn: { element: 'closeButton', event: 'click' },
                    stem: 'topRight',
                    hook: { mouse:true, tip: 'topRight' },
                    width: 'auto',
                    viewport:false  
                });
    }
  });
});
 


//function checkDate(el,year,month,date){
    /*
    var myDate=new Date();
    month = month - 1;
        
    myDate.setFullYear(year,month,date); 
    
    d = isDate($F(el));

    
    if (d < myDate){
    
        day = myDate.getDate() + "";
        month = (myDate.getMonth() + 1) + "";

        if(day.length == 1){
          day = "0" + day;
        }
        if(month.length == 1){
          month = "0" + month;
        }    
    
        alert('Leveringen kunnen pas de volgende werkdag bezorgd worden!');
        str = day  + '-' + month + '-' + myDate.getFullYear();
        $(el).value = str;
    }
    else{
                                      
        w = d.getDay();
        
        if (w==0){
             d.setDate(d.getDate()+1);
             alert('Geen leveringen in het weekend!');
        }
        if (w==6){ 
            d.setDate(d.getDate()+2);
            alert('Geen leveringen in het weekend!');
        }
        
        day = d.getDate() + "";
        month = (d.getMonth() + 1) + "";

        if(day.length == 1){
          day = "0" + day;
        }
        if(month.length == 1){
          month = "0" + month;
        }
        
        str = day + '-' + month + '-' + d.getFullYear();
        $(el).value = str;    
    }    */

//}





document.observe('lightview:hidden', function(event) {

    if(order_after_close){
        location.replace("/mijn_dumoulin/?action=order_history_overview");
        order_after_close = false;
    }
    else{
        if(!event.target.href){   
			showBackground();
			
            if($('header_cart'))
                shop.getCart().refreshCart('header_cart');   
            
            if($('icm_upload_container'))
                ppage.updateResultaat('icm_upload_advanced');
                                        
            if($('order_list_overview') != null ){ 
               ppage.updateResultaat('order_list_overview'); 
            }
        }
    }

});    



function checkICMFile(value){

    if (value.length > 0)
    {

        re = new RegExp("\.(ICM|TXT|icm|txt)$");
        if (value.match(re)){

            return true;
        }
    }
    return false;
}

function initUpload(){
 	new FileUploader('icmUploader', 'status');  
}

