var valid;

Prototype.Browser.IE6=Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==6;          
Prototype.Browser.IE7=Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==7;
Prototype.Browser.IE8=Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==8;

var Cart = Class.create({

    initialize: function() {     
        this.url = SHOP_BASE_REL+'front/server_cart.php';  
    }
    ,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: this.showCompleteCart()
            }    
        );    
    }
    ,addToCart: function(div_id,product_id,quantity){
        
        cart_mode = 'simple';
        if(div_id == 'winkelmand_container')
            cart_mode = 'extended';
    
    
        var params=$H(
            {
                action: 'addToCart',
                product_id:product_id,
                cart_mode: cart_mode,
                quantity:quantity,
                sid:Math.random()
            }
        ).toQueryString();

        if(div_id == 'winkelmand_container'){
            $(div_id).ajaxUpdate(
                this.url,
                {
                    method: 'get',
                    parameters: params
                }    
            );
        }
        else{
            $(div_id).ajaxUpdate(
                this.url,
                {
                    method: 'get',
                    parameters: params
                }    
            );    
        }
    }
    ,removeFromCart: function(id,product_id,cart_id){
        
        var params=$H(
            {
                action: 'remove',
                product_id: product_id,
                cart_id: cart_id,
                sid:Math.random()
            }
        ).toQueryString();

    
        $(id).ajaxUpdate(
            this.url,
            {
                method: 'get',
                parameters: params,
                onComplete: function(){
                        $$('.resizing_textareas').each(function(el){

                            new Texpand(el, {autoShrink: true}); 
                        });                      
                    resize();                   
                }          
            }    
        );
    }
    ,checkUpdate: function(id,start_quantity,product_id,quantity,cart_id){
        
        if(start_quantity != quantity){
            this.updateToCart(id,product_id,quantity,cart_id);   
            
        }
        
    }
    ,updateToCart: function(id,product_id,quantity,cart_id){

        var params=$H(
            {
                action: 'update',
                product_id: product_id,  
                cart_id: cart_id,
                quantity:quantity,
                sid:Math.random()
            }
        ).toQueryString();

    
        $(id).ajaxUpdate(
            this.url,
            {
                method: 'get',
                parameters: params,
                onComplete: function(){
                        $$('.resizing_textareas').each(function(el){

                            new Texpand(el, {autoShrink: true}); 
                        });                      
                    resize();             
                }            
            }    
        );
    }
    ,refreshCart: function(id){

        var params=$H(
            {
                action: 'refresh',
                sid:Math.random()
            }
        ).toQueryString();

    
        $(id).ajaxUpdate(
            this.url,
            {
                method: 'get',
                parameters: params,
                onComplete: function(){
                        $$('.resizing_textareas').each(function(el){

                            new Texpand(el, {autoShrink: true}); 
                        });                      
                }
            }    
        );
    }
    ,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(SHOP_SHOPPINGCART_STORED);
                    
                        $$('.resizing_textareas').each(function(el){

                            new Texpand(el, {autoShrink: true}); 
                        });                      
                    
                }

            }    
        );
    }
    ,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(){
                    showCompleteCart();
                }

            }    
        );
    }
    ,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 form_elements = "";
            f = $('form_winkelmand_cadeau');
            if(f){
                form_elements = f.serialize();
            }     
            
            f2 = $('form_shoppingcart_items');              
            if(f2){
                if(form_elements.length > 0)
                    form_elements += "&"; 
                    
                form_elements += f2.serialize();
            }
        
            var params=$H(
                {
                    action: action,
                    step: step,
                    substep: substep,
                    sid:Math.random()
                }
            ).toQueryString();

            if(form_elements){
                params = form_elements + "&" + params;
            }            
            
            
            $('popup_module-resultaat').ajaxUpdate(
                this.url,
                {
                   method: 'POST',
                   parameters: params,
                   onComplete: function(){

                        if($('form_checkout')){
                            valid = new Validation('form_checkout', {immediate : true, onFormValidate : formCallback,useTitles:true});          
                        }
                        else{ 
                            if($('form_user') && $('user_choice_subscribe').visible()){
                                valid = new Validation('form_user', {immediate : true, onFormValidate : formCallback,useTitles:true});          
                                Validation.addAllThese([
                                        ['validate-url', 'Een correcte adres invullen a.u.b.', function (v) {
                                            return Validation.get('IsEmpty').test(v) || v == "http://" || /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i.test(v)
                                        }],            
                                        ['validate-postcode', 'Postcode is niet correct', function (v,elm) {

                                            return Validation.get('IsEmpty').test(v) ||  /^[\d]{4,4}\s{0,1}[a-zA-Z]{2,2}$/.test(v) 
                                        }]
                                    ]);                     
                            }
                            else if($('form_login') && $('user_choice_login').visible()){
                                valid = new Validation('form_login', {immediate : true, onFormValidate : formCallback,useTitles:true});          
                            }
                        }
                        
                        $$('.resizing_textareas').each(function(el){

                            new Texpand(el, {autoShrink: true}); 
                        });                          
                        
                        resize();  
                  }
                }    
            );  
    }    
    ,checkout_goto: function(step){
        this.goto_checkout(step,'','show');
    }
    ,checkout_confirm_form: function(form,nextstep){

        $(form).request({
            onComplete: function(req){ 
                    //if(dialogCounter == 1) 
                        //hideProcessing();  
                    
                    
                    $('popup_module-resultaat').innerHTML = req.responseText; 
                    
                    if($('form_checkout')){  
                        valid = new Validation('form_checkout', {immediate : true, onFormValidate : formCallback,useTitles:true});
                    }
                    else{
                        if($('form_user') && $('user_choice_subscribe').visible()){ 
                            valid = new Validation('form_user', {immediate : true, onFormValidate : formCallback,useTitles:true});   
                            Validation.addAllThese([
                                    ['validate-url', 'Een correcte adres invullen a.u.b.', function (v) {
                                        return Validation.get('IsEmpty').test(v) || v == "http://" || /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i.test(v)
                                    }],            
                                    ['validate-postcode', 'Postcode is niet correct', function (v,elm) {

                                        return Validation.get('IsEmpty').test(v) ||  /^[\d]{4,4}\s{0,1}[a-zA-Z]{2,2}$/.test(v) 
                                    }]
                                ]);                                
                        }
                        else if($('form_login') && $('user_choice_login').visible()){ 
                            valid = new Validation('form_login', {immediate : true, onFormValidate : formCallback,useTitles:true});          
                        }                    
                    }

                    
                    resize();                         
                    
            }
        }); 
        return false;
    }
    ,showCompleteCart: function(action){

        if(action && action=='payment'){
            action = action;
        }
        else
            action = 'show';  

        var params=$H(
        {
            action:action,
            sid:Math.random()
        }
        ).toQueryString();

        this.req_parameters = params;

        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: this.req_parameters, 
              onComplete: function(){ 
                        $$('.resizing_textareas').each(function(el){

                            new Texpand(el, {autoShrink: true}); 
                        });                         
              }
            }
          }
        });
    }    
    
});

