Subversion Repositories SmartDukaan

Rev

Rev 6912 | Rev 6924 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1463 varun.gupt 1
var totalAmount = 0.00;
2
 
794 rajveer 3
$(document).ready(function(){
3830 chandransh 4
	checkIfUserHasAddress();
6561 amit.gupta 5
	if(window.location.pathname === "/cart") {
6
		changeEstimate();
7
	}
6903 anupam.sin 8
 
9
	$('.wantInsurance').change( function() {
10
		productId = $(this).parents('tr').prev().attr('id');
6912 anupam.sin 11
		if(this.checked) {
12
			trackEventWithGA('Insurance', 'Want Insurance', productId);
13
		}
6903 anupam.sin 14
		totalPrice = $('#totalAmountColumn').text();
15
		var initialPrice = parseFloat(totalPrice.replace(/,/g, ''));
16
		var initalNetPrice = 0.0;
17
		if($('#discountedAmount').length != 0) {
18
			initalNetPrice = parseFloat($('#discountedAmount').text().replace(/,/g, ''));
19
		}
20
		var insuranceAmount = parseFloat($('#agreeInsuranceTnc_' + productId).attr('insuranceAmount'));
21
		$(this).attr('disabled', 'disabled');
22
		//The animation will be running for 1 second
23
		var interval = parseInt(1000/(insuranceAmount/4));
24
		//If insurance amount is large we need to set a minimum possible interval. 5 ms looks safe. 
25
		if(interval < 5) {
26
			interval = 5;
27
		}
28
		if(this.checked) {
29
			$('#agreeInsuranceTncDiv_' + productId).show();
30
			//Update grand total but,
31
			//Disable the checkbox so that user can't change while animation is running 
32
 
33
			displayCounter(initialPrice, initialPrice + insuranceAmount, $('#totalAmountColumn'), "increase", interval);
34
			if($('#discountedAmount').length != 0) {
35
				displayCounter(initalNetPrice, initalNetPrice + insuranceAmount, $('#discountedAmount'), "increase", interval);
36
			}
37
		}
38
		else {
39
			var previousStatus = $('#agreeInsuranceTnc_' + productId)[0].checked;
40
			$('#agreeInsuranceTnc_' + productId)[0].checked =  false;
41
			if(previousStatus === true) {
42
				$('#agreeInsuranceTnc_' + productId).trigger('change');
43
			}
44
			$('#agreeInsuranceTncDiv_' + productId).hide();
45
 
46
			//Disable the checkbox so that user can't change while animation is running 
47
			displayCounter(initialPrice, initialPrice - insuranceAmount, $('#totalAmountColumn'), "decrease", interval);
48
			if($('#discountedAmount').length != 0) {
49
				displayCounter(initalNetPrice, initalNetPrice - insuranceAmount, $('#discountedAmount'), "decrease", interval);
50
			}
51
		}
52
		$(".payable").animate({ "background-color": "orange"}, 400 );
53
		$(".payable").animate({ "background-color": "#FFC"}, 400 );
54
	});
55
 
56
	function displayCounter(start, end, jQueryDom, direction, interval) {
57
		if(direction === "increase") {
58
			if(start + 4 < end) {
59
				start = start + 4;
60
				jQueryDom.html('<img src="/images/rupee-symbol.png" alt="Rs."> ' + formatAmount(start.toFixed(2)));
61
				window.setTimeout(function() {displayCounter(start, end, jQueryDom, direction, interval);}, interval);
62
			} else {
63
				jQueryDom.html('<img src="/images/rupee-symbol.png" alt="Rs."> ' + formatAmount(end.toFixed(2)));
64
				$('.wantInsurance').removeAttr('disabled');
65
			}
66
		} else if (direction === "decrease") {
67
			if(start + 4 > end) {
68
				start = start - 4;
69
				jQueryDom.html('<img src="/images/rupee-symbol.png" alt="Rs."> ' + formatAmount(start.toFixed(2)));
70
				window.setTimeout(function() {displayCounter(start, end, jQueryDom, direction, interval);}, interval);
71
			} else {
72
				jQueryDom.html('<img src="/images/rupee-symbol.png" alt="Rs."> ' + formatAmount(end.toFixed(2)));
73
				$('.wantInsurance').removeAttr('disabled');
74
			}
75
		}
76
	}
77
 
78
	function formatAmount(amount) {
79
		var numbers = amount.toString().split('.');
80
		var beforeDecimal = parseInt(numbers[0]);
81
		var afterDecimal = numbers[1];
82
		var finalString = '';
83
		while( beforeDecimal > 0) {
84
			if($.inArray(finalString.length, [3,6,9,12]) > -1) {
85
				finalString = ',' + finalString;
86
			}
87
			var r = beforeDecimal % 10;
88
			beforeDecimal /= 10;
89
			beforeDecimal = parseInt(beforeDecimal);
90
			finalString = r.toString() + finalString;
91
		}
92
		finalString = finalString + '.' + afterDecimal;
93
		return finalString;
94
	} 
95
 
96
	$('.agreeInsuranceTnc').change(function() {
97
		$('.agreeInsuranceTnc').attr('disabled', 'disabled');
98
		$('.wantInsurance').attr('disabled', 'disabled');
99
		productId = $(this).attr('itemId');
6912 anupam.sin 100
		if(this.checked){
101
			trackEventWithGA('Insurance', 'Agreed to Terms', productId);
102
		}
6903 anupam.sin 103
		jQuery.ajax({
104
			type: "POST",
105
			url: "/cart!insureItem",
106
			data: "toInsure=" + this.checked + "&productId=" + productId + "&quantity=" + $(this).attr('quantity'),
107
			success: function(response){
108
				if(response === "SUCCESS") {
109
					if ($('#agreeInsuranceTnc_' + productId)[0].checked == true) {
110
						$("#insuranceMessage").html("Your product is now insured!");
111
					} else {
112
						$("#insuranceMessage").attr('style', 'color:red;border:1px solid red;box-shadow: red 0 0 30px 1px;');
113
						$("#insuranceMessage").html("Insurance removed!");
114
					}
115
					$("#insuranceMessage").fadeIn(600, function() {
116
						window.setTimeout(function() {
117
							$("#insuranceMessage").fadeOut(800, function() {
118
								$('.agreeInsuranceTnc').removeAttr('disabled');
119
								$('.wantInsurance').removeAttr('disabled');
120
								$("#insuranceMessage").removeAttr('style');
121
							});
122
						}, 1500);
123
					});
124
				} else {
125
 
126
//					$("#insuranceMessage").html("Sorry! There is some problem with insurance right now.");
127
//					$("#insuranceMessage").attr('style', 'color:red;');
128
//					$("#insuranceMessage").fadeIn(100);
129
//					$("#insuranceMessage").fadeOut(6000);
130
//					$('#wantInsurance')[0].checked = false;
131
//					$('#agreeInsuranceTnc')[0].checked =  false;
132
//					$('#agreeInsuranceTncDiv').hide();
133
 
134
					//If we were not able to update user's insurance preference then let's reload.
135
					location.reload();
136
 
137
					//OR ????
138
					//window.location.href = "/exception";
139
 
140
				}
141
			},
142
			error : function(){
143
				//There was an error with AJAX call and we can't be sure what happened; 
144
				//Let us reload this page and see what was persisted in cart.
145
				location.reload();
146
			}
147
		});
148
	});
149
 
4453 varun.gupt 150
	$('#checkout').click(function(){
6903 anupam.sin 151
		if($('input.agreeInsuranceTnc:checkbox:not(:hidden):not(:checked)').length != 0) {
152
			alert('Please agree to the terms and conditions to get insurance');
153
			return false;
154
		}
3830 chandransh 155
		window.location.href = "/shipping";
156
	});
157
 
6903 anupam.sin 158
	$('#cancelInsurance').click(function() {
159
		jQuery.ajax({
160
			type: "POST",
161
			url: "/shipping!cancelInsurance",
162
			success: function(msg){
163
				//If shipping-failure.vm page was sent then send the user to cart page.
164
				if (msg.replace(/^\s+|\s+$/g,'') === "0") {
165
					window.location.href = "/cart";
166
				} else {
167
					window.location.reload();
168
				}
169
			},
170
			error: function(msg){
171
				//We don't know what happened, send the user to cart page.
172
				window.location.href = "/cart";
173
			}
174
		});
175
	});
176
 
177
	$('#companyInsurance').click(function() {
178
		$('#insuranceDetailDiv').slideUp(400);
179
		$.ajax({
180
			type: "POST",
181
			url: "/shipping!storeInsuranceDetails?addressId=" + $('#addressid').val(),
182
			data: "&guardianName=Organisation&dob=Organisation",
183
			success: function(msg){
184
				;//Don't do anything.
185
			},
186
			error: function(response) {
187
				//Don't do anything.
188
			}
189
		});
190
	});
191
 
192
	$('#dob').focusin(function() {
193
		if($(this).val() === 'Example: 18/08/1986') {
194
			$(this).val('');
195
			$(this).removeAttr('style');
196
		}
197
	});
198
 
199
 
200
	function validateDob() {
201
		var dateStr = $('#dob').val();
202
		dateStrArray = dateStr.split('/');
203
		//array[0] is day, array[1] is month, array[2] is year 
204
		//Now we will interchange the first and second placed numbers so that the date is in mm/dd/yyyy format 
205
		//which will be accepted by the Date.parse() method
206
		temp = dateStrArray[0];
207
		dateStrArray[0] = dateStrArray[1];
208
		dateStrArray[1] = temp;
209
		if(isNaN(Date.parse(dateStrArray.join('/')))) {
210
			alert('Please enter date of birth in given format');
211
			$('#dob').attr('style', 'color:#999;');
212
			$('#dob').val('Example: 18/08/1986');
213
			return false;
214
		}
215
		return true;
216
	}
217
 
218
	function focusOutHandler() {
219
		if($(this).val() === '') {
220
			$(this).attr('style', 'color:#999;');
221
			$(this).val('Example: 18/08/1986');
222
		} else {
223
			if(validateDob()) {
224
				$("#dob").animate({ "background-color": "orange"}, 200 );
225
				$("#dob").animate({ "background-color": "white"}, 200 );
226
				$('#dob').val((new Date(Date.parse(dateStrArray.join('/')))).toDateString().substring(4));
227
			}
228
		}
229
	};
230
 
231
	$('#dob').focusout(focusOutHandler);
232
 
233
	$('#submitInsuranceDetails').hover(
234
		function() {
235
			$('#dob').unbind('focusout');
236
		}, 
237
		function() {
238
			$('#dob').bind(focusOutHandler);
239
		}
240
	);
241
 
242
 
243
	$('#submitInsuranceDetails').click(function() {
244
		if($('#guardianName').val() === '') {
245
			alert('Please enter the name of your Father/Husband');
246
			return false;
247
		}
248
		if($('#dob').val() === '' || $('#dob').val() === 'Example: 18/08/1986') {
249
			alert('Please enter your date of birth');
250
			return false;
251
		}
252
 
253
		if(!validateDob()) {
254
			return false;
255
		}
256
 
257
		$('#thanks').fadeIn(1000);
258
		$('#insuranceDetailDiv h3').fadeOut(100);
259
		$('#insuranceDetailsForm').slideUp(100);
260
		jQuery.ajax({
261
			type: "POST",
262
			url: "/shipping!storeInsuranceDetails?addressId=" + $('#addressid').val(),
263
			data: $('#insuranceDetailsForm').serialize(),
264
			success: function(msg){
265
				window.setTimeout(function() {$('#insuranceDetailDiv').slideUp(100);}, 2000);
266
			},
267
			error: function(response) {
268
				//We dont want to bother user too much with these details.We can ignore this in case of rare failures.
269
				window.setTimeout(function() {$('#insuranceDetailDiv').slideUp(100);}, 2000);
270
			}
271
		});
272
	});
273
 
4453 varun.gupt 274
	$('#poplogin').click(function(){
4222 varun.gupt 275
		$.colorbox({
276
    		width: "860px",
4458 varun.gupt 277
    		height: "340px",
4222 varun.gupt 278
    		iframe: false,
279
    		href: "/login-mini",
280
 
281
    		onComplete: function(){
4372 varun.gupt 282
    			trackEventWithGA('Cart', 'Login/Register Popup', '');
4222 varun.gupt 283
    		}
284
    	});
285
	});
286
 
3830 chandransh 287
	$('#proceedToPay').click(function(){
6921 anupam.sin 288
		if($('#insuranceDetailsForm:visible').length == 1) {
6912 anupam.sin 289
			alert("Please submit the insurance details first");
290
			return false;
291
		}
292
 
5748 anupam.sin 293
		if(($('#store-addresses').find('.default-address').attr('zone') !=  $('#zone-selector').val()) && ($("#tabSelector").val() == "HotSpot")) {
5747 anupam.sin 294
			alert("Please select one store address");
295
			return false;
296
		}
4222 varun.gupt 297
		var canProceedToPay = parseInt($('#canProceedToPay').val());
3830 chandransh 298
 
299
		if (canProceedToPay == 1)	{
300
 
301
			trackProceedToPay();
302
			$('#formProceedToPay').submit();
303
		} else	{
5040 varun.gupt 304
			var reasonActionDisability = $('#reasonActionDisability').val();
3830 chandransh 305
 
5040 varun.gupt 306
			if(reasonActionDisability.indexOf('Location not serviceable') >= 0)	{
307
				trackEventWithGA('Cart', 'Location not serviceable', $('#selectedPincode').val());
308
			}
5831 anupam.sin 309
			else if (($("#tabSelector").val() == "HotSpot")) {
310
				reasonActionDisability = 'Please enter an address which will be printed on the bill';
311
			} else {
312
				reasonActionDisability = 'Please specify a delivery address';
313
			}
5040 varun.gupt 314
			alert(reasonActionDisability);
3830 chandransh 315
		}
316
	});
4791 varun.gupt 317
 
318
	$('#viewOrders').click(function(){
319
		window.location.href = "/myaccount";
320
	});
1190 varun.gupt 321
 
3830 chandransh 322
	$('#cart .remove-quantitybttn').click(function(){
323
		var itemId = $(this).attr('id').split('_')[1];
324
		window.location.href = "/cart/"  + itemId + "?_method=delete" + "&productid=" + itemId;
1463 varun.gupt 325
	});
1981 varun.gupt 326
 
3830 chandransh 327
	$('#computeEstimate').click(function(){
328
		changeEstimate();
3941 varun.gupt 329
		$('#cartDetail').find('thead .dev-pincode').html($('#zipcode').val());
3830 chandransh 330
	});
331
 
1981 varun.gupt 332
	$('#applyCoupon').click(function(){
333
		$('#couponAction').val('applycoupon');
334
		$('#frmCouponCode').submit();
335
	});
336
 
337
	$('#removeCoupon').click(function(){
338
		$('#couponAction').val('removecoupon');
339
		$('#frmCouponCode').submit();
340
	});
3830 chandransh 341
 
342
	$('.cart-item-quantity').change(function(){
343
		var itemId = $(this).attr("id").split('_')[1];
344
		var quantity = parseInt($(this).val());
345
 
346
		if (quantity > 5)	{
347
			alert("You can not order more than 5 pieces of same product.");
348
			$(obj).focus();
349
		}
350
		else	{
351
			jQuery.ajax({
352
				type: "POST",
353
				url: "/cart/" + itemId + "?_method=put&productid=" + itemId + "&quantity=" + quantity,
354
				data: "productid=" + itemId + "&quantity=" + quantity,
355
				success: function(msg){
356
					window.location.reload();
357
				}
358
			});
359
		}
360
	});
361
 
362
	$('#submitAddress').click(function(){
6912 anupam.sin 363
		if($('#guardianName').length > 0 && $('#guardianName').val() === '') {
364
			alert('Please enter the name of your Father/Husband');
365
			return false;
366
		}
367
		if($('#dob').length > 0 && ($('#dob').val() === '' || $('#dob').val() === 'Example: 18/08/1986')) {
368
			alert('Please enter your date of birth');
369
			return false;
370
		}
371
 
372
		if($('#dob').length > 0) {
373
			if(!validateDob()) {
374
				return false;
375
			}
376
		}
377
 
378
 
3830 chandransh 379
		$('#frmShippingAddress').submit();
380
	});
381
 
4325 mandeep.dh 382
	$('#addAddress').live('click', function(){
3830 chandransh 383
		showAddAddressForm();
384
	});
4325 mandeep.dh 385
 
386
	$('#closeAddAddressForm').live('click', function() {
3830 chandransh 387
		showAddressList();
388
	});
389
 
390
	$('#addresses .button-address-select').click(function(){
391
		var addressId = $(this).attr('id').split('_')[1];
392
		$('#formChangeAddressTo_' + addressId).submit();
393
	});
394
 
5716 anupam.sin 395
	function checkIfUserHasAddress(){
3830 chandransh 396
		var addressEmpty = parseInt($('#addressEmpty').val());
397
 
398
		if (addressEmpty == 1)	{
399
			showAddAddressForm();
400
		}
1460 varun.gupt 401
	}
3830 chandransh 402
 
403
	function showAddAddressForm(){
5716 anupam.sin 404
		//$('#main-right-container').hide();
405
		$('#shipping-address-div').hide();
406
		$('#billing-address-div').hide();
3830 chandransh 407
		$('#frmShippingAddress').show();
408
	}
409
 
410
	function showAddressList()	{
411
		$('#frmShippingAddress').hide();
5716 anupam.sin 412
		//$('#main-right-container').show();
5831 anupam.sin 413
		if($("#tabSelector").val() == "HotSpot") {
414
			$('#billing-address-div').show();
415
		} else {
416
			$('#shipping-address-div').show();
417
		}
3830 chandransh 418
	}
419
});
794 rajveer 420
 
