Subversion Repositories SmartDukaan

Rev

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