Subversion Repositories SmartDukaan

Rev

Rev 2038 | Rev 2652 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/* 
     Author: Binay Kumar
     Created on: 30.09.2010
     
     Example:
     addResearch(refdivid,reqtype) refdivid=pane1, reqtype=single/multi
     Multiple request: onclick="javascript:addResearch('pane2','multi');"
     Single request: onclick="javascript:addResearch('','single');"
     
     addToCart(refdivid,reqtype) refdivid=pane1, reqtype=single/multi
     Multiple request: onclick="javascript:addToCart('pane2','multi');"
     Single request: onclick="javascript:addToCart('','single');"
     
     NOTE: incase reqtype=list then refdivid should be product id
*/

function trackEventWithGA(eventCategory, actionName, label)     {
        if(typeof _gaq != "undefined" && _gaq != null)  {
                _gaq.push(['_trackEvent', eventCategory, actionName, label, 2]);
        }
}

function trackPageWithGA(page)  {
        if(typeof _gaq != "undefined" && _gaq != null)  {
                _gaq.push(['_trackPageview', page]);
        }
}
        
$(function() {
        $("a.addtocart").live('click', function() {
                var catalogid = $(this).attr('catalogid');

                var radBtn = "";
                var param = "PARAMETERS_" + catalogid;
        
                $.each(eval(param), function(val, text) {
                        radBtn += '<label><input type="radio" name="radColorSel" value="' + val + '" /> <span>' + text + '</span></label>';
                });
                
                radBtn += '<div class="clearBoth"></div>';
                $(".colorSelLightBoxColor .content").html(radBtn);

                $.fn.colorbox({inline:true, href:".colorSelLightBoxColor", open:true, onComplete:function(){
                        var posLeft = (document.documentElement.clientWidth / 2) - (parseInt($('#colorbox').css('width')) / 2);
                        var posTop = (document.documentElement.clientHeight / 2) - (parseInt($('#colorbox').css('height')) / 2);

                        $('#colorbox').css('top', posTop);
                        $('#colorbox').css('left', posLeft);
                }});
                
                /*
                var select = $('#colorselector');
                var options = select.attr('options');
                $('option', select).remove();
                $.each(eval(param), function(val, text) {
                    options[options.length] = new Option(text, val);
                    //alert(val + ": " + text);
                });
                $( "#dialog-form" ).dialog( "open" );
                */
        });
        
        /*
        $( "#dialog-form" ).dialog({
                autoOpen: false,
                height: 200,
                width: 250,
                modal: true,
                buttons: {
                        Ok: function() {
                                var bValid = true;
                                var selected = $("#colorselector option:selected");
                                var itemid = selected.val();
                                addToCart(itemid,'list');
                                $( this ).dialog( "close" );
                                },
                        Cancel: function() {
                                $( this ).dialog( "close" );
                                }
                        },
                        close: function() {
                                
                        }
                });
        */
});

function selectColor(){
        var bValid = true;
        var selected = $(".colorSelLightBoxColor .content input[name='radColorSel']:checked");
        var itemid = selected.val();

        trackEventWithGA('Order', 'Select Product Color', itemid);
        
        if(itemid == undefined) {
                alert('Please select a color.');
        } else  {
                addToCart(itemid,'list');
                $.colorbox.close();
        }
}

function onSelectChange(itemid){
        //alert("selected color is"+itemid);
}