1463 varun.gupt 421
function sumOfColumns(tableID, columnIndex, hasHeader)	{
422
	var tot = 0;
423
	var inc = 1;
424
	var tableElement = $("#" + tableID + " tr" + (hasHeader ? ":gt(0)" : ""));
425
	var tableChildren = tableElement.children("td:nth-child(" + columnIndex + ")");
426
 
427
	tableChildren.each(function() {
428
		var currentVal = document.getElementById('totalPrice' + inc).innerHTML;
429
		inc ++;
430
		var splitVal = currentVal.split("Rs.");
431
		var num = parseFloat(splitVal[1].replace(/[^\d\.-]/g,''));
432
		tot += parseFloat(num);
433
	});
434
	totalAmount = tot;
435
	var formated_value = $().number_format(tot, {
436
		numberOfDecimals:2,
437
		decimalSeparator: '.',
438
		thousandSeparator: ',',
439
		symbol: 'Rs.'
440
	});
441
	return formated_value;
794 rajveer 442
}
443
 
444
function subtractionOfColumns(tableID, columnIndex, hasHeader) {
1463 varun.gupt 445
	var tot = 0;
446
 
447
	$("#" + tableID + " tr" + (hasHeader ? ":gt(0)" : "")).children("td:nth-child(" + columnIndex + ")").each(function(){
448
		var currentVal=$(this).html();
449
		var splitVal=currentVal.split("Rs.");
450
		var num = parseFloat(splitVal[1].replace(/[^\d\.-]/g,''));
451
		tot += parseInt(num);
452
	});
453
 
454
	var formated_value = $().number_format(tot, {
455
		numberOfDecimals: 2,
456
		decimalSeparator: '.',
457
		thousandSeparator: ',',
458
		symbol: 'Rs.'
459
	});
460
	return formated_value;
794 rajveer 461
}
462
 