var Payment = Class.create({   
    initialize: function() {   
    
    }
    ,checkAgree: function(){
        return $('agree2').checked;
        //return true;
    }    
    ,showPaymentDescription: function(){
        pchoice = $RF('pchoice');  
        if (pchoice != null){ 
           switch(pchoice){
                
                case 'Buckaroo_ideal': 
                    $('payment_method_description').innerHTML = $('form_description_buckaroo_ideal').innerHTML;
                    break;
                case 'Buckaroo_incasso':       
                    $('payment_method_description').innerHTML = $('form_description_buckaroo_incasso').innerHTML;                   
                    break;                    
                case 'Vooruitbetalen': 
                    $('payment_method_description').innerHTML = $('form_description_vooruitbetalen').innerHTML;   
                    break;
                case 'OpRekening': 
                    $('payment_method_description').innerHTML = $('form_description_oprekening').innerHTML;       
                    break;                    
                case 'PayPal': 
                    $('payment_method_description').innerHTML = $('form_description_paypal').innerHTML;       
                    break;   
                default:
                    $('payment_method_description').innerHTML = $('form_description_'+pchoice).innerHTML;       
                    break; 
           }          
        }
    }
    ,pay: function(){
        if(this.checkAgree()){
            checkout_only = false;
            if($('form_payment_checkout_only')){
                checkout_only = true;
            }
                
                

            if(checkout_only){
                var pform = $('form_payment_checkout_only');    
                pform.submit();
            }
            else{
                pchoice = $RF('pchoice');
                
                if (pchoice == null){
                    alert(SHOP_PAYMENT_CHOSE_METHOD);
                    return;
                }

                switch(pchoice){
                    
                    case 'Buckaroo_ideal': 
                                   
                        var pform = $('form_payment_buckaroo_ideal');
                        
                        $('payment_method_description').innerHTML = $('form_description_buckaroo_ideal').innerHTML;
                        
                        /*if($F('BPE_Issuer') == "" ){
                            alert("Kies eerst de gewenste betaalmethode");
                            return;
                        }                             
                        
                        if($F('BPE_Issuer') == "X" ){
                            alert("U kunt uitsluitend met iDeal betalen\nals u bankiert bij één van de genoemde banken.\nSelecteer andere betaalmethode!");
                            //setRadio('pform','pchoice','incasso');
                            return;
                        } */
                        //self.name = "payopener"; 
                        //prevWin = open('','_pay','toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=yes, resizable=yes, width=800, height=600, top=100, left=150');
                        //if (prevWin && !prevWin.closed)
                            //prevWin.focus();

                        pform.submit();                                
                        //submitFormAfterValidate_project(pform.id,0); 
                    
                        return true;
                        break;

                    case 'Buckaroo_incasso': 
                        
                        var pform = $('form_payment_buckaroo_incasso');           
                        $('payment_method_description').innerHTML = $('form_description_buckaroo_incasso').innerHTML;
                        
                        //self.name = "payopener"; 
                        //prevWin = open('','_pay','toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=yes, resizable=yes, width=800, height=600, top=100, left=150');
                        //if (prevWin && !prevWin.closed)
                           // prevWin.focus();
                        
                        
                        pform.submit();                                
                        //submitFormAfterValidate_project(pform.id,0);
                        return true;                       
                        break;                    
                        
                    case 'Vooruitbetalen': 
                        var pform = $('form_payment_vooruitbetalen'); 
                        $('payment_method_description').innerHTML = $('form_description_vooruitbetalen').innerHTML;   
                        
                        pform.submit();

                        break;
                        
                    case 'OpRekening': 
                        var pform = $('form_payment_oprekening'); 
                        $('payment_method_description').innerHTML = $('form_description_oprekening').innerHTML;       
                        pform.submit();
                        
                        break;                    
                        
                    case 'PayPal': 
                        var pform = $('form_payment_paypal'); 
                        $('payment_method_description').innerHTML = $('form_description_paypal').innerHTML;       
                        pform.submit();
                        
                        // $('paysubscription').submit();
                       //submitFormAfterValidate_project($('paysubscription').id,0);
                        break;   
                    
                    default : 
                           if(pchoice.indexOf('MSP_') == 0){
                            var url = SHOP_BASE_REL+'front/server_msp.php';
                            var method = pchoice.substring(4);
                            var params=$H(
                                {
                                    action: 'start_request',
                                    method:  pchoice.substring(4),
                                    session: $F('MSP_'+method+'_session'),
                                    id: $F('MSP_'+method+'_order_id'), 
                                    step: $F('MSP_'+method+'_step'),
                                    sid:Math.random()
                                }
                            ).toQueryString();                            
                            
                            new Ajax.Request(url, {
                              method: 'post',
                              parameters:params,
                              onSuccess: function(transport) {
                                    
                                    var json = transport.responseText.evalJSON();

                                    if(json.success){
                                        window.location = json.url;
                                    }
                                    else{
                                        alert(SHOP_PAYMENT_PROVIDER_ERROR);
                                    }
                              }
                            });
                            
                            
                            
                                        
                        }
                        else{
                            alert(SHOP_PAYMENT_NOT_POSSIBLE);  
                        }
            
                }
            }
        }else{
            alert(SHOP_ACCEPT_GENERAL_CONDITIONS);
        }
        
    }
    ,showGeneralConditions: function(buttons){
    
         if($('general_conditions')){
            
            if($(buttons + '_confirm'))
                $(buttons + '_confirm').toggle();
            if($(buttons + '_previous'))
                $(buttons + '_previous').toggle();                
                
            $('payment_container').toggle();
            $('general_conditions').clonePosition($('lightview'));
            $('general_conditions').setStyle({'width':width-20+'px', 'height':height-100+'px'});
            $('general_conditions').toggle();
        }   
    
    }

});

