Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
7272 amit.gupta 1
$(document).ready(function(){
2
 
3
	/**
4
		Shipping Address page 
5
	*/
6
	if($("#shippingAddress").length){
7
 
8
		otherAddressCount 	= 0;
9
		lastContainerID 	= 0;
10
		lastAddressID		= 0;
11
 
12
		shippingAddressSetup();
13
	}
14
 
15
	$('#myaccountFormShippingAddress').validate({
16
		rules: {
17
			name: {
18
				required: true
19
			},
20
			line1: {
21
				required: true
22
			},
23
			state: {
24
				required: true
25
			},
26
			city: {
27
				required: true
28
			},
29
			pincode: {
30
				required: true,
31
				minlength: 6,
32
				maxlength: 6
33
			},
34
			phone: {
35
				required: true,
36
				minlength: 10,
37
				maxlength: 10
38
			}
39
		}
40
	});
41
});
42
 
43
$(function() {
44
	var zone = 'All';
45
	$('#store-address-container').find('tr').each(function() {
46
		if($(this).hasClass('default-address')) {
47
			zone = $(this).attr('zone');
48
		}
49
	});
50
	$('#' + zone).attr('selected', true);
51
	$('#zone-selector').val(zone);
52
	showSpecificStores();
53
});
54
 
55
var showSpecificStores = function() {
56
	//var rows = $(this).parents('#hotspot-address-heading').siblings('#store-address-container').find('tr');
57
	var index = 0;
58
	var rows = $('#store-address-container').find('tr');
59
	if($('#zone-selector').val() == 'All') {
60
		$(rows).show();
61
	} else {
62
		$(rows).hide();
63
		$(rows).each(function() {
64
			if($(this).attr('zone') == $('#zone-selector').val()) {
65
				index++;
66
				$(this).show();
67
			}
68
		});
69
	}
70
	if(index == 0) {
71
		var count = $(rows).length + " stores";
72
	} else if(index == 1) {
73
		var count = index + " store";
74
	} else {
75
		var count= index + " stores";
76
	}
77
	trackEventWithGA("InStore Pickup Region", $('#zone-selector').val());
78
	$('#store-address-header-text').html("&nbsp;Select a pickup point (<b>" + count + "</b> available)");
79
};
80
 
81
$("#zone-selector").change(showSpecificStores);
82
 
83
$(".unselected-tab").live('click', function() {
84
	var tabToBeSelected = $(this).attr('id');
85
	var deliveryType = "In-Store Pickup";
86
	if(tabToBeSelected == 'tab-left') {
87
		$('#inStoreLocationMsg').addClass('invisibleDiv');
88
		$("#tabSelector").val("myLocation");
89
		$('#tabSelectorForAddAddressForm').val('myLocation');
90
		$('#newAddressFormHeading').text("Enter the address where you want the items to be delivered");
91
		deliveryType = "Deliver to Address";
92
	} else {
93
		$('#inStoreLocationMsg').removeClass('invisibleDiv');
94
		$("#tabSelector").val("HotSpot");
95
		$('#tabSelectorForAddAddressForm').val('HotSpot');
96
		$('#newAddressFormHeading').text("Enter the address which you want printed on the bill");
97
	}
98
	trackEventWithGA('DeliveryType', deliveryType);
99
	$(this).removeClass('unselected-tab').addClass('selected-tab');
100
	$(this).siblings().removeClass('selected-tab').addClass('unselected-tab');
101
	var hiddenPane = $(this).parent().siblings('.hidden-div');
102
	$(this).parent().siblings().addClass('hidden-div');
103
	$(hiddenPane).removeClass('hidden-div');
104
});
105
 
106
$('.shipping-page-addresses .button-address-select').click(function(){
107
	var addressId = $(this).attr('id').split('_')[1];
108
	$('#formChangeAddressTo_' + addressId).submit();
109
});
110
 
111
$('.shipping-page-addresses .store-address-selector-button').click(function(){
112
	var addressId = $(this).attr('id').split('_')[1];
113
	$('#formChangeStoreAddressTo_' + addressId).submit();
114
});
115
 
