Subversion Repositories SmartDukaan

Rev

Rev 6561 | Rev 6912 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6561 Rev 6903
Line 1... Line 1...
1
var totalAmount = 0.00;
1
var totalAmount = 0.00;
2
 
2
 
3
$(document).ready(function(){
3
$(document).ready(function(){
4
 
-
 
5
	checkIfUserHasAddress();
4
	checkIfUserHasAddress();
6
	if(window.location.pathname === "/cart") {
5
	if(window.location.pathname === "/cart") {
7
		changeEstimate();
6
		changeEstimate();
8
	}
7
	}
-
 
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
	
9
	$('#checkout').click(function(){
144
	$('#checkout').click(function(){
-
 
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
		}
10
		window.location.href = "/shipping";
149
		window.location.href = "/shipping";
11
	});
150
	});
12
	
151
	
-
 
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
	
13
	$('#poplogin').click(function(){
268
	$('#poplogin').click(function(){
14
		$.colorbox({
269
		$.colorbox({
15
    		width: "860px",
270
    		width: "860px",
16
    		height: "340px",
271
    		height: "340px",
17
    		iframe: false,
272
    		iframe: false,