Subversion Repositories SmartDukaan

Rev

Rev 5716 | Rev 5747 | 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
	}
77
	$('#store-address-header-text').text(" Select a pickup point nearest to you (" + count + " available)");
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
 
275
		alert("Ajax call to delete" + sourceId);
276
 
277
		// make first other address as primary address if available
1048 chandransh 278
		if(otherAddressCount === 0){
595 rajveer 279
			$("#" + sourceIdParentId).html("");
280
			$(".noAddress1").slideDown('slow');
281
		}else{
282
 
283
			// make first other address to primary and delete that
284
			var destId = $("#A1 div").attr("id");
285
 
1048 chandransh 286
			sourceIdParentId = $("#addressContainer div").attr("id");
595 rajveer 287
			sourceId = $("#" + sourceIdParentId + " div").attr("id");		
288
 
289
			var sourceAddress = $("#" + sourceId + " .address").html();
290
 
291
			// Replace destination Id and its content with source
292
			$("#A1 #" + destId).attr("id", sourceId);
293
			$("#A1 #" + sourceId + " .address").html(sourceAddress);
294
 
295
			// Replace primary address delete button parameter with source id
296
			var priDelBtn = "<input onclick=delAddress('" + sourceId + "') value='Delete' class='button' type='button' />";
297
			$("#A1 #" + sourceId + " .addressButton .imgDeleteButton .left .right").html("");
298
			$("#A1 #" + sourceId + " .addressButton .imgDeleteButton .left .right").html(priDelBtn);
299
 
300
			ajaxMakePrimary(sourceId.substring(7,sourceId.length));
301
			// Remove first other address
302
			removeOtherAddress(sourceIdParentId);
303
		}
304
	}else{
305
		removeOtherAddress(sourceIdParentId);
306
	}
307
}
308
 
309
/**
310
	Invoked from delAddress() function
311
*/
312
function removeOtherAddress(sourceIdParentId){
313
 
314
	$("#" + sourceIdParentId).slideUp('slow', function(){
315
		$(this).remove();
316
		otherAddressCount--;
317
 
1048 chandransh 318
		if(otherAddressCount === 0){
595 rajveer 319
			$(".noAddress2").slideDown('slow');
320
		}
321
	});
322
}
323
 
324
/**
325
	Invoked on "Save and make primary Address " & "Save" btn
326
*/
327
function saveAddress(str){
328
 
329
	// form validation
330
	var nameVal 	= jQuery.trim($("#shippingAddress #txtName").val());
331
	var add1Val 	= jQuery.trim($("#shippingAddress #txtAddress").val());
332
	var stateVal 	= $("#shippingAddress #state option:selected").val();
333
	var cityVal 	= $("#shippingAddress #txtCity").val();
334
	var pinCodeVal  = jQuery.trim($("#shippingAddress #txtPinCode").val());
335
	var phoneVal	= jQuery.trim($("#shippingAddress #txtPhone").val());
336
 
337
 
1048 chandransh 338
	if(nameVal.length === 0){
595 rajveer 339
		alert("Please enter name.");
340
 
341
		$("#shippingAddress #txtName").focus();
342
		return false;	
1048 chandransh 343
	}else if(add1Val.length === 0){
595 rajveer 344
		alert("Please enter address.");
345
 
346
		$("#shippingAddress #txtAddress").focus();
347
		return false;	
348
	}else if(stateVal == "0"){
349
		alert("Please select state.");
350
 
351
		$("#shippingAddress #state").focus();
352
		return false;	
1048 chandransh 353
	}else if(phoneVal.length === 0){
595 rajveer 354
		alert("Please enter phone no.");
355
 
356
		$("#shippingAddress #txtPhone").focus();
357
		return false;		
358
	}else if(pinCodeVal.length == 0){
359
		alert("Please enter pin code.");
360
 
361
		$("#shippingAddress #txtPinCode").focus();
362
		return false;	
363
	}else if(cityVal.length == 0){
364
		alert("Please enter city.");
365
 
366
		$("#shippingAddress #txtCity").focus();
367
		return false;	
368
	}else{
369
		if(str == "makePrimary"){
370
			addPriAddress();
371
		}else if(str == "save"){
372
			addOtherAddress("save", 0 , "");
373
		}
374
	}
375
 
376
	// Reset Form
377
	resetShipingAddress();
378
}
379
 
