Subversion Repositories SmartDukaan

Rev

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