116
$('.shipping-page-addresses .billing-address-selector-button').live('click', function(){
117
	var addressId = $(this).closest('tr').attr('id').split('_')[1];
118
	$('#addressid').val(addressId);
119
	var image = $(this).closest('tr').siblings('.default-address').find('img')[0];
120
	var previousAddress = $(this).closest('tr').siblings('.default-address').attr('id').split('_')[1];
121
	$(image).attr('id', 'selectAddress_' + previousAddress);
122
	$(image).attr('src', '/images/RadioButton_Unselected.png');
123
	$(image).attr('title', 'Select this Store for Shipment');
124
	$(image).removeClass('dummy-class');
125
	$(image).addClass('billing-address-selector-button');
126
	$(this).closest('tr').siblings('.default-address').removeClass('default-address');
127
	$('#tr_' + addressId).addClass('default-address');
128
	$(this).attr('src', '/images/RadioButton_Selected.png');
129
	$(this).addClass('dummy-class');
130
	$(this).removeClass('billing-address-selector-button');
131
});
132
 
133
 
134
 
135
$(document).ready(function(){
136
	$('.default-address').focus();
137
});
138
 
139
/**
140
	Shipping Address page setup
141
*/
142
function shippingAddressSetup(){
143
 
144
	// Count no of other addresses for delete operation
145
	countOtherAddress();
146
 
147
	// Add Address button
148
	$("#shippingAddress input[name=addAddress1]").click(function(){
149
		addNewAddress();
150
	});
151
 
152
	$("#shippingAddress input[name=addAddress2]").click(function(){
153
		addNewAddress();
154
	});
155
 
156
 
157
	// Save and make primary address button
158
	$("#shippingAddress input[name=saveMakePriAddress]").click(function(){
159
		saveAddress('makePrimary');
160
	});
161
 
162
	// Save address button
163
	$("#shippingAddress input[name=saveAddress]").click(function(){
164
		saveAddress('save');
165
	});
166
 
167
	// Cancel address button
168
	$("#shippingAddress input[name=cancelAddress]").click(function(){
169
		resetShipingAddress();
170
	});
171
}
172
 
173
/**
174
	Count no. of other addresses
175
*/
176
function countOtherAddress(){
177
	var i 			= 0;
178
	var count 		= 2;
179
	var addressID 	= 0;
180
	var arrAddressID = [];
181
	var otherAddressCount = 0;
182
 
183
	if ($("#shippingAddress #A1 div").attr("id") != undefined) {
184
		addressID = parseInt($("#shippingAddress #A1 div").attr("id").substr(7));
185
		arrAddressID.push(addressID);
186
	}
187
 
188
	while(i === 0){
189
		if($("#shippingAddress").find("#A" + count + ":visible").length == 1){
190
			addressID = parseInt($("#shippingAddress").find("#A" + count + " div").attr("id").substr(7));
191
			arrAddressID.push(addressID);
192
 
193
			otherAddressCount++;	// Count of other addresses
194
		}else{
195
			i = 1;
196
		}
197
 
198
		count++;
199
	}
200
 
201
	// sort address ids so that find the lastest address id
202
	for(i=0; i<arrAddressID.length; i++){
203
		for(j=i+1; j<arrAddressID.length; j++){
204
			if(arrAddressID[i] < arrAddressID[j]){
205
				temp = arrAddressID[i];
206
				arrAddressID[i] = arrAddressID[j];
207
				arrAddressID[j] = temp;
208
			}
209
		}
210
	}
211
 
212
	lastContainerID	= otherAddressCount + 1;	// last address container id
213
	lastAddressID 	= arrAddressID[0];			// last address id
214
}
215
 
