Subversion Repositories SmartDukaan

Rev

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