var Login = Class.create({
    
    initialize: function() {   
    
    }
    ,password_forgotten: function(form_id,login_box_id,login_box_forgotten_id,name_id,name_forgotten_id){
      
        if($(login_box_id + '_confirm'))
                $(login_box_id + '_confirm').hide();
                    
        $(name_forgotten_id).value = $F(name_id);
        $(login_box_id).toggle(); 
        $(login_box_forgotten_id).toggle(); 
      }
      ,
      send_password: function(form_id,login_box_id,login_box_forgotten_id,name_id,name_forgotten_id){
            f_value = $F(name_forgotten_id);

            var url = SHOP_BASE_REL+'front/server_user.php';
            var params=$H(
                {
                    action: 'send',
                    id:f_value,
                    sid:Math.random()
                }
            ).toQueryString();

    
            $('passwordforgotten_response').ajaxUpdate(
                url,
                {
                  method: 'get',
                  parameters: params,
                  onComplete: function(){

                         
                         $(form_id).password.value = '';   
                         $(name_id).value = $F(name_forgotten_id);
                         this.password_forgotten(form_id,login_box_id,login_box_forgotten_id,name_id,name_forgotten_id);
                         if($(login_box_id + '_confirm'))
                            $(login_box_id + '_confirm').show();                 
                  }
                }    
            );
    }
    ,loginEnter: function(e){
        
      if(window.event && e.keyCode == 13){ // IE{
            valid.validate();
      }
      else if(e.which && e.which == 13){ // Netscape/Firefox/Opera{
            valid.validate();
      } 
    }  

});