216
/**
217
	Invoked on "Make Primary Address" btn click
218
*/
219
function makePriAddress(sourceId){
220
	ajaxMakePrimary(sourceId.substring(7,sourceId.length));
221
    var destId="";
222
    var destAddress="";
223
	// if primary address is present then take backup of primary id and address
224
	if($(".noAddress1:visible").length != 1){
225
		destId 		= $("#A1 div").attr("id");
226
		destAddress = $("#" + destId + " .address").html();
227
	}
228
 
229
	var sourceIdParentId 	= $("#" + sourceId).parent().attr("id");
230
	var sourceAddress 		= $("#" + sourceId + " .address").html();
231
 
232
	// if primary address is present then swap address
233
	if($(".noAddress1:visible").length != 1){
234
		// Replace destination Id and its content with source
235
		$("#A1 #" + destId).attr("id", sourceId);
236
		$("#A1 #" + sourceId + " .address").html(sourceAddress);
237
 
238
		// Replace primary address delete button parameter with source id
239
		var priDelBtn = "<input onclick=delAddress('" + sourceId + "') value='Delete' class='button' type='button' />";
240
		$("#A1 #" + sourceId + " .addressButton .imgDeleteButton").html("");
241
		$("#A1 #" + sourceId + " .addressButton .imgDeleteButton").html(priDelBtn);
242
 
243
 
244
		// Replace source Id and its content with destination
245
		$("#" + sourceIdParentId + " #" + sourceId).attr("id", destId);
246
		$("#" + sourceIdParentId + " #" + destId + " .address").html(destAddress);
247
 
248
		// Replace other address primary button parameter with destination id
249
		var priBtn = "<input type='button' class='button' value='Make Primary Address' onClick=makePriAddress('" + destId + "') />";
250
		$("#" + sourceIdParentId + " #" + destId + " .addressButton .imgEnableButton").html("");
251
		$("#" + sourceIdParentId + " #" + destId + " .addressButton .imgEnableButton").html(priBtn);
252
 
253
		// Replace other address delete button parameter with destination id
254
		var delBtn = "<input type='button' class='button' value='Delete' onClick=delAddress('" + destId + "') />";
255
		$("#" + sourceIdParentId + " #" + destId + " .addressButton .imgDeleteButton").html("");
256
		$("#" + sourceIdParentId + " #" + destId + " .addressButton .imgDeleteButton").html(delBtn);
257
	}else{
258
		// if no primary address is present then add new primary address
259
		addNewPriAddress(sourceId, sourceAddress);
260
 
261
		// Delete clicked swapping other address
262
		removeOtherAddress(sourceIdParentId);
263
	}
264
}
265
 
266
/**
267
	Invoked on "Delete" btn click of both primary and other address
268
*/
269
function delAddress(sourceId){
270
	ajaxDeleteAddress(sourceId.substring(7,sourceId.length));
271
 
272
 
273
	var sourceIdParentId = $("#" + sourceId).parent().attr("id");
274
 
275
	// on delete of primary address
276
	if(sourceIdParentId == "A1"){
277
 
278
		$("#" + sourceIdParentId + " #" + sourceId + " .address").html("");
279
 
280
		// make first other address as primary address if available
281
		if(otherAddressCount === 0){
282
			$("#" + sourceIdParentId).html("");
283
			$(".noAddress1").slideDown('slow');
284
		}else{
285
 
286
			// make first other address to primary and delete that
287
			var destId = $("#A1 div").attr("id");
288
 
289
			sourceIdParentId = $("#addressContainer div").attr("id");
290
			sourceId = $("#" + sourceIdParentId + " div").attr("id");		
291
 
292
			var sourceAddress = $("#" + sourceId + " .address").html();
293
 
294
			// Replace destination Id and its content with source
295
			$("#A1 #" + destId).attr("id", sourceId);
296
			$("#A1 #" + sourceId + " .address").html(sourceAddress);
297
 
298
			// Replace primary address delete button parameter with source id
299
			var priDelBtn = "<input onclick=delAddress('" + sourceId + "') value='Delete' class='button' type='button' />";
300
			$("#A1 #" + sourceId + " .addressButton .imgDeleteButton .left .right").html("");
301
			$("#A1 #" + sourceId + " .addressButton .imgDeleteButton .left .right").html(priDelBtn);
302
 
303
			ajaxMakePrimary(sourceId.substring(7,sourceId.length));
304
			// Remove first other address
305
			removeOtherAddress(sourceIdParentId);
306
		}
307
	}else{
308
		removeOtherAddress(sourceIdParentId);
309
	}
310
}
311
 
