Subversion Repositories SmartDukaan

Rev

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

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