Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7272 amit.gupta 1
/* 
2
     Example:
3
     addResearch(refdivid,reqtype) refdivid=pane1, reqtype=single/multi
4
     Multiple request: onclick="javascript:addResearch('pane2','multi');"
5
     Single request: onclick="javascript:addResearch('','single');"
6
 
7
     addToCart(refdivid,reqtype) refdivid=pane1, reqtype=single/multi
8
     Multiple request: onclick="javascript:addToCart('pane2','multi');"
9
     Single request: onclick="javascript:addToCart('','single');"
10
 
11
     NOTE: incase reqtype=list then refdivid should be product id
12
*/
13
 
14
function trackEventWithGA(eventCategory, actionName, label)	{
15
	if(typeof _gaq != "undefined" && _gaq != null)	{
16
		_gaq.push(['_trackEvent', eventCategory, actionName, label, 2]);
17
	}
18
}
19
 
20
function trackPageWithGA(page)	{
21
	if(typeof _gaq != "undefined" && _gaq != null)	{
22
		_gaq.push(['_trackPageview', page]);
23
	}
24
}
25
 
26
$(function() {
27
	  $('.add-to-cart-icon').live('click', function() {
28
		clickedCatalogItem=$(this).closest('div.productDetails').find('a').html();
29
	    var items = this.id;
30
	    var itemsarray = items.split('-');
31
	    addToCart(itemsarray[1], itemsarray[0]);
32
	    trackEventWithGA('Home Page', 'Add to Cart Click', '');
33
	    return false;
34
	  });
35
 
36
	  $('.best-deals .title a, .best-deals .productImg a').click(function(){
37
		  trackEventWithGA('Home Page', 'Product Link Click', 'Best Deals');
38
	  });
39
	  $('.latest-arrivals .title a, .latest-arrivals .productImg a').click(function(){
40
		  trackEventWithGA('Home Page', 'Product Link Click', 'Latest Arrivals');
41
	  });
42
	  $('.best-sellers .title a, .best-sellers .productImg a').click(function(){
43
		  trackEventWithGA('Home Page', 'Product Link Click', 'Best Sellers');
44
	  });
45
 
46
	$("a.addtocart").live('click', function() {
47
		clickedCatalogItem=$(this).closest('div.productDetails').find('a').html();
48
		var catalogid = $(this).attr('catalogid');
49
 
50
		var radBtn = "";
51
		var param = "PARAMETERS_" + catalogid;
52
		radBtn += '<input type="hidden" id="selected_catalog_id" value=' + catalogid + ' />';
53
		$.each(eval(param), function(val, text) {
54
			radBtn += '<label><input type="radio" name="radColorSel" value="' + val + '" /> <span>' + text + '</span></label>';
55
		});
56
 
57
		radBtn += '<div class="clearBoth"></div>';
58
		$(".colorSelLightBoxColor .content").html(radBtn);
59
 
60
		$.fn.colorbox({
61
			inline:true, 
62
			href:".colorSelLightBoxColor", 
63
			open:true, 
64
			onComplete:function(){
65
				var posLeft = (document.documentElement.clientWidth / 2) - (parseInt($('#colorbox').css('width')) / 2);
66
				var posTop = (document.documentElement.clientHeight / 2) - (parseInt($('#colorbox').css('height')) / 2);
67
 
68
				$('#colorbox').css('top', posTop);
69
				$('#colorbox').css('left', posLeft);
70
			    trackEventWithGA('Home Page', 'Add to Cart Click', '');
71
			}
72
		});
73
	});
74
 
75
 
76
	$('#notify').live('click', function() {
77
		emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
78
		email=$('#emailId').val();
79
		if(!email.match(emailExp)){
80
			alert("Please enter valid email.");
81
			return false;
82
		}
83
		itemId=$('#itemId').val();
84
		emailId=$('#emailId').val();
85
		jQuery.ajax({
86
   		 type: "POST",
87
   		 url: "/product-notification",
88
   		 data: "emailId="+emailId+"&itemId="+itemId,
89
   		 success: function(msg){
90
   			$('#notificationBox').html("<h3>We will notify you once we get the stock.</h3>");	 
91
   		 }
92
   	 	});
93
	});
94
 
95
 
96
    $("#query").autocomplete({  autoFocus: true, minLength: 3,
97
    	source: "/auto-suggest",
98
    	select: function( event, ui ) {
99
    		this.value = ui.item.value;
100
			$('#formSearch').submit();
101
			return false;
102
		}
103
    });
104
 
105
});
106
 