380
/**
381
	Invoke from saveAddress() function
382
*/
383
function addPriAddress(){
1048 chandransh 384
    var priAddressID = "";
385
    var priAddress = "";
595 rajveer 386
	// if primary address is present then take backup of primary id and address to make as first other id and address
387
	if($(".noAddress1:visible").length != 1){
1048 chandransh 388
		priAddressID 	= $("#A1 div").attr("id");
389
		priAddress	= $("#" + priAddressID + " .address").html();
595 rajveer 390
	}
391
 
392
	var priName 	= $("#shippingAddress #txtName").val();
393
	var priAdd1 	= $("#shippingAddress #txtAddress").val();
394
	var priAdd2 	= $("#shippingAddress #txtAddress2").val();
395
	var priCity 	= $("#shippingAddress #txtCity").val();
396
	var priPinCode 	= $("#shippingAddress #txtPinCode").val();
397
	var priState 	= $("#shippingAddress #state option:selected").val();
398
	var priPhone 	= $("#shippingAddress #txtPhone").val();
399
	var priEmail = "";
400
 
401
	var newPriAddressID	= "address" + (++lastAddressID);	// create new primary id to get newly inserted data from form	
402
 
1048 chandransh 403
	var newPriAddress = priName + "<br />" + priAdd1 + "<br />" + priAdd2 + "<br />" + priCity + "<br />" + priPinCode + "<br />" + priState + "<br /> Phone: " + priPhone ;
595 rajveer 404
 
405
	// if primary address is present then add new primary address and shift old primary address as first other address
406
	if($(".noAddress1:visible").length != 1){
407
		$("#A1 #" + priAddressID).attr("id", newPriAddressID);
408
		$("#A1 #" + newPriAddressID + " .address").html(newPriAddress);
409
 
410
		// Replace primary address delete button parameter with new primary id
411
		var priDelBtn = "<input onclick=delAddress('" + newPriAddressID + "') value='Delete' class='button' type='button' />";
412
		$("#A1 #" + newPriAddressID + " .addressButton .imgDeleteButton .left .right").html("");
413
		$("#A1 #" + newPriAddressID + " .addressButton .imgDeleteButton .left .right").html(priDelBtn);
414
 
415
		ajaxAddAddress("true", priName, priAdd1, priAdd2, priCity, priState, priPinCode, priPhone, priEmail);
416
 
417
		// shift old primary address to make first other address
418
		addOtherAddress("", priAddressID, priAddress);
419
	}else{
420
 
421
		// if no primary address is present then add new primary address
422
		addNewPriAddress(newPriAddressID, newPriAddress);
423
	}
424
}
425
 
426
/**
427
	Invoke from saveAddress(), addPriAddress() functions
428
 
429
	params:
430
		> condition = save	// for saving other address only
431
		> id 				// for old primary id
432
		> address 			// for old primary address
433
*/
434
function addOtherAddress(condition, id, address){
435
	var newContainerID 	= "A" + (++lastContainerID);
436
	var newAddressID	= (condition == "save") ? "address" + (++lastAddressID) : id;
437
	var container 		= "";
438
 
439
	if(condition == "save"){
440
		var otherName		= $("#shippingAddress #txtName").val();
441
		var otherAdd1 		= $("#shippingAddress #txtAddress").val();
442
		var otherAdd2 		= $("#shippingAddress #txtAddress2").val();
443
		var otherCity 		= $("#shippingAddress #txtCity").val();
444
		var otherPinCode 	= $("#shippingAddress #txtPinCode").val();
445
		var otherState 		= $("#shippingAddress #state option:selected").val();
446
		var otherPhone 	= $("#shippingAddress #txtPhone").val();
447
		var otherEmail = "";
448
		var otherAddress = otherName + "<br />" + otherAdd1 + "<br />" + otherAdd2 + "<br />" + otherCity + "<br />" + otherPinCode + "<br />" + otherState + "<br /> Phone: " + otherPhone;
449
 
450
		ajaxAddAddress("false", otherName, otherAdd1, otherAdd2, otherCity, otherState, otherPinCode, otherPhone, otherEmail);
451
 
452
	}else{
453
		otherAddress = address;
454
	}
455
 
456
	container = '<div id="' + newContainerID + '">';
457
		container += '	<div id="' + newAddressID + '">';
458
 
459
			if(condition == "save"){
460
				container += (otherAddressCount != 0) ? "<br /><br />" : "";
461
			}
462
 
463
			// Address
464
			container += '<div class="address">';
465
			container += otherAddress;
466
			container += '</div>';
467
 
468
			// Buttons
469
			container += '<div class="addressButton">';
470
 
471
				// Delete button
472
				container += '<div class="imgDeleteButton deleteWidth">';
473
				container += '	<div class="left">';
474
				container += '	<div class="right">';
475
				container += '		<input type="button" onclick="delAddress(\'' + newAddressID + '\');" value="Delete" class="button" />';
476
				container += '	</div>';
477
				container += '	</div>';
478
				container += '</div>';
479
 
480
				// Make primary address button
481
				container += '<div class="imgEnableButton priAddressWidth">';
482
				container += '		<div class="left">';
483
				container += '		<div class="right">';
484
				container += '			 <input type="button" onclick="makePriAddress(\'' + newAddressID + '\');" value="Make Primary Address" class="button" />';
485
				container += '		</div>';
486
				container += '		</div>';
487
				container += '</div>';
488
				container += '<div class="clearBoth"></div>';
489
 
490
			container += '</div>';
491
 
492
			if(condition != "save"){
493
				container += (otherAddressCount != 0)? "<br /><br />" : "";
494
			}
495
		container += '</div>';
496
	container += '</div>';
497
 
498
	if(condition == "save"){
499
		$("#addressContainer").append(container);	// to save other address
500
	}else{	
501
		$("#addressContainer").prepend(container);	// to save old primary address as first other address
502
	}
503
 
504
	if(otherAddressCount == 0){
505
		$(".noAddress2").hide();
506
	}
507
 
508
	otherAddressCount++;
509
 
510
 
511
}
512
 