Array.prototype.removeItems = function(itemsToRemove) {

    if (!/Array/.test(itemsToRemove.constructor)) {
        itemsToRemove = [ itemsToRemove ];
    }

    var j;
    for (var i = 0; i < itemsToRemove.length; i++) {
        j = 0;
        while (j < this.length) {
            if (this[j] == itemsToRemove[i]) {
                this.splice(j, 1);
            } else {
                j++;
            }
        }
    }
}

var ShopRequest = Class.create({
    
    initialize: function(url, options,filters) {     
        this.options = {
            action:'',
            column:'',
            direction:'',
            nav_page:'',
            nr_results:'',
            searchTerms:'',
            exact:''
        };
        Object.extend(this.options, options || { });    
    
        this.filters = {
           cat_id: new Array(),
           f_product_a1: new Array(),
           f_product_price_incl: new Array(),
           f_product_administration_discount: new Array()
        };
        Object.extend(this.filters, filters || { });    
        
    
    },
    addParameters: function(options){
        this.options.sid = null;
        Object.extend(this.options, options || { });     
    },
    removeParameters: function(toRemove){
            
            this.options.removeItems(toRemove);

    },
    setParameters: function(options){
        this.options = options;
    },
    setFilter:function(key,value){
        var length = this.filters[key].length;
        this.filters[key][length] = value;  
    },
    removeFilter:function(key,value){

        
        this.filters[key].removeItems(value);     
    },
    removeFilters:function(key){
        this.filters[key] = new Array();
    },
    removeAllFilters:function(){
        for(var filter in this.filters){ 
            this.filters[filter] = new Array();    
        }
    },
    getQueryString: function(){
        var params = $H(this.options).toQueryString();
        
        for(var filter in this.filters){ 

            for(i=0;i<this.filters[filter].length;i++){
                 
                 params = params + '&'+filter + '[]=' + encodeURIComponent(this.filters[filter][i]);
                
            }
        }
        
        return params;
        //var filters = $H(this.filters)
    }
});