312
/**
313
	Invoked from delAddress() function
314
*/
315
function removeOtherAddress(sourceIdParentId){
316
 
317
	$("#" + sourceIdParentId).slideUp('slow', function(){
318
		$(this).remove();
319
		otherAddressCount--;
320
 
321
		if(otherAddressCount === 0){
322
			$(".noAddress2").slideDown('slow');
323
		}
324
	});
325
}
326
 
327
/**
328
	Invoked on "Save and make primary Address " & "Save" btn
329
*/
330
function saveAddress(str){
331
 
332
	// form validation
333
	var nameVal 	= jQuery.trim($("#shippingAddress #txtName").val());
334
	var add1Val 	= jQuery.trim($("#shippingAddress #txtAddress").val());
335
	var stateVal 	= $("#shippingAddress #state option:selected").val();
336
	var cityVal 	= $("#shippingAddress #txtCity").val();
337
	var pinCodeVal  = jQuery.trim($("#shippingAddress #txtPinCode").val());
338
	var phoneVal	= jQuery.trim($("#shippingAddress #txtPhone").val());
339
 
340
 
341
	if(nameVal.length === 0){
342
		alert("Please enter name.");
343
 
344
		$("#shippingAddress #txtName").focus();
345
		return false;	
346
	}else if(add1Val.length === 0){
347
		alert("Please enter address.");
348
 
349
		$("#shippingAddress #txtAddress").focus();
350
		return false;	
351
	}else if(stateVal == "0"){
352
		alert("Please select state.");
353
 
354
		$("#shippingAddress #state").focus();
355
		return false;	
356
	}else if(phoneVal.length === 0){
357
		alert("Please enter phone no.");
358
 
359
		$("#shippingAddress #txtPhone").focus();
360
		return false;		
361
	}else if(pinCodeVal.length == 0){
362
		alert("Please enter pin code.");
363
 
364
		$("#shippingAddress #txtPinCode").focus();
365
		return false;	
366
	}else if(cityVal.length == 0){
367
		alert("Please enter city.");
368
 
369
		$("#shippingAddress #txtCity").focus();
370
		return false;	
371
	}else{
372
		if(str == "makePrimary"){
373
			addPriAddress();
374
		}else if(str == "save"){
375
			addOtherAddress("save", 0 , "");
376
		}
377
	}
378
 
379
	// Reset Form
380
	resetShipingAddress();
381
}
382
 
383
/**
384
	Invoke from saveAddress() function
385
*/
386
function addPriAddress(){
387
    var priAddressID = "";
388
    var priAddress = "";
389
	// if primary address is present then take backup of primary id and address to make as first other id and address
390
	if($(".noAddress1:visible").length != 1){
391
		priAddressID 	= $("#A1 div").attr("id");
392
		priAddress	= $("#" + priAddressID + " .address").html();
393
	}
394
 
395
	var priName 	= $("#shippingAddress #txtName").val();
396
	var priAdd1 	= $("#shippingAddress #txtAddress").val();
397
	var priAdd2 	= $("#shippingAddress #txtAddress2").val();
398
	var priCity 	= $("#shippingAddress #txtCity").val();
399
	var priPinCode 	= $("#shippingAddress #txtPinCode").val();
400
	var priState 	= $("#shippingAddress #state option:selected").val();
401
	var priPhone 	= $("#shippingAddress #txtPhone").val();
402
	var priEmail = "";
403
 
404
	var newPriAddressID	= "address" + (++lastAddressID);	// create new primary id to get newly inserted data from form	
405
 
406
	var newPriAddress = priName + "<br />" + priAdd1 + "<br />" + priAdd2 + "<br />" + priCity + "<br />" + priPinCode + "<br />" + priState + "<br /> Phone: " + priPhone ;
407
 
408
	// if primary address is present then add new primary address and shift old primary address as first other address
409
	if($(".noAddress1:visible").length != 1){
410
		$("#A1 #" + priAddressID).attr("id", newPriAddressID);
411
		$("#A1 #" + newPriAddressID + " .address").html(newPriAddress);
412
 
413
		// Replace primary address delete button parameter with new primary id
414
		var priDelBtn = "<input onclick=delAddress('" + newPriAddressID + "') value='Delete' class='button' type='button' />";
415
		$("#A1 #" + newPriAddressID + " .addressButton .imgDeleteButton .left .right").html("");
416
		$("#A1 #" + newPriAddressID + " .addressButton .imgDeleteButton .left .right").html(priDelBtn);
417
 
418
		ajaxAddAddress("true", priName, priAdd1, priAdd2, priCity, priState, priPinCode, priPhone, priEmail);
419
 
420
		// shift old primary address to make first other address
421
		addOtherAddress("", priAddressID, priAddress);
422
	}else{
423
 
424
		// if no primary address is present then add new primary address
425
		addNewPriAddress(newPriAddressID, newPriAddress);
426
	}
427
}
428
 
