Subversion Repositories SmartDukaan

Rev

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