Subversion Repositories SmartDukaan

Rev

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