﻿
    function notifyStock(id){
        iBox.load();
        var status='';
        var statusback="<br><div style=\"text-align:center;font-size:14px;font-weight:bold;\"><a href=\"javascript:iBox.close()\">Continue Shopping</a></div>";
        var row=$('tr'+id);
        var email=row.getElement('input[id$=txtEmail]').get('value');
        if(email!=""){
        
             var jsonReq = new Request.JSON({'url':'beads.aspx/notifyStock',
                    onComplete: function(result){
                
                       if(result.charAt(0)=='A'){
                      
                             status=genStatus('pass','Email added to stock notification list',result);
                        }
                        else{
                             status=genStatus('fail','Unable to add email to stock notification list',result);
                        }
                           iBox.open(status+statusback);
                       
                    },
                    onFailure: function(result){
                        
                            status=genStatus("fail","Unable to add email to notify list","Please contact shop@beadcharm.com for assistance.");
                           iBox.open(status+statusback);
  
                    }
                    
                }
                ).send({'id':id,'email':email});
       }
       else{
            status=genStatus("fail","Unable to add email to notify list","Email field empty. Please enter your email address.");
             iBox.open(status+statusback);
       }
     }
     
    
function addProduct(id){
        var error='';
        var productids='';
        var packs='';
        
       var ids=id.split('|').each(function(item){
            var row=$('tr'+item);
            
           // if($('iPacks'+item).get('value').toInt()<=$('sStock'+item).get('text').toInt()){
                    productids+=item+'|';
                    
                    packs+=row.getElement('input[id$=txtPacks]').get('value')+'|';
             // }
            //  else{
            //  
           //   }
       });
       if(productids.length>0){
            productids=productids.chop(1);
            
            packs=packs.chop(1);
              iBox.load();
              var resultback="<br><div style=\"text-align:center;font-size:14px;font-weight:bold;\"><a href=\"javascript:iBox.close()\">Continue Shopping</a> | <a href=\"basket.aspx\">View Basket</a> | <a href=\"checkout.aspx\">Check Out</a></div>";
            var jsonReq = new Request.JSON({'url':'beads.aspx//addProducts',
                    onComplete: function(result){
                       iBox.open(result+resultback);
                    },
                    onFailure: function(result){
                       iBox.open(genStatus("fail","Unable to add items to your shopping cart","Please contact shop@beadcharm.com for assistance.")+resultback);
                    }
                    
                }
                ).send({'id':productids,'quantity':packs});
        }
    }
    
    function addAll(){ 
       
        var items='';
        var quantity='';
        $$('tr[id^=tr]').each(function(item){
            var packs=item.getElement('input[id$=txtPacks]').get('value');
            if(packs!='' && packs!='0'){
                items+=item.id.replace('tr','')+'|';
            }
        });
        if(items.length>0){
            addProduct(items.chop(1));
        }
    }
    
    function registerEvents(){
                    
            $$('a[id^=aProd]').each(
                    function(item,index){
               
                         item.addEvents({
                            'mouseover':function(e){
                            var event = new Event(e);
                            var iPreview=$('iPreview');
                            iPreview.setProperty('src','img/products/images/'+item.id.replace('aProd','')+'.jpg');  
                            iPreview.set('styles',{'display':'','left':(item.getPosition().x+245)+'px','top':item.getPosition().y+'px'});
                            
                         },
                         'mouseout':function(e){
                            $('iPreview').set('styles',{'display':'none'});
                         }
                         });
                    });
    }

window.addEvent('domready',registerEvents);