var Shop = Class.create({   
    
    initialize: function(options) {     
        this.url = SHOP_BASE_REL+'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 = new ShopRequest(this.url,options);      
    }
    ,setParameters: function(params){
        this.request.setParameters(params);
    }    
    ,showCompleteCart: function(action){
        this.cart.showCompleteCart(action);
    }
    ,getCart: function(){    
        return this.cart;
    }
    ,getLogin: function(){    
        return this.login;
    }
    ,getPayment: function(){    
        return this.payment;
    }        
    ,showProductDetail: function(){                                         
        
        if($('main_content_detail')){ 
            $('main_content_detail').remove();
        }       
               
        if($('filters'))
            $('filters').hide();
         
        if($('module-resultaat.resultaat')){
            $('module-resultaat.resultaat').hide();
        }
        
        if($('module-resultaat.breadcrumb')){
            $('module-resultaat.breadcrumb').hide();
        }
            
        if($('module-resultaat.detail')){

            if($('module-resultaat')){
            
                if(this.product_detail_fullscreen){

                        $('module-resultaat.detail').absolutize(); 
                        $('module-resultaat.detail').style.zIndex = "4999";
                        $('module-resultaat.detail').style.background = "#000000 none repeat scroll 0 0";            
                        $('module-resultaat.detail').clonePosition($('document_body'));
                        
                        
                        $('module-resultaat.detail').height = document.viewport.getHeight() + "px";
                        
                }
                else{            
                    el = $('contentcontent'); 
		
                    if(Prototype.Browser.IE8 || Prototype.Browser.IE7){
         
                        $('module-resultaat.detail').style.zIndex = "4999";
                        $('module-resultaat.detail').style.background = "#ffffff";
						
						$('module-resultaat.detail').clonePosition(el,{setHeight:false});
						el.childElements().each(function(el){
				
							if(el.id != 'module-resultaat')
								el.hide();
						});
						                  
                    }
                    else{            

                   
                        $('module-resultaat.detail').style.zIndex = "4999";
                        $('module-resultaat.detail').style.background = "#ffffff";
                        
						
			
						$('module-resultaat.detail').clonePosition(el,{setHeight:false});
						el.childElements().each(function(el){
				
							if(el.id != 'module-resultaat')
								el.hide();
						});

                    }
                }
            }            
            $('module-resultaat.detail').show();
            scroll(0,0); 
        }
    }
    ,showProductSearch: function(){


       if($('main_content_detail')){ 
            $('main_content_detail').remove();
       }        
    
        if($('product-details') ){
            if($('product-details') != "undefined" )
                $('product-details').hide();                                                     
        }

        if($('module-resultaat.breadcrumb')){
            $('module-resultaat.breadcrumb').show();    
        }

        if($('filters') ){ 
            $('filters').show();                                                    
        }    

        if($('module-resultaat.detail'))
            $('module-resultaat.detail').hide();                                                                                                    

        if($('module-resultaat.resultaat'))
            $('module-resultaat.resultaat').show();


		el = $('contentcontent');
		if (el) {
			el.childElements().each(function(el){
				el.show();
			});
		}

        scroll(0,0);  

    }
    ,closeProductDetail: function(){
        this.showProductSearch();   
    }    
    ,updateCategory: function(catid){
        
        
        if($('module-resultaat.resultaat')){ 
           
            if(catid == "")
                catid = 0;
        
            if($('module-resultaat.breadcrumb')){
                $('module-resultaat.breadcrumb').show();  
                this.updateCategoryForElement(catid,'breadcrumb','module-resultaat.breadcrumb');   
            }
                 
            if($('module-resultaat.resultaat')){                                     
                this.updateCategoryForElement(catid,'gallery','module-resultaat.resultaat');   
            }
            
            if($('product_bestsellers')){
                this.updateBestsellers(catid);
            }
                    
            this.closeProductDetail();   
        
            return false;              

        }
        return true;         
    }
    ,updateBestsellers: function(catid){
    
        var params=$H(
            {
                action:'update',
                catid : catid,       
                sid:Math.random()
            }
        ).toQueryString();    
      
        $('product_bestsellers_list').ajaxUpdate(
            SHOP_BASE_REL+'front/server_bestellers.php',
            {
                method: 'get',
                parameters: params,
                onComplete:function(transport){
                     if($('product_bestsellers_list')){   
                     
                        if(transport.responseText.length > 0){
                            $('product_bestsellers').show();
                        }
                        else{
                            $('product_bestsellers').hide();  
                        }
                     }
                }
            }    
        );        
    
    }
    ,changeTab: function(artikelCode,tab,tab_selected){
  
    }
    ,changeViewType: function(){

        this.showProductSearch();


        params = {
                action:'typeSelection',
                type: $F('weergave'),
                sid:Math.random(),
                nav_page:1
                 };           

        this.request.addParameters(
            params
        );

        $('module-resultaat.resultaat').ajaxUpdate(
            this.url,
            {
              method: 'get',
              parameters: this.request.getQueryString(),
              onComplete: function(){
                Lightview.updateViews();  
              }
            }    
        ); 
    }
    ,changeSort: function(column,direction){
    
        this.showProductSearch();
    
        if(!column){
            pair = $F('sortering').split("|");
            column = pair[0];
            direction = pair[1];
        }

        params = {
                action:'sort',
                column:column,
                direction:direction,
                sid:Math.random(),
                sort:''
        };          
        
        this.request.addParameters(
            params
        );

        $('module-resultaat.resultaat').ajaxUpdate(
            this.url,
            {
              method: 'get',
              parameters: this.request.getQueryString(),
              onComplete: function(){
                Lightview.updateViews();  
              }
            }    
        ); 
    }
    ,subproductDetails: function(artikelCode,parentArtikelCode){

        this.productDetail(artikelCode,parentArtikelCode,1,0);  
    }
    ,returnToParentProductDetails: function(artikelCode){
        this.productDetail(artikelCode,'',0,1);
    }
    ,productDetails: function(artikelCode){
    
        return this.productDetail(artikelCode,'',0,0);
    }
    ,productDetail: function(artikelCode,parentArtikelCode,sub,backfromsub){
    
        if($('module-resultaat.detail')){   
            this.showProductDetail();    
            
            var params=$H(
                {
                    action:'details',
                    artikelCode : artikelCode,
                    parentArtikelCode : parentArtikelCode, 
                    sub: sub,
                    backfromsub: backfromsub,
                    sid:Math.random()                                      
                }
            ).toQueryString();
        
            $('module-resultaat.detail').ajaxUpdate(
                this.url,
                {
                    method: 'get',
                    parameters: params,
                    onComplete:function(req){ 
                        Lightview.updateViews();   
                        //getProductPrice(req.request.parameters.artikelCode);
                    }
                }    
            );     
        
        return false;    
        } 

        return true;
    }   
    ,searchProducts: function(clean,general){

        this.showProductSearch();
        
        if(general){
            searchTerms = $F('general_searchterms');
            if($('general_exact'))
                exact = $F('general_exact');  
            else
                exact = 0;        
        }
        else{
            searchTerms = $F('searchterms');
            if($('exact'))
                exact = $F('exact');  
            else
                exact = 0;
        }

        if(clean){
            this.request.setParameters(
                {
                    action:'search',
                    searchTerms : searchTerms,
                    exact: exact, 
                    sid:Math.random()                                      
                }            
            );
            

        }
        else{
            this.request.setParameters(
                {
                    action:'search',
                    searchTerms : searchTerms,
                    exact: exact, 
                    sid:Math.random()                                      
                }            
            );
        }
        
        this.request.removeAllFilters();        
        
        
        $('module-resultaat.resultaat').ajaxUpdate(
            this.url,
            {
                method: 'get',
                parameters:  this.request.getQueryString(),
                onComplete:function(){ 
                    Lightview.updateViews();   
                
                    if($('module-resultaat')){
                        $('module-resultaat').removeClassName('module-resultaat');
                        $('module-resultaat').addClassName('module-resultaat-full');
                    }

                    if($('menu_titel')) 
                        $('menu_titel').addClassName('menu_titel_collapse');  
                    
                    
                    if($('module-info'))    
                        $('module-info').style.width =  '22px';
                    
                    if($('module-infoContents'))
                        $('module-infoContents').hide();   
                    
                    if($('module-resultaat.breadcrumb'))
                        $('module-resultaat.breadcrumb').hide();
                    
                    isExtended = 0; 
                    if($('filters')){   
                        this.updateFilterList();
                    }                    
                    
                }
            }    
        );       
    }

    ,updateProductListPage: function(page){

        this.showProductSearch();

        params = {
                    action:'navigate',
                    nav_page:page,  
                    sid:Math.random()
                 };           

        this.request.addParameters(
            params
        );

        $('module-resultaat.resultaat').ajaxUpdate(
            this.url,
            {
              method: 'get',
              parameters: this.request.getQueryString(),
              onComplete: function(){
                Lightview.updateViews();  
              }
            }    
        );      
    }
    ,updateCategoryForElement: function(catid,action,elementId){


        if(action == 'gallery'){

            params = {
                        action:action,
                        catid:catid,
                        nav_page: 1,    
                        sid:Math.random(),
                        column:'',
                        direction:'',
						searchTerms:''
                     };           
        
            this.request.addParameters(
                params
            );        
        
    

            $(elementId).ajaxUpdate(
                this.url,
                {
                  method: 'get',
                  parameters: this.request.getQueryString(),  
                  onComplete: function(){

                        Lightview.updateViews();   
                  }
                }    
            );
        }
        else{
            params = {
                        action:action,
                        catid:catid,
                        nav_page: 1,    
                        sid:Math.random(),
                        column:'',
                        direction:''
                     };           
        
            this.request.addParameters(
                params
            );        
        
    

            $(elementId).ajaxUpdate(
                this.url,
                {
                  method: 'get',
                  parameters: this.request.getQueryString(),  
                  onComplete: function(){

                        Lightview.updateViews();   
                  }
                }    
            );        
        
        }
    }
    ,switchPersonal: function(){


        if($('user_choice_subscribe').visible()){
        
            $('user_choice_login').show();
            $('user_choice_subscribe').hide();
            $('tabs_tab_subscribe').removeClassName('active');
            $('tabs_tab_login').addClassName('active');        
            
            valid = new Validation('form_login', {immediate : true, onFormValidate : formCallback,useTitles:true});          
        }
        else if($('user_choice_login').visible()){
        
            $('user_choice_subscribe').show();
            $('user_choice_login').hide();
            $('tabs_tab_subscribe').addClassName('active');
            $('tabs_tab_login').removeClassName('active');
            
            valid = new Validation('form_user', {immediate : true, onFormValidate : formCallback,useTitles:true});          
                Validation.addAllThese([
                        ['validate-url', 'Een correcte adres invullen a.u.b.', function (v) {
                            return Validation.get('IsEmpty').test(v) || v == "http://" || /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i.test(v)
                        }],            
                        ['validate-postcode', 'Postcode is niet correct', function (v,elm) {

                            return Validation.get('IsEmpty').test(v) ||  /^[\d]{4,4}\s{0,1}[a-zA-Z]{2,2}$/.test(v) 
                        }]
                    ]);                             
        }
    }
    ,checkout_login: function(button){
        valid = new Validation('form_login', {immediate : true, onFormValidate : formCallback,useTitles:true});          
        
        button.disabled = true; 
        if(!valid.validate()){
            button.disabled = false;
        }
        return false;        
        
    }
    ,checkout_subscribe: function(button){
        valid = new Validation('form_user', {immediate : true, onFormValidate : formCallback,useTitles:true});          
            Validation.addAllThese([
                    ['validate-url', 'Een correcte adres invullen a.u.b.', function (v) {
                        return Validation.get('IsEmpty').test(v) || v == "http://" || /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i.test(v)
                    }],            
                    ['validate-postcode', 'Postcode is niet correct', function (v,elm) {

                        return Validation.get('IsEmpty').test(v) ||  /^[\d]{4,4}\s{0,1}[a-zA-Z]{2,2}$/.test(v) 
                    }]
                ]);                                 
        button.disabled = true; 
        if(!valid.validate()){
            button.disabled = false;
        }
        return false;                        
    }
    ,removeFilter: function(key,value){

        this.showProductSearch();
        
        if(value == ''){
            $$("#"+key+" a.filter_active").each(function(el){
                el.removeClassName('filter_active')
            });
        }
        else{
            $(key+'_'+value).removeClassName('filter_active');
        }

        params = {
                    action:'removefilter', 
                    nav_page: 1,    
                    sid:Math.random(),
                    column:'',
                    direction:''
                 };           
        
        if(value == ''){           
            this.request.removeFilters(key);
        }
        else{
            this.request.removeFilter(key,value);
        }

        this.request.addParameters(
            params
        );        

        $('module-resultaat.resultaat').ajaxUpdate(
            this.url,
            {
              method: 'get',
              parameters: this.request.getQueryString(),
              onComplete: function(){
                Lightview.updateViews();  
              }
            }    
        );    
           
    }    
    ,addFilter: function(key,value){
   
       this.showProductSearch();
        
        var id= key +  '_' + value;
        
        
        
        if($(id).hasClassName('filter_active')){
           this.removeFilter(key,value);
        }
        else{
            $(id).addClassName('filter_active');  
            params = {
                        action:'addfilter',
                        nav_page: 1,    
                        sid:Math.random(),
                        column:'',
                        direction:''
                     };           
                     
            this.request.setFilter(key,value);
        
            
            this.request.addParameters(
                params
            );

            $('module-resultaat.resultaat').ajaxUpdate(
                this.url,
                {
                  method: 'get',
                  parameters: this.request.getQueryString(),
                  onComplete: function(){
                    Lightview.updateViews(); 

                     
                  }
                }    
            );    
        }
   }
   ,showLargeImage: function(source_el){

       var el = $('img_groot_replace');
       $('img_groot').hide();
       
       if(el){
            el.src = (source_el.src.replace(/.thumb.png/, ".gallery.png"));
            el.show();
            
       }
        
   }
   ,hideLargeImage: function(){
       $('img_groot').show();
       if($('img_groot_replace')){
            $('img_groot_replace').hide();
       }
   }    
});