107
function addToCart(refdivid,reqtype){
108
     var seldata="";
109
     var tot=0;
110
     var saprt="";
111
     if(reqtype == "multi"){
112
          var containerdiv = "#" + refdivid;
113
          var par= containerdiv + " input[type=checkbox]:checked";
114
          var isMulti = false;
115
          $(par).each(function(){
116
 
117
	       	   var totalcolors = $(this).attr('totalcolors');
118
	           saprt = (tot>0)? "_":"";
119
               seldata += saprt+$(this).attr('itemid');
120
 
121
	           if(totalcolors != 1)	{
122
	        	   isMulti = true;
123
	       	   }
124
               tot++;
125
          });
126
          if(isMulti == true){
127
        	  alert("Some items are available in multiple colors. Please go to product page and select color you want.");
128
        	  return;
129
          } else	{
130
        	  var items = seldata.split('_');
131
        	  for(var i in items)	{
132
        		  trackEventWithGA('Order', 'Add to Cart', items[i]);
133
        	  }
134
          }
135
     }else if(reqtype == "single"){
136
          seldata = $("#item_id").val();
137
		  trackEventWithGA('Order', 'Add to Cart', seldata + '');
138
          tot=1;
139
     }else if(reqtype == "list"){
140
    	  var seldata=refdivid;
141
		  trackEventWithGA('Order', 'Add to Cart', seldata + '');
142
          tot=1;
143
     }
144
 
145
     if(tot > 0)	{
146
    	 jQuery.ajax({
147
    		 type: "POST",
148
    		 url: "/addtocart",
149
    		 data: "productid=" + seldata,
150
    		 success: function(msg){
151
    			 if(msg==""){
152
    				 window.location = "/cart";
153
    			 }else{
154
    				 trackEventWithGA('Order', 'OOS Lightbox', clickedCatalogItem);
155
    				 displayRelatedProducts(msg);
156
    			 }
157
    		 }
158
    	 });
159
     } else	{
160
    	 alert("Please select atleast one product");
161
     }
162
}
163
 
164
 
165
function selectColor(){
166
	var selected = $(".colorSelLightBoxColor .content input[name='radColorSel']:checked");
167
	var itemid = selected.val();
168
 
169
	trackEventWithGA('Order', 'Select Product Color', itemid);
170
 
171
	if(itemid == undefined)	{
172
		alert('Please select a color.');
173
	} else	{
174
		addToCart(itemid,'list');
175
	}
176
}
177
 
178
 
179
function displayRelatedProducts(message){
180
	$.colorbox({
181
		html:message,
182
		maxWidth:"400px",
183
		maxHeight:"410px",
184
		width:"400px",
185
 
186
		onComplete: function(){
187
			$('#cartRecommendationBox').show().find('.widgetChkBox').hide();
188
		},
189
		onCleanup: function(){
190
			$('#cartRecommendationBox').hide();
191
		}
192
	});
193
 
194
}
195
 
196
 
197
 
