Subversion Repositories SmartDukaan

Rev

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