var Dealer = Class.create({  
    initialize: function() {   
    
    }    
    ,openinfowindow:function(id){

        d_info = $$('div.dealer_info');
        
        d_info.each(function(el){
                el.removeClassName('active_dealer');
                el.hide();
        });
        
       // d_open_info = $$('span.open_dealer_info');
       // d_open_info.each(function(el){
         //       el.show();
        //});        

      // $('open_dealer_'+id).hide();
       $('dealer_'+id).show();
       $('dealer_'+id).addClassName('active_dealer');

    }
    ,opensidebar:function(id){

        this.openinfowindow(id);
        click_sidebar(id);
    }
    
});

var PPage = Class.create({
    
    initialize: function() {   
        this.url = SHOP_BASE_REL+'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){
            
            if($('user_delivery_name')){
                if($F('user_infix'))
                    $('user_delivery_name').value = $F('user_firstname') + ' ' + $F('user_infix') + ' ' + $F('user_lastname');
                else
                    $('user_delivery_name').value = $F('user_firstname') + ' ' + $F('user_lastname');            
            }
            
            $('user_delivery_address').value = $F('user_address'); 
            
            if($('user_housenumber'))
                $('user_delivery_housenumber').value = $F('user_housenumber');   
            $('user_delivery_zipcode').value = $F('user_zipcode');    
            $('user_delivery_city').value = $F('user_city');      
        }
    }            
    ,submitResultaat: function(form,action){

        $(form).request({
            onComplete: function(req){ 
                    //if(dialogCounter == 1) 
                        //hideProcessing(); 
                    if (req.responseText == 'refresh') {
                        window.location.replace("/index.php?page=7");
                    }
                    else {
                                            
                        $('module-resultaat.resultaat').innerHTML = req.responseText; 
                    }
                   
                    if($('form_ppage_user')){
                        valid = new Validation('form_ppage_user', {immediate : true, onFormValidate : formCallback,useTitles:true});
                    } 
            }
        });    
    
        return false;
    }     
    ,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_ppage_user')){
                            valid = new Validation('form_ppage_user', {immediate : true, onFormValidate : formCallback,useTitles:true});
                        } 
                                               
                  }
                  
                }    
            );  
        }
    }
    ,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,
              onComplete: function(){
                    
                        if($('form_ppage_user')){
                            valid = new Validation('form_ppage_user', {immediate : true, onFormValidate : formCallback,useTitles:true});
                        }                      
              }
              
            }    
        );  
    }
    ,updateResultaat: function(action){

        if(typeof $('module-resultaat.resultaat') == "undefined" ){
            return true;
        }
        else{

            if($('locatie-informatie'))
                $('locatie-informatie').innerHTML = '<a href=\'index.php?page=7\'>Mijn Tuincentrum</a><span id="module-resultaat.breadcrumb" style="margin: 0px; padding: 0px; float: left;"></span>'; 
            
            if($('module-resultaat.breadcrumb'))
                $('module-resultaat.breadcrumb').hide();

            var params=$H(
            {
                action:action,
                sid:Math.random() 
                
            }
            ).toQueryString();

            $('module-resultaat.resultaat').ajaxUpdate(
                this.url,
                {
                  method: 'get',
                  parameters: params,
                  onComplete: function(){
                        if($('form_ppage_user')){
                            valid = new Validation('form_ppage_user', {immediate : true, onFormValidate : formCallback,useTitles:true});
                        }                                    
                        
                        if($('module-resultaat') && $('module-resultaat.resultaat') ){   
                             el = $('module-resultaat');   

                         
                        }                        
                        
                  }
                }    
            );  
            return false;
       }
    }
});