198
function addResearch(refdivid, reqtype){
199
     var research_tot = $("#research_total").val();
200
     var seldata = [];
201
     var tot = 0;
202
     var fromsrc = "";
203
 
204
     if(reqtype == "multi")	{
205
          var containerdiv = "#" + refdivid;     
206
          var par = containerdiv + " input[type=checkbox]:checked";
207
          $(par).each(function(){
208
               seldata.unshift($(this).val());
209
               tot ++;
210
          });
211
     }else if(reqtype == "single")	{
212
    	  seldata.unshift($("#product_id").val());
213
          tot = 1;
214
          fromsrc = "product_page";
215
     }else if(reqtype == "list"){
216
    	  seldata.unshift(refdivid);
217
          tot = 1;
218
          fromsrc = "other_pages";
219
     }
220
 
221
     if(tot > 0)	{
222
          jQuery.ajax({
223
               type: "POST",
224
                      url: "/addtoresearch",
225
                      data: "productid=[" + seldata +  "]&fromsrc=" + fromsrc,
226
                      success: function(msg){
227
                           var postSuccessDOMProcessing = function(objValue)	{
228
                        	   trackEventWithGA('Research', 'Add to Research', objValue);
229
                        	   add_to_storage_set("resitems", objValue);
230
                        	   //Updating the count
231
                        	   var truncFields=function(){
232
                        		   var msgDom = $(msg);
233
                        		   $("#pane1").prepend(msgDom);
234
                        		   $(msgDom).find("a.truncate").truncate({addtitle: true});
235
                            	   $(msgDom).find("div.price").truncate({addtitle: true});
236
                            	   $(msgDom).find("div.text").truncate({addtitle: true});
237
                            	   $(msgDom).animate({ backgroundColor: "#fcffb3" }, 'slow');
238
                            	   $(msgDom).animate({ backgroundColor: "transparent" }, 'slow');
239
                            	   $('#research_compare_count').html($('#myresearch input[type="checkbox"]:checked').length);
240
                        	   };
241
                        	   if($('.common-widget-top-bar').data('is_colapsed')){
242
                        		   $("#myresearch").children(".common-widget-top-bar").trigger('click',[truncFields]);
243
                        	   }else {
244
                        		   truncFields();
245
                        	   }
246
                           };
247
 
248
                           if(msg == 0){
249
                                alert( "Product is already in compare list" );
250
                           } else	{
251
                                var t = (research_tot * 1) + (tot * 1);
252
                                $("#research_total").val(t);
253
                                if(t > 0)	{
254
                                     $("#research_default").css("display","none");
255
                                }     
256
                                if(reqtype == "multi")	{
257
                                     jQuery.each(seldata,function(intIndex, objValue)	{
258
                                    	 postSuccessDOMProcessing(objValue);
259
                                     });
260
 
261
                                } else if(reqtype == "single")	{
262
                                	postSuccessDOMProcessing(seldata);
263
 
264
                                } else if(reqtype == "list")	{
265
                                	postSuccessDOMProcessing(seldata);
266
                                }
267
                           }
268
                      }
269
          });
270
     } else	{
271
          alert("Please select atleast one product");
272
     }
273
}
274
 
275
 
276
 
277
 
278
function scrollWinToTop(){
279
    $('html, body').animate({
280
      scrollTop: $("body").offset().top
281
    }, 0);
282
}
283
 
284
function changeShipAddress(id){
285
  addressId = id.substring(20);
286
   jQuery.ajax({
287
      type: "POST",
288
      url: "/shipping",
289
      data: "action=change&addressid="+addressId,
290
      success: function(msg){
291
         $("#addressid").val(msg);
292
      }
293
   });
294
  document.getElementById('shipAddressContainer').innerHTML = document.getElementById(id).innerHTML;
295
	$(".proceed-bttn .yellow-image").removeClass('imgDisableButton').addClass('imgEnableButton');
296
	$(".proceed-bttn input[disabled='']").removeAttr('disabled');
297
 
298
	$('#newShipAdr').hide();
299
	scrollWinToTop();
300
 
301
}
302
 