429
/**
430
	Invoke from saveAddress(), addPriAddress() functions
431
 
432
	params:
433
		> condition = save	// for saving other address only
434
		> id 				// for old primary id
435
		> address 			// for old primary address
436
*/
437
function addOtherAddress(condition, id, address){
438
	var newContainerID 	= "A" + (++lastContainerID);
439
	var newAddressID	= (condition == "save") ? "address" + (++lastAddressID) : id;
440
	var container 		= "";
441
 
442
	if(condition == "save"){
443
		var otherName		= $("#shippingAddress #txtName").val();
444
		var otherAdd1 		= $("#shippingAddress #txtAddress").val();
445
		var otherAdd2 		= $("#shippingAddress #txtAddress2").val();
446
		var otherCity 		= $("#shippingAddress #txtCity").val();
447
		var otherPinCode 	= $("#shippingAddress #txtPinCode").val();
448
		var otherState 		= $("#shippingAddress #state option:selected").val();
449
		var otherPhone 	= $("#shippingAddress #txtPhone").val();
450
		var otherEmail = "";
451
		var otherAddress = otherName + "<br />" + otherAdd1 + "<br />" + otherAdd2 + "<br />" + otherCity + "<br />" + otherPinCode + "<br />" + otherState + "<br /> Phone: " + otherPhone;
452
 
453
		ajaxAddAddress("false", otherName, otherAdd1, otherAdd2, otherCity, otherState, otherPinCode, otherPhone, otherEmail);
454
 
455
	}else{
456
		otherAddress = address;
457
	}
458
 
459
	container = '<div id="' + newContainerID + '">';
460
		container += '	<div id="' + newAddressID + '">';
461
 
462
			if(condition == "save"){
463
				container += (otherAddressCount != 0) ? "<br /><br />" : "";
464
			}
465
 
466
			// Address
467
			container += '<div class="address">';
468
			container += otherAddress;
469
			container += '</div>';
470
 
471
			// Buttons
472
			container += '<div class="addressButton">';
473
 
474
				// Delete button
475
				container += '<div class="imgDeleteButton deleteWidth">';
476
				container += '	<div class="left">';
477
				container += '	<div class="right">';
478
				container += '		<input type="button" onclick="delAddress(\'' + newAddressID + '\');" value="Delete" class="button" />';
479
				container += '	</div>';
480
				container += '	</div>';
481
				container += '</div>';
482
 
483
				// Make primary address button
484
				container += '<div class="imgEnableButton priAddressWidth">';
485
				container += '		<div class="left">';
486
				container += '		<div class="right">';
487
				container += '			 <input type="button" onclick="makePriAddress(\'' + newAddressID + '\');" value="Make Primary Address" class="button" />';
488
				container += '		</div>';
489
				container += '		</div>';
490
				container += '</div>';
491
				container += '<div class="clearBoth"></div>';
492
 
493
			container += '</div>';
494
 
495
			if(condition != "save"){
496
				container += (otherAddressCount != 0)? "<br /><br />" : "";
497
			}
498
		container += '</div>';
499
	container += '</div>';
500
 
501
	if(condition == "save"){
502
		$("#addressContainer").append(container);	// to save other address
503
	}else{	
504
		$("#addressContainer").prepend(container);	// to save old primary address as first other address
505
	}
506
 
507
	if(otherAddressCount == 0){
508
		$(".noAddress2").hide();
509
	}
510
 
511
	otherAddressCount++;
512
 
513
 
514
}
515
 