513
 
514
/**
515
	Inovke from makePriAddress(), addPriAddress() function
516
*/
517
function addNewPriAddress(id, address){
518
 
519
	if($(".noAddress1:visible").length == 1){
520
		$(".noAddress1").hide();
521
	}
522
 
523
	var container = "";
524
	container = '<div id="' + id + '">';
525
 
526
		// Address
527
		container += '<div class="address">';
528
		container += address;
529
		container += '</div>';
530
 
531
		// Buttons
532
		container += '<div class="addressButton">';
533
		container += '	<div class="imgDeleteButton deleteWidth">';
534
		container += '		<div class="left"><div class="right">';
535
		container += '			<input type="button" class="button" value="Delete" onclick="delAddress(\'' + id + '\');">';
536
		container += '		</div></div>';
537
		container += '	</div>';
538
		container += '</div>';
539
 
540
	container += '</div>';
541
 
542
	$("#A1").append(container);
543
 
544
}
545
 
546
/**
547
	Add address for Shipping Address page
548
*/
549
function addNewAddress(){
550
	// Disable first addAddress button
551
	$("#addAddress1").removeClass('imgEnableButton').addClass('imgDisableButton');
552
	$("#addAddress1 input[name='addAddress1']").attr('disabled','disabled');
553
 
554
	// Hide second addAddress button
555
	$("#addAddress2").hide();
556
 
557
	// Show new address form
558
	$("#addNewAddress").show();
559
 
560
	$("#addNewAddress #txtName").focus();
561
}
562
 
563
/**
564
 * 
565
	Reset Shiping address for Shipping Address page
566
*/
567
function resetShipingAddress(){
568
	// Enable first addAddress button
569
	$("#addAddress1").removeClass('imgDisableButton').addClass('imgEnableButton');
570
	$("#addAddress1 input[disabled='']").removeAttr('disabled');
571
 
572
	// Show second addAddress button
573
	$("#addAddress2").show();
574
 
575
	// Hide add new address form
576
	$("#addNewAddress").hide();
577
 
578
	$("#shippingAddress #txtName").val("");
579
	$("#shippingAddress #txtAddress").val("");
580
	$("#shippingAddress #txtAddress2").val("");
581
	$("#shippingAddress #txtCity").val("");
582
	$("#shippingAddress #state option[value='0']").attr('selected', 'selected');
583
	$("#shippingAddress #txtPinCode").val("");
584
	$("#shippingAddress #txtPhone").val("");
585
}
586
 
587
 
588
function ajaxAddAddress(isprimary, name, add1, add2, city, state, pin, phone, email){
589
	jQuery.ajax({
590
		  type: "POST",
1919 rajveer 591
		  url: "/address",
595 rajveer 592
		  data: "action=add&default="+isprimary+"&name="+name+"&line1="+add1+"&line2="+add2+"&city="+city
593
		  +"&state="+state+"&pincode="+pin+"&phone="+phone+"&country=India",
594
		  success: function(msg){
595
		  }
596
	});
597
}
598
 
599
function ajaxMakePrimary(addressid){
600
	jQuery.ajax({
601
		  type: "POST",
1919 rajveer 602
		  url: "/address",
595 rajveer 603
		  data: "action=setdefault&addressid="+addressid,
604
		  success: function(msg){
605
		  }
606
	});
607
}
608
 
609
function ajaxDeleteAddress(addressid){
610
	jQuery.ajax({
611
		  type: "POST",
1919 rajveer 612
		  url: "/address",
595 rajveer 613
		  data: "action=delete&addressid="+addressid,
614
		  success: function(msg){
615
		  }
616
	});
1048 chandransh 617
}