303
function merge_history_items(){
304
	params = $.Storage.get("histitems");
305
 
306
	jQuery.ajax({
307
		type: "POST",
308
		url: "/browse-history",
309
		data: "id="+params,
310
		cache: false,
311
		success: function(json) {
312
		    $.Storage.set("histitems", json);
313
		    load_history_widget();
314
	    }
315
	});
316
}
317
 
318
function load_history_widget() {
319
 
320
	var browseHistoryWidgetContainer = $("#browsehistory");
321
 
322
	if($(browseHistoryWidgetContainer).length == 0) {
323
		return;
324
	}
325
	var histitems = $.Storage.get("histitems");
326
	if (histitems == "[]") {
327
		$(browseHistoryWidgetContainer).find(".common-widget-content-area").html("");
328
		$(browseHistoryWidgetContainer).children('.common-widget-top-bar').trigger('click');
329
		return;
330
	}
331
	var params = "/" + histitems;
332
 
333
	jQuery.ajax({
334
		type: "GET",
335
		url: "/browse-history" + params,
336
		cache: true,
337
		success: function(html){
338
		    $("#browsehistory").html(html);
339
 
340
		    // Product Title
341
		    $("#browsehistory table td div a").each(function(){
342
			    $(this).truncate({addtitle: true}); 
343
		    });
344
 
345
    		// Product Price
346
	    	$("#browsehistory table td div div.price").each(function(){
347
		    	$(this).truncate({addtitle: true});
348
		    });
349
 
350
		    // Product Details
351
		    $("#browsehistory table td div div.text").each(function(){
352
			    $(this).truncate({addtitle: true});
353
		    });
354
	    }
355
	});
356
}
357
 
358
function add_to_storage_set(key, id) {
359
	var set = eval($.Storage.get(key));
360
	if (set) {
361
		$.each(set, function(index, value) {
362
			if (value == id) {
363
				set.splice(index, 1);
364
			}
365
		});
366
	}
367
	set.unshift(id);
368
	if (set.length>10)
369
	{
370
		set.splice(10, set.length-10);
371
	}
372
	$.Storage.set(key, "[" + set.toString() + "]");
373
}
374
 
375
function delete_from_storage_set(key, id) {
376
	var set = eval($.Storage.get(key));
377
	if (set) {
378
		$.each(set, function(index, value) {
379
			if (value == id) {
380
				set.splice(index, 1);
381
			}
382
		});
383
	}
384
	$.Storage.set(key, "[" + set.toString() + "]");
385
}
386
 
387
function add_track_log(event, url, data) {
388
	if ($.cookie("uafc")) {
389
		jQuery.ajax( {
390
			url : "/tracking",
391
			type : "POST",
392
			data : {
393
				"event" : event,
394
				"url" : url,
395
				"data" : data
396
			},
397
			cache : false
398
		});
399
	}
400
}
401
 
402
function load_accessories_widget(){
403
	var accessoriesContainer = $("#accessories");
404
 
405
	if(accessoriesContainer.length == 0) {
406
		return;
407
	}
408
 
409
	jQuery.ajax({
410
		type : "GET",
411
		url : "/related-accessories/" + $("#product_id").val(),
412
		cache: true,
413
		success : function(html) {
414
 
415
			if($.trim(html).length > 0)	{
416
	   		    $(accessoriesContainer).html(html);
417
 
418
	   		    var accessoryInfoContainer = $(accessoriesContainer).find("table td div");
419
			    // Product Title
420
			    $(accessoryInfoContainer).children("a").each(function() {
421
			    	$(this).truncate({addtitle : true});
422
			    });
423
 
424
			    // Product Price
425
			    $(accessoryInfoContainer).children("div.price").each(function() {
426
			    	$(this).truncate({addtitle : true});
427
				});
428
 
429
			    // Product Details
430
			    $(accessoryInfoContainer).children("div.text").each(function() {
431
			    	$(this).truncate( {addtitle : true});
432
				});
433
			} else	{
434
				$(accessoriesContainer).hide();
435
			}
436
	    }
437
	});
438
}