516
 
517
/**
518
	Inovke from makePriAddress(), addPriAddress() function
519
*/
520
function addNewPriAddress(id, address){
521
 
522
	if($(".noAddress1:visible").length == 1){
523
		$(".noAddress1").hide();
524
	}
525
 
526
	var container = "";
527
	container = '<div id="' + id + '">';
528
 
529
		// Address
530
		container += '<div class="address">';
531
		container += address;
532
		container += '</div>';
533
 
534
		// Buttons
535
		container += '<div class="addressButton">';
536
		container += '	<div class="imgDeleteButton deleteWidth">';
537
		container += '		<div class="left"><div class="right">';
538
		container += '			<input type="button" class="button" value="Delete" onclick="delAddress(\'' + id + '\');">';
539
		container += '		</div></div>';
540
		container += '	</div>';
541
		container += '</div>';
542
 
543
	container += '</div>';
544
 
545
	$("#A1").append(container);
546
 
547
}
548
 
549
/**
550
	Add address for Shipping Address page
551
*/
552
function addNewAddress(){
553
	// Disable first addAddress button
554
	$("#addAddress1").removeClass('imgEnableButton').addClass('imgDisableButton');
555
	$("#addAddress1 input[name='addAddress1']").attr('disabled','disabled');
556
 
557
	// Hide second addAddress button
558
	$("#addAddress2").hide();
559
 
560
	// Show new address form
561
	$("#addNewAddress").show();
562
 
563
	$("#addNewAddress #txtName").focus();
564
}
565
 
566
/**
567
 * 
568
	Reset Shiping address for Shipping Address page
569
*/
570
function resetShipingAddress(){
571
	// Enable first addAddress button
572
	$("#addAddress1").removeClass('imgDisableButton').addClass('imgEnableButton');
573
	$("#addAddress1 input[disabled='']").removeAttr('disabled');
574
 
575
	// Show second addAddress button
576
	$("#addAddress2").show();
577
 
578
	// Hide add new address form
579
	$("#addNewAddress").hide();
580
 
581
	$("#shippingAddress #txtName").val("");
582
	$("#shippingAddress #txtAddress").val("");
583
	$("#shippingAddress #txtAddress2").val("");
584
	$("#shippingAddress #txtCity").val("");
585
	$("#shippingAddress #state option[value='0']").attr('selected', 'selected');
586
	$("#shippingAddress #txtPinCode").val("");
587
	$("#shippingAddress #txtPhone").val("");
588
}
589
 
590
 
591
function ajaxAddAddress(isprimary, name, add1, add2, city, state, pin, phone, email){
592
	jQuery.ajax({
593
		  type: "POST",
594
		  url: "/address",
595
		  data: "action=add&default="+isprimary+"&name="+name+"&line1="+add1+"&line2="+add2+"&city="+city
596
		  +"&state="+state+"&pincode="+pin+"&phone="+phone+"&country=India",
597
		  success: function(msg){
598
		  }
599
	});
600
}
601
 
602
function ajaxMakePrimary(addressid){
603
	jQuery.ajax({
604
		  type: "POST",
605
		  url: "/address",
606
		  data: "action=setdefault&addressid="+addressid,
607
		  success: function(msg){
608
		  }
609
	});
610
}
611
 
612
function ajaxDeleteAddress(addressid){
613
	jQuery.ajax({
614
		  type: "POST",
615
		  url: "/address",
616
		  data: "action=delete&addressid="+addressid,
617
		  success: function(msg){
618
		  }
619
	});
620
}