var shop = new Shop(); 
var ppage = new PPage();


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 resize(){
    
    if($('lightview')){                     
    
       // $('lightview').style.marginTop = '0px';
        //$('lightview').scrollTop = '0px';
    
        height = $('lightview').getHeight(); 
        width  = $('lightview').getWidth(); 
        $$('.lv_contentTop')[0].setStyle({'width':width+'px', 'height':height+'px'});
        
        
        //resize winkelmandje
        if($('winkelmandje-overzicht')){
            if(Prototype.Browser.IE)    
                $('winkelmandje-overzicht').setStyle({'width':width-18+'px','height':(height*0.45)-5+'px'});          
            else
                $('winkelmandje-overzicht').setStyle({'width':width-25+'px','height':(height*0.45)-5+'px'});          
        }     
        
        //resize subscription form
        
        if($('checkout-subscribe.content')){   
            subscribe_height = ((height*0.8)-5);
            if(Prototype.Browser.IE)    
                    $('checkout-subscribe.content').setStyle({'overflow':'hidden','width':width-15+'px', 'height':subscribe_height+'px'});  
            else
                    $('checkout-subscribe.content').setStyle({'overflow':'hidden','width':width-15+'px', 'height':subscribe_height+'px'});  
        }
        
        if($('subscription_wrapper')){
           
           if($('checkout-subscribe.content')){
                
                if($('button_checkout_subscribe')){       
                    subscribe_height = ($('checkout-subscribe.content').getHeight() - 20); 
                }
                else{
                    subscribe_height = $('checkout-subscribe.content').getHeight(); 
                }
                    
                if($('user_choice_login'))
                    subscribe_width  = ($('checkout-subscribe.content').getWidth()*0.60);            
                else    
                    subscribe_width  = ($('checkout-subscribe.content').getWidth()*0.98);            
                if(Prototype.Browser.IE){
                    $('subscription_wrapper').setStyle({'width':subscribe_width+'px','height':(subscribe_height-10)+'px'});          
                    $('subscription_content').setStyle({'width':$('subscription_wrapper').style.width+'px','height':$('subscription_wrapper').style.height+'px'});          
                }
                else{
                    $('subscription_wrapper').setStyle({'width':subscribe_width+'px','height':(subscribe_height-10)+'px'});                    
                    $('subscription_content').setStyle({'width':$('subscription_wrapper').style.width+'px','height':($('subscription_wrapper').style.height-15)+'px'});          
                }           
           }
           else{
            if(Prototype.Browser.IE){
                $('subscription_wrapper').setStyle({'width':width-18+'px','height':(height*0.80)-5+'px'});          
                $('subscription_content').setStyle({'width':$('subscription_wrapper').style.width+'px','height':$('subscription_wrapper').style.height+'px'});          
            }
            else{
                $('subscription_wrapper').setStyle({'width':width-25+'px','height':(height*0.80)-5+'px'});                  
                $('subscription_content').setStyle({'width':$('subscription_wrapper').style.width+'px','height':($('subscription_wrapper').style.height-15)+'px'});          
            }
            
           }
        }   
    }

}