1463 varun.gupt 463
function changeEstimate(item_id)	{
464
	if(item_id == null )	{
3830 chandransh 465
		$("#cartDetail tbody tr").each(function(index, item){
466
			var itemId = $(item).attr("id");
1463 varun.gupt 467
			changeEstimate(itemId);
3830 chandransh 468
		});
469
 
1463 varun.gupt 470
	} else	{
471
		jQuery.ajax({
472
			type: "GET",
473
			url: "/estimate/" + $("#zipcode").val() + "_" + item_id,
474
			beforeSend: function(){
6561 amit.gupta 475
				$("#img" + "_" + item_id).html("<img src='/images/loader_l.gif'>").show();
476
				$("#block" + "_" + item_id).hide();
477
				$("#serv_" + item_id).hide();
1463 varun.gupt 478
			},
3830 chandransh 479
			success: function(data){
6561 amit.gupta 480
				$("#img" + "_" + item_id).html("<img src='/images/loader_l.gif'>").hide();
3830 chandransh 481
				var response = eval('(' + data + ')');
482
				var deliveryEstimate = parseInt(response['delivery_estimate']);
6561 amit.gupta 483
				var otg= response['on_time_guarantee'];
3830 chandransh 484
 
485
				if(deliveryEstimate == -1)	{
6561 amit.gupta 486
					$("#serv_" + item_id).show();
487
					return;
3830 chandransh 488
 
489
				} else if(deliveryEstimate == 1)	{
6561 amit.gupta 490
					$("#days_" + item_id).html('1 Business Day');
3830 chandransh 491
				}
492
				else	{
6561 amit.gupta 493
					$("#days_" + item_id).html(deliveryEstimate + ' Business Days');
3830 chandransh 494
				}
6561 amit.gupta 495
				if(otg == "true")	{
496
					$("#otg_" + item_id).show();
497
				} else {
498
					$("#otg_" + item_id).hide();
499
				}
500
				$("#block" + "_" + item_id).show();
1463 varun.gupt 501
			}
502
		});
503
	}
504
}