function addResearch(refdivid, reqtype){
     var research_tot = $("#research_total").val();
     var seldata = [];
     var tot = 0;
     
     if(reqtype == "multi")     {
          var containerdiv = "#" + refdivid;     
          var par = containerdiv + " input[type=checkbox]:checked";
          $(par).each(function(){
               seldata.unshift($(this).val());
               tot ++;
          });
     }else if(reqtype == "single")      {
          seldata.unshift($("#product_id").val());
          var tot = 1;
     }else if(reqtype == "list"){
          seldata.unshift(refdivid);
          var tot = 1;
     }

     if(tot > 0)        {
          //alert("AJAX request to add products in My research. Product ID: "+seldata+" and total: "+tot);
          jQuery.ajax({
               type: "POST",
                      url: "/addtoresearch",
                      data: "productid=[" + seldata +  "]",
                      success: function(msg){
                           
                           if(msg == 0){
                                alert( "Product is already in my research" );
                           }else {
                                var t = (research_tot * 1) + (tot * 1);
                                $("#research_total").val(t);
                                if(t > 0){
                                     $("#research_default").css("display","none");
                                }     
                                $("#pane1").prepend(msg);
                                // $('#pane1').jScrollPane({showArrows:true, scrollbarWidth: 15, arrowSize: 16});
                                
                                if(reqtype == "multi"){
                                     jQuery.each(seldata,function(intIndex, objValue){
                                         trackEventWithGA('Research', 'Add to Research', objValue);
                                          add_to_storage_set("resitems", objValue);
                                          var tblid="#pane1 #"+objValue+ " td";
                                          $(tblid + " div a").truncate({addtitle: true});
                                          $(tblid + " div div.price").truncate({addtitle: true});
                                          $(tblid + " div div.text").truncate({addtitle: true});
                                          $(tblid).animate({ backgroundColor: "#fcffb3" }, 'slow');
                                          $(tblid).animate({ backgroundColor: "#F5F5F5" }, 'slow');
                                     });
                                     
                                }else if(reqtype == "single"){
                                         add_to_storage_set("resitems", seldata);
                                     trackEventWithGA('Research', 'Add to Research', seldata + "");
                                     var tblid="#pane1 #"+seldata+ " td";
                                     $(tblid + " div a").truncate({addtitle: true});
                                     $(tblid + " div div.price").truncate({addtitle: true});
                                     $(tblid + " div div.text").truncate({addtitle: true});
                                     $(tblid).animate({ backgroundColor: "#fcffb3" }, 'slow');
                                     $(tblid).animate({ backgroundColor: "#F5F5F5" }, 'slow');  
                                }else if(reqtype == "list"){

                                    trackEventWithGA('Research', 'Add to Research', seldata + "");
                                    
                                         add_to_storage_set("resitems", seldata);
                                     var tblid="#pane1 #"+seldata+ " td";
                                     $(tblid + " div a").truncate({addtitle: true});
                                     $(tblid + " div div.price").truncate({addtitle: true});
                                     $(tblid + " div div.text").truncate({addtitle: true});
                                     $(tblid).animate({ backgroundColor: "#fcffb3" }, 'slow');
                                     $(tblid).animate({ backgroundColor: "#F5F5F5" }, 'slow');     
                                }    
                           }
                           //alert( "Data Saved: " + msg );
                      }
          });
          
     }else{
          alert("Please select atleast one product");
     }
}


function addToCart(refdivid,reqtype){
     var seldata="";
     var tot=0;
     var saprt="";
     if(reqtype == "multi"){
          var containerdiv = "#" + refdivid;
          var par= containerdiv + " input[type=checkbox]:checked";
          var isMulti = false;
          $(par).each(function(){
                  
                   var totalcolors = $(this).attr('totalcolors');
                   //alert(totalcolors);
                   saprt = (tot>0)? "_":"";
               seldata += saprt+$(this).attr('itemid');

                   if(totalcolors != 1) {
                           isMulti = true;
                   }
               tot++;
          });
          //alert("ismulti" + isMulti + " seldata " + seldata + "   refdiv is "+ refdivid + "  par "+ par);
          if(isMulti == true){
                  alert("Some items are available in multiple colors. Please go to product page and select color you want.");
                  return;
          } else        {
                  var items = seldata.split('_');
                  for(var i in items)   {
                          trackEventWithGA('Order', 'Add to Cart', items[i]);
                  }
          }
     }else if(reqtype == "single"){
          seldata = $("#item_id").val();
                  trackEventWithGA('Order', 'Add to Cart', seldata + '');
          tot=1;
     }else if(reqtype == "list"){
          var seldata=refdivid;
                  trackEventWithGA('Order', 'Add to Cart', seldata + '');
          var tot=1;
     }
     /*
     var historyitems = "";
     if($.Storage.get("historyitems")){
         historyitems = "&historyitems=" +  $.Storage.get("historyitems");
         $.Storage.remove("historyitems");
     }
     +historyitems
     */

     if(tot > 0)        {
         jQuery.ajax({
                 type: "POST",
                 url: "/addtocart",
                 data: "productid=" + seldata,
                 success: function(msg){
                         if(msg==""){
                                 window.location = "/cart";
                         }else{
                                 alert(msg);
                         }
                 }
         });
     } else     {
         alert("Please select atleast one product");
     }
}

function selectProduct(itemId){
        //alert("Selected item is "+ itemId);
}

function scrollWinToTop(){
    $('html, body').animate({
      scrollTop: $("body").offset().top
    }, 0);
}

function resetShipForm(){
  $('#shipName').val("");
  $('#shipAddress1').val("");
  $('#shipAddress2').val("");
  $('#shipState').val("");
  $('#shipCity').val("");
  $('#shipPin').val("");
  $('#shipPhone').val("");
        $('#newShipAdr').hide();
        scrollWinToTop();
}

function changeShipAddress(id){
  addressId = id.substring(20);
 //alert("TODO AJAX request to change shipping address. " + addressId);
   jQuery.ajax({
      type: "POST",
      url: "/shipping",
      data: "action=change&addressid="+addressId,
      success: function(msg){
         //alert( "Data Saved: " + msg );
         $("#addressid").val(msg);
      }
   });
  //$("#addressid").val(addressId);
  document.getElementById('shipAddressContainer').innerHTML = document.getElementById(id).innerHTML;
        $(".proceed-bttn .yellow-image").removeClass('imgDisableButton').addClass('imgEnableButton');
        $(".proceed-bttn input[disabled='']").removeAttr('disabled');
        
  //document.getElementById('newShipAdr').style.display = 'none';
  $('#newShipAdr').hide();
        scrollWinToTop();

}

function merge_history_items(){
        params = $.Storage.get("histitems");
        
        jQuery.ajax({
                type: "POST",
                url: "/browse-history",
                data: "id="+params,
                cache: false,
                success: function(json) {
                    $.Storage.set("histitems", json);
                    load_history_widget();
            }
        });
}

function load_research_items(){
        jQuery.ajax({
                type: "GET",
                url: "/myresearch",
                cache: false,
                success: function(json) {
                    $.Storage.set("resitems", json);
                    load_research_widget();
            }
        });
}

function load_research_widget(){
        var params = "";
        params = "/" + $.Storage.get("resitems");
        
        jQuery.ajax({
                type : "GET",
                url : "/myresearch" + params,
                cache: false,
                success : function(html) {
                    $("#myresearch").html(html);

                    // Product Title
                    $("#myresearch table td div a").each(function() {
                                        $(this).truncate({addtitle : true});
                    });

                    // Product Price
                    $("#myresearch table td div div.price").each(function() {
                                        $(this).truncate({addtitle : true});
                        });

                    // Product Details
                    $("#myresearch table td div div.text").each(function() {
                                        $(this).truncate( {addtitle : true});
                        });
            }
        });
}

function load_history_widget() {
        var params = "";
        params = "/" + $.Storage.get("histitems");
        
        jQuery.ajax({
                type: "GET",
                url: "/browse-history" + params,
                cache: false,
                success: function(html){
                    $("#browsehistory").html(html);

                    // Product Title
                    $("#browsehistory table td div a").each(function(){
                            $(this).truncate({addtitle: true}); 
                    });

                // Product Price
                $("#browsehistory table td div div.price").each(function(){
                        $(this).truncate({addtitle: true});
                    });

                    // Product Details
                    $("#browsehistory table td div div.text").each(function(){
                            $(this).truncate({addtitle: true});
                    });
            }
        });
}

function add_to_storage_set(key, id) {
        var set = eval($.Storage.get(key));
        if (set) {
                $.each(set, function(index, value) {
                        if (value == id) {
                                set.splice(index, 1);
                        }
                });
        }
        set.unshift(id);
        if (set.length>10)
        {
                set.splice(10, set.length-10);
        }
        $.Storage.set(key, "[" + set.toString() + "]");
}

function delete_from_storage_set(key, id) {
        var set = eval($.Storage.get(key));
        if (set) {
                $.each(set, function(index, value) {
                        if (value == id) {
                                set.splice(index, 1);
                        }
                });
        }
        $.Storage.set(key, "[" + set.toString() + "]");
}

function add_track_log(event, url, data) {
        if ($.cookie("uafc")) {
                jQuery.ajax( {
                        url : "/tracking",
                        type : "POST",
                        data : {
                                "event" : event,
                                "url" : url,
                                "data" : data
                        },
                        cache : false
                });
        }
}