function trigger(name){

    var element=$('subcat_'+name);
    var hrefElement = $('href_'+name);

    if(trim(hrefElement.innerHTML) == "+"){
        hrefElement.innerHTML = "-";
        element.style.display='block';
    }
    else{
        hrefElement.innerHTML = "+";
        element.style.display='none';
    }
}


function submitForm(form,action){


    //dialogCounter = 0;
    //showProcessing();

        
    if(form ==  'form_checkout' || form ==  'form_login' || form ==  'form_user')
        shop.getCart().checkout_confirm_form(form,action);
    else 
        ppage.submitResultaat(form,action);
}

function formCallback(result, form) 
{   form_status =  result;
   /* include the ajax httprequest here */
   
  
   
    if (form_status){
        //submitFormAfterValidate_project(form.id,0);

        var id = '';
        if(Prototype.Browser.IE7 || Prototype.Browser.IE6)
            id = form.getAttribute('name');      
        else
            id = form.identify();        
        
        if(id ==  'form_checkout' || id ==  'form_login' || id ==  'form_user'){
            submitForm(id,$F('step'));
        }
        else{
            submitForm(id,$F('action'));
        }
    }
}   

document.observe('lightview:opened', function(event) {
    if(!event.target.href){  
        resize();
        Event.observe(window, "resize", function() {   
            resize(); 
        });  
    }
});

document.observe('lightview:hidden', function(event) {
  if(!event.target.href